blob: c85a73385b4ff2fb3910e48e14c89b01e42d08f0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/basicpacketsocketfactory.h"
15#include "p2p/base/p2pconstants.h"
16#include "p2p/base/p2ptransportchannel.h"
17#include "p2p/base/testrelayserver.h"
18#include "p2p/base/teststunserver.h"
19#include "p2p/base/testturnserver.h"
20#include "p2p/client/basicportallocator.h"
21#include "rtc_base/fakeclock.h"
22#include "rtc_base/fakenetwork.h"
23#include "rtc_base/firewallsocketserver.h"
24#include "rtc_base/gunit.h"
25#include "rtc_base/helpers.h"
26#include "rtc_base/ipaddress.h"
27#include "rtc_base/logging.h"
28#include "rtc_base/natserver.h"
29#include "rtc_base/natsocketfactory.h"
30#include "rtc_base/nethelpers.h"
31#include "rtc_base/network.h"
32#include "rtc_base/socketaddress.h"
33#include "rtc_base/ssladapter.h"
34#include "rtc_base/thread.h"
35#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036
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
deadbeef9a6f4d42017-05-15 19:43:33 -070041#define MAYBE_SKIP_IPV4 \
42 if (!rtc::HasIPv4Enabled()) { \
43 LOG(LS_INFO) << "No IPv4... skipping"; \
44 return; \
45 }
46
deadbeefe97389c2016-12-23 01:43:45 -080047static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070049static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070050static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000052static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070053static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
54 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070055static const SocketAddress kClientIPv6Addr2(
56 "2401:fa00:4:2000:be30:5bff:fee5:c3",
57 0);
deadbeef3427f532017-07-26 16:09:33 -070058static const SocketAddress kClientIPv6Addr3(
59 "2401:fa00:4:3000:be30:5bff:fee5:c3",
60 0);
deadbeefc5d0d952015-07-16 10:22:21 -070061static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
62static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
64static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
65static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
66static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
67static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
68static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
69static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
70static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
71static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070072static const SocketAddress kTurnUdpIntIPv6Addr(
73 "2402:fb00:4:1000:be30:5bff:fee5:c3",
74 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070076static const SocketAddress kTurnTcpIntIPv6Addr(
77 "2402:fb00:4:2000:be30:5bff:fee5:c3",
78 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
80
81// Minimum and maximum port for port range tests.
82static const int kMinPort = 10000;
83static const int kMaxPort = 10099;
84
85// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070086static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087// Based on ICE_PWD_LENGTH
88static const char kIcePwd0[] = "TESTICEPWD00000000000000";
89
90static const char kContentName[] = "test content";
91
Honghai Zhangc67e0f52016-09-19 16:57:37 -070092static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093static const char kTurnUsername[] = "test";
94static const char kTurnPassword[] = "test";
95
pthatcher94a2f212017-02-08 14:42:22 -080096// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070097// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080098static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070099
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100namespace cricket {
101
102// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700103std::ostream& operator<<(std::ostream& os,
104 const std::vector<Candidate>& candidates) {
105 os << '[';
106 bool first = true;
107 for (const Candidate& c : candidates) {
108 if (!first) {
109 os << ", ";
110 }
111 os << c.ToString();
112 first = false;
113 };
114 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 return os;
116}
117
pthatcher1749bc32017-02-08 13:18:00 -0800118class BasicPortAllocatorTestBase : public testing::Test,
119 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 public:
pthatcher1749bc32017-02-08 13:18:00 -0800121 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700122 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700124 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800125 // Note that the NAT is not used by default. ResetWithStunServerAndNat
126 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700127 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700128 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700129 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700130 relay_server_(Thread::Current(),
131 kRelayUdpIntAddr,
132 kRelayUdpExtAddr,
133 kRelayTcpIntAddr,
134 kRelayTcpExtAddr,
135 kRelaySslTcpIntAddr,
136 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
138 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700139 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 stun_servers.insert(kStunAddr);
141 // Passing the addresses of GTURN servers will enable GTURN in
142 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800143 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
144 // configuration should be blank by default (preferred), or it should use
145 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700146 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
147 kRelayUdpIntAddr, kRelayTcpIntAddr,
148 kRelaySslTcpIntAddr));
149 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150 }
151
152 void AddInterface(const SocketAddress& addr) {
153 network_manager_.AddInterface(addr);
154 }
honghaiz8c404fa2015-09-28 07:59:43 -0700155 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
156 network_manager_.AddInterface(addr, if_name);
157 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800158 void AddInterface(const SocketAddress& addr,
159 const std::string& if_name,
160 rtc::AdapterType type) {
161 network_manager_.AddInterface(addr, if_name, type);
162 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800163 // The default route is the public address that STUN server will observe when
164 // the endpoint is sitting on the public internet and the local port is bound
165 // to the "any" address. This may be different from the default local address
166 // which the endpoint observes. This can occur if the route to the public
167 // endpoint like 8.8.8.8 (specified as the default local address) is
168 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700169 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
170 AddInterface(addr);
171 // When a binding comes from the any address, the |addr| will be used as the
172 // srflx address.
173 vss_->SetDefaultRoute(addr.ipaddr());
174 }
honghaiz8c404fa2015-09-28 07:59:43 -0700175 void RemoveInterface(const SocketAddress& addr) {
176 network_manager_.RemoveInterface(addr);
177 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000178 bool SetPortRange(int min_port, int max_port) {
179 return allocator_->SetPortRange(min_port, max_port);
180 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700181 // Endpoint is on the public network. No STUN or TURN.
182 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700183 allocator_.reset(new BasicPortAllocator(&network_manager_));
184 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700185 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700186 // Endpoint is behind a NAT, with STUN specified.
187 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
188 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700189 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700190 // Endpoint is on the public network, with STUN specified.
191 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
192 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700194 // Endpoint is on the public network, with TURN specified.
195 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
196 const rtc::SocketAddress& tcp_turn) {
197 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 AddTurnServers(udp_turn, tcp_turn);
199 }
200
201 void AddTurnServers(const rtc::SocketAddress& udp_turn,
202 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700203 RelayServerConfig turn_server(RELAY_TURN);
204 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800205 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206
207 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800208 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 }
210 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800211 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 }
deadbeef653b8e02015-11-11 12:55:10 -0800213 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 }
215
216 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700217 session_ = CreateSession("session", component);
218 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700220 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return true;
222 }
223
224 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 session_ = CreateSession("session", content_name, component);
226 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700228 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 return true;
230 }
231
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700232 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
233 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 return CreateSession(sid, kContentName, component);
235 }
236
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700237 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700238 const std::string& sid,
239 const std::string& content_name,
240 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
242 }
243
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700244 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700245 const std::string& sid,
246 const std::string& content_name,
247 int component,
248 const std::string& ice_ufrag,
249 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700250 std::unique_ptr<PortAllocatorSession> session =
251 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800253 &BasicPortAllocatorTestBase::OnPortReady);
254 session->SignalPortsPruned.connect(
255 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700256 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800257 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700258 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800259 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700260 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800261 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 return session;
263 }
264
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700265 // Return true if the addresses are the same, or the port is 0 in |pattern|
266 // (acting as a wildcard) and the IPs are the same.
267 // Even with a wildcard port, the port of the address should be nonzero if
268 // the IP is nonzero.
269 static bool AddressMatch(const SocketAddress& address,
270 const SocketAddress& pattern) {
271 return address.ipaddr() == pattern.ipaddr() &&
272 ((pattern.port() == 0 &&
273 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
274 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700276
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700277 // Returns the number of ports that have matching type, protocol and
278 // address.
279 static int CountPorts(const std::vector<PortInterface*>& ports,
280 const std::string& type,
281 ProtocolType protocol,
282 const SocketAddress& client_addr) {
283 return std::count_if(
284 ports.begin(), ports.end(),
285 [type, protocol, client_addr](PortInterface* port) {
286 return port->Type() == type && port->GetProtocol() == protocol &&
287 port->Network()->GetBestIP() == client_addr.ipaddr();
288 });
289 }
290
deadbeefe97389c2016-12-23 01:43:45 -0800291 static int CountCandidates(const std::vector<Candidate>& candidates,
292 const std::string& type,
293 const std::string& proto,
294 const SocketAddress& addr) {
295 return std::count_if(candidates.begin(), candidates.end(),
296 [type, proto, addr](const Candidate& c) {
297 return c.type() == type && c.protocol() == proto &&
298 AddressMatch(c.address(), addr);
299 });
300 }
301
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700302 // Find a candidate and return it.
303 static bool FindCandidate(const std::vector<Candidate>& candidates,
304 const std::string& type,
305 const std::string& proto,
306 const SocketAddress& addr,
307 Candidate* found) {
308 auto it = std::find_if(candidates.begin(), candidates.end(),
309 [type, proto, addr](const Candidate& c) {
310 return c.type() == type && c.protocol() == proto &&
311 AddressMatch(c.address(), addr);
312 });
313 if (it != candidates.end() && found) {
314 *found = *it;
315 }
316 return it != candidates.end();
317 }
318
319 // Convenience method to call FindCandidate with no return.
320 static bool HasCandidate(const std::vector<Candidate>& candidates,
321 const std::string& type,
322 const std::string& proto,
323 const SocketAddress& addr) {
324 return FindCandidate(candidates, type, proto, addr, nullptr);
325 }
326
327 // Version of HasCandidate that also takes a related address.
328 static bool HasCandidateWithRelatedAddr(
329 const std::vector<Candidate>& candidates,
330 const std::string& type,
331 const std::string& proto,
332 const SocketAddress& addr,
333 const SocketAddress& related_addr) {
334 auto it =
335 std::find_if(candidates.begin(), candidates.end(),
336 [type, proto, addr, related_addr](const Candidate& c) {
337 return c.type() == type && c.protocol() == proto &&
338 AddressMatch(c.address(), addr) &&
339 AddressMatch(c.related_address(), related_addr);
340 });
341 return it != candidates.end();
342 }
343
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700345 int min_port,
346 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000347 return (addr.port() >= min_port && addr.port() <= max_port);
348 }
349
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700350 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000351 // We should only get this callback once, except in the mux test where
352 // we have multiple port allocation sessions.
353 if (session == session_.get()) {
354 ASSERT_FALSE(candidate_allocation_done_);
355 candidate_allocation_done_ = true;
356 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700357 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000358 }
359
360 // Check if all ports allocated have send-buffer size |expected|. If
361 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
362 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700363 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 for (it = ports_.begin(); it < ports_.end(); ++it) {
365 int send_buffer_size;
366 if (expected == -1) {
367 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700368 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700370 EXPECT_EQ(0,
371 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000372 ASSERT_EQ(expected, send_buffer_size);
373 }
374 }
375 }
376
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700377 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
378
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000379 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700380 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700382 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
384 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700385 // Make sure the new port is added to ReadyPorts.
386 auto ready_ports = ses->ReadyPorts();
387 EXPECT_NE(ready_ports.end(),
388 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700390 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700391 const std::vector<PortInterface*>& pruned_ports) {
392 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700393 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700394 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700395 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700396 new_end = std::remove(ports_.begin(), new_end, port);
397 // Make sure the pruned port is not in ReadyPorts.
398 EXPECT_EQ(ready_ports.end(),
399 std::find(ready_ports.begin(), ready_ports.end(), port));
400 }
401 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700402 }
403
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700404 void OnCandidatesReady(PortAllocatorSession* ses,
405 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700406 for (const Candidate& candidate : candidates) {
407 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
408 // Sanity check that the ICE component is set.
409 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
410 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700412 // Make sure the new candidates are added to Candidates.
413 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700414 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700415 EXPECT_NE(
416 ses_candidates.end(),
417 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
418 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000419 }
420
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700421 void OnCandidatesRemoved(PortAllocatorSession* session,
422 const std::vector<Candidate>& removed_candidates) {
423 auto new_end = std::remove_if(
424 candidates_.begin(), candidates_.end(),
425 [removed_candidates](Candidate& candidate) {
426 for (const Candidate& removed_candidate : removed_candidates) {
427 if (candidate.MatchesForRemoval(removed_candidate)) {
428 return true;
429 }
430 }
431 return false;
432 });
433 candidates_.erase(new_end, candidates_.end());
434 }
435
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700436 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800437 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700438 RelayServerConfig server_config = allocator_->turn_servers()[i];
439 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000440 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700441 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 if (proto_addr.address == relay_port->address &&
443 proto_addr.proto == relay_port->proto)
444 return true;
445 }
446 }
447 return false;
448 }
449
Guo-wei Shieh11477022015-08-15 09:28:41 -0700450 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
451 bool with_nat) {
452 if (with_nat) {
453 nat_server_.reset(new rtc::NATServer(
454 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
455 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
456 } else {
457 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
458 }
459
460 ServerAddresses stun_servers;
461 if (!stun_server.IsNil()) {
462 stun_servers.insert(stun_server);
463 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700464 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700465 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700466 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700467 }
468
pthatcher1749bc32017-02-08 13:18:00 -0800469 std::unique_ptr<rtc::VirtualSocketServer> vss_;
470 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700471 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800472 std::unique_ptr<rtc::NATServer> nat_server_;
473 rtc::NATSocketFactory nat_factory_;
474 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
475 std::unique_ptr<TestStunServer> stun_server_;
476 TestRelayServer relay_server_;
477 TestTurnServer turn_server_;
478 rtc::FakeNetworkManager network_manager_;
479 std::unique_ptr<BasicPortAllocator> allocator_;
480 std::unique_ptr<PortAllocatorSession> session_;
481 std::vector<PortInterface*> ports_;
482 std::vector<Candidate> candidates_;
483 bool candidate_allocation_done_;
484};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700485
pthatcher1749bc32017-02-08 13:18:00 -0800486class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
487};
488
489class FakeClockBase {
490 public:
491 rtc::ScopedFakeClock fake_clock;
492};
493
494class BasicPortAllocatorTest : public FakeClockBase,
495 public BasicPortAllocatorTestBase {
496 public:
497 // This function starts the port/address gathering and check the existence of
498 // candidates as specified. When |expect_stun_candidate| is true,
499 // |stun_candidate_addr| carries the expected reflective address, which is
500 // also the related address for TURN candidate if it is expected. Otherwise,
501 // it should be ignore.
502 void CheckDisableAdapterEnumeration(
503 uint32_t total_ports,
504 const rtc::IPAddress& host_candidate_addr,
505 const rtc::IPAddress& stun_candidate_addr,
506 const rtc::IPAddress& relay_candidate_udp_transport_addr,
507 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
508 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
509 rtc::IPAddress());
510 if (!session_) {
511 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
512 }
513 session_->set_flags(session_->flags() |
514 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
515 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
516 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700517 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800518 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
519 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700520
pthatcher1749bc32017-02-08 13:18:00 -0800521 uint32_t total_candidates = 0;
522 if (!host_candidate_addr.IsNil()) {
523 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
524 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
525 ++total_candidates;
526 }
527 if (!stun_candidate_addr.IsNil()) {
528 rtc::SocketAddress related_address(host_candidate_addr, 0);
529 if (host_candidate_addr.IsNil()) {
530 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
531 }
532 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
533 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
534 ++total_candidates;
535 }
536 if (!relay_candidate_udp_transport_addr.IsNil()) {
537 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
538 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
539 rtc::SocketAddress(stun_candidate_addr, 0));
540 ++total_candidates;
541 }
542 if (!relay_candidate_tcp_transport_addr.IsNil()) {
543 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
544 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
545 rtc::SocketAddress(stun_candidate_addr, 0));
546 ++total_candidates;
547 }
548
549 EXPECT_EQ(total_candidates, candidates_.size());
550 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700551 }
552
553 void TestIPv6TurnPortPrunesIPv4TurnPort() {
554 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
555 // Add two IP addresses on the same interface.
556 AddInterface(kClientAddr, "net1");
557 AddInterface(kClientIPv6Addr, "net1");
558 allocator_.reset(new BasicPortAllocator(&network_manager_));
559 allocator_->SetConfiguration(allocator_->stun_servers(),
560 allocator_->turn_servers(), 0, true);
561 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
562 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
563
564 allocator_->set_step_delay(kMinimumStepDelay);
565 allocator_->set_flags(
566 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
567 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
568
569 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
570 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800571 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
572 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700573 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
574 EXPECT_EQ(3U, session_->ReadyPorts().size());
575 EXPECT_EQ(3U, ports_.size());
576 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
577 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
578 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
579 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
580
581 // Now that we remove candidates when a TURN port is pruned, there will be
582 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
583 EXPECT_EQ(3U, candidates_.size());
584 const std::vector<Candidate>& ready_candidates =
585 session_->ReadyCandidates();
586 EXPECT_EQ(3U, ready_candidates.size());
587 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
588 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
589 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
590 }
591
pthatcher1749bc32017-02-08 13:18:00 -0800592 void TestUdpTurnPortPrunesTcpTurnPort() {
593 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
594 AddInterface(kClientAddr);
595 allocator_.reset(new BasicPortAllocator(&network_manager_));
596 allocator_->SetConfiguration(allocator_->stun_servers(),
597 allocator_->turn_servers(), 0, true);
598 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
599 allocator_->set_step_delay(kMinimumStepDelay);
600 allocator_->set_flags(allocator().flags() |
601 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
602 PORTALLOCATOR_DISABLE_TCP);
603
604 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
605 session_->StartGettingPorts();
606 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
607 kDefaultAllocationTimeout, fake_clock);
608 // Only 2 ports (one STUN and one TURN) are actually being used.
609 EXPECT_EQ(2U, session_->ReadyPorts().size());
610 // We have verified that each port, when it is added to |ports_|, it is
611 // found in |ready_ports|, and when it is pruned, it is not found in
612 // |ready_ports|, so we only need to verify the content in one of them.
613 EXPECT_EQ(2U, ports_.size());
614 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
615 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
616 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
617
618 // Now that we remove candidates when a TURN port is pruned, |candidates_|
619 // should only contains two candidates regardless whether the TCP TURN port
620 // is created before or after the UDP turn port.
621 EXPECT_EQ(2U, candidates_.size());
622 // There will only be 2 candidates in |ready_candidates| because it only
623 // includes the candidates in the ready ports.
624 const std::vector<Candidate>& ready_candidates =
625 session_->ReadyCandidates();
626 EXPECT_EQ(2U, ready_candidates.size());
627 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
628 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
629 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
630 }
631
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700632 void TestEachInterfaceHasItsOwnTurnPorts() {
633 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
634 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
635 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
636 // Add two interfaces both having IPv4 and IPv6 addresses.
637 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
638 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
639 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
640 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
641 allocator_.reset(new BasicPortAllocator(&network_manager_));
642 allocator_->SetConfiguration(allocator_->stun_servers(),
643 allocator_->turn_servers(), 0, true);
644 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
645 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
646 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
647
648 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800649 allocator_->set_flags(
650 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
651 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700652 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
653 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800654 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
655 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700656 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
657 // use.
658 EXPECT_EQ(10U, session_->ReadyPorts().size());
659 EXPECT_EQ(10U, ports_.size());
660 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
661 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
662 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
663 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
664 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
665 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
666 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
667 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
668 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
669 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
670
671 // Now that we remove candidates when TURN ports are pruned, there will be
672 // exactly 10 candidates in |candidates_|.
673 EXPECT_EQ(10U, candidates_.size());
674 const std::vector<Candidate>& ready_candidates =
675 session_->ReadyCandidates();
676 EXPECT_EQ(10U, ready_candidates.size());
677 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
678 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
679 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
680 kClientIPv6Addr);
681 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
682 kClientIPv6Addr2);
683 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
684 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
685 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
686 kClientIPv6Addr);
687 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
688 kClientIPv6Addr2);
689 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
690 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
691 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000692};
693
694// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700695TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000696 EXPECT_EQ(&network_manager_, allocator().network_manager());
697 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800698 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700699 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000700 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800701 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
702 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700703 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
704 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
705 EXPECT_TRUE(
706 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
707 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000709}
710
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800711// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700712TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800713 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
714 rtc::ADAPTER_TYPE_ETHERNET);
715 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
716 rtc::ADAPTER_TYPE_WIFI);
717 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
718 rtc::ADAPTER_TYPE_CELLULAR);
719 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
720 rtc::ADAPTER_TYPE_VPN);
721 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
722 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700723 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
724 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
725 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800727 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
728 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800729 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700730 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800731 EXPECT_LT(candidate.address().ip(), 0x12345604U);
732 }
733}
734
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700735TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800736 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
737 rtc::ADAPTER_TYPE_ETHERNET);
738 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
739 rtc::ADAPTER_TYPE_WIFI);
740 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
741 rtc::ADAPTER_TYPE_CELLULAR);
742 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
743 rtc::ADAPTER_TYPE_LOOPBACK |
744 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700745 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
746 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
747 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800748 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800749 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
750 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 EXPECT_EQ(1U, candidates_.size());
752 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
753}
754
honghaiz60347052016-05-31 18:29:12 -0700755// Test that high cost networks are filtered if the flag
756// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
757TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
758 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
759 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
760 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
761 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
762 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
763 // interface.
764 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
765 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
766 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
767 cricket::PORTALLOCATOR_DISABLE_RELAY |
768 cricket::PORTALLOCATOR_DISABLE_TCP |
769 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
770 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
771 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800772 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
773 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700774 EXPECT_EQ(1U, candidates_.size());
775 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
776
777 // If both cellular and unknown interfaces are present, only gather on the
778 // unknown interfaces.
779 candidates_.clear();
780 candidate_allocation_done_ = false;
781 RemoveInterface(addr_wifi);
782 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
783 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
784 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800785 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
786 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700787 EXPECT_EQ(2U, candidates_.size());
788 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
789 addr_unknown2.EqualIPs(candidates_[1].address())) ||
790 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
791 addr_unknown2.EqualIPs(candidates_[0].address())));
792
793 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
794 // Wi-Fi interface.
795 candidates_.clear();
796 candidate_allocation_done_ = false;
797 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
798 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800799 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
800 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700801 EXPECT_EQ(1U, candidates_.size());
802 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
803}
804
deadbeef3427f532017-07-26 16:09:33 -0700805// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
806// to gather candidates.
807TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
808 // Add three IPv6 network interfaces, but tell the allocator to only use two.
809 allocator().set_max_ipv6_networks(2);
810 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
811 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
812 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
813
814 // To simplify the test, only gather UDP host candidates.
815 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
816 PORTALLOCATOR_DISABLE_STUN |
817 PORTALLOCATOR_DISABLE_RELAY);
818
819 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
820 session_->StartGettingPorts();
821 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
822 kDefaultAllocationTimeout, fake_clock);
823 EXPECT_EQ(2U, candidates_.size());
824 // Ensure the expected two interfaces (eth0 and eth1) were used.
825 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
826 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr2);
827}
828
829// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
830// being used.
831TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
832 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
833 allocator().set_max_ipv6_networks(1);
834 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
835 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
836 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
837 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
838
839 // To simplify the test, only gather UDP host candidates.
840 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
841 PORTALLOCATOR_DISABLE_STUN |
842 PORTALLOCATOR_DISABLE_RELAY);
843
844 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
845 session_->StartGettingPorts();
846 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
847 kDefaultAllocationTimeout, fake_clock);
848 EXPECT_EQ(3U, candidates_.size());
849 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
850 // were used.
851 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
852 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
853 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr2);
854}
855
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700856// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700857TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800858 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
859 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700860 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
861 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
862 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700863 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800864 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
865 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700866 EXPECT_EQ(1U, candidates_.size());
867}
868
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000869// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700870TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000871 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700872 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700874 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
875 kDefaultAllocationTimeout, fake_clock);
876 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000877 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700878 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
879 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
880 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
881 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
882 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
883 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
884 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
885 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000886}
887
honghaiz8c404fa2015-09-28 07:59:43 -0700888// Test that when the same network interface is brought down and up, the
889// port allocator session will restart a new allocation sequence if
890// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700891TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700892 std::string if_name("test_net0");
893 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700894 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700895 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700896 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
897 kDefaultAllocationTimeout, fake_clock);
898 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700899 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700900 candidate_allocation_done_ = false;
901 candidates_.clear();
902 ports_.clear();
903
deadbeef7f1563f2017-09-15 17:40:01 -0700904 // Disable socket creation to simulate the network interface being down. When
905 // no network interfaces are available, BasicPortAllocator will fall back to
906 // binding to the "ANY" address, so we need to make sure that fails too.
907 fss_->set_tcp_sockets_enabled(false);
908 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -0700909 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -0700910 SIMULATED_WAIT(false, 1000, fake_clock);
911 EXPECT_EQ(0U, candidates_.size());
912 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -0700913
914 // When the same interfaces are added again, new candidates/ports should be
915 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -0700916 fss_->set_tcp_sockets_enabled(true);
917 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -0700918 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -0700919 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
920 kDefaultAllocationTimeout, fake_clock);
921 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700922 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700923}
924
925// Test that when the same network interface is brought down and up, the
926// port allocator session will not restart a new allocation sequence if
927// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700928TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700929 std::string if_name("test_net0");
930 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700931 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700932 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700933 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
934 kDefaultAllocationTimeout, fake_clock);
935 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700936 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700937 session_->StopGettingPorts();
938 candidates_.clear();
939 ports_.clear();
940
941 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -0700942 // Wait one (simulated) second and then verify no new candidates have
943 // appeared.
944 SIMULATED_WAIT(false, 1000, fake_clock);
945 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700946 EXPECT_EQ(0U, ports_.size());
947
948 // When the same interfaces are added again, new candidates/ports should not
949 // be generated because the session has stopped.
950 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -0700951 SIMULATED_WAIT(false, 1000, fake_clock);
952 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700953 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700954}
955
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000956// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700957TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000958 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700959 allocator_->set_step_delay(kDefaultStepDelay);
960 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000961 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800962 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000963 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800964 ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000965 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700966 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
967 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800968 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700969 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
970 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000971 EXPECT_EQ(4U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800972 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700973 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
974 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000975 EXPECT_EQ(4U, ports_.size());
976 EXPECT_TRUE(candidate_allocation_done_);
977 // If we Stop gathering now, we shouldn't get a second "done" callback.
978 session_->StopGettingPorts();
979}
980
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700981TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000982 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700983 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000984 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700985 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
986 kDefaultAllocationTimeout, fake_clock);
987 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000988 // If we Stop gathering now, we shouldn't get a second "done" callback.
989 session_->StopGettingPorts();
990
991 // All ports should have unset send-buffer sizes.
992 CheckSendBufferSizesOfAllPorts(-1);
993}
994
deadbeef7f1563f2017-09-15 17:40:01 -0700995// Tests that we can get callback after StopGetAllPorts when called in the
996// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700997TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000998 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700999 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001000 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001001 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1002 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001003 EXPECT_EQ(2U, ports_.size());
1004 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001005 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1006 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001007}
1008
1009// Test that we restrict client ports appropriately when a port range is set.
1010// We check the candidates for udp/stun/tcp ports, and the from address
1011// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001012TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001013 AddInterface(kClientAddr);
1014 // Check that an invalid port range fails.
1015 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1016 // Check that a null port range succeeds.
1017 EXPECT_TRUE(SetPortRange(0, 0));
1018 // Check that a valid port range succeeds.
1019 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001020 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001021 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001022 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1023 kDefaultAllocationTimeout, fake_clock);
1024 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001025 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001026
1027 int num_nonrelay_candidates = 0;
1028 for (const Candidate& candidate : candidates_) {
1029 // Check the port number for the UDP/STUN/TCP port objects.
1030 if (candidate.type() != RELAY_PORT_TYPE) {
1031 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
1032 ++num_nonrelay_candidates;
1033 }
1034 }
1035 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001036 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001037 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
1038 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001039}
1040
deadbeefe97389c2016-12-23 01:43:45 -08001041// Test that if we have no network adapters, we bind to the ANY address and
1042// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001043TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001044 // Default config uses GTURN and no NAT, so replace that with the
1045 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1046 ResetWithStunServerAndNat(kStunAddr);
1047 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1048 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1049 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1050 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1051 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1052 // TODO(deadbeef): Fix this.
1053 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001054 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001055 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001056 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1057 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001058 EXPECT_EQ(4U, ports_.size());
1059 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1060 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1061 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1062 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1063 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1064 // The "any" address port should be in the signaled ready ports, but the host
1065 // candidate for it is useless and shouldn't be signaled. So we only have
1066 // STUN/TURN candidates.
1067 EXPECT_EQ(3U, candidates_.size());
1068 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1069 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1070 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1071 // server.
1072 EXPECT_EQ(2,
1073 CountCandidates(candidates_, "relay", "udp",
1074 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001075}
1076
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001077// Test that when enumeration is disabled, we should not have any ports when
1078// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001079TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001080 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001081 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001082 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001083 // Expect to see no ports and no candidates.
1084 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1085 rtc::IPAddress(), rtc::IPAddress());
1086}
1087
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001088// Test that even with multiple interfaces, the result should still be a single
1089// default private, one STUN and one TURN candidate since we bind to any address
1090// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001091TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001092 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001093 AddInterface(kPrivateAddr);
1094 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001095 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001096 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001097
1098 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1099 // address set and we have no IPv6 STUN server, there should be no IPv6
1100 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001101 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1102 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001103
1104 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1105 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1106 // TURN/UDP candidates.
1107 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001108 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1109 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001110}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001111
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001112// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1113// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001114TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001115 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001116 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001117 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001118 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001119 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1120 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1121 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1122 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001123 kTurnUdpExtAddr.ipaddr());
1124}
1125
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001126// Test that when adapter enumeration is disabled, for endpoints without
1127// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001128TEST_F(BasicPortAllocatorTest,
1129 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001130 ResetWithNoServersOrNat();
1131 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1132 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1133 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001134}
1135
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001136// Test that when adapter enumeration is disabled, with
1137// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1138// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001139TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001140 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1141 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001142 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1143 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001144 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1145 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001146 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001147 rtc::IPAddress(), rtc::IPAddress());
1148}
1149
1150// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001151// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1152// a NAT, there is no local candidate. However, this specified default route
1153// (kClientAddr) which was discovered when sending STUN requests, will become
1154// the srflx addresses.
1155TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001156 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001157 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001158 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001159 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001160 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1161 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001162 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1163 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001164 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1165 rtc::IPAddress(), rtc::IPAddress());
1166}
1167
1168// Test that when adapter enumeration is disabled, with
1169// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1170// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001171TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001172 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1173 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001174 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1175 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001176 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1177 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1178 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001179}
1180
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001181// Test that we disable relay over UDP, and only TCP is used when connecting to
1182// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001183TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001184 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001185 AddInterface(kClientAddr);
1186 ResetWithStunServerAndNat(kStunAddr);
1187 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001188 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1189 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1190 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1191 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001192
1193 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001194 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1195 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001196
1197 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1198 // TURN/TCP candidates.
1199 EXPECT_EQ(2U, ports_.size());
1200 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001201 Candidate turn_candidate;
1202 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1203 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001204 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001205 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1206 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001207}
1208
Erik Språngefdce692015-06-05 09:41:26 +02001209// Disable for asan, see
1210// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1211#if !defined(ADDRESS_SANITIZER)
1212
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001213// Test that we can get OnCandidatesAllocationDone callback when all the ports
1214// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001215TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001216 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001217 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1218 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1219 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001220 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001221 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001222 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001223}
1224
1225// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001226TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001227 AddInterface(kClientAddr);
1228 fss_->set_udp_sockets_enabled(false);
1229 EXPECT_TRUE(CreateSession(1));
1230 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001231 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1232 kDefaultAllocationTimeout, fake_clock);
1233 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001234 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001235 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1236 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1237 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1238 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1239 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1240 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241}
1242
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001243#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001244
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001245// Test that we don't crash or malfunction if we can't create UDP sockets or
1246// listen on TCP sockets. We still give out a local TCP address, since
1247// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001248TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001249 AddInterface(kClientAddr);
1250 fss_->set_udp_sockets_enabled(false);
1251 fss_->set_tcp_listen_enabled(false);
1252 EXPECT_TRUE(CreateSession(1));
1253 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001254 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1255 kDefaultAllocationTimeout, fake_clock);
1256 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001257 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001258 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1259 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1260 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1261 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1262 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001263 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001264}
1265
1266// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001267// TODO(deadbeef): Find a way to exit early here.
1268TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001269 AddInterface(kClientAddr);
1270 fss_->set_tcp_sockets_enabled(false);
1271 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001272 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001273 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001274 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001275 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001276 // In case of Relay, ports creation will succeed but sockets will fail.
1277 // There is no error reporting from RelayEntry to handle this failure.
1278}
1279
1280// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001281TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001282 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1283 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001284 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001285 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001286 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1287 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001288 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001289 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1290 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001291 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001292 // will be tried after about 3 seconds.
1293 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001294 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001295 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1296 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1297 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1298 kRelaySslTcpIntAddr);
1299 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001300 // We wait at least for a full STUN timeout, which
1301 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1302 // already passed (see above), we wait 3 seconds less than that.
1303 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1304 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001305}
1306
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001307TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001308 AddInterface(kClientAddr);
1309 AddInterface(kClientAddr2);
1310 // Allocating only host UDP ports. This is done purely for testing
1311 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001312 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1313 PORTALLOCATOR_DISABLE_RELAY);
1314 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001315 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001316 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1317 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001318 ASSERT_EQ(2U, candidates_.size());
1319 EXPECT_EQ(2U, ports_.size());
1320 // Candidates priorities should be different.
1321 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1322}
1323
1324// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001325TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001326 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001327 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001328 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001329 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1330 kDefaultAllocationTimeout, fake_clock);
1331 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001333 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001334}
1335
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001336// Test that the allocator session uses the candidate filter it's created with,
1337// rather than the filter of its parent allocator.
1338// The filter of the allocator should only affect the next gathering phase,
1339// according to JSEP, which means the *next* allocator session returned.
1340TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1341 AddInterface(kClientAddr);
1342 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1343 // Set candidate filter *after* creating the session. Should have no effect.
1344 allocator().set_candidate_filter(CF_RELAY);
1345 session_->StartGettingPorts();
1346 // 7 candidates and 4 ports is what we would normally get (see the
1347 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001348 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1349 kDefaultAllocationTimeout, fake_clock);
1350 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001351 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001352}
1353
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001354// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1355// This test also verifies that when the allocator is only allowed to use
1356// relay (i.e. IceTransportsType is relay), the raddr is an empty
1357// address with the correct family. This is to prevent any local
1358// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001359TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360 AddInterface(kClientAddr);
1361 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001362 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001363 allocator().set_candidate_filter(CF_RELAY);
1364 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001365 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001366 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1367 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001368 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1369 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001370
1371 EXPECT_EQ(1U, candidates_.size());
1372 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001373 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1374 EXPECT_EQ(
1375 candidates_[0].related_address(),
1376 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001377}
1378
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001379TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001380 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001381 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1382 allocator().set_candidate_filter(CF_HOST);
1383 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001384 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001385 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1386 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001387 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1388 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001389 for (const Candidate& candidate : candidates_) {
1390 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001391 }
1392}
1393
1394// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001395TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001396 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001397 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001399 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1400 allocator().set_candidate_filter(CF_REFLEXIVE);
1401 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001402 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001403 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1404 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405 // Host is behind NAT, no private address will be exposed. Hence only UDP
1406 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001407 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1408 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001409 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1410 EXPECT_EQ(
1411 candidates_[0].related_address(),
1412 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413}
1414
1415// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001416TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001417 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001418 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1419 allocator().set_candidate_filter(CF_REFLEXIVE);
1420 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001422 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1423 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001424 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001425 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001426 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001427 for (const Candidate& candidate : candidates_) {
1428 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001429 }
1430}
1431
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001432// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001433TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001434 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001435 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001436 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001437 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1438 kDefaultAllocationTimeout, fake_clock);
1439 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001440 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1441 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1442 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001443 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001444 for (const Candidate& candidate : candidates_) {
1445 EXPECT_EQ(kIceUfrag0, candidate.username());
1446 EXPECT_EQ(kIcePwd0, candidate.password());
1447 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001448}
1449
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1451// is allocated for udp and stun. Also verify there is only one candidate
1452// (local) if stun candidate is same as local candidate, which will be the case
1453// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001454TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001455 AddInterface(kClientAddr);
1456 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001457 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1458 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001459 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001460 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1461 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001462 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001463 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001464 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1465 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001466}
1467
1468// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1469// is allocated for udp and stun. In this test we should expect both stun and
1470// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001471TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001472 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001473 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001474
1475 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001476 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1477 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001478 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001479 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1480 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001481 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001482 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1483 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1484 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001485 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1486 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001487 EXPECT_EQ(3U, candidates_.size());
1488}
1489
deadbeefc5d0d952015-07-16 10:22:21 -07001490// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001491TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001492 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001493 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001494 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001495
1496 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1497
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001498 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001499 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001500 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1501 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001502
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001503 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001504 session_->StartGettingPorts();
1505
deadbeef7f1563f2017-09-15 17:40:01 -07001506 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1507 kDefaultAllocationTimeout, fake_clock);
1508 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001509 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001510 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1511 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1512 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1513 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1514 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001515}
1516
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001517// Test that if prune_turn_ports is set, TCP TURN port will not be used
1518// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1519TEST_F(BasicPortAllocatorTest,
1520 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1521 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1522 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1523 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001524
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001525 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001526}
1527
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001528// Test that if prune_turn_ports is set, TCP TURN port will not be used
1529// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1530TEST_F(BasicPortAllocatorTest,
1531 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1532 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1533 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1534 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001535
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001536 TestUdpTurnPortPrunesTcpTurnPort();
1537}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001538
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001539// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1540// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1541TEST_F(BasicPortAllocatorTest,
1542 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1543 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1544 // first.
1545 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1546 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001547
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001548 TestIPv6TurnPortPrunesIPv4TurnPort();
1549}
1550
1551// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1552// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1553TEST_F(BasicPortAllocatorTest,
1554 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1555 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1556 // first.
1557 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1558 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1559
1560 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001561}
1562
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001563// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001564// will has its own set of TurnPorts based on their priorities, in the default
1565// case where no transit delay is set.
1566TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1567 TestEachInterfaceHasItsOwnTurnPorts();
1568}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001569
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001570// Tests that if prune_turn_ports is set, each network interface
1571// will has its own set of TurnPorts based on their priorities, given that
1572// IPv4/TCP TURN port becomes ready first.
1573TEST_F(BasicPortAllocatorTest,
1574 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1575 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1576 // becomes ready last.
1577 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1578 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1579 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1580 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001581
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001582 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001583}
1584
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001585// Testing DNS resolve for the TURN server, this will test AllocationSequence
1586// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001587// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1588// appears that it doesn't currently because of the DNS look up not
1589// using the fake clock.
1590TEST_F(BasicPortAllocatorTestWithRealClock,
1591 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001592 // This test relies on a real query for "localhost", so it won't work on an
1593 // IPv6-only machine.
1594 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001595 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001596 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001597 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001598 allocator_.reset(new BasicPortAllocator(&network_manager_));
1599 RelayServerConfig turn_server(RELAY_TURN);
1600 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001601 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001602 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001603 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001604 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001605
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001606 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001607 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001608 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1609 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001610
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001611 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001612 session_->StartGettingPorts();
1613
1614 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1615}
1616
1617// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1618// is allocated for udp/stun/turn. In this test we should expect all local,
1619// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001620TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001621 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001622 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623
1624 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1625
1626 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001627 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1628 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001629
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001630 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001631 session_->StartGettingPorts();
1632
deadbeef7f1563f2017-09-15 17:40:01 -07001633 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1634 kDefaultAllocationTimeout, fake_clock);
1635 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001636 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001637 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1638 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1639 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1640 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1641 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001642 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1643 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001644 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001645 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1646 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001647 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1648 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1649}
1650
1651// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1652// server is also used as the STUN server, we should get 'local', 'stun', and
1653// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001654TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001655 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001656 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001657 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1659
1660 // Must set the step delay to 0 to make sure the relay allocation phase is
1661 // started before the STUN candidates are obtained, so that the STUN binding
1662 // response is processed when both StunPort and TurnPort exist to reproduce
1663 // webrtc issue 3537.
1664 allocator_->set_step_delay(0);
1665 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001666 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1667 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001669 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001670 session_->StartGettingPorts();
1671
deadbeef7f1563f2017-09-15 17:40:01 -07001672 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1673 kDefaultAllocationTimeout, fake_clock);
1674 EXPECT_EQ(3U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001675 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1676 Candidate stun_candidate;
1677 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1678 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1679 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1680 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1681 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001682
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001683 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001684 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1685 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001686 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1687 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1688}
1689
deadbeefc5d0d952015-07-16 10:22:21 -07001690// Test that when only a TCP TURN server is available, we do NOT use it as
1691// a UDP STUN server, as this could leak our IP address. Thus we should only
1692// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001693TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001694 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001695 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001696 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001697 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1698
1699 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001700 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1701 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001702
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001703 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001704 session_->StartGettingPorts();
1705
deadbeef7f1563f2017-09-15 17:40:01 -07001706 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1707 kDefaultAllocationTimeout, fake_clock);
1708 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001709 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001710 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1711 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1712 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001713 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1714 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1715}
1716
1717// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1718// TURN server is used as the STUN server and we get 'local', 'stun', and
1719// 'relay' candidates.
1720// TODO(deadbeef): Remove this test when support for non-shared socket mode
1721// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001722TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001723 AddInterface(kClientAddr);
1724 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001725 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001726 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1727
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001728 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001729
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001730 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001731 session_->StartGettingPorts();
1732
deadbeef7f1563f2017-09-15 17:40:01 -07001733 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1734 kDefaultAllocationTimeout, fake_clock);
1735 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001736 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001737 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1738 Candidate stun_candidate;
1739 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1740 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1741 Candidate turn_candidate;
1742 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1743 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1744 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001745 // Not using shared socket, so the STUN request's server reflexive address
1746 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001747 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001748
deadbeefc5d0d952015-07-16 10:22:21 -07001749 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1750 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1751 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1752}
1753
1754// Test that even when both a STUN and TURN server are configured, the TURN
1755// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001756TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001757 AddInterface(kClientAddr);
1758 // Configure with STUN server but destroy it, so we can ensure that it's
1759 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001760 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001761 stun_server_.reset();
1762 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1763
1764 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001765 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1766 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001767
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001768 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001769 session_->StartGettingPorts();
1770
pthatcher1749bc32017-02-08 13:18:00 -08001771 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1772 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001773 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1774 Candidate stun_candidate;
1775 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1776 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1777 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1778 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1779 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001780
1781 // Don't bother waiting for STUN timeout, since we already verified
1782 // that we got a STUN candidate from the TURN server.
1783}
1784
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001785// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1786// and fail to generate STUN candidate, local UDP candidate is generated
1787// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001788TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001789 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1790 PORTALLOCATOR_DISABLE_TCP |
1791 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001792 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1793 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001794 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001795 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001796 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1797 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001798 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001799 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001800 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001801 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1802 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001803 EXPECT_EQ(1U, candidates_.size());
1804}
1805
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001806// Test that when the NetworkManager doesn't have permission to enumerate
1807// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1808// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001809TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001810 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1811 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001812 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001813 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001814 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1815 PORTALLOCATOR_DISABLE_TCP |
1816 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1817 EXPECT_EQ(0U,
1818 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1819 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1820 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001821 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001822 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1823 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001824 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001825 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001826 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001827}
1828
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001829// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001830TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001831 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1832 PORTALLOCATOR_ENABLE_IPV6 |
1833 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001834 AddInterface(kClientIPv6Addr);
1835 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001836 allocator_->set_step_delay(kMinimumStepDelay);
1837 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001838 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001839 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08001840 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07001841 EXPECT_EQ(4U, ports_.size());
1842 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001843 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1844 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1845 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1846 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001847}
honghaiz98db68f2015-09-29 07:58:17 -07001848
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001849TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001850 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001851 allocator_->set_step_delay(kDefaultStepDelay);
1852 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001853 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001854 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001855 EXPECT_EQ(2U, ports_.size());
1856 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001857 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001858
1859 // After stopping getting ports, adding a new interface will not start
1860 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001861 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001862 candidates_.clear();
1863 ports_.clear();
1864 candidate_allocation_done_ = false;
1865 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001866 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001867 EXPECT_EQ(0U, candidates_.size());
1868 EXPECT_EQ(0U, ports_.size());
1869}
1870
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001871TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001872 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001873 allocator_->set_step_delay(kDefaultStepDelay);
1874 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001875 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001876 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001877 EXPECT_EQ(2U, ports_.size());
1878 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001879 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001880
1881 // After clearing getting ports, adding a new interface will start getting
1882 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001883 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001884 candidates_.clear();
1885 ports_.clear();
1886 candidate_allocation_done_ = false;
1887 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001888 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001889 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001890 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1891 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001892}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001893
1894// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1895// a pooled session is taken out of the pool.
1896TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1897 AddInterface(kClientAddr);
1898 int pool_size = 1;
1899 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001900 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001901 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001902 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001903 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1904 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001905 // Expect that when TakePooledSession is called,
1906 // UpdateTransportInformationInternal will be called and the
1907 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1908 // candidates.
1909 session_ =
1910 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1911 ASSERT_NE(nullptr, session_.get());
1912 auto ready_ports = session_->ReadyPorts();
1913 auto candidates = session_->ReadyCandidates();
1914 EXPECT_FALSE(ready_ports.empty());
1915 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001916 for (const PortInterface* port_interface : ready_ports) {
1917 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001918 EXPECT_EQ(kContentName, port->content_name());
1919 EXPECT_EQ(1, port->component());
1920 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1921 EXPECT_EQ(kIcePwd0, port->password());
1922 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001923 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001924 EXPECT_EQ(1, candidate.component());
1925 EXPECT_EQ(kIceUfrag0, candidate.username());
1926 EXPECT_EQ(kIcePwd0, candidate.password());
1927 }
1928}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001929
1930// Test that a new candidate filter takes effect even on already-gathered
1931// candidates.
1932TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1933 AddInterface(kClientAddr);
1934 int pool_size = 1;
1935 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001936 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001937 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1938 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001939 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1940 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001941 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1942 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1943 allocator_->set_candidate_filter(CF_RELAY);
1944 // Assume that when TakePooledSession is called, the candidate filter will be
1945 // applied to the pooled session. This is tested by PortAllocatorTest.
1946 session_ =
1947 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1948 ASSERT_NE(nullptr, session_.get());
1949 auto candidates = session_->ReadyCandidates();
1950 auto ports = session_->ReadyPorts();
1951 // Sanity check that the number of candidates and ports decreased.
1952 EXPECT_GT(initial_candidates_size, candidates.size());
1953 EXPECT_GT(initial_ports_size, ports.size());
1954 for (const PortInterface* port : ports) {
1955 // Expect only relay ports.
1956 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1957 }
1958 for (const Candidate& candidate : candidates) {
1959 // Expect only relay candidates now that the filter is applied.
1960 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1961 // Expect that the raddr is emptied due to the CF_RELAY filter.
1962 EXPECT_EQ(candidate.related_address(),
1963 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1964 }
1965}
1966
1967} // namespace cricket