Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: content/browser/renderer_host/p2p/socket_host_tcp_unittest.cc

Issue 16516003: SSLTCP (pseudo-SSL with fake handshake and unencrypted data) support for p2p socket. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
6 6
7 #include <deque> 7 #include <deque>
8 8
9 #include "base/sys_byteorder.h" 9 #include "base/sys_byteorder.h"
10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" 10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h"
11 #include "content/browser/renderer_host/p2p/ssltcp_helper.h"
11 #include "net/socket/stream_socket.h" 12 #include "net/socket/stream_socket.h"
12 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 using ::testing::_; 16 using ::testing::_;
16 using ::testing::DeleteArg; 17 using ::testing::DeleteArg;
17 using ::testing::DoAll; 18 using ::testing::DoAll;
18 using ::testing::Return; 19 using ::testing::Return;
19 20
20 namespace content { 21 namespace content {
21 22
22 class P2PSocketHostTcpTest : public testing::Test { 23 class P2PSocketHostTcpTest : public testing::Test {
23 protected: 24 protected:
24 virtual void SetUp() OVERRIDE { 25 void SetUp(bool ssl) {
25 EXPECT_CALL(sender_, Send( 26 EXPECT_CALL(sender_, Send(
26 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) 27 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID))))
27 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 28 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
28 29
29 socket_host_.reset(new P2PSocketHostTcp(&sender_, 0)); 30 socket_host_.reset(new P2PSocketHostTcp(&sender_, 0, ssl));
30 socket_ = new FakeSocket(&sent_data_); 31 socket_ = new FakeSocket(&sent_data_);
31 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); 32 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1));
32 socket_host_->socket_.reset(socket_); 33 socket_host_->socket_.reset(socket_);
33 34
34 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); 35 dest_ = ParseAddress(kTestIpAddress1, kTestPort1);
35 36
36 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); 37 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1);
37 38
38 socket_host_->remote_address_ = dest_; 39 socket_host_->remote_address_ = dest_;
39 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; 40 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING;
(...skipping 14 matching lines...) Expand all
54 MockIPCSender sender_; 55 MockIPCSender sender_;
55 56
56 net::IPEndPoint local_address_; 57 net::IPEndPoint local_address_;
57 58
58 net::IPEndPoint dest_; 59 net::IPEndPoint dest_;
59 net::IPEndPoint dest2_; 60 net::IPEndPoint dest2_;
60 }; 61 };
61 62
62 class P2PSocketHostStunTcpTest : public testing::Test { 63 class P2PSocketHostStunTcpTest : public testing::Test {
63 protected: 64 protected:
64 virtual void SetUp() OVERRIDE { 65 void SetUp(bool ssl) {
65 EXPECT_CALL(sender_, Send( 66 EXPECT_CALL(sender_, Send(
66 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) 67 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID))))
67 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 68 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
68 69
69 socket_host_.reset(new P2PSocketHostStunTcp(&sender_, 0)); 70 socket_host_.reset(new P2PSocketHostStunTcp(&sender_, 0, ssl));
70 socket_ = new FakeSocket(&sent_data_); 71 socket_ = new FakeSocket(&sent_data_);
71 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); 72 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1));
72 socket_host_->socket_.reset(socket_); 73 socket_host_->socket_.reset(socket_);
73 74
74 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); 75 dest_ = ParseAddress(kTestIpAddress1, kTestPort1);
75 76
76 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); 77 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1);
77 78
78 socket_host_->remote_address_ = dest_; 79 socket_host_->remote_address_ = dest_;
79 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; 80 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING;
(...skipping 15 matching lines...) Expand all
95 96
96 net::IPEndPoint local_address_; 97 net::IPEndPoint local_address_;
97 98
98 net::IPEndPoint dest_; 99 net::IPEndPoint dest_;
99 net::IPEndPoint dest2_; 100 net::IPEndPoint dest2_;
100 }; 101 };
101 102
102 // Verify that we can send STUN message and that they are formatted 103 // Verify that we can send STUN message and that they are formatted
103 // properly. 104 // properly.
104 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { 105 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) {
106 SetUp(false);
105 EXPECT_CALL(sender_, Send( 107 EXPECT_CALL(sender_, Send(
106 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 108 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
107 .Times(3) 109 .Times(3)
108 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); 110 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
109 111
110 std::vector<char> packet1; 112 std::vector<char> packet1;
111 CreateStunRequest(&packet1); 113 CreateStunRequest(&packet1);
112 socket_host_->Send(dest_, packet1); 114 socket_host_->Send(dest_, packet1);
113 115
114 std::vector<char> packet2; 116 std::vector<char> packet2;
115 CreateStunResponse(&packet2); 117 CreateStunResponse(&packet2);
116 socket_host_->Send(dest_, packet2); 118 socket_host_->Send(dest_, packet2);
117 119
118 std::vector<char> packet3; 120 std::vector<char> packet3;
119 CreateStunError(&packet3); 121 CreateStunError(&packet3);
120 socket_host_->Send(dest_, packet3); 122 socket_host_->Send(dest_, packet3);
121 123
122 std::string expected_data; 124 std::string expected_data;
123 expected_data.append(IntToSize(packet1.size())); 125 expected_data.append(IntToSize(packet1.size()));
124 expected_data.append(packet1.begin(), packet1.end()); 126 expected_data.append(packet1.begin(), packet1.end());
125 expected_data.append(IntToSize(packet2.size())); 127 expected_data.append(IntToSize(packet2.size()));
126 expected_data.append(packet2.begin(), packet2.end()); 128 expected_data.append(packet2.begin(), packet2.end());
127 expected_data.append(IntToSize(packet3.size())); 129 expected_data.append(IntToSize(packet3.size()));
128 expected_data.append(packet3.begin(), packet3.end()); 130 expected_data.append(packet3.begin(), packet3.end());
129 131
130 EXPECT_EQ(expected_data, sent_data_); 132 EXPECT_EQ(expected_data, sent_data_);
131 } 133 }
132 134
135 TEST_F(P2PSocketHostTcpTest, SendStunNoAuthSsl) {
136 SetUp(true);
137 EXPECT_CALL(sender_, Send(
138 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
139 .Times(4)
140 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
141
142 std::vector<char> packet1;
143 CreateStunRequest(&packet1);
144 socket_host_->Send(dest_, packet1);
145
146 std::vector<char> packet2;
147 CreateStunResponse(&packet2);
148 socket_host_->Send(dest_, packet2);
149
150 std::vector<char> packet3;
151 CreateStunError(&packet3);
152 socket_host_->Send(dest_, packet3);
153
154 std::string expected_data;
155 SsltcpHelper ssl_helper;
156 expected_data.append(IntToSize(ssl_helper.client_hello_message().size()));
157 expected_data.append(ssl_helper.client_hello_message().begin(),
158 ssl_helper.client_hello_message().end());
159 expected_data.append(IntToSize(packet1.size()));
160 expected_data.append(packet1.begin(), packet1.end());
161 expected_data.append(IntToSize(packet2.size()));
162 expected_data.append(packet2.begin(), packet2.end());
163 expected_data.append(IntToSize(packet3.size()));
164 expected_data.append(packet3.begin(), packet3.end());
165
166 EXPECT_EQ(expected_data, sent_data_);
167 }
168
133 // Verify that we can receive STUN messages from the socket, and that 169 // Verify that we can receive STUN messages from the socket, and that
134 // the messages are parsed properly. 170 // the messages are parsed properly.
135 TEST_F(P2PSocketHostTcpTest, ReceiveStun) { 171 TEST_F(P2PSocketHostTcpTest, ReceiveStun) {
172 SetUp(false);
136 EXPECT_CALL(sender_, Send( 173 EXPECT_CALL(sender_, Send(
137 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 174 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
138 .Times(3) 175 .Times(3)
139 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); 176 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
140 177
141 std::vector<char> packet1; 178 std::vector<char> packet1;
142 CreateStunRequest(&packet1); 179 CreateStunRequest(&packet1);
143 socket_host_->Send(dest_, packet1); 180 socket_host_->Send(dest_, packet1);
144 181
145 std::vector<char> packet2; 182 std::vector<char> packet2;
(...skipping 24 matching lines...) Expand all
170 size_t step = 0; 207 size_t step = 0;
171 while (pos < received_data.size()) { 208 while (pos < received_data.size()) {
172 size_t step_size = std::min(step_sizes[step], received_data.size() - pos); 209 size_t step_size = std::min(step_sizes[step], received_data.size() - pos);
173 socket_->AppendInputData(&received_data[pos], step_size); 210 socket_->AppendInputData(&received_data[pos], step_size);
174 pos += step_size; 211 pos += step_size;
175 if (++step >= arraysize(step_sizes)) 212 if (++step >= arraysize(step_sizes))
176 step = 0; 213 step = 0;
177 } 214 }
178 } 215 }
179 216
217 TEST_F(P2PSocketHostTcpTest, ReceiveStunSsl) {
218 SetUp(true);
219 EXPECT_CALL(sender_, Send(
220 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
221 .Times(4)
222 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
223
224 std::vector<char> packet1;
225 CreateStunRequest(&packet1);
226 socket_host_->Send(dest_, packet1);
227
228 std::vector<char> packet2;
229 CreateStunResponse(&packet2);
230 socket_host_->Send(dest_, packet2);
231
232 std::vector<char> packet3;
233 CreateStunError(&packet3);
234 socket_host_->Send(dest_, packet3);
235
236 std::string received_data;
237 SsltcpHelper ssl_helper;
238 received_data.append(IntToSize(ssl_helper.server_hello_message().size()));
239 received_data.append(ssl_helper.server_hello_message().begin(),
240 ssl_helper.server_hello_message().end());
241 received_data.append(IntToSize(packet1.size()));
242 received_data.append(packet1.begin(), packet1.end());
243 received_data.append(IntToSize(packet2.size()));
244 received_data.append(packet2.begin(), packet2.end());
245 received_data.append(IntToSize(packet3.size()));
246 received_data.append(packet3.begin(), packet3.end());
247
248 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet1)))
249 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
250 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet2)))
251 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
252 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet3)))
253 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
254
255 size_t pos = 0;
256 size_t step_sizes[] = {3, 2, 1};
257 size_t step = 0;
258 while (pos < received_data.size()) {
259 size_t step_size = std::min(step_sizes[step], received_data.size() - pos);
260 socket_->AppendInputData(&received_data[pos], step_size);
261 pos += step_size;
262 if (++step >= arraysize(step_sizes))
263 step = 0;
264 }
265 }
266
180 // Verify that we can't send data before we've received STUN response 267 // Verify that we can't send data before we've received STUN response
181 // from the other side. 268 // from the other side.
182 TEST_F(P2PSocketHostTcpTest, SendDataNoAuth) { 269 TEST_F(P2PSocketHostTcpTest, SendDataNoAuth) {
270 SetUp(false);
183 EXPECT_CALL(sender_, Send( 271 EXPECT_CALL(sender_, Send(
184 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) 272 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID))))
185 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 273 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
186 274
187 std::vector<char> packet; 275 std::vector<char> packet;
188 CreateRandomPacket(&packet); 276 CreateRandomPacket(&packet);
189 socket_host_->Send(dest_, packet); 277 socket_host_->Send(dest_, packet);
190 278
191 EXPECT_EQ(0U, sent_data_.size()); 279 EXPECT_EQ(0U, sent_data_.size());
192 } 280 }
193 281
194 // Verify that we can send data after we've received STUN response 282 // Verify that we can send data after we've received STUN response
195 // from the other side. 283 // from the other side.
196 TEST_F(P2PSocketHostTcpTest, SendAfterStunRequest) { 284 TEST_F(P2PSocketHostTcpTest, SendAfterStunRequest) {
285 SetUp(false);
197 // Receive packet from |dest_|. 286 // Receive packet from |dest_|.
198 std::vector<char> request_packet; 287 std::vector<char> request_packet;
199 CreateStunRequest(&request_packet); 288 CreateStunRequest(&request_packet);
200 289
201 std::string received_data; 290 std::string received_data;
202 received_data.append(IntToSize(request_packet.size())); 291 received_data.append(IntToSize(request_packet.size()));
203 received_data.append(request_packet.begin(), request_packet.end()); 292 received_data.append(request_packet.begin(), request_packet.end());
204 293
205 EXPECT_CALL(sender_, Send( 294 EXPECT_CALL(sender_, Send(
206 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 295 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
207 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 296 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
208 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) 297 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet)))
209 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 298 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
210 socket_->AppendInputData(&received_data[0], received_data.size()); 299 socket_->AppendInputData(&received_data[0], received_data.size());
211 300
212 // Now we should be able to send any data to |dest_|. 301 // Now we should be able to send any data to |dest_|.
213 std::vector<char> packet; 302 std::vector<char> packet;
214 CreateRandomPacket(&packet); 303 CreateRandomPacket(&packet);
215 socket_host_->Send(dest_, packet); 304 socket_host_->Send(dest_, packet);
216 305
217 std::string expected_data; 306 std::string expected_data;
218 expected_data.append(IntToSize(packet.size())); 307 expected_data.append(IntToSize(packet.size()));
219 expected_data.append(packet.begin(), packet.end()); 308 expected_data.append(packet.begin(), packet.end());
220 309
221 EXPECT_EQ(expected_data, sent_data_); 310 EXPECT_EQ(expected_data, sent_data_);
222 } 311 }
223 312
224 // Verify that asynchronous writes are handled correctly. 313 // Verify that asynchronous writes are handled correctly.
225 TEST_F(P2PSocketHostTcpTest, AsyncWrites) { 314 TEST_F(P2PSocketHostTcpTest, AsyncWrites) {
315 SetUp(false);
226 base::MessageLoop message_loop; 316 base::MessageLoop message_loop;
227 317
228 socket_->set_async_write(true); 318 socket_->set_async_write(true);
229 319
230 EXPECT_CALL(sender_, Send( 320 EXPECT_CALL(sender_, Send(
231 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 321 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
232 .Times(2) 322 .Times(2)
233 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); 323 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
234 324
235 std::vector<char> packet1; 325 std::vector<char> packet1;
(...skipping 11 matching lines...) Expand all
247 expected_data.append(packet1.begin(), packet1.end()); 337 expected_data.append(packet1.begin(), packet1.end());
248 expected_data.append(IntToSize(packet2.size())); 338 expected_data.append(IntToSize(packet2.size()));
249 expected_data.append(packet2.begin(), packet2.end()); 339 expected_data.append(packet2.begin(), packet2.end());
250 340
251 EXPECT_EQ(expected_data, sent_data_); 341 EXPECT_EQ(expected_data, sent_data_);
252 } 342 }
253 343
254 // Verify that we can send STUN message and that they are formatted 344 // Verify that we can send STUN message and that they are formatted
255 // properly. 345 // properly.
256 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuth) { 346 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuth) {
347 SetUp(false);
257 EXPECT_CALL(sender_, Send( 348 EXPECT_CALL(sender_, Send(
258 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 349 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
259 .Times(3) 350 .Times(3)
260 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); 351 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
261 352
262 std::vector<char> packet1; 353 std::vector<char> packet1;
263 CreateStunRequest(&packet1); 354 CreateStunRequest(&packet1);
264 socket_host_->Send(dest_, packet1); 355 socket_host_->Send(dest_, packet1);
265 356
266 std::vector<char> packet2; 357 std::vector<char> packet2;
267 CreateStunResponse(&packet2); 358 CreateStunResponse(&packet2);
268 socket_host_->Send(dest_, packet2); 359 socket_host_->Send(dest_, packet2);
269 360
270 std::vector<char> packet3; 361 std::vector<char> packet3;
271 CreateStunError(&packet3); 362 CreateStunError(&packet3);
272 socket_host_->Send(dest_, packet3); 363 socket_host_->Send(dest_, packet3);
273 364
274 std::string expected_data; 365 std::string expected_data;
275 expected_data.append(packet1.begin(), packet1.end()); 366 expected_data.append(packet1.begin(), packet1.end());
276 expected_data.append(packet2.begin(), packet2.end()); 367 expected_data.append(packet2.begin(), packet2.end());
277 expected_data.append(packet3.begin(), packet3.end()); 368 expected_data.append(packet3.begin(), packet3.end());
278 369
279 EXPECT_EQ(expected_data, sent_data_); 370 EXPECT_EQ(expected_data, sent_data_);
280 } 371 }
281 372
373 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuthSsl) {
374 SetUp(true);
375 EXPECT_CALL(sender_, Send(
376 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
377 .Times(4)
378 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
379
380 std::vector<char> packet1;
381 CreateStunRequest(&packet1);
382 socket_host_->Send(dest_, packet1);
383
384 std::vector<char> packet2;
385 CreateStunResponse(&packet2);
386 socket_host_->Send(dest_, packet2);
387
388 std::vector<char> packet3;
389 CreateStunError(&packet3);
390 socket_host_->Send(dest_, packet3);
391
392 std::string expected_data;
393 SsltcpHelper ssl_helper;
394 expected_data.append(ssl_helper.client_hello_message().begin(),
395 ssl_helper.client_hello_message().end());
396 expected_data.append(packet1.begin(), packet1.end());
397 expected_data.append(packet2.begin(), packet2.end());
398 expected_data.append(packet3.begin(), packet3.end());
399
400 EXPECT_EQ(expected_data, sent_data_);
401 }
402
282 // Verify that we can receive STUN messages from the socket, and that 403 // Verify that we can receive STUN messages from the socket, and that
283 // the messages are parsed properly. 404 // the messages are parsed properly.
284 TEST_F(P2PSocketHostStunTcpTest, ReceiveStun) { 405 TEST_F(P2PSocketHostStunTcpTest, ReceiveStun) {
406 SetUp(false);
285 EXPECT_CALL(sender_, Send( 407 EXPECT_CALL(sender_, Send(
286 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 408 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
287 .Times(3) 409 .Times(3)
288 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); 410 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
289 411
290 std::vector<char> packet1; 412 std::vector<char> packet1;
291 CreateStunRequest(&packet1); 413 CreateStunRequest(&packet1);
292 socket_host_->Send(dest_, packet1); 414 socket_host_->Send(dest_, packet1);
293 415
294 std::vector<char> packet2; 416 std::vector<char> packet2;
(...skipping 21 matching lines...) Expand all
316 size_t step = 0; 438 size_t step = 0;
317 while (pos < received_data.size()) { 439 while (pos < received_data.size()) {
318 size_t step_size = std::min(step_sizes[step], received_data.size() - pos); 440 size_t step_size = std::min(step_sizes[step], received_data.size() - pos);
319 socket_->AppendInputData(&received_data[pos], step_size); 441 socket_->AppendInputData(&received_data[pos], step_size);
320 pos += step_size; 442 pos += step_size;
321 if (++step >= arraysize(step_sizes)) 443 if (++step >= arraysize(step_sizes))
322 step = 0; 444 step = 0;
323 } 445 }
324 } 446 }
325 447
448 TEST_F(P2PSocketHostStunTcpTest, ReceiveStunSsl) {
449 SetUp(true);
450 EXPECT_CALL(sender_, Send(
451 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
452 .Times(4)
453 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
454
455 std::vector<char> packet1;
456 CreateStunRequest(&packet1);
457 socket_host_->Send(dest_, packet1);
458
459 std::vector<char> packet2;
460 CreateStunResponse(&packet2);
461 socket_host_->Send(dest_, packet2);
462
463 std::vector<char> packet3;
464 CreateStunError(&packet3);
465 socket_host_->Send(dest_, packet3);
466
467 std::string received_data;
468 SsltcpHelper ssl_helper;
469 received_data.append(ssl_helper.server_hello_message().begin(),
470 ssl_helper.server_hello_message().end());
471 received_data.append(packet1.begin(), packet1.end());
472 received_data.append(packet2.begin(), packet2.end());
473 received_data.append(packet3.begin(), packet3.end());
474
475 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet1)))
476 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
477 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet2)))
478 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
479 EXPECT_CALL(sender_, Send(MatchPacketMessage(packet3)))
480 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
481
482 size_t pos = 0;
483 size_t step_sizes[] = {3, 2, 1};
484 size_t step = 0;
485 while (pos < received_data.size()) {
486 size_t step_size = std::min(step_sizes[step], received_data.size() - pos);
487 socket_->AppendInputData(&received_data[pos], step_size);
488 pos += step_size;
489 if (++step >= arraysize(step_sizes))
490 step = 0;
491 }
492 }
493
326 // Verify that we can't send data before we've received STUN response 494 // Verify that we can't send data before we've received STUN response
327 // from the other side. 495 // from the other side.
328 TEST_F(P2PSocketHostStunTcpTest, SendDataNoAuth) { 496 TEST_F(P2PSocketHostStunTcpTest, SendDataNoAuth) {
497 SetUp(false);
329 EXPECT_CALL(sender_, Send( 498 EXPECT_CALL(sender_, Send(
330 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) 499 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID))))
331 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 500 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
332 501
333 std::vector<char> packet; 502 std::vector<char> packet;
334 CreateRandomPacket(&packet); 503 CreateRandomPacket(&packet);
335 socket_host_->Send(dest_, packet); 504 socket_host_->Send(dest_, packet);
336 505
337 EXPECT_EQ(0U, sent_data_.size()); 506 EXPECT_EQ(0U, sent_data_.size());
338 } 507 }
339 508
340 // Verify that asynchronous writes are handled correctly. 509 // Verify that asynchronous writes are handled correctly.
341 TEST_F(P2PSocketHostStunTcpTest, AsyncWrites) { 510 TEST_F(P2PSocketHostStunTcpTest, AsyncWrites) {
511 SetUp(false);
342 base::MessageLoop message_loop; 512 base::MessageLoop message_loop;
343 513
344 socket_->set_async_write(true); 514 socket_->set_async_write(true);
345 515
346 EXPECT_CALL(sender_, Send( 516 EXPECT_CALL(sender_, Send(
347 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) 517 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID))))
348 .Times(2) 518 .Times(2)
349 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); 519 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true)));
350 520
351 std::vector<char> packet1; 521 std::vector<char> packet1;
352 CreateStunRequest(&packet1); 522 CreateStunRequest(&packet1);
353 socket_host_->Send(dest_, packet1); 523 socket_host_->Send(dest_, packet1);
354 524
355 std::vector<char> packet2; 525 std::vector<char> packet2;
356 CreateStunResponse(&packet2); 526 CreateStunResponse(&packet2);
357 socket_host_->Send(dest_, packet2); 527 socket_host_->Send(dest_, packet2);
358 528
359 message_loop.RunUntilIdle(); 529 message_loop.RunUntilIdle();
360 530
361 std::string expected_data; 531 std::string expected_data;
362 expected_data.append(packet1.begin(), packet1.end()); 532 expected_data.append(packet1.begin(), packet1.end());
363 expected_data.append(packet2.begin(), packet2.end()); 533 expected_data.append(packet2.begin(), packet2.end());
364 534
365 EXPECT_EQ(expected_data, sent_data_); 535 EXPECT_EQ(expected_data, sent_data_);
366 } 536 }
367 537
368 } // namespace content 538 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698