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