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/ref_counted_object.h"
26 #include "rtc_base/socket_address.h"
27 #include "rtc_base/thread.h"
28 #include "rtc_base/virtual_socket_server.h"
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 
48 class RegatheringControllerTest : public ::testing::Test,
49                                   public sigslot::has_slots<> {
50  public:
RegatheringControllerTest()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)) {
57     BasicRegatheringController::Config regathering_config;
58     regathering_config.regather_on_failed_networks_interval = 0;
59     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.
InitializeAndGatherOnce()64   void InitializeAndGatherOnce() {
65     cricket::ServerAddresses stun_servers;
66     stun_servers.insert(kStunAddr);
67     cricket::RelayServerConfig turn_server;
68     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 */,
74                                  webrtc::NO_PRUNE);
75     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.
InitializeAndGatherOnceWithSessionCleared()89   void InitializeAndGatherOnceWithSessionCleared() {
90     InitializeAndGatherOnce();
91     allocator_session_->ClearGettingPorts();
92   }
93 
OnIceRegathering(cricket::PortAllocatorSession * allocator_session,cricket::IceRegatheringReason reason)94   void OnIceRegathering(cricket::PortAllocatorSession* allocator_session,
95                         cricket::IceRegatheringReason reason) {
96     ++count_[reason];
97   }
98 
GetRegatheringReasonCount(cricket::IceRegatheringReason reason)99   int GetRegatheringReasonCount(cricket::IceRegatheringReason reason) {
100     return count_[reason];
101   }
102 
regathering_controller()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.
TEST_F(RegatheringControllerTest,IceRegatheringDoesNotOccurIfSessionNotCleared)120 TEST_F(RegatheringControllerTest,
121        IceRegatheringDoesNotOccurIfSessionNotCleared) {
122   rtc::ScopedFakeClock clock;
123   InitializeAndGatherOnce();  // Session not cleared.
124 
125   BasicRegatheringController::Config config;
126   config.regather_on_failed_networks_interval = 2000;
127   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(
132                    cricket::IceRegatheringReason::NETWORK_FAILURE));
133 }
134 
TEST_F(RegatheringControllerTest,IceRegatheringRepeatsAsScheduled)135 TEST_F(RegatheringControllerTest, IceRegatheringRepeatsAsScheduled) {
136   rtc::ScopedFakeClock clock;
137   InitializeAndGatherOnceWithSessionCleared();
138 
139   BasicRegatheringController::Config config;
140   config.regather_on_failed_networks_interval = 2000;
141   regathering_controller()->SetConfig(config);
142   regathering_controller()->Start();
143   SIMULATED_WAIT(false, 2000 - 1, clock);
144   // Expect no regathering.
145   EXPECT_EQ(0, GetRegatheringReasonCount(
146                    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(
151                    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(
155                    cricket::IceRegatheringReason::NETWORK_FAILURE));
156 }
157 
158 // Tests that the schedule of ICE regathering on failed networks can be canceled
159 // and replaced by a new recurring schedule.
TEST_F(RegatheringControllerTest,ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced)160 TEST_F(RegatheringControllerTest,
161        ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced) {
162   rtc::ScopedFakeClock clock;
163   InitializeAndGatherOnceWithSessionCleared();
164 
165   BasicRegatheringController::Config config;
166   config.regather_on_failed_networks_interval = 2000;
167   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
182