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* PeekPooledSession() const { | |
41 return static_cast<const cricket::FakePortAllocatorSession*>( | |
42 allocator_->PeekPooledSession()); | |
43 } | |
44 | |
45 std::unique_ptr<cricket::FakePortAllocatorSession> GetPooledSession() { | |
46 return std::unique_ptr<cricket::FakePortAllocatorSession>( | |
47 static_cast<cricket::FakePortAllocatorSession*>( | |
48 allocator_->GetPooledSession(kContentName, 0, kIceUfrag, kIcePwd))); | |
49 } | |
50 | |
51 // Should only be called at the end of a test as it clears the sessions. | |
52 bool HasExactlyNPooledSessions(int num_sessions) { | |
pthatcher1
2016/05/05 21:51:25
Why not call this TakeAllPooledSessions() and have
Taylor Brandstetter
2016/05/06 03:53:35
Good idea, done.
| |
53 while (num_sessions > 0) { | |
54 auto session = GetPooledSession(); | |
55 if (!session.get()) { | |
56 return false; | |
57 } | |
58 --num_sessions; | |
59 } | |
60 if (PeekPooledSession()) { | |
61 return false; | |
62 } | |
63 return true; | |
64 } | |
65 | |
66 std::unique_ptr<cricket::FakePortAllocator> allocator_; | |
67 rtc::SocketAddress stun_server_1{"11.11.11.11", 3478}; | |
68 rtc::SocketAddress stun_server_2{"22.22.22.22", 3478}; | |
69 cricket::RelayServerConfig turn_server_1{"11.11.11.11", 3478, | |
70 kTurnUsername, kTurnPassword, | |
71 cricket::PROTO_UDP, false}; | |
72 cricket::RelayServerConfig turn_server_2{"22.22.22.22", 3478, | |
73 kTurnUsername, kTurnPassword, | |
74 cricket::PROTO_UDP, false}; | |
75 }; | |
76 | |
77 TEST_F(PortAllocatorTest, TestDefaults) { | |
78 EXPECT_EQ(0UL, allocator_->stun_servers().size()); | |
79 EXPECT_EQ(0UL, allocator_->turn_servers().size()); | |
80 EXPECT_EQ(0, allocator_->candidate_pool_size()); | |
81 EXPECT_TRUE(HasExactlyNPooledSessions(0)); | |
82 } | |
83 | |
84 TEST_F(PortAllocatorTest, SetConfigurationUpdatesIceServers) { | |
85 cricket::ServerAddresses stun_servers_1 = {stun_server_1}; | |
86 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; | |
87 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 0); | |
88 EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); | |
89 EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); | |
90 | |
91 // Update with a different set of servers. | |
92 cricket::ServerAddresses stun_servers_2 = {stun_server_2}; | |
93 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; | |
94 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 0); | |
95 EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); | |
96 EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); | |
97 } | |
98 | |
99 TEST_F(PortAllocatorTest, SetConfigurationUpdatesCandidatePoolSize) { | |
100 SetConfigurationWithPoolSize(2); | |
101 EXPECT_EQ(2, allocator_->candidate_pool_size()); | |
102 SetConfigurationWithPoolSize(3); | |
103 EXPECT_EQ(3, allocator_->candidate_pool_size()); | |
104 SetConfigurationWithPoolSize(1); | |
105 EXPECT_EQ(1, allocator_->candidate_pool_size()); | |
106 SetConfigurationWithPoolSize(4); | |
107 EXPECT_EQ(4, allocator_->candidate_pool_size()); | |
108 } | |
109 | |
110 // A negative pool size should just be treated as zero. | |
111 TEST_F(PortAllocatorTest, SetConfigurationWithNegativePoolSizeDoesntCrash) { | |
112 SetConfigurationWithPoolSize(-1); | |
pthatcher1
2016/05/05 21:51:25
Are you going to verify something here?
Taylor Brandstetter
2016/05/06 03:53:35
Just that it "DoesntCrash" (see test name).
pthatcher1
2016/05/07 00:21:44
Ah. I missed that. Perhaps just a comment like s
Taylor Brandstetter
2016/05/09 22:07:10
Done.
| |
113 } | |
114 | |
115 // Test that if the candidate pool size is nonzero, pooled sessions are | |
116 // created, and StartGettingPorts is called on them. | |
117 TEST_F(PortAllocatorTest, SetConfigurationCreatesPooledSessions) { | |
118 SetConfigurationWithPoolSize(2); | |
119 auto session_1 = GetPooledSession(); | |
120 auto session_2 = GetPooledSession(); | |
121 ASSERT_NE(nullptr, session_1.get()); | |
122 ASSERT_NE(nullptr, session_2.get()); | |
123 EXPECT_EQ(1, session_1->port_config_count()); | |
124 EXPECT_EQ(1, session_2->port_config_count()); | |
125 EXPECT_TRUE(HasExactlyNPooledSessions(0)); | |
126 } | |
127 | |
128 // Test that if the candidate pool size is increased, pooled sessions are | |
129 // created as necessary. | |
130 TEST_F(PortAllocatorTest, SetConfigurationCreatesMorePooledSessions) { | |
131 SetConfigurationWithPoolSize(1); | |
132 SetConfigurationWithPoolSize(2); | |
133 EXPECT_TRUE(HasExactlyNPooledSessions(2)); | |
134 } | |
135 | |
136 // Test that if the candidate pool size is reduced, extra sessions are | |
137 // destroyed. | |
138 TEST_F(PortAllocatorTest, SetConfigurationDestroysPooledSessions) { | |
139 SetConfigurationWithPoolSize(2); | |
140 SetConfigurationWithPoolSize(1); | |
141 EXPECT_TRUE(HasExactlyNPooledSessions(1)); | |
142 } | |
143 | |
144 // Test that if the candidate pool size is reduced and increased, but reducing | |
145 // didn't actually destroy any sessions (because they were already given away), | |
146 // increasing the size to its initial value doesn't create a new session. | |
147 TEST_F(PortAllocatorTest, SetConfigurationDoesntCreateExtraSessions) { | |
148 SetConfigurationWithPoolSize(1); | |
149 GetPooledSession(); | |
150 SetConfigurationWithPoolSize(0); | |
151 SetConfigurationWithPoolSize(1); | |
152 EXPECT_TRUE(HasExactlyNPooledSessions(0)); | |
153 } | |
154 | |
155 // According to JSEP, exising pooled sessions should be destroyed and new | |
156 // ones created when the ICE servers change. | |
157 TEST_F(PortAllocatorTest, | |
158 SetConfigurationRecreatesPooledSessionsWhenIceServersChange) { | |
159 cricket::ServerAddresses stun_servers_1 = {stun_server_1}; | |
160 std::vector<cricket::RelayServerConfig> turn_servers_1 = {turn_server_1}; | |
161 allocator_->SetConfiguration(stun_servers_1, turn_servers_1, 1); | |
162 EXPECT_EQ(stun_servers_1, allocator_->stun_servers()); | |
163 EXPECT_EQ(turn_servers_1, allocator_->turn_servers()); | |
164 | |
165 // Update with a different set of servers (and also change pool size). | |
166 cricket::ServerAddresses stun_servers_2 = {stun_server_2}; | |
167 std::vector<cricket::RelayServerConfig> turn_servers_2 = {turn_server_2}; | |
168 allocator_->SetConfiguration(stun_servers_2, turn_servers_2, 2); | |
169 EXPECT_EQ(stun_servers_2, allocator_->stun_servers()); | |
170 EXPECT_EQ(turn_servers_2, allocator_->turn_servers()); | |
171 auto session_1 = GetPooledSession(); | |
172 auto session_2 = GetPooledSession(); | |
173 ASSERT_NE(nullptr, session_1.get()); | |
174 ASSERT_NE(nullptr, session_2.get()); | |
175 EXPECT_EQ(stun_servers_2, session_1->stun_servers()); | |
176 EXPECT_EQ(turn_servers_2, session_1->turn_servers()); | |
177 EXPECT_EQ(stun_servers_2, session_2->stun_servers()); | |
178 EXPECT_EQ(turn_servers_2, session_2->turn_servers()); | |
179 EXPECT_TRUE(HasExactlyNPooledSessions(0)); | |
180 } | |
181 | |
182 TEST_F(PortAllocatorTest, PeekPooledSessionReturnsNextSession) { | |
183 SetConfigurationWithPoolSize(2); | |
184 auto peeked_session_1 = PeekPooledSession(); | |
185 auto session_1 = GetPooledSession(); | |
186 EXPECT_EQ(session_1.get(), peeked_session_1); | |
187 auto peeked_session_2 = PeekPooledSession(); | |
188 auto session_2 = GetPooledSession(); | |
189 EXPECT_EQ(session_2.get(), peeked_session_2); | |
190 } | |
191 | |
192 // Verify that subclasses of PortAllocatorSession are given a chance to update | |
193 // transport info when GetPooledSession is called, and the base class updates | |
194 // the info itself. | |
195 TEST_F(PortAllocatorTest, GetPooledSessionUpdatesTransportInfo) { | |
196 SetConfigurationWithPoolSize(1); | |
197 auto peeked_session = PeekPooledSession(); | |
198 ASSERT_NE(nullptr, peeked_session); | |
199 EXPECT_EQ(0, peeked_session->transport_info_updates()); | |
200 std::unique_ptr<cricket::FakePortAllocatorSession> session( | |
201 static_cast<cricket::FakePortAllocatorSession*>( | |
202 allocator_->GetPooledSession(kContentName, 1, kIceUfrag, kIcePwd))); | |
203 EXPECT_EQ(1, session->transport_info_updates()); | |
204 EXPECT_EQ(kContentName, session->content_name()); | |
205 EXPECT_EQ(1, session->component()); | |
206 EXPECT_EQ(kIceUfrag, session->ice_ufrag()); | |
207 EXPECT_EQ(kIcePwd, session->ice_pwd()); | |
208 } | |
OLD | NEW |