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