blob: a9edbeccafff4ae26d5f2e223536899531141d6b [file] [log] [blame]
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001/*
2 * Copyright 2016 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 <memory>
12
Steve Anton10542f22019-01-11 09:11:00 -080013#include "p2p/base/fake_port_allocator.h"
14#include "p2p/base/port_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/virtual_socket_server.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "test/gtest.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070018
19static const char kContentName[] = "test content";
20// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070021static const char kIceUfrag[] = "UF00";
Taylor Brandstettera1c30352016-05-13 08:15:11 -070022// Based on ICE_PWD_LENGTH
23static const char kIcePwd[] = "TESTICEPWD00000000000000";
24static const char kTurnUsername[] = "test";
25static const char kTurnPassword[] = "test";
26
Mirko Bonadei6a489f22019-04-09 15:11:12 +020027class PortAllocatorTest : public ::testing::Test, public sigslot::has_slots<> {
Taylor Brandstettera1c30352016-05-13 08:15:11 -070028 public:
deadbeef9a6f4d42017-05-15 19:43:33 -070029 PortAllocatorTest()
deadbeef98e186c2017-05-16 18:00:06 -070030 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -070031 allocator_.reset(
32 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
33 }
34
35 protected:
36 void SetConfigurationWithPoolSize(int candidate_pool_size) {
deadbeef6de92f92016-12-12 18:49:32 -080037 EXPECT_TRUE(allocator_->SetConfiguration(
38 cricket::ServerAddresses(), std::vector<cricket::RelayServerConfig>(),
39 candidate_pool_size, false));
40 }
41
42 void SetConfigurationWithPoolSizeExpectFailure(int candidate_pool_size) {
43 EXPECT_FALSE(allocator_->SetConfiguration(
44 cricket::ServerAddresses(), std::vector<cricket::RelayServerConfig>(),
45 candidate_pool_size, false));
Taylor Brandstettera1c30352016-05-13 08:15:11 -070046 }
47
Taylor Brandstetter417eebe2016-05-23 16:02:19 -070048 std::unique_ptr<cricket::FakePortAllocatorSession> CreateSession(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -070049 const std::string& content_name,
50 int component,
51 const std::string& ice_ufrag,
52 const std::string& ice_pwd) {
53 return std::unique_ptr<cricket::FakePortAllocatorSession>(
54 static_cast<cricket::FakePortAllocatorSession*>(
55 allocator_
johanfe1ffb12016-08-11 12:37:42 -070056 ->CreateSession(content_name, component, ice_ufrag, ice_pwd)
Taylor Brandstetter417eebe2016-05-23 16:02:19 -070057 .release()));
58 }
59
Taylor Brandstettera1c30352016-05-13 08:15:11 -070060 const cricket::FakePortAllocatorSession* GetPooledSession() const {
61 return static_cast<const cricket::FakePortAllocatorSession*>(
62 allocator_->GetPooledSession());
63 }
64
65 std::unique_ptr<cricket::FakePortAllocatorSession> TakePooledSession() {
66 return std::unique_ptr<cricket::FakePortAllocatorSession>(
67 static_cast<cricket::FakePortAllocatorSession*>(
68 allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd)
69 .release()));
70 }
71
72 int GetAllPooledSessionsReturnCount() {
73 int count = 0;
Jonas Oreland1cd39fa2018-10-11 07:47:12 +020074 while (TakePooledSession() != nullptr) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -070075 ++count;
76 }
77 return count;
78 }
79
deadbeef9a6f4d42017-05-15 19:43:33 -070080 std::unique_ptr<rtc::VirtualSocketServer> vss_;
81 rtc::AutoSocketServerThread main_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -070082 std::unique_ptr<cricket::FakePortAllocator> allocator_;
83 rtc::SocketAddress stun_server_1{"11.11.11.11", 3478};
84 rtc::SocketAddress stun_server_2{"22.22.22.22", 3478};
85 cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478,
86 kTurnUsername, kTurnPassword,
87 cricket::PROTO_UDP, false};
88 cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478,
89 kTurnUsername, kTurnPassword,
90 cricket::PROTO_UDP, false};
91};
92
93TEST_F(PortAllocatorTest, TestDefaults) {
94 EXPECT_EQ(0UL, allocator_->stun_servers().size());
95 EXPECT_EQ(0UL, allocator_->turn_servers().size());
96 EXPECT_EQ(0, allocator_->candidate_pool_size());
97 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
98}
99
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700100// Call CreateSession and verify that the parameters passed in and the
101// candidate filter are applied as expected.
102TEST_F(PortAllocatorTest, CreateSession) {
Qingsi Wang797ede82019-04-17 21:21:54 +0000103 allocator_->set_candidate_filter(cricket::CF_RELAY);
johanfe1ffb12016-08-11 12:37:42 -0700104 auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700105 ASSERT_NE(nullptr, session);
106 EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
107 EXPECT_EQ(kContentName, session->content_name());
108 EXPECT_EQ(1, session->component());
109 EXPECT_EQ(kIceUfrag, session->ice_ufrag());
110 EXPECT_EQ(kIcePwd, session->ice_pwd());
111}
112
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700113TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) {
114 cricket::ServerAddresses stun_servers_1 = {stun_server_1};
115 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
deadbeef6de92f92016-12-12 18:49:32 -0800116 EXPECT_TRUE(
117 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0, false));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700118 EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
119 EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
120
121 // Update with a different set of servers.
122 cricket::ServerAddresses stun_servers_2 = {stun_server_2};
123 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
deadbeef6de92f92016-12-12 18:49:32 -0800124 EXPECT_TRUE(
125 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0, false));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700126 EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
127 EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
128}
129
130TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) {
131 SetConfigurationWithPoolSize(2);
132 EXPECT_EQ(2, allocator_->candidate_pool_size());
133 SetConfigurationWithPoolSize(3);
134 EXPECT_EQ(3, allocator_->candidate_pool_size());
135 SetConfigurationWithPoolSize(1);
136 EXPECT_EQ(1, allocator_->candidate_pool_size());
137 SetConfigurationWithPoolSize(4);
138 EXPECT_EQ(4, allocator_->candidate_pool_size());
139}
140
141// A negative pool size should just be treated as zero.
deadbeef6de92f92016-12-12 18:49:32 -0800142TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeFails) {
143 SetConfigurationWithPoolSizeExpectFailure(-1);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700144}
145
146// Test that if the candidate pool size is nonzero, pooled sessions are
147// created, and StartGettingPorts is called on them.
148TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) {
149 SetConfigurationWithPoolSize(2);
150 auto session_1 = TakePooledSession();
151 auto session_2 = TakePooledSession();
152 ASSERT_NE(nullptr, session_1.get());
153 ASSERT_NE(nullptr, session_2.get());
154 EXPECT_EQ(1, session_1->port_config_count());
155 EXPECT_EQ(1, session_2->port_config_count());
156 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
157}
158
159// Test that if the candidate pool size is increased, pooled sessions are
160// created as necessary.
161TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) {
162 SetConfigurationWithPoolSize(1);
163 SetConfigurationWithPoolSize(2);
164 EXPECT_EQ(2, GetAllPooledSessionsReturnCount());
165}
166
167// Test that if the candidate pool size is reduced, extra sessions are
168// destroyed.
169TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) {
170 SetConfigurationWithPoolSize(2);
171 SetConfigurationWithPoolSize(1);
172 EXPECT_EQ(1, GetAllPooledSessionsReturnCount());
173}
174
deadbeef6de92f92016-12-12 18:49:32 -0800175// According to JSEP, existing pooled sessions should be destroyed and new
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700176// ones created when the ICE servers change.
177TEST_F(PortAllocatorTest,
178 SetConfigurationRecreatesPooledSessionsWhenIceServersChange) {
179 cricket::ServerAddresses stun_servers_1 = {stun_server_1};
180 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700181 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700182 EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
183 EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
184
185 // Update with a different set of servers (and also change pool size).
186 cricket::ServerAddresses stun_servers_2 = {stun_server_2};
187 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700188 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700189 EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
190 EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
191 auto session_1 = TakePooledSession();
192 auto session_2 = TakePooledSession();
193 ASSERT_NE(nullptr, session_1.get());
194 ASSERT_NE(nullptr, session_2.get());
195 EXPECT_EQ(stun_servers_2, session_1->stun_servers());
196 EXPECT_EQ(turn_servers_2, session_1->turn_servers());
197 EXPECT_EQ(stun_servers_2, session_2->stun_servers());
198 EXPECT_EQ(turn_servers_2, session_2->turn_servers());
199 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
200}
201
deadbeef42a42632017-03-10 15:18:00 -0800202// According to JSEP, after SetLocalDescription, setting different ICE servers
203// will not cause the pool to be refilled. This is implemented by the
204// PeerConnection calling FreezeCandidatePool when a local description is set.
205TEST_F(PortAllocatorTest,
206 SetConfigurationDoesNotRecreatePooledSessionsAfterFreezeCandidatePool) {
207 cricket::ServerAddresses stun_servers_1 = {stun_server_1};
208 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
209 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false);
210 EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
211 EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
212
213 // Update with a different set of servers, but first freeze the pool.
214 allocator_->FreezeCandidatePool();
215 cricket::ServerAddresses stun_servers_2 = {stun_server_2};
216 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
217 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false);
218 EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
219 EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
220 auto session = TakePooledSession();
221 ASSERT_NE(nullptr, session.get());
222 EXPECT_EQ(stun_servers_1, session->stun_servers());
223 EXPECT_EQ(turn_servers_1, session->turn_servers());
224 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
225}
226
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700227TEST_F(PortAllocatorTest, GetPooledSessionReturnsNextSession) {
228 SetConfigurationWithPoolSize(2);
229 auto peeked_session_1 = GetPooledSession();
230 auto session_1 = TakePooledSession();
231 EXPECT_EQ(session_1.get(), peeked_session_1);
232 auto peeked_session_2 = GetPooledSession();
233 auto session_2 = TakePooledSession();
234 EXPECT_EQ(session_2.get(), peeked_session_2);
235}
236
237// Verify that subclasses of PortAllocatorSession are given a chance to update
238// ICE parameters when TakePooledSession is called, and the base class updates
239// the info itself.
240TEST_F(PortAllocatorTest, TakePooledSessionUpdatesIceParameters) {
241 SetConfigurationWithPoolSize(1);
242 auto peeked_session = GetPooledSession();
243 ASSERT_NE(nullptr, peeked_session);
244 EXPECT_EQ(0, peeked_session->transport_info_update_count());
245 std::unique_ptr<cricket::FakePortAllocatorSession> session(
246 static_cast<cricket::FakePortAllocatorSession*>(
247 allocator_->TakePooledSession(kContentName, 1, kIceUfrag, kIcePwd)
248 .release()));
249 EXPECT_EQ(1, session->transport_info_update_count());
250 EXPECT_EQ(kContentName, session->content_name());
251 EXPECT_EQ(1, session->component());
252 EXPECT_EQ(kIceUfrag, session->ice_ufrag());
253 EXPECT_EQ(kIcePwd, session->ice_pwd());
254}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700255
256// According to JSEP, candidate filtering should be done when the pooled
257// candidates are surfaced to the application. This means when a pooled
258// session is taken. So a pooled session should gather candidates
259// unfiltered until it's returned by TakePooledSession.
260TEST_F(PortAllocatorTest, TakePooledSessionUpdatesCandidateFilter) {
Qingsi Wang797ede82019-04-17 21:21:54 +0000261 allocator_->set_candidate_filter(cricket::CF_RELAY);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700262 SetConfigurationWithPoolSize(1);
263 auto peeked_session = GetPooledSession();
264 ASSERT_NE(nullptr, peeked_session);
265 EXPECT_EQ(cricket::CF_ALL, peeked_session->candidate_filter());
266 auto session = TakePooledSession();
267 EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
268}
deadbeef42a42632017-03-10 15:18:00 -0800269
270// Verify that after DiscardCandidatePool, TakePooledSession doesn't return
271// anything.
272TEST_F(PortAllocatorTest, DiscardCandidatePool) {
273 SetConfigurationWithPoolSize(1);
274 allocator_->DiscardCandidatePool();
275 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
276}
Jonas Oreland1cd39fa2018-10-11 07:47:12 +0200277
278TEST_F(PortAllocatorTest, RestrictIceCredentialsChange) {
279 SetConfigurationWithPoolSize(1);
280 EXPECT_EQ(1, GetAllPooledSessionsReturnCount());
281 allocator_->DiscardCandidatePool();
282
283 // Only return pooled sessions with the ice credentials that
284 // match those requested in TakePooledSession().
285 allocator_->set_restrict_ice_credentials_change(true);
286 SetConfigurationWithPoolSize(1);
287 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
288 allocator_->DiscardCandidatePool();
289
290 SetConfigurationWithPoolSize(1);
291 auto credentials = allocator_->GetPooledIceCredentials();
292 ASSERT_EQ(1u, credentials.size());
293 EXPECT_EQ(nullptr,
294 allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd));
295 EXPECT_NE(nullptr,
296 allocator_->TakePooledSession(kContentName, 0, credentials[0].ufrag,
297 credentials[0].pwd));
298 EXPECT_EQ(nullptr,
299 allocator_->TakePooledSession(kContentName, 0, credentials[0].ufrag,
300 credentials[0].pwd));
301 allocator_->DiscardCandidatePool();
302}