blob: 7dc25140cccf91a91194af75af8f8b4c18f33993 [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"
honghaizf421bdc2015-07-17 16:21:55 -070023#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#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);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000040static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041static const SocketAddress kClientIPv6Addr(
42 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
43static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070044static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
45static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
47static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
48static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
49static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
50static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
51static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
52static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
53static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
54static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
55static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
56static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
57
58// Minimum and maximum port for port range tests.
59static const int kMinPort = 10000;
60static const int kMaxPort = 10099;
61
62// Based on ICE_UFRAG_LENGTH
63static const char kIceUfrag0[] = "TESTICEUFRAG0000";
64// Based on ICE_PWD_LENGTH
65static const char kIcePwd0[] = "TESTICEPWD00000000000000";
66
67static const char kContentName[] = "test content";
68
69static const int kDefaultAllocationTimeout = 1000;
70static const char kTurnUsername[] = "test";
71static const char kTurnPassword[] = "test";
72
73namespace cricket {
74
75// Helper for dumping candidates
76std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
77 os << c.ToString();
78 return os;
79}
80
81} // namespace cricket
82
83class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
84 public:
85 PortAllocatorTest()
86 : pss_(new rtc::PhysicalSocketServer),
87 vss_(new rtc::VirtualSocketServer(pss_.get())),
88 fss_(new rtc::FirewallSocketServer(vss_.get())),
89 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070090 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091 nat_socket_factory_(&nat_factory_),
92 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
93 kStunAddr)),
94 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
95 kRelayTcpIntAddr, kRelayTcpExtAddr,
96 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
97 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
98 candidate_allocation_done_(false) {
99 cricket::ServerAddresses stun_servers;
100 stun_servers.insert(kStunAddr);
101 // Passing the addresses of GTURN servers will enable GTURN in
102 // Basicportallocator.
103 allocator_.reset(new cricket::BasicPortAllocator(
104 &network_manager_,
105 stun_servers,
106 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
107 allocator_->set_step_delay(cricket::kMinimumStepDelay);
108 }
109
110 void AddInterface(const SocketAddress& addr) {
111 network_manager_.AddInterface(addr);
112 }
113 bool SetPortRange(int min_port, int max_port) {
114 return allocator_->SetPortRange(min_port, max_port);
115 }
116 void ResetWithNatServer(const rtc::SocketAddress& stun_server) {
117 nat_server_.reset(new rtc::NATServer(
deadbeefc5d0d952015-07-16 10:22:21 -0700118 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
119 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120
121 ServerAddresses stun_servers;
Jiayang Liud7e5c442015-04-27 11:47:21 -0700122 if (!stun_server.IsNil()) {
123 stun_servers.insert(stun_server);
124 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 allocator_.reset(new cricket::BasicPortAllocator(
126 &network_manager_, &nat_socket_factory_, stun_servers));
127 allocator().set_step_delay(cricket::kMinimumStepDelay);
128 }
129
130 // Create a BasicPortAllocator without GTURN and add the TURN servers.
131 void ResetWithTurnServers(const rtc::SocketAddress& udp_turn,
132 const rtc::SocketAddress& tcp_turn) {
133 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
134 allocator().set_step_delay(cricket::kMinimumStepDelay);
135 AddTurnServers(udp_turn, tcp_turn);
136 }
137
138 void AddTurnServers(const rtc::SocketAddress& udp_turn,
139 const rtc::SocketAddress& tcp_turn) {
140 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
141 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
142 relay_server.credentials = credentials;
143
144 if (!udp_turn.IsNil()) {
145 relay_server.ports.push_back(cricket::ProtocolAddress(
146 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
147 }
148 if (!tcp_turn.IsNil()) {
149 relay_server.ports.push_back(cricket::ProtocolAddress(
150 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
151 }
152 allocator_->AddRelay(relay_server);
153 }
154
155 bool CreateSession(int component) {
156 session_.reset(CreateSession("session", component));
157 if (!session_)
158 return false;
159 return true;
160 }
161
162 bool CreateSession(int component, const std::string& content_name) {
163 session_.reset(CreateSession("session", content_name, component));
164 if (!session_)
165 return false;
166 return true;
167 }
168
169 cricket::PortAllocatorSession* CreateSession(
170 const std::string& sid, int component) {
171 return CreateSession(sid, kContentName, component);
172 }
173
174 cricket::PortAllocatorSession* CreateSession(
175 const std::string& sid, const std::string& content_name, int component) {
176 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
177 }
178
179 cricket::PortAllocatorSession* CreateSession(
180 const std::string& sid, const std::string& content_name, int component,
181 const std::string& ice_ufrag, const std::string& ice_pwd) {
182 cricket::PortAllocatorSession* session =
183 allocator_->CreateSession(
184 sid, content_name, component, ice_ufrag, ice_pwd);
185 session->SignalPortReady.connect(this,
186 &PortAllocatorTest::OnPortReady);
187 session->SignalCandidatesReady.connect(this,
188 &PortAllocatorTest::OnCandidatesReady);
189 session->SignalCandidatesAllocationDone.connect(this,
190 &PortAllocatorTest::OnCandidatesAllocationDone);
191 return session;
192 }
193
194 static bool CheckCandidate(const cricket::Candidate& c,
195 int component, const std::string& type,
196 const std::string& proto,
197 const SocketAddress& addr) {
198 return (c.component() == component && c.type() == type &&
199 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
200 ((addr.port() == 0 && (c.address().port() != 0)) ||
201 (c.address().port() == addr.port())));
202 }
203 static bool CheckPort(const rtc::SocketAddress& addr,
204 int min_port, int max_port) {
205 return (addr.port() >= min_port && addr.port() <= max_port);
206 }
207
208 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
209 // We should only get this callback once, except in the mux test where
210 // we have multiple port allocation sessions.
211 if (session == session_.get()) {
212 ASSERT_FALSE(candidate_allocation_done_);
213 candidate_allocation_done_ = true;
214 }
215 }
216
217 // Check if all ports allocated have send-buffer size |expected|. If
218 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
219 void CheckSendBufferSizesOfAllPorts(int expected) {
220 std::vector<cricket::PortInterface*>::iterator it;
221 for (it = ports_.begin(); it < ports_.end(); ++it) {
222 int send_buffer_size;
223 if (expected == -1) {
224 EXPECT_EQ(SOCKET_ERROR,
225 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
226 &send_buffer_size));
227 } else {
228 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
229 &send_buffer_size));
230 ASSERT_EQ(expected, send_buffer_size);
231 }
232 }
233 }
234
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000235 void CheckDisableAdapterEnumeration() {
236 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
237 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
238 session_->StartGettingPorts();
239 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
240
241 // Only 2 candidates as local UDP/TCP are all 0s and get trimmed out.
242 EXPECT_EQ(2U, candidates_.size());
243 EXPECT_EQ(2U, ports_.size()); // One stunport and one turnport.
244
245 EXPECT_PRED5(CheckCandidate, candidates_[0],
246 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700247 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000248 EXPECT_EQ(
249 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()),
250 candidates_[0].related_address());
251
252 EXPECT_PRED5(CheckCandidate, candidates_[1],
253 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
254 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -0700255 EXPECT_EQ(kNatUdpAddr.ipaddr(), candidates_[1].related_address().ipaddr());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000256 }
257
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000258 protected:
259 cricket::BasicPortAllocator& allocator() {
260 return *allocator_;
261 }
262
263 void OnPortReady(cricket::PortAllocatorSession* ses,
264 cricket::PortInterface* port) {
265 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
266 ports_.push_back(port);
267 }
268 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
269 const std::vector<cricket::Candidate>& candidates) {
270 for (size_t i = 0; i < candidates.size(); ++i) {
271 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
272 candidates_.push_back(candidates[i]);
273 }
274 }
275
276 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
277 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
278 cricket::RelayServerConfig server_config = allocator_->relays()[i];
279 cricket::PortList::const_iterator relay_port;
280 for (relay_port = server_config.ports.begin();
281 relay_port != server_config.ports.end(); ++relay_port) {
282 if (proto_addr.address == relay_port->address &&
283 proto_addr.proto == relay_port->proto)
284 return true;
285 }
286 }
287 return false;
288 }
289
290 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
291 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
292 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
293 rtc::SocketServerScope ss_scope_;
294 rtc::scoped_ptr<rtc::NATServer> nat_server_;
295 rtc::NATSocketFactory nat_factory_;
296 rtc::BasicPacketSocketFactory nat_socket_factory_;
297 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
298 cricket::TestRelayServer relay_server_;
299 cricket::TestTurnServer turn_server_;
300 rtc::FakeNetworkManager network_manager_;
301 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
302 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
303 std::vector<cricket::PortInterface*> ports_;
304 std::vector<cricket::Candidate> candidates_;
305 bool candidate_allocation_done_;
306};
307
308// Tests that we can init the port allocator and create a session.
309TEST_F(PortAllocatorTest, TestBasic) {
310 EXPECT_EQ(&network_manager_, allocator().network_manager());
311 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
312 ASSERT_EQ(1u, allocator().relays().size());
313 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
314 // Empty relay credentials are used for GTURN.
315 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
316 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
317 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
318 kRelayUdpIntAddr, cricket::PROTO_UDP)));
319 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
320 kRelayTcpIntAddr, cricket::PROTO_TCP)));
321 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
322 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
323 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
324}
325
326// Tests that we allocator session not trying to allocate ports for every 250ms.
327TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
328 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
329 session_->StartGettingPorts();
330 // Waiting for one second to make sure BasicPortAllocatorSession has not
331 // called OnAllocate multiple times. In old behavior it's called every 250ms.
332 // When there are no network interfaces, each execution of OnAllocate will
333 // result in SignalCandidatesAllocationDone signal.
334 rtc::Thread::Current()->ProcessMessages(1000);
335 EXPECT_TRUE(candidate_allocation_done_);
336 EXPECT_EQ(0U, candidates_.size());
337}
338
339// Tests that we can get all the desired addresses successfully.
340TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
341 AddInterface(kClientAddr);
342 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
343 session_->StartGettingPorts();
344 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
345 EXPECT_EQ(4U, ports_.size());
346 EXPECT_PRED5(CheckCandidate, candidates_[0],
347 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
348 EXPECT_PRED5(CheckCandidate, candidates_[1],
349 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
350 EXPECT_PRED5(CheckCandidate, candidates_[2],
351 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
352 EXPECT_PRED5(CheckCandidate, candidates_[3],
353 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
354 EXPECT_PRED5(CheckCandidate, candidates_[4],
355 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
356 EXPECT_PRED5(CheckCandidate, candidates_[5],
357 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
358 EXPECT_PRED5(CheckCandidate, candidates_[6],
359 cricket::ICE_CANDIDATE_COMPONENT_RTP,
360 "relay", "ssltcp", kRelaySslTcpIntAddr);
361 EXPECT_TRUE(candidate_allocation_done_);
362}
363
364// Verify candidates with default step delay of 1sec.
365TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
366 AddInterface(kClientAddr);
367 allocator_->set_step_delay(cricket::kDefaultStepDelay);
368 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
369 session_->StartGettingPorts();
370 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
371 EXPECT_EQ(2U, ports_.size());
372 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
373 EXPECT_EQ(3U, ports_.size());
374 EXPECT_PRED5(CheckCandidate, candidates_[2],
375 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
376 EXPECT_PRED5(CheckCandidate, candidates_[3],
377 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
378 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
379 EXPECT_PRED5(CheckCandidate, candidates_[4],
380 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
381 EXPECT_PRED5(CheckCandidate, candidates_[5],
382 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
383 EXPECT_EQ(4U, ports_.size());
384 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
385 EXPECT_PRED5(CheckCandidate, candidates_[6],
386 cricket::ICE_CANDIDATE_COMPONENT_RTP,
387 "relay", "ssltcp", kRelaySslTcpIntAddr);
388 EXPECT_EQ(4U, ports_.size());
389 EXPECT_TRUE(candidate_allocation_done_);
390 // If we Stop gathering now, we shouldn't get a second "done" callback.
391 session_->StopGettingPorts();
392}
393
394TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
395 AddInterface(kClientAddr);
396 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
397 cricket::CN_VIDEO));
398 session_->StartGettingPorts();
399 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
400 EXPECT_TRUE(candidate_allocation_done_);
401 // If we Stop gathering now, we shouldn't get a second "done" callback.
402 session_->StopGettingPorts();
403
404 // All ports should have unset send-buffer sizes.
405 CheckSendBufferSizesOfAllPorts(-1);
406}
407
408// Tests that we can get callback after StopGetAllPorts.
409TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
410 AddInterface(kClientAddr);
411 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
412 session_->StartGettingPorts();
413 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
414 EXPECT_EQ(2U, ports_.size());
415 session_->StopGettingPorts();
416 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
417}
418
419// Test that we restrict client ports appropriately when a port range is set.
420// We check the candidates for udp/stun/tcp ports, and the from address
421// for relay ports.
422TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
423 AddInterface(kClientAddr);
424 // Check that an invalid port range fails.
425 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
426 // Check that a null port range succeeds.
427 EXPECT_TRUE(SetPortRange(0, 0));
428 // Check that a valid port range succeeds.
429 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
430 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
431 session_->StartGettingPorts();
432 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
433 EXPECT_EQ(4U, ports_.size());
434 // Check the port number for the UDP port object.
435 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
436 // Check the port number for the STUN port object.
437 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
438 // Check the port number used to connect to the relay server.
439 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
440 kMinPort, kMaxPort);
441 // Check the port number for the TCP port object.
442 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
443 EXPECT_TRUE(candidate_allocation_done_);
444}
445
446// Test that we don't crash or malfunction if we have no network adapters.
447TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
448 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
449 session_->StartGettingPorts();
450 rtc::Thread::Current()->ProcessMessages(100);
451 // Without network adapter, we should not get any candidate.
452 EXPECT_EQ(0U, candidates_.size());
453 EXPECT_TRUE(candidate_allocation_done_);
454}
455
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000456// Test that we should only get STUN and TURN candidates when adapter
457// enumeration is disabled.
458TEST_F(PortAllocatorTest, TestDisableAdapterEnumeration) {
459 AddInterface(kClientAddr);
460 // GTURN is not configured here.
461 ResetWithNatServer(kStunAddr);
462 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
463
464 CheckDisableAdapterEnumeration();
465}
466
467// Test that even with multiple interfaces, the result should be only 1 Stun
468// candidate since we bind to any address (i.e. all 0s).
469TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationMultipleInterfaces) {
470 AddInterface(kPrivateAddr);
471 AddInterface(kPrivateAddr2);
472 ResetWithNatServer(kStunAddr);
473 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
474
475 CheckDisableAdapterEnumeration();
476}
477
Erik Språngefdce692015-06-05 09:41:26 +0200478// Disable for asan, see
479// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
480#if !defined(ADDRESS_SANITIZER)
481
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000482// Test that we can get OnCandidatesAllocationDone callback when all the ports
483// are disabled.
484TEST_F(PortAllocatorTest, TestDisableAllPorts) {
485 AddInterface(kClientAddr);
486 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
487 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
488 cricket::PORTALLOCATOR_DISABLE_STUN |
489 cricket::PORTALLOCATOR_DISABLE_RELAY |
490 cricket::PORTALLOCATOR_DISABLE_TCP);
491 session_->StartGettingPorts();
492 rtc::Thread::Current()->ProcessMessages(100);
493 EXPECT_EQ(0U, candidates_.size());
494 EXPECT_TRUE(candidate_allocation_done_);
495}
496
497// Test that we don't crash or malfunction if we can't create UDP sockets.
498TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
499 AddInterface(kClientAddr);
500 fss_->set_udp_sockets_enabled(false);
501 EXPECT_TRUE(CreateSession(1));
502 session_->StartGettingPorts();
503 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
504 EXPECT_EQ(2U, ports_.size());
505 EXPECT_PRED5(CheckCandidate, candidates_[0],
506 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
507 EXPECT_PRED5(CheckCandidate, candidates_[1],
508 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
509 EXPECT_PRED5(CheckCandidate, candidates_[2],
510 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
511 EXPECT_PRED5(CheckCandidate, candidates_[3],
512 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
513 EXPECT_PRED5(CheckCandidate, candidates_[4],
514 cricket::ICE_CANDIDATE_COMPONENT_RTP,
515 "relay", "ssltcp", kRelaySslTcpIntAddr);
516 EXPECT_TRUE(candidate_allocation_done_);
517}
518
Erik Språngefdce692015-06-05 09:41:26 +0200519#endif // if !defined(ADDRESS_SANITIZER)
520
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000521// Test that we don't crash or malfunction if we can't create UDP sockets or
522// listen on TCP sockets. We still give out a local TCP address, since
523// apparently this is needed for the remote side to accept our connection.
524TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
525 AddInterface(kClientAddr);
526 fss_->set_udp_sockets_enabled(false);
527 fss_->set_tcp_listen_enabled(false);
528 EXPECT_TRUE(CreateSession(1));
529 session_->StartGettingPorts();
530 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
531 EXPECT_EQ(2U, ports_.size());
532 EXPECT_PRED5(CheckCandidate, candidates_[0],
533 1, "relay", "udp", kRelayUdpIntAddr);
534 EXPECT_PRED5(CheckCandidate, candidates_[1],
535 1, "relay", "udp", kRelayUdpExtAddr);
536 EXPECT_PRED5(CheckCandidate, candidates_[2],
537 1, "relay", "tcp", kRelayTcpIntAddr);
538 EXPECT_PRED5(CheckCandidate, candidates_[3],
539 1, "local", "tcp", kClientAddr);
540 EXPECT_PRED5(CheckCandidate, candidates_[4],
541 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
542 EXPECT_TRUE(candidate_allocation_done_);
543}
544
545// Test that we don't crash or malfunction if we can't create any sockets.
546// TODO: Find a way to exit early here.
547TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
548 AddInterface(kClientAddr);
549 fss_->set_tcp_sockets_enabled(false);
550 fss_->set_udp_sockets_enabled(false);
551 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
552 session_->StartGettingPorts();
553 WAIT(candidates_.size() > 0, 2000);
554 // TODO - Check candidate_allocation_done signal.
555 // In case of Relay, ports creation will succeed but sockets will fail.
556 // There is no error reporting from RelayEntry to handle this failure.
557}
558
559// Testing STUN timeout.
560TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
561 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
562 AddInterface(kClientAddr);
563 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
564 session_->StartGettingPorts();
565 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
566 EXPECT_EQ(2U, ports_.size());
567 EXPECT_PRED5(CheckCandidate, candidates_[0],
568 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
569 EXPECT_PRED5(CheckCandidate, candidates_[1],
570 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
571 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
572 // will be tried after 3 seconds.
573 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
574 EXPECT_EQ(3U, ports_.size());
575 EXPECT_PRED5(CheckCandidate, candidates_[2],
576 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
577 EXPECT_PRED5(CheckCandidate, candidates_[3],
578 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
579 EXPECT_PRED5(CheckCandidate, candidates_[4],
580 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
581 kRelaySslTcpIntAddr);
582 EXPECT_PRED5(CheckCandidate, candidates_[5],
583 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
584 // Stun Timeout is 9sec.
585 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
586}
587
588TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
589 AddInterface(kClientAddr);
590 AddInterface(kClientAddr2);
591 // Allocating only host UDP ports. This is done purely for testing
592 // convenience.
593 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
594 cricket::PORTALLOCATOR_DISABLE_STUN |
595 cricket::PORTALLOCATOR_DISABLE_RELAY);
596 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
597 session_->StartGettingPorts();
598 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
599 ASSERT_EQ(2U, candidates_.size());
600 EXPECT_EQ(2U, ports_.size());
601 // Candidates priorities should be different.
602 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
603}
604
605// Test to verify ICE restart process.
606TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
607 AddInterface(kClientAddr);
608 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
609 session_->StartGettingPorts();
610 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
611 EXPECT_EQ(4U, ports_.size());
612 EXPECT_TRUE(candidate_allocation_done_);
613 // TODO - Extend this to verify ICE restart.
614}
615
616// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
617// This test also verifies that when the allocator is only allowed to use
618// relay (i.e. IceTransportsType is relay), the raddr is an empty
619// address with the correct family. This is to prevent any local
620// reflective address leakage in the sdp line.
621TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
622 AddInterface(kClientAddr);
623 // GTURN is not configured here.
624 ResetWithTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
625 allocator().set_candidate_filter(cricket::CF_RELAY);
626 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
627 session_->StartGettingPorts();
628 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
629 EXPECT_PRED5(CheckCandidate,
630 candidates_[0],
631 cricket::ICE_CANDIDATE_COMPONENT_RTP,
632 "relay",
633 "udp",
634 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
635
636 EXPECT_EQ(1U, candidates_.size());
637 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
638 for (size_t i = 0; i < candidates_.size(); ++i) {
639 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
640 EXPECT_EQ(
641 candidates_[0].related_address(),
642 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
643 }
644}
645
646TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
647 AddInterface(kClientAddr);
pthatcherfa301802015-08-11 04:12:56 -0700648 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
649 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000650 allocator().set_candidate_filter(cricket::CF_HOST);
651 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
652 session_->StartGettingPorts();
653 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
654 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
655 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
656 for (size_t i = 0; i < candidates_.size(); ++i) {
657 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
658 }
659}
660
661// Host is behind the NAT.
662TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
663 AddInterface(kPrivateAddr);
664 ResetWithNatServer(kStunAddr);
665
pthatcherfa301802015-08-11 04:12:56 -0700666 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
667 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000668 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
669 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
670 session_->StartGettingPorts();
671 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
672 // Host is behind NAT, no private address will be exposed. Hence only UDP
673 // port with STUN candidate will be sent outside.
674 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
675 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
676 for (size_t i = 0; i < candidates_.size(); ++i) {
677 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
678 EXPECT_EQ(
679 candidates_[0].related_address(),
680 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
681 }
682}
683
684// Host is not behind the NAT.
685TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
686 AddInterface(kClientAddr);
pthatcherfa301802015-08-11 04:12:56 -0700687 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
688 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000689 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
690 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
691 session_->StartGettingPorts();
692 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
693 // Host has a public address, both UDP and TCP candidates will be exposed.
694 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
695 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
696 for (size_t i = 0; i < candidates_.size(); ++i) {
697 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
698 }
699}
700
pthatcherfa301802015-08-11 04:12:56 -0700701// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
702// ufrag and pwd for the collected candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000703TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
pthatcherfa301802015-08-11 04:12:56 -0700704 allocator().set_flags(allocator().flags() |
705 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000706 AddInterface(kClientAddr);
707 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
708 session_->StartGettingPorts();
709 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
710 EXPECT_PRED5(CheckCandidate, candidates_[0],
711 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
712 EXPECT_PRED5(CheckCandidate, candidates_[1],
713 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
714 EXPECT_PRED5(CheckCandidate, candidates_[5],
715 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
716 EXPECT_EQ(4U, ports_.size());
717 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
718 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
719 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
720 EXPECT_EQ(kIcePwd0, candidates_[0].password());
721 EXPECT_EQ(kIcePwd0, candidates_[1].password());
722 EXPECT_TRUE(candidate_allocation_done_);
723}
724
pthatcherfa301802015-08-11 04:12:56 -0700725// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
726// different ufrag and pwd for the collected candidates.
727TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
728 allocator().set_flags(allocator().flags() &
729 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
730 AddInterface(kClientAddr);
731 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
732 session_->StartGettingPorts();
733 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
734 EXPECT_PRED5(CheckCandidate, candidates_[0],
735 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
736 EXPECT_PRED5(CheckCandidate, candidates_[1],
737 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
738 EXPECT_EQ(4U, ports_.size());
739 // Port should generate random ufrag and pwd.
740 EXPECT_NE(kIceUfrag0, candidates_[0].username());
741 EXPECT_NE(kIceUfrag0, candidates_[1].username());
742 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
743 EXPECT_NE(kIcePwd0, candidates_[0].password());
744 EXPECT_NE(kIcePwd0, candidates_[1].password());
745 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
746 EXPECT_TRUE(candidate_allocation_done_);
747}
748
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000749// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
750// is allocated for udp and stun. Also verify there is only one candidate
751// (local) if stun candidate is same as local candidate, which will be the case
752// in a public network like the below test.
753TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
754 AddInterface(kClientAddr);
755 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700756 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000757 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
758 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
759 session_->StartGettingPorts();
760 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
761 EXPECT_EQ(3U, ports_.size());
762 EXPECT_PRED5(CheckCandidate, candidates_[0],
763 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
764 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
765}
766
767// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
768// is allocated for udp and stun. In this test we should expect both stun and
769// local candidates as client behind a nat.
770TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
771 AddInterface(kClientAddr);
772 ResetWithNatServer(kStunAddr);
773
774 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700775 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000776 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
777 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
778 session_->StartGettingPorts();
779 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
780 ASSERT_EQ(2U, ports_.size());
781 EXPECT_PRED5(CheckCandidate, candidates_[0],
782 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
783 EXPECT_PRED5(CheckCandidate, candidates_[1],
784 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700785 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
787 EXPECT_EQ(3U, candidates_.size());
788}
789
deadbeefc5d0d952015-07-16 10:22:21 -0700790// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000791TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
792 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
793 AddInterface(kClientAddr);
794 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
795
796 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
797
798 allocator_->set_step_delay(cricket::kMinimumStepDelay);
799 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700800 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000801 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
802 cricket::PORTALLOCATOR_DISABLE_TCP);
803
804 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
805 session_->StartGettingPorts();
806
807 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
808 ASSERT_EQ(3U, ports_.size());
809 EXPECT_PRED5(CheckCandidate, candidates_[0],
810 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
811 EXPECT_PRED5(CheckCandidate, candidates_[1],
812 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
813 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
814 EXPECT_PRED5(CheckCandidate, candidates_[2],
815 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
816 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
817 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
818 EXPECT_EQ(3U, candidates_.size());
819}
820
821// Testing DNS resolve for the TURN server, this will test AllocationSequence
822// handling the unresolved address signal from TurnPort.
823TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
824 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
825 cricket::PROTO_UDP);
826 AddInterface(kClientAddr);
827 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
828 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
829 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
830 relay_server.credentials = credentials;
831 relay_server.ports.push_back(cricket::ProtocolAddress(
832 rtc::SocketAddress("localhost", 3478),
833 cricket::PROTO_UDP, false));
834 allocator_->AddRelay(relay_server);
835
836 allocator_->set_step_delay(cricket::kMinimumStepDelay);
837 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700838 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000839 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
840 cricket::PORTALLOCATOR_DISABLE_TCP);
841
842 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
843 session_->StartGettingPorts();
844
845 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
846}
847
848// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
849// is allocated for udp/stun/turn. In this test we should expect all local,
850// stun and turn candidates.
851TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
852 AddInterface(kClientAddr);
853 ResetWithNatServer(kStunAddr);
854
855 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
856
857 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700858 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000859 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
860 cricket::PORTALLOCATOR_DISABLE_TCP);
861
862 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
863 session_->StartGettingPorts();
864
865 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
866 ASSERT_EQ(2U, ports_.size());
867 EXPECT_PRED5(CheckCandidate, candidates_[0],
868 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
869 EXPECT_PRED5(CheckCandidate, candidates_[1],
870 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700871 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000872 EXPECT_PRED5(CheckCandidate, candidates_[2],
873 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
874 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
875 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
876 EXPECT_EQ(3U, candidates_.size());
877 // Local port will be created first and then TURN port.
878 EXPECT_EQ(2U, ports_[0]->Candidates().size());
879 EXPECT_EQ(1U, ports_[1]->Candidates().size());
880}
881
882// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
883// server is also used as the STUN server, we should get 'local', 'stun', and
884// 'relay' candidates.
885TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
886 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -0700887 // Use an empty SocketAddress to add a NAT without STUN server.
888 ResetWithNatServer(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000889 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
890
891 // Must set the step delay to 0 to make sure the relay allocation phase is
892 // started before the STUN candidates are obtained, so that the STUN binding
893 // response is processed when both StunPort and TurnPort exist to reproduce
894 // webrtc issue 3537.
895 allocator_->set_step_delay(0);
896 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700897 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
899 cricket::PORTALLOCATOR_DISABLE_TCP);
900
901 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
902 session_->StartGettingPorts();
903
904 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
905 EXPECT_PRED5(CheckCandidate, candidates_[0],
906 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
907 EXPECT_PRED5(CheckCandidate, candidates_[1],
908 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700909 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000910 EXPECT_PRED5(CheckCandidate, candidates_[2],
911 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
912 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
913 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
914
915 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
916 EXPECT_EQ(3U, candidates_.size());
917 // Local port will be created first and then TURN port.
918 EXPECT_EQ(2U, ports_[0]->Candidates().size());
919 EXPECT_EQ(1U, ports_[1]->Candidates().size());
920}
921
deadbeefc5d0d952015-07-16 10:22:21 -0700922// Test that when only a TCP TURN server is available, we do NOT use it as
923// a UDP STUN server, as this could leak our IP address. Thus we should only
924// expect two ports, a UDPPort and TurnPort.
925TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
926 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
927 AddInterface(kClientAddr);
928 ResetWithNatServer(rtc::SocketAddress());
929 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
930
931 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700932 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -0700933 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
934 cricket::PORTALLOCATOR_DISABLE_TCP);
935
936 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
937 session_->StartGettingPorts();
938
939 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
940 ASSERT_EQ(2U, ports_.size());
941 EXPECT_PRED5(CheckCandidate, candidates_[0],
942 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
943 kClientAddr);
944 EXPECT_PRED5(CheckCandidate, candidates_[1],
945 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
946 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
947 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
948 EXPECT_EQ(2U, candidates_.size());
949 EXPECT_EQ(1U, ports_[0]->Candidates().size());
950 EXPECT_EQ(1U, ports_[1]->Candidates().size());
951}
952
953// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
954// TURN server is used as the STUN server and we get 'local', 'stun', and
955// 'relay' candidates.
956// TODO(deadbeef): Remove this test when support for non-shared socket mode
957// is removed.
958TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
959 AddInterface(kClientAddr);
960 // Use an empty SocketAddress to add a NAT without STUN server.
961 ResetWithNatServer(SocketAddress());
962 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
963
964 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700965 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -0700966 cricket::PORTALLOCATOR_DISABLE_TCP);
967
968 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
969 session_->StartGettingPorts();
970
971 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
972 ASSERT_EQ(3U, ports_.size());
973 EXPECT_PRED5(CheckCandidate, candidates_[0],
974 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
975 kClientAddr);
976 EXPECT_PRED5(CheckCandidate, candidates_[1],
977 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
978 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
979 EXPECT_PRED5(CheckCandidate, candidates_[2],
980 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
981 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
982 // Not using shared socket, so the STUN request's server reflexive address
983 // should be different than the TURN request's server reflexive address.
984 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
985
986 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
987 EXPECT_EQ(3U, candidates_.size());
988 EXPECT_EQ(1U, ports_[0]->Candidates().size());
989 EXPECT_EQ(1U, ports_[1]->Candidates().size());
990 EXPECT_EQ(1U, ports_[2]->Candidates().size());
991}
992
993// Test that even when both a STUN and TURN server are configured, the TURN
994// server is used as a STUN server and we get a 'stun' candidate.
995TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
996 AddInterface(kClientAddr);
997 // Configure with STUN server but destroy it, so we can ensure that it's
998 // the TURN server actually being used as a STUN server.
999 ResetWithNatServer(kStunAddr);
1000 stun_server_.reset();
1001 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1002
1003 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001004 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001005 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1006 cricket::PORTALLOCATOR_DISABLE_TCP);
1007
1008 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1009 session_->StartGettingPorts();
1010
1011 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1012 EXPECT_PRED5(CheckCandidate, candidates_[0],
1013 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1014 kClientAddr);
1015 EXPECT_PRED5(CheckCandidate, candidates_[1],
1016 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1017 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1018 EXPECT_PRED5(CheckCandidate, candidates_[2],
1019 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1020 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1021 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1022
1023 // Don't bother waiting for STUN timeout, since we already verified
1024 // that we got a STUN candidate from the TURN server.
1025}
1026
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001027// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1028// and fail to generate STUN candidate, local UDP candidate is generated
1029// properly.
1030TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1031 allocator().set_flags(allocator().flags() |
1032 cricket::PORTALLOCATOR_DISABLE_RELAY |
1033 cricket::PORTALLOCATOR_DISABLE_TCP |
pthatcherfa301802015-08-11 04:12:56 -07001034 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001035 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1036 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1037 AddInterface(kClientAddr);
1038 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1039 session_->StartGettingPorts();
1040 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1041 EXPECT_EQ(1U, candidates_.size());
1042 EXPECT_PRED5(CheckCandidate, candidates_[0],
1043 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1044 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1045 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1046 EXPECT_EQ(1U, candidates_.size());
1047}
1048
1049// This test verifies allocator can use IPv6 addresses along with IPv4.
1050TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1051 allocator().set_flags(allocator().flags() |
1052 cricket::PORTALLOCATOR_DISABLE_RELAY |
1053 cricket::PORTALLOCATOR_ENABLE_IPV6 |
pthatcherfa301802015-08-11 04:12:56 -07001054 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001055 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1056 AddInterface(kClientIPv6Addr);
1057 AddInterface(kClientAddr);
1058 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1059 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1060 session_->StartGettingPorts();
1061 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1062 EXPECT_EQ(4U, candidates_.size());
1063 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1064 EXPECT_PRED5(CheckCandidate, candidates_[0],
1065 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1066 kClientIPv6Addr);
1067 EXPECT_PRED5(CheckCandidate, candidates_[1],
1068 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1069 kClientAddr);
1070 EXPECT_PRED5(CheckCandidate, candidates_[2],
1071 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1072 kClientIPv6Addr);
1073 EXPECT_PRED5(CheckCandidate, candidates_[3],
1074 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1075 kClientAddr);
1076 EXPECT_EQ(4U, candidates_.size());
1077}
1078
1079// Test that the httpportallocator correctly maintains its lists of stun and
1080// relay servers, by never allowing an empty list.
1081TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1082 rtc::FakeNetworkManager network_manager;
1083 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1084 EXPECT_EQ(1U, alloc.relay_hosts().size());
1085 EXPECT_EQ(1U, alloc.stun_hosts().size());
1086
1087 std::vector<std::string> relay_servers;
1088 std::vector<rtc::SocketAddress> stun_servers;
1089
1090 alloc.SetRelayHosts(relay_servers);
1091 alloc.SetStunHosts(stun_servers);
1092 EXPECT_EQ(1U, alloc.relay_hosts().size());
1093 EXPECT_EQ(1U, alloc.stun_hosts().size());
1094
1095 relay_servers.push_back("1.unittest.corp.google.com");
1096 relay_servers.push_back("2.unittest.corp.google.com");
1097 stun_servers.push_back(
1098 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1099 stun_servers.push_back(
1100 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1101
1102 alloc.SetRelayHosts(relay_servers);
1103 alloc.SetStunHosts(stun_servers);
1104 EXPECT_EQ(2U, alloc.relay_hosts().size());
1105 EXPECT_EQ(2U, alloc.stun_hosts().size());
1106}
1107
1108// Test that the HttpPortAllocator uses correct URL to create sessions.
1109TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1110 rtc::FakeNetworkManager network_manager;
1111 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1112
pthatcherfa301802015-08-11 04:12:56 -07001113 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1114 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001115 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1116 static_cast<cricket::HttpPortAllocatorSession*>(
1117 alloc.CreateSessionInternal(
1118 "test content", 0, kIceUfrag0, kIcePwd0)));
1119 std::string url = session->GetSessionRequestUrl();
1120 LOG(LS_INFO) << "url: " << url;
pthatcherfa301802015-08-11 04:12:56 -07001121 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
1122
1123 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1124 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1125 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
1126 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
1127 url = session->GetSessionRequestUrl();
1128 LOG(LS_INFO) << "url: " << url;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001129 std::vector<std::string> parts;
1130 rtc::split(url, '?', &parts);
1131 ASSERT_EQ(2U, parts.size());
1132
1133 std::vector<std::string> args_parts;
1134 rtc::split(parts[1], '&', &args_parts);
1135
1136 std::map<std::string, std::string> args;
1137 for (std::vector<std::string>::iterator it = args_parts.begin();
1138 it != args_parts.end(); ++it) {
1139 std::vector<std::string> parts;
1140 rtc::split(*it, '=', &parts);
1141 ASSERT_EQ(2U, parts.size());
1142 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1143 }
1144
1145 EXPECT_EQ(kIceUfrag0, args["username"]);
1146 EXPECT_EQ(kIcePwd0, args["password"]);
1147}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001148
1149// Tests that destroying ports with non-shared sockets does not crash.
1150// b/19074679.
1151TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1152 AddInterface(kClientAddr);
1153 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1154 session_->StartGettingPorts();
1155 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1156 EXPECT_EQ(4U, ports_.size());
1157
1158 auto it = ports_.begin();
1159 for (; it != ports_.end(); ++it) {
1160 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1161 }
1162}
honghaizf421bdc2015-07-17 16:21:55 -07001163
1164class AllocationSequenceForTest : public cricket::AllocationSequence {
1165 public:
1166 AllocationSequenceForTest(cricket::BasicPortAllocatorSession* session,
1167 rtc::Network* network,
1168 cricket::PortConfiguration* config,
1169 uint32 flags)
1170 : cricket::AllocationSequence(session, network, config, flags) {}
1171 using cricket::AllocationSequence::CreateTurnPort;
1172};
1173
1174TEST_F(PortAllocatorTest, TestCreateTurnPortWithNullSocket) {
1175 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1176 session_->StartGettingPorts();
1177
1178 cricket::ServerAddresses stun_servers;
1179 stun_servers.insert(kStunAddr);
1180 cricket::PortConfiguration config(stun_servers, kIceUfrag0, kIcePwd0);
1181 rtc::Network network1("test_eth0", "Test Network Adapter 1",
1182 rtc::IPAddress(0x12345600U), 24);
1183 uint32 flag = cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
1184 AllocationSequenceForTest alloc_sequence(
1185 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1186 &network1, &config, flag);
1187 // This simply tests it will not crash if udp_socket_ in the
1188 // AllocationSequence is null, which is chosen in the constructor.
1189 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1190 relay_server.ports.push_back(
1191 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1192 alloc_sequence.CreateTurnPort(relay_server);
1193}