blob: 0eaa49e4d8db8eba3754515546b75172baeb82ea [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
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/basicpacketsocketfactory.h"
kjellanderf4752772016-03-02 05:42:30 -080015#include "webrtc/p2p/base/p2pconstants.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/testrelayserver.h"
18#include "webrtc/p2p/base/teststunserver.h"
19#include "webrtc/p2p/base/testturnserver.h"
20#include "webrtc/p2p/client/basicportallocator.h"
21#include "webrtc/p2p/client/httpportallocator.h"
22#include "webrtc/base/fakenetwork.h"
23#include "webrtc/base/firewallsocketserver.h"
24#include "webrtc/base/gunit.h"
25#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070026#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/natserver.h"
29#include "webrtc/base/natsocketfactory.h"
30#include "webrtc/base/network.h"
31#include "webrtc/base/physicalsocketserver.h"
32#include "webrtc/base/socketaddress.h"
33#include "webrtc/base/ssladapter.h"
34#include "webrtc/base/thread.h"
35#include "webrtc/base/virtualsocketserver.h"
36
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080037using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038using rtc::SocketAddress;
39using rtc::Thread;
40
41static const SocketAddress kClientAddr("11.11.11.11", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070042static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000044static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070045static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
46 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070048static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
49static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
51static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
52static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
53static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
54static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
55static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
56static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
57static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
58static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
59static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
60static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
61
62// Minimum and maximum port for port range tests.
63static const int kMinPort = 10000;
64static const int kMaxPort = 10099;
65
66// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070067static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068// Based on ICE_PWD_LENGTH
69static const char kIcePwd0[] = "TESTICEPWD00000000000000";
70
71static const char kContentName[] = "test content";
72
73static const int kDefaultAllocationTimeout = 1000;
74static const char kTurnUsername[] = "test";
75static const char kTurnPassword[] = "test";
76
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070077// STUN timeout (with all retries) is 9500ms.
78// Add some margin of error for slow bots.
79// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
80// fix flaky tests.
81static const int kStunTimeoutMs = 15000;
82
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083namespace cricket {
84
85// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070086std::ostream& operator<<(std::ostream& os,
87 const std::vector<Candidate>& candidates) {
88 os << '[';
89 bool first = true;
90 for (const Candidate& c : candidates) {
91 if (!first) {
92 os << ", ";
93 }
94 os << c.ToString();
95 first = false;
96 };
97 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 return os;
99}
100
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700101class BasicPortAllocatorTest : public testing::Test,
102 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700104 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105 : pss_(new rtc::PhysicalSocketServer),
106 vss_(new rtc::VirtualSocketServer(pss_.get())),
107 fss_(new rtc::FirewallSocketServer(vss_.get())),
108 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700109 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700110 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700111 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700112 relay_server_(Thread::Current(),
113 kRelayUdpIntAddr,
114 kRelayUdpExtAddr,
115 kRelayTcpIntAddr,
116 kRelayTcpExtAddr,
117 kRelaySslTcpIntAddr,
118 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
120 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700121 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 stun_servers.insert(kStunAddr);
123 // Passing the addresses of GTURN servers will enable GTURN in
124 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700125 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
126 kRelayUdpIntAddr, kRelayTcpIntAddr,
127 kRelaySslTcpIntAddr));
128 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 }
130
131 void AddInterface(const SocketAddress& addr) {
132 network_manager_.AddInterface(addr);
133 }
honghaiz8c404fa2015-09-28 07:59:43 -0700134 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
135 network_manager_.AddInterface(addr, if_name);
136 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800137 void AddInterface(const SocketAddress& addr,
138 const std::string& if_name,
139 rtc::AdapterType type) {
140 network_manager_.AddInterface(addr, if_name, type);
141 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800142 // The default route is the public address that STUN server will observe when
143 // the endpoint is sitting on the public internet and the local port is bound
144 // to the "any" address. This may be different from the default local address
145 // which the endpoint observes. This can occur if the route to the public
146 // endpoint like 8.8.8.8 (specified as the default local address) is
147 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700148 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
149 AddInterface(addr);
150 // When a binding comes from the any address, the |addr| will be used as the
151 // srflx address.
152 vss_->SetDefaultRoute(addr.ipaddr());
153 }
honghaiz8c404fa2015-09-28 07:59:43 -0700154 void RemoveInterface(const SocketAddress& addr) {
155 network_manager_.RemoveInterface(addr);
156 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 bool SetPortRange(int min_port, int max_port) {
158 return allocator_->SetPortRange(min_port, max_port);
159 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700160 // Endpoint is on the public network. No STUN or TURN.
161 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700162 allocator_.reset(new BasicPortAllocator(&network_manager_));
163 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700164 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700165 // Endpoint is behind a NAT, with STUN specified.
166 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
167 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700168 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700169 // Endpoint is on the public network, with STUN specified.
170 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
171 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700173 // Endpoint is on the public network, with TURN specified.
174 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
175 const rtc::SocketAddress& tcp_turn) {
176 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 AddTurnServers(udp_turn, tcp_turn);
178 }
179
180 void AddTurnServers(const rtc::SocketAddress& udp_turn,
181 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700182 RelayServerConfig turn_server(RELAY_TURN);
183 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800184 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185
186 if (!udp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800187 turn_server.ports.push_back(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700188 ProtocolAddress(kTurnUdpIntAddr, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 }
190 if (!tcp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800191 turn_server.ports.push_back(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700192 ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 }
deadbeef653b8e02015-11-11 12:55:10 -0800194 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000195 }
196
197 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700198 session_ = CreateSession("session", component);
199 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700201 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 return true;
203 }
204
205 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700206 session_ = CreateSession("session", content_name, component);
207 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700209 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 return true;
211 }
212
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700213 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
214 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 return CreateSession(sid, kContentName, component);
216 }
217
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700218 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700219 const std::string& sid,
220 const std::string& content_name,
221 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
223 }
224
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700226 const std::string& sid,
227 const std::string& content_name,
228 int component,
229 const std::string& ice_ufrag,
230 const std::string& ice_pwd) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700231 std::unique_ptr<PortAllocatorSession> session = allocator_->CreateSession(
232 sid, content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700234 &BasicPortAllocatorTest::OnPortReady);
235 session->SignalCandidatesReady.connect(
236 this, &BasicPortAllocatorTest::OnCandidatesReady);
237 session->SignalCandidatesAllocationDone.connect(
238 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 return session;
240 }
241
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700242 // Return true if the addresses are the same, or the port is 0 in |pattern|
243 // (acting as a wildcard) and the IPs are the same.
244 // Even with a wildcard port, the port of the address should be nonzero if
245 // the IP is nonzero.
246 static bool AddressMatch(const SocketAddress& address,
247 const SocketAddress& pattern) {
248 return address.ipaddr() == pattern.ipaddr() &&
249 ((pattern.port() == 0 &&
250 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
251 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700253
254 // Find a candidate and return it.
255 static bool FindCandidate(const std::vector<Candidate>& candidates,
256 const std::string& type,
257 const std::string& proto,
258 const SocketAddress& addr,
259 Candidate* found) {
260 auto it = std::find_if(candidates.begin(), candidates.end(),
261 [type, proto, addr](const Candidate& c) {
262 return c.type() == type && c.protocol() == proto &&
263 AddressMatch(c.address(), addr);
264 });
265 if (it != candidates.end() && found) {
266 *found = *it;
267 }
268 return it != candidates.end();
269 }
270
271 // Convenience method to call FindCandidate with no return.
272 static bool HasCandidate(const std::vector<Candidate>& candidates,
273 const std::string& type,
274 const std::string& proto,
275 const SocketAddress& addr) {
276 return FindCandidate(candidates, type, proto, addr, nullptr);
277 }
278
279 // Version of HasCandidate that also takes a related address.
280 static bool HasCandidateWithRelatedAddr(
281 const std::vector<Candidate>& candidates,
282 const std::string& type,
283 const std::string& proto,
284 const SocketAddress& addr,
285 const SocketAddress& related_addr) {
286 auto it =
287 std::find_if(candidates.begin(), candidates.end(),
288 [type, proto, addr, related_addr](const Candidate& c) {
289 return c.type() == type && c.protocol() == proto &&
290 AddressMatch(c.address(), addr) &&
291 AddressMatch(c.related_address(), related_addr);
292 });
293 return it != candidates.end();
294 }
295
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700297 int min_port,
298 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000299 return (addr.port() >= min_port && addr.port() <= max_port);
300 }
301
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700302 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000303 // We should only get this callback once, except in the mux test where
304 // we have multiple port allocation sessions.
305 if (session == session_.get()) {
306 ASSERT_FALSE(candidate_allocation_done_);
307 candidate_allocation_done_ = true;
308 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700309 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000310 }
311
312 // Check if all ports allocated have send-buffer size |expected|. If
313 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
314 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700315 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000316 for (it = ports_.begin(); it < ports_.end(); ++it) {
317 int send_buffer_size;
318 if (expected == -1) {
319 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700320 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000321 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700322 EXPECT_EQ(0,
323 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000324 ASSERT_EQ(expected, send_buffer_size);
325 }
326 }
327 }
328
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700329 // This function starts the port/address gathering and check the existence of
330 // candidates as specified. When |expect_stun_candidate| is true,
331 // |stun_candidate_addr| carries the expected reflective address, which is
332 // also the related address for TURN candidate if it is expected. Otherwise,
333 // it should be ignore.
334 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200335 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700336 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700337 const rtc::IPAddress& stun_candidate_addr,
338 const rtc::IPAddress& relay_candidate_udp_transport_addr,
339 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800340 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
341 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700342 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700343 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700344 }
345 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700346 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
347 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700348 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000349 session_->StartGettingPorts();
350 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
351
Peter Boström0c4e06b2015-10-07 12:23:21 +0200352 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700353 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700354 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800355 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700356 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700357 }
358 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700359 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800360 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700361 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800362 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700363 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
364 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700365 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700366 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700367 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700368 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
369 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
370 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700371 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700372 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700373 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700374 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
375 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
376 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700377 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700378 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000379
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700380 EXPECT_EQ(total_candidates, candidates_.size());
381 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000382 }
383
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700385 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700387 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
389 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 // Make sure the new port is added to ReadyPorts.
391 auto ready_ports = ses->ReadyPorts();
392 EXPECT_NE(ready_ports.end(),
393 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700395 void OnCandidatesReady(PortAllocatorSession* ses,
396 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700397 for (const Candidate& candidate : candidates) {
398 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
399 // Sanity check that the ICE component is set.
400 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
401 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000402 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700403 // Make sure the new candidates are added to Candidates.
404 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700405 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700406 EXPECT_NE(
407 ses_candidates.end(),
408 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
409 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 }
411
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700412 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800413 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700414 RelayServerConfig server_config = allocator_->turn_servers()[i];
415 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000416 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700417 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 if (proto_addr.address == relay_port->address &&
419 proto_addr.proto == relay_port->proto)
420 return true;
421 }
422 }
423 return false;
424 }
425
Guo-wei Shieh11477022015-08-15 09:28:41 -0700426 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
427 bool with_nat) {
428 if (with_nat) {
429 nat_server_.reset(new rtc::NATServer(
430 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
431 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
432 } else {
433 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
434 }
435
436 ServerAddresses stun_servers;
437 if (!stun_server.IsNil()) {
438 stun_servers.insert(stun_server);
439 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700440 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700441 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700442 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700443 }
444
kwiberg3ec46792016-04-27 07:22:53 -0700445 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
446 std::unique_ptr<rtc::VirtualSocketServer> vss_;
447 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000448 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700449 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000450 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700451 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700452 std::unique_ptr<TestStunServer> stun_server_;
453 TestRelayServer relay_server_;
454 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000455 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700456 std::unique_ptr<BasicPortAllocator> allocator_;
457 std::unique_ptr<PortAllocatorSession> session_;
458 std::vector<PortInterface*> ports_;
459 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 bool candidate_allocation_done_;
461};
462
463// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700464TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000465 EXPECT_EQ(&network_manager_, allocator().network_manager());
466 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800467 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700468 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800470 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
471 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700472 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
473 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
474 EXPECT_TRUE(
475 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
476 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700477 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000478}
479
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800480// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700481TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800482 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
483 rtc::ADAPTER_TYPE_ETHERNET);
484 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
485 rtc::ADAPTER_TYPE_WIFI);
486 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
487 rtc::ADAPTER_TYPE_CELLULAR);
488 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
489 rtc::ADAPTER_TYPE_VPN);
490 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
491 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700492 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
493 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
494 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800495 session_->StartGettingPorts();
496 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
497 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700498 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800499 EXPECT_LT(candidate.address().ip(), 0x12345604U);
500 }
501}
502
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700503TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800504 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
505 rtc::ADAPTER_TYPE_ETHERNET);
506 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
507 rtc::ADAPTER_TYPE_WIFI);
508 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
509 rtc::ADAPTER_TYPE_CELLULAR);
510 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
511 rtc::ADAPTER_TYPE_LOOPBACK |
512 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700513 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
514 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
515 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800516 session_->StartGettingPorts();
517 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
518 EXPECT_EQ(1U, candidates_.size());
519 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
520}
521
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700523TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700524 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000525 session_->StartGettingPorts();
526 // Waiting for one second to make sure BasicPortAllocatorSession has not
527 // called OnAllocate multiple times. In old behavior it's called every 250ms.
528 // When there are no network interfaces, each execution of OnAllocate will
529 // result in SignalCandidatesAllocationDone signal.
530 rtc::Thread::Current()->ProcessMessages(1000);
531 EXPECT_TRUE(candidate_allocation_done_);
532 EXPECT_EQ(0U, candidates_.size());
533}
534
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700535// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700536TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800537 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
538 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700539 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
540 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
541 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700542 session_->StartGettingPorts();
543 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
544 EXPECT_EQ(1U, candidates_.size());
545}
546
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000547// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700548TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000549 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700550 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000551 session_->StartGettingPorts();
552 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
553 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700554 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
555 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
556 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
557 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
558 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
559 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
560 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
561 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000562 EXPECT_TRUE(candidate_allocation_done_);
563}
564
honghaiz8c404fa2015-09-28 07:59:43 -0700565// Test that when the same network interface is brought down and up, the
566// port allocator session will restart a new allocation sequence if
567// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700568TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700569 std::string if_name("test_net0");
570 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700571 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700572 session_->StartGettingPorts();
573 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
574 EXPECT_EQ(4U, ports_.size());
575 EXPECT_TRUE(candidate_allocation_done_);
576 candidate_allocation_done_ = false;
577 candidates_.clear();
578 ports_.clear();
579
580 RemoveInterface(kClientAddr);
581 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
582 EXPECT_EQ(0U, ports_.size());
583 EXPECT_FALSE(candidate_allocation_done_);
584
585 // When the same interfaces are added again, new candidates/ports should be
586 // generated.
587 AddInterface(kClientAddr, if_name);
588 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
589 EXPECT_EQ(4U, ports_.size());
590 EXPECT_TRUE(candidate_allocation_done_);
591}
592
593// Test that when the same network interface is brought down and up, the
594// port allocator session will not restart a new allocation sequence if
595// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700596TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700597 std::string if_name("test_net0");
598 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700599 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700600 session_->StartGettingPorts();
601 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
602 EXPECT_EQ(4U, ports_.size());
603 EXPECT_TRUE(candidate_allocation_done_);
604 session_->StopGettingPorts();
605 candidates_.clear();
606 ports_.clear();
607
608 RemoveInterface(kClientAddr);
609 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
610 EXPECT_EQ(0U, ports_.size());
611
612 // When the same interfaces are added again, new candidates/ports should not
613 // be generated because the session has stopped.
614 AddInterface(kClientAddr, if_name);
615 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
616 EXPECT_EQ(0U, ports_.size());
617 EXPECT_TRUE(candidate_allocation_done_);
618}
619
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000620// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700621TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000622 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700623 allocator_->set_step_delay(kDefaultStepDelay);
624 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000625 session_->StartGettingPorts();
626 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
627 EXPECT_EQ(2U, ports_.size());
628 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
629 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700630 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
631 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000632 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700633 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
634 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000635 EXPECT_EQ(4U, ports_.size());
636 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700637 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
638 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000639 EXPECT_EQ(4U, ports_.size());
640 EXPECT_TRUE(candidate_allocation_done_);
641 // If we Stop gathering now, we shouldn't get a second "done" callback.
642 session_->StopGettingPorts();
643}
644
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700645TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000646 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700647 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000648 session_->StartGettingPorts();
649 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
650 EXPECT_TRUE(candidate_allocation_done_);
651 // If we Stop gathering now, we shouldn't get a second "done" callback.
652 session_->StopGettingPorts();
653
654 // All ports should have unset send-buffer sizes.
655 CheckSendBufferSizesOfAllPorts(-1);
656}
657
658// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700659TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000660 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700661 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000662 session_->StartGettingPorts();
663 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
664 EXPECT_EQ(2U, ports_.size());
665 session_->StopGettingPorts();
666 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
667}
668
669// Test that we restrict client ports appropriately when a port range is set.
670// We check the candidates for udp/stun/tcp ports, and the from address
671// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700672TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000673 AddInterface(kClientAddr);
674 // Check that an invalid port range fails.
675 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
676 // Check that a null port range succeeds.
677 EXPECT_TRUE(SetPortRange(0, 0));
678 // Check that a valid port range succeeds.
679 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700680 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000681 session_->StartGettingPorts();
682 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
683 EXPECT_EQ(4U, ports_.size());
684 // Check the port number for the UDP port object.
685 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
686 // Check the port number for the STUN port object.
687 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
688 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700689 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
690 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000691 // Check the port number for the TCP port object.
692 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
693 EXPECT_TRUE(candidate_allocation_done_);
694}
695
696// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700697TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700698 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000699 session_->StartGettingPorts();
700 rtc::Thread::Current()->ProcessMessages(100);
701 // Without network adapter, we should not get any candidate.
702 EXPECT_EQ(0U, candidates_.size());
703 EXPECT_TRUE(candidate_allocation_done_);
704}
705
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700706// Test that when enumeration is disabled, we should not have any ports when
707// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700709 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700710 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700711 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700712 // Expect to see no ports and no candidates.
713 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
714 rtc::IPAddress(), rtc::IPAddress());
715}
716
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800717// Test that even with multiple interfaces, the result should still be a single
718// default private, one STUN and one TURN candidate since we bind to any address
719// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700721 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000722 AddInterface(kPrivateAddr);
723 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700724 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000725 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800726
727 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
728 // address set and we have no IPv6 STUN server, there should be no IPv6
729 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700730 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
731 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800732
733 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
734 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
735 // TURN/UDP candidates.
736 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800737 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
738 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700739}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000740
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800741// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
742// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700743TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700744 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800745 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700746 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700747 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800748 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
749 // private, STUN, TURN/UDP, and TURN/TCP candidates.
750 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
751 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700752 kTurnUdpExtAddr.ipaddr());
753}
754
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800755// Test that when adapter enumeration is disabled, for endpoints without
756// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700757TEST_F(BasicPortAllocatorTest,
758 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800759 ResetWithNoServersOrNat();
760 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
761 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
762 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700763}
764
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800765// Test that when adapter enumeration is disabled, with
766// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
767// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700768TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800769 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
770 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700771 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
772 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800773 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
774 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700775 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700776 rtc::IPAddress(), rtc::IPAddress());
777}
778
779// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800780// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
781// a NAT, there is no local candidate. However, this specified default route
782// (kClientAddr) which was discovered when sending STUN requests, will become
783// the srflx addresses.
784TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700785 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800786 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700787 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800788 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700789 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
790 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700791 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
792 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800793 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
794 rtc::IPAddress(), rtc::IPAddress());
795}
796
797// Test that when adapter enumeration is disabled, with
798// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
799// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700800TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800801 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
802 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700803 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
804 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800805 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
806 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
807 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000808}
809
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700810// Test that we disable relay over UDP, and only TCP is used when connecting to
811// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700812TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700813 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700814 AddInterface(kClientAddr);
815 ResetWithStunServerAndNat(kStunAddr);
816 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700817 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
818 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
819 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
820 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700821
822 session_->StartGettingPorts();
823 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
824
825 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
826 // TURN/TCP candidates.
827 EXPECT_EQ(2U, ports_.size());
828 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700829 Candidate turn_candidate;
830 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
831 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700832 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700833 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
834 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700835}
836
Erik Språngefdce692015-06-05 09:41:26 +0200837// Disable for asan, see
838// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
839#if !defined(ADDRESS_SANITIZER)
840
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000841// Test that we can get OnCandidatesAllocationDone callback when all the ports
842// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700843TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700845 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
846 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
847 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000848 session_->StartGettingPorts();
849 rtc::Thread::Current()->ProcessMessages(100);
850 EXPECT_EQ(0U, candidates_.size());
851 EXPECT_TRUE(candidate_allocation_done_);
852}
853
854// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700855TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000856 AddInterface(kClientAddr);
857 fss_->set_udp_sockets_enabled(false);
858 EXPECT_TRUE(CreateSession(1));
859 session_->StartGettingPorts();
860 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
861 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700862 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
863 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
864 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
865 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
866 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
867 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000868 EXPECT_TRUE(candidate_allocation_done_);
869}
870
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700871#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200872
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873// Test that we don't crash or malfunction if we can't create UDP sockets or
874// listen on TCP sockets. We still give out a local TCP address, since
875// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700876TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000877 AddInterface(kClientAddr);
878 fss_->set_udp_sockets_enabled(false);
879 fss_->set_tcp_listen_enabled(false);
880 EXPECT_TRUE(CreateSession(1));
881 session_->StartGettingPorts();
882 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
883 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700884 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
885 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
886 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
887 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
888 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700889 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000890 EXPECT_TRUE(candidate_allocation_done_);
891}
892
893// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700894// TODO(deadbeef): Find a way to exit early here.
895TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000896 AddInterface(kClientAddr);
897 fss_->set_tcp_sockets_enabled(false);
898 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700899 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 session_->StartGettingPorts();
901 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700902 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 // In case of Relay, ports creation will succeed but sockets will fail.
904 // There is no error reporting from RelayEntry to handle this failure.
905}
906
907// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700908TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000909 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
910 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700911 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000912 session_->StartGettingPorts();
913 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
914 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700915 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
916 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000917 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
918 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -0700919 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
920 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000921 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700922 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
923 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
924 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
925 kRelaySslTcpIntAddr);
926 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -0700927 // Stun Timeout is 9.5sec.
928 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
929 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930}
931
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700932TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 AddInterface(kClientAddr);
934 AddInterface(kClientAddr2);
935 // Allocating only host UDP ports. This is done purely for testing
936 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700937 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
938 PORTALLOCATOR_DISABLE_RELAY);
939 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940 session_->StartGettingPorts();
941 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
942 ASSERT_EQ(2U, candidates_.size());
943 EXPECT_EQ(2U, ports_.size());
944 // Candidates priorities should be different.
945 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
946}
947
948// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700949TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000950 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700951 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000952 session_->StartGettingPorts();
953 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
954 EXPECT_EQ(4U, ports_.size());
955 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700956 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000957}
958
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700959// Test that the allocator session uses the candidate filter it's created with,
960// rather than the filter of its parent allocator.
961// The filter of the allocator should only affect the next gathering phase,
962// according to JSEP, which means the *next* allocator session returned.
963TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
964 AddInterface(kClientAddr);
965 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
966 // Set candidate filter *after* creating the session. Should have no effect.
967 allocator().set_candidate_filter(CF_RELAY);
968 session_->StartGettingPorts();
969 // 7 candidates and 4 ports is what we would normally get (see the
970 // TestGetAllPorts* tests).
971 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
972 EXPECT_EQ(4U, ports_.size());
973 EXPECT_TRUE(candidate_allocation_done_);
974}
975
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000976// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
977// This test also verifies that when the allocator is only allowed to use
978// relay (i.e. IceTransportsType is relay), the raddr is an empty
979// address with the correct family. This is to prevent any local
980// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700981TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000982 AddInterface(kClientAddr);
983 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700984 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700985 allocator().set_candidate_filter(CF_RELAY);
986 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000987 session_->StartGettingPorts();
988 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700989 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
990 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991
992 EXPECT_EQ(1U, candidates_.size());
993 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700994 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
995 EXPECT_EQ(
996 candidates_[0].related_address(),
997 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000998}
999
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001000TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001001 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001002 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1003 allocator().set_candidate_filter(CF_HOST);
1004 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001005 session_->StartGettingPorts();
1006 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001007 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1008 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001009 for (const Candidate& candidate : candidates_) {
1010 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001011 }
1012}
1013
1014// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001015TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001016 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001017 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001018
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001019 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1020 allocator().set_candidate_filter(CF_REFLEXIVE);
1021 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001022 session_->StartGettingPorts();
1023 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1024 // Host is behind NAT, no private address will be exposed. Hence only UDP
1025 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001026 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1027 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001028 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1029 EXPECT_EQ(
1030 candidates_[0].related_address(),
1031 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001032}
1033
1034// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001035TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001036 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001037 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1038 allocator().set_candidate_filter(CF_REFLEXIVE);
1039 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001040 session_->StartGettingPorts();
1041 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1042 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001043 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001044 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001045 for (const Candidate& candidate : candidates_) {
1046 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001047 }
1048}
1049
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001050// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001051TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001053 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001054 session_->StartGettingPorts();
1055 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001056 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1057 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1058 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001059 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001060 for (const Candidate& candidate : candidates_) {
1061 EXPECT_EQ(kIceUfrag0, candidate.username());
1062 EXPECT_EQ(kIcePwd0, candidate.password());
1063 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001064 EXPECT_TRUE(candidate_allocation_done_);
1065}
1066
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001067// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1068// is allocated for udp and stun. Also verify there is only one candidate
1069// (local) if stun candidate is same as local candidate, which will be the case
1070// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001071TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001072 AddInterface(kClientAddr);
1073 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001074 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1075 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001076 session_->StartGettingPorts();
1077 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1078 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001079 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001080 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1081}
1082
1083// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1084// is allocated for udp and stun. In this test we should expect both stun and
1085// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001086TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001087 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001088 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001089
1090 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001091 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1092 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001093 session_->StartGettingPorts();
1094 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1095 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001096 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1097 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1098 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001099 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1100 EXPECT_EQ(3U, candidates_.size());
1101}
1102
deadbeefc5d0d952015-07-16 10:22:21 -07001103// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001104TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001105 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001107 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001108
1109 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1110
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001111 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001112 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001113 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1114 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001115
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001116 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001117 session_->StartGettingPorts();
1118
1119 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1120 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001121 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1122 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1123 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1124 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1125 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001126 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1127 EXPECT_EQ(3U, candidates_.size());
1128}
1129
1130// Testing DNS resolve for the TURN server, this will test AllocationSequence
1131// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001132TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001133 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001134 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001135 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001136 allocator_.reset(new BasicPortAllocator(&network_manager_));
1137 RelayServerConfig turn_server(RELAY_TURN);
1138 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001139 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001140 turn_server.ports.push_back(
1141 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001142 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001144 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001146 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1147 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001148
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001149 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001150 session_->StartGettingPorts();
1151
1152 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1153}
1154
1155// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1156// is allocated for udp/stun/turn. In this test we should expect all local,
1157// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001158TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001160 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001161
1162 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1163
1164 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001165 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1166 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001167
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001168 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001169 session_->StartGettingPorts();
1170
1171 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1172 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001173 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1174 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1175 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1176 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1177 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001178 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1179 EXPECT_EQ(3U, candidates_.size());
1180 // Local port will be created first and then TURN port.
1181 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1182 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1183}
1184
1185// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1186// server is also used as the STUN server, we should get 'local', 'stun', and
1187// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001188TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001190 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001191 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1193
1194 // Must set the step delay to 0 to make sure the relay allocation phase is
1195 // started before the STUN candidates are obtained, so that the STUN binding
1196 // response is processed when both StunPort and TurnPort exist to reproduce
1197 // webrtc issue 3537.
1198 allocator_->set_step_delay(0);
1199 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001200 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1201 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001202
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001203 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001204 session_->StartGettingPorts();
1205
1206 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001207 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1208 Candidate stun_candidate;
1209 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1210 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1211 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1212 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1213 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001214
1215 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1216 EXPECT_EQ(3U, candidates_.size());
1217 // Local port will be created first and then TURN port.
1218 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1219 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1220}
1221
deadbeefc5d0d952015-07-16 10:22:21 -07001222// Test that when only a TCP TURN server is available, we do NOT use it as
1223// a UDP STUN server, as this could leak our IP address. Thus we should only
1224// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001225TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001226 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001227 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001228 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001229 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1230
1231 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001232 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1233 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001234
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001235 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001236 session_->StartGettingPorts();
1237
1238 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1239 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001240 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1241 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1242 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001243 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1244 EXPECT_EQ(2U, candidates_.size());
1245 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1246 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1247}
1248
1249// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1250// TURN server is used as the STUN server and we get 'local', 'stun', and
1251// 'relay' candidates.
1252// TODO(deadbeef): Remove this test when support for non-shared socket mode
1253// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001254TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001255 AddInterface(kClientAddr);
1256 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001257 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001258 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1259
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001260 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001261
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001262 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001263 session_->StartGettingPorts();
1264
1265 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1266 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001267 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1268 Candidate stun_candidate;
1269 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1270 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1271 Candidate turn_candidate;
1272 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1273 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1274 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001275 // Not using shared socket, so the STUN request's server reflexive address
1276 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001277 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001278
1279 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1280 EXPECT_EQ(3U, candidates_.size());
1281 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1282 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1283 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1284}
1285
1286// Test that even when both a STUN and TURN server are configured, the TURN
1287// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001288TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001289 AddInterface(kClientAddr);
1290 // Configure with STUN server but destroy it, so we can ensure that it's
1291 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001292 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001293 stun_server_.reset();
1294 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1295
1296 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001297 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1298 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001299
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001300 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001301 session_->StartGettingPorts();
1302
1303 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001304 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1305 Candidate stun_candidate;
1306 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1307 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1308 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1309 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1310 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001311
1312 // Don't bother waiting for STUN timeout, since we already verified
1313 // that we got a STUN candidate from the TURN server.
1314}
1315
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1317// and fail to generate STUN candidate, local UDP candidate is generated
1318// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001319TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001320 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1321 PORTALLOCATOR_DISABLE_TCP |
1322 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001323 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1324 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001325 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001326 session_->StartGettingPorts();
1327 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1328 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001329 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001330 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1331 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332 EXPECT_EQ(1U, candidates_.size());
1333}
1334
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001335// Test that when the NetworkManager doesn't have permission to enumerate
1336// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1337// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001338TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001339 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1340 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001341 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001342 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001343 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1344 PORTALLOCATOR_DISABLE_TCP |
1345 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1346 EXPECT_EQ(0U,
1347 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1348 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1349 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001350 session_->StartGettingPorts();
1351 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001352 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001353 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001354 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001355}
1356
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001357// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001358TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001359 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1360 PORTALLOCATOR_ENABLE_IPV6 |
1361 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001362 AddInterface(kClientIPv6Addr);
1363 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001364 allocator_->set_step_delay(kMinimumStepDelay);
1365 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001366 session_->StartGettingPorts();
1367 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1368 EXPECT_EQ(4U, candidates_.size());
1369 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001370 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1371 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1372 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1373 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001374 EXPECT_EQ(4U, candidates_.size());
1375}
honghaiz98db68f2015-09-29 07:58:17 -07001376
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001377TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001378 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001379 allocator_->set_step_delay(kDefaultStepDelay);
1380 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001381 session_->StartGettingPorts();
1382 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1383 EXPECT_EQ(2U, ports_.size());
1384 session_->StopGettingPorts();
1385 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1386
1387 // After stopping getting ports, adding a new interface will not start
1388 // getting ports again.
1389 candidates_.clear();
1390 ports_.clear();
1391 candidate_allocation_done_ = false;
1392 network_manager_.AddInterface(kClientAddr2);
1393 rtc::Thread::Current()->ProcessMessages(1000);
1394 EXPECT_EQ(0U, candidates_.size());
1395 EXPECT_EQ(0U, ports_.size());
1396}
1397
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001398TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001399 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001400 allocator_->set_step_delay(kDefaultStepDelay);
1401 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001402 session_->StartGettingPorts();
1403 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1404 EXPECT_EQ(2U, ports_.size());
1405 session_->ClearGettingPorts();
1406 WAIT(candidate_allocation_done_, 1000);
1407 EXPECT_FALSE(candidate_allocation_done_);
1408
1409 // After clearing getting ports, adding a new interface will start getting
1410 // ports again.
1411 candidates_.clear();
1412 ports_.clear();
1413 candidate_allocation_done_ = false;
1414 network_manager_.AddInterface(kClientAddr2);
1415 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1416 EXPECT_EQ(2U, ports_.size());
1417}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001418
1419// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1420// a pooled session is taken out of the pool.
1421TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1422 AddInterface(kClientAddr);
1423 int pool_size = 1;
1424 allocator_->SetConfiguration(allocator_->stun_servers(),
1425 allocator_->turn_servers(), pool_size);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001426 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001427 ASSERT_NE(nullptr, peeked_session);
1428 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1429 kDefaultAllocationTimeout);
1430 // Expect that when TakePooledSession is called,
1431 // UpdateTransportInformationInternal will be called and the
1432 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1433 // candidates.
1434 session_ =
1435 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1436 ASSERT_NE(nullptr, session_.get());
1437 auto ready_ports = session_->ReadyPorts();
1438 auto candidates = session_->ReadyCandidates();
1439 EXPECT_FALSE(ready_ports.empty());
1440 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001441 for (const PortInterface* port_interface : ready_ports) {
1442 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001443 EXPECT_EQ(kContentName, port->content_name());
1444 EXPECT_EQ(1, port->component());
1445 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1446 EXPECT_EQ(kIcePwd0, port->password());
1447 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001448 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001449 EXPECT_EQ(1, candidate.component());
1450 EXPECT_EQ(kIceUfrag0, candidate.username());
1451 EXPECT_EQ(kIcePwd0, candidate.password());
1452 }
1453}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001454
1455// Test that a new candidate filter takes effect even on already-gathered
1456// candidates.
1457TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1458 AddInterface(kClientAddr);
1459 int pool_size = 1;
1460 allocator_->SetConfiguration(allocator_->stun_servers(),
1461 allocator_->turn_servers(), pool_size);
1462 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1463 ASSERT_NE(nullptr, peeked_session);
1464 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1465 kDefaultAllocationTimeout);
1466 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1467 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1468 allocator_->set_candidate_filter(CF_RELAY);
1469 // Assume that when TakePooledSession is called, the candidate filter will be
1470 // applied to the pooled session. This is tested by PortAllocatorTest.
1471 session_ =
1472 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1473 ASSERT_NE(nullptr, session_.get());
1474 auto candidates = session_->ReadyCandidates();
1475 auto ports = session_->ReadyPorts();
1476 // Sanity check that the number of candidates and ports decreased.
1477 EXPECT_GT(initial_candidates_size, candidates.size());
1478 EXPECT_GT(initial_ports_size, ports.size());
1479 for (const PortInterface* port : ports) {
1480 // Expect only relay ports.
1481 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1482 }
1483 for (const Candidate& candidate : candidates) {
1484 // Expect only relay candidates now that the filter is applied.
1485 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1486 // Expect that the raddr is emptied due to the CF_RELAY filter.
1487 EXPECT_EQ(candidate.related_address(),
1488 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1489 }
1490}
1491
1492} // namespace cricket