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) { |
| 35 | allocator_->SetConfiguration(cricket::ServerAddresses(), |
| 36 | std::vector<cricket::RelayServerConfig>(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 37 | candidate_pool_size, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 40 | std::unique_ptr<cricket::FakePortAllocatorSession> CreateSession( |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 41 | const std::string& content_name, |
| 42 | int component, |
| 43 | const std::string& ice_ufrag, |
| 44 | const std::string& ice_pwd) { |
| 45 | return std::unique_ptr<cricket::FakePortAllocatorSession>( |
| 46 | static_cast<cricket::FakePortAllocatorSession*>( |
| 47 | allocator_ |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 48 | ->CreateSession(content_name, component, ice_ufrag, ice_pwd) |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 49 | .release())); |
| 50 | } |
| 51 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 52 | const cricket::FakePortAllocatorSession* GetPooledSession() const { |
| 53 | return static_cast<const cricket::FakePortAllocatorSession*>( |
| 54 | allocator_->GetPooledSession()); |
| 55 | } |
| 56 | |
| 57 | std::unique_ptr<cricket::FakePortAllocatorSession> TakePooledSession() { |
| 58 | return std::unique_ptr<cricket::FakePortAllocatorSession>( |
| 59 | static_cast<cricket::FakePortAllocatorSession*>( |
| 60 | allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd) |
| 61 | .release())); |
| 62 | } |
| 63 | |
| 64 | int GetAllPooledSessionsReturnCount() { |
| 65 | int count = 0; |
| 66 | while (GetPooledSession()) { |
| 67 | TakePooledSession(); |
| 68 | ++count; |
| 69 | } |
| 70 | return count; |
| 71 | } |
| 72 | |
| 73 | std::unique_ptr<cricket::FakePortAllocator> allocator_; |
| 74 | rtc::SocketAddress stun_server_1{"11.11.11.11", 3478}; |
| 75 | rtc::SocketAddress stun_server_2{"22.22.22.22", 3478}; |
| 76 | cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478, |
| 77 | kTurnUsername, kTurnPassword, |
| 78 | cricket::PROTO_UDP, false}; |
| 79 | cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478, |
| 80 | kTurnUsername, kTurnPassword, |
| 81 | cricket::PROTO_UDP, false}; |
| 82 | }; |
| 83 | |
| 84 | TEST_F(PortAllocatorTest, TestDefaults) { |
| 85 | EXPECT_EQ(0UL, allocator_->stun_servers().size()); |
| 86 | EXPECT_EQ(0UL, allocator_->turn_servers().size()); |
| 87 | EXPECT_EQ(0, allocator_->candidate_pool_size()); |
| 88 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 89 | } |
| 90 | |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 91 | // Call CreateSession and verify that the parameters passed in and the |
| 92 | // candidate filter are applied as expected. |
| 93 | TEST_F(PortAllocatorTest, CreateSession) { |
| 94 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
johan | fe1ffb1 | 2016-08-11 12:37:42 -0700 | [diff] [blame] | 95 | auto session = CreateSession(kContentName, 1, kIceUfrag, kIcePwd); |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 96 | ASSERT_NE(nullptr, session); |
| 97 | EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter()); |
| 98 | EXPECT_EQ(kContentName, session->content_name()); |
| 99 | EXPECT_EQ(1, session->component()); |
| 100 | EXPECT_EQ(kIceUfrag, session->ice_ufrag()); |
| 101 | EXPECT_EQ(kIcePwd, session->ice_pwd()); |
| 102 | } |
| 103 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 104 | TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) { |
| 105 | cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 106 | std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 107 | allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 108 | EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); |
| 109 | EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); |
| 110 | |
| 111 | // Update with a different set of servers. |
| 112 | cricket::ServerAddresses stun_servers_2 = {stun_server_2}; |
| 113 | std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 114 | allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 115 | EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 116 | EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 117 | } |
| 118 | |
| 119 | TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) { |
| 120 | SetConfigurationWithPoolSize(2); |
| 121 | EXPECT_EQ(2, allocator_->candidate_pool_size()); |
| 122 | SetConfigurationWithPoolSize(3); |
| 123 | EXPECT_EQ(3, allocator_->candidate_pool_size()); |
| 124 | SetConfigurationWithPoolSize(1); |
| 125 | EXPECT_EQ(1, allocator_->candidate_pool_size()); |
| 126 | SetConfigurationWithPoolSize(4); |
| 127 | EXPECT_EQ(4, allocator_->candidate_pool_size()); |
| 128 | } |
| 129 | |
| 130 | // A negative pool size should just be treated as zero. |
| 131 | TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeDoesntCrash) { |
| 132 | SetConfigurationWithPoolSize(-1); |
| 133 | // No asserts; we're just testing that this doesn't crash. |
| 134 | } |
| 135 | |
| 136 | // Test that if the candidate pool size is nonzero, pooled sessions are |
| 137 | // created, and StartGettingPorts is called on them. |
| 138 | TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) { |
| 139 | SetConfigurationWithPoolSize(2); |
| 140 | auto session_1 = TakePooledSession(); |
| 141 | auto session_2 = TakePooledSession(); |
| 142 | ASSERT_NE(nullptr, session_1.get()); |
| 143 | ASSERT_NE(nullptr, session_2.get()); |
| 144 | EXPECT_EQ(1, session_1->port_config_count()); |
| 145 | EXPECT_EQ(1, session_2->port_config_count()); |
| 146 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 147 | } |
| 148 | |
| 149 | // Test that if the candidate pool size is increased, pooled sessions are |
| 150 | // created as necessary. |
| 151 | TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) { |
| 152 | SetConfigurationWithPoolSize(1); |
| 153 | SetConfigurationWithPoolSize(2); |
| 154 | EXPECT_EQ(2, GetAllPooledSessionsReturnCount()); |
| 155 | } |
| 156 | |
| 157 | // Test that if the candidate pool size is reduced, extra sessions are |
| 158 | // destroyed. |
| 159 | TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) { |
| 160 | SetConfigurationWithPoolSize(2); |
| 161 | SetConfigurationWithPoolSize(1); |
| 162 | EXPECT_EQ(1, GetAllPooledSessionsReturnCount()); |
| 163 | } |
| 164 | |
| 165 | // Test that if the candidate pool size is reduced and increased, but reducing |
| 166 | // didn't actually destroy any sessions (because they were already given away), |
| 167 | // increasing the size to its initial value doesn't create a new session. |
| 168 | TEST_F(PortAllocatorTest, SetConfigurationDoesntCreateExtraSessions) { |
| 169 | SetConfigurationWithPoolSize(1); |
| 170 | TakePooledSession(); |
| 171 | SetConfigurationWithPoolSize(0); |
| 172 | SetConfigurationWithPoolSize(1); |
| 173 | EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 174 | } |
| 175 | |
| 176 | // According to JSEP, exising pooled sessions should be destroyed and new |
| 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 | |
| 203 | TEST_F(PortAllocatorTest, GetPooledSessionReturnsNextSession) { |
| 204 | SetConfigurationWithPoolSize(2); |
| 205 | auto peeked_session_1 = GetPooledSession(); |
| 206 | auto session_1 = TakePooledSession(); |
| 207 | EXPECT_EQ(session_1.get(), peeked_session_1); |
| 208 | auto peeked_session_2 = GetPooledSession(); |
| 209 | auto session_2 = TakePooledSession(); |
| 210 | EXPECT_EQ(session_2.get(), peeked_session_2); |
| 211 | } |
| 212 | |
| 213 | // Verify that subclasses of PortAllocatorSession are given a chance to update |
| 214 | // ICE parameters when TakePooledSession is called, and the base class updates |
| 215 | // the info itself. |
| 216 | TEST_F(PortAllocatorTest, TakePooledSessionUpdatesIceParameters) { |
| 217 | SetConfigurationWithPoolSize(1); |
| 218 | auto peeked_session = GetPooledSession(); |
| 219 | ASSERT_NE(nullptr, peeked_session); |
| 220 | EXPECT_EQ(0, peeked_session->transport_info_update_count()); |
| 221 | std::unique_ptr<cricket::FakePortAllocatorSession> session( |
| 222 | static_cast<cricket::FakePortAllocatorSession*>( |
| 223 | allocator_->TakePooledSession(kContentName, 1, kIceUfrag, kIcePwd) |
| 224 | .release())); |
| 225 | EXPECT_EQ(1, session->transport_info_update_count()); |
| 226 | EXPECT_EQ(kContentName, session->content_name()); |
| 227 | EXPECT_EQ(1, session->component()); |
| 228 | EXPECT_EQ(kIceUfrag, session->ice_ufrag()); |
| 229 | EXPECT_EQ(kIcePwd, session->ice_pwd()); |
| 230 | } |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 231 | |
| 232 | // According to JSEP, candidate filtering should be done when the pooled |
| 233 | // candidates are surfaced to the application. This means when a pooled |
| 234 | // session is taken. So a pooled session should gather candidates |
| 235 | // unfiltered until it's returned by TakePooledSession. |
| 236 | TEST_F(PortAllocatorTest, TakePooledSessionUpdatesCandidateFilter) { |
| 237 | allocator_->set_candidate_filter(cricket::CF_RELAY); |
| 238 | SetConfigurationWithPoolSize(1); |
| 239 | auto peeked_session = GetPooledSession(); |
| 240 | ASSERT_NE(nullptr, peeked_session); |
| 241 | EXPECT_EQ(cricket::CF_ALL, peeked_session->candidate_filter()); |
| 242 | auto session = TakePooledSession(); |
| 243 | EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter()); |
| 244 | } |