blob: 211c3545f57ba4d1e83514b9104ad9e0fc89e4d2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2009 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/fakenetwork.h"
29#include "talk/base/firewallsocketserver.h"
30#include "talk/base/gunit.h"
31#include "talk/base/helpers.h"
32#include "talk/base/logging.h"
33#include "talk/base/natserver.h"
34#include "talk/base/natsocketfactory.h"
35#include "talk/base/network.h"
36#include "talk/base/physicalsocketserver.h"
37#include "talk/base/socketaddress.h"
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +000038#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/base/thread.h"
40#include "talk/base/virtualsocketserver.h"
41#include "talk/p2p/base/basicpacketsocketfactory.h"
42#include "talk/p2p/base/constants.h"
43#include "talk/p2p/base/p2ptransportchannel.h"
44#include "talk/p2p/base/portallocatorsessionproxy.h"
45#include "talk/p2p/base/testrelayserver.h"
46#include "talk/p2p/base/teststunserver.h"
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000047#include "talk/p2p/base/testturnserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/p2p/client/basicportallocator.h"
49#include "talk/p2p/client/httpportallocator.h"
50
51using talk_base::SocketAddress;
52using talk_base::Thread;
53
54static const SocketAddress kClientAddr("11.11.11.11", 0);
wu@webrtc.org97077a32013-10-25 21:18:33 +000055static const SocketAddress kClientIPv6Addr(
56 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000057static const SocketAddress kClientAddr2("22.22.22.22", 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058static const SocketAddress kNatAddr("77.77.77.77", talk_base::NAT_SERVER_PORT);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000059static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
61static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
62static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
63static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
64static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
65static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
66static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000067static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
68static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
70// Minimum and maximum port for port range tests.
71static const int kMinPort = 10000;
72static const int kMaxPort = 10099;
73
74// Based on ICE_UFRAG_LENGTH
75static const char kIceUfrag0[] = "TESTICEUFRAG0000";
76// Based on ICE_PWD_LENGTH
77static const char kIcePwd0[] = "TESTICEPWD00000000000000";
78
79static const char kContentName[] = "test content";
80
81static const int kDefaultAllocationTimeout = 1000;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000082static const char kTurnUsername[] = "test";
83static const char kTurnPassword[] = "test";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084
85namespace cricket {
86
87// Helper for dumping candidates
88std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
89 os << c.ToString();
90 return os;
91}
92
93} // namespace cricket
94
95class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
96 public:
97 static void SetUpTestCase() {
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +000098 talk_base::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 }
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +0000100
101 static void TearDownTestCase() {
102 talk_base::CleanupSSL();
103 }
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 PortAllocatorTest()
106 : pss_(new talk_base::PhysicalSocketServer),
107 vss_(new talk_base::VirtualSocketServer(pss_.get())),
108 fss_(new talk_base::FirewallSocketServer(vss_.get())),
109 ss_scope_(fss_.get()),
110 nat_factory_(vss_.get(), kNatAddr),
111 nat_socket_factory_(&nat_factory_),
112 stun_server_(Thread::Current(), kStunAddr),
113 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
114 kRelayTcpIntAddr, kRelayTcpExtAddr,
115 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000116 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 allocator_(new cricket::BasicPortAllocator(
118 &network_manager_, kStunAddr,
119 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr)),
120 candidate_allocation_done_(false) {
121 allocator_->set_step_delay(cricket::kMinimumStepDelay);
122 }
123
124 void AddInterface(const SocketAddress& addr) {
125 network_manager_.AddInterface(addr);
126 }
127 bool SetPortRange(int min_port, int max_port) {
128 return allocator_->SetPortRange(min_port, max_port);
129 }
130 talk_base::NATServer* CreateNatServer(const SocketAddress& addr,
131 talk_base::NATType type) {
132 return new talk_base::NATServer(type, vss_.get(), addr, vss_.get(), addr);
133 }
134
135 bool CreateSession(int component) {
136 session_.reset(CreateSession("session", component));
137 if (!session_)
138 return false;
139 return true;
140 }
141
142 bool CreateSession(int component, const std::string& content_name) {
143 session_.reset(CreateSession("session", content_name, component));
144 if (!session_)
145 return false;
146 return true;
147 }
148
149 cricket::PortAllocatorSession* CreateSession(
150 const std::string& sid, int component) {
151 return CreateSession(sid, kContentName, component);
152 }
153
154 cricket::PortAllocatorSession* CreateSession(
155 const std::string& sid, const std::string& content_name, int component) {
156 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
157 }
158
159 cricket::PortAllocatorSession* CreateSession(
160 const std::string& sid, const std::string& content_name, int component,
161 const std::string& ice_ufrag, const std::string& ice_pwd) {
162 cricket::PortAllocatorSession* session =
163 allocator_->CreateSession(
164 sid, content_name, component, ice_ufrag, ice_pwd);
165 session->SignalPortReady.connect(this,
166 &PortAllocatorTest::OnPortReady);
167 session->SignalCandidatesReady.connect(this,
168 &PortAllocatorTest::OnCandidatesReady);
169 session->SignalCandidatesAllocationDone.connect(this,
170 &PortAllocatorTest::OnCandidatesAllocationDone);
171 return session;
172 }
173
174 static bool CheckCandidate(const cricket::Candidate& c,
175 int component, const std::string& type,
176 const std::string& proto,
177 const SocketAddress& addr) {
178 return (c.component() == component && c.type() == type &&
179 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
180 ((addr.port() == 0 && (c.address().port() != 0)) ||
181 (c.address().port() == addr.port())));
182 }
183 static bool CheckPort(const talk_base::SocketAddress& addr,
184 int min_port, int max_port) {
185 return (addr.port() >= min_port && addr.port() <= max_port);
186 }
187
188 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
189 // We should only get this callback once, except in the mux test where
190 // we have multiple port allocation sessions.
191 if (session == session_.get()) {
192 ASSERT_FALSE(candidate_allocation_done_);
193 candidate_allocation_done_ = true;
194 }
195 }
196
197 // Check if all ports allocated have send-buffer size |expected|. If
198 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
199 void CheckSendBufferSizesOfAllPorts(int expected) {
200 std::vector<cricket::PortInterface*>::iterator it;
201 for (it = ports_.begin(); it < ports_.end(); ++it) {
202 int send_buffer_size;
203 if (expected == -1) {
204 EXPECT_EQ(SOCKET_ERROR,
205 (*it)->GetOption(talk_base::Socket::OPT_SNDBUF,
206 &send_buffer_size));
207 } else {
208 EXPECT_EQ(0, (*it)->GetOption(talk_base::Socket::OPT_SNDBUF,
209 &send_buffer_size));
210 ASSERT_EQ(expected, send_buffer_size);
211 }
212 }
213 }
214
215 protected:
216 cricket::BasicPortAllocator& allocator() {
217 return *allocator_;
218 }
219
220 void OnPortReady(cricket::PortAllocatorSession* ses,
221 cricket::PortInterface* port) {
222 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
223 ports_.push_back(port);
224 }
225 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
226 const std::vector<cricket::Candidate>& candidates) {
227 for (size_t i = 0; i < candidates.size(); ++i) {
228 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
229 candidates_.push_back(candidates[i]);
230 }
231 }
232
233 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
234 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
235 cricket::RelayServerConfig server_config = allocator_->relays()[i];
236 cricket::PortList::const_iterator relay_port;
237 for (relay_port = server_config.ports.begin();
238 relay_port != server_config.ports.end(); ++relay_port) {
239 if (proto_addr.address == relay_port->address &&
240 proto_addr.proto == relay_port->proto)
241 return true;
242 }
243 }
244 return false;
245 }
246
247 talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
248 talk_base::scoped_ptr<talk_base::VirtualSocketServer> vss_;
249 talk_base::scoped_ptr<talk_base::FirewallSocketServer> fss_;
250 talk_base::SocketServerScope ss_scope_;
251 talk_base::NATSocketFactory nat_factory_;
252 talk_base::BasicPacketSocketFactory nat_socket_factory_;
253 cricket::TestStunServer stun_server_;
254 cricket::TestRelayServer relay_server_;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000255 cricket::TestTurnServer turn_server_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 talk_base::FakeNetworkManager network_manager_;
257 talk_base::scoped_ptr<cricket::BasicPortAllocator> allocator_;
258 talk_base::scoped_ptr<cricket::PortAllocatorSession> session_;
259 std::vector<cricket::PortInterface*> ports_;
260 std::vector<cricket::Candidate> candidates_;
261 bool candidate_allocation_done_;
262};
263
264// Tests that we can init the port allocator and create a session.
265TEST_F(PortAllocatorTest, TestBasic) {
266 EXPECT_EQ(&network_manager_, allocator().network_manager());
267 EXPECT_EQ(kStunAddr, allocator().stun_address());
268 ASSERT_EQ(1u, allocator().relays().size());
269 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
270 // Empty relay credentials are used for GTURN.
271 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
272 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
273 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
274 kRelayUdpIntAddr, cricket::PROTO_UDP)));
275 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
276 kRelayTcpIntAddr, cricket::PROTO_TCP)));
277 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
278 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
279 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
280}
281
282// Tests that we can get all the desired addresses successfully.
283TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
284 AddInterface(kClientAddr);
285 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
286 session_->StartGettingPorts();
287 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
288 EXPECT_EQ(4U, ports_.size());
289 EXPECT_PRED5(CheckCandidate, candidates_[0],
290 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
291 EXPECT_PRED5(CheckCandidate, candidates_[1],
292 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
293 EXPECT_PRED5(CheckCandidate, candidates_[2],
294 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
295 EXPECT_PRED5(CheckCandidate, candidates_[3],
296 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
297 EXPECT_PRED5(CheckCandidate, candidates_[4],
298 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
299 EXPECT_PRED5(CheckCandidate, candidates_[5],
300 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
301 EXPECT_PRED5(CheckCandidate, candidates_[6],
302 cricket::ICE_CANDIDATE_COMPONENT_RTP,
303 "relay", "ssltcp", kRelaySslTcpIntAddr);
304 EXPECT_TRUE(candidate_allocation_done_);
305}
306
307// Verify candidates with default step delay of 1sec.
308TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
309 AddInterface(kClientAddr);
310 allocator_->set_step_delay(cricket::kDefaultStepDelay);
311 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
312 session_->StartGettingPorts();
313 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
314 EXPECT_EQ(2U, ports_.size());
315 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
316 EXPECT_EQ(3U, ports_.size());
317 EXPECT_PRED5(CheckCandidate, candidates_[2],
318 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
319 EXPECT_PRED5(CheckCandidate, candidates_[3],
320 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
321 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
322 EXPECT_PRED5(CheckCandidate, candidates_[4],
323 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
324 EXPECT_PRED5(CheckCandidate, candidates_[5],
325 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
326 EXPECT_EQ(4U, ports_.size());
327 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
328 EXPECT_PRED5(CheckCandidate, candidates_[6],
329 cricket::ICE_CANDIDATE_COMPONENT_RTP,
330 "relay", "ssltcp", kRelaySslTcpIntAddr);
331 EXPECT_EQ(4U, ports_.size());
332 EXPECT_TRUE(candidate_allocation_done_);
333 // If we Stop gathering now, we shouldn't get a second "done" callback.
334 session_->StopGettingPorts();
335}
336
337TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
338 AddInterface(kClientAddr);
339 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
340 cricket::CN_VIDEO));
341 session_->StartGettingPorts();
342 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
343 EXPECT_TRUE(candidate_allocation_done_);
344 // If we Stop gathering now, we shouldn't get a second "done" callback.
345 session_->StopGettingPorts();
346
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000347 // All ports should have unset send-buffer sizes.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 CheckSendBufferSizesOfAllPorts(-1);
349}
350
351// Tests that we can get callback after StopGetAllPorts.
352TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
353 AddInterface(kClientAddr);
354 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
355 session_->StartGettingPorts();
356 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
357 EXPECT_EQ(2U, ports_.size());
358 session_->StopGettingPorts();
359 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
360}
361
362// Test that we restrict client ports appropriately when a port range is set.
363// We check the candidates for udp/stun/tcp ports, and the from address
364// for relay ports.
365TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
366 AddInterface(kClientAddr);
367 // Check that an invalid port range fails.
368 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
369 // Check that a null port range succeeds.
370 EXPECT_TRUE(SetPortRange(0, 0));
371 // Check that a valid port range succeeds.
372 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
373 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
374 session_->StartGettingPorts();
375 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
376 EXPECT_EQ(4U, ports_.size());
377 // Check the port number for the UDP port object.
378 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
379 // Check the port number for the STUN port object.
380 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
381 // Check the port number used to connect to the relay server.
382 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
383 kMinPort, kMaxPort);
384 // Check the port number for the TCP port object.
385 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
386 EXPECT_TRUE(candidate_allocation_done_);
387}
388
389// Test that we don't crash or malfunction if we have no network adapters.
390TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
391 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
392 session_->StartGettingPorts();
393 talk_base::Thread::Current()->ProcessMessages(100);
394 // Without network adapter, we should not get any candidate.
395 EXPECT_EQ(0U, candidates_.size());
396 EXPECT_TRUE(candidate_allocation_done_);
397}
398
399// Test that we can get OnCandidatesAllocationDone callback when all the ports
400// are disabled.
401TEST_F(PortAllocatorTest, TestDisableAllPorts) {
402 AddInterface(kClientAddr);
403 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
404 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
405 cricket::PORTALLOCATOR_DISABLE_STUN |
406 cricket::PORTALLOCATOR_DISABLE_RELAY |
407 cricket::PORTALLOCATOR_DISABLE_TCP);
408 session_->StartGettingPorts();
409 talk_base::Thread::Current()->ProcessMessages(100);
410 EXPECT_EQ(0U, candidates_.size());
411 EXPECT_TRUE(candidate_allocation_done_);
412}
413
414// Test that we don't crash or malfunction if we can't create UDP sockets.
415TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
416 AddInterface(kClientAddr);
417 fss_->set_udp_sockets_enabled(false);
418 EXPECT_TRUE(CreateSession(1));
419 session_->StartGettingPorts();
420 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
421 EXPECT_EQ(2U, ports_.size());
422 EXPECT_PRED5(CheckCandidate, candidates_[0],
423 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
424 EXPECT_PRED5(CheckCandidate, candidates_[1],
425 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
426 EXPECT_PRED5(CheckCandidate, candidates_[2],
427 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
428 EXPECT_PRED5(CheckCandidate, candidates_[3],
429 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
430 EXPECT_PRED5(CheckCandidate, candidates_[4],
431 cricket::ICE_CANDIDATE_COMPONENT_RTP,
432 "relay", "ssltcp", kRelaySslTcpIntAddr);
433 EXPECT_TRUE(candidate_allocation_done_);
434}
435
436// Test that we don't crash or malfunction if we can't create UDP sockets or
437// listen on TCP sockets. We still give out a local TCP address, since
438// apparently this is needed for the remote side to accept our connection.
439TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
440 AddInterface(kClientAddr);
441 fss_->set_udp_sockets_enabled(false);
442 fss_->set_tcp_listen_enabled(false);
443 EXPECT_TRUE(CreateSession(1));
444 session_->StartGettingPorts();
445 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
446 EXPECT_EQ(2U, ports_.size());
447 EXPECT_PRED5(CheckCandidate, candidates_[0],
448 1, "relay", "udp", kRelayUdpIntAddr);
449 EXPECT_PRED5(CheckCandidate, candidates_[1],
450 1, "relay", "udp", kRelayUdpExtAddr);
451 EXPECT_PRED5(CheckCandidate, candidates_[2],
452 1, "relay", "tcp", kRelayTcpIntAddr);
453 EXPECT_PRED5(CheckCandidate, candidates_[3],
454 1, "local", "tcp", kClientAddr);
455 EXPECT_PRED5(CheckCandidate, candidates_[4],
456 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
457 EXPECT_TRUE(candidate_allocation_done_);
458}
459
460// Test that we don't crash or malfunction if we can't create any sockets.
461// TODO: Find a way to exit early here.
462TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
463 AddInterface(kClientAddr);
464 fss_->set_tcp_sockets_enabled(false);
465 fss_->set_udp_sockets_enabled(false);
466 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
467 session_->StartGettingPorts();
468 WAIT(candidates_.size() > 0, 2000);
469 // TODO - Check candidate_allocation_done signal.
470 // In case of Relay, ports creation will succeed but sockets will fail.
471 // There is no error reporting from RelayEntry to handle this failure.
472}
473
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474// Testing STUN timeout.
475TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
476 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
477 AddInterface(kClientAddr);
478 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
479 session_->StartGettingPorts();
480 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
481 EXPECT_EQ(2U, ports_.size());
482 EXPECT_PRED5(CheckCandidate, candidates_[0],
483 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
484 EXPECT_PRED5(CheckCandidate, candidates_[1],
485 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
486 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
487 // will be tried after 3 seconds.
488 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
489 EXPECT_EQ(3U, ports_.size());
490 EXPECT_PRED5(CheckCandidate, candidates_[2],
491 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
492 EXPECT_PRED5(CheckCandidate, candidates_[3],
493 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
494 EXPECT_PRED5(CheckCandidate, candidates_[4],
495 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
496 kRelaySslTcpIntAddr);
497 EXPECT_PRED5(CheckCandidate, candidates_[5],
498 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
499 // Stun Timeout is 9sec.
500 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
501}
502
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000503TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
504 AddInterface(kClientAddr);
505 AddInterface(kClientAddr2);
506 // Allocating only host UDP ports. This is done purely for testing
507 // convenience.
508 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
509 cricket::PORTALLOCATOR_DISABLE_STUN |
510 cricket::PORTALLOCATOR_DISABLE_RELAY);
511 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
512 session_->StartGettingPorts();
513 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
514 ASSERT_EQ(2U, candidates_.size());
515 EXPECT_EQ(2U, ports_.size());
516 // Candidates priorities should be different.
517 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
518}
519
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520// Test to verify ICE restart process.
521TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
522 AddInterface(kClientAddr);
523 EXPECT_TRUE(CreateSession(1));
524 session_->StartGettingPorts();
525 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
526 EXPECT_EQ(4U, ports_.size());
527 EXPECT_TRUE(candidate_allocation_done_);
528 // TODO - Extend this to verify ICE restart.
529}
530
531TEST_F(PortAllocatorTest, TestBasicMuxFeatures) {
532 AddInterface(kClientAddr);
533 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
534 // Session ID - session1.
535 talk_base::scoped_ptr<cricket::PortAllocatorSession> session1(
536 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
537 talk_base::scoped_ptr<cricket::PortAllocatorSession> session2(
538 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTCP));
539 session1->StartGettingPorts();
540 session2->StartGettingPorts();
541 // Each session should receive two proxy ports of local and stun.
542 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
543 EXPECT_EQ(8U, ports_.size());
544
545 talk_base::scoped_ptr<cricket::PortAllocatorSession> session3(
546 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
547 session3->StartGettingPorts();
548 // Already allocated candidates and ports will be sent to the newly
549 // allocated proxy session.
550 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
551 EXPECT_EQ(12U, ports_.size());
552}
553
554// This test verifies by changing ice_ufrag and/or ice_pwd
555// will result in different set of candidates when BUNDLE is enabled.
556// If BUNDLE is disabled, CreateSession will always allocate new
557// set of candidates.
558TEST_F(PortAllocatorTest, TestBundleIceRestart) {
559 AddInterface(kClientAddr);
560 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
561 // Session ID - session1.
562 talk_base::scoped_ptr<cricket::PortAllocatorSession> session1(
563 CreateSession("session1", kContentName,
564 cricket::ICE_CANDIDATE_COMPONENT_RTP,
565 kIceUfrag0, kIcePwd0));
566 session1->StartGettingPorts();
567 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
568 EXPECT_EQ(4U, ports_.size());
569
570 // Allocate a different session with sid |session1| and different ice_ufrag.
571 talk_base::scoped_ptr<cricket::PortAllocatorSession> session2(
572 CreateSession("session1", kContentName,
573 cricket::ICE_CANDIDATE_COMPONENT_RTP,
574 "TestIceUfrag", kIcePwd0));
575 session2->StartGettingPorts();
576 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
577 EXPECT_EQ(8U, ports_.size());
578 // Verifying the candidate address different from previously allocated
579 // address.
580 // Skipping verification of component id and candidate type.
581 EXPECT_NE(candidates_[0].address(), candidates_[7].address());
582 EXPECT_NE(candidates_[1].address(), candidates_[8].address());
583
584 // Allocating a different session with sid |session1| and
585 // different ice_pwd.
586 talk_base::scoped_ptr<cricket::PortAllocatorSession> session3(
587 CreateSession("session1", kContentName,
588 cricket::ICE_CANDIDATE_COMPONENT_RTP,
589 kIceUfrag0, "TestIcePwd"));
590 session3->StartGettingPorts();
591 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
592 EXPECT_EQ(12U, ports_.size());
593 // Verifying the candidate address different from previously
594 // allocated address.
595 EXPECT_NE(candidates_[7].address(), candidates_[14].address());
596 EXPECT_NE(candidates_[8].address(), candidates_[15].address());
597
598 // Allocating a session with by changing both ice_ufrag and ice_pwd.
599 talk_base::scoped_ptr<cricket::PortAllocatorSession> session4(
600 CreateSession("session1", kContentName,
601 cricket::ICE_CANDIDATE_COMPONENT_RTP,
602 "TestIceUfrag", "TestIcePwd"));
603 session4->StartGettingPorts();
604 ASSERT_EQ_WAIT(28U, candidates_.size(), kDefaultAllocationTimeout);
605 EXPECT_EQ(16U, ports_.size());
606 // Verifying the candidate address different from previously
607 // allocated address.
608 EXPECT_NE(candidates_[14].address(), candidates_[21].address());
609 EXPECT_NE(candidates_[15].address(), candidates_[22].address());
610}
611
612// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
613// ufrag and pwd for the collected candidates.
614TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
615 allocator().set_flags(allocator().flags() |
616 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
617 AddInterface(kClientAddr);
618 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
619 session_->StartGettingPorts();
620 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
621 EXPECT_PRED5(CheckCandidate, candidates_[0],
622 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
623 EXPECT_PRED5(CheckCandidate, candidates_[1],
624 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
625 EXPECT_PRED5(CheckCandidate, candidates_[5],
626 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
627 EXPECT_EQ(4U, ports_.size());
628 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
629 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
630 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
631 EXPECT_EQ(kIcePwd0, candidates_[0].password());
632 EXPECT_EQ(kIcePwd0, candidates_[1].password());
633 EXPECT_TRUE(candidate_allocation_done_);
634}
635
636// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
637// different ufrag and pwd for the collected candidates.
638TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
639 allocator().set_flags(allocator().flags() &
640 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
641 AddInterface(kClientAddr);
642 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
643 session_->StartGettingPorts();
644 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
645 EXPECT_PRED5(CheckCandidate, candidates_[0],
646 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
647 EXPECT_PRED5(CheckCandidate, candidates_[1],
648 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
649 EXPECT_EQ(4U, ports_.size());
650 // Port should generate random ufrag and pwd.
651 EXPECT_NE(kIceUfrag0, candidates_[0].username());
652 EXPECT_NE(kIceUfrag0, candidates_[1].username());
653 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
654 EXPECT_NE(kIcePwd0, candidates_[0].password());
655 EXPECT_NE(kIcePwd0, candidates_[1].password());
656 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
657 EXPECT_TRUE(candidate_allocation_done_);
658}
659
660// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
661// is allocated for udp and stun. Also verify there is only one candidate
662// (local) if stun candidate is same as local candidate, which will be the case
663// in a public network like the below test.
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000664TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 AddInterface(kClientAddr);
666 allocator_->set_flags(allocator().flags() |
667 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
668 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
669 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
670 session_->StartGettingPorts();
671 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
672 EXPECT_EQ(3U, ports_.size());
673 EXPECT_PRED5(CheckCandidate, candidates_[0],
674 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
675 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
676}
677
678// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
679// is allocated for udp and stun. In this test we should expect both stun and
680// local candidates as client behind a nat.
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000681TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 AddInterface(kClientAddr);
683 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
684 CreateNatServer(kNatAddr, talk_base::NAT_OPEN_CONE));
685 allocator_.reset(new cricket::BasicPortAllocator(
686 &network_manager_, &nat_socket_factory_, kStunAddr));
687 allocator_->set_step_delay(cricket::kMinimumStepDelay);
688 allocator_->set_flags(allocator().flags() |
689 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
690 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
691 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
692 session_->StartGettingPorts();
693 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
694 ASSERT_EQ(2U, ports_.size());
695 EXPECT_PRED5(CheckCandidate, candidates_[0],
696 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
697 EXPECT_PRED5(CheckCandidate, candidates_[1],
698 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
699 talk_base::SocketAddress(kNatAddr.ipaddr(), 0));
700 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
701 EXPECT_EQ(3U, candidates_.size());
702}
703
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000704// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
705// is allocated for udp/stun/turn. In this test we should expect all local,
706// stun and turn candidates.
707TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
708 AddInterface(kClientAddr);
709 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
710 CreateNatServer(kNatAddr, talk_base::NAT_OPEN_CONE));
711 allocator_.reset(new cricket::BasicPortAllocator(
712 &network_manager_, &nat_socket_factory_, kStunAddr));
713 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
714 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
715 relay_server.credentials = credentials;
716 relay_server.ports.push_back(cricket::ProtocolAddress(
717 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
718 allocator_->AddRelay(relay_server);
719
720 allocator_->set_step_delay(cricket::kMinimumStepDelay);
721 allocator_->set_flags(allocator().flags() |
722 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
723 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
724 cricket::PORTALLOCATOR_DISABLE_TCP);
725 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
726 session_->StartGettingPorts();
727 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
728 ASSERT_EQ(2U, ports_.size());
729 EXPECT_PRED5(CheckCandidate, candidates_[0],
730 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
731 EXPECT_PRED5(CheckCandidate, candidates_[1],
732 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
733 talk_base::SocketAddress(kNatAddr.ipaddr(), 0));
734 EXPECT_PRED5(CheckCandidate, candidates_[2],
735 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
736 talk_base::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
737 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
738 EXPECT_EQ(3U, candidates_.size());
739 // Local port will be created first and then TURN port.
740 // Checking TURN port has two candidates, STUN + TURN.
741 EXPECT_EQ(1U, ports_[0]->Candidates().size());
742 EXPECT_EQ(2U, ports_[1]->Candidates().size());
743}
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
746// and fail to generate STUN candidate, local UDP candidate is generated
747// properly.
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000748TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 allocator().set_flags(allocator().flags() |
750 cricket::PORTALLOCATOR_DISABLE_RELAY |
751 cricket::PORTALLOCATOR_DISABLE_TCP |
752 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
753 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
754 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
755 AddInterface(kClientAddr);
756 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
757 session_->StartGettingPorts();
758 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
759 EXPECT_EQ(1U, candidates_.size());
760 EXPECT_PRED5(CheckCandidate, candidates_[0],
761 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
762 // STUN timeout is 9sec. We need to wait to get candidate done signal.
763 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
764 EXPECT_EQ(1U, candidates_.size());
765}
766
wu@webrtc.org97077a32013-10-25 21:18:33 +0000767// This test verifies allocator can use IPv6 addresses along with IPv4.
768TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
769 allocator().set_flags(allocator().flags() |
770 cricket::PORTALLOCATOR_DISABLE_RELAY |
771 cricket::PORTALLOCATOR_ENABLE_IPV6 |
772 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
773 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
774 AddInterface(kClientIPv6Addr);
775 AddInterface(kClientAddr);
776 allocator_->set_step_delay(cricket::kMinimumStepDelay);
777 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
778 session_->StartGettingPorts();
779 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
780 EXPECT_EQ(4U, candidates_.size());
781 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
782 EXPECT_PRED5(CheckCandidate, candidates_[0],
783 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
784 kClientIPv6Addr);
785 EXPECT_PRED5(CheckCandidate, candidates_[1],
786 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
787 kClientAddr);
788 EXPECT_PRED5(CheckCandidate, candidates_[2],
789 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
790 kClientIPv6Addr);
791 EXPECT_PRED5(CheckCandidate, candidates_[3],
792 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
793 kClientAddr);
794 EXPECT_EQ(4U, candidates_.size());
795}
796
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797// Test that the httpportallocator correctly maintains its lists of stun and
798// relay servers, by never allowing an empty list.
799TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
800 talk_base::FakeNetworkManager network_manager;
801 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
802 EXPECT_EQ(1U, alloc.relay_hosts().size());
803 EXPECT_EQ(1U, alloc.stun_hosts().size());
804
805 std::vector<std::string> relay_servers;
806 std::vector<talk_base::SocketAddress> stun_servers;
807
808 alloc.SetRelayHosts(relay_servers);
809 alloc.SetStunHosts(stun_servers);
810 EXPECT_EQ(1U, alloc.relay_hosts().size());
811 EXPECT_EQ(1U, alloc.stun_hosts().size());
812
813 relay_servers.push_back("1.unittest.corp.google.com");
814 relay_servers.push_back("2.unittest.corp.google.com");
815 stun_servers.push_back(
816 talk_base::SocketAddress("1.unittest.corp.google.com", 0));
817 stun_servers.push_back(
818 talk_base::SocketAddress("2.unittest.corp.google.com", 0));
819 alloc.SetRelayHosts(relay_servers);
820 alloc.SetStunHosts(stun_servers);
821 EXPECT_EQ(2U, alloc.relay_hosts().size());
822 EXPECT_EQ(2U, alloc.stun_hosts().size());
823}
824
825// Test that the HttpPortAllocator uses correct URL to create sessions.
826TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
827 talk_base::FakeNetworkManager network_manager;
828 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
829
830 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
831 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
832 talk_base::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
833 static_cast<cricket::HttpPortAllocatorSession*>(
834 alloc.CreateSessionInternal(
835 "test content", 0, kIceUfrag0, kIcePwd0)));
836 std::string url = session->GetSessionRequestUrl();
837 LOG(LS_INFO) << "url: " << url;
838 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
839
840 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
841 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
842 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
843 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
844 url = session->GetSessionRequestUrl();
845 LOG(LS_INFO) << "url: " << url;
846 std::vector<std::string> parts;
847 talk_base::split(url, '?', &parts);
848 ASSERT_EQ(2U, parts.size());
849
850 std::vector<std::string> args_parts;
851 talk_base::split(parts[1], '&', &args_parts);
852
853 std::map<std::string, std::string> args;
854 for (std::vector<std::string>::iterator it = args_parts.begin();
855 it != args_parts.end(); ++it) {
856 std::vector<std::string> parts;
857 talk_base::split(*it, '=', &parts);
858 ASSERT_EQ(2U, parts.size());
859 args[talk_base::s_url_decode(parts[0])] = talk_base::s_url_decode(parts[1]);
860 }
861
862 EXPECT_EQ(kIceUfrag0, args["username"]);
863 EXPECT_EQ(kIcePwd0, args["password"]);
864}