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