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