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