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: Created Feb. 17, 2015, 12:36 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
« src/shared/Communication.h ('K') | « 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, PipeReadWrite)
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 {
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, SendReceivePipe)
40 {
41 std::unique_ptr<Communication::Pipe> server;
42 auto serverThread = std::thread([&server]
Eric 2015/02/17 18:15:13 Is it necessary to use a separate thread to initia
43 {
44 server.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE_CRE ATE));
45 });
46 std::this_thread::sleep_for(std::chrono::milliseconds(100));
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);
48 // send to the server
49 {
50 Communication::OutputBuffer message;
51 message << Communication::ProcType::PROC_IS_WHITELISTED_URL
52 << std::string("This is a test message") << 64ll << 32 << true << false;
53 client.WriteMessage(message);
54 }
55 // receive on the server and send back
56 {
57 Communication::OutputBuffer response;
58 Communication::InputBuffer message = server->ReadMessage();
59 Communication::ProcType proc;
60 message >> proc;
61 EXPECT_EQ(Communication::ProcType::PROC_IS_WHITELISTED_URL, proc);
62 response << Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED;
63
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;
65 message >> text;
66 EXPECT_EQ("This is a test message", text);
67 response << std::string("Response");
68
69 int64_t integer64;
70 message >> integer64;
71 EXPECT_EQ(64ll, integer64);
72 response << 46ll;
73
74 int32_t integer32;
75 message >> integer32;
76 EXPECT_EQ(32, integer32);
77 response << 23;
78
79 bool boolean;
80 message >> boolean;
81 EXPECT_TRUE(boolean);
82 response << false;
83
84 message >> boolean;
85 EXPECT_FALSE(boolean);
86 response << true;
87
88 server->WriteMessage(response);
89 }
90
91 // receive on the client
92 {
93 Communication::InputBuffer message = client.ReadMessage();
94 Communication::ProcType proc;
95 message >> proc;
96 EXPECT_EQ(Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED, proc);
97
98 std::string text;
99 message >> text;
100 EXPECT_EQ("Response", text);
101
102 int64_t integer64;
103 message >> integer64;
104 EXPECT_EQ(46ll, integer64);
105
106 int32_t integer32;
107 message >> integer32;
108 EXPECT_EQ(23, integer32);
109
110 bool boolean;
111 message >> boolean;
112 EXPECT_FALSE(boolean);
113
114 message >> boolean;
115 EXPECT_TRUE(boolean);
116 }
117 serverThread.join();
118 }
119
25 namespace 120 namespace
26 { 121 {
27 DWORD WINAPI CreatePipe(LPVOID param) 122 template<typename TestedType, typename AnotherType>
28 { 123 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.
29 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE); 124 {
30 return 0; 125 Communication::OutputBuffer message;
31 } 126 message << anotherTypeValue << value;
32 127 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
33 DWORD WINAPI ReceiveSend(LPVOID param) 128 TestedType received;
34 { 129 ASSERT_ANY_THROW(response >> received);
35 Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE); 130 AnotherType anotherTypeValueReceived;
36 131 response >> anotherTypeValueReceived;
37 Communication::InputBuffer message = pipe.ReadMessage(); 132 response >> received;
38 133 EXPECT_EQ(value, received);
39 std::string stringValue; 134 }
40 std::wstring wstringValue; 135 }
41 int64_t int64Value; 136
42 int32_t int32Value; 137 TEST(CommunicationTest, TypeProc)
43 bool boolValue; 138 {
44 message >> stringValue >> wstringValue >> int64Value >> int32Value >> boolVa lue; 139 SendReceiveTypes(Communication::ProcType::PROC_GET_ELEMHIDE_SELECTORS, std::st ring("non-ProcType"));
45 140 }
46 stringValue += " Received"; 141
47 wstringValue += L" \u043f\u0440\u0438\u043d\u044f\u0442\u043e"; 142 TEST(CommunicationTest, TypeString)
48 int64Value += 1; 143 {
49 int32Value += 2; 144 int32_t nonStringTypeValue = 10;
50 boolValue = !boolValue; 145 SendReceiveTypes(std::string("stringValue"), nonStringTypeValue);
51 146 }
52 Communication::OutputBuffer response; 147
53 response << stringValue << wstringValue << int64Value << int32Value << boolV alue; 148 TEST(CommunicationTest, SendWString_ReceiveWString)
Eric 2015/02/17 18:15:13 Probably better to spell out "Wide" here and below
sergei 2015/02/18 14:13:06 done.
54 pipe.WriteMessage(response); 149 {
55 150 int32_t nonStringTypeValue = 10;
56 return 0; 151 SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue);
57 } 152 }
58 } 153
59 154 TEST(CommunicationTest, SendString_ReceiveWString)
60 TEST(CommunicationTest, ConnectPipe) 155 {
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; 156 Communication::OutputBuffer message;
78 message << std::string("Foo") << std::wstring(L"Bar") << int64_t(9876543210L) << int32_t(5) << true; 157 message << std::string("test");
79 pipe.WriteMessage(message); 158 Communication::InputBuffer response(message.Get());
80 159 std::wstring received;
81 Communication::InputBuffer response = pipe.ReadMessage(); 160 response >> received;
82 161 EXPECT_EQ(L"test", received);
83 std::string stringValue; 162 }
84 std::wstring wstringValue; 163
85 int64_t int64Value; 164 TEST(CommunicationTest, SendWString_ReceiveString)
86 int32_t int32Value; 165 {
87 bool boolValue; 166 Communication::OutputBuffer message;
88 167 message << std::wstring(L"test");
89 ASSERT_ANY_THROW(response >> wstringValue); 168 Communication::InputBuffer response(message.Get());
90 ASSERT_ANY_THROW(response >> int64Value); 169 std::string received;
91 ASSERT_ANY_THROW(response >> int32Value); 170 response >> received;
92 ASSERT_ANY_THROW(response >> boolValue); 171 EXPECT_EQ("test", received);
93 172 }
94 response >> stringValue >> wstringValue; 173
95 174 TEST(CommunicationTest, SendStrings_ReceiveWStrings)
96 ASSERT_ANY_THROW(response >> stringValue); 175 {
97 ASSERT_ANY_THROW(response >> wstringValue); 176 Communication::OutputBuffer message;
98 ASSERT_ANY_THROW(response >> int32Value); 177 std::vector<std::string> strings;
99 ASSERT_ANY_THROW(response >> boolValue); 178 strings.emplace_back("test0");
100 179 strings.emplace_back("test1");
101 response >> int64Value >> int32Value >> boolValue; 180 message << strings;
102 181 Communication::InputBuffer response(message.Get());
103 ASSERT_ANY_THROW(response >> stringValue); 182 std::vector<std::wstring> received;
104 ASSERT_ANY_THROW(response >> wstringValue); 183 response >> received;
105 ASSERT_ANY_THROW(response >> int64Value); 184 ASSERT_EQ(2u, received.size());
106 ASSERT_ANY_THROW(response >> int32Value); 185 EXPECT_EQ(L"test0", received[0]);
107 ASSERT_ANY_THROW(response >> boolValue); 186 EXPECT_EQ(L"test1", received[1]);
108 187 }
109 ASSERT_EQ("Foo Received", stringValue); 188
110 ASSERT_EQ(L"Bar \u043f\u0440\u0438\u043d\u044f\u0442\u043e", wstringValue); 189 TEST(CommunicationTest, TypeInt64)
111 ASSERT_EQ(9876543211L, int64Value); 190 {
112 ASSERT_EQ(7, int32Value); 191 SendReceiveTypes<int64_t>(10, std::string("non-ProcType"));
113 ASSERT_FALSE(boolValue); 192 }
114 } 193
115 194 TEST(CommunicationTest, TypeInt32)
116 void SendReceiveStrings(const std::vector<std::string>& src) 195 {
117 { 196 SendReceiveTypes<int32_t>(10, std::string("non-ProcType"));
118 Communication::OutputBuffer outputBuffer; 197 }
119 outputBuffer << src; 198
120 Communication::InputBuffer inputBuffer(outputBuffer.Get()); 199 TEST(CommunicationTest, TypeBool)
121 std::vector<std::string> dst; 200 {
122 inputBuffer >> dst; 201 SendReceiveTypes<bool>(true, std::string("non-ProcType"));
123 auto dstSize = dst.size(); 202 SendReceiveTypes<bool>(false, std::string("non-ProcType"));
124 ASSERT_EQ(dstSize, src.size()); 203 }
125 for (auto i = 0; i < dstSize; ++i) 204
126 { 205 namespace
127 EXPECT_EQ(dst[i], src[i]); 206 {
128 } 207 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
129 } 208 {
130 209 Communication::OutputBuffer outputBuffer;
210 outputBuffer << src;
211 Communication::InputBuffer inputBuffer(outputBuffer.Get());
212 std::vector<std::string> dst;
213 inputBuffer >> dst;
214 auto dstSize = dst.size();
215 ASSERT_EQ(dstSize, src.size());
216 for (auto i = 0u; i < dstSize; ++i)
217 {
218 EXPECT_EQ(dst[i], src[i]);
219 }
220 }
221 }
222
131 TEST(InputOutputBuffersTests, EmptyStrings) 223 TEST(InputOutputBuffersTests, EmptyStrings)
132 { 224 {
133 SendReceiveStrings(std::vector<std::string>()); 225 SendReceiveStrings(std::vector<std::string>());
134 } 226 }
135 227
136 TEST(InputOutputBuffersTests, StringsWithOneValue) 228 TEST(InputOutputBuffersTests, StringsWithOneValue)
137 { 229 {
138 std::vector<std::string> src; 230 std::vector<std::string> src;
139 src.emplace_back("string1"); 231 src.emplace_back("string1");
140 SendReceiveStrings(src); 232 SendReceiveStrings(src);
141 } 233 }
142 234
143 TEST(InputOutputBuffersTests, MultivalueStrings) 235 TEST(InputOutputBuffersTests, MultivalueStrings)
144 { 236 {
145 std::vector<std::string> src; 237 std::vector<std::string> src;
146 src.emplace_back("string1"); 238 src.emplace_back("string1");
147 src.emplace_back("str2"); 239 src.emplace_back("str2");
148 src.emplace_back("value"); 240 src.emplace_back("value");
149 SendReceiveStrings(src); 241 SendReceiveStrings(src);
150 } 242 }
OLDNEW
« src/shared/Communication.h ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld