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 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 13 | #include "webrtc/p2p/base/fakeportallocator.h" |
| 14 | #include "webrtc/p2p/base/portallocator.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame^] | 15 | #include "webrtc/rtc_base/gunit.h" |
| 16 | #include "webrtc/rtc_base/thread.h" |
| 17 | #include "webrtc/rtc_base/virtualsocketserver.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; |
| 74 | while (GetPooledSession()) { |
| 75 | TakePooledSession(); |
| 76 | ++count; |
| 77 | } |
| 78 | return count; |
| 79 | } |
| 80 | |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 81 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 82 | rtc::AutoSocketServerThread main_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 83 | 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 | |
| 94 | TEST_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 Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 101 | // Call CreateSession and verify that the parameters passed in and the |
| 102 | // candidate filter are applied as expected. |
| 103 | TEST_F(PortAllocatorTest, CreateSession) { |
| 104 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 105 | auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 106 | 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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 114 | TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) { |
| 115 | cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 116 | std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 117 | EXPECT_TRUE( |
| 118 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 119 | 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}; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 125 | EXPECT_TRUE( |
| 126 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 127 | EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 128 | EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 129 | } |
| 130 | |
| 131 | TEST_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. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 143 | TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeFails) { |
| 144 | SetConfigurationWithPoolSizeExpectFailure(-1); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Test that if the candidate pool size is nonzero, pooled sessions are |
| 148 | // created, and StartGettingPorts is called on them. |
| 149 | TEST_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. |
| 162 | TEST_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. |
| 170 | TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) { |
| 171 | SetConfigurationWithPoolSize(2); |
| 172 | SetConfigurationWithPoolSize(1); |
| 173 | EXPECT_EQ(1, GetAllPooledSessionsReturnCount()); |
| 174 | } |
| 175 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 176 | // According to JSEP, existing pooled sessions should be destroyed and new |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 177 | // ones created when the ICE servers change. |
| 178 | TEST_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 Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 182 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 183 | 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 Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 189 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 190 | 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 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 203 | // 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. |
| 206 | TEST_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 Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 228 | TEST_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. |
| 241 | TEST_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 Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 256 | |
| 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. |
| 261 | TEST_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 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 270 | |
| 271 | // Verify that after DiscardCandidatePool, TakePooledSession doesn't return |
| 272 | // anything. |
| 273 | TEST_F(PortAllocatorTest, DiscardCandidatePool) { |
| 274 | SetConfigurationWithPoolSize(1); |
| 275 | allocator_->DiscardCandidatePool(); |
| 276 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 277 | } |