blob: 3887a90db1f29ba6f08123bb13e2d8cbf5327f17 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "p2p/base/fakeportallocator.h"
14#include "p2p/base/portallocator.h"
15#include "rtc_base/gunit.h"
16#include "rtc_base/thread.h"
17#include "rtc_base/virtualsocketserver.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
27class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
28 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;
74 while (GetPooledSession()) {
75 TakePooledSession();
76 ++count;
77 }
78 return count;
79 }
80
deadbeef9a6f4d42017-05-15 19:43:33 -070081 std::unique_ptr<rtc::VirtualSocketServer> vss_;
82 rtc::AutoSocketServerThread main_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -070083 std::unique_ptr<cricket::FakePortAllocator> allocator_;
84 rtc::SocketAddress stun_server_1{"11.11.11.11", 3478};
85 rtc::SocketAddress stun_server_2{"22.22.22.22", 3478};
86 cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478,
87 kTurnUsername, kTurnPassword,
88 cricket::PROTO_UDP, false};
89 cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478,
90 kTurnUsername, kTurnPassword,
91 cricket::PROTO_UDP, false};
92};
93
94TEST_F(PortAllocatorTest, TestDefaults) {
95 EXPECT_EQ(0UL, allocator_->stun_servers().size());
96 EXPECT_EQ(0UL, allocator_->turn_servers().size());
97 EXPECT_EQ(0, allocator_->candidate_pool_size());
98 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
99}
100
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700101// Call CreateSession and verify that the parameters passed in and the
102// candidate filter are applied as expected.
103TEST_F(PortAllocatorTest, CreateSession) {
104 allocator_->set_candidate_filter(cricket::CF_RELAY);
johanfe1ffb12016-08-11 12:37:42 -0700105 auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700106 ASSERT_NE(nullptr, session);
107 EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
108 EXPECT_EQ(kContentName, session->content_name());
109 EXPECT_EQ(1, session->component());
110 EXPECT_EQ(kIceUfrag, session->ice_ufrag());
111 EXPECT_EQ(kIcePwd, session->ice_pwd());
112}
113
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700114TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) {
115 cricket::ServerAddresses stun_servers_1 = {stun_server_1};
116 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
deadbeef6de92f92016-12-12 18:49:32 -0800117 EXPECT_TRUE(
118 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0, false));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700119 EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
120 EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
121
122 // Update with a different set of servers.
123 cricket::ServerAddresses stun_servers_2 = {stun_server_2};
124 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
deadbeef6de92f92016-12-12 18:49:32 -0800125 EXPECT_TRUE(
126 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0, false));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700127 EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
128 EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
129}
130
131TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) {
132 SetConfigurationWithPoolSize(2);
133 EXPECT_EQ(2, allocator_->candidate_pool_size());
134 SetConfigurationWithPoolSize(3);
135 EXPECT_EQ(3, allocator_->candidate_pool_size());
136 SetConfigurationWithPoolSize(1);
137 EXPECT_EQ(1, allocator_->candidate_pool_size());
138 SetConfigurationWithPoolSize(4);
139 EXPECT_EQ(4, allocator_->candidate_pool_size());
140}
141
142// A negative pool size should just be treated as zero.
deadbeef6de92f92016-12-12 18:49:32 -0800143TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeFails) {
144 SetConfigurationWithPoolSizeExpectFailure(-1);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700145}
146
147// Test that if the candidate pool size is nonzero, pooled sessions are
148// created, and StartGettingPorts is called on them.
149TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) {
150 SetConfigurationWithPoolSize(2);
151 auto session_1 = TakePooledSession();
152 auto session_2 = TakePooledSession();
153 ASSERT_NE(nullptr, session_1.get());
154 ASSERT_NE(nullptr, session_2.get());
155 EXPECT_EQ(1, session_1->port_config_count());
156 EXPECT_EQ(1, session_2->port_config_count());
157 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
158}
159
160// Test that if the candidate pool size is increased, pooled sessions are
161// created as necessary.
162TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) {
163 SetConfigurationWithPoolSize(1);
164 SetConfigurationWithPoolSize(2);
165 EXPECT_EQ(2, GetAllPooledSessionsReturnCount());
166}
167
168// Test that if the candidate pool size is reduced, extra sessions are
169// destroyed.
170TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) {
171 SetConfigurationWithPoolSize(2);
172 SetConfigurationWithPoolSize(1);
173 EXPECT_EQ(1, GetAllPooledSessionsReturnCount());
174}
175
deadbeef6de92f92016-12-12 18:49:32 -0800176// According to JSEP, existing pooled sessions should be destroyed and new
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700177// ones created when the ICE servers change.
178TEST_F(PortAllocatorTest,
179 SetConfigurationRecreatesPooledSessionsWhenIceServersChange) {
180 cricket::ServerAddresses stun_servers_1 = {stun_server_1};
181 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700182 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700183 EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
184 EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
185
186 // Update with a different set of servers (and also change pool size).
187 cricket::ServerAddresses stun_servers_2 = {stun_server_2};
188 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700189 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700190 EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
191 EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
192 auto session_1 = TakePooledSession();
193 auto session_2 = TakePooledSession();
194 ASSERT_NE(nullptr, session_1.get());
195 ASSERT_NE(nullptr, session_2.get());
196 EXPECT_EQ(stun_servers_2, session_1->stun_servers());
197 EXPECT_EQ(turn_servers_2, session_1->turn_servers());
198 EXPECT_EQ(stun_servers_2, session_2->stun_servers());
199 EXPECT_EQ(turn_servers_2, session_2->turn_servers());
200 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
201}
202
deadbeef42a42632017-03-10 15:18:00 -0800203// According to JSEP, after SetLocalDescription, setting different ICE servers
204// will not cause the pool to be refilled. This is implemented by the
205// PeerConnection calling FreezeCandidatePool when a local description is set.
206TEST_F(PortAllocatorTest,
207 SetConfigurationDoesNotRecreatePooledSessionsAfterFreezeCandidatePool) {
208 cricket::ServerAddresses stun_servers_1 = {stun_server_1};
209 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1};
210 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false);
211 EXPECT_EQ(stun_servers_1, allocator_->stun_servers());
212 EXPECT_EQ(turn_servers_1, allocator_->turn_servers());
213
214 // Update with a different set of servers, but first freeze the pool.
215 allocator_->FreezeCandidatePool();
216 cricket::ServerAddresses stun_servers_2 = {stun_server_2};
217 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2};
218 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false);
219 EXPECT_EQ(stun_servers_2, allocator_->stun_servers());
220 EXPECT_EQ(turn_servers_2, allocator_->turn_servers());
221 auto session = TakePooledSession();
222 ASSERT_NE(nullptr, session.get());
223 EXPECT_EQ(stun_servers_1, session->stun_servers());
224 EXPECT_EQ(turn_servers_1, session->turn_servers());
225 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
226}
227
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700228TEST_F(PortAllocatorTest, GetPooledSessionReturnsNextSession) {
229 SetConfigurationWithPoolSize(2);
230 auto peeked_session_1 = GetPooledSession();
231 auto session_1 = TakePooledSession();
232 EXPECT_EQ(session_1.get(), peeked_session_1);
233 auto peeked_session_2 = GetPooledSession();
234 auto session_2 = TakePooledSession();
235 EXPECT_EQ(session_2.get(), peeked_session_2);
236}
237
238// Verify that subclasses of PortAllocatorSession are given a chance to update
239// ICE parameters when TakePooledSession is called, and the base class updates
240// the info itself.
241TEST_F(PortAllocatorTest, TakePooledSessionUpdatesIceParameters) {
242 SetConfigurationWithPoolSize(1);
243 auto peeked_session = GetPooledSession();
244 ASSERT_NE(nullptr, peeked_session);
245 EXPECT_EQ(0, peeked_session->transport_info_update_count());
246 std::unique_ptr<cricket::FakePortAllocatorSession> session(
247 static_cast<cricket::FakePortAllocatorSession*>(
248 allocator_->TakePooledSession(kContentName, 1, kIceUfrag, kIcePwd)
249 .release()));
250 EXPECT_EQ(1, session->transport_info_update_count());
251 EXPECT_EQ(kContentName, session->content_name());
252 EXPECT_EQ(1, session->component());
253 EXPECT_EQ(kIceUfrag, session->ice_ufrag());
254 EXPECT_EQ(kIcePwd, session->ice_pwd());
255}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700256
257// According to JSEP, candidate filtering should be done when the pooled
258// candidates are surfaced to the application. This means when a pooled
259// session is taken. So a pooled session should gather candidates
260// unfiltered until it's returned by TakePooledSession.
261TEST_F(PortAllocatorTest, TakePooledSessionUpdatesCandidateFilter) {
262 allocator_->set_candidate_filter(cricket::CF_RELAY);
263 SetConfigurationWithPoolSize(1);
264 auto peeked_session = GetPooledSession();
265 ASSERT_NE(nullptr, peeked_session);
266 EXPECT_EQ(cricket::CF_ALL, peeked_session->candidate_filter());
267 auto session = TakePooledSession();
268 EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
269}
deadbeef42a42632017-03-10 15:18:00 -0800270
271// Verify that after DiscardCandidatePool, TakePooledSession doesn't return
272// anything.
273TEST_F(PortAllocatorTest, DiscardCandidatePool) {
274 SetConfigurationWithPoolSize(1);
275 allocator_->DiscardCandidatePool();
276 EXPECT_EQ(0, GetAllPooledSessionsReturnCount());
277}