| 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_udp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_udp.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 }; | 210 }; |
| 211 | 211 |
| 212 // Verify that we can send STUN messages before we receive anything | 212 // Verify that we can send STUN messages before we receive anything |
| 213 // from the other side. | 213 // from the other side. |
| 214 TEST_F(P2PSocketHostUdpTest, SendStunNoAuth) { | 214 TEST_F(P2PSocketHostUdpTest, SendStunNoAuth) { |
| 215 EXPECT_CALL(sender_, Send( | 215 EXPECT_CALL(sender_, Send( |
| 216 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 216 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 217 .Times(3) | 217 .Times(3) |
| 218 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 218 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 219 | 219 |
| 220 talk_base::PacketOptions options; |
| 220 std::vector<char> packet1; | 221 std::vector<char> packet1; |
| 221 CreateStunRequest(&packet1); | 222 CreateStunRequest(&packet1); |
| 222 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0); | 223 socket_host_->Send(dest1_, packet1, options, 0); |
| 223 | 224 |
| 224 std::vector<char> packet2; | 225 std::vector<char> packet2; |
| 225 CreateStunResponse(&packet2); | 226 CreateStunResponse(&packet2); |
| 226 socket_host_->Send(dest1_, packet2, net::DSCP_NO_CHANGE, 0); | 227 socket_host_->Send(dest1_, packet2, options, 0); |
| 227 | 228 |
| 228 std::vector<char> packet3; | 229 std::vector<char> packet3; |
| 229 CreateStunError(&packet3); | 230 CreateStunError(&packet3); |
| 230 socket_host_->Send(dest1_, packet3, net::DSCP_NO_CHANGE, 0); | 231 socket_host_->Send(dest1_, packet3, options, 0); |
| 231 | 232 |
| 232 ASSERT_EQ(sent_packets_.size(), 3U); | 233 ASSERT_EQ(sent_packets_.size(), 3U); |
| 233 ASSERT_EQ(sent_packets_[0].second, packet1); | 234 ASSERT_EQ(sent_packets_[0].second, packet1); |
| 234 ASSERT_EQ(sent_packets_[1].second, packet2); | 235 ASSERT_EQ(sent_packets_[1].second, packet2); |
| 235 ASSERT_EQ(sent_packets_[2].second, packet3); | 236 ASSERT_EQ(sent_packets_[2].second, packet3); |
| 236 } | 237 } |
| 237 | 238 |
| 238 // Verify that no data packets can be sent before STUN binding has | 239 // Verify that no data packets can be sent before STUN binding has |
| 239 // finished. | 240 // finished. |
| 240 TEST_F(P2PSocketHostUdpTest, SendDataNoAuth) { | 241 TEST_F(P2PSocketHostUdpTest, SendDataNoAuth) { |
| 241 EXPECT_CALL(sender_, Send( | 242 EXPECT_CALL(sender_, Send( |
| 242 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 243 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
| 243 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 244 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 244 | 245 |
| 246 talk_base::PacketOptions options; |
| 245 std::vector<char> packet; | 247 std::vector<char> packet; |
| 246 CreateRandomPacket(&packet); | 248 CreateRandomPacket(&packet); |
| 247 socket_host_->Send(dest1_, packet, net::DSCP_NO_CHANGE, 0); | 249 socket_host_->Send(dest1_, packet, options, 0); |
| 248 | 250 |
| 249 ASSERT_EQ(sent_packets_.size(), 0U); | 251 ASSERT_EQ(sent_packets_.size(), 0U); |
| 250 } | 252 } |
| 251 | 253 |
| 252 // Verify that we can send data after we've received STUN request | 254 // Verify that we can send data after we've received STUN request |
| 253 // from the other side. | 255 // from the other side. |
| 254 TEST_F(P2PSocketHostUdpTest, SendAfterStunRequest) { | 256 TEST_F(P2PSocketHostUdpTest, SendAfterStunRequest) { |
| 255 // Receive packet from |dest1_|. | 257 // Receive packet from |dest1_|. |
| 256 std::vector<char> request_packet; | 258 std::vector<char> request_packet; |
| 257 CreateStunRequest(&request_packet); | 259 CreateStunRequest(&request_packet); |
| 258 | 260 |
| 259 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 261 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 260 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 262 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 261 socket_->ReceivePacket(dest1_, request_packet); | 263 socket_->ReceivePacket(dest1_, request_packet); |
| 262 | 264 |
| 263 // Now we should be able to send any data to |dest1_|. | 265 // Now we should be able to send any data to |dest1_|. |
| 264 EXPECT_CALL(sender_, Send( | 266 EXPECT_CALL(sender_, Send( |
| 265 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 267 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 266 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 268 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 269 |
| 270 talk_base::PacketOptions options; |
| 267 std::vector<char> packet; | 271 std::vector<char> packet; |
| 268 CreateRandomPacket(&packet); | 272 CreateRandomPacket(&packet); |
| 269 socket_host_->Send(dest1_, packet, net::DSCP_NO_CHANGE, 0); | 273 socket_host_->Send(dest1_, packet, options, 0); |
| 270 | 274 |
| 271 ASSERT_EQ(1U, sent_packets_.size()); | 275 ASSERT_EQ(1U, sent_packets_.size()); |
| 272 ASSERT_EQ(dest1_, sent_packets_[0].first); | 276 ASSERT_EQ(dest1_, sent_packets_[0].first); |
| 273 } | 277 } |
| 274 | 278 |
| 275 // Verify that we can send data after we've received STUN response | 279 // Verify that we can send data after we've received STUN response |
| 276 // from the other side. | 280 // from the other side. |
| 277 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponse) { | 281 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponse) { |
| 278 // Receive packet from |dest1_|. | 282 // Receive packet from |dest1_|. |
| 279 std::vector<char> request_packet; | 283 std::vector<char> request_packet; |
| 280 CreateStunRequest(&request_packet); | 284 CreateStunRequest(&request_packet); |
| 281 | 285 |
| 282 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 286 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 283 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 287 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 284 socket_->ReceivePacket(dest1_, request_packet); | 288 socket_->ReceivePacket(dest1_, request_packet); |
| 285 | 289 |
| 286 // Now we should be able to send any data to |dest1_|. | 290 // Now we should be able to send any data to |dest1_|. |
| 287 EXPECT_CALL(sender_, Send( | 291 EXPECT_CALL(sender_, Send( |
| 288 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 292 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 289 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 293 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 294 |
| 295 talk_base::PacketOptions options; |
| 290 std::vector<char> packet; | 296 std::vector<char> packet; |
| 291 CreateRandomPacket(&packet); | 297 CreateRandomPacket(&packet); |
| 292 socket_host_->Send(dest1_, packet, net::DSCP_NO_CHANGE, 0); | 298 socket_host_->Send(dest1_, packet, options, 0); |
| 293 | 299 |
| 294 ASSERT_EQ(1U, sent_packets_.size()); | 300 ASSERT_EQ(1U, sent_packets_.size()); |
| 295 ASSERT_EQ(dest1_, sent_packets_[0].first); | 301 ASSERT_EQ(dest1_, sent_packets_[0].first); |
| 296 } | 302 } |
| 297 | 303 |
| 298 // Verify messages still cannot be sent to an unathorized host after | 304 // Verify messages still cannot be sent to an unathorized host after |
| 299 // successful binding with different host. | 305 // successful binding with different host. |
| 300 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponseDifferentHost) { | 306 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponseDifferentHost) { |
| 301 // Receive packet from |dest1_|. | 307 // Receive packet from |dest1_|. |
| 302 std::vector<char> request_packet; | 308 std::vector<char> request_packet; |
| 303 CreateStunRequest(&request_packet); | 309 CreateStunRequest(&request_packet); |
| 304 | 310 |
| 305 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 311 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 306 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 312 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 307 socket_->ReceivePacket(dest1_, request_packet); | 313 socket_->ReceivePacket(dest1_, request_packet); |
| 308 | 314 |
| 309 // Should fail when trying to send the same packet to |dest2_|. | 315 // Should fail when trying to send the same packet to |dest2_|. |
| 316 talk_base::PacketOptions options; |
| 310 std::vector<char> packet; | 317 std::vector<char> packet; |
| 311 CreateRandomPacket(&packet); | 318 CreateRandomPacket(&packet); |
| 312 EXPECT_CALL(sender_, Send( | 319 EXPECT_CALL(sender_, Send( |
| 313 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 320 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
| 314 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 321 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 315 socket_host_->Send(dest2_, packet, net::DSCP_NO_CHANGE, 0); | 322 socket_host_->Send(dest2_, packet, options, 0); |
| 316 } | 323 } |
| 317 | 324 |
| 318 // Verify throttler not allowing unlimited sending of ICE messages to | 325 // Verify throttler not allowing unlimited sending of ICE messages to |
| 319 // any destination. | 326 // any destination. |
| 320 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimit) { | 327 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimit) { |
| 321 EXPECT_CALL(sender_, Send( | 328 EXPECT_CALL(sender_, Send( |
| 322 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 329 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 323 .Times(2) | 330 .Times(2) |
| 324 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 331 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 325 | 332 |
| 333 talk_base::PacketOptions options; |
| 326 std::vector<char> packet1; | 334 std::vector<char> packet1; |
| 327 CreateStunRequest(&packet1); | 335 CreateStunRequest(&packet1); |
| 328 throttler_.SetSendIceBandwidth(packet1.size() * 2); | 336 throttler_.SetSendIceBandwidth(packet1.size() * 2); |
| 329 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0); | 337 socket_host_->Send(dest1_, packet1, options, 0); |
| 330 socket_host_->Send(dest2_, packet1, net::DSCP_NO_CHANGE, 0); | 338 socket_host_->Send(dest2_, packet1, options, 0); |
| 331 | 339 |
| 332 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2222); | 340 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2222); |
| 333 // This packet must be dropped by the throttler. | 341 // This packet must be dropped by the throttler. |
| 334 socket_host_->Send(dest3, packet1, net::DSCP_NO_CHANGE, 0); | 342 socket_host_->Send(dest3, packet1, options, 0); |
| 335 ASSERT_EQ(sent_packets_.size(), 2U); | 343 ASSERT_EQ(sent_packets_.size(), 2U); |
| 336 } | 344 } |
| 337 | 345 |
| 338 // Verify we can send packets to a known destination when ICE throttling is | 346 // Verify we can send packets to a known destination when ICE throttling is |
| 339 // active. | 347 // active. |
| 340 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) { | 348 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) { |
| 341 // Receive packet from |dest1_|. | 349 // Receive packet from |dest1_|. |
| 342 std::vector<char> request_packet; | 350 std::vector<char> request_packet; |
| 343 CreateStunRequest(&request_packet); | 351 CreateStunRequest(&request_packet); |
| 344 | 352 |
| 345 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 353 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 346 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 354 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 347 socket_->ReceivePacket(dest1_, request_packet); | 355 socket_->ReceivePacket(dest1_, request_packet); |
| 348 | 356 |
| 349 EXPECT_CALL(sender_, Send( | 357 EXPECT_CALL(sender_, Send( |
| 350 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 358 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 351 .Times(4) | 359 .Times(4) |
| 352 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 360 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 353 | 361 |
| 362 talk_base::PacketOptions options; |
| 354 std::vector<char> packet1; | 363 std::vector<char> packet1; |
| 355 CreateStunRequest(&packet1); | 364 CreateStunRequest(&packet1); |
| 356 throttler_.SetSendIceBandwidth(packet1.size()); | 365 throttler_.SetSendIceBandwidth(packet1.size()); |
| 357 // |dest1_| is known address, throttling will not be applied. | 366 // |dest1_| is known address, throttling will not be applied. |
| 358 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0); | 367 socket_host_->Send(dest1_, packet1, options, 0); |
| 359 // Trying to send the packet to dest1_ in the same window. It should go. | 368 // Trying to send the packet to dest1_ in the same window. It should go. |
| 360 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0); | 369 socket_host_->Send(dest1_, packet1, options, 0); |
| 361 | 370 |
| 362 // Throttler should allow this packet to go through. | 371 // Throttler should allow this packet to go through. |
| 363 socket_host_->Send(dest2_, packet1, net::DSCP_NO_CHANGE, 0); | 372 socket_host_->Send(dest2_, packet1, options, 0); |
| 364 | 373 |
| 365 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2223); | 374 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2223); |
| 366 // This packet will be dropped, as limit only for a single packet. | 375 // This packet will be dropped, as limit only for a single packet. |
| 367 socket_host_->Send(dest3, packet1, net::DSCP_NO_CHANGE, 0); | 376 socket_host_->Send(dest3, packet1, options, 0); |
| 368 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); | 377 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); |
| 369 // This packet should also be dropped. | 378 // This packet should also be dropped. |
| 370 socket_host_->Send(dest4, packet1, net::DSCP_NO_CHANGE, 0); | 379 socket_host_->Send(dest4, packet1, options, 0); |
| 371 // |dest1| is known, we can send as many packets to it. | 380 // |dest1| is known, we can send as many packets to it. |
| 372 socket_host_->Send(dest1_, packet1, net::DSCP_NO_CHANGE, 0); | 381 socket_host_->Send(dest1_, packet1, options, 0); |
| 373 ASSERT_EQ(sent_packets_.size(), 4U); | 382 ASSERT_EQ(sent_packets_.size(), 4U); |
| 374 } | 383 } |
| 375 | 384 |
| 376 } // namespace content | 385 } // namespace content |
| OLD | NEW |