| 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" |
| 11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
| 12 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 12 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
| 13 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" | 13 #include "content/browser/renderer_host/p2p/socket_host_throttler.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/udp/datagram_server_socket.h" | 17 #include "net/udp/datagram_server_socket.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/libjingle/source/talk/base/timing.h" | 20 #include "third_party/webrtc/base/timing.h" |
| 21 | 21 |
| 22 using ::testing::_; | 22 using ::testing::_; |
| 23 using ::testing::DeleteArg; | 23 using ::testing::DeleteArg; |
| 24 using ::testing::DoAll; | 24 using ::testing::DoAll; |
| 25 using ::testing::Return; | 25 using ::testing::Return; |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 class FakeTiming : public talk_base::Timing { | 29 class FakeTiming : public rtc::Timing { |
| 30 public: | 30 public: |
| 31 FakeTiming() : now_(0.0) {} | 31 FakeTiming() : now_(0.0) {} |
| 32 virtual double TimerNow() OVERRIDE { return now_; } | 32 virtual double TimerNow() OVERRIDE { return now_; } |
| 33 void set_now(double now) { now_ = now; } | 33 void set_now(double now) { now_ = now; } |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 double now_; | 36 double now_; |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 class FakeDatagramServerSocket : public net::DatagramServerSocket { | 39 class FakeDatagramServerSocket : public net::DatagramServerSocket { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); | 190 socket_host_.reset(new P2PSocketHostUdp(&sender_, 0, &throttler_)); |
| 191 socket_ = new FakeDatagramServerSocket(&sent_packets_); | 191 socket_ = new FakeDatagramServerSocket(&sent_packets_); |
| 192 socket_host_->socket_.reset(socket_); | 192 socket_host_->socket_.reset(socket_); |
| 193 | 193 |
| 194 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | 194 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); |
| 195 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); | 195 socket_host_->Init(local_address_, P2PHostAndIPEndPoint()); |
| 196 | 196 |
| 197 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); | 197 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); |
| 198 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); | 198 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); |
| 199 | 199 |
| 200 scoped_ptr<talk_base::Timing> timing(new FakeTiming()); | 200 scoped_ptr<rtc::Timing> timing(new FakeTiming()); |
| 201 throttler_.SetTiming(timing.Pass()); | 201 throttler_.SetTiming(timing.Pass()); |
| 202 } | 202 } |
| 203 | 203 |
| 204 P2PMessageThrottler throttler_; | 204 P2PMessageThrottler throttler_; |
| 205 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; | 205 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; |
| 206 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. | 206 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. |
| 207 scoped_ptr<P2PSocketHostUdp> socket_host_; | 207 scoped_ptr<P2PSocketHostUdp> socket_host_; |
| 208 MockIPCSender sender_; | 208 MockIPCSender sender_; |
| 209 | 209 |
| 210 net::IPEndPoint local_address_; | 210 net::IPEndPoint local_address_; |
| 211 | 211 |
| 212 net::IPEndPoint dest1_; | 212 net::IPEndPoint dest1_; |
| 213 net::IPEndPoint dest2_; | 213 net::IPEndPoint dest2_; |
| 214 }; | 214 }; |
| 215 | 215 |
| 216 // Verify that we can send STUN messages before we receive anything | 216 // Verify that we can send STUN messages before we receive anything |
| 217 // from the other side. | 217 // from the other side. |
| 218 TEST_F(P2PSocketHostUdpTest, SendStunNoAuth) { | 218 TEST_F(P2PSocketHostUdpTest, SendStunNoAuth) { |
| 219 EXPECT_CALL(sender_, Send( | 219 EXPECT_CALL(sender_, Send( |
| 220 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 220 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 221 .Times(3) | 221 .Times(3) |
| 222 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 222 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 223 | 223 |
| 224 talk_base::PacketOptions options; | 224 rtc::PacketOptions options; |
| 225 std::vector<char> packet1; | 225 std::vector<char> packet1; |
| 226 CreateStunRequest(&packet1); | 226 CreateStunRequest(&packet1); |
| 227 socket_host_->Send(dest1_, packet1, options, 0); | 227 socket_host_->Send(dest1_, packet1, options, 0); |
| 228 | 228 |
| 229 std::vector<char> packet2; | 229 std::vector<char> packet2; |
| 230 CreateStunResponse(&packet2); | 230 CreateStunResponse(&packet2); |
| 231 socket_host_->Send(dest1_, packet2, options, 0); | 231 socket_host_->Send(dest1_, packet2, options, 0); |
| 232 | 232 |
| 233 std::vector<char> packet3; | 233 std::vector<char> packet3; |
| 234 CreateStunError(&packet3); | 234 CreateStunError(&packet3); |
| 235 socket_host_->Send(dest1_, packet3, options, 0); | 235 socket_host_->Send(dest1_, packet3, options, 0); |
| 236 | 236 |
| 237 ASSERT_EQ(sent_packets_.size(), 3U); | 237 ASSERT_EQ(sent_packets_.size(), 3U); |
| 238 ASSERT_EQ(sent_packets_[0].second, packet1); | 238 ASSERT_EQ(sent_packets_[0].second, packet1); |
| 239 ASSERT_EQ(sent_packets_[1].second, packet2); | 239 ASSERT_EQ(sent_packets_[1].second, packet2); |
| 240 ASSERT_EQ(sent_packets_[2].second, packet3); | 240 ASSERT_EQ(sent_packets_[2].second, packet3); |
| 241 } | 241 } |
| 242 | 242 |
| 243 // Verify that no data packets can be sent before STUN binding has | 243 // Verify that no data packets can be sent before STUN binding has |
| 244 // finished. | 244 // finished. |
| 245 TEST_F(P2PSocketHostUdpTest, SendDataNoAuth) { | 245 TEST_F(P2PSocketHostUdpTest, SendDataNoAuth) { |
| 246 EXPECT_CALL(sender_, Send( | 246 EXPECT_CALL(sender_, Send( |
| 247 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 247 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
| 248 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 248 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 249 | 249 |
| 250 talk_base::PacketOptions options; | 250 rtc::PacketOptions options; |
| 251 std::vector<char> packet; | 251 std::vector<char> packet; |
| 252 CreateRandomPacket(&packet); | 252 CreateRandomPacket(&packet); |
| 253 socket_host_->Send(dest1_, packet, options, 0); | 253 socket_host_->Send(dest1_, packet, options, 0); |
| 254 | 254 |
| 255 ASSERT_EQ(sent_packets_.size(), 0U); | 255 ASSERT_EQ(sent_packets_.size(), 0U); |
| 256 } | 256 } |
| 257 | 257 |
| 258 // Verify that we can send data after we've received STUN request | 258 // Verify that we can send data after we've received STUN request |
| 259 // from the other side. | 259 // from the other side. |
| 260 TEST_F(P2PSocketHostUdpTest, SendAfterStunRequest) { | 260 TEST_F(P2PSocketHostUdpTest, SendAfterStunRequest) { |
| 261 // Receive packet from |dest1_|. | 261 // Receive packet from |dest1_|. |
| 262 std::vector<char> request_packet; | 262 std::vector<char> request_packet; |
| 263 CreateStunRequest(&request_packet); | 263 CreateStunRequest(&request_packet); |
| 264 | 264 |
| 265 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 265 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 266 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 266 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 267 socket_->ReceivePacket(dest1_, request_packet); | 267 socket_->ReceivePacket(dest1_, request_packet); |
| 268 | 268 |
| 269 // Now we should be able to send any data to |dest1_|. | 269 // Now we should be able to send any data to |dest1_|. |
| 270 EXPECT_CALL(sender_, Send( | 270 EXPECT_CALL(sender_, Send( |
| 271 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 271 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 272 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 272 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 273 | 273 |
| 274 talk_base::PacketOptions options; | 274 rtc::PacketOptions options; |
| 275 std::vector<char> packet; | 275 std::vector<char> packet; |
| 276 CreateRandomPacket(&packet); | 276 CreateRandomPacket(&packet); |
| 277 socket_host_->Send(dest1_, packet, options, 0); | 277 socket_host_->Send(dest1_, packet, options, 0); |
| 278 | 278 |
| 279 ASSERT_EQ(1U, sent_packets_.size()); | 279 ASSERT_EQ(1U, sent_packets_.size()); |
| 280 ASSERT_EQ(dest1_, sent_packets_[0].first); | 280 ASSERT_EQ(dest1_, sent_packets_[0].first); |
| 281 } | 281 } |
| 282 | 282 |
| 283 // Verify that we can send data after we've received STUN response | 283 // Verify that we can send data after we've received STUN response |
| 284 // from the other side. | 284 // from the other side. |
| 285 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponse) { | 285 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponse) { |
| 286 // Receive packet from |dest1_|. | 286 // Receive packet from |dest1_|. |
| 287 std::vector<char> request_packet; | 287 std::vector<char> request_packet; |
| 288 CreateStunRequest(&request_packet); | 288 CreateStunRequest(&request_packet); |
| 289 | 289 |
| 290 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 290 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 291 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 291 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 292 socket_->ReceivePacket(dest1_, request_packet); | 292 socket_->ReceivePacket(dest1_, request_packet); |
| 293 | 293 |
| 294 // Now we should be able to send any data to |dest1_|. | 294 // Now we should be able to send any data to |dest1_|. |
| 295 EXPECT_CALL(sender_, Send( | 295 EXPECT_CALL(sender_, Send( |
| 296 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 296 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 297 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 297 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 298 | 298 |
| 299 talk_base::PacketOptions options; | 299 rtc::PacketOptions options; |
| 300 std::vector<char> packet; | 300 std::vector<char> packet; |
| 301 CreateRandomPacket(&packet); | 301 CreateRandomPacket(&packet); |
| 302 socket_host_->Send(dest1_, packet, options, 0); | 302 socket_host_->Send(dest1_, packet, options, 0); |
| 303 | 303 |
| 304 ASSERT_EQ(1U, sent_packets_.size()); | 304 ASSERT_EQ(1U, sent_packets_.size()); |
| 305 ASSERT_EQ(dest1_, sent_packets_[0].first); | 305 ASSERT_EQ(dest1_, sent_packets_[0].first); |
| 306 } | 306 } |
| 307 | 307 |
| 308 // Verify messages still cannot be sent to an unathorized host after | 308 // Verify messages still cannot be sent to an unathorized host after |
| 309 // successful binding with different host. | 309 // successful binding with different host. |
| 310 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponseDifferentHost) { | 310 TEST_F(P2PSocketHostUdpTest, SendAfterStunResponseDifferentHost) { |
| 311 // Receive packet from |dest1_|. | 311 // Receive packet from |dest1_|. |
| 312 std::vector<char> request_packet; | 312 std::vector<char> request_packet; |
| 313 CreateStunRequest(&request_packet); | 313 CreateStunRequest(&request_packet); |
| 314 | 314 |
| 315 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 315 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 316 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 316 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 317 socket_->ReceivePacket(dest1_, request_packet); | 317 socket_->ReceivePacket(dest1_, request_packet); |
| 318 | 318 |
| 319 // Should fail when trying to send the same packet to |dest2_|. | 319 // Should fail when trying to send the same packet to |dest2_|. |
| 320 talk_base::PacketOptions options; | 320 rtc::PacketOptions options; |
| 321 std::vector<char> packet; | 321 std::vector<char> packet; |
| 322 CreateRandomPacket(&packet); | 322 CreateRandomPacket(&packet); |
| 323 EXPECT_CALL(sender_, Send( | 323 EXPECT_CALL(sender_, Send( |
| 324 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) | 324 MatchMessage(static_cast<uint32>(P2PMsg_OnError::ID)))) |
| 325 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 325 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 326 socket_host_->Send(dest2_, packet, options, 0); | 326 socket_host_->Send(dest2_, packet, options, 0); |
| 327 } | 327 } |
| 328 | 328 |
| 329 // Verify throttler not allowing unlimited sending of ICE messages to | 329 // Verify throttler not allowing unlimited sending of ICE messages to |
| 330 // any destination. | 330 // any destination. |
| 331 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimit) { | 331 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimit) { |
| 332 EXPECT_CALL(sender_, Send( | 332 EXPECT_CALL(sender_, Send( |
| 333 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 333 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 334 .Times(2) | 334 .Times(2) |
| 335 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 335 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 336 | 336 |
| 337 talk_base::PacketOptions options; | 337 rtc::PacketOptions options; |
| 338 std::vector<char> packet1; | 338 std::vector<char> packet1; |
| 339 CreateStunRequest(&packet1); | 339 CreateStunRequest(&packet1); |
| 340 throttler_.SetSendIceBandwidth(packet1.size() * 2); | 340 throttler_.SetSendIceBandwidth(packet1.size() * 2); |
| 341 socket_host_->Send(dest1_, packet1, options, 0); | 341 socket_host_->Send(dest1_, packet1, options, 0); |
| 342 socket_host_->Send(dest2_, packet1, options, 0); | 342 socket_host_->Send(dest2_, packet1, options, 0); |
| 343 | 343 |
| 344 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2222); | 344 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2222); |
| 345 // This packet must be dropped by the throttler. | 345 // This packet must be dropped by the throttler. |
| 346 socket_host_->Send(dest3, packet1, options, 0); | 346 socket_host_->Send(dest3, packet1, options, 0); |
| 347 ASSERT_EQ(sent_packets_.size(), 2U); | 347 ASSERT_EQ(sent_packets_.size(), 2U); |
| 348 } | 348 } |
| 349 | 349 |
| 350 // Verify we can send packets to a known destination when ICE throttling is | 350 // Verify we can send packets to a known destination when ICE throttling is |
| 351 // active. | 351 // active. |
| 352 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) { | 352 TEST_F(P2PSocketHostUdpTest, ThrottleAfterLimitAfterReceive) { |
| 353 // Receive packet from |dest1_|. | 353 // Receive packet from |dest1_|. |
| 354 std::vector<char> request_packet; | 354 std::vector<char> request_packet; |
| 355 CreateStunRequest(&request_packet); | 355 CreateStunRequest(&request_packet); |
| 356 | 356 |
| 357 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) | 357 EXPECT_CALL(sender_, Send(MatchPacketMessage(request_packet))) |
| 358 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 358 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 359 socket_->ReceivePacket(dest1_, request_packet); | 359 socket_->ReceivePacket(dest1_, request_packet); |
| 360 | 360 |
| 361 EXPECT_CALL(sender_, Send( | 361 EXPECT_CALL(sender_, Send( |
| 362 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 362 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 363 .Times(4) | 363 .Times(4) |
| 364 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 364 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 365 | 365 |
| 366 talk_base::PacketOptions options; | 366 rtc::PacketOptions options; |
| 367 std::vector<char> packet1; | 367 std::vector<char> packet1; |
| 368 CreateStunRequest(&packet1); | 368 CreateStunRequest(&packet1); |
| 369 throttler_.SetSendIceBandwidth(packet1.size()); | 369 throttler_.SetSendIceBandwidth(packet1.size()); |
| 370 // |dest1_| is known address, throttling will not be applied. | 370 // |dest1_| is known address, throttling will not be applied. |
| 371 socket_host_->Send(dest1_, packet1, options, 0); | 371 socket_host_->Send(dest1_, packet1, options, 0); |
| 372 // Trying to send the packet to dest1_ in the same window. It should go. | 372 // Trying to send the packet to dest1_ in the same window. It should go. |
| 373 socket_host_->Send(dest1_, packet1, options, 0); | 373 socket_host_->Send(dest1_, packet1, options, 0); |
| 374 | 374 |
| 375 // Throttler should allow this packet to go through. | 375 // Throttler should allow this packet to go through. |
| 376 socket_host_->Send(dest2_, packet1, options, 0); | 376 socket_host_->Send(dest2_, packet1, options, 0); |
| 377 | 377 |
| 378 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2223); | 378 net::IPEndPoint dest3 = ParseAddress(kTestIpAddress1, 2223); |
| 379 // This packet will be dropped, as limit only for a single packet. | 379 // This packet will be dropped, as limit only for a single packet. |
| 380 socket_host_->Send(dest3, packet1, options, 0); | 380 socket_host_->Send(dest3, packet1, options, 0); |
| 381 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); | 381 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); |
| 382 // This packet should also be dropped. | 382 // This packet should also be dropped. |
| 383 socket_host_->Send(dest4, packet1, options, 0); | 383 socket_host_->Send(dest4, packet1, options, 0); |
| 384 // |dest1| is known, we can send as many packets to it. | 384 // |dest1| is known, we can send as many packets to it. |
| 385 socket_host_->Send(dest1_, packet1, options, 0); | 385 socket_host_->Send(dest1_, packet1, options, 0); |
| 386 ASSERT_EQ(sent_packets_.size(), 4U); | 386 ASSERT_EQ(sent_packets_.size(), 4U); |
| 387 } | 387 } |
| 388 | 388 |
| 389 } // namespace content | 389 } // namespace content |
| OLD | NEW |