OLD | NEW |
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" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 }; | 82 }; |
83 | 83 |
84 // Verify that we can send STUN message and that they are formatted | 84 // Verify that we can send STUN message and that they are formatted |
85 // properly. | 85 // properly. |
86 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { | 86 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { |
87 EXPECT_CALL(sender_, Send( | 87 EXPECT_CALL(sender_, Send( |
88 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 88 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
89 .Times(3) | 89 .Times(3) |
90 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 90 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
91 | 91 |
92 rtc::PacketOptions options; | 92 talk_base::PacketOptions options; |
93 std::vector<char> packet1; | 93 std::vector<char> packet1; |
94 CreateStunRequest(&packet1); | 94 CreateStunRequest(&packet1); |
95 socket_host_->Send(dest_.ip_address, packet1, options, 0); | 95 socket_host_->Send(dest_.ip_address, packet1, options, 0); |
96 | 96 |
97 std::vector<char> packet2; | 97 std::vector<char> packet2; |
98 CreateStunResponse(&packet2); | 98 CreateStunResponse(&packet2); |
99 socket_host_->Send(dest_.ip_address, packet2, options, 0); | 99 socket_host_->Send(dest_.ip_address, packet2, options, 0); |
100 | 100 |
101 std::vector<char> packet3; | 101 std::vector<char> packet3; |
102 CreateStunError(&packet3); | 102 CreateStunError(&packet3); |
(...skipping 11 matching lines...) Expand all Loading... |
114 } | 114 } |
115 | 115 |
116 // Verify that we can receive STUN messages from the socket, and that | 116 // Verify that we can receive STUN messages from the socket, and that |
117 // the messages are parsed properly. | 117 // the messages are parsed properly. |
118 TEST_F(P2PSocketHostTcpTest, ReceiveStun) { | 118 TEST_F(P2PSocketHostTcpTest, ReceiveStun) { |
119 EXPECT_CALL(sender_, Send( | 119 EXPECT_CALL(sender_, Send( |
120 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 120 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
121 .Times(3) | 121 .Times(3) |
122 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 122 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
123 | 123 |
124 rtc::PacketOptions options; | 124 talk_base::PacketOptions options; |
125 std::vector<char> packet1; | 125 std::vector<char> packet1; |
126 CreateStunRequest(&packet1); | 126 CreateStunRequest(&packet1); |
127 socket_host_->Send(dest_.ip_address, packet1, options, 0); | 127 socket_host_->Send(dest_.ip_address, packet1, options, 0); |
128 | 128 |
129 std::vector<char> packet2; | 129 std::vector<char> packet2; |
130 CreateStunResponse(&packet2); | 130 CreateStunResponse(&packet2); |
131 socket_host_->Send(dest_.ip_address, packet2, options, 0); | 131 socket_host_->Send(dest_.ip_address, packet2, options, 0); |
132 | 132 |
133 std::vector<char> packet3; | 133 std::vector<char> packet3; |
134 CreateStunError(&packet3); | 134 CreateStunError(&packet3); |
(...skipping 26 matching lines...) Expand all Loading... |
161 } | 161 } |
162 } | 162 } |
163 | 163 |
164 // Verify that we can't send data before we've received STUN response | 164 // Verify that we can't send data before we've received STUN response |
165 // from the other side. | 165 // from the other side. |
166 TEST_F(P2PSocketHostTcpTest, SendDataNoAuth) { | 166 TEST_F(P2PSocketHostTcpTest, SendDataNoAuth) { |
167 EXPECT_CALL(sender_, Send( | 167 EXPECT_CALL(sender_, Send( |
168 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 168 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
169 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 169 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
170 | 170 |
171 rtc::PacketOptions options; | 171 talk_base::PacketOptions options; |
172 std::vector<char> packet; | 172 std::vector<char> packet; |
173 CreateRandomPacket(&packet); | 173 CreateRandomPacket(&packet); |
174 socket_host_->Send(dest_.ip_address, packet, options, 0); | 174 socket_host_->Send(dest_.ip_address, packet, options, 0); |
175 | 175 |
176 EXPECT_EQ(0U, sent_data_.size()); | 176 EXPECT_EQ(0U, sent_data_.size()); |
177 } | 177 } |
178 | 178 |
179 // Verify that we can send data after we've received STUN response | 179 // Verify that we can send data after we've received STUN response |
180 // from the other side. | 180 // from the other side. |
181 TEST_F(P2PSocketHostTcpTest, SendAfterStunRequest) { | 181 TEST_F(P2PSocketHostTcpTest, SendAfterStunRequest) { |
182 // Receive packet from |dest_|. | 182 // Receive packet from |dest_|. |
183 std::vector<char> request_packet; | 183 std::vector<char> request_packet; |
184 CreateStunRequest(&request_packet); | 184 CreateStunRequest(&request_packet); |
185 | 185 |
186 std::string received_data; | 186 std::string received_data; |
187 received_data.append(IntToSize(request_packet.size())); | 187 received_data.append(IntToSize(request_packet.size())); |
188 received_data.append(request_packet.begin(), request_packet.end()); | 188 received_data.append(request_packet.begin(), request_packet.end()); |
189 | 189 |
190 EXPECT_CALL(sender_, Send( | 190 EXPECT_CALL(sender_, Send( |
191 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 191 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
192 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 192 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
193 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 193 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
194 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 194 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
195 socket_->AppendInputData(&received_data[0], received_data.size()); | 195 socket_->AppendInputData(&received_data[0], received_data.size()); |
196 | 196 |
197 rtc::PacketOptions options; | 197 talk_base::PacketOptions options; |
198 // Now we should be able to send any data to |dest_|. | 198 // Now we should be able to send any data to |dest_|. |
199 std::vector<char> packet; | 199 std::vector<char> packet; |
200 CreateRandomPacket(&packet); | 200 CreateRandomPacket(&packet); |
201 socket_host_->Send(dest_.ip_address, packet, options, 0); | 201 socket_host_->Send(dest_.ip_address, packet, options, 0); |
202 | 202 |
203 std::string expected_data; | 203 std::string expected_data; |
204 expected_data.append(IntToSize(packet.size())); | 204 expected_data.append(IntToSize(packet.size())); |
205 expected_data.append(packet.begin(), packet.end()); | 205 expected_data.append(packet.begin(), packet.end()); |
206 | 206 |
207 EXPECT_EQ(expected_data, sent_data_); | 207 EXPECT_EQ(expected_data, sent_data_); |
208 } | 208 } |
209 | 209 |
210 // Verify that asynchronous writes are handled correctly. | 210 // Verify that asynchronous writes are handled correctly. |
211 TEST_F(P2PSocketHostTcpTest, AsyncWrites) { | 211 TEST_F(P2PSocketHostTcpTest, AsyncWrites) { |
212 base::MessageLoop message_loop; | 212 base::MessageLoop message_loop; |
213 | 213 |
214 socket_->set_async_write(true); | 214 socket_->set_async_write(true); |
215 | 215 |
216 EXPECT_CALL(sender_, Send( | 216 EXPECT_CALL(sender_, Send( |
217 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 217 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
218 .Times(2) | 218 .Times(2) |
219 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 219 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
220 | 220 |
221 rtc::PacketOptions options; | 221 talk_base::PacketOptions options; |
222 std::vector<char> packet1; | 222 std::vector<char> packet1; |
223 CreateStunRequest(&packet1); | 223 CreateStunRequest(&packet1); |
224 | 224 |
225 socket_host_->Send(dest_.ip_address, packet1, options, 0); | 225 socket_host_->Send(dest_.ip_address, packet1, options, 0); |
226 | 226 |
227 std::vector<char> packet2; | 227 std::vector<char> packet2; |
228 CreateStunResponse(&packet2); | 228 CreateStunResponse(&packet2); |
229 socket_host_->Send(dest_.ip_address, packet2, options, 0); | 229 socket_host_->Send(dest_.ip_address, packet2, options, 0); |
230 | 230 |
231 message_loop.RunUntilIdle(); | 231 message_loop.RunUntilIdle(); |
(...skipping 15 matching lines...) Expand all Loading... |
247 received_data.append(IntToSize(request_packet.size())); | 247 received_data.append(IntToSize(request_packet.size())); |
248 received_data.append(request_packet.begin(), request_packet.end()); | 248 received_data.append(request_packet.begin(), request_packet.end()); |
249 | 249 |
250 EXPECT_CALL(sender_, Send( | 250 EXPECT_CALL(sender_, Send( |
251 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 251 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
252 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 252 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
253 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 253 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
254 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 254 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
255 socket_->AppendInputData(&received_data[0], received_data.size()); | 255 socket_->AppendInputData(&received_data[0], received_data.size()); |
256 | 256 |
257 rtc::PacketOptions options; | 257 talk_base::PacketOptions options; |
258 options.packet_time_params.rtp_sendtime_extension_id = 3; | 258 options.packet_time_params.rtp_sendtime_extension_id = 3; |
259 // Now we should be able to send any data to |dest_|. | 259 // Now we should be able to send any data to |dest_|. |
260 std::vector<char> packet; | 260 std::vector<char> packet; |
261 CreateRandomPacket(&packet); | 261 CreateRandomPacket(&packet); |
262 // Make it a RTP packet. | 262 // Make it a RTP packet. |
263 *reinterpret_cast<uint16*>(&*packet.begin()) = base::HostToNet16(0x8000); | 263 *reinterpret_cast<uint16*>(&*packet.begin()) = base::HostToNet16(0x8000); |
264 socket_host_->Send(dest_.ip_address, packet, options, 0); | 264 socket_host_->Send(dest_.ip_address, packet, options, 0); |
265 | 265 |
266 std::string expected_data; | 266 std::string expected_data; |
267 expected_data.append(IntToSize(packet.size())); | 267 expected_data.append(IntToSize(packet.size())); |
268 expected_data.append(packet.begin(), packet.end()); | 268 expected_data.append(packet.begin(), packet.end()); |
269 | 269 |
270 EXPECT_EQ(expected_data, sent_data_); | 270 EXPECT_EQ(expected_data, sent_data_); |
271 } | 271 } |
272 | 272 |
273 // Verify that we can send STUN message and that they are formatted | 273 // Verify that we can send STUN message and that they are formatted |
274 // properly. | 274 // properly. |
275 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuth) { | 275 TEST_F(P2PSocketHostStunTcpTest, SendStunNoAuth) { |
276 EXPECT_CALL(sender_, Send( | 276 EXPECT_CALL(sender_, Send( |
277 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 277 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
278 .Times(3) | 278 .Times(3) |
279 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 279 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
280 | 280 |
281 rtc::PacketOptions options; | 281 talk_base::PacketOptions options; |
282 std::vector<char> packet1; | 282 std::vector<char> packet1; |
283 CreateStunRequest(&packet1); | 283 CreateStunRequest(&packet1); |
284 socket_host_->Send(dest_.ip_address, packet1, options, 0); | 284 socket_host_->Send(dest_.ip_address, packet1, options, 0); |
285 | 285 |
286 std::vector<char> packet2; | 286 std::vector<char> packet2; |
287 CreateStunResponse(&packet2); | 287 CreateStunResponse(&packet2); |
288 socket_host_->Send(dest_.ip_address, packet2, options, 0); | 288 socket_host_->Send(dest_.ip_address, packet2, options, 0); |
289 | 289 |
290 std::vector<char> packet3; | 290 std::vector<char> packet3; |
291 CreateStunError(&packet3); | 291 CreateStunError(&packet3); |
292 socket_host_->Send(dest_.ip_address, packet3, options, 0); | 292 socket_host_->Send(dest_.ip_address, packet3, options, 0); |
293 | 293 |
294 std::string expected_data; | 294 std::string expected_data; |
295 expected_data.append(packet1.begin(), packet1.end()); | 295 expected_data.append(packet1.begin(), packet1.end()); |
296 expected_data.append(packet2.begin(), packet2.end()); | 296 expected_data.append(packet2.begin(), packet2.end()); |
297 expected_data.append(packet3.begin(), packet3.end()); | 297 expected_data.append(packet3.begin(), packet3.end()); |
298 | 298 |
299 EXPECT_EQ(expected_data, sent_data_); | 299 EXPECT_EQ(expected_data, sent_data_); |
300 } | 300 } |
301 | 301 |
302 // Verify that we can receive STUN messages from the socket, and that | 302 // Verify that we can receive STUN messages from the socket, and that |
303 // the messages are parsed properly. | 303 // the messages are parsed properly. |
304 TEST_F(P2PSocketHostStunTcpTest, ReceiveStun) { | 304 TEST_F(P2PSocketHostStunTcpTest, ReceiveStun) { |
305 EXPECT_CALL(sender_, Send( | 305 EXPECT_CALL(sender_, Send( |
306 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 306 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
307 .Times(3) | 307 .Times(3) |
308 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 308 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
309 | 309 |
310 rtc::PacketOptions options; | 310 talk_base::PacketOptions options; |
311 std::vector<char> packet1; | 311 std::vector<char> packet1; |
312 CreateStunRequest(&packet1); | 312 CreateStunRequest(&packet1); |
313 socket_host_->Send(dest_.ip_address, packet1, options, 0); | 313 socket_host_->Send(dest_.ip_address, packet1, options, 0); |
314 | 314 |
315 std::vector<char> packet2; | 315 std::vector<char> packet2; |
316 CreateStunResponse(&packet2); | 316 CreateStunResponse(&packet2); |
317 socket_host_->Send(dest_.ip_address, packet2, options, 0); | 317 socket_host_->Send(dest_.ip_address, packet2, options, 0); |
318 | 318 |
319 std::vector<char> packet3; | 319 std::vector<char> packet3; |
320 CreateStunError(&packet3); | 320 CreateStunError(&packet3); |
(...skipping 23 matching lines...) Expand all Loading... |
344 } | 344 } |
345 } | 345 } |
346 | 346 |
347 // Verify that we can't send data before we've received STUN response | 347 // Verify that we can't send data before we've received STUN response |
348 // from the other side. | 348 // from the other side. |
349 TEST_F(P2PSocketHostStunTcpTest, SendDataNoAuth) { | 349 TEST_F(P2PSocketHostStunTcpTest, SendDataNoAuth) { |
350 EXPECT_CALL(sender_, Send( | 350 EXPECT_CALL(sender_, Send( |
351 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 351 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
352 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 352 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
353 | 353 |
354 rtc::PacketOptions options; | 354 talk_base::PacketOptions options; |
355 std::vector<char> packet; | 355 std::vector<char> packet; |
356 CreateRandomPacket(&packet); | 356 CreateRandomPacket(&packet); |
357 socket_host_->Send(dest_.ip_address, packet, options, 0); | 357 socket_host_->Send(dest_.ip_address, packet, options, 0); |
358 | 358 |
359 EXPECT_EQ(0U, sent_data_.size()); | 359 EXPECT_EQ(0U, sent_data_.size()); |
360 } | 360 } |
361 | 361 |
362 // Verify that asynchronous writes are handled correctly. | 362 // Verify that asynchronous writes are handled correctly. |
363 TEST_F(P2PSocketHostStunTcpTest, AsyncWrites) { | 363 TEST_F(P2PSocketHostStunTcpTest, AsyncWrites) { |
364 base::MessageLoop message_loop; | 364 base::MessageLoop message_loop; |
365 | 365 |
366 socket_->set_async_write(true); | 366 socket_->set_async_write(true); |
367 | 367 |
368 EXPECT_CALL(sender_, Send( | 368 EXPECT_CALL(sender_, Send( |
369 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 369 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
370 .Times(2) | 370 .Times(2) |
371 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 371 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
372 | 372 |
373 rtc::PacketOptions options; | 373 talk_base::PacketOptions options; |
374 std::vector<char> packet1; | 374 std::vector<char> packet1; |
375 CreateStunRequest(&packet1); | 375 CreateStunRequest(&packet1); |
376 socket_host_->Send(dest_.ip_address, packet1, options, 0); | 376 socket_host_->Send(dest_.ip_address, packet1, options, 0); |
377 | 377 |
378 std::vector<char> packet2; | 378 std::vector<char> packet2; |
379 CreateStunResponse(&packet2); | 379 CreateStunResponse(&packet2); |
380 socket_host_->Send(dest_.ip_address, packet2, options, 0); | 380 socket_host_->Send(dest_.ip_address, packet2, options, 0); |
381 | 381 |
382 message_loop.RunUntilIdle(); | 382 message_loop.RunUntilIdle(); |
383 | 383 |
384 std::string expected_data; | 384 std::string expected_data; |
385 expected_data.append(packet1.begin(), packet1.end()); | 385 expected_data.append(packet1.begin(), packet1.end()); |
386 expected_data.append(packet2.begin(), packet2.end()); | 386 expected_data.append(packet2.begin(), packet2.end()); |
387 | 387 |
388 EXPECT_EQ(expected_data, sent_data_); | 388 EXPECT_EQ(expected_data, sent_data_); |
389 } | 389 } |
390 | 390 |
391 } // namespace content | 391 } // namespace content |
OLD | NEW |