OLD | NEW |
(Empty) | |
| 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 |
| 20 static const char kIceUfrag[] = "TESTICEUFRAG0000"; |
| 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>(), |
| 37 candidate_pool_size); |
| 38 } |
| 39 |
| 40 const cricket::FakePortAllocatorSession* GetPooledSession() const { |
| 41 return static_cast<const cricket::FakePortAllocatorSession*>( |
| 42 allocator_->GetPooledSession()); |
| 43 } |
| 44 |
| 45 std::unique_ptr<cricket::FakePortAllocatorSession> TakePooledSession() { |
| 46 return std::unique_ptr<cricket::FakePortAllocatorSession>( |
| 47 static_cast<cricket::FakePortAllocatorSession*>( |
| 48 allocator_->TakePooledSession(kContentName, 0, kIceUfrag, kIcePwd) |
| 49 .release())); |
| 50 } |
| 51 |
| 52 int GetAllPooledSessionsReturnCount() { |
| 53 int count = 0; |
| 54 while (GetPooledSession()) { |
| 55 TakePooledSession(); |
| 56 ++count; |
| 57 } |
| 58 return count; |
| 59 } |
| 60 |
| 61 std::unique_ptr<cricket::FakePortAllocator> allocator_; |
| 62 rtc::SocketAddress stun_server_1{"11.11.11.11", 3478}; |
| 63 rtc::SocketAddress stun_server_2{"22.22.22.22", 3478}; |
| 64 cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478, |
| 65 kTurnUsername, kTurnPassword, |
| 66 cricket::PROTO_UDP, false}; |
| 67 cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478, |
| 68 kTurnUsername, kTurnPassword, |
| 69 cricket::PROTO_UDP, false}; |
| 70 }; |
| 71 |
| 72 TEST_F(PortAllocatorTest, TestDefaults) { |
| 73 EXPECT_EQ(0UL, allocator_->stun_servers().size()); |
| 74 EXPECT_EQ(0UL, allocator_->turn_servers().size()); |
| 75 EXPECT_EQ(0, allocator_->candidate_pool_size()); |
| 76 EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 77 } |
| 78 |
| 79 TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) { |
| 80 cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 81 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
| 82 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0); |
| 83 EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); |
| 84 EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); |
| 85 |
| 86 // Update with a different set of servers. |
| 87 cricket::ServerAddresses stun_servers_2 = {stun_server_2}; |
| 88 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; |
| 89 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0); |
| 90 EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 91 EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 92 } |
| 93 |
| 94 TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) { |
| 95 SetConfigurationWithPoolSize(2); |
| 96 EXPECT_EQ(2, allocator_->candidate_pool_size()); |
| 97 SetConfigurationWithPoolSize(3); |
| 98 EXPECT_EQ(3, allocator_->candidate_pool_size()); |
| 99 SetConfigurationWithPoolSize(1); |
| 100 EXPECT_EQ(1, allocator_->candidate_pool_size()); |
| 101 SetConfigurationWithPoolSize(4); |
| 102 EXPECT_EQ(4, allocator_->candidate_pool_size()); |
| 103 } |
| 104 |
| 105 // A negative pool size should just be treated as zero. |
| 106 TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeDoesntCrash) { |
| 107 SetConfigurationWithPoolSize(-1); |
| 108 // No asserts; we're just testing that this doesn't crash. |
| 109 } |
| 110 |
| 111 // Test that if the candidate pool size is nonzero, pooled sessions are |
| 112 // created, and StartGettingPorts is called on them. |
| 113 TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) { |
| 114 SetConfigurationWithPoolSize(2); |
| 115 auto session_1 = TakePooledSession(); |
| 116 auto session_2 = TakePooledSession(); |
| 117 ASSERT_NE(nullptr, session_1.get()); |
| 118 ASSERT_NE(nullptr, session_2.get()); |
| 119 EXPECT_EQ(1, session_1->port_config_count()); |
| 120 EXPECT_EQ(1, session_2->port_config_count()); |
| 121 EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 122 } |
| 123 |
| 124 // Test that if the candidate pool size is increased, pooled sessions are |
| 125 // created as necessary. |
| 126 TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) { |
| 127 SetConfigurationWithPoolSize(1); |
| 128 SetConfigurationWithPoolSize(2); |
| 129 EXPECT_EQ(2, GetAllPooledSessionsReturnCount()); |
| 130 } |
| 131 |
| 132 // Test that if the candidate pool size is reduced, extra sessions are |
| 133 // destroyed. |
| 134 TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) { |
| 135 SetConfigurationWithPoolSize(2); |
| 136 SetConfigurationWithPoolSize(1); |
| 137 EXPECT_EQ(1, GetAllPooledSessionsReturnCount()); |
| 138 } |
| 139 |
| 140 // Test that if the candidate pool size is reduced and increased, but reducing |
| 141 // didn't actually destroy any sessions (because they were already given away), |
| 142 // increasing the size to its initial value doesn't create a new session. |
| 143 TEST_F(PortAllocatorTest, SetConfigurationDoesntCreateExtraSessions) { |
| 144 SetConfigurationWithPoolSize(1); |
| 145 TakePooledSession(); |
| 146 SetConfigurationWithPoolSize(0); |
| 147 SetConfigurationWithPoolSize(1); |
| 148 EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 149 } |
| 150 |
| 151 // According to JSEP, exising pooled sessions should be destroyed and new |
| 152 // ones created when the ICE servers change. |
| 153 TEST_F(PortAllocatorTest, |
| 154 SetConfigurationRecreatesPooledSessionsWhenIceServersChange) { |
| 155 cricket::ServerAddresses stun_servers_1 = {stun_server_1}; |
| 156 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; |
| 157 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1); |
| 158 EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); |
| 159 EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); |
| 160 |
| 161 // Update with a different set of servers (and also change pool size). |
| 162 cricket::ServerAddresses stun_servers_2 = {stun_server_2}; |
| 163 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; |
| 164 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2); |
| 165 EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); |
| 166 EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); |
| 167 auto session_1 = TakePooledSession(); |
| 168 auto session_2 = TakePooledSession(); |
| 169 ASSERT_NE(nullptr, session_1.get()); |
| 170 ASSERT_NE(nullptr, session_2.get()); |
| 171 EXPECT_EQ(stun_servers_2, session_1->stun_servers()); |
| 172 EXPECT_EQ(turn_servers_2, session_1->turn_servers()); |
| 173 EXPECT_EQ(stun_servers_2, session_2->stun_servers()); |
| 174 EXPECT_EQ(turn_servers_2, session_2->turn_servers()); |
| 175 EXPECT_EQ(0, GetAllPooledSessionsReturnCount()); |
| 176 } |
| 177 |
| 178 TEST_F(PortAllocatorTest, GetPooledSessionReturnsNextSession) { |
| 179 SetConfigurationWithPoolSize(2); |
| 180 auto peeked_session_1 = GetPooledSession(); |
| 181 auto session_1 = TakePooledSession(); |
| 182 EXPECT_EQ(session_1.get(), peeked_session_1); |
| 183 auto peeked_session_2 = GetPooledSession(); |
| 184 auto session_2 = TakePooledSession(); |
| 185 EXPECT_EQ(session_2.get(), peeked_session_2); |
| 186 } |
| 187 |
| 188 // Verify that subclasses of PortAllocatorSession are given a chance to update |
| 189 // ICE parameters when TakePooledSession is called, and the base class updates |
| 190 // the info itself. |
| 191 TEST_F(PortAllocatorTest, TakePooledSessionUpdatesIceParameters) { |
| 192 SetConfigurationWithPoolSize(1); |
| 193 auto peeked_session = GetPooledSession(); |
| 194 ASSERT_NE(nullptr, peeked_session); |
| 195 EXPECT_EQ(0, peeked_session->transport_info_update_count()); |
| 196 std::unique_ptr<cricket::FakePortAllocatorSession> session( |
| 197 static_cast<cricket::FakePortAllocatorSession*>( |
| 198 allocator_->TakePooledSession(kContentName, 1, kIceUfrag, kIcePwd) |
| 199 .release())); |
| 200 EXPECT_EQ(1, session->transport_info_update_count()); |
| 201 EXPECT_EQ(kContentName, session->content_name()); |
| 202 EXPECT_EQ(1, session->component()); |
| 203 EXPECT_EQ(kIceUfrag, session->ice_ufrag()); |
| 204 EXPECT_EQ(kIcePwd, session->ice_pwd()); |
| 205 } |
OLD | NEW |