Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/CommunicationTest.cpp

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Patch Set: address some comments Created Feb. 18, 2015, 2:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/shared/Communication.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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>
21 #include <chrono>
22 #include <memory>
20 #include "../src/shared/AutoHandle.h" 23 #include "../src/shared/AutoHandle.h"
21 #include "../src/shared/Communication.h" 24 #include "../src/shared/Communication.h"
22 25
23 const std::wstring pipeName(L"\\\\.\\pipe\\adblockplustests"); 26 const std::wstring pipeName(L"\\\\.\\pipe\\adblockplustests");
24 27
28 TEST(CommunicationTest, PipeConnect)
29 {
30 auto serverThread = std::thread([]
31 {
32 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE);
33 });
34 std::this_thread::sleep_for(std::chrono::milliseconds(100));
35 ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_C ONNECT));
36 serverThread.join();
37 }
38
39 TEST(CommunicationTest, PipeSendReceive)
40 {
41 std::unique_ptr<Communication::Pipe> client;
42 auto clientThread = std::thread([&client]
43 {
44 while(!client)
45 {
46 try
47 {
48 client.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE _CONNECT));
49 }
50 catch(...)
51 {
52 std::this_thread::yield();
53 }
54 }
55 });
56 Communication::Pipe server(pipeName, Communication::Pipe::MODE_CREATE);
57 clientThread.join();
58
59 // send to the server
60 {
61 Communication::OutputBuffer message;
62 message << Communication::ProcType::PROC_IS_WHITELISTED_URL
63 << std::string("This is a test message") << 64ll << 32 << true << false;
64 client->WriteMessage(message);
65 }
66 // receive on the server and send back
67 {
68 Communication::OutputBuffer response;
69 Communication::InputBuffer message = server.ReadMessage();
70 Communication::ProcType proc;
71 message >> proc;
72 EXPECT_EQ(Communication::ProcType::PROC_IS_WHITELISTED_URL, proc);
73 response << Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED;
74
75 std::string text;
76 message >> text;
77 EXPECT_EQ("This is a test message", text);
78 response << std::string("Response");
79
80 int64_t integer64;
81 message >> integer64;
82 EXPECT_EQ(64ll, integer64);
83 response << 46ll;
84
85 int32_t integer32;
86 message >> integer32;
87 EXPECT_EQ(32, integer32);
88 response << 23;
89
90 bool boolean;
91 message >> boolean;
92 EXPECT_TRUE(boolean);
93 response << false;
94
95 message >> boolean;
96 EXPECT_FALSE(boolean);
97 response << true;
98
99 server.WriteMessage(response);
100 }
101
102 // receive on the client
103 {
104 Communication::InputBuffer message = client->ReadMessage();
105 Communication::ProcType proc;
106 message >> proc;
107 EXPECT_EQ(Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED, proc);
108
109 std::string text;
110 message >> text;
111 EXPECT_EQ("Response", text);
112
113 int64_t integer64;
114 message >> integer64;
115 EXPECT_EQ(46ll, integer64);
116
117 int32_t integer32;
118 message >> integer32;
119 EXPECT_EQ(23, integer32);
120
121 bool boolean;
122 message >> boolean;
123 EXPECT_FALSE(boolean);
124
125 message >> boolean;
126 EXPECT_TRUE(boolean);
127 }
128 }
129
25 namespace 130 namespace
26 { 131 {
27 DWORD WINAPI CreatePipe(LPVOID param) 132 template<typename TestedType, typename AnotherType>
28 { 133 void SendReceiveTypes(TestedType value, AnotherType anotherTypeValue)
29 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE); 134 {
30 return 0; 135 Communication::OutputBuffer message;
31 } 136 message << anotherTypeValue << value;
32 137 Communication::InputBuffer response(message.Get());
33 DWORD WINAPI ReceiveSend(LPVOID param) 138 TestedType received;
34 { 139 ASSERT_ANY_THROW(response >> received);
35 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE); 140 AnotherType anotherTypeValueReceived;
36 141 response >> anotherTypeValueReceived;
37 Communication::InputBuffer message = pipe.ReadMessage(); 142 response >> received;
38 143 EXPECT_EQ(value, received);
39 std::string stringValue; 144 }
40 std::wstring wstringValue; 145 }
41 int64_t int64Value; 146
42 int32_t int32Value; 147 TEST(CommunicationTest, TypeProc)
43 bool boolValue; 148 {
44 message >> stringValue >> wstringValue >> int64Value >> int32Value >> boolVa lue; 149 SendReceiveTypes(Communication::ProcType::PROC_GET_ELEMHIDE_SELECTORS, std::st ring("non-ProcType"));
45 150 }
46 stringValue += " Received"; 151
47 wstringValue += L" \u043f\u0440\u0438\u043d\u044f\u0442\u043e"; 152 TEST(CommunicationTest, TypeString)
48 int64Value += 1; 153 {
49 int32Value += 2; 154 int32_t nonStringTypeValue = 10;
50 boolValue = !boolValue; 155 SendReceiveTypes(std::string("stringValue"), nonStringTypeValue);
51 156 }
52 Communication::OutputBuffer response; 157
53 response << stringValue << wstringValue << int64Value << int32Value << boolV alue; 158 TEST(CommunicationTest, TypeStringSendWideReceiveWide)
54 pipe.WriteMessage(response); 159 {
55 160 int32_t nonStringTypeValue = 10;
56 return 0; 161 SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue);
57 } 162 }
58 } 163
59 164 TEST(CommunicationTest, SendString_ReceiveWString)
60 TEST(CommunicationTest, ConnectPipe) 165 {
61 {
62 AutoHandle thread(CreateThread(0, 0, CreatePipe, 0, 0, 0));
63
64 Sleep(100);
65
66 ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_C ONNECT));
67 }
68
69 TEST(CommunicationTest, SendReceive)
70 {
71 AutoHandle thread(CreateThread(0, 0, ReceiveSend, 0, 0, 0));
72
73 Sleep(100);
74
75 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT);
76
77 Communication::OutputBuffer message; 166 Communication::OutputBuffer message;
78 message << std::string("Foo") << std::wstring(L"Bar") << int64_t(9876543210L) << int32_t(5) << true; 167 message << std::string("test");
79 pipe.WriteMessage(message); 168 Communication::InputBuffer response(message.Get());
80 169 std::wstring received;
81 Communication::InputBuffer response = pipe.ReadMessage(); 170 response >> received;
82 171 EXPECT_EQ(L"test", received);
83 std::string stringValue; 172 }
84 std::wstring wstringValue; 173
85 int64_t int64Value; 174 TEST(CommunicationTest, SendWString_ReceiveString)
86 int32_t int32Value; 175 {
87 bool boolValue; 176 Communication::OutputBuffer message;
88 177 message << std::wstring(L"test");
89 ASSERT_ANY_THROW(response >> wstringValue); 178 Communication::InputBuffer response(message.Get());
90 ASSERT_ANY_THROW(response >> int64Value); 179 std::string received;
91 ASSERT_ANY_THROW(response >> int32Value); 180 response >> received;
92 ASSERT_ANY_THROW(response >> boolValue); 181 EXPECT_EQ("test", received);
93 182 }
94 response >> stringValue >> wstringValue; 183
95 184 TEST(CommunicationTest, SendStrings_ReceiveWStrings)
96 ASSERT_ANY_THROW(response >> stringValue); 185 {
97 ASSERT_ANY_THROW(response >> wstringValue); 186 Communication::OutputBuffer message;
98 ASSERT_ANY_THROW(response >> int32Value); 187 std::vector<std::string> strings;
99 ASSERT_ANY_THROW(response >> boolValue); 188 strings.emplace_back("test0");
100 189 strings.emplace_back("test1");
101 response >> int64Value >> int32Value >> boolValue; 190 message << strings;
102 191 Communication::InputBuffer response(message.Get());
103 ASSERT_ANY_THROW(response >> stringValue); 192 std::vector<std::wstring> received;
104 ASSERT_ANY_THROW(response >> wstringValue); 193 response >> received;
105 ASSERT_ANY_THROW(response >> int64Value); 194 ASSERT_EQ(2u, received.size());
106 ASSERT_ANY_THROW(response >> int32Value); 195 EXPECT_EQ(L"test0", received[0]);
107 ASSERT_ANY_THROW(response >> boolValue); 196 EXPECT_EQ(L"test1", received[1]);
108 197 }
109 ASSERT_EQ("Foo Received", stringValue); 198
110 ASSERT_EQ(L"Bar \u043f\u0440\u0438\u043d\u044f\u0442\u043e", wstringValue); 199 TEST(CommunicationTest, TypeInt64)
111 ASSERT_EQ(9876543211L, int64Value); 200 {
112 ASSERT_EQ(7, int32Value); 201 SendReceiveTypes<int64_t>(10, std::string("non-ProcType"));
113 ASSERT_FALSE(boolValue); 202 }
114 } 203
115 204 TEST(CommunicationTest, TypeInt32)
116 void SendReceiveStrings(const std::vector<std::string>& src) 205 {
117 { 206 SendReceiveTypes<int32_t>(10, std::string("non-ProcType"));
118 Communication::OutputBuffer outputBuffer; 207 }
119 outputBuffer << src; 208
120 Communication::InputBuffer inputBuffer(outputBuffer.Get()); 209 TEST(CommunicationTest, TypeBool)
121 std::vector<std::string> dst; 210 {
122 inputBuffer >> dst; 211 SendReceiveTypes<bool>(true, std::string("non-ProcType"));
123 auto dstSize = dst.size(); 212 SendReceiveTypes<bool>(false, std::string("non-ProcType"));
124 ASSERT_EQ(dstSize, src.size()); 213 }
125 for (auto i = 0; i < dstSize; ++i) 214
126 { 215 namespace
127 EXPECT_EQ(dst[i], src[i]); 216 {
128 } 217 void SendReceiveStrings(const std::vector<std::string>& src)
129 } 218 {
130 219 Communication::OutputBuffer outputBuffer;
131 TEST(InputOutputBuffersTests, EmptyStrings) 220 outputBuffer << src;
221 Communication::InputBuffer inputBuffer(outputBuffer.Get());
222 std::vector<std::string> dst;
223 inputBuffer >> dst;
224 auto dstSize = dst.size();
225 ASSERT_EQ(src.size(), dstSize);
226 for (auto i = 0u; i < dstSize; ++i)
227 {
228 EXPECT_EQ(src[i], dst[i]);
229 }
230 }
231
232 std::vector<std::wstring> SendReceiveStringsToWStrings(const std::vector<std:: string>& src)
233 {
234 Communication::OutputBuffer outputBuffer;
235 outputBuffer << src;
236 Communication::InputBuffer inputBuffer(outputBuffer.Get());
237 std::vector<std::wstring> dst;
238 inputBuffer >> dst;
239 return dst;
240 }
241 }
242
243 TEST(InputOutputBuffersTests, InputOutputBufferEmptyStrings)
132 { 244 {
133 SendReceiveStrings(std::vector<std::string>()); 245 SendReceiveStrings(std::vector<std::string>());
134 } 246 }
135 247
136 TEST(InputOutputBuffersTests, StringsWithOneValue) 248 TEST(InputOutputBuffersTests, InputOutputBufferEmptyStringsToWideStrings)
249 {
250 EXPECT_EQ(0u, SendReceiveStringsToWStrings(std::vector<std::string>()).size()) ;
251 }
252
253 TEST(InputOutputBuffersTests, InputOutputBufferStringsWithOneValue)
137 { 254 {
138 std::vector<std::string> src; 255 std::vector<std::string> src;
139 src.emplace_back("string1"); 256 src.emplace_back("string1");
140 SendReceiveStrings(src); 257 SendReceiveStrings(src);
141 } 258 }
142 259
143 TEST(InputOutputBuffersTests, MultivalueStrings) 260 TEST(InputOutputBuffersTests, InputOutputBufferStringsWithOneValueToWStrings)
261 {
262 std::vector<std::string> src;
263 src.emplace_back("string1");
264 auto dst = SendReceiveStringsToWStrings(src);
265 ASSERT_EQ(1u, dst.size());
266 EXPECT_EQ(L"string1", dst[0]);
267 }
268
269 TEST(InputOutputBuffersTests, InputOutputBufferWithMultivalueStrings)
144 { 270 {
145 std::vector<std::string> src; 271 std::vector<std::string> src;
146 src.emplace_back("string1"); 272 src.emplace_back("string1");
147 src.emplace_back("str2"); 273 src.emplace_back("str2");
148 src.emplace_back("value"); 274 src.emplace_back("value");
149 SendReceiveStrings(src); 275 SendReceiveStrings(src);
276 }
277
278 TEST(InputOutputBuffersTests, InputOutputBufferWithMultivalueStringsToWStrings)
279 {
280 std::vector<std::string> src;
281 src.emplace_back("string1");
282 src.emplace_back("str2");
283 src.emplace_back("value");
284 auto dst = SendReceiveStringsToWStrings(src);
285 ASSERT_EQ(3u, dst.size());
286 EXPECT_EQ(L"string1", dst[0]);
287 EXPECT_EQ(L"str2", dst[1]);
288 EXPECT_EQ(L"value", dst[2]);
150 } 289 }
OLDNEW
« no previous file with comments | « src/shared/Communication.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld