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