blob: 21131036c2538602657c4259d878e8314b9564fc [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"
38#include "talk/base/thread.h"
39#include "talk/base/virtualsocketserver.h"
40#include "talk/p2p/base/basicpacketsocketfactory.h"
41#include "talk/p2p/base/constants.h"
42#include "talk/p2p/base/p2ptransportchannel.h"
43#include "talk/p2p/base/portallocatorsessionproxy.h"
44#include "talk/p2p/base/testrelayserver.h"
45#include "talk/p2p/base/teststunserver.h"
46#include "talk/p2p/client/basicportallocator.h"
47#include "talk/p2p/client/httpportallocator.h"
48
49using talk_base::SocketAddress;
50using talk_base::Thread;
51
52static const SocketAddress kClientAddr("11.11.11.11", 0);
53static const SocketAddress kNatAddr("77.77.77.77", talk_base::NAT_SERVER_PORT);
54static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
55static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
56static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
57static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
58static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
59static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
60static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
61static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
62
63// Minimum and maximum port for port range tests.
64static const int kMinPort = 10000;
65static const int kMaxPort = 10099;
66
67// Based on ICE_UFRAG_LENGTH
68static const char kIceUfrag0[] = "TESTICEUFRAG0000";
69// Based on ICE_PWD_LENGTH
70static const char kIcePwd0[] = "TESTICEPWD00000000000000";
71
72static const char kContentName[] = "test content";
73
74static const int kDefaultAllocationTimeout = 1000;
75
76namespace cricket {
77
78// Helper for dumping candidates
79std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
80 os << c.ToString();
81 return os;
82}
83
84} // namespace cricket
85
86class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
87 public:
88 static void SetUpTestCase() {
89 // Ensure the RNG is inited.
90 talk_base::InitRandom(NULL, 0);
91 }
92 PortAllocatorTest()
93 : pss_(new talk_base::PhysicalSocketServer),
94 vss_(new talk_base::VirtualSocketServer(pss_.get())),
95 fss_(new talk_base::FirewallSocketServer(vss_.get())),
96 ss_scope_(fss_.get()),
97 nat_factory_(vss_.get(), kNatAddr),
98 nat_socket_factory_(&nat_factory_),
99 stun_server_(Thread::Current(), kStunAddr),
100 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
101 kRelayTcpIntAddr, kRelayTcpExtAddr,
102 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
103 allocator_(new cricket::BasicPortAllocator(
104 &network_manager_, kStunAddr,
105 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr)),
106 candidate_allocation_done_(false) {
107 allocator_->set_step_delay(cricket::kMinimumStepDelay);
108 }
109
110 void AddInterface(const SocketAddress& addr) {
111 network_manager_.AddInterface(addr);
112 }
113 bool SetPortRange(int min_port, int max_port) {
114 return allocator_->SetPortRange(min_port, max_port);
115 }
116 talk_base::NATServer* CreateNatServer(const SocketAddress& addr,
117 talk_base::NATType type) {
118 return new talk_base::NATServer(type, vss_.get(), addr, vss_.get(), addr);
119 }
120
121 bool CreateSession(int component) {
122 session_.reset(CreateSession("session", component));
123 if (!session_)
124 return false;
125 return true;
126 }
127
128 bool CreateSession(int component, const std::string& content_name) {
129 session_.reset(CreateSession("session", content_name, component));
130 if (!session_)
131 return false;
132 return true;
133 }
134
135 cricket::PortAllocatorSession* CreateSession(
136 const std::string& sid, int component) {
137 return CreateSession(sid, kContentName, component);
138 }
139
140 cricket::PortAllocatorSession* CreateSession(
141 const std::string& sid, const std::string& content_name, int component) {
142 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
143 }
144
145 cricket::PortAllocatorSession* CreateSession(
146 const std::string& sid, const std::string& content_name, int component,
147 const std::string& ice_ufrag, const std::string& ice_pwd) {
148 cricket::PortAllocatorSession* session =
149 allocator_->CreateSession(
150 sid, content_name, component, ice_ufrag, ice_pwd);
151 session->SignalPortReady.connect(this,
152 &PortAllocatorTest::OnPortReady);
153 session->SignalCandidatesReady.connect(this,
154 &PortAllocatorTest::OnCandidatesReady);
155 session->SignalCandidatesAllocationDone.connect(this,
156 &PortAllocatorTest::OnCandidatesAllocationDone);
157 return session;
158 }
159
160 static bool CheckCandidate(const cricket::Candidate& c,
161 int component, const std::string& type,
162 const std::string& proto,
163 const SocketAddress& addr) {
164 return (c.component() == component && c.type() == type &&
165 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
166 ((addr.port() == 0 && (c.address().port() != 0)) ||
167 (c.address().port() == addr.port())));
168 }
169 static bool CheckPort(const talk_base::SocketAddress& addr,
170 int min_port, int max_port) {
171 return (addr.port() >= min_port && addr.port() <= max_port);
172 }
173
174 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
175 // We should only get this callback once, except in the mux test where
176 // we have multiple port allocation sessions.
177 if (session == session_.get()) {
178 ASSERT_FALSE(candidate_allocation_done_);
179 candidate_allocation_done_ = true;
180 }
181 }
182
183 // Check if all ports allocated have send-buffer size |expected|. If
184 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
185 void CheckSendBufferSizesOfAllPorts(int expected) {
186 std::vector<cricket::PortInterface*>::iterator it;
187 for (it = ports_.begin(); it < ports_.end(); ++it) {
188 int send_buffer_size;
189 if (expected == -1) {
190 EXPECT_EQ(SOCKET_ERROR,
191 (*it)->GetOption(talk_base::Socket::OPT_SNDBUF,
192 &send_buffer_size));
193 } else {
194 EXPECT_EQ(0, (*it)->GetOption(talk_base::Socket::OPT_SNDBUF,
195 &send_buffer_size));
196 ASSERT_EQ(expected, send_buffer_size);
197 }
198 }
199 }
200
201 protected:
202 cricket::BasicPortAllocator& allocator() {
203 return *allocator_;
204 }
205
206 void OnPortReady(cricket::PortAllocatorSession* ses,
207 cricket::PortInterface* port) {
208 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
209 ports_.push_back(port);
210 }
211 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
212 const std::vector<cricket::Candidate>& candidates) {
213 for (size_t i = 0; i < candidates.size(); ++i) {
214 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
215 candidates_.push_back(candidates[i]);
216 }
217 }
218
219 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
220 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
221 cricket::RelayServerConfig server_config = allocator_->relays()[i];
222 cricket::PortList::const_iterator relay_port;
223 for (relay_port = server_config.ports.begin();
224 relay_port != server_config.ports.end(); ++relay_port) {
225 if (proto_addr.address == relay_port->address &&
226 proto_addr.proto == relay_port->proto)
227 return true;
228 }
229 }
230 return false;
231 }
232
233 talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
234 talk_base::scoped_ptr<talk_base::VirtualSocketServer> vss_;
235 talk_base::scoped_ptr<talk_base::FirewallSocketServer> fss_;
236 talk_base::SocketServerScope ss_scope_;
237 talk_base::NATSocketFactory nat_factory_;
238 talk_base::BasicPacketSocketFactory nat_socket_factory_;
239 cricket::TestStunServer stun_server_;
240 cricket::TestRelayServer relay_server_;
241 talk_base::FakeNetworkManager network_manager_;
242 talk_base::scoped_ptr<cricket::BasicPortAllocator> allocator_;
243 talk_base::scoped_ptr<cricket::PortAllocatorSession> session_;
244 std::vector<cricket::PortInterface*> ports_;
245 std::vector<cricket::Candidate> candidates_;
246 bool candidate_allocation_done_;
247};
248
249// Tests that we can init the port allocator and create a session.
250TEST_F(PortAllocatorTest, TestBasic) {
251 EXPECT_EQ(&network_manager_, allocator().network_manager());
252 EXPECT_EQ(kStunAddr, allocator().stun_address());
253 ASSERT_EQ(1u, allocator().relays().size());
254 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
255 // Empty relay credentials are used for GTURN.
256 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
257 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
258 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
259 kRelayUdpIntAddr, cricket::PROTO_UDP)));
260 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
261 kRelayTcpIntAddr, cricket::PROTO_TCP)));
262 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
263 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
264 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
265}
266
267// Tests that we can get all the desired addresses successfully.
268TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
269 AddInterface(kClientAddr);
270 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
271 session_->StartGettingPorts();
272 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
273 EXPECT_EQ(4U, ports_.size());
274 EXPECT_PRED5(CheckCandidate, candidates_[0],
275 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
276 EXPECT_PRED5(CheckCandidate, candidates_[1],
277 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
278 EXPECT_PRED5(CheckCandidate, candidates_[2],
279 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
280 EXPECT_PRED5(CheckCandidate, candidates_[3],
281 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
282 EXPECT_PRED5(CheckCandidate, candidates_[4],
283 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
284 EXPECT_PRED5(CheckCandidate, candidates_[5],
285 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
286 EXPECT_PRED5(CheckCandidate, candidates_[6],
287 cricket::ICE_CANDIDATE_COMPONENT_RTP,
288 "relay", "ssltcp", kRelaySslTcpIntAddr);
289 EXPECT_TRUE(candidate_allocation_done_);
290}
291
292// Verify candidates with default step delay of 1sec.
293TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
294 AddInterface(kClientAddr);
295 allocator_->set_step_delay(cricket::kDefaultStepDelay);
296 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
297 session_->StartGettingPorts();
298 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
299 EXPECT_EQ(2U, ports_.size());
300 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
301 EXPECT_EQ(3U, ports_.size());
302 EXPECT_PRED5(CheckCandidate, candidates_[2],
303 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
304 EXPECT_PRED5(CheckCandidate, candidates_[3],
305 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
306 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
307 EXPECT_PRED5(CheckCandidate, candidates_[4],
308 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
309 EXPECT_PRED5(CheckCandidate, candidates_[5],
310 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
311 EXPECT_EQ(4U, ports_.size());
312 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
313 EXPECT_PRED5(CheckCandidate, candidates_[6],
314 cricket::ICE_CANDIDATE_COMPONENT_RTP,
315 "relay", "ssltcp", kRelaySslTcpIntAddr);
316 EXPECT_EQ(4U, ports_.size());
317 EXPECT_TRUE(candidate_allocation_done_);
318 // If we Stop gathering now, we shouldn't get a second "done" callback.
319 session_->StopGettingPorts();
320}
321
322TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
323 AddInterface(kClientAddr);
324 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
325 cricket::CN_VIDEO));
326 session_->StartGettingPorts();
327 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
328 EXPECT_TRUE(candidate_allocation_done_);
329 // If we Stop gathering now, we shouldn't get a second "done" callback.
330 session_->StopGettingPorts();
331
332 // All ports should have normal send-buffer sizes (64KB).
333 CheckSendBufferSizesOfAllPorts(64 * 1024);
334}
335
336TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithLargeSendBuffers) {
337 AddInterface(kClientAddr);
338 allocator_->set_flags(allocator_->flags() |
339 cricket::PORTALLOCATOR_USE_LARGE_SOCKET_SEND_BUFFERS);
340 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
341 cricket::CN_VIDEO));
342 session_->StartGettingPorts();
343 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
344 EXPECT_TRUE(candidate_allocation_done_);
345 // If we Stop gathering now, we shouldn't get a second "done" callback.
346 session_->StopGettingPorts();
347
348 // All ports should have large send-buffer sizes (128KB).
349 CheckSendBufferSizesOfAllPorts(128 * 1024);
350}
351
352TEST_F(PortAllocatorTest, TestSetupVideoRtcpPortsAndCheckSendBuffers) {
353 AddInterface(kClientAddr);
354 allocator_->set_flags(allocator_->flags() |
355 cricket::PORTALLOCATOR_USE_LARGE_SOCKET_SEND_BUFFERS);
356 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTCP,
357 cricket::CN_DATA));
358 session_->StartGettingPorts();
359 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
360 EXPECT_TRUE(candidate_allocation_done_);
361 // If we Stop gathering now, we shouldn't get a second "done" callback.
362 session_->StopGettingPorts();
363
364 // No ports should have send-buffer size set.
365 CheckSendBufferSizesOfAllPorts(-1);
366}
367
368
369TEST_F(PortAllocatorTest, TestSetupNonVideoPortsAndCheckSendBuffers) {
370 AddInterface(kClientAddr);
371 allocator_->set_flags(allocator_->flags() |
372 cricket::PORTALLOCATOR_USE_LARGE_SOCKET_SEND_BUFFERS);
373 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
374 cricket::CN_DATA));
375 session_->StartGettingPorts();
376 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
377 EXPECT_TRUE(candidate_allocation_done_);
378 // If we Stop gathering now, we shouldn't get a second "done" callback.
379 session_->StopGettingPorts();
380
381 // No ports should have send-buffer size set.
382 CheckSendBufferSizesOfAllPorts(-1);
383}
384
385// Tests that we can get callback after StopGetAllPorts.
386TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
387 AddInterface(kClientAddr);
388 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
389 session_->StartGettingPorts();
390 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
391 EXPECT_EQ(2U, ports_.size());
392 session_->StopGettingPorts();
393 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
394}
395
396// Test that we restrict client ports appropriately when a port range is set.
397// We check the candidates for udp/stun/tcp ports, and the from address
398// for relay ports.
399TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
400 AddInterface(kClientAddr);
401 // Check that an invalid port range fails.
402 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
403 // Check that a null port range succeeds.
404 EXPECT_TRUE(SetPortRange(0, 0));
405 // Check that a valid port range succeeds.
406 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
407 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
408 session_->StartGettingPorts();
409 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
410 EXPECT_EQ(4U, ports_.size());
411 // Check the port number for the UDP port object.
412 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
413 // Check the port number for the STUN port object.
414 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
415 // Check the port number used to connect to the relay server.
416 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
417 kMinPort, kMaxPort);
418 // Check the port number for the TCP port object.
419 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
420 EXPECT_TRUE(candidate_allocation_done_);
421}
422
423// Test that we don't crash or malfunction if we have no network adapters.
424TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
425 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
426 session_->StartGettingPorts();
427 talk_base::Thread::Current()->ProcessMessages(100);
428 // Without network adapter, we should not get any candidate.
429 EXPECT_EQ(0U, candidates_.size());
430 EXPECT_TRUE(candidate_allocation_done_);
431}
432
433// Test that we can get OnCandidatesAllocationDone callback when all the ports
434// are disabled.
435TEST_F(PortAllocatorTest, TestDisableAllPorts) {
436 AddInterface(kClientAddr);
437 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
438 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
439 cricket::PORTALLOCATOR_DISABLE_STUN |
440 cricket::PORTALLOCATOR_DISABLE_RELAY |
441 cricket::PORTALLOCATOR_DISABLE_TCP);
442 session_->StartGettingPorts();
443 talk_base::Thread::Current()->ProcessMessages(100);
444 EXPECT_EQ(0U, candidates_.size());
445 EXPECT_TRUE(candidate_allocation_done_);
446}
447
448// Test that we don't crash or malfunction if we can't create UDP sockets.
449TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
450 AddInterface(kClientAddr);
451 fss_->set_udp_sockets_enabled(false);
452 EXPECT_TRUE(CreateSession(1));
453 session_->StartGettingPorts();
454 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
455 EXPECT_EQ(2U, ports_.size());
456 EXPECT_PRED5(CheckCandidate, candidates_[0],
457 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
458 EXPECT_PRED5(CheckCandidate, candidates_[1],
459 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
460 EXPECT_PRED5(CheckCandidate, candidates_[2],
461 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
462 EXPECT_PRED5(CheckCandidate, candidates_[3],
463 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
464 EXPECT_PRED5(CheckCandidate, candidates_[4],
465 cricket::ICE_CANDIDATE_COMPONENT_RTP,
466 "relay", "ssltcp", kRelaySslTcpIntAddr);
467 EXPECT_TRUE(candidate_allocation_done_);
468}
469
470// Test that we don't crash or malfunction if we can't create UDP sockets or
471// listen on TCP sockets. We still give out a local TCP address, since
472// apparently this is needed for the remote side to accept our connection.
473TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
474 AddInterface(kClientAddr);
475 fss_->set_udp_sockets_enabled(false);
476 fss_->set_tcp_listen_enabled(false);
477 EXPECT_TRUE(CreateSession(1));
478 session_->StartGettingPorts();
479 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
480 EXPECT_EQ(2U, ports_.size());
481 EXPECT_PRED5(CheckCandidate, candidates_[0],
482 1, "relay", "udp", kRelayUdpIntAddr);
483 EXPECT_PRED5(CheckCandidate, candidates_[1],
484 1, "relay", "udp", kRelayUdpExtAddr);
485 EXPECT_PRED5(CheckCandidate, candidates_[2],
486 1, "relay", "tcp", kRelayTcpIntAddr);
487 EXPECT_PRED5(CheckCandidate, candidates_[3],
488 1, "local", "tcp", kClientAddr);
489 EXPECT_PRED5(CheckCandidate, candidates_[4],
490 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
491 EXPECT_TRUE(candidate_allocation_done_);
492}
493
494// Test that we don't crash or malfunction if we can't create any sockets.
495// TODO: Find a way to exit early here.
496TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
497 AddInterface(kClientAddr);
498 fss_->set_tcp_sockets_enabled(false);
499 fss_->set_udp_sockets_enabled(false);
500 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
501 session_->StartGettingPorts();
502 WAIT(candidates_.size() > 0, 2000);
503 // TODO - Check candidate_allocation_done signal.
504 // In case of Relay, ports creation will succeed but sockets will fail.
505 // There is no error reporting from RelayEntry to handle this failure.
506}
507
508TEST_F(PortAllocatorTest, TestTcpPortNoListenAllowed) {
509 AddInterface(kClientAddr);
510 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
511 cricket::PORTALLOCATOR_DISABLE_STUN |
512 cricket::PORTALLOCATOR_DISABLE_RELAY);
513 allocator().set_allow_tcp_listen(false);
514 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
515 session_->StartGettingPorts();
516 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
517 EXPECT_TRUE(candidates_.empty());
518}
519
520// Testing STUN timeout.
521TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
522 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
523 AddInterface(kClientAddr);
524 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
525 session_->StartGettingPorts();
526 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
527 EXPECT_EQ(2U, ports_.size());
528 EXPECT_PRED5(CheckCandidate, candidates_[0],
529 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
530 EXPECT_PRED5(CheckCandidate, candidates_[1],
531 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
532 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
533 // will be tried after 3 seconds.
534 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
535 EXPECT_EQ(3U, ports_.size());
536 EXPECT_PRED5(CheckCandidate, candidates_[2],
537 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
538 EXPECT_PRED5(CheckCandidate, candidates_[3],
539 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
540 EXPECT_PRED5(CheckCandidate, candidates_[4],
541 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
542 kRelaySslTcpIntAddr);
543 EXPECT_PRED5(CheckCandidate, candidates_[5],
544 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
545 // Stun Timeout is 9sec.
546 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
547}
548
549// Test to verify ICE restart process.
550TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
551 AddInterface(kClientAddr);
552 EXPECT_TRUE(CreateSession(1));
553 session_->StartGettingPorts();
554 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
555 EXPECT_EQ(4U, ports_.size());
556 EXPECT_TRUE(candidate_allocation_done_);
557 // TODO - Extend this to verify ICE restart.
558}
559
560TEST_F(PortAllocatorTest, TestBasicMuxFeatures) {
561 AddInterface(kClientAddr);
562 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
563 // Session ID - session1.
564 talk_base::scoped_ptr<cricket::PortAllocatorSession> session1(
565 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
566 talk_base::scoped_ptr<cricket::PortAllocatorSession> session2(
567 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTCP));
568 session1->StartGettingPorts();
569 session2->StartGettingPorts();
570 // Each session should receive two proxy ports of local and stun.
571 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
572 EXPECT_EQ(8U, ports_.size());
573
574 talk_base::scoped_ptr<cricket::PortAllocatorSession> session3(
575 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
576 session3->StartGettingPorts();
577 // Already allocated candidates and ports will be sent to the newly
578 // allocated proxy session.
579 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
580 EXPECT_EQ(12U, ports_.size());
581}
582
583// This test verifies by changing ice_ufrag and/or ice_pwd
584// will result in different set of candidates when BUNDLE is enabled.
585// If BUNDLE is disabled, CreateSession will always allocate new
586// set of candidates.
587TEST_F(PortAllocatorTest, TestBundleIceRestart) {
588 AddInterface(kClientAddr);
589 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
590 // Session ID - session1.
591 talk_base::scoped_ptr<cricket::PortAllocatorSession> session1(
592 CreateSession("session1", kContentName,
593 cricket::ICE_CANDIDATE_COMPONENT_RTP,
594 kIceUfrag0, kIcePwd0));
595 session1->StartGettingPorts();
596 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
597 EXPECT_EQ(4U, ports_.size());
598
599 // Allocate a different session with sid |session1| and different ice_ufrag.
600 talk_base::scoped_ptr<cricket::PortAllocatorSession> session2(
601 CreateSession("session1", kContentName,
602 cricket::ICE_CANDIDATE_COMPONENT_RTP,
603 "TestIceUfrag", kIcePwd0));
604 session2->StartGettingPorts();
605 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
606 EXPECT_EQ(8U, ports_.size());
607 // Verifying the candidate address different from previously allocated
608 // address.
609 // Skipping verification of component id and candidate type.
610 EXPECT_NE(candidates_[0].address(), candidates_[7].address());
611 EXPECT_NE(candidates_[1].address(), candidates_[8].address());
612
613 // Allocating a different session with sid |session1| and
614 // different ice_pwd.
615 talk_base::scoped_ptr<cricket::PortAllocatorSession> session3(
616 CreateSession("session1", kContentName,
617 cricket::ICE_CANDIDATE_COMPONENT_RTP,
618 kIceUfrag0, "TestIcePwd"));
619 session3->StartGettingPorts();
620 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
621 EXPECT_EQ(12U, ports_.size());
622 // Verifying the candidate address different from previously
623 // allocated address.
624 EXPECT_NE(candidates_[7].address(), candidates_[14].address());
625 EXPECT_NE(candidates_[8].address(), candidates_[15].address());
626
627 // Allocating a session with by changing both ice_ufrag and ice_pwd.
628 talk_base::scoped_ptr<cricket::PortAllocatorSession> session4(
629 CreateSession("session1", kContentName,
630 cricket::ICE_CANDIDATE_COMPONENT_RTP,
631 "TestIceUfrag", "TestIcePwd"));
632 session4->StartGettingPorts();
633 ASSERT_EQ_WAIT(28U, candidates_.size(), kDefaultAllocationTimeout);
634 EXPECT_EQ(16U, ports_.size());
635 // Verifying the candidate address different from previously
636 // allocated address.
637 EXPECT_NE(candidates_[14].address(), candidates_[21].address());
638 EXPECT_NE(candidates_[15].address(), candidates_[22].address());
639}
640
641// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
642// ufrag and pwd for the collected candidates.
643TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
644 allocator().set_flags(allocator().flags() |
645 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
646 AddInterface(kClientAddr);
647 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
648 session_->StartGettingPorts();
649 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
650 EXPECT_PRED5(CheckCandidate, candidates_[0],
651 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
652 EXPECT_PRED5(CheckCandidate, candidates_[1],
653 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
654 EXPECT_PRED5(CheckCandidate, candidates_[5],
655 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
656 EXPECT_EQ(4U, ports_.size());
657 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
658 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
659 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
660 EXPECT_EQ(kIcePwd0, candidates_[0].password());
661 EXPECT_EQ(kIcePwd0, candidates_[1].password());
662 EXPECT_TRUE(candidate_allocation_done_);
663}
664
665// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
666// different ufrag and pwd for the collected candidates.
667TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
668 allocator().set_flags(allocator().flags() &
669 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
670 AddInterface(kClientAddr);
671 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
672 session_->StartGettingPorts();
673 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
674 EXPECT_PRED5(CheckCandidate, candidates_[0],
675 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
676 EXPECT_PRED5(CheckCandidate, candidates_[1],
677 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
678 EXPECT_EQ(4U, ports_.size());
679 // Port should generate random ufrag and pwd.
680 EXPECT_NE(kIceUfrag0, candidates_[0].username());
681 EXPECT_NE(kIceUfrag0, candidates_[1].username());
682 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
683 EXPECT_NE(kIcePwd0, candidates_[0].password());
684 EXPECT_NE(kIcePwd0, candidates_[1].password());
685 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
686 EXPECT_TRUE(candidate_allocation_done_);
687}
688
689// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
690// is allocated for udp and stun. Also verify there is only one candidate
691// (local) if stun candidate is same as local candidate, which will be the case
692// in a public network like the below test.
693TEST_F(PortAllocatorTest, TestEnableSharedSocketWithoutNat) {
694 AddInterface(kClientAddr);
695 allocator_->set_flags(allocator().flags() |
696 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
697 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
698 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
699 session_->StartGettingPorts();
700 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
701 EXPECT_EQ(3U, ports_.size());
702 EXPECT_PRED5(CheckCandidate, candidates_[0],
703 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
704 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
705}
706
707// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
708// is allocated for udp and stun. In this test we should expect both stun and
709// local candidates as client behind a nat.
710TEST_F(PortAllocatorTest, TestEnableSharedSocketWithNat) {
711 AddInterface(kClientAddr);
712 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
713 CreateNatServer(kNatAddr, talk_base::NAT_OPEN_CONE));
714 allocator_.reset(new cricket::BasicPortAllocator(
715 &network_manager_, &nat_socket_factory_, kStunAddr));
716 allocator_->set_step_delay(cricket::kMinimumStepDelay);
717 allocator_->set_flags(allocator().flags() |
718 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
719 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
720 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
721 session_->StartGettingPorts();
722 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
723 ASSERT_EQ(2U, ports_.size());
724 EXPECT_PRED5(CheckCandidate, candidates_[0],
725 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
726 EXPECT_PRED5(CheckCandidate, candidates_[1],
727 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
728 talk_base::SocketAddress(kNatAddr.ipaddr(), 0));
729 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
730 EXPECT_EQ(3U, candidates_.size());
731}
732
733// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
734// and fail to generate STUN candidate, local UDP candidate is generated
735// properly.
736TEST_F(PortAllocatorTest, TestEnableSharedSocketNoUdpAllowed) {
737 allocator().set_flags(allocator().flags() |
738 cricket::PORTALLOCATOR_DISABLE_RELAY |
739 cricket::PORTALLOCATOR_DISABLE_TCP |
740 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
741 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
742 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
743 AddInterface(kClientAddr);
744 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
745 session_->StartGettingPorts();
746 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
747 EXPECT_EQ(1U, candidates_.size());
748 EXPECT_PRED5(CheckCandidate, candidates_[0],
749 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
750 // STUN timeout is 9sec. We need to wait to get candidate done signal.
751 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
752 EXPECT_EQ(1U, candidates_.size());
753}
754
755// Test that the httpportallocator correctly maintains its lists of stun and
756// relay servers, by never allowing an empty list.
757TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
758 talk_base::FakeNetworkManager network_manager;
759 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
760 EXPECT_EQ(1U, alloc.relay_hosts().size());
761 EXPECT_EQ(1U, alloc.stun_hosts().size());
762
763 std::vector<std::string> relay_servers;
764 std::vector<talk_base::SocketAddress> stun_servers;
765
766 alloc.SetRelayHosts(relay_servers);
767 alloc.SetStunHosts(stun_servers);
768 EXPECT_EQ(1U, alloc.relay_hosts().size());
769 EXPECT_EQ(1U, alloc.stun_hosts().size());
770
771 relay_servers.push_back("1.unittest.corp.google.com");
772 relay_servers.push_back("2.unittest.corp.google.com");
773 stun_servers.push_back(
774 talk_base::SocketAddress("1.unittest.corp.google.com", 0));
775 stun_servers.push_back(
776 talk_base::SocketAddress("2.unittest.corp.google.com", 0));
777 alloc.SetRelayHosts(relay_servers);
778 alloc.SetStunHosts(stun_servers);
779 EXPECT_EQ(2U, alloc.relay_hosts().size());
780 EXPECT_EQ(2U, alloc.stun_hosts().size());
781}
782
783// Test that the HttpPortAllocator uses correct URL to create sessions.
784TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
785 talk_base::FakeNetworkManager network_manager;
786 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
787
788 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
789 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
790 talk_base::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
791 static_cast<cricket::HttpPortAllocatorSession*>(
792 alloc.CreateSessionInternal(
793 "test content", 0, kIceUfrag0, kIcePwd0)));
794 std::string url = session->GetSessionRequestUrl();
795 LOG(LS_INFO) << "url: " << url;
796 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
797
798 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
799 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
800 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
801 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
802 url = session->GetSessionRequestUrl();
803 LOG(LS_INFO) << "url: " << url;
804 std::vector<std::string> parts;
805 talk_base::split(url, '?', &parts);
806 ASSERT_EQ(2U, parts.size());
807
808 std::vector<std::string> args_parts;
809 talk_base::split(parts[1], '&', &args_parts);
810
811 std::map<std::string, std::string> args;
812 for (std::vector<std::string>::iterator it = args_parts.begin();
813 it != args_parts.end(); ++it) {
814 std::vector<std::string> parts;
815 talk_base::split(*it, '=', &parts);
816 ASSERT_EQ(2U, parts.size());
817 args[talk_base::s_url_decode(parts[0])] = talk_base::s_url_decode(parts[1]);
818 }
819
820 EXPECT_EQ(kIceUfrag0, args["username"]);
821 EXPECT_EQ(kIcePwd0, args["password"]);
822}