blob: 91b7270f77ef20e6d344f54dc9f18750f6302dd2 [file] [log] [blame]
Qingsi Wang1b368942018-06-13 13:54:08 -07001/*
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 Olssona4d87372019-07-05 19:08:33 +020011#include "p2p/base/regathering_controller.h"
12
Qingsi Wang1b368942018-06-13 13:54:08 -070013#include <map>
14#include <memory>
15#include <string>
16#include <vector>
17
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "p2p/base/fake_port_allocator.h"
20#include "p2p/base/mock_ice_transport.h"
21#include "p2p/base/p2p_constants.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070022#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "p2p/base/stun_server.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070024#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/socket_address.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070026#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/virtual_socket_server.h"
Sameer Vijaykar0793ee72023-01-23 16:31:29 +010028#include "test/scoped_key_value_config.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070029
30namespace {
31
32const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
33 cricket::PORTALLOCATOR_DISABLE_RELAY |
34 cricket::PORTALLOCATOR_DISABLE_TCP;
35// The address of the public STUN server.
36const rtc::SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
37// The addresses for the public TURN server.
38const rtc::SocketAddress kTurnUdpIntAddr("99.99.99.3",
39 cricket::STUN_SERVER_PORT);
40const cricket::RelayCredentials kRelayCredentials("test", "test");
41const char kIceUfrag[] = "UF00";
42const char kIcePwd[] = "TESTICEPWD00000000000000";
Philipp Hanckefd91d022022-10-27 20:08:23 +020043constexpr uint64_t kTiebreakerDefault = 44444;
Qingsi Wang1b368942018-06-13 13:54:08 -070044
45} // namespace
46
47namespace webrtc {
48
Mirko Bonadei6a489f22019-04-09 15:11:12 +020049class RegatheringControllerTest : public ::testing::Test,
Qingsi Wang1b368942018-06-13 13:54:08 -070050 public sigslot::has_slots<> {
51 public:
52 RegatheringControllerTest()
Byoungchan Leed58f5262022-06-27 18:05:22 +090053 : vss_(std::make_unique<rtc::VirtualSocketServer>()),
Qingsi Wang1b368942018-06-13 13:54:08 -070054 thread_(vss_.get()),
Byoungchan Leed58f5262022-06-27 18:05:22 +090055 ice_transport_(std::make_unique<cricket::MockIceTransport>()),
56 packet_socket_factory_(
57 std::make_unique<rtc::BasicPacketSocketFactory>(vss_.get())),
58 allocator_(std::make_unique<cricket::FakePortAllocator>(
59 rtc::Thread::Current(),
Sameer Vijaykar0793ee72023-01-23 16:31:29 +010060 packet_socket_factory_.get(),
61 &field_trials_)) {
Philipp Hanckefd91d022022-10-27 20:08:23 +020062 allocator_->SetIceTiebreaker(kTiebreakerDefault);
Steve Antonf4172382020-01-27 15:45:02 -080063 BasicRegatheringController::Config regathering_config;
64 regathering_config.regather_on_failed_networks_interval = 0;
Qingsi Wang1b368942018-06-13 13:54:08 -070065 regathering_controller_.reset(new BasicRegatheringController(
66 regathering_config, ice_transport_.get(), rtc::Thread::Current()));
67 }
68
69 // Initializes the allocator and gathers candidates once by StartGettingPorts.
70 void InitializeAndGatherOnce() {
71 cricket::ServerAddresses stun_servers;
72 stun_servers.insert(kStunAddr);
Niels Möller191e38f2019-11-04 08:49:12 +010073 cricket::RelayServerConfig turn_server;
Qingsi Wang1b368942018-06-13 13:54:08 -070074 turn_server.credentials = kRelayCredentials;
75 turn_server.ports.push_back(
76 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP));
77 std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server);
78 allocator_->set_flags(kOnlyLocalPorts);
79 allocator_->SetConfiguration(stun_servers, turn_servers, 0 /* pool size */,
Honghai Zhangf8998cf2019-10-14 11:27:50 -070080 webrtc::NO_PRUNE);
Qingsi Wang1b368942018-06-13 13:54:08 -070081 allocator_session_ = allocator_->CreateSession(
82 "test", cricket::ICE_CANDIDATE_COMPONENT_RTP, kIceUfrag, kIcePwd);
83 // The gathering will take place on the current thread and the following
84 // call of StartGettingPorts is blocking. We will not ClearGettingPorts
85 // prematurely.
86 allocator_session_->StartGettingPorts();
87 allocator_session_->SignalIceRegathering.connect(
88 this, &RegatheringControllerTest::OnIceRegathering);
89 regathering_controller_->set_allocator_session(allocator_session_.get());
90 }
91
92 // The regathering controller is initialized with the allocator session
93 // cleared. Only after clearing the session, we would be able to regather. See
94 // the comments for BasicRegatheringController in regatheringcontroller.h.
95 void InitializeAndGatherOnceWithSessionCleared() {
96 InitializeAndGatherOnce();
97 allocator_session_->ClearGettingPorts();
98 }
99
100 void OnIceRegathering(cricket::PortAllocatorSession* allocator_session,
101 cricket::IceRegatheringReason reason) {
102 ++count_[reason];
103 }
104
105 int GetRegatheringReasonCount(cricket::IceRegatheringReason reason) {
106 return count_[reason];
107 }
108
109 BasicRegatheringController* regathering_controller() {
110 return regathering_controller_.get();
111 }
112
113 private:
Sameer Vijaykar0793ee72023-01-23 16:31:29 +0100114 webrtc::test::ScopedKeyValueConfig field_trials_;
Qingsi Wang1b368942018-06-13 13:54:08 -0700115 std::unique_ptr<rtc::VirtualSocketServer> vss_;
116 rtc::AutoSocketServerThread thread_;
117 std::unique_ptr<cricket::IceTransportInternal> ice_transport_;
118 std::unique_ptr<BasicRegatheringController> regathering_controller_;
Byoungchan Leed58f5262022-06-27 18:05:22 +0900119 std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
Qingsi Wang1b368942018-06-13 13:54:08 -0700120 std::unique_ptr<cricket::PortAllocator> allocator_;
121 std::unique_ptr<cricket::PortAllocatorSession> allocator_session_;
122 std::map<cricket::IceRegatheringReason, int> count_;
123};
124
125// Tests that ICE regathering occurs only if the port allocator session is
126// cleared. A port allocation session is not cleared if the initial gathering is
127// still in progress or the continual gathering is not enabled.
128TEST_F(RegatheringControllerTest,
129 IceRegatheringDoesNotOccurIfSessionNotCleared) {
130 rtc::ScopedFakeClock clock;
131 InitializeAndGatherOnce(); // Session not cleared.
132
Steve Antonf4172382020-01-27 15:45:02 -0800133 BasicRegatheringController::Config config;
134 config.regather_on_failed_networks_interval = 2000;
Qingsi Wang1b368942018-06-13 13:54:08 -0700135 regathering_controller()->SetConfig(config);
136 regathering_controller()->Start();
137 SIMULATED_WAIT(false, 10000, clock);
138 // Expect no regathering in the last 10s.
139 EXPECT_EQ(0, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700140 cricket::IceRegatheringReason::NETWORK_FAILURE));
141}
142
143TEST_F(RegatheringControllerTest, IceRegatheringRepeatsAsScheduled) {
144 rtc::ScopedFakeClock clock;
145 InitializeAndGatherOnceWithSessionCleared();
146
Steve Antonf4172382020-01-27 15:45:02 -0800147 BasicRegatheringController::Config config;
148 config.regather_on_failed_networks_interval = 2000;
Qingsi Wang1b368942018-06-13 13:54:08 -0700149 regathering_controller()->SetConfig(config);
150 regathering_controller()->Start();
151 SIMULATED_WAIT(false, 2000 - 1, clock);
152 // Expect no regathering.
153 EXPECT_EQ(0, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700154 cricket::IceRegatheringReason::NETWORK_FAILURE));
155 SIMULATED_WAIT(false, 2, clock);
156 // Expect regathering on all networks and on failed networks to happen once
157 // respectively in that last 2s with 2s interval.
158 EXPECT_EQ(1, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700159 cricket::IceRegatheringReason::NETWORK_FAILURE));
160 SIMULATED_WAIT(false, 11000, clock);
161 // Expect regathering to happen for another 5 times in 11s with 2s interval.
162 EXPECT_EQ(6, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700163 cricket::IceRegatheringReason::NETWORK_FAILURE));
164}
165
Qingsi Wang1b368942018-06-13 13:54:08 -0700166// Tests that the schedule of ICE regathering on failed networks can be canceled
167// and replaced by a new recurring schedule.
168TEST_F(RegatheringControllerTest,
169 ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced) {
170 rtc::ScopedFakeClock clock;
171 InitializeAndGatherOnceWithSessionCleared();
172
Steve Antonf4172382020-01-27 15:45:02 -0800173 BasicRegatheringController::Config config;
174 config.regather_on_failed_networks_interval = 2000;
Qingsi Wang1b368942018-06-13 13:54:08 -0700175 regathering_controller()->SetConfig(config);
176 regathering_controller()->Start();
177 config.regather_on_failed_networks_interval = 5000;
178 regathering_controller()->SetConfig(config);
179 SIMULATED_WAIT(false, 3000, clock);
180 // Expect no regathering from the previous schedule.
181 EXPECT_EQ(0, GetRegatheringReasonCount(
182 cricket::IceRegatheringReason::NETWORK_FAILURE));
183 SIMULATED_WAIT(false, 11000 - 3000, clock);
184 // Expect regathering to happen twice in the last 11s with 5s interval.
185 EXPECT_EQ(2, GetRegatheringReasonCount(
186 cricket::IceRegatheringReason::NETWORK_FAILURE));
187}
188
189} // namespace webrtc