| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include <gtest/gtest.h> | 18 #include <gtest/gtest.h> |
| 19 | 19 |
| 20 #include <thread> | 20 #include <thread> |
| 21 #include <chrono> | 21 #include <chrono> |
| 22 #include <memory> | 22 #include <memory> |
| 23 #include "../src/shared/AutoHandle.h" | 23 #include "../src/shared/AutoHandle.h" |
| 24 #include "../src/shared/Communication.h" | 24 #include "../src/shared/Communication.h" |
| 25 | 25 |
| 26 const std::wstring pipeName(L"\\\\.\\pipe\\adblockplustests"); | 26 const std::wstring pipeName(L"\\\\.\\pipe\\adblockplustests"); |
| 27 | 27 |
| 28 TEST(CommunicationTest, PipeReadWrite) | 28 TEST(CommunicationTest, PipeConnect) |
|
Eric
2015/02/17 18:15:13
As long as we're improving the unit tests, can we
sergei
2015/02/18 14:13:06
Actually I would not like to improve tests in that
Eric
2015/02/18 18:12:47
OK. I am most concerned with eliminating the expli
| |
| 29 { | 29 { |
| 30 auto serverThread = std::thread([] | 30 auto serverThread = std::thread([] |
| 31 { | 31 { |
| 32 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE); | 32 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE); |
| 33 }); | 33 }); |
| 34 std::this_thread::sleep_for(std::chrono::milliseconds(100)); | 34 std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 35 ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_C ONNECT)); | 35 ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_C ONNECT)); |
| 36 serverThread.join(); | 36 serverThread.join(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 TEST(CommunicationTest, SendReceivePipe) | 39 TEST(CommunicationTest, PipeSendReceive) |
| 40 { | 40 { |
| 41 std::unique_ptr<Communication::Pipe> server; | 41 std::unique_ptr<Communication::Pipe> client; |
| 42 auto serverThread = std::thread([&server] | 42 auto clientThread = std::thread([&client] |
|
Eric
2015/02/17 18:15:13
Is it necessary to use a separate thread to initia
| |
| 43 { | 43 { |
| 44 server.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE_CRE ATE)); | 44 for (int i = 0; !client && i < 100; ++i) |
|
Eric
2015/05/14 13:53:38
There's no need to guard the loop with the term "!
sergei
2015/05/15 09:45:55
Before declaration of `clientThread` `client` is a
| |
| 45 { | |
| 46 try | |
| 47 { | |
| 48 client.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE _CONNECT)); | |
| 49 } | |
| 50 catch(...) | |
| 51 { | |
| 52 std::this_thread::sleep_for(i * std::chrono::milliseconds(20)); | |
| 53 } | |
| 54 } | |
| 45 }); | 55 }); |
| 46 std::this_thread::sleep_for(std::chrono::milliseconds(100)); | 56 Communication::Pipe server(pipeName, Communication::Pipe::MODE_CREATE); |
|
Eric
2015/02/17 18:15:13
I myself have had trouble with the Communication u
| |
| 47 Communication::Pipe client(pipeName, Communication::Pipe::MODE_CONNECT); | 57 clientThread.join(); |
| 58 ASSERT_TRUE(client); | |
|
Eric
2015/05/14 13:53:38
This assertion should appear before the declaratio
| |
| 48 // send to the server | 59 // send to the server |
| 49 { | 60 { |
| 50 Communication::OutputBuffer message; | 61 Communication::OutputBuffer message; |
| 51 message << Communication::ProcType::PROC_IS_WHITELISTED_URL | 62 message << Communication::ProcType::PROC_GET_WHITELISTING_FITER |
| 52 << std::string("This is a test message") << 64ll << 32 << true << false; | 63 << std::string("This is a test message") << 64ll << 32 << true << false; |
| 53 client.WriteMessage(message); | 64 client->WriteMessage(message); |
| 54 } | 65 } |
| 55 // receive on the server and send back | 66 // receive on the server and send back |
| 56 { | 67 { |
| 57 Communication::OutputBuffer response; | 68 Communication::OutputBuffer response; |
| 58 Communication::InputBuffer message = server->ReadMessage(); | 69 Communication::InputBuffer message = server.ReadMessage(); |
| 59 Communication::ProcType proc; | 70 Communication::ProcType proc; |
| 60 message >> proc; | 71 message >> proc; |
| 61 EXPECT_EQ(Communication::ProcType::PROC_IS_WHITELISTED_URL, proc); | 72 EXPECT_EQ(Communication::ProcType::PROC_GET_WHITELISTING_FITER, proc); |
| 62 response << Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED; | 73 response << Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED; |
| 63 | 74 |
|
Eric
2015/02/17 18:15:13
Each of these individual tests on disparate variab
sergei
2015/02/18 14:13:06
The tests of each type are below, it's not a testi
Eric
2015/02/18 18:12:47
I don't want to belabor the point here, but they'r
| |
| 64 std::string text; | 75 std::string text; |
| 65 message >> text; | 76 message >> text; |
| 66 EXPECT_EQ("This is a test message", text); | 77 EXPECT_EQ("This is a test message", text); |
| 67 response << std::string("Response"); | 78 response << std::string("Response"); |
| 68 | 79 |
| 69 int64_t integer64; | 80 int64_t integer64; |
| 70 message >> integer64; | 81 message >> integer64; |
| 71 EXPECT_EQ(64ll, integer64); | 82 EXPECT_EQ(64ll, integer64); |
| 72 response << 46ll; | 83 response << 46ll; |
| 73 | 84 |
| 74 int32_t integer32; | 85 int32_t integer32; |
| 75 message >> integer32; | 86 message >> integer32; |
| 76 EXPECT_EQ(32, integer32); | 87 EXPECT_EQ(32, integer32); |
| 77 response << 23; | 88 response << 23; |
| 78 | 89 |
| 79 bool boolean; | 90 bool boolean; |
| 80 message >> boolean; | 91 message >> boolean; |
| 81 EXPECT_TRUE(boolean); | 92 EXPECT_TRUE(boolean); |
| 82 response << false; | 93 response << false; |
| 83 | 94 |
| 84 message >> boolean; | 95 message >> boolean; |
| 85 EXPECT_FALSE(boolean); | 96 EXPECT_FALSE(boolean); |
| 86 response << true; | 97 response << true; |
| 87 | 98 |
| 88 server->WriteMessage(response); | 99 server.WriteMessage(response); |
| 89 } | 100 } |
| 90 | 101 |
| 91 // receive on the client | 102 // receive on the client |
| 92 { | 103 { |
| 93 Communication::InputBuffer message = client.ReadMessage(); | 104 Communication::InputBuffer message = client->ReadMessage(); |
| 94 Communication::ProcType proc; | 105 Communication::ProcType proc; |
| 95 message >> proc; | 106 message >> proc; |
| 96 EXPECT_EQ(Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED, proc); | 107 EXPECT_EQ(Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED, proc); |
| 97 | 108 |
| 98 std::string text; | 109 std::string text; |
| 99 message >> text; | 110 message >> text; |
| 100 EXPECT_EQ("Response", text); | 111 EXPECT_EQ("Response", text); |
| 101 | 112 |
| 102 int64_t integer64; | 113 int64_t integer64; |
| 103 message >> integer64; | 114 message >> integer64; |
| 104 EXPECT_EQ(46ll, integer64); | 115 EXPECT_EQ(46ll, integer64); |
| 105 | 116 |
| 106 int32_t integer32; | 117 int32_t integer32; |
| 107 message >> integer32; | 118 message >> integer32; |
| 108 EXPECT_EQ(23, integer32); | 119 EXPECT_EQ(23, integer32); |
| 109 | 120 |
| 110 bool boolean; | 121 bool boolean; |
| 111 message >> boolean; | 122 message >> boolean; |
| 112 EXPECT_FALSE(boolean); | 123 EXPECT_FALSE(boolean); |
| 113 | 124 |
| 114 message >> boolean; | 125 message >> boolean; |
| 115 EXPECT_TRUE(boolean); | 126 EXPECT_TRUE(boolean); |
| 116 } | 127 } |
| 117 serverThread.join(); | |
| 118 } | 128 } |
| 119 | 129 |
| 120 namespace | 130 namespace |
| 121 { | 131 { |
| 122 template<typename TestedType, typename AnotherType> | 132 template<typename TestedType, typename AnotherType> |
| 123 void SendReceiveTypes(TestedType value, AnotherType anotherTypeValue) | 133 void SendReceiveTypes(TestedType value, AnotherType anotherTypeValue) |
|
Eric
2015/02/17 18:15:13
You're combining two kinds of test here.
1) Ensure
sergei
2015/02/18 14:13:06
Before the everything was in one test, would you m
Eric
2015/02/18 18:12:47
That's fine.
| |
| 124 { | 134 { |
| 125 Communication::OutputBuffer message; | 135 Communication::OutputBuffer message; |
| 126 message << anotherTypeValue << value; | 136 message << anotherTypeValue << value; |
| 127 Communication::InputBuffer response(message.Get()); | 137 Communication::InputBuffer response(message.Get()); |
|
Eric
2015/02/17 18:15:13
I don't particularly care for Get() in the current
sergei
2015/02/18 14:13:06
I find it as overkill here.
On 2015/02/17 18:15:1
| |
| 128 TestedType received; | 138 TestedType received; |
| 129 ASSERT_ANY_THROW(response >> received); | 139 ASSERT_ANY_THROW(response >> received); |
| 130 AnotherType anotherTypeValueReceived; | 140 AnotherType anotherTypeValueReceived; |
| 131 response >> anotherTypeValueReceived; | 141 response >> anotherTypeValueReceived; |
| 132 response >> received; | 142 response >> received; |
| 133 EXPECT_EQ(value, received); | 143 EXPECT_EQ(value, received); |
| 134 } | 144 } |
| 135 } | 145 } |
| 136 | 146 |
| 137 TEST(CommunicationTest, TypeProc) | 147 TEST(CommunicationTest, TypeProc) |
| 138 { | 148 { |
| 139 SendReceiveTypes(Communication::ProcType::PROC_GET_ELEMHIDE_SELECTORS, std::st ring("non-ProcType")); | 149 SendReceiveTypes(Communication::ProcType::PROC_GET_ELEMHIDE_SELECTORS, std::st ring("non-ProcType")); |
| 140 } | 150 } |
| 141 | 151 |
| 142 TEST(CommunicationTest, TypeString) | 152 TEST(CommunicationTest, TypeString) |
| 143 { | 153 { |
| 144 int32_t nonStringTypeValue = 10; | 154 int32_t nonStringTypeValue = 10; |
| 145 SendReceiveTypes(std::string("stringValue"), nonStringTypeValue); | 155 SendReceiveTypes(std::string("stringValue"), nonStringTypeValue); |
| 146 } | 156 } |
| 147 | 157 |
| 148 TEST(CommunicationTest, SendWString_ReceiveWString) | 158 TEST(CommunicationTest, SendReceiveEmptyString) |
|
Eric
2015/02/17 18:15:13
Probably better to spell out "Wide" here and below
sergei
2015/02/18 14:13:06
done.
| |
| 159 { | |
| 160 Communication::OutputBuffer message; | |
| 161 message << std::string(); | |
| 162 Communication::InputBuffer response(message.Get()); | |
| 163 std::string received = "clean me"; | |
| 164 response >> received; | |
| 165 EXPECT_EQ("", received); | |
| 166 } | |
| 167 | |
| 168 TEST(CommunicationTest, TypeStringSendWideReceiveWide) | |
| 149 { | 169 { |
| 150 int32_t nonStringTypeValue = 10; | 170 int32_t nonStringTypeValue = 10; |
| 151 SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue); | 171 SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue); |
| 152 } | 172 } |
| 153 | 173 |
| 154 TEST(CommunicationTest, SendString_ReceiveWString) | 174 TEST(CommunicationTest, SendString_ReceiveWString) |
| 155 { | 175 { |
| 156 Communication::OutputBuffer message; | 176 Communication::OutputBuffer message; |
| 157 message << std::string("test"); | 177 message << std::string("test"); |
| 158 Communication::InputBuffer response(message.Get()); | 178 Communication::InputBuffer response(message.Get()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 } | 217 } |
| 198 | 218 |
| 199 TEST(CommunicationTest, TypeBool) | 219 TEST(CommunicationTest, TypeBool) |
| 200 { | 220 { |
| 201 SendReceiveTypes<bool>(true, std::string("non-ProcType")); | 221 SendReceiveTypes<bool>(true, std::string("non-ProcType")); |
| 202 SendReceiveTypes<bool>(false, std::string("non-ProcType")); | 222 SendReceiveTypes<bool>(false, std::string("non-ProcType")); |
| 203 } | 223 } |
| 204 | 224 |
| 205 namespace | 225 namespace |
| 206 { | 226 { |
| 207 void SendReceiveStrings(const std::vector<std::string>& src) | 227 void SendReceiveStrings(const std::vector<std::string>& src) |
|
Eric
2015/02/17 18:15:13
We should test narrow-wide conversions here as wel
sergei
2015/02/18 14:13:06
added
| |
| 208 { | 228 { |
| 209 Communication::OutputBuffer outputBuffer; | 229 Communication::OutputBuffer outputBuffer; |
| 210 outputBuffer << src; | 230 outputBuffer << src; |
| 211 Communication::InputBuffer inputBuffer(outputBuffer.Get()); | 231 Communication::InputBuffer inputBuffer(outputBuffer.Get()); |
| 212 std::vector<std::string> dst; | 232 std::vector<std::string> dst; |
| 213 inputBuffer >> dst; | 233 inputBuffer >> dst; |
| 214 auto dstSize = dst.size(); | 234 auto dstSize = dst.size(); |
| 215 ASSERT_EQ(dstSize, src.size()); | 235 ASSERT_EQ(src.size(), dstSize); |
| 216 for (auto i = 0u; i < dstSize; ++i) | 236 for (auto i = 0u; i < dstSize; ++i) |
| 217 { | 237 { |
| 218 EXPECT_EQ(dst[i], src[i]); | 238 EXPECT_EQ(src[i], dst[i]); |
| 219 } | 239 } |
| 220 } | 240 } |
| 221 } | 241 |
| 222 | 242 std::vector<std::wstring> SendReceiveStringsToWStrings(const std::vector<std:: string>& src) |
| 223 TEST(InputOutputBuffersTests, EmptyStrings) | 243 { |
| 244 Communication::OutputBuffer outputBuffer; | |
| 245 outputBuffer << src; | |
| 246 Communication::InputBuffer inputBuffer(outputBuffer.Get()); | |
| 247 std::vector<std::wstring> dst; | |
| 248 inputBuffer >> dst; | |
| 249 return dst; | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 TEST(InputOutputBuffersTest, EmptyStrings) | |
| 224 { | 254 { |
| 225 SendReceiveStrings(std::vector<std::string>()); | 255 SendReceiveStrings(std::vector<std::string>()); |
| 226 } | 256 } |
| 227 | 257 |
| 228 TEST(InputOutputBuffersTests, StringsWithOneValue) | 258 TEST(InputOutputBuffersTest, EmptyStringsToWideStrings) |
| 259 { | |
| 260 EXPECT_EQ(0u, SendReceiveStringsToWStrings(std::vector<std::string>()).size()) ; | |
| 261 } | |
| 262 | |
| 263 TEST(InputOutputBuffersTest, StringsWithOneValue) | |
| 229 { | 264 { |
| 230 std::vector<std::string> src; | 265 std::vector<std::string> src; |
| 231 src.emplace_back("string1"); | 266 src.emplace_back("string1"); |
| 232 SendReceiveStrings(src); | 267 SendReceiveStrings(src); |
| 233 } | 268 } |
| 234 | 269 |
| 235 TEST(InputOutputBuffersTests, MultivalueStrings) | 270 TEST(InputOutputBuffersTest, StringsWithOneValueToWStrings) |
| 271 { | |
| 272 std::vector<std::string> src; | |
| 273 src.emplace_back("string1"); | |
| 274 auto dst = SendReceiveStringsToWStrings(src); | |
| 275 ASSERT_EQ(1u, dst.size()); | |
| 276 EXPECT_EQ(L"string1", dst[0]); | |
| 277 } | |
| 278 | |
| 279 TEST(InputOutputBuffersTest, WithMultivalueStrings) | |
| 236 { | 280 { |
| 237 std::vector<std::string> src; | 281 std::vector<std::string> src; |
| 238 src.emplace_back("string1"); | 282 src.emplace_back("string1"); |
| 239 src.emplace_back("str2"); | 283 src.emplace_back("str2"); |
| 240 src.emplace_back("value"); | 284 src.emplace_back("value"); |
| 241 SendReceiveStrings(src); | 285 SendReceiveStrings(src); |
| 286 } | |
| 287 | |
| 288 TEST(InputOutputBuffersTest, WithMultivalueStringsToWStrings) | |
| 289 { | |
| 290 std::vector<std::string> src; | |
| 291 src.emplace_back("string1"); | |
| 292 src.emplace_back("str2"); | |
| 293 src.emplace_back("value"); | |
| 294 auto dst = SendReceiveStringsToWStrings(src); | |
| 295 ASSERT_EQ(3u, dst.size()); | |
| 296 EXPECT_EQ(L"string1", dst[0]); | |
| 297 EXPECT_EQ(L"str2", dst[1]); | |
| 298 EXPECT_EQ(L"value", dst[2]); | |
| 242 } | 299 } |
| LEFT | RIGHT |