blob: 1617b92894582d6f1ac2216548c0d57c343aa209 [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/ref_counted_object.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/socket_address.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070027#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/virtual_socket_server.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";
43
44} // namespace
45
46namespace webrtc {
47
Mirko Bonadei6a489f22019-04-09 15:11:12 +020048class RegatheringControllerTest : public ::testing::Test,
Qingsi Wang1b368942018-06-13 13:54:08 -070049 public sigslot::has_slots<> {
50 public:
51 RegatheringControllerTest()
52 : vss_(new rtc::VirtualSocketServer()),
53 thread_(vss_.get()),
54 ice_transport_(new cricket::MockIceTransport()),
55 allocator_(
56 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)) {
Steve Antonf4172382020-01-27 15:45:02 -080057 BasicRegatheringController::Config regathering_config;
58 regathering_config.regather_on_failed_networks_interval = 0;
Qingsi Wang1b368942018-06-13 13:54:08 -070059 regathering_controller_.reset(new BasicRegatheringController(
60 regathering_config, ice_transport_.get(), rtc::Thread::Current()));
61 }
62
63 // Initializes the allocator and gathers candidates once by StartGettingPorts.
64 void InitializeAndGatherOnce() {
65 cricket::ServerAddresses stun_servers;
66 stun_servers.insert(kStunAddr);
Niels Möller191e38f2019-11-04 08:49:12 +010067 cricket::RelayServerConfig turn_server;
Qingsi Wang1b368942018-06-13 13:54:08 -070068 turn_server.credentials = kRelayCredentials;
69 turn_server.ports.push_back(
70 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP));
71 std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server);
72 allocator_->set_flags(kOnlyLocalPorts);
73 allocator_->SetConfiguration(stun_servers, turn_servers, 0 /* pool size */,
Honghai Zhangf8998cf2019-10-14 11:27:50 -070074 webrtc::NO_PRUNE);
Qingsi Wang1b368942018-06-13 13:54:08 -070075 allocator_session_ = allocator_->CreateSession(
76 "test", cricket::ICE_CANDIDATE_COMPONENT_RTP, kIceUfrag, kIcePwd);
77 // The gathering will take place on the current thread and the following
78 // call of StartGettingPorts is blocking. We will not ClearGettingPorts
79 // prematurely.
80 allocator_session_->StartGettingPorts();
81 allocator_session_->SignalIceRegathering.connect(
82 this, &RegatheringControllerTest::OnIceRegathering);
83 regathering_controller_->set_allocator_session(allocator_session_.get());
84 }
85
86 // The regathering controller is initialized with the allocator session
87 // cleared. Only after clearing the session, we would be able to regather. See
88 // the comments for BasicRegatheringController in regatheringcontroller.h.
89 void InitializeAndGatherOnceWithSessionCleared() {
90 InitializeAndGatherOnce();
91 allocator_session_->ClearGettingPorts();
92 }
93
94 void OnIceRegathering(cricket::PortAllocatorSession* allocator_session,
95 cricket::IceRegatheringReason reason) {
96 ++count_[reason];
97 }
98
99 int GetRegatheringReasonCount(cricket::IceRegatheringReason reason) {
100 return count_[reason];
101 }
102
103 BasicRegatheringController* regathering_controller() {
104 return regathering_controller_.get();
105 }
106
107 private:
108 std::unique_ptr<rtc::VirtualSocketServer> vss_;
109 rtc::AutoSocketServerThread thread_;
110 std::unique_ptr<cricket::IceTransportInternal> ice_transport_;
111 std::unique_ptr<BasicRegatheringController> regathering_controller_;
112 std::unique_ptr<cricket::PortAllocator> allocator_;
113 std::unique_ptr<cricket::PortAllocatorSession> allocator_session_;
114 std::map<cricket::IceRegatheringReason, int> count_;
115};
116
117// Tests that ICE regathering occurs only if the port allocator session is
118// cleared. A port allocation session is not cleared if the initial gathering is
119// still in progress or the continual gathering is not enabled.
120TEST_F(RegatheringControllerTest,
121 IceRegatheringDoesNotOccurIfSessionNotCleared) {
122 rtc::ScopedFakeClock clock;
123 InitializeAndGatherOnce(); // Session not cleared.
124
Steve Antonf4172382020-01-27 15:45:02 -0800125 BasicRegatheringController::Config config;
126 config.regather_on_failed_networks_interval = 2000;
Qingsi Wang1b368942018-06-13 13:54:08 -0700127 regathering_controller()->SetConfig(config);
128 regathering_controller()->Start();
129 SIMULATED_WAIT(false, 10000, clock);
130 // Expect no regathering in the last 10s.
131 EXPECT_EQ(0, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700132 cricket::IceRegatheringReason::NETWORK_FAILURE));
133}
134
135TEST_F(RegatheringControllerTest, IceRegatheringRepeatsAsScheduled) {
136 rtc::ScopedFakeClock clock;
137 InitializeAndGatherOnceWithSessionCleared();
138
Steve Antonf4172382020-01-27 15:45:02 -0800139 BasicRegatheringController::Config config;
140 config.regather_on_failed_networks_interval = 2000;
Qingsi Wang1b368942018-06-13 13:54:08 -0700141 regathering_controller()->SetConfig(config);
142 regathering_controller()->Start();
143 SIMULATED_WAIT(false, 2000 - 1, clock);
144 // Expect no regathering.
145 EXPECT_EQ(0, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700146 cricket::IceRegatheringReason::NETWORK_FAILURE));
147 SIMULATED_WAIT(false, 2, clock);
148 // Expect regathering on all networks and on failed networks to happen once
149 // respectively in that last 2s with 2s interval.
150 EXPECT_EQ(1, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700151 cricket::IceRegatheringReason::NETWORK_FAILURE));
152 SIMULATED_WAIT(false, 11000, clock);
153 // Expect regathering to happen for another 5 times in 11s with 2s interval.
154 EXPECT_EQ(6, GetRegatheringReasonCount(
Qingsi Wang1b368942018-06-13 13:54:08 -0700155 cricket::IceRegatheringReason::NETWORK_FAILURE));
156}
157
Qingsi Wang1b368942018-06-13 13:54:08 -0700158// Tests that the schedule of ICE regathering on failed networks can be canceled
159// and replaced by a new recurring schedule.
160TEST_F(RegatheringControllerTest,
161 ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced) {
162 rtc::ScopedFakeClock clock;
163 InitializeAndGatherOnceWithSessionCleared();
164
Steve Antonf4172382020-01-27 15:45:02 -0800165 BasicRegatheringController::Config config;
166 config.regather_on_failed_networks_interval = 2000;
Qingsi Wang1b368942018-06-13 13:54:08 -0700167 regathering_controller()->SetConfig(config);
168 regathering_controller()->Start();
169 config.regather_on_failed_networks_interval = 5000;
170 regathering_controller()->SetConfig(config);
171 SIMULATED_WAIT(false, 3000, clock);
172 // Expect no regathering from the previous schedule.
173 EXPECT_EQ(0, GetRegatheringReasonCount(
174 cricket::IceRegatheringReason::NETWORK_FAILURE));
175 SIMULATED_WAIT(false, 11000 - 3000, clock);
176 // Expect regathering to happen twice in the last 11s with 5s interval.
177 EXPECT_EQ(2, GetRegatheringReasonCount(
178 cricket::IceRegatheringReason::NETWORK_FAILURE));
179}
180
181} // namespace webrtc