blob: 5dcc8f9881d7c44a0921ca7068445e6d6d391646 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/p2p/base/basicpacketsocketfactory.h"
12#include "webrtc/p2p/base/constants.h"
13#include "webrtc/p2p/base/p2ptransportchannel.h"
14#include "webrtc/p2p/base/portallocatorsessionproxy.h"
15#include "webrtc/p2p/base/testrelayserver.h"
16#include "webrtc/p2p/base/teststunserver.h"
17#include "webrtc/p2p/base/testturnserver.h"
18#include "webrtc/p2p/client/basicportallocator.h"
19#include "webrtc/p2p/client/httpportallocator.h"
20#include "webrtc/base/fakenetwork.h"
21#include "webrtc/base/firewallsocketserver.h"
22#include "webrtc/base/gunit.h"
23#include "webrtc/base/helpers.h"
24#include "webrtc/base/logging.h"
25#include "webrtc/base/natserver.h"
26#include "webrtc/base/natsocketfactory.h"
27#include "webrtc/base/network.h"
28#include "webrtc/base/physicalsocketserver.h"
29#include "webrtc/base/socketaddress.h"
30#include "webrtc/base/ssladapter.h"
31#include "webrtc/base/thread.h"
32#include "webrtc/base/virtualsocketserver.h"
33
34using cricket::ServerAddresses;
35using rtc::SocketAddress;
36using rtc::Thread;
37
38static const SocketAddress kClientAddr("11.11.11.11", 0);
39static const SocketAddress kPrivateAddr("192.168.1.11", 0);
40static const SocketAddress kClientIPv6Addr(
41 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
42static const SocketAddress kClientAddr2("22.22.22.22", 0);
43static const SocketAddress kNatAddr("77.77.77.77", rtc::NAT_SERVER_PORT);
44static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
45static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
46static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
47static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
48static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
49static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
50static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
51static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
52static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
53static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
54static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
55
56// Minimum and maximum port for port range tests.
57static const int kMinPort = 10000;
58static const int kMaxPort = 10099;
59
60// Based on ICE_UFRAG_LENGTH
61static const char kIceUfrag0[] = "TESTICEUFRAG0000";
62// Based on ICE_PWD_LENGTH
63static const char kIcePwd0[] = "TESTICEPWD00000000000000";
64
65static const char kContentName[] = "test content";
66
67static const int kDefaultAllocationTimeout = 1000;
68static const char kTurnUsername[] = "test";
69static const char kTurnPassword[] = "test";
70
71namespace cricket {
72
73// Helper for dumping candidates
74std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
75 os << c.ToString();
76 return os;
77}
78
79} // namespace cricket
80
81class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
82 public:
83 PortAllocatorTest()
84 : pss_(new rtc::PhysicalSocketServer),
85 vss_(new rtc::VirtualSocketServer(pss_.get())),
86 fss_(new rtc::FirewallSocketServer(vss_.get())),
87 ss_scope_(fss_.get()),
88 nat_factory_(vss_.get(), kNatAddr),
89 nat_socket_factory_(&nat_factory_),
90 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
91 kStunAddr)),
92 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
93 kRelayTcpIntAddr, kRelayTcpExtAddr,
94 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
95 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
96 candidate_allocation_done_(false) {
97 cricket::ServerAddresses stun_servers;
98 stun_servers.insert(kStunAddr);
99 // Passing the addresses of GTURN servers will enable GTURN in
100 // Basicportallocator.
101 allocator_.reset(new cricket::BasicPortAllocator(
102 &network_manager_,
103 stun_servers,
104 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
105 allocator_->set_step_delay(cricket::kMinimumStepDelay);
106 }
107
108 void AddInterface(const SocketAddress& addr) {
109 network_manager_.AddInterface(addr);
110 }
111 bool SetPortRange(int min_port, int max_port) {
112 return allocator_->SetPortRange(min_port, max_port);
113 }
114 void ResetWithNatServer(const rtc::SocketAddress& stun_server) {
115 nat_server_.reset(new rtc::NATServer(
116 rtc::NAT_OPEN_CONE, vss_.get(), kNatAddr, vss_.get(), kNatAddr));
117
118 ServerAddresses stun_servers;
119 stun_servers.insert(stun_server);
120 allocator_.reset(new cricket::BasicPortAllocator(
121 &network_manager_, &nat_socket_factory_, stun_servers));
122 allocator().set_step_delay(cricket::kMinimumStepDelay);
123 }
124
125 // Create a BasicPortAllocator without GTURN and add the TURN servers.
126 void ResetWithTurnServers(const rtc::SocketAddress& udp_turn,
127 const rtc::SocketAddress& tcp_turn) {
128 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
129 allocator().set_step_delay(cricket::kMinimumStepDelay);
130 AddTurnServers(udp_turn, tcp_turn);
131 }
132
133 void AddTurnServers(const rtc::SocketAddress& udp_turn,
134 const rtc::SocketAddress& tcp_turn) {
135 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
136 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
137 relay_server.credentials = credentials;
138
139 if (!udp_turn.IsNil()) {
140 relay_server.ports.push_back(cricket::ProtocolAddress(
141 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
142 }
143 if (!tcp_turn.IsNil()) {
144 relay_server.ports.push_back(cricket::ProtocolAddress(
145 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
146 }
147 allocator_->AddRelay(relay_server);
148 }
149
150 bool CreateSession(int component) {
151 session_.reset(CreateSession("session", component));
152 if (!session_)
153 return false;
154 return true;
155 }
156
157 bool CreateSession(int component, const std::string& content_name) {
158 session_.reset(CreateSession("session", content_name, component));
159 if (!session_)
160 return false;
161 return true;
162 }
163
164 cricket::PortAllocatorSession* CreateSession(
165 const std::string& sid, int component) {
166 return CreateSession(sid, kContentName, component);
167 }
168
169 cricket::PortAllocatorSession* CreateSession(
170 const std::string& sid, const std::string& content_name, int component) {
171 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
172 }
173
174 cricket::PortAllocatorSession* CreateSession(
175 const std::string& sid, const std::string& content_name, int component,
176 const std::string& ice_ufrag, const std::string& ice_pwd) {
177 cricket::PortAllocatorSession* session =
178 allocator_->CreateSession(
179 sid, content_name, component, ice_ufrag, ice_pwd);
180 session->SignalPortReady.connect(this,
181 &PortAllocatorTest::OnPortReady);
182 session->SignalCandidatesReady.connect(this,
183 &PortAllocatorTest::OnCandidatesReady);
184 session->SignalCandidatesAllocationDone.connect(this,
185 &PortAllocatorTest::OnCandidatesAllocationDone);
186 return session;
187 }
188
189 static bool CheckCandidate(const cricket::Candidate& c,
190 int component, const std::string& type,
191 const std::string& proto,
192 const SocketAddress& addr) {
193 return (c.component() == component && c.type() == type &&
194 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
195 ((addr.port() == 0 && (c.address().port() != 0)) ||
196 (c.address().port() == addr.port())));
197 }
198 static bool CheckPort(const rtc::SocketAddress& addr,
199 int min_port, int max_port) {
200 return (addr.port() >= min_port && addr.port() <= max_port);
201 }
202
203 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
204 // We should only get this callback once, except in the mux test where
205 // we have multiple port allocation sessions.
206 if (session == session_.get()) {
207 ASSERT_FALSE(candidate_allocation_done_);
208 candidate_allocation_done_ = true;
209 }
210 }
211
212 // Check if all ports allocated have send-buffer size |expected|. If
213 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
214 void CheckSendBufferSizesOfAllPorts(int expected) {
215 std::vector<cricket::PortInterface*>::iterator it;
216 for (it = ports_.begin(); it < ports_.end(); ++it) {
217 int send_buffer_size;
218 if (expected == -1) {
219 EXPECT_EQ(SOCKET_ERROR,
220 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
221 &send_buffer_size));
222 } else {
223 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
224 &send_buffer_size));
225 ASSERT_EQ(expected, send_buffer_size);
226 }
227 }
228 }
229
230 protected:
231 cricket::BasicPortAllocator& allocator() {
232 return *allocator_;
233 }
234
235 void OnPortReady(cricket::PortAllocatorSession* ses,
236 cricket::PortInterface* port) {
237 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
238 ports_.push_back(port);
239 }
240 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
241 const std::vector<cricket::Candidate>& candidates) {
242 for (size_t i = 0; i < candidates.size(); ++i) {
243 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
244 candidates_.push_back(candidates[i]);
245 }
246 }
247
248 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
249 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
250 cricket::RelayServerConfig server_config = allocator_->relays()[i];
251 cricket::PortList::const_iterator relay_port;
252 for (relay_port = server_config.ports.begin();
253 relay_port != server_config.ports.end(); ++relay_port) {
254 if (proto_addr.address == relay_port->address &&
255 proto_addr.proto == relay_port->proto)
256 return true;
257 }
258 }
259 return false;
260 }
261
262 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
263 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
264 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
265 rtc::SocketServerScope ss_scope_;
266 rtc::scoped_ptr<rtc::NATServer> nat_server_;
267 rtc::NATSocketFactory nat_factory_;
268 rtc::BasicPacketSocketFactory nat_socket_factory_;
269 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
270 cricket::TestRelayServer relay_server_;
271 cricket::TestTurnServer turn_server_;
272 rtc::FakeNetworkManager network_manager_;
273 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
274 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
275 std::vector<cricket::PortInterface*> ports_;
276 std::vector<cricket::Candidate> candidates_;
277 bool candidate_allocation_done_;
278};
279
280// Tests that we can init the port allocator and create a session.
281TEST_F(PortAllocatorTest, TestBasic) {
282 EXPECT_EQ(&network_manager_, allocator().network_manager());
283 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
284 ASSERT_EQ(1u, allocator().relays().size());
285 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
286 // Empty relay credentials are used for GTURN.
287 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
288 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
289 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
290 kRelayUdpIntAddr, cricket::PROTO_UDP)));
291 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
292 kRelayTcpIntAddr, cricket::PROTO_TCP)));
293 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
294 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
295 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
296}
297
298// Tests that we allocator session not trying to allocate ports for every 250ms.
299TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
300 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
301 session_->StartGettingPorts();
302 // Waiting for one second to make sure BasicPortAllocatorSession has not
303 // called OnAllocate multiple times. In old behavior it's called every 250ms.
304 // When there are no network interfaces, each execution of OnAllocate will
305 // result in SignalCandidatesAllocationDone signal.
306 rtc::Thread::Current()->ProcessMessages(1000);
307 EXPECT_TRUE(candidate_allocation_done_);
308 EXPECT_EQ(0U, candidates_.size());
309}
310
311// Tests that we can get all the desired addresses successfully.
312TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
313 AddInterface(kClientAddr);
314 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
315 session_->StartGettingPorts();
316 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
317 EXPECT_EQ(4U, ports_.size());
318 EXPECT_PRED5(CheckCandidate, candidates_[0],
319 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
320 EXPECT_PRED5(CheckCandidate, candidates_[1],
321 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
322 EXPECT_PRED5(CheckCandidate, candidates_[2],
323 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
324 EXPECT_PRED5(CheckCandidate, candidates_[3],
325 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
326 EXPECT_PRED5(CheckCandidate, candidates_[4],
327 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
328 EXPECT_PRED5(CheckCandidate, candidates_[5],
329 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
330 EXPECT_PRED5(CheckCandidate, candidates_[6],
331 cricket::ICE_CANDIDATE_COMPONENT_RTP,
332 "relay", "ssltcp", kRelaySslTcpIntAddr);
333 EXPECT_TRUE(candidate_allocation_done_);
334}
335
336// Verify candidates with default step delay of 1sec.
337TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
338 AddInterface(kClientAddr);
339 allocator_->set_step_delay(cricket::kDefaultStepDelay);
340 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
341 session_->StartGettingPorts();
342 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
343 EXPECT_EQ(2U, ports_.size());
344 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
345 EXPECT_EQ(3U, ports_.size());
346 EXPECT_PRED5(CheckCandidate, candidates_[2],
347 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
348 EXPECT_PRED5(CheckCandidate, candidates_[3],
349 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
350 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
351 EXPECT_PRED5(CheckCandidate, candidates_[4],
352 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
353 EXPECT_PRED5(CheckCandidate, candidates_[5],
354 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
355 EXPECT_EQ(4U, ports_.size());
356 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
357 EXPECT_PRED5(CheckCandidate, candidates_[6],
358 cricket::ICE_CANDIDATE_COMPONENT_RTP,
359 "relay", "ssltcp", kRelaySslTcpIntAddr);
360 EXPECT_EQ(4U, ports_.size());
361 EXPECT_TRUE(candidate_allocation_done_);
362 // If we Stop gathering now, we shouldn't get a second "done" callback.
363 session_->StopGettingPorts();
364}
365
366TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
367 AddInterface(kClientAddr);
368 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
369 cricket::CN_VIDEO));
370 session_->StartGettingPorts();
371 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
372 EXPECT_TRUE(candidate_allocation_done_);
373 // If we Stop gathering now, we shouldn't get a second "done" callback.
374 session_->StopGettingPorts();
375
376 // All ports should have unset send-buffer sizes.
377 CheckSendBufferSizesOfAllPorts(-1);
378}
379
380// Tests that we can get callback after StopGetAllPorts.
381TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
382 AddInterface(kClientAddr);
383 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
384 session_->StartGettingPorts();
385 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
386 EXPECT_EQ(2U, ports_.size());
387 session_->StopGettingPorts();
388 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
389}
390
391// Test that we restrict client ports appropriately when a port range is set.
392// We check the candidates for udp/stun/tcp ports, and the from address
393// for relay ports.
394TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
395 AddInterface(kClientAddr);
396 // Check that an invalid port range fails.
397 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
398 // Check that a null port range succeeds.
399 EXPECT_TRUE(SetPortRange(0, 0));
400 // Check that a valid port range succeeds.
401 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
402 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
403 session_->StartGettingPorts();
404 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
405 EXPECT_EQ(4U, ports_.size());
406 // Check the port number for the UDP port object.
407 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
408 // Check the port number for the STUN port object.
409 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
410 // Check the port number used to connect to the relay server.
411 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
412 kMinPort, kMaxPort);
413 // Check the port number for the TCP port object.
414 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
415 EXPECT_TRUE(candidate_allocation_done_);
416}
417
418// Test that we don't crash or malfunction if we have no network adapters.
419TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
420 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
421 session_->StartGettingPorts();
422 rtc::Thread::Current()->ProcessMessages(100);
423 // Without network adapter, we should not get any candidate.
424 EXPECT_EQ(0U, candidates_.size());
425 EXPECT_TRUE(candidate_allocation_done_);
426}
427
428// Test that we can get OnCandidatesAllocationDone callback when all the ports
429// are disabled.
430TEST_F(PortAllocatorTest, TestDisableAllPorts) {
431 AddInterface(kClientAddr);
432 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
433 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
434 cricket::PORTALLOCATOR_DISABLE_STUN |
435 cricket::PORTALLOCATOR_DISABLE_RELAY |
436 cricket::PORTALLOCATOR_DISABLE_TCP);
437 session_->StartGettingPorts();
438 rtc::Thread::Current()->ProcessMessages(100);
439 EXPECT_EQ(0U, candidates_.size());
440 EXPECT_TRUE(candidate_allocation_done_);
441}
442
443// Test that we don't crash or malfunction if we can't create UDP sockets.
444TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
445 AddInterface(kClientAddr);
446 fss_->set_udp_sockets_enabled(false);
447 EXPECT_TRUE(CreateSession(1));
448 session_->StartGettingPorts();
449 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
450 EXPECT_EQ(2U, ports_.size());
451 EXPECT_PRED5(CheckCandidate, candidates_[0],
452 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
453 EXPECT_PRED5(CheckCandidate, candidates_[1],
454 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
455 EXPECT_PRED5(CheckCandidate, candidates_[2],
456 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
457 EXPECT_PRED5(CheckCandidate, candidates_[3],
458 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
459 EXPECT_PRED5(CheckCandidate, candidates_[4],
460 cricket::ICE_CANDIDATE_COMPONENT_RTP,
461 "relay", "ssltcp", kRelaySslTcpIntAddr);
462 EXPECT_TRUE(candidate_allocation_done_);
463}
464
465// Test that we don't crash or malfunction if we can't create UDP sockets or
466// listen on TCP sockets. We still give out a local TCP address, since
467// apparently this is needed for the remote side to accept our connection.
468TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
469 AddInterface(kClientAddr);
470 fss_->set_udp_sockets_enabled(false);
471 fss_->set_tcp_listen_enabled(false);
472 EXPECT_TRUE(CreateSession(1));
473 session_->StartGettingPorts();
474 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
475 EXPECT_EQ(2U, ports_.size());
476 EXPECT_PRED5(CheckCandidate, candidates_[0],
477 1, "relay", "udp", kRelayUdpIntAddr);
478 EXPECT_PRED5(CheckCandidate, candidates_[1],
479 1, "relay", "udp", kRelayUdpExtAddr);
480 EXPECT_PRED5(CheckCandidate, candidates_[2],
481 1, "relay", "tcp", kRelayTcpIntAddr);
482 EXPECT_PRED5(CheckCandidate, candidates_[3],
483 1, "local", "tcp", kClientAddr);
484 EXPECT_PRED5(CheckCandidate, candidates_[4],
485 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
486 EXPECT_TRUE(candidate_allocation_done_);
487}
488
489// Test that we don't crash or malfunction if we can't create any sockets.
490// TODO: Find a way to exit early here.
491TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
492 AddInterface(kClientAddr);
493 fss_->set_tcp_sockets_enabled(false);
494 fss_->set_udp_sockets_enabled(false);
495 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
496 session_->StartGettingPorts();
497 WAIT(candidates_.size() > 0, 2000);
498 // TODO - Check candidate_allocation_done signal.
499 // In case of Relay, ports creation will succeed but sockets will fail.
500 // There is no error reporting from RelayEntry to handle this failure.
501}
502
503// Testing STUN timeout.
504TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
505 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
506 AddInterface(kClientAddr);
507 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
508 session_->StartGettingPorts();
509 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
510 EXPECT_EQ(2U, ports_.size());
511 EXPECT_PRED5(CheckCandidate, candidates_[0],
512 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
513 EXPECT_PRED5(CheckCandidate, candidates_[1],
514 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
515 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
516 // will be tried after 3 seconds.
517 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
518 EXPECT_EQ(3U, ports_.size());
519 EXPECT_PRED5(CheckCandidate, candidates_[2],
520 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
521 EXPECT_PRED5(CheckCandidate, candidates_[3],
522 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
523 EXPECT_PRED5(CheckCandidate, candidates_[4],
524 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
525 kRelaySslTcpIntAddr);
526 EXPECT_PRED5(CheckCandidate, candidates_[5],
527 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
528 // Stun Timeout is 9sec.
529 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
530}
531
532TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
533 AddInterface(kClientAddr);
534 AddInterface(kClientAddr2);
535 // Allocating only host UDP ports. This is done purely for testing
536 // convenience.
537 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
538 cricket::PORTALLOCATOR_DISABLE_STUN |
539 cricket::PORTALLOCATOR_DISABLE_RELAY);
540 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
541 session_->StartGettingPorts();
542 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
543 ASSERT_EQ(2U, candidates_.size());
544 EXPECT_EQ(2U, ports_.size());
545 // Candidates priorities should be different.
546 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
547}
548
549// Test to verify ICE restart process.
550TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
551 AddInterface(kClientAddr);
552 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
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
560// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
561// This test also verifies that when the allocator is only allowed to use
562// relay (i.e. IceTransportsType is relay), the raddr is an empty
563// address with the correct family. This is to prevent any local
564// reflective address leakage in the sdp line.
565TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
566 AddInterface(kClientAddr);
567 // GTURN is not configured here.
568 ResetWithTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
569 allocator().set_candidate_filter(cricket::CF_RELAY);
570 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
571 session_->StartGettingPorts();
572 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
573 EXPECT_PRED5(CheckCandidate,
574 candidates_[0],
575 cricket::ICE_CANDIDATE_COMPONENT_RTP,
576 "relay",
577 "udp",
578 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
579
580 EXPECT_EQ(1U, candidates_.size());
581 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
582 for (size_t i = 0; i < candidates_.size(); ++i) {
583 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
584 EXPECT_EQ(
585 candidates_[0].related_address(),
586 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
587 }
588}
589
590TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
591 AddInterface(kClientAddr);
592 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
593 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
594 allocator().set_candidate_filter(cricket::CF_HOST);
595 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
596 session_->StartGettingPorts();
597 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
598 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
599 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
600 for (size_t i = 0; i < candidates_.size(); ++i) {
601 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
602 }
603}
604
605// Host is behind the NAT.
606TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
607 AddInterface(kPrivateAddr);
608 ResetWithNatServer(kStunAddr);
609
610 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
611 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
612 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
613 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
614 session_->StartGettingPorts();
615 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
616 // Host is behind NAT, no private address will be exposed. Hence only UDP
617 // port with STUN candidate will be sent outside.
618 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
619 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
620 for (size_t i = 0; i < candidates_.size(); ++i) {
621 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
622 EXPECT_EQ(
623 candidates_[0].related_address(),
624 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
625 }
626}
627
628// Host is not behind the NAT.
629TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
630 AddInterface(kClientAddr);
631 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
632 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
633 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
634 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
635 session_->StartGettingPorts();
636 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
637 // Host has a public address, both UDP and TCP candidates will be exposed.
638 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
639 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
640 for (size_t i = 0; i < candidates_.size(); ++i) {
641 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
642 }
643}
644
645TEST_F(PortAllocatorTest, TestBasicMuxFeatures) {
646 AddInterface(kClientAddr);
pthatcher@webrtc.org96572652015-01-09 19:08:27 +0000647 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000648 // Session ID - session1.
649 rtc::scoped_ptr<cricket::PortAllocatorSession> session1(
650 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
651 rtc::scoped_ptr<cricket::PortAllocatorSession> session2(
652 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTCP));
653 session1->StartGettingPorts();
654 session2->StartGettingPorts();
655 // Each session should receive two proxy ports of local and stun.
656 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
657 EXPECT_EQ(8U, ports_.size());
658
659 rtc::scoped_ptr<cricket::PortAllocatorSession> session3(
660 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
661 session3->StartGettingPorts();
662 // Already allocated candidates and ports will be sent to the newly
663 // allocated proxy session.
664 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
665 EXPECT_EQ(12U, ports_.size());
666}
667
668// This test verifies by changing ice_ufrag and/or ice_pwd
669// will result in different set of candidates when BUNDLE is enabled.
670// If BUNDLE is disabled, CreateSession will always allocate new
671// set of candidates.
672TEST_F(PortAllocatorTest, TestBundleIceRestart) {
673 AddInterface(kClientAddr);
pthatcher@webrtc.org96572652015-01-09 19:08:27 +0000674 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000675 // Session ID - session1.
676 rtc::scoped_ptr<cricket::PortAllocatorSession> session1(
677 CreateSession("session1", kContentName,
678 cricket::ICE_CANDIDATE_COMPONENT_RTP,
679 kIceUfrag0, kIcePwd0));
680 session1->StartGettingPorts();
681 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
682 EXPECT_EQ(4U, ports_.size());
683
684 // Allocate a different session with sid |session1| and different ice_ufrag.
685 rtc::scoped_ptr<cricket::PortAllocatorSession> session2(
686 CreateSession("session1", kContentName,
687 cricket::ICE_CANDIDATE_COMPONENT_RTP,
688 "TestIceUfrag", kIcePwd0));
689 session2->StartGettingPorts();
690 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
691 EXPECT_EQ(8U, ports_.size());
692 // Verifying the candidate address different from previously allocated
693 // address.
694 // Skipping verification of component id and candidate type.
695 EXPECT_NE(candidates_[0].address(), candidates_[7].address());
696 EXPECT_NE(candidates_[1].address(), candidates_[8].address());
697
698 // Allocating a different session with sid |session1| and
699 // different ice_pwd.
700 rtc::scoped_ptr<cricket::PortAllocatorSession> session3(
701 CreateSession("session1", kContentName,
702 cricket::ICE_CANDIDATE_COMPONENT_RTP,
703 kIceUfrag0, "TestIcePwd"));
704 session3->StartGettingPorts();
705 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
706 EXPECT_EQ(12U, ports_.size());
707 // Verifying the candidate address different from previously
708 // allocated address.
709 EXPECT_NE(candidates_[7].address(), candidates_[14].address());
710 EXPECT_NE(candidates_[8].address(), candidates_[15].address());
711
712 // Allocating a session with by changing both ice_ufrag and ice_pwd.
713 rtc::scoped_ptr<cricket::PortAllocatorSession> session4(
714 CreateSession("session1", kContentName,
715 cricket::ICE_CANDIDATE_COMPONENT_RTP,
716 "TestIceUfrag", "TestIcePwd"));
717 session4->StartGettingPorts();
718 ASSERT_EQ_WAIT(28U, candidates_.size(), kDefaultAllocationTimeout);
719 EXPECT_EQ(16U, ports_.size());
720 // Verifying the candidate address different from previously
721 // allocated address.
722 EXPECT_NE(candidates_[14].address(), candidates_[21].address());
723 EXPECT_NE(candidates_[15].address(), candidates_[22].address());
724}
725
726// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
727// ufrag and pwd for the collected candidates.
728TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
729 allocator().set_flags(allocator().flags() |
730 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
731 AddInterface(kClientAddr);
732 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
733 session_->StartGettingPorts();
734 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
735 EXPECT_PRED5(CheckCandidate, candidates_[0],
736 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
737 EXPECT_PRED5(CheckCandidate, candidates_[1],
738 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
739 EXPECT_PRED5(CheckCandidate, candidates_[5],
740 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
741 EXPECT_EQ(4U, ports_.size());
742 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
743 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
744 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
745 EXPECT_EQ(kIcePwd0, candidates_[0].password());
746 EXPECT_EQ(kIcePwd0, candidates_[1].password());
747 EXPECT_TRUE(candidate_allocation_done_);
748}
749
750// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
751// different ufrag and pwd for the collected candidates.
752TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
753 allocator().set_flags(allocator().flags() &
754 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
755 AddInterface(kClientAddr);
756 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
757 session_->StartGettingPorts();
758 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
759 EXPECT_PRED5(CheckCandidate, candidates_[0],
760 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
761 EXPECT_PRED5(CheckCandidate, candidates_[1],
762 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
763 EXPECT_EQ(4U, ports_.size());
764 // Port should generate random ufrag and pwd.
765 EXPECT_NE(kIceUfrag0, candidates_[0].username());
766 EXPECT_NE(kIceUfrag0, candidates_[1].username());
767 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
768 EXPECT_NE(kIcePwd0, candidates_[0].password());
769 EXPECT_NE(kIcePwd0, candidates_[1].password());
770 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
771 EXPECT_TRUE(candidate_allocation_done_);
772}
773
774// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
775// is allocated for udp and stun. Also verify there is only one candidate
776// (local) if stun candidate is same as local candidate, which will be the case
777// in a public network like the below test.
778TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
779 AddInterface(kClientAddr);
780 allocator_->set_flags(allocator().flags() |
781 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
782 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
783 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
784 session_->StartGettingPorts();
785 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
786 EXPECT_EQ(3U, ports_.size());
787 EXPECT_PRED5(CheckCandidate, candidates_[0],
788 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
789 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
790}
791
792// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
793// is allocated for udp and stun. In this test we should expect both stun and
794// local candidates as client behind a nat.
795TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
796 AddInterface(kClientAddr);
797 ResetWithNatServer(kStunAddr);
798
799 allocator_->set_flags(allocator().flags() |
800 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
801 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
802 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
803 session_->StartGettingPorts();
804 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
805 ASSERT_EQ(2U, ports_.size());
806 EXPECT_PRED5(CheckCandidate, candidates_[0],
807 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
808 EXPECT_PRED5(CheckCandidate, candidates_[1],
809 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
810 rtc::SocketAddress(kNatAddr.ipaddr(), 0));
811 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
812 EXPECT_EQ(3U, candidates_.size());
813}
814
815// Test TURN port in shared socket mode with UDP and TCP TURN server adderesses.
816TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
817 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
818 AddInterface(kClientAddr);
819 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
820
821 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
822
823 allocator_->set_step_delay(cricket::kMinimumStepDelay);
824 allocator_->set_flags(allocator().flags() |
825 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
826 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
827 cricket::PORTALLOCATOR_DISABLE_TCP);
828
829 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
830 session_->StartGettingPorts();
831
832 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
833 ASSERT_EQ(3U, ports_.size());
834 EXPECT_PRED5(CheckCandidate, candidates_[0],
835 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
836 EXPECT_PRED5(CheckCandidate, candidates_[1],
837 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
838 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
839 EXPECT_PRED5(CheckCandidate, candidates_[2],
840 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
841 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
842 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
843 EXPECT_EQ(3U, candidates_.size());
844}
845
846// Testing DNS resolve for the TURN server, this will test AllocationSequence
847// handling the unresolved address signal from TurnPort.
848TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
849 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
850 cricket::PROTO_UDP);
851 AddInterface(kClientAddr);
852 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
853 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
854 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
855 relay_server.credentials = credentials;
856 relay_server.ports.push_back(cricket::ProtocolAddress(
857 rtc::SocketAddress("localhost", 3478),
858 cricket::PROTO_UDP, false));
859 allocator_->AddRelay(relay_server);
860
861 allocator_->set_step_delay(cricket::kMinimumStepDelay);
862 allocator_->set_flags(allocator().flags() |
863 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
864 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
865 cricket::PORTALLOCATOR_DISABLE_TCP);
866
867 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
868 session_->StartGettingPorts();
869
870 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
871}
872
873// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
874// is allocated for udp/stun/turn. In this test we should expect all local,
875// stun and turn candidates.
876TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
877 AddInterface(kClientAddr);
878 ResetWithNatServer(kStunAddr);
879
880 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
881
882 allocator_->set_flags(allocator().flags() |
883 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
884 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
885 cricket::PORTALLOCATOR_DISABLE_TCP);
886
887 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
888 session_->StartGettingPorts();
889
890 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
891 ASSERT_EQ(2U, ports_.size());
892 EXPECT_PRED5(CheckCandidate, candidates_[0],
893 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
894 EXPECT_PRED5(CheckCandidate, candidates_[1],
895 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
896 rtc::SocketAddress(kNatAddr.ipaddr(), 0));
897 EXPECT_PRED5(CheckCandidate, candidates_[2],
898 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
899 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
900 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
901 EXPECT_EQ(3U, candidates_.size());
902 // Local port will be created first and then TURN port.
903 EXPECT_EQ(2U, ports_[0]->Candidates().size());
904 EXPECT_EQ(1U, ports_[1]->Candidates().size());
905}
906
907// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
908// server is also used as the STUN server, we should get 'local', 'stun', and
909// 'relay' candidates.
910TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
911 AddInterface(kClientAddr);
912 ResetWithNatServer(kTurnUdpIntAddr);
913 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
914
915 // Must set the step delay to 0 to make sure the relay allocation phase is
916 // started before the STUN candidates are obtained, so that the STUN binding
917 // response is processed when both StunPort and TurnPort exist to reproduce
918 // webrtc issue 3537.
919 allocator_->set_step_delay(0);
920 allocator_->set_flags(allocator().flags() |
921 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
922 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
923 cricket::PORTALLOCATOR_DISABLE_TCP);
924
925 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
926 session_->StartGettingPorts();
927
928 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
929 EXPECT_PRED5(CheckCandidate, candidates_[0],
930 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
931 EXPECT_PRED5(CheckCandidate, candidates_[1],
932 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
933 rtc::SocketAddress(kNatAddr.ipaddr(), 0));
934 EXPECT_PRED5(CheckCandidate, candidates_[2],
935 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
936 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
937 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
938
939 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
940 EXPECT_EQ(3U, candidates_.size());
941 // Local port will be created first and then TURN port.
942 EXPECT_EQ(2U, ports_[0]->Candidates().size());
943 EXPECT_EQ(1U, ports_[1]->Candidates().size());
944}
945
946// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
947// and fail to generate STUN candidate, local UDP candidate is generated
948// properly.
949TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
950 allocator().set_flags(allocator().flags() |
951 cricket::PORTALLOCATOR_DISABLE_RELAY |
952 cricket::PORTALLOCATOR_DISABLE_TCP |
953 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
954 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
955 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
956 AddInterface(kClientAddr);
957 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
958 session_->StartGettingPorts();
959 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
960 EXPECT_EQ(1U, candidates_.size());
961 EXPECT_PRED5(CheckCandidate, candidates_[0],
962 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
963 // STUN timeout is 9sec. We need to wait to get candidate done signal.
964 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
965 EXPECT_EQ(1U, candidates_.size());
966}
967
968// This test verifies allocator can use IPv6 addresses along with IPv4.
969TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
970 allocator().set_flags(allocator().flags() |
971 cricket::PORTALLOCATOR_DISABLE_RELAY |
972 cricket::PORTALLOCATOR_ENABLE_IPV6 |
973 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
974 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
975 AddInterface(kClientIPv6Addr);
976 AddInterface(kClientAddr);
977 allocator_->set_step_delay(cricket::kMinimumStepDelay);
978 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
979 session_->StartGettingPorts();
980 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
981 EXPECT_EQ(4U, candidates_.size());
982 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
983 EXPECT_PRED5(CheckCandidate, candidates_[0],
984 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
985 kClientIPv6Addr);
986 EXPECT_PRED5(CheckCandidate, candidates_[1],
987 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
988 kClientAddr);
989 EXPECT_PRED5(CheckCandidate, candidates_[2],
990 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
991 kClientIPv6Addr);
992 EXPECT_PRED5(CheckCandidate, candidates_[3],
993 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
994 kClientAddr);
995 EXPECT_EQ(4U, candidates_.size());
996}
997
998// Test that the httpportallocator correctly maintains its lists of stun and
999// relay servers, by never allowing an empty list.
1000TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1001 rtc::FakeNetworkManager network_manager;
1002 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1003 EXPECT_EQ(1U, alloc.relay_hosts().size());
1004 EXPECT_EQ(1U, alloc.stun_hosts().size());
1005
1006 std::vector<std::string> relay_servers;
1007 std::vector<rtc::SocketAddress> stun_servers;
1008
1009 alloc.SetRelayHosts(relay_servers);
1010 alloc.SetStunHosts(stun_servers);
1011 EXPECT_EQ(1U, alloc.relay_hosts().size());
1012 EXPECT_EQ(1U, alloc.stun_hosts().size());
1013
1014 relay_servers.push_back("1.unittest.corp.google.com");
1015 relay_servers.push_back("2.unittest.corp.google.com");
1016 stun_servers.push_back(
1017 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1018 stun_servers.push_back(
1019 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1020
1021 alloc.SetRelayHosts(relay_servers);
1022 alloc.SetStunHosts(stun_servers);
1023 EXPECT_EQ(2U, alloc.relay_hosts().size());
1024 EXPECT_EQ(2U, alloc.stun_hosts().size());
1025}
1026
1027// Test that the HttpPortAllocator uses correct URL to create sessions.
1028TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1029 rtc::FakeNetworkManager network_manager;
1030 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1031
1032 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1033 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1034 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1035 static_cast<cricket::HttpPortAllocatorSession*>(
1036 alloc.CreateSessionInternal(
1037 "test content", 0, kIceUfrag0, kIcePwd0)));
1038 std::string url = session->GetSessionRequestUrl();
1039 LOG(LS_INFO) << "url: " << url;
1040 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
1041
1042 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1043 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1044 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
1045 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
1046 url = session->GetSessionRequestUrl();
1047 LOG(LS_INFO) << "url: " << url;
1048 std::vector<std::string> parts;
1049 rtc::split(url, '?', &parts);
1050 ASSERT_EQ(2U, parts.size());
1051
1052 std::vector<std::string> args_parts;
1053 rtc::split(parts[1], '&', &args_parts);
1054
1055 std::map<std::string, std::string> args;
1056 for (std::vector<std::string>::iterator it = args_parts.begin();
1057 it != args_parts.end(); ++it) {
1058 std::vector<std::string> parts;
1059 rtc::split(*it, '=', &parts);
1060 ASSERT_EQ(2U, parts.size());
1061 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1062 }
1063
1064 EXPECT_EQ(kIceUfrag0, args["username"]);
1065 EXPECT_EQ(kIcePwd0, args["password"]);
1066}