Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 13 | #include "p2p/base/fake_port_allocator.h" |
| 14 | #include "p2p/base/port_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 16 | #include "rtc_base/virtual_socket_server.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include "test/gtest.h" |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 18 | |
| 19 | static const char kContentName[] = "test content"; |
| 20 | // Based on ICE_UFRAG_LENGTH |
zhihuang | 6d0d4bf | 2016-05-24 10:13:32 -0700 | [diff] [blame] | 21 | static const char kIceUfrag[] = "UF00"; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 22 | // Based on ICE_PWD_LENGTH |
| 23 | static const char kIcePwd[] = "TESTICEPWD00000000000000"; |
| 24 | static const char kTurnUsername[] = "test"; |
| 25 | static const char kTurnPassword[] = "test"; |
| 26 | |
| 27 | class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { |
| 28 | public: |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 29 | PortAllocatorTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 30 | : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 31 | allocator_.reset( |
| 32 | new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); |
| 33 | } |
| 34 | |
| 35 | protected: |
| 36 | void SetConfigurationWithPoolSize(int candidate_pool_size) { |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 37 | 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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 48 | std::unique_ptr<cricket::FakePortAllocatorSession> CreateSession( |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 49 | 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_ |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 56 | ->CreateSession(content_name, component, ice_ufrag, ice_pwd) |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 57 | .release())); |
| 58 | } |
| 59 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 60 | 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 Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 74 | while (TakePooledSession() != nullptr) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 75 | ++count; |
| 76 | } |
| 77 | return count; |
| 78 | } |
| 79 | |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 80 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 81 | rtc::AutoSocketServerThread main_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 82 | 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 | |
| 93 | TEST_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 Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 100 | // Call CreateSession and verify that the parameters passed in and the |
| 101 | // candidate filter are applied as expected. |
| 102 | TEST_F(PortAllocatorTest, CreateSession) { |
| 103 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 104 | auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 105 | 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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 113 | TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) { |
| 114 | cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 115 | std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 116 | EXPECT_TRUE( |
| 117 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 118 | 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}; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 124 | EXPECT_TRUE( |
| 125 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 126 | EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 127 | EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 128 | } |
| 129 | |
| 130 | TEST_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. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 142 | TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeFails) { |
| 143 | SetConfigurationWithPoolSizeExpectFailure(-1); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Test that if the candidate pool size is nonzero, pooled sessions are |
| 147 | // created, and StartGettingPorts is called on them. |
| 148 | TEST_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. |
| 161 | TEST_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. |
| 169 | TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) { |
| 170 | SetConfigurationWithPoolSize(2); |
| 171 | SetConfigurationWithPoolSize(1); |
| 172 | EXPECT_EQ(1, GetAllPooledSessionsReturnCount()); |
| 173 | } |
| 174 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 175 | // According to JSEP, existing pooled sessions should be destroyed and new |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 176 | // ones created when the ICE servers change. |
| 177 | TEST_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 Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 181 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 182 | 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 Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 188 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 189 | 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 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 202 | // 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. |
| 205 | TEST_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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 227 | TEST_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. |
| 240 | TEST_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 Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 255 | |
| 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. |
| 260 | TEST_F(PortAllocatorTest, TakePooledSessionUpdatesCandidateFilter) { |
| 261 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
| 262 | 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 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 269 | |
| 270 | // Verify that after DiscardCandidatePool, TakePooledSession doesn't return |
| 271 | // anything. |
| 272 | TEST_F(PortAllocatorTest, DiscardCandidatePool) { |
| 273 | SetConfigurationWithPoolSize(1); |
| 274 | allocator_->DiscardCandidatePool(); |
| 275 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 276 | } |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 277 | |
| 278 | TEST_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 | } |