| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 using ::testing::_; | 25 using ::testing::_; |
| 26 using ::testing::DeleteArg; | 26 using ::testing::DeleteArg; |
| 27 using ::testing::DoAll; | 27 using ::testing::DoAll; |
| 28 using ::testing::Return; | 28 using ::testing::Return; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // TODO(nisse): We can't currently use rtc::ScopedFakeClock, because | 32 // TODO(nisse): We can't currently use rtc::ScopedFakeClock, because |
| 33 // we don't link with webrtc rtc_base_tests_utils. So roll our own, | 33 // we don't link with webrtc rtc_base_tests_utils. So roll our own. |
| 34 // for the special case of a clock standing still at zero. | |
| 35 | 34 |
| 36 // Creating an object of this class makes rtc::TimeMicros() and | 35 // Creating an object of this class makes rtc::TimeMicros() and |
| 37 // related functions return zero for the lifetime of the object. | 36 // related functions return zero unless the clock is advanced. |
| 38 class ScopedFakeZeroClock : public rtc::ClockInterface { | 37 class ScopedFakeClock : public rtc::ClockInterface { |
| 39 public: | 38 public: |
| 40 ScopedFakeZeroClock() { | 39 ScopedFakeClock() { prev_clock_ = rtc::SetClockForTesting(this); } |
| 41 prev_clock_ = rtc::SetClockForTesting(this); | 40 ~ScopedFakeClock() override { rtc::SetClockForTesting(prev_clock_); } |
| 42 } | |
| 43 ~ScopedFakeZeroClock() override { | |
| 44 rtc::SetClockForTesting(prev_clock_); | |
| 45 } | |
| 46 // ClockInterface implementation. | 41 // ClockInterface implementation. |
| 47 uint64_t TimeNanos() const override { | 42 uint64_t TimeNanos() const override { return time_nanos_; } |
| 48 return 0; | 43 void SetTimeNanos(uint64_t time_nanos) { time_nanos_ = time_nanos; } |
| 49 } | 44 |
| 50 private: | 45 private: |
| 51 ClockInterface* prev_clock_; | 46 ClockInterface* prev_clock_; |
| 47 uint64_t time_nanos_ = 0; |
| 52 }; | 48 }; |
| 53 | 49 |
| 54 class FakeDatagramServerSocket : public net::DatagramServerSocket { | 50 class FakeDatagramServerSocket : public net::DatagramServerSocket { |
| 55 public: | 51 public: |
| 56 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; | 52 typedef std::pair<net::IPEndPoint, std::vector<char> > UDPPacket; |
| 57 | 53 |
| 58 // P2PSocketHostUdp destroys a socket on errors so sent packets | 54 // P2PSocketHostUdp destroys a socket on errors so sent packets |
| 59 // need to be stored outside of this object. | 55 // need to be stored outside of this object. |
| 60 FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets, | 56 FakeDatagramServerSocket(std::deque<UDPPacket>* sent_packets, |
| 61 std::vector<uint16_t>* used_ports) | 57 std::vector<uint16_t>* used_ports) |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); | 219 dest1_ = ParseAddress(kTestIpAddress1, kTestPort1); |
| 224 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); | 220 dest2_ = ParseAddress(kTestIpAddress2, kTestPort2); |
| 225 } | 221 } |
| 226 | 222 |
| 227 static FakeDatagramServerSocket* GetSocketFromHost( | 223 static FakeDatagramServerSocket* GetSocketFromHost( |
| 228 P2PSocketHostUdp* socket_host) { | 224 P2PSocketHostUdp* socket_host) { |
| 229 return static_cast<FakeDatagramServerSocket*>(socket_host->socket_.get()); | 225 return static_cast<FakeDatagramServerSocket*>(socket_host->socket_.get()); |
| 230 } | 226 } |
| 231 | 227 |
| 232 P2PMessageThrottler throttler_; | 228 P2PMessageThrottler throttler_; |
| 233 ScopedFakeZeroClock fake_clock_; | 229 ScopedFakeClock fake_clock_; |
| 234 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; | 230 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets_; |
| 235 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. | 231 FakeDatagramServerSocket* socket_; // Owned by |socket_host_|. |
| 236 std::unique_ptr<P2PSocketHostUdp> socket_host_; | 232 std::unique_ptr<P2PSocketHostUdp> socket_host_; |
| 237 MockIPCSender sender_; | 233 MockIPCSender sender_; |
| 238 | 234 |
| 239 net::IPEndPoint local_address_; | 235 net::IPEndPoint local_address_; |
| 240 | 236 |
| 241 net::IPEndPoint dest1_; | 237 net::IPEndPoint dest1_; |
| 242 net::IPEndPoint dest2_; | 238 net::IPEndPoint dest2_; |
| 243 }; | 239 }; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 // This packet will be dropped, as limit only for a single packet. | 409 // This packet will be dropped, as limit only for a single packet. |
| 414 socket_host_->Send(dest3, packet1, options, 0); | 410 socket_host_->Send(dest3, packet1, options, 0); |
| 415 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); | 411 net::IPEndPoint dest4 = ParseAddress(kTestIpAddress1, 2224); |
| 416 // This packet should also be dropped. | 412 // This packet should also be dropped. |
| 417 socket_host_->Send(dest4, packet1, options, 0); | 413 socket_host_->Send(dest4, packet1, options, 0); |
| 418 // |dest1| is known, we can send as many packets to it. | 414 // |dest1| is known, we can send as many packets to it. |
| 419 socket_host_->Send(dest1_, packet1, options, 0); | 415 socket_host_->Send(dest1_, packet1, options, 0); |
| 420 ASSERT_EQ(sent_packets_.size(), 4U); | 416 ASSERT_EQ(sent_packets_.size(), 4U); |
| 421 } | 417 } |
| 422 | 418 |
| 419 // The fake clock mechanism used for this test doesn't work in component builds. |
| 420 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6490 |
| 421 #if defined(COMPONENT_BUILD) |
| 422 #define MAYBE_ThrottlingStopsAtExpectedTimes DISABLED_ThrottlingStopsAtExpectedT
imes |
| 423 #else |
| 424 #define MAYBE_ThrottlingStopsAtExpectedTimes ThrottlingStopsAtExpectedTimes |
| 425 #endif |
| 426 // Test that once the limit is hit, the throttling stops at the expected time, |
| 427 // allowing packets to be sent again. |
| 428 TEST_F(P2PSocketHostUdpTest, MAYBE_ThrottlingStopsAtExpectedTimes) { |
| 429 EXPECT_CALL( |
| 430 sender_, |
| 431 Send(MatchMessage(static_cast<uint32_t>(P2PMsg_OnSendComplete::ID)))) |
| 432 .Times(12) |
| 433 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 434 |
| 435 rtc::PacketOptions options; |
| 436 std::vector<char> packet; |
| 437 CreateStunRequest(&packet); |
| 438 // Limit of 2 packets per second. |
| 439 throttler_.SetSendIceBandwidth(packet.size() * 2); |
| 440 socket_host_->Send(dest1_, packet, options, 0); |
| 441 socket_host_->Send(dest2_, packet, options, 0); |
| 442 EXPECT_EQ(2U, sent_packets_.size()); |
| 443 |
| 444 // These packets must be dropped by the throttler since the limit was hit and |
| 445 // the time hasn't advanced. |
| 446 socket_host_->Send(dest1_, packet, options, 0); |
| 447 socket_host_->Send(dest2_, packet, options, 0); |
| 448 EXPECT_EQ(2U, sent_packets_.size()); |
| 449 |
| 450 // Advance the time to 0.999 seconds; throttling should still just barely be |
| 451 // active. |
| 452 fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 999); |
| 453 socket_host_->Send(dest1_, packet, options, 0); |
| 454 socket_host_->Send(dest2_, packet, options, 0); |
| 455 EXPECT_EQ(2U, sent_packets_.size()); |
| 456 |
| 457 // After hitting the second mark, we should be able to send again. |
| 458 // Add an extra millisecond to account for rounding errors. |
| 459 fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 1001); |
| 460 socket_host_->Send(dest1_, packet, options, 0); |
| 461 EXPECT_EQ(3U, sent_packets_.size()); |
| 462 |
| 463 // This time, hit the limit in the middle of the period. |
| 464 fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 1500); |
| 465 socket_host_->Send(dest2_, packet, options, 0); |
| 466 EXPECT_EQ(4U, sent_packets_.size()); |
| 467 |
| 468 // Again, throttling should be active until the next second mark. |
| 469 fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 1999); |
| 470 socket_host_->Send(dest1_, packet, options, 0); |
| 471 socket_host_->Send(dest2_, packet, options, 0); |
| 472 EXPECT_EQ(4U, sent_packets_.size()); |
| 473 fake_clock_.SetTimeNanos(rtc::kNumNanosecsPerMillisec * 2002); |
| 474 socket_host_->Send(dest1_, packet, options, 0); |
| 475 socket_host_->Send(dest2_, packet, options, 0); |
| 476 EXPECT_EQ(6U, sent_packets_.size()); |
| 477 } |
| 478 |
| 423 // Verify that we can open UDP sockets listening in a given port range, | 479 // Verify that we can open UDP sockets listening in a given port range, |
| 424 // and fail if all ports in the range are already in use. | 480 // and fail if all ports in the range are already in use. |
| 425 TEST_F(P2PSocketHostUdpTest, PortRangeImplicitPort) { | 481 TEST_F(P2PSocketHostUdpTest, PortRangeImplicitPort) { |
| 426 const uint16_t min_port = 10000; | 482 const uint16_t min_port = 10000; |
| 427 const uint16_t max_port = 10001; | 483 const uint16_t max_port = 10001; |
| 428 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets; | 484 std::deque<FakeDatagramServerSocket::UDPPacket> sent_packets; |
| 429 std::vector<uint16_t> used_ports; | 485 std::vector<uint16_t> used_ports; |
| 430 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory = | 486 P2PSocketHostUdp::DatagramServerSocketFactory fake_socket_factory = |
| 431 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports); | 487 base::Bind(&CreateFakeDatagramServerSocket, &sent_packets, &used_ports); |
| 432 P2PMessageThrottler throttler; | 488 P2PMessageThrottler throttler; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 std::unique_ptr<P2PSocketHostUdp> socket_host( | 567 std::unique_ptr<P2PSocketHostUdp> socket_host( |
| 512 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory)); | 568 new P2PSocketHostUdp(&sender, 0, &throttler, fake_socket_factory)); |
| 513 net::IPEndPoint local_address = | 569 net::IPEndPoint local_address = |
| 514 ParseAddress(kTestLocalIpAddress, invalid_port); | 570 ParseAddress(kTestLocalIpAddress, invalid_port); |
| 515 bool rv = socket_host->Init(local_address, min_port, max_port, | 571 bool rv = socket_host->Init(local_address, min_port, max_port, |
| 516 P2PHostAndIPEndPoint()); | 572 P2PHostAndIPEndPoint()); |
| 517 EXPECT_FALSE(rv); | 573 EXPECT_FALSE(rv); |
| 518 } | 574 } |
| 519 | 575 |
| 520 } // namespace content | 576 } // namespace content |
| OLD | NEW |