You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "p2p/base/regathering_controller.h"
  11. #include <map>
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "api/scoped_refptr.h"
  16. #include "p2p/base/fake_port_allocator.h"
  17. #include "p2p/base/mock_ice_transport.h"
  18. #include "p2p/base/p2p_constants.h"
  19. #include "p2p/base/port.h"
  20. #include "p2p/base/stun_server.h"
  21. #include "rtc_base/gunit.h"
  22. #include "rtc_base/ref_counted_object.h"
  23. #include "rtc_base/socket_address.h"
  24. #include "rtc_base/thread.h"
  25. #include "rtc_base/virtual_socket_server.h"
  26. namespace {
  27. const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
  28. cricket::PORTALLOCATOR_DISABLE_RELAY |
  29. cricket::PORTALLOCATOR_DISABLE_TCP;
  30. // The address of the public STUN server.
  31. const rtc::SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
  32. // The addresses for the public TURN server.
  33. const rtc::SocketAddress kTurnUdpIntAddr("99.99.99.3",
  34. cricket::STUN_SERVER_PORT);
  35. const cricket::RelayCredentials kRelayCredentials("test", "test");
  36. const char kIceUfrag[] = "UF00";
  37. const char kIcePwd[] = "TESTICEPWD00000000000000";
  38. } // namespace
  39. namespace webrtc {
  40. class RegatheringControllerTest : public ::testing::Test,
  41. public sigslot::has_slots<> {
  42. public:
  43. RegatheringControllerTest()
  44. : vss_(new rtc::VirtualSocketServer()),
  45. thread_(vss_.get()),
  46. ice_transport_(new cricket::MockIceTransport()),
  47. allocator_(
  48. new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)) {
  49. BasicRegatheringController::Config regathering_config(absl::nullopt, 0);
  50. regathering_controller_.reset(new BasicRegatheringController(
  51. regathering_config, ice_transport_.get(), rtc::Thread::Current()));
  52. }
  53. // Initializes the allocator and gathers candidates once by StartGettingPorts.
  54. void InitializeAndGatherOnce() {
  55. cricket::ServerAddresses stun_servers;
  56. stun_servers.insert(kStunAddr);
  57. cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
  58. turn_server.credentials = kRelayCredentials;
  59. turn_server.ports.push_back(
  60. cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP));
  61. std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server);
  62. allocator_->set_flags(kOnlyLocalPorts);
  63. allocator_->SetConfiguration(stun_servers, turn_servers, 0 /* pool size */,
  64. webrtc::NO_PRUNE);
  65. allocator_session_ = allocator_->CreateSession(
  66. "test", cricket::ICE_CANDIDATE_COMPONENT_RTP, kIceUfrag, kIcePwd);
  67. // The gathering will take place on the current thread and the following
  68. // call of StartGettingPorts is blocking. We will not ClearGettingPorts
  69. // prematurely.
  70. allocator_session_->StartGettingPorts();
  71. allocator_session_->SignalIceRegathering.connect(
  72. this, &RegatheringControllerTest::OnIceRegathering);
  73. regathering_controller_->set_allocator_session(allocator_session_.get());
  74. }
  75. // The regathering controller is initialized with the allocator session
  76. // cleared. Only after clearing the session, we would be able to regather. See
  77. // the comments for BasicRegatheringController in regatheringcontroller.h.
  78. void InitializeAndGatherOnceWithSessionCleared() {
  79. InitializeAndGatherOnce();
  80. allocator_session_->ClearGettingPorts();
  81. }
  82. void OnIceRegathering(cricket::PortAllocatorSession* allocator_session,
  83. cricket::IceRegatheringReason reason) {
  84. ++count_[reason];
  85. }
  86. int GetRegatheringReasonCount(cricket::IceRegatheringReason reason) {
  87. return count_[reason];
  88. }
  89. BasicRegatheringController* regathering_controller() {
  90. return regathering_controller_.get();
  91. }
  92. private:
  93. std::unique_ptr<rtc::VirtualSocketServer> vss_;
  94. rtc::AutoSocketServerThread thread_;
  95. std::unique_ptr<cricket::IceTransportInternal> ice_transport_;
  96. std::unique_ptr<BasicRegatheringController> regathering_controller_;
  97. std::unique_ptr<cricket::PortAllocator> allocator_;
  98. std::unique_ptr<cricket::PortAllocatorSession> allocator_session_;
  99. std::map<cricket::IceRegatheringReason, int> count_;
  100. };
  101. // Tests that ICE regathering occurs only if the port allocator session is
  102. // cleared. A port allocation session is not cleared if the initial gathering is
  103. // still in progress or the continual gathering is not enabled.
  104. TEST_F(RegatheringControllerTest,
  105. IceRegatheringDoesNotOccurIfSessionNotCleared) {
  106. rtc::ScopedFakeClock clock;
  107. InitializeAndGatherOnce(); // Session not cleared.
  108. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  109. BasicRegatheringController::Config config(
  110. regather_all_networks_interval_range, 2000);
  111. regathering_controller()->SetConfig(config);
  112. regathering_controller()->Start();
  113. SIMULATED_WAIT(false, 10000, clock);
  114. // Expect no regathering in the last 10s.
  115. EXPECT_EQ(0, GetRegatheringReasonCount(
  116. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  117. EXPECT_EQ(0, GetRegatheringReasonCount(
  118. cricket::IceRegatheringReason::NETWORK_FAILURE));
  119. }
  120. TEST_F(RegatheringControllerTest, IceRegatheringRepeatsAsScheduled) {
  121. rtc::ScopedFakeClock clock;
  122. InitializeAndGatherOnceWithSessionCleared();
  123. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  124. BasicRegatheringController::Config config(
  125. regather_all_networks_interval_range, 2000);
  126. regathering_controller()->SetConfig(config);
  127. regathering_controller()->Start();
  128. SIMULATED_WAIT(false, 2000 - 1, clock);
  129. // Expect no regathering.
  130. EXPECT_EQ(0, GetRegatheringReasonCount(
  131. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  132. EXPECT_EQ(0, GetRegatheringReasonCount(
  133. cricket::IceRegatheringReason::NETWORK_FAILURE));
  134. SIMULATED_WAIT(false, 2, clock);
  135. // Expect regathering on all networks and on failed networks to happen once
  136. // respectively in that last 2s with 2s interval.
  137. EXPECT_EQ(1, GetRegatheringReasonCount(
  138. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  139. EXPECT_EQ(1, GetRegatheringReasonCount(
  140. cricket::IceRegatheringReason::NETWORK_FAILURE));
  141. SIMULATED_WAIT(false, 11000, clock);
  142. // Expect regathering to happen for another 5 times in 11s with 2s interval.
  143. EXPECT_EQ(6, GetRegatheringReasonCount(
  144. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  145. EXPECT_EQ(6, GetRegatheringReasonCount(
  146. cricket::IceRegatheringReason::NETWORK_FAILURE));
  147. }
  148. // Tests that the schedule of ICE regathering on all networks can be started
  149. // when not scheduled initially.
  150. TEST_F(RegatheringControllerTest,
  151. IceRegatheringOnAllNetworksCanBeScheduledAfterStart) {
  152. rtc::ScopedFakeClock clock;
  153. InitializeAndGatherOnceWithSessionCleared();
  154. BasicRegatheringController::Config config(absl::nullopt, 2000);
  155. regathering_controller()->SetConfig(config);
  156. regathering_controller()->Start();
  157. SIMULATED_WAIT(false, 3000, clock);
  158. // Expect no regathering on all networks.
  159. EXPECT_EQ(0, GetRegatheringReasonCount(
  160. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  161. config.regather_on_all_networks_interval_range =
  162. rtc::IntervalRange(2000, 2000);
  163. regathering_controller()->SetConfig(config);
  164. SIMULATED_WAIT(false, 11000, clock);
  165. // Expect regathering to happen for 5 times on all networks in the last 11s
  166. // with 2s interval.
  167. EXPECT_EQ(5, GetRegatheringReasonCount(
  168. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  169. }
  170. // Tests that ICE regathering on all networks can be canceled by changing the
  171. // config.
  172. TEST_F(RegatheringControllerTest, IceRegatheringOnAllNetworksCanBeCanceled) {
  173. rtc::ScopedFakeClock clock;
  174. InitializeAndGatherOnceWithSessionCleared();
  175. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  176. BasicRegatheringController::Config config(
  177. regather_all_networks_interval_range, 2000);
  178. regathering_controller()->SetConfig(config);
  179. regathering_controller()->Start();
  180. config.regather_on_all_networks_interval_range.reset();
  181. // Set the regathering interval range on all networks to nullopt should cancel
  182. // the schedule on all networks.
  183. regathering_controller()->SetConfig(config);
  184. SIMULATED_WAIT(false, 10000, clock);
  185. // Expect no regathering on all networks happened in the last 10s.
  186. EXPECT_EQ(0, GetRegatheringReasonCount(
  187. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  188. }
  189. // Tests that canceling the regathering on all networks does not cancel the
  190. // schedule on failed networks.
  191. TEST_F(RegatheringControllerTest,
  192. CancelingRegatheringOnAllNetworksDoesNotCancelOnFailedNetworks) {
  193. rtc::ScopedFakeClock clock;
  194. InitializeAndGatherOnceWithSessionCleared();
  195. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  196. BasicRegatheringController::Config config(
  197. regather_all_networks_interval_range, 2000);
  198. regathering_controller()->SetConfig(config);
  199. regathering_controller()->Start();
  200. config.regather_on_all_networks_interval_range =
  201. rtc::IntervalRange(20000, 20000);
  202. // Canceling and rescheduling the regathering on all networks should not
  203. // impact the schedule for failed networks.
  204. regathering_controller()->SetConfig(config);
  205. SIMULATED_WAIT(false, 11000, clock);
  206. // Expect regathering to happen for 5 times for failed networks in the last
  207. // 11s with 2s interval.
  208. EXPECT_EQ(5, GetRegatheringReasonCount(
  209. cricket::IceRegatheringReason::NETWORK_FAILURE));
  210. }
  211. // Tests that canceling the regathering on failed networks does not cancel the
  212. // schedule on all networks.
  213. TEST_F(RegatheringControllerTest,
  214. CancelingRegatheringOnFailedNetworksDoesNotCancelOnAllNetworks) {
  215. rtc::ScopedFakeClock clock;
  216. InitializeAndGatherOnceWithSessionCleared();
  217. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  218. BasicRegatheringController::Config config(
  219. regather_all_networks_interval_range, 2000);
  220. regathering_controller()->SetConfig(config);
  221. regathering_controller()->Start();
  222. config.regather_on_failed_networks_interval = 20000;
  223. // Canceling and rescheduling the regathering on failed networks should not
  224. // impact the schedule for all networks.
  225. regathering_controller()->SetConfig(config);
  226. SIMULATED_WAIT(false, 11000, clock);
  227. // Expect regathering to happen for 5 times for all networks in the last 11s
  228. // with 2s interval.
  229. EXPECT_EQ(5, GetRegatheringReasonCount(
  230. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  231. }
  232. // Tests that the schedule of ICE regathering on all networks can be canceled
  233. // and replaced by a new recurring schedule.
  234. TEST_F(RegatheringControllerTest,
  235. ScheduleOfIceRegatheringOnAllNetworksCanBeReplaced) {
  236. rtc::ScopedFakeClock clock;
  237. InitializeAndGatherOnceWithSessionCleared();
  238. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  239. BasicRegatheringController::Config config(
  240. regather_all_networks_interval_range, 2000);
  241. regathering_controller()->SetConfig(config);
  242. regathering_controller()->Start();
  243. config.regather_on_all_networks_interval_range =
  244. rtc::IntervalRange(5000, 5000);
  245. regathering_controller()->SetConfig(config);
  246. SIMULATED_WAIT(false, 3000, clock);
  247. // Expect no regathering from the previous schedule.
  248. EXPECT_EQ(0, GetRegatheringReasonCount(
  249. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  250. SIMULATED_WAIT(false, 11000 - 3000, clock);
  251. // Expect regathering to happen twice in the last 11s with 5s interval.
  252. EXPECT_EQ(2, GetRegatheringReasonCount(
  253. cricket::IceRegatheringReason::OCCASIONAL_REFRESH));
  254. }
  255. // Tests that the schedule of ICE regathering on failed networks can be canceled
  256. // and replaced by a new recurring schedule.
  257. TEST_F(RegatheringControllerTest,
  258. ScheduleOfIceRegatheringOnFailedNetworksCanBeReplaced) {
  259. rtc::ScopedFakeClock clock;
  260. InitializeAndGatherOnceWithSessionCleared();
  261. rtc::IntervalRange regather_all_networks_interval_range(2000, 2000);
  262. BasicRegatheringController::Config config(
  263. regather_all_networks_interval_range, 2000);
  264. regathering_controller()->SetConfig(config);
  265. regathering_controller()->Start();
  266. config.regather_on_failed_networks_interval = 5000;
  267. regathering_controller()->SetConfig(config);
  268. SIMULATED_WAIT(false, 3000, clock);
  269. // Expect no regathering from the previous schedule.
  270. EXPECT_EQ(0, GetRegatheringReasonCount(
  271. cricket::IceRegatheringReason::NETWORK_FAILURE));
  272. SIMULATED_WAIT(false, 11000 - 3000, clock);
  273. // Expect regathering to happen twice in the last 11s with 5s interval.
  274. EXPECT_EQ(2, GetRegatheringReasonCount(
  275. cricket::IceRegatheringReason::NETWORK_FAILURE));
  276. }
  277. } // namespace webrtc