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
11 #include "p2p/base/regathering_controller.h"
12
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <vector>
17
18 #include "api/scoped_refptr.h"
19 #include "p2p/base/fake_port_allocator.h"
20 #include "p2p/base/mock_ice_transport.h"
21 #include "p2p/base/p2p_constants.h"
22 #include "p2p/base/port.h"
23 #include "p2p/base/stun_server.h"
24 #include "rtc_base/gunit.h"
25 #include "rtc_base/socket_address.h"
26 #include "rtc_base/thread.h"
27 #include "rtc_base/virtual_socket_server.h"
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 constexpr uint64_t kTiebreakerDefault = 44444;
43
44 } // namespace
45
46 namespace webrtc {
47
48 class RegatheringControllerTest : public ::testing::Test,
49 public sigslot::has_slots<> {
50 public:
RegatheringControllerTest()51 RegatheringControllerTest()
52 : vss_(std::make_unique<rtc::VirtualSocketServer>()),
53 thread_(vss_.get()),
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(),
59 packet_socket_factory_.get())) {
60 allocator_->SetIceTiebreaker(kTiebreakerDefault);
61 BasicRegatheringController::Config regathering_config;
62 regathering_config.regather_on_failed_networks_interval = 0;
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.
InitializeAndGatherOnce()68 void InitializeAndGatherOnce() {
69 cricket::ServerAddresses stun_servers;
70 stun_servers.insert(kStunAddr);
71 cricket::RelayServerConfig turn_server;
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 */,
78 webrtc::NO_PRUNE);
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.
InitializeAndGatherOnceWithSessionCleared()93 void InitializeAndGatherOnceWithSessionCleared() {
94 InitializeAndGatherOnce();
95 allocator_session_->ClearGettingPorts();
96 }
97
OnIceRegathering(cricket::PortAllocatorSession * allocator_session,cricket::IceRegatheringReason reason)98 void OnIceRegathering(cricket::PortAllocatorSession* allocator_session,
99 cricket::IceRegatheringReason reason) {
100 ++count_[reason];
101 }
102
GetRegatheringReasonCount(cricket::IceRegatheringReason reason)103 int GetRegatheringReasonCount(cricket::IceRegatheringReason reason) {
104 return count_[reason];
105 }
106
regathering_controller()107 BasicRegatheringController* regathering_controller() {
108 return regathering_controller_.get();
109 }
110
111 private:
112 std::unique_ptr<rtc::VirtualSocketServer> vss_;
113 rtc::AutoSocketServerThread thread_;
114 std::unique_ptr<cricket::IceTransportInternal> ice_transport_;
115 std::unique_ptr<BasicRegatheringController> regathering_controller_;
116 std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
117 std::unique_ptr<cricket::PortAllocator> allocator_;
118 std::unique_ptr<cricket::PortAllocatorSession> allocator_session_;
119 std::map<cricket::IceRegatheringReason, int> count_;
120 };
121
122 // Tests that ICE regathering occurs only if the port allocator session is
123 // cleared. A port allocation session is not cleared if the initial gathering is
124 // still in progress or the continual gathering is not enabled.
TEST_F(RegatheringControllerTest,IceRegatheringDoesNotOccurIfSessionNotCleared)125 TEST_F(RegatheringControllerTest,
126 IceRegatheringDoesNotOccurIfSessionNotCleared) {
127 rtc::ScopedFakeClock clock;
128 InitializeAndGatherOnce(); // Session not cleared.
129
130 BasicRegatheringController::Config config;
131 config.regather_on_failed_networks_interval = 2000;
132 regathering_controller()->SetConfig(config);
133 regathering_controller()->Start();
134 SIMULATED_WAIT(false, 10000, clock);
135 // Expect no regathering in the last 10s.
136 EXPECT_EQ(0, GetRegatheringReasonCount(
137 cricket::IceRegatheringReason::NETWORK_FAILURE));
138 }
139
TEST_F(RegatheringControllerTest,IceRegatheringRepeatsAsScheduled)140 TEST_F(RegatheringControllerTest, IceRegatheringRepeatsAsScheduled) {
141 rtc::ScopedFakeClock clock;
142 InitializeAndGatherOnceWithSessionCleared();
143
144 BasicRegatheringController::Config config;
145 config.regather_on_failed_networks_interval = 2000;
146 regathering_controller()->SetConfig(config);
147 regathering_controller()->Start();
148 SIMULATED_WAIT(false, 2000 - 1, clock);
149 // Expect no regathering.
150 EXPECT_EQ(0, GetRegatheringReasonCount(
151 cricket::IceRegatheringReason::NETWORK_FAILURE));
152 SIMULATED_WAIT(false, 2, clock);
153 // Expect regathering on all networks and on failed networks to happen once
154 // respectively in that last 2s with 2s interval.
155 EXPECT_EQ(1, GetRegatheringReasonCount(
156 cricket::IceRegatheringReason::NETWORK_FAILURE));
157 SIMULATED_WAIT(false, 11000, clock);
158 // Expect regathering to happen for another 5 times in 11s with 2s interval.
159 EXPECT_EQ(6, GetRegatheringReasonCount(
160 cricket::IceRegatheringReason::NETWORK_FAILURE));
161 }
162
163 // Tests that the schedule of ICE regathering on failed networks can be canceled
164 // and replaced by a new recurring schedule.
TEST_F(RegatheringControllerTest,ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced)165 TEST_F(RegatheringControllerTest,
166 ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced) {
167 rtc::ScopedFakeClock clock;
168 InitializeAndGatherOnceWithSessionCleared();
169
170 BasicRegatheringController::Config config;
171 config.regather_on_failed_networks_interval = 2000;
172 regathering_controller()->SetConfig(config);
173 regathering_controller()->Start();
174 config.regather_on_failed_networks_interval = 5000;
175 regathering_controller()->SetConfig(config);
176 SIMULATED_WAIT(false, 3000, clock);
177 // Expect no regathering from the previous schedule.
178 EXPECT_EQ(0, GetRegatheringReasonCount(
179 cricket::IceRegatheringReason::NETWORK_FAILURE));
180 SIMULATED_WAIT(false, 11000 - 3000, clock);
181 // Expect regathering to happen twice in the last 11s with 5s interval.
182 EXPECT_EQ(2, GetRegatheringReasonCount(
183 cricket::IceRegatheringReason::NETWORK_FAILURE));
184 }
185
186 } // namespace webrtc
187