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 | |
| 13 | #include "webrtc/base/gunit.h" |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame^] | 14 | #include "webrtc/base/physicalsocketserver.h" |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 15 | #include "webrtc/base/thread.h" |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame^] | 16 | #include "webrtc/base/virtualsocketserver.h" |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 17 | #include "webrtc/p2p/base/fakeportallocator.h" |
| 18 | #include "webrtc/p2p/base/portallocator.h" |
| 19 | |
| 20 | static const char kContentName[] = "test content"; |
| 21 | // Based on ICE_UFRAG_LENGTH |
zhihuang | 6d0d4bf | 2016-05-24 10:13:32 -0700 | [diff] [blame] | 22 | static const char kIceUfrag[] = "UF00"; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 23 | // Based on ICE_PWD_LENGTH |
| 24 | static const char kIcePwd[] = "TESTICEPWD00000000000000"; |
| 25 | static const char kTurnUsername[] = "test"; |
| 26 | static const char kTurnPassword[] = "test"; |
| 27 | |
| 28 | class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> { |
| 29 | public: |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame^] | 30 | PortAllocatorTest() |
| 31 | : pss_(new rtc::PhysicalSocketServer), |
| 32 | vss_(new rtc::VirtualSocketServer(pss_.get())), |
| 33 | main_(vss_.get()) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 34 | allocator_.reset( |
| 35 | new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); |
| 36 | } |
| 37 | |
| 38 | protected: |
| 39 | void SetConfigurationWithPoolSize(int candidate_pool_size) { |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 40 | EXPECT_TRUE(allocator_->SetConfiguration( |
| 41 | cricket::ServerAddresses(), std::vector<cricket::RelayServerConfig>(), |
| 42 | candidate_pool_size, false)); |
| 43 | } |
| 44 | |
| 45 | void SetConfigurationWithPoolSizeExpectFailure(int candidate_pool_size) { |
| 46 | EXPECT_FALSE(allocator_->SetConfiguration( |
| 47 | cricket::ServerAddresses(), std::vector<cricket::RelayServerConfig>(), |
| 48 | candidate_pool_size, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 51 | std::unique_ptr<cricket::FakePortAllocatorSession> CreateSession( |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 52 | const std::string& content_name, |
| 53 | int component, |
| 54 | const std::string& ice_ufrag, |
| 55 | const std::string& ice_pwd) { |
| 56 | return std::unique_ptr<cricket::FakePortAllocatorSession>( |
| 57 | static_cast<cricket::FakePortAllocatorSession*>( |
| 58 | allocator_ |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 59 | ->CreateSession(content_name, component, ice_ufrag, ice_pwd) |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 60 | .release())); |
| 61 | } |
| 62 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 63 | const cricket::FakePortAllocatorSession* GetPooledSession() const { |
| 64 | return static_cast<const cricket::FakePortAllocatorSession*>( |
| 65 | allocator_->GetPooledSession()); |
| 66 | } |
| 67 | |
| 68 | std::unique_ptr<cricket::FakePortAllocatorSession> TakePooledSession() { |
| 69 | return std::unique_ptr<cricket::FakePortAllocatorSession>( |
| 70 | static_cast<cricket::FakePortAllocatorSession*>( |
| 71 | allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd) |
| 72 | .release())); |
| 73 | } |
| 74 | |
| 75 | int GetAllPooledSessionsReturnCount() { |
| 76 | int count = 0; |
| 77 | while (GetPooledSession()) { |
| 78 | TakePooledSession(); |
| 79 | ++count; |
| 80 | } |
| 81 | return count; |
| 82 | } |
| 83 | |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame^] | 84 | std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 85 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 86 | rtc::AutoSocketServerThread main_; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 87 | std::unique_ptr<cricket::FakePortAllocator> allocator_; |
| 88 | rtc::SocketAddress stun_server_1{"11.11.11.11", 3478}; |
| 89 | rtc::SocketAddress stun_server_2{"22.22.22.22", 3478}; |
| 90 | cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478, |
| 91 | kTurnUsername, kTurnPassword, |
| 92 | cricket::PROTO_UDP, false}; |
| 93 | cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478, |
| 94 | kTurnUsername, kTurnPassword, |
| 95 | cricket::PROTO_UDP, false}; |
| 96 | }; |
| 97 | |
| 98 | TEST_F(PortAllocatorTest, TestDefaults) { |
| 99 | EXPECT_EQ(0UL, allocator_->stun_servers().size()); |
| 100 | EXPECT_EQ(0UL, allocator_->turn_servers().size()); |
| 101 | EXPECT_EQ(0, allocator_->candidate_pool_size()); |
| 102 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 103 | } |
| 104 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 105 | // Call CreateSession and verify that the parameters passed in and the |
| 106 | // candidate filter are applied as expected. |
| 107 | TEST_F(PortAllocatorTest, CreateSession) { |
| 108 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 109 | auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 110 | ASSERT_NE(nullptr, session); |
| 111 | EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter()); |
| 112 | EXPECT_EQ(kContentName, session->content_name()); |
| 113 | EXPECT_EQ(1, session->component()); |
| 114 | EXPECT_EQ(kIceUfrag, session->ice_ufrag()); |
| 115 | EXPECT_EQ(kIcePwd, session->ice_pwd()); |
| 116 | } |
| 117 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 118 | TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) { |
| 119 | cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 120 | std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 121 | EXPECT_TRUE( |
| 122 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 123 | EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); |
| 124 | EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); |
| 125 | |
| 126 | // Update with a different set of servers. |
| 127 | cricket::ServerAddresses stun_servers_2 = {stun_server_2}; |
| 128 | std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 129 | EXPECT_TRUE( |
| 130 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0, false)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 131 | EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 132 | EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 133 | } |
| 134 | |
| 135 | TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) { |
| 136 | SetConfigurationWithPoolSize(2); |
| 137 | EXPECT_EQ(2, allocator_->candidate_pool_size()); |
| 138 | SetConfigurationWithPoolSize(3); |
| 139 | EXPECT_EQ(3, allocator_->candidate_pool_size()); |
| 140 | SetConfigurationWithPoolSize(1); |
| 141 | EXPECT_EQ(1, allocator_->candidate_pool_size()); |
| 142 | SetConfigurationWithPoolSize(4); |
| 143 | EXPECT_EQ(4, allocator_->candidate_pool_size()); |
| 144 | } |
| 145 | |
| 146 | // A negative pool size should just be treated as zero. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 147 | TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeFails) { |
| 148 | SetConfigurationWithPoolSizeExpectFailure(-1); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Test that if the candidate pool size is nonzero, pooled sessions are |
| 152 | // created, and StartGettingPorts is called on them. |
| 153 | TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) { |
| 154 | SetConfigurationWithPoolSize(2); |
| 155 | auto session_1 = TakePooledSession(); |
| 156 | auto session_2 = TakePooledSession(); |
| 157 | ASSERT_NE(nullptr, session_1.get()); |
| 158 | ASSERT_NE(nullptr, session_2.get()); |
| 159 | EXPECT_EQ(1, session_1->port_config_count()); |
| 160 | EXPECT_EQ(1, session_2->port_config_count()); |
| 161 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 162 | } |
| 163 | |
| 164 | // Test that if the candidate pool size is increased, pooled sessions are |
| 165 | // created as necessary. |
| 166 | TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) { |
| 167 | SetConfigurationWithPoolSize(1); |
| 168 | SetConfigurationWithPoolSize(2); |
| 169 | EXPECT_EQ(2, GetAllPooledSessionsReturnCount()); |
| 170 | } |
| 171 | |
| 172 | // Test that if the candidate pool size is reduced, extra sessions are |
| 173 | // destroyed. |
| 174 | TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) { |
| 175 | SetConfigurationWithPoolSize(2); |
| 176 | SetConfigurationWithPoolSize(1); |
| 177 | EXPECT_EQ(1, GetAllPooledSessionsReturnCount()); |
| 178 | } |
| 179 | |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 180 | // According to JSEP, existing pooled sessions should be destroyed and new |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 181 | // ones created when the ICE servers change. |
| 182 | TEST_F(PortAllocatorTest, |
| 183 | SetConfigurationRecreatesPooledSessionsWhenIceServersChange) { |
| 184 | cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 185 | std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 186 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 187 | EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); |
| 188 | EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); |
| 189 | |
| 190 | // Update with a different set of servers (and also change pool size). |
| 191 | cricket::ServerAddresses stun_servers_2 = {stun_server_2}; |
| 192 | std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 193 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 194 | EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 195 | EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 196 | auto session_1 = TakePooledSession(); |
| 197 | auto session_2 = TakePooledSession(); |
| 198 | ASSERT_NE(nullptr, session_1.get()); |
| 199 | ASSERT_NE(nullptr, session_2.get()); |
| 200 | EXPECT_EQ(stun_servers_2, session_1->stun_servers()); |
| 201 | EXPECT_EQ(turn_servers_2, session_1->turn_servers()); |
| 202 | EXPECT_EQ(stun_servers_2, session_2->stun_servers()); |
| 203 | EXPECT_EQ(turn_servers_2, session_2->turn_servers()); |
| 204 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 205 | } |
| 206 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 207 | // According to JSEP, after SetLocalDescription, setting different ICE servers |
| 208 | // will not cause the pool to be refilled. This is implemented by the |
| 209 | // PeerConnection calling FreezeCandidatePool when a local description is set. |
| 210 | TEST_F(PortAllocatorTest, |
| 211 | SetConfigurationDoesNotRecreatePooledSessionsAfterFreezeCandidatePool) { |
| 212 | cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 213 | std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
| 214 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1, false); |
| 215 | EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); |
| 216 | EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); |
| 217 | |
| 218 | // Update with a different set of servers, but first freeze the pool. |
| 219 | allocator_->FreezeCandidatePool(); |
| 220 | cricket::ServerAddresses stun_servers_2 = {stun_server_2}; |
| 221 | std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; |
| 222 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2, false); |
| 223 | EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 224 | EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 225 | auto session = TakePooledSession(); |
| 226 | ASSERT_NE(nullptr, session.get()); |
| 227 | EXPECT_EQ(stun_servers_1, session->stun_servers()); |
| 228 | EXPECT_EQ(turn_servers_1, session->turn_servers()); |
| 229 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 230 | } |
| 231 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 232 | TEST_F(PortAllocatorTest, GetPooledSessionReturnsNextSession) { |
| 233 | SetConfigurationWithPoolSize(2); |
| 234 | auto peeked_session_1 = GetPooledSession(); |
| 235 | auto session_1 = TakePooledSession(); |
| 236 | EXPECT_EQ(session_1.get(), peeked_session_1); |
| 237 | auto peeked_session_2 = GetPooledSession(); |
| 238 | auto session_2 = TakePooledSession(); |
| 239 | EXPECT_EQ(session_2.get(), peeked_session_2); |
| 240 | } |
| 241 | |
| 242 | // Verify that subclasses of PortAllocatorSession are given a chance to update |
| 243 | // ICE parameters when TakePooledSession is called, and the base class updates |
| 244 | // the info itself. |
| 245 | TEST_F(PortAllocatorTest, TakePooledSessionUpdatesIceParameters) { |
| 246 | SetConfigurationWithPoolSize(1); |
| 247 | auto peeked_session = GetPooledSession(); |
| 248 | ASSERT_NE(nullptr, peeked_session); |
| 249 | EXPECT_EQ(0, peeked_session->transport_info_update_count()); |
| 250 | std::unique_ptr<cricket::FakePortAllocatorSession> session( |
| 251 | static_cast<cricket::FakePortAllocatorSession*>( |
| 252 | allocator_->TakePooledSession(kContentName, 1, kIceUfrag, kIcePwd) |
| 253 | .release())); |
| 254 | EXPECT_EQ(1, session->transport_info_update_count()); |
| 255 | EXPECT_EQ(kContentName, session->content_name()); |
| 256 | EXPECT_EQ(1, session->component()); |
| 257 | EXPECT_EQ(kIceUfrag, session->ice_ufrag()); |
| 258 | EXPECT_EQ(kIcePwd, session->ice_pwd()); |
| 259 | } |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 260 | |
| 261 | // According to JSEP, candidate filtering should be done when the pooled |
| 262 | // candidates are surfaced to the application. This means when a pooled |
| 263 | // session is taken. So a pooled session should gather candidates |
| 264 | // unfiltered until it's returned by TakePooledSession. |
| 265 | TEST_F(PortAllocatorTest, TakePooledSessionUpdatesCandidateFilter) { |
| 266 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
| 267 | SetConfigurationWithPoolSize(1); |
| 268 | auto peeked_session = GetPooledSession(); |
| 269 | ASSERT_NE(nullptr, peeked_session); |
| 270 | EXPECT_EQ(cricket::CF_ALL, peeked_session->candidate_filter()); |
| 271 | auto session = TakePooledSession(); |
| 272 | EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter()); |
| 273 | } |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 274 | |
| 275 | // Verify that after DiscardCandidatePool, TakePooledSession doesn't return |
| 276 | // anything. |
| 277 | TEST_F(PortAllocatorTest, DiscardCandidatePool) { |
| 278 | SetConfigurationWithPoolSize(1); |
| 279 | allocator_->DiscardCandidatePool(); |
| 280 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 281 | } |