Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "p2p/base/regathering_controller.h" |
| 12 | |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 13 | #include <map> |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "p2p/base/fake_port_allocator.h" |
| 20 | #include "p2p/base/mock_ice_transport.h" |
| 21 | #include "p2p/base/p2p_constants.h" |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 22 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "p2p/base/stun_server.h" |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 24 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/socket_address.h" |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 26 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/virtual_socket_server.h" |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN | |
| 32 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 33 | cricket::PORTALLOCATOR_DISABLE_TCP; |
| 34 | // The address of the public STUN server. |
| 35 | const rtc::SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT); |
| 36 | // The addresses for the public TURN server. |
| 37 | const rtc::SocketAddress kTurnUdpIntAddr("99.99.99.3", |
| 38 | cricket::STUN_SERVER_PORT); |
| 39 | const cricket::RelayCredentials kRelayCredentials("test", "test"); |
| 40 | const char kIceUfrag[] = "UF00"; |
| 41 | const char kIcePwd[] = "TESTICEPWD00000000000000"; |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | namespace webrtc { |
| 46 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 47 | class RegatheringControllerTest : public ::testing::Test, |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 48 | public sigslot::has_slots<> { |
| 49 | public: |
| 50 | RegatheringControllerTest() |
| 51 | : vss_(new rtc::VirtualSocketServer()), |
| 52 | thread_(vss_.get()), |
| 53 | ice_transport_(new cricket::MockIceTransport()), |
| 54 | allocator_( |
| 55 | new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)) { |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame] | 56 | BasicRegatheringController::Config regathering_config; |
| 57 | regathering_config.regather_on_failed_networks_interval = 0; |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 58 | regathering_controller_.reset(new BasicRegatheringController( |
| 59 | regathering_config, ice_transport_.get(), rtc::Thread::Current())); |
| 60 | } |
| 61 | |
| 62 | // Initializes the allocator and gathers candidates once by StartGettingPorts. |
| 63 | void InitializeAndGatherOnce() { |
| 64 | cricket::ServerAddresses stun_servers; |
| 65 | stun_servers.insert(kStunAddr); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 66 | cricket::RelayServerConfig turn_server; |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 67 | turn_server.credentials = kRelayCredentials; |
| 68 | turn_server.ports.push_back( |
| 69 | cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP)); |
| 70 | std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server); |
| 71 | allocator_->set_flags(kOnlyLocalPorts); |
| 72 | allocator_->SetConfiguration(stun_servers, turn_servers, 0 /* pool size */, |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 73 | webrtc::NO_PRUNE); |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 74 | allocator_session_ = allocator_->CreateSession( |
| 75 | "test", cricket::ICE_CANDIDATE_COMPONENT_RTP, kIceUfrag, kIcePwd); |
| 76 | // The gathering will take place on the current thread and the following |
| 77 | // call of StartGettingPorts is blocking. We will not ClearGettingPorts |
| 78 | // prematurely. |
| 79 | allocator_session_->StartGettingPorts(); |
| 80 | allocator_session_->SignalIceRegathering.connect( |
| 81 | this, &RegatheringControllerTest::OnIceRegathering); |
| 82 | regathering_controller_->set_allocator_session(allocator_session_.get()); |
| 83 | } |
| 84 | |
| 85 | // The regathering controller is initialized with the allocator session |
| 86 | // cleared. Only after clearing the session, we would be able to regather. See |
| 87 | // the comments for BasicRegatheringController in regatheringcontroller.h. |
| 88 | void InitializeAndGatherOnceWithSessionCleared() { |
| 89 | InitializeAndGatherOnce(); |
| 90 | allocator_session_->ClearGettingPorts(); |
| 91 | } |
| 92 | |
| 93 | void OnIceRegathering(cricket::PortAllocatorSession* allocator_session, |
| 94 | cricket::IceRegatheringReason reason) { |
| 95 | ++count_[reason]; |
| 96 | } |
| 97 | |
| 98 | int GetRegatheringReasonCount(cricket::IceRegatheringReason reason) { |
| 99 | return count_[reason]; |
| 100 | } |
| 101 | |
| 102 | BasicRegatheringController* regathering_controller() { |
| 103 | return regathering_controller_.get(); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 108 | rtc::AutoSocketServerThread thread_; |
| 109 | std::unique_ptr<cricket::IceTransportInternal> ice_transport_; |
| 110 | std::unique_ptr<BasicRegatheringController> regathering_controller_; |
| 111 | std::unique_ptr<cricket::PortAllocator> allocator_; |
| 112 | std::unique_ptr<cricket::PortAllocatorSession> allocator_session_; |
| 113 | std::map<cricket::IceRegatheringReason, int> count_; |
| 114 | }; |
| 115 | |
| 116 | // Tests that ICE regathering occurs only if the port allocator session is |
| 117 | // cleared. A port allocation session is not cleared if the initial gathering is |
| 118 | // still in progress or the continual gathering is not enabled. |
| 119 | TEST_F(RegatheringControllerTest, |
| 120 | IceRegatheringDoesNotOccurIfSessionNotCleared) { |
| 121 | rtc::ScopedFakeClock clock; |
| 122 | InitializeAndGatherOnce(); // Session not cleared. |
| 123 | |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame] | 124 | BasicRegatheringController::Config config; |
| 125 | config.regather_on_failed_networks_interval = 2000; |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 126 | regathering_controller()->SetConfig(config); |
| 127 | regathering_controller()->Start(); |
| 128 | SIMULATED_WAIT(false, 10000, clock); |
| 129 | // Expect no regathering in the last 10s. |
| 130 | EXPECT_EQ(0, GetRegatheringReasonCount( |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 131 | cricket::IceRegatheringReason::NETWORK_FAILURE)); |
| 132 | } |
| 133 | |
| 134 | TEST_F(RegatheringControllerTest, IceRegatheringRepeatsAsScheduled) { |
| 135 | rtc::ScopedFakeClock clock; |
| 136 | InitializeAndGatherOnceWithSessionCleared(); |
| 137 | |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame] | 138 | BasicRegatheringController::Config config; |
| 139 | config.regather_on_failed_networks_interval = 2000; |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 140 | regathering_controller()->SetConfig(config); |
| 141 | regathering_controller()->Start(); |
| 142 | SIMULATED_WAIT(false, 2000 - 1, clock); |
| 143 | // Expect no regathering. |
| 144 | EXPECT_EQ(0, GetRegatheringReasonCount( |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 145 | cricket::IceRegatheringReason::NETWORK_FAILURE)); |
| 146 | SIMULATED_WAIT(false, 2, clock); |
| 147 | // Expect regathering on all networks and on failed networks to happen once |
| 148 | // respectively in that last 2s with 2s interval. |
| 149 | EXPECT_EQ(1, GetRegatheringReasonCount( |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 150 | cricket::IceRegatheringReason::NETWORK_FAILURE)); |
| 151 | SIMULATED_WAIT(false, 11000, clock); |
| 152 | // Expect regathering to happen for another 5 times in 11s with 2s interval. |
| 153 | EXPECT_EQ(6, GetRegatheringReasonCount( |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 154 | cricket::IceRegatheringReason::NETWORK_FAILURE)); |
| 155 | } |
| 156 | |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 157 | // Tests that the schedule of ICE regathering on failed networks can be canceled |
| 158 | // and replaced by a new recurring schedule. |
| 159 | TEST_F(RegatheringControllerTest, |
| 160 | ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced) { |
| 161 | rtc::ScopedFakeClock clock; |
| 162 | InitializeAndGatherOnceWithSessionCleared(); |
| 163 | |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame] | 164 | BasicRegatheringController::Config config; |
| 165 | config.regather_on_failed_networks_interval = 2000; |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 166 | regathering_controller()->SetConfig(config); |
| 167 | regathering_controller()->Start(); |
| 168 | config.regather_on_failed_networks_interval = 5000; |
| 169 | regathering_controller()->SetConfig(config); |
| 170 | SIMULATED_WAIT(false, 3000, clock); |
| 171 | // Expect no regathering from the previous schedule. |
| 172 | EXPECT_EQ(0, GetRegatheringReasonCount( |
| 173 | cricket::IceRegatheringReason::NETWORK_FAILURE)); |
| 174 | SIMULATED_WAIT(false, 11000 - 3000, clock); |
| 175 | // Expect regathering to happen twice in the last 11s with 5s interval. |
| 176 | EXPECT_EQ(2, GetRegatheringReasonCount( |
| 177 | cricket::IceRegatheringReason::NETWORK_FAILURE)); |
| 178 | } |
| 179 | |
| 180 | } // namespace webrtc |