blob: d995eb18dee1e69e2586203db1960adb42892b71 [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"
Qingsi Wang4ff54432018-03-01 18:25:20 -080017#include "p2p/base/stunport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/testrelayserver.h"
19#include "p2p/base/teststunserver.h"
20#include "p2p/base/testturnserver.h"
21#include "p2p/client/basicportallocator.h"
22#include "rtc_base/fakeclock.h"
23#include "rtc_base/fakenetwork.h"
24#include "rtc_base/firewallsocketserver.h"
25#include "rtc_base/gunit.h"
26#include "rtc_base/helpers.h"
27#include "rtc_base/ipaddress.h"
28#include "rtc_base/logging.h"
29#include "rtc_base/natserver.h"
30#include "rtc_base/natsocketfactory.h"
31#include "rtc_base/nethelpers.h"
32#include "rtc_base/network.h"
33#include "rtc_base/socketaddress.h"
34#include "rtc_base/ssladapter.h"
35#include "rtc_base/thread.h"
36#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080038using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039using rtc::SocketAddress;
40using rtc::Thread;
41
Mirko Bonadei675513b2017-11-09 11:09:25 +010042#define MAYBE_SKIP_IPV4 \
43 if (!rtc::HasIPv4Enabled()) { \
44 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
45 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070046 }
47
deadbeefe97389c2016-12-23 01:43:45 -080048static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070050static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070051static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000053static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070054static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
55 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070056static const SocketAddress kClientIPv6Addr2(
57 "2401:fa00:4:2000:be30:5bff:fee5:c3",
58 0);
deadbeef3427f532017-07-26 16:09:33 -070059static const SocketAddress kClientIPv6Addr3(
60 "2401:fa00:4:3000:be30:5bff:fee5:c3",
61 0);
deadbeefc5d0d952015-07-16 10:22:21 -070062static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
63static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
65static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
66static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
67static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
68static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
69static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
70static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
71static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
72static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070073static const SocketAddress kTurnUdpIntIPv6Addr(
74 "2402:fb00:4:1000:be30:5bff:fee5:c3",
75 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070077static const SocketAddress kTurnTcpIntIPv6Addr(
78 "2402:fb00:4:2000:be30:5bff:fee5:c3",
79 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
81
82// Minimum and maximum port for port range tests.
83static const int kMinPort = 10000;
84static const int kMaxPort = 10099;
85
86// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070087static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088// Based on ICE_PWD_LENGTH
89static const char kIcePwd0[] = "TESTICEPWD00000000000000";
90
91static const char kContentName[] = "test content";
92
Honghai Zhangc67e0f52016-09-19 16:57:37 -070093static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094static const char kTurnUsername[] = "test";
95static const char kTurnPassword[] = "test";
96
pthatcher94a2f212017-02-08 14:42:22 -080097// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070098// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080099static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -0700100
Qingsi Wang4ff54432018-03-01 18:25:20 -0800101namespace {
102
103void CheckStunKeepaliveIntervalOfAllReadyPorts(
104 const cricket::PortAllocatorSession* allocator_session,
105 int expected) {
106 auto ready_ports = allocator_session->ReadyPorts();
107 for (const auto* port : ready_ports) {
108 if (port->Type() == cricket::STUN_PORT_TYPE ||
109 (port->Type() == cricket::LOCAL_PORT_TYPE &&
110 port->GetProtocol() == cricket::PROTO_UDP)) {
111 EXPECT_EQ(
112 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
113 expected);
114 }
115 }
116}
117
118} // namespace
119
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120namespace cricket {
121
122// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700123std::ostream& operator<<(std::ostream& os,
124 const std::vector<Candidate>& candidates) {
125 os << '[';
126 bool first = true;
127 for (const Candidate& c : candidates) {
128 if (!first) {
129 os << ", ";
130 }
131 os << c.ToString();
132 first = false;
Steve Anton6c38cc72017-11-29 10:25:58 -0800133 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700134 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 return os;
136}
137
pthatcher1749bc32017-02-08 13:18:00 -0800138class BasicPortAllocatorTestBase : public testing::Test,
139 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 public:
pthatcher1749bc32017-02-08 13:18:00 -0800141 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700142 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700144 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800145 // Note that the NAT is not used by default. ResetWithStunServerAndNat
146 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700147 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700148 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700149 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700150 relay_server_(Thread::Current(),
151 kRelayUdpIntAddr,
152 kRelayUdpExtAddr,
153 kRelayTcpIntAddr,
154 kRelayTcpExtAddr,
155 kRelaySslTcpIntAddr,
156 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
158 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 stun_servers.insert(kStunAddr);
161 // Passing the addresses of GTURN servers will enable GTURN in
162 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800163 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
164 // configuration should be blank by default (preferred), or it should use
165 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700166 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
167 kRelayUdpIntAddr, kRelayTcpIntAddr,
168 kRelaySslTcpIntAddr));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700169 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700170 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 }
172
173 void AddInterface(const SocketAddress& addr) {
174 network_manager_.AddInterface(addr);
175 }
honghaiz8c404fa2015-09-28 07:59:43 -0700176 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
177 network_manager_.AddInterface(addr, if_name);
178 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800179 void AddInterface(const SocketAddress& addr,
180 const std::string& if_name,
181 rtc::AdapterType type) {
182 network_manager_.AddInterface(addr, if_name, type);
183 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800184 // The default route is the public address that STUN server will observe when
185 // the endpoint is sitting on the public internet and the local port is bound
186 // to the "any" address. This may be different from the default local address
187 // which the endpoint observes. This can occur if the route to the public
188 // endpoint like 8.8.8.8 (specified as the default local address) is
189 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700190 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
191 AddInterface(addr);
192 // When a binding comes from the any address, the |addr| will be used as the
193 // srflx address.
194 vss_->SetDefaultRoute(addr.ipaddr());
195 }
honghaiz8c404fa2015-09-28 07:59:43 -0700196 void RemoveInterface(const SocketAddress& addr) {
197 network_manager_.RemoveInterface(addr);
198 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 bool SetPortRange(int min_port, int max_port) {
200 return allocator_->SetPortRange(min_port, max_port);
201 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700202 // Endpoint is on the public network. No STUN or TURN.
203 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700204 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700205 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700206 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700207 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700208 // Endpoint is behind a NAT, with STUN specified.
209 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
210 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700211 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700212 // Endpoint is on the public network, with STUN specified.
213 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
214 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700216 // Endpoint is on the public network, with TURN specified.
217 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
218 const rtc::SocketAddress& tcp_turn) {
219 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 AddTurnServers(udp_turn, tcp_turn);
221 }
222
223 void AddTurnServers(const rtc::SocketAddress& udp_turn,
224 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 RelayServerConfig turn_server(RELAY_TURN);
226 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800227 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228
229 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800230 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 }
232 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800233 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 }
deadbeef653b8e02015-11-11 12:55:10 -0800235 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 }
237
238 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700239 session_ = CreateSession("session", component);
240 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700242 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243 return true;
244 }
245
246 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700247 session_ = CreateSession("session", content_name, component);
248 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700250 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 return true;
252 }
253
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700254 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
255 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000256 return CreateSession(sid, kContentName, component);
257 }
258
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700259 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700260 const std::string& sid,
261 const std::string& content_name,
262 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000263 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
264 }
265
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700266 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700267 const std::string& sid,
268 const std::string& content_name,
269 int component,
270 const std::string& ice_ufrag,
271 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700272 std::unique_ptr<PortAllocatorSession> session =
273 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800275 &BasicPortAllocatorTestBase::OnPortReady);
276 session->SignalPortsPruned.connect(
277 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700278 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800279 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700280 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800281 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700282 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800283 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000284 return session;
285 }
286
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700287 // Return true if the addresses are the same, or the port is 0 in |pattern|
288 // (acting as a wildcard) and the IPs are the same.
289 // Even with a wildcard port, the port of the address should be nonzero if
290 // the IP is nonzero.
291 static bool AddressMatch(const SocketAddress& address,
292 const SocketAddress& pattern) {
293 return address.ipaddr() == pattern.ipaddr() &&
294 ((pattern.port() == 0 &&
295 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
296 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000297 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700298
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700299 // Returns the number of ports that have matching type, protocol and
300 // address.
301 static int CountPorts(const std::vector<PortInterface*>& ports,
302 const std::string& type,
303 ProtocolType protocol,
304 const SocketAddress& client_addr) {
305 return std::count_if(
306 ports.begin(), ports.end(),
307 [type, protocol, client_addr](PortInterface* port) {
308 return port->Type() == type && port->GetProtocol() == protocol &&
309 port->Network()->GetBestIP() == client_addr.ipaddr();
310 });
311 }
312
deadbeefe97389c2016-12-23 01:43:45 -0800313 static int CountCandidates(const std::vector<Candidate>& candidates,
314 const std::string& type,
315 const std::string& proto,
316 const SocketAddress& addr) {
317 return std::count_if(candidates.begin(), candidates.end(),
318 [type, proto, addr](const Candidate& c) {
319 return c.type() == type && c.protocol() == proto &&
320 AddressMatch(c.address(), addr);
321 });
322 }
323
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700324 // Find a candidate and return it.
325 static bool FindCandidate(const std::vector<Candidate>& candidates,
326 const std::string& type,
327 const std::string& proto,
328 const SocketAddress& addr,
329 Candidate* found) {
330 auto it = std::find_if(candidates.begin(), candidates.end(),
331 [type, proto, addr](const Candidate& c) {
332 return c.type() == type && c.protocol() == proto &&
333 AddressMatch(c.address(), addr);
334 });
335 if (it != candidates.end() && found) {
336 *found = *it;
337 }
338 return it != candidates.end();
339 }
340
341 // Convenience method to call FindCandidate with no return.
342 static bool HasCandidate(const std::vector<Candidate>& candidates,
343 const std::string& type,
344 const std::string& proto,
345 const SocketAddress& addr) {
346 return FindCandidate(candidates, type, proto, addr, nullptr);
347 }
348
349 // Version of HasCandidate that also takes a related address.
350 static bool HasCandidateWithRelatedAddr(
351 const std::vector<Candidate>& candidates,
352 const std::string& type,
353 const std::string& proto,
354 const SocketAddress& addr,
355 const SocketAddress& related_addr) {
356 auto it =
357 std::find_if(candidates.begin(), candidates.end(),
358 [type, proto, addr, related_addr](const Candidate& c) {
359 return c.type() == type && c.protocol() == proto &&
360 AddressMatch(c.address(), addr) &&
361 AddressMatch(c.related_address(), related_addr);
362 });
363 return it != candidates.end();
364 }
365
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700367 int min_port,
368 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 return (addr.port() >= min_port && addr.port() <= max_port);
370 }
371
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700372 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373 // We should only get this callback once, except in the mux test where
374 // we have multiple port allocation sessions.
375 if (session == session_.get()) {
376 ASSERT_FALSE(candidate_allocation_done_);
377 candidate_allocation_done_ = true;
378 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700379 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380 }
381
382 // Check if all ports allocated have send-buffer size |expected|. If
383 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
384 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700385 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 for (it = ports_.begin(); it < ports_.end(); ++it) {
387 int send_buffer_size;
388 if (expected == -1) {
389 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000391 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700392 EXPECT_EQ(0,
393 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 ASSERT_EQ(expected, send_buffer_size);
395 }
396 }
397 }
398
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700399 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
400
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700402 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700404 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100405 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000406 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700407 // Make sure the new port is added to ReadyPorts.
408 auto ready_ports = ses->ReadyPorts();
409 EXPECT_NE(ready_ports.end(),
410 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700412 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700413 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100414 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700415 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700416 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700417 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700418 new_end = std::remove(ports_.begin(), new_end, port);
419 // Make sure the pruned port is not in ReadyPorts.
420 EXPECT_EQ(ready_ports.end(),
421 std::find(ready_ports.begin(), ready_ports.end(), port));
422 }
423 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700424 }
425
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700426 void OnCandidatesReady(PortAllocatorSession* ses,
427 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700428 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100429 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700430 // Sanity check that the ICE component is set.
431 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
432 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000433 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700434 // Make sure the new candidates are added to Candidates.
435 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700436 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700437 EXPECT_NE(
438 ses_candidates.end(),
439 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
440 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000441 }
442
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700443 void OnCandidatesRemoved(PortAllocatorSession* session,
444 const std::vector<Candidate>& removed_candidates) {
445 auto new_end = std::remove_if(
446 candidates_.begin(), candidates_.end(),
447 [removed_candidates](Candidate& candidate) {
448 for (const Candidate& removed_candidate : removed_candidates) {
449 if (candidate.MatchesForRemoval(removed_candidate)) {
450 return true;
451 }
452 }
453 return false;
454 });
455 candidates_.erase(new_end, candidates_.end());
456 }
457
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700458 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800459 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700460 RelayServerConfig server_config = allocator_->turn_servers()[i];
461 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000462 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700463 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000464 if (proto_addr.address == relay_port->address &&
465 proto_addr.proto == relay_port->proto)
466 return true;
467 }
468 }
469 return false;
470 }
471
Guo-wei Shieh11477022015-08-15 09:28:41 -0700472 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
473 bool with_nat) {
474 if (with_nat) {
475 nat_server_.reset(new rtc::NATServer(
476 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
477 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
478 } else {
479 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
480 }
481
482 ServerAddresses stun_servers;
483 if (!stun_server.IsNil()) {
484 stun_servers.insert(stun_server);
485 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700486 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700487 &network_manager_, nat_socket_factory_.get(), stun_servers));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700488 allocator_->Initialize();
489 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700490 }
491
pthatcher1749bc32017-02-08 13:18:00 -0800492 std::unique_ptr<rtc::VirtualSocketServer> vss_;
493 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700494 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800495 std::unique_ptr<rtc::NATServer> nat_server_;
496 rtc::NATSocketFactory nat_factory_;
497 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
498 std::unique_ptr<TestStunServer> stun_server_;
499 TestRelayServer relay_server_;
500 TestTurnServer turn_server_;
501 rtc::FakeNetworkManager network_manager_;
502 std::unique_ptr<BasicPortAllocator> allocator_;
503 std::unique_ptr<PortAllocatorSession> session_;
504 std::vector<PortInterface*> ports_;
505 std::vector<Candidate> candidates_;
506 bool candidate_allocation_done_;
507};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700508
pthatcher1749bc32017-02-08 13:18:00 -0800509class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
510};
511
512class FakeClockBase {
513 public:
514 rtc::ScopedFakeClock fake_clock;
515};
516
517class BasicPortAllocatorTest : public FakeClockBase,
518 public BasicPortAllocatorTestBase {
519 public:
520 // This function starts the port/address gathering and check the existence of
521 // candidates as specified. When |expect_stun_candidate| is true,
522 // |stun_candidate_addr| carries the expected reflective address, which is
523 // also the related address for TURN candidate if it is expected. Otherwise,
524 // it should be ignore.
525 void CheckDisableAdapterEnumeration(
526 uint32_t total_ports,
527 const rtc::IPAddress& host_candidate_addr,
528 const rtc::IPAddress& stun_candidate_addr,
529 const rtc::IPAddress& relay_candidate_udp_transport_addr,
530 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
531 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
532 rtc::IPAddress());
533 if (!session_) {
Qingsi Wange53ac042018-05-08 11:55:07 -0700534 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 13:18:00 -0800535 }
536 session_->set_flags(session_->flags() |
537 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
538 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
539 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700540 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800541 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
542 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700543
pthatcher1749bc32017-02-08 13:18:00 -0800544 uint32_t total_candidates = 0;
545 if (!host_candidate_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200546 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
547 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800548 ++total_candidates;
549 }
550 if (!stun_candidate_addr.IsNil()) {
551 rtc::SocketAddress related_address(host_candidate_addr, 0);
552 if (host_candidate_addr.IsNil()) {
553 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
554 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200555 EXPECT_TRUE(HasCandidateWithRelatedAddr(
556 candidates_, "stun", "udp",
557 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
pthatcher1749bc32017-02-08 13:18:00 -0800558 ++total_candidates;
559 }
560 if (!relay_candidate_udp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200561 EXPECT_TRUE(HasCandidateWithRelatedAddr(
562 candidates_, "relay", "udp",
563 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
564 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800565 ++total_candidates;
566 }
567 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200568 EXPECT_TRUE(HasCandidateWithRelatedAddr(
569 candidates_, "relay", "udp",
570 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
571 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800572 ++total_candidates;
573 }
574
575 EXPECT_EQ(total_candidates, candidates_.size());
576 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700577 }
578
579 void TestIPv6TurnPortPrunesIPv4TurnPort() {
580 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
581 // Add two IP addresses on the same interface.
582 AddInterface(kClientAddr, "net1");
583 AddInterface(kClientIPv6Addr, "net1");
584 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700585 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700586 allocator_->SetConfiguration(allocator_->stun_servers(),
587 allocator_->turn_servers(), 0, true);
588 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
589 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
590
591 allocator_->set_step_delay(kMinimumStepDelay);
592 allocator_->set_flags(
593 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
594 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
595
Qingsi Wange53ac042018-05-08 11:55:07 -0700596 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700597 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800598 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
599 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700600 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
601 EXPECT_EQ(3U, session_->ReadyPorts().size());
602 EXPECT_EQ(3U, ports_.size());
603 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
604 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
605 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
606 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
607
608 // Now that we remove candidates when a TURN port is pruned, there will be
609 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
610 EXPECT_EQ(3U, candidates_.size());
611 const std::vector<Candidate>& ready_candidates =
612 session_->ReadyCandidates();
613 EXPECT_EQ(3U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200614 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
615 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
616 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700617 }
618
pthatcher1749bc32017-02-08 13:18:00 -0800619 void TestUdpTurnPortPrunesTcpTurnPort() {
620 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
621 AddInterface(kClientAddr);
622 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700623 allocator_->Initialize();
pthatcher1749bc32017-02-08 13:18:00 -0800624 allocator_->SetConfiguration(allocator_->stun_servers(),
625 allocator_->turn_servers(), 0, true);
626 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
627 allocator_->set_step_delay(kMinimumStepDelay);
628 allocator_->set_flags(allocator().flags() |
629 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
630 PORTALLOCATOR_DISABLE_TCP);
631
Qingsi Wange53ac042018-05-08 11:55:07 -0700632 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 13:18:00 -0800633 session_->StartGettingPorts();
634 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
635 kDefaultAllocationTimeout, fake_clock);
636 // Only 2 ports (one STUN and one TURN) are actually being used.
637 EXPECT_EQ(2U, session_->ReadyPorts().size());
638 // We have verified that each port, when it is added to |ports_|, it is
639 // found in |ready_ports|, and when it is pruned, it is not found in
640 // |ready_ports|, so we only need to verify the content in one of them.
641 EXPECT_EQ(2U, ports_.size());
642 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
643 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
644 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
645
646 // Now that we remove candidates when a TURN port is pruned, |candidates_|
647 // should only contains two candidates regardless whether the TCP TURN port
648 // is created before or after the UDP turn port.
649 EXPECT_EQ(2U, candidates_.size());
650 // There will only be 2 candidates in |ready_candidates| because it only
651 // includes the candidates in the ready ports.
652 const std::vector<Candidate>& ready_candidates =
653 session_->ReadyCandidates();
654 EXPECT_EQ(2U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200655 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
656 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
657 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800658 }
659
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700660 void TestEachInterfaceHasItsOwnTurnPorts() {
661 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
662 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
663 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
664 // Add two interfaces both having IPv4 and IPv6 addresses.
665 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
666 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
667 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
668 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
669 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700670 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700671 allocator_->SetConfiguration(allocator_->stun_servers(),
672 allocator_->turn_servers(), 0, true);
673 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
674 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
675 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
676
677 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800678 allocator_->set_flags(
679 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
680 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Qingsi Wange53ac042018-05-08 11:55:07 -0700681 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700682 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800683 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
684 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700685 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
686 // use.
687 EXPECT_EQ(10U, session_->ReadyPorts().size());
688 EXPECT_EQ(10U, ports_.size());
689 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
690 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
691 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
692 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
693 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
694 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
695 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
696 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
697 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
698 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
699
700 // Now that we remove candidates when TURN ports are pruned, there will be
701 // exactly 10 candidates in |candidates_|.
702 EXPECT_EQ(10U, candidates_.size());
703 const std::vector<Candidate>& ready_candidates =
704 session_->ReadyCandidates();
705 EXPECT_EQ(10U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200706 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
707 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
708 EXPECT_TRUE(
709 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
710 EXPECT_TRUE(
711 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
712 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
713 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
714 EXPECT_TRUE(
715 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
716 EXPECT_TRUE(
717 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
718 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
719 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700720 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000721};
722
723// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700724TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000725 EXPECT_EQ(&network_manager_, allocator().network_manager());
726 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800727 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700728 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000729 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800730 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
731 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700732 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
733 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
734 EXPECT_TRUE(
735 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
Qingsi Wange53ac042018-05-08 11:55:07 -0700736 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700737 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000738}
739
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800740// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700741TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800742 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
743 rtc::ADAPTER_TYPE_ETHERNET);
744 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
745 rtc::ADAPTER_TYPE_WIFI);
746 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
747 rtc::ADAPTER_TYPE_CELLULAR);
748 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
749 rtc::ADAPTER_TYPE_VPN);
750 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
751 rtc::ADAPTER_TYPE_LOOPBACK);
Qingsi Wange53ac042018-05-08 11:55:07 -0700752 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700753 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
754 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800755 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800756 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
757 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800758 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700759 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 EXPECT_LT(candidate.address().ip(), 0x12345604U);
761 }
762}
763
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700764TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800765 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
766 rtc::ADAPTER_TYPE_ETHERNET);
767 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
768 rtc::ADAPTER_TYPE_WIFI);
769 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
770 rtc::ADAPTER_TYPE_CELLULAR);
771 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
772 rtc::ADAPTER_TYPE_LOOPBACK |
773 rtc::ADAPTER_TYPE_WIFI);
Qingsi Wange53ac042018-05-08 11:55:07 -0700774 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700775 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
776 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800777 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800778 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
779 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800780 EXPECT_EQ(1U, candidates_.size());
781 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
782}
783
deadbeef1c46a352017-09-27 11:24:05 -0700784// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
785// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
786TEST_F(BasicPortAllocatorTest,
787 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
788 SocketAddress wifi(IPAddress(0x12345600U), 0);
789 SocketAddress cell(IPAddress(0x12345601U), 0);
790 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
791 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
792 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-05-31 18:29:12 -0700793 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
794 cricket::PORTALLOCATOR_DISABLE_RELAY |
795 cricket::PORTALLOCATOR_DISABLE_TCP |
796 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700797 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700798 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800799 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
800 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700801 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700802 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200803 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700804}
honghaiz60347052016-05-31 18:29:12 -0700805
deadbeef1c46a352017-09-27 11:24:05 -0700806// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
807// both "unknown" and cell interfaces are available, only the unknown are used.
808// The unknown interface may be something that ultimately uses Wi-Fi, so we do
809// this to be on the safe side.
810TEST_F(BasicPortAllocatorTest,
811 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
812 SocketAddress cell(IPAddress(0x12345601U), 0);
813 SocketAddress unknown1(IPAddress(0x12345602U), 0);
814 SocketAddress unknown2(IPAddress(0x12345603U), 0);
815 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
816 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
817 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
818 // Disable all but UDP candidates to make the test simpler.
819 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
820 cricket::PORTALLOCATOR_DISABLE_RELAY |
821 cricket::PORTALLOCATOR_DISABLE_TCP |
822 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700823 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700824 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800825 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
826 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700827 // Should only get two candidates, none of which is cell.
honghaiz60347052016-05-31 18:29:12 -0700828 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200829 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
830 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
deadbeef1c46a352017-09-27 11:24:05 -0700831}
honghaiz60347052016-05-31 18:29:12 -0700832
deadbeef1c46a352017-09-27 11:24:05 -0700833// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
834// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
835// interface is used.
836TEST_F(BasicPortAllocatorTest,
837 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
838 SocketAddress wifi(IPAddress(0x12345600U), 0);
839 SocketAddress cellular(IPAddress(0x12345601U), 0);
840 SocketAddress unknown1(IPAddress(0x12345602U), 0);
841 SocketAddress unknown2(IPAddress(0x12345603U), 0);
842 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
843 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
844 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
845 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
846 // Disable all but UDP candidates to make the test simpler.
847 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
848 cricket::PORTALLOCATOR_DISABLE_RELAY |
849 cricket::PORTALLOCATOR_DISABLE_TCP |
850 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700851 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700852 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800853 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
854 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700855 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700856 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200857 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700858}
859
860// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
861// only interface available is cellular, it ends up used anyway. A costly
862// connection is always better than no connection.
863TEST_F(BasicPortAllocatorTest,
864 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
865 SocketAddress cellular(IPAddress(0x12345601U), 0);
866 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
867 // Disable all but UDP candidates to make the test simpler.
868 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
869 cricket::PORTALLOCATOR_DISABLE_RELAY |
870 cricket::PORTALLOCATOR_DISABLE_TCP |
871 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700872 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 11:24:05 -0700873 session_->StartGettingPorts();
874 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
875 kDefaultAllocationTimeout, fake_clock);
876 // Make sure we got the cell candidate.
877 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200878 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
honghaiz60347052016-05-31 18:29:12 -0700879}
880
Yuwei Huangb181f712018-01-22 17:01:28 -0800881// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
882// a WiFi network with link-local IP address and a cellular network, then the
883// cellular candidate will still be gathered.
884TEST_F(BasicPortAllocatorTest,
885 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
886 SocketAddress wifi_link_local("169.254.0.1", 0);
887 SocketAddress cellular(IPAddress(0x12345601U), 0);
888 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
889 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
890
891 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
892 cricket::PORTALLOCATOR_DISABLE_RELAY |
893 cricket::PORTALLOCATOR_DISABLE_TCP |
894 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700895 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-22 17:01:28 -0800896 session_->StartGettingPorts();
897 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
898 kDefaultAllocationTimeout, fake_clock);
899 // Make sure we got both wifi and cell candidates.
900 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200901 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
902 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
Yuwei Huangb181f712018-01-22 17:01:28 -0800903}
904
905// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
906// a WiFi network with link-local IP address, a WiFi network with a normal IP
907// address and a cellular network, then the cellular candidate will not be
908// gathered.
909TEST_F(BasicPortAllocatorTest,
910 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
911 SocketAddress wifi(IPAddress(0x12345600U), 0);
912 SocketAddress wifi_link_local("169.254.0.1", 0);
913 SocketAddress cellular(IPAddress(0x12345601U), 0);
914 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
915 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
916 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
917
918 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
919 cricket::PORTALLOCATOR_DISABLE_RELAY |
920 cricket::PORTALLOCATOR_DISABLE_TCP |
921 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700922 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-22 17:01:28 -0800923 session_->StartGettingPorts();
924 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
925 kDefaultAllocationTimeout, fake_clock);
926 // Make sure we got only wifi candidates.
927 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200928 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
929 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
Yuwei Huangb181f712018-01-22 17:01:28 -0800930}
931
Qingsi Wange53ac042018-05-08 11:55:07 -0700932// Test that the adapter types of the Ethernet and the VPN can be correctly
933// identified so that the Ethernet has a lower network cost than the VPN, and
934// the Ethernet is not filtered out if PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is
935// set.
936TEST_F(BasicPortAllocatorTest,
937 EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent) {
938 AddInterface(kClientAddr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
939 AddInterface(kClientAddr2, "tap0", rtc::ADAPTER_TYPE_VPN);
940 allocator().set_flags(PORTALLOCATOR_DISABLE_COSTLY_NETWORKS |
941 PORTALLOCATOR_DISABLE_RELAY |
942 PORTALLOCATOR_DISABLE_TCP);
943 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
944 session_->StartGettingPorts();
945 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
946 kDefaultAllocationTimeout, fake_clock);
947 // The VPN tap0 network should be filtered out as a costly network, and we
948 // should have a UDP port and a STUN port from the Ethernet eth0.
949 ASSERT_EQ(2U, ports_.size());
950 EXPECT_EQ(ports_[0]->Network()->name(), "eth0");
951 EXPECT_EQ(ports_[1]->Network()->name(), "eth0");
952}
953
deadbeef3427f532017-07-26 16:09:33 -0700954// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
955// to gather candidates.
956TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
957 // Add three IPv6 network interfaces, but tell the allocator to only use two.
958 allocator().set_max_ipv6_networks(2);
959 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
960 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
961 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
962
963 // To simplify the test, only gather UDP host candidates.
964 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
965 PORTALLOCATOR_DISABLE_STUN |
966 PORTALLOCATOR_DISABLE_RELAY);
967
Qingsi Wange53ac042018-05-08 11:55:07 -0700968 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 16:09:33 -0700969 session_->StartGettingPorts();
970 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
971 kDefaultAllocationTimeout, fake_clock);
972 EXPECT_EQ(2U, candidates_.size());
973 // Ensure the expected two interfaces (eth0 and eth1) were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +0200974 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
975 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
deadbeef3427f532017-07-26 16:09:33 -0700976}
977
978// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
979// being used.
980TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
981 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
982 allocator().set_max_ipv6_networks(1);
983 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
984 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
985 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
986 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
987
988 // To simplify the test, only gather UDP host candidates.
989 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
990 PORTALLOCATOR_DISABLE_STUN |
991 PORTALLOCATOR_DISABLE_RELAY);
992
Qingsi Wange53ac042018-05-08 11:55:07 -0700993 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 16:09:33 -0700994 session_->StartGettingPorts();
995 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
996 kDefaultAllocationTimeout, fake_clock);
997 EXPECT_EQ(3U, candidates_.size());
998 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
999 // were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001000 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
1001 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1002 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
deadbeef3427f532017-07-26 16:09:33 -07001003}
1004
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001005// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001006TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001007 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
1008 allocator_->SetNetworkIgnoreMask(0);
Qingsi Wange53ac042018-05-08 11:55:07 -07001009 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001010 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
1011 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001012 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001013 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1014 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001015 EXPECT_EQ(1U, candidates_.size());
1016}
1017
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001018// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001019TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001020 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001021 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001022 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001023 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1024 kDefaultAllocationTimeout, fake_clock);
1025 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001026 EXPECT_EQ(4U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001027 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1028 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1029 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1030 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1031 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1032 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1033 EXPECT_TRUE(
1034 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001035}
1036
honghaiz8c404fa2015-09-28 07:59:43 -07001037// Test that when the same network interface is brought down and up, the
1038// port allocator session will restart a new allocation sequence if
1039// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001040TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001041 std::string if_name("test_net0");
1042 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 11:55:07 -07001043 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001044 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001045 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1046 kDefaultAllocationTimeout, fake_clock);
1047 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001048 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001049 candidate_allocation_done_ = false;
1050 candidates_.clear();
1051 ports_.clear();
1052
deadbeef7f1563f2017-09-15 17:40:01 -07001053 // Disable socket creation to simulate the network interface being down. When
1054 // no network interfaces are available, BasicPortAllocator will fall back to
1055 // binding to the "ANY" address, so we need to make sure that fails too.
1056 fss_->set_tcp_sockets_enabled(false);
1057 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -07001058 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001059 SIMULATED_WAIT(false, 1000, fake_clock);
1060 EXPECT_EQ(0U, candidates_.size());
1061 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -07001062
1063 // When the same interfaces are added again, new candidates/ports should be
1064 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -07001065 fss_->set_tcp_sockets_enabled(true);
1066 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -07001067 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001068 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1069 kDefaultAllocationTimeout, fake_clock);
1070 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001071 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001072}
1073
1074// Test that when the same network interface is brought down and up, the
1075// port allocator session will not restart a new allocation sequence if
1076// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001077TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001078 std::string if_name("test_net0");
1079 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 11:55:07 -07001080 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001081 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001082 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1083 kDefaultAllocationTimeout, fake_clock);
1084 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001085 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001086 session_->StopGettingPorts();
1087 candidates_.clear();
1088 ports_.clear();
1089
1090 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001091 // Wait one (simulated) second and then verify no new candidates have
1092 // appeared.
1093 SIMULATED_WAIT(false, 1000, fake_clock);
1094 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001095 EXPECT_EQ(0U, ports_.size());
1096
1097 // When the same interfaces are added again, new candidates/ports should not
1098 // be generated because the session has stopped.
1099 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001100 SIMULATED_WAIT(false, 1000, fake_clock);
1101 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001102 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001103}
1104
deadbeef1c46a352017-09-27 11:24:05 -07001105// Similar to the above tests, but tests a situation when sockets can't be
1106// bound to a network interface, then after a network change event can be.
1107// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1108TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1109 // Only test local ports to simplify test.
1110 ResetWithNoServersOrNat();
1111 // Provide a situation where the interface appears to be available, but
1112 // binding the sockets fails. See bug for description of when this can
1113 // happen.
1114 std::string if_name("test_net0");
1115 AddInterface(kClientAddr, if_name);
1116 fss_->set_tcp_sockets_enabled(false);
1117 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001118 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 11:24:05 -07001119 session_->StartGettingPorts();
1120 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1121 kDefaultAllocationTimeout, fake_clock);
1122 // Make sure we actually prevented candidates from being gathered (other than
1123 // a single TCP active candidate, since that doesn't require creating a
1124 // socket).
1125 ASSERT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001126 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001127 candidate_allocation_done_ = false;
1128
1129 // Now simulate the interface coming up, with the newfound ability to bind
1130 // sockets.
1131 fss_->set_tcp_sockets_enabled(true);
1132 fss_->set_udp_sockets_enabled(true);
1133 AddInterface(kClientAddr, if_name);
1134 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1135 kDefaultAllocationTimeout, fake_clock);
1136 // Should get UDP and TCP candidate.
1137 ASSERT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001138 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001139 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1140 // We should extend this test to also verify that a server candidate is
1141 // gathered.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001142 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001143}
1144
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001146TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001147 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001148 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07001149 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001150 session_->StartGettingPorts();
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001151 // Host and STUN candidates from kClientAddr.
pthatcher1749bc32017-02-08 13:18:00 -08001152 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001153 // UDP and STUN ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 EXPECT_EQ(2U, ports_.size());
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001155 // Host, STUN and relay candidates from kClientAddr.
deadbeef1c5e6d02017-09-15 17:46:56 -07001156 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001157 // UDP, STUN and relay ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001158 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001159 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1160 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1161 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1162 EXPECT_TRUE(
1163 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001164 // One more TCP candidate from kClientAddr.
deadbeef1c5e6d02017-09-15 17:46:56 -07001165 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001166 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001167 EXPECT_EQ(4U, ports_.size());
1168 EXPECT_TRUE(candidate_allocation_done_);
1169 // If we Stop gathering now, we shouldn't get a second "done" callback.
1170 session_->StopGettingPorts();
1171}
1172
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001173TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001174 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001175 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001176 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001177 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1178 kDefaultAllocationTimeout, fake_clock);
1179 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001180 // If we Stop gathering now, we shouldn't get a second "done" callback.
1181 session_->StopGettingPorts();
1182
1183 // All ports should have unset send-buffer sizes.
1184 CheckSendBufferSizesOfAllPorts(-1);
1185}
1186
deadbeef7f1563f2017-09-15 17:40:01 -07001187// Tests that we can get callback after StopGetAllPorts when called in the
1188// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001189TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001191 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001193 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1194 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 EXPECT_EQ(2U, ports_.size());
1196 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001197 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1198 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001199}
1200
1201// Test that we restrict client ports appropriately when a port range is set.
1202// We check the candidates for udp/stun/tcp ports, and the from address
1203// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001204TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001205 AddInterface(kClientAddr);
1206 // Check that an invalid port range fails.
1207 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1208 // Check that a null port range succeeds.
1209 EXPECT_TRUE(SetPortRange(0, 0));
1210 // Check that a valid port range succeeds.
1211 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Qingsi Wange53ac042018-05-08 11:55:07 -07001212 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001213 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001214 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1215 kDefaultAllocationTimeout, fake_clock);
1216 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001217 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001218
1219 int num_nonrelay_candidates = 0;
1220 for (const Candidate& candidate : candidates_) {
1221 // Check the port number for the UDP/STUN/TCP port objects.
1222 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001223 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 11:44:17 -07001224 ++num_nonrelay_candidates;
1225 }
1226 }
1227 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001228 // Check the port number used to connect to the relay server.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001229 EXPECT_TRUE(
1230 CheckPort(relay_server_.GetConnection(0).source(), kMinPort, kMaxPort));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001231}
1232
deadbeefe97389c2016-12-23 01:43:45 -08001233// Test that if we have no network adapters, we bind to the ANY address and
1234// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001235TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001236 // Default config uses GTURN and no NAT, so replace that with the
1237 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1238 ResetWithStunServerAndNat(kStunAddr);
1239 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1240 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1241 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1242 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1243 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1244 // TODO(deadbeef): Fix this.
1245 network_manager_.set_ipv6_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001246 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001247 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001248 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1249 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001250 EXPECT_EQ(4U, ports_.size());
1251 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1252 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1253 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1254 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1255 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1256 // The "any" address port should be in the signaled ready ports, but the host
1257 // candidate for it is useless and shouldn't be signaled. So we only have
1258 // STUN/TURN candidates.
1259 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001260 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1261 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 01:43:45 -08001262 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1263 // server.
1264 EXPECT_EQ(2,
1265 CountCandidates(candidates_, "relay", "udp",
1266 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001267}
1268
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001269// Test that when enumeration is disabled, we should not have any ports when
1270// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001271TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001272 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001273 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001274 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001275 // Expect to see no ports and no candidates.
1276 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1277 rtc::IPAddress(), rtc::IPAddress());
1278}
1279
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001280// Test that even with multiple interfaces, the result should still be a single
1281// default private, one STUN and one TURN candidate since we bind to any address
1282// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001283TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001284 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001285 AddInterface(kPrivateAddr);
1286 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001287 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001288 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001289
1290 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1291 // address set and we have no IPv6 STUN server, there should be no IPv6
1292 // candidates.
Qingsi Wange53ac042018-05-08 11:55:07 -07001293 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001294 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001295
1296 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1297 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1298 // TURN/UDP candidates.
1299 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001300 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1301 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001302}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001303
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001304// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1305// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001306TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001307 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001308 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001309 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001310 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001311 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1312 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1313 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1314 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001315 kTurnUdpExtAddr.ipaddr());
1316}
1317
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001318// Test that when adapter enumeration is disabled, for endpoints without
1319// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001320TEST_F(BasicPortAllocatorTest,
1321 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001322 ResetWithNoServersOrNat();
1323 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1324 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1325 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001326}
1327
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001328// Test that when adapter enumeration is disabled, with
1329// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1330// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001331TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001332 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1333 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001334 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001335 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001336 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1337 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001338 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001339 rtc::IPAddress(), rtc::IPAddress());
1340}
1341
1342// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001343// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1344// a NAT, there is no local candidate. However, this specified default route
1345// (kClientAddr) which was discovered when sending STUN requests, will become
1346// the srflx addresses.
Yves Gerey665174f2018-06-19 15:03:05 +02001347TEST_F(BasicPortAllocatorTest,
1348 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001349 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001350 AddInterfaceAsDefaultRoute(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001351 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001352 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001353 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1354 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001355 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1356 rtc::IPAddress(), rtc::IPAddress());
1357}
1358
1359// Test that when adapter enumeration is disabled, with
1360// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1361// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001362TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001363 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1364 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001365 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001366 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001367 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1368 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1369 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001370}
1371
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001372// Test that we disable relay over UDP, and only TCP is used when connecting to
1373// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001374TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001375 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001376 AddInterface(kClientAddr);
1377 ResetWithStunServerAndNat(kStunAddr);
1378 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001379 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001380 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1381 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1382 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001383
1384 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001385 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1386 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001387
1388 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1389 // TURN/TCP candidates.
1390 EXPECT_EQ(2U, ports_.size());
1391 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001392 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001393 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1394 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001395 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001396 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001397 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001398}
1399
Erik Språngefdce692015-06-05 09:41:26 +02001400// Disable for asan, see
1401// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1402#if !defined(ADDRESS_SANITIZER)
1403
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001404// Test that we can get OnCandidatesAllocationDone callback when all the ports
1405// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001406TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001407 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001408 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001409 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1410 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001411 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001412 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414}
1415
1416// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001417TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001418 AddInterface(kClientAddr);
1419 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001420 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001422 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1423 kDefaultAllocationTimeout, fake_clock);
1424 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001425 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001426 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1427 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1428 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1429 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1430 EXPECT_TRUE(
1431 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432}
1433
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001434#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001435
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001436// Test that we don't crash or malfunction if we can't create UDP sockets or
1437// listen on TCP sockets. We still give out a local TCP address, since
1438// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001439TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001440 AddInterface(kClientAddr);
1441 fss_->set_udp_sockets_enabled(false);
1442 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001443 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001445 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1446 kDefaultAllocationTimeout, fake_clock);
1447 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001448 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001449 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1450 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1451 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1452 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1453 EXPECT_TRUE(
1454 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001455}
1456
1457// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001458// TODO(deadbeef): Find a way to exit early here.
1459TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001460 AddInterface(kClientAddr);
1461 fss_->set_tcp_sockets_enabled(false);
1462 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001463 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001464 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001465 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001466 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001467 // In case of Relay, ports creation will succeed but sockets will fail.
1468 // There is no error reporting from RelayEntry to handle this failure.
1469}
1470
1471// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001472TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001473 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001474 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kAnyAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001475 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001476 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001477 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001478 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1479 fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001480 // UDP and TCP ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001481 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001482 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1483 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001484 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001485 // will be tried after about 3 seconds.
1486 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001487 // UDP, TCP and relay ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001488 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001489 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1490 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1491 EXPECT_TRUE(
1492 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1493 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001494 // We wait at least for a full STUN timeout, which
1495 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1496 // already passed (see above), we wait 3 seconds less than that.
1497 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1498 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001499}
1500
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001501TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001502 AddInterface(kClientAddr);
1503 AddInterface(kClientAddr2);
1504 // Allocating only host UDP ports. This is done purely for testing
1505 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001506 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1507 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001508 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001509 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001510 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1511 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001512 ASSERT_EQ(2U, candidates_.size());
1513 EXPECT_EQ(2U, ports_.size());
1514 // Candidates priorities should be different.
1515 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1516}
1517
1518// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001519TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001520 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001521 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001522 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001523 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1524 kDefaultAllocationTimeout, fake_clock);
1525 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001526 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001527 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001528}
1529
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001530// Test that the allocator session uses the candidate filter it's created with,
1531// rather than the filter of its parent allocator.
1532// The filter of the allocator should only affect the next gathering phase,
1533// according to JSEP, which means the *next* allocator session returned.
1534TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1535 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001536 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001537 // Set candidate filter *after* creating the session. Should have no effect.
1538 allocator().set_candidate_filter(CF_RELAY);
1539 session_->StartGettingPorts();
1540 // 7 candidates and 4 ports is what we would normally get (see the
1541 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001542 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1543 kDefaultAllocationTimeout, fake_clock);
1544 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001545 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001546}
1547
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001548// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1549// This test also verifies that when the allocator is only allowed to use
1550// relay (i.e. IceTransportsType is relay), the raddr is an empty
1551// address with the correct family. This is to prevent any local
1552// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001553TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001554 AddInterface(kClientAddr);
1555 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001556 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001557 allocator().set_candidate_filter(CF_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001558 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001559 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001560 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1561 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001562 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1563 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001564
1565 EXPECT_EQ(1U, candidates_.size());
1566 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001567 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1568 EXPECT_EQ(
1569 candidates_[0].related_address(),
1570 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001571}
1572
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001573TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001574 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001575 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1576 allocator().set_candidate_filter(CF_HOST);
Qingsi Wange53ac042018-05-08 11:55:07 -07001577 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001578 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001579 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1580 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001581 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1582 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001583 for (const Candidate& candidate : candidates_) {
1584 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001585 }
1586}
1587
1588// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001589TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001590 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001591 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001593 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1594 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001595 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001596 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001597 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1598 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599 // Host is behind NAT, no private address will be exposed. Hence only UDP
1600 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001601 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1602 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001603 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1604 EXPECT_EQ(
1605 candidates_[0].related_address(),
1606 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001607}
1608
1609// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001610TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001611 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001612 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1613 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001614 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001615 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001616 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1617 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001618 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001619 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001620 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001621 for (const Candidate& candidate : candidates_) {
1622 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623 }
1624}
1625
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001626// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001627TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001628 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001629 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001630 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001631 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1632 kDefaultAllocationTimeout, fake_clock);
1633 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001634 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1635 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1636 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001637 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001638 for (const Candidate& candidate : candidates_) {
1639 EXPECT_EQ(kIceUfrag0, candidate.username());
1640 EXPECT_EQ(kIcePwd0, candidate.password());
1641 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001642}
1643
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001644// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1645// is allocated for udp and stun. Also verify there is only one candidate
1646// (local) if stun candidate is same as local candidate, which will be the case
1647// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001648TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001649 AddInterface(kClientAddr);
1650 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001651 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001652 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001653 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001654 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1655 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001656 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001657 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001658 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1659 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001660}
1661
1662// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1663// is allocated for udp and stun. In this test we should expect both stun and
1664// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001665TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001666 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001667 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668
1669 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001670 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001671 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001672 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001673 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1674 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001675 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001676 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1677 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1678 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001679 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1680 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001681 EXPECT_EQ(3U, candidates_.size());
1682}
1683
deadbeefc5d0d952015-07-16 10:22:21 -07001684// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001685TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001686 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001687 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001688 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001689 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001690
1691 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1692
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001693 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001694 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001695 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1696 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001697
Qingsi Wange53ac042018-05-08 11:55:07 -07001698 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001699 session_->StartGettingPorts();
1700
deadbeef7f1563f2017-09-15 17:40:01 -07001701 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1702 kDefaultAllocationTimeout, fake_clock);
1703 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001704 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001705 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1706 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1707 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1708 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1709 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001710}
1711
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001712// Test that if prune_turn_ports is set, TCP TURN port will not be used
1713// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1714TEST_F(BasicPortAllocatorTest,
1715 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1716 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1717 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1718 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001719
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001720 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001721}
1722
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001723// Test that if prune_turn_ports is set, TCP TURN port will not be used
1724// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1725TEST_F(BasicPortAllocatorTest,
1726 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1727 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1728 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1729 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001730
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001731 TestUdpTurnPortPrunesTcpTurnPort();
1732}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001733
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001734// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1735// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1736TEST_F(BasicPortAllocatorTest,
1737 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1738 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1739 // first.
1740 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1741 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001742
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001743 TestIPv6TurnPortPrunesIPv4TurnPort();
1744}
1745
1746// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1747// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1748TEST_F(BasicPortAllocatorTest,
1749 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1750 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1751 // first.
1752 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1753 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1754
1755 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001756}
1757
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001758// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001759// will has its own set of TurnPorts based on their priorities, in the default
1760// case where no transit delay is set.
1761TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1762 TestEachInterfaceHasItsOwnTurnPorts();
1763}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001764
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001765// Tests that if prune_turn_ports is set, each network interface
1766// will has its own set of TurnPorts based on their priorities, given that
1767// IPv4/TCP TURN port becomes ready first.
1768TEST_F(BasicPortAllocatorTest,
1769 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1770 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1771 // becomes ready last.
1772 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1773 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1774 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1775 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001776
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001777 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001778}
1779
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001780// Testing DNS resolve for the TURN server, this will test AllocationSequence
1781// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001782// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1783// appears that it doesn't currently because of the DNS look up not
1784// using the fake clock.
1785TEST_F(BasicPortAllocatorTestWithRealClock,
1786 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001787 // This test relies on a real query for "localhost", so it won't work on an
1788 // IPv6-only machine.
1789 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001790 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001791 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001792 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001793 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001794 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001795 RelayServerConfig turn_server(RELAY_TURN);
1796 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001797 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001798 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001799 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001800 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001801
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001802 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001803 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001804 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1805 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001806
Qingsi Wange53ac042018-05-08 11:55:07 -07001807 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001808 session_->StartGettingPorts();
1809
1810 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1811}
1812
1813// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1814// is allocated for udp/stun/turn. In this test we should expect all local,
1815// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001816TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001817 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001818 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001819
1820 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1821
1822 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001823 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1824 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001825
Qingsi Wange53ac042018-05-08 11:55:07 -07001826 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001827 session_->StartGettingPorts();
1828
deadbeef7f1563f2017-09-15 17:40:01 -07001829 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1830 kDefaultAllocationTimeout, fake_clock);
1831 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001832 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001833 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1834 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1835 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1836 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1837 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001838 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1839 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001840 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001841 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1842 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001843 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1844 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1845}
1846
1847// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1848// server is also used as the STUN server, we should get 'local', 'stun', and
1849// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001850TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001851 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001852 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001853 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001854 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1855
1856 // Must set the step delay to 0 to make sure the relay allocation phase is
1857 // started before the STUN candidates are obtained, so that the STUN binding
1858 // response is processed when both StunPort and TurnPort exist to reproduce
1859 // webrtc issue 3537.
1860 allocator_->set_step_delay(0);
1861 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001862 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1863 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001864
Qingsi Wange53ac042018-05-08 11:55:07 -07001865 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001866 session_->StartGettingPorts();
1867
deadbeef7f1563f2017-09-15 17:40:01 -07001868 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1869 kDefaultAllocationTimeout, fake_clock);
1870 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001871 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001872 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001873 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1874 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1875 &stun_candidate));
1876 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1877 candidates_, "relay", "udp",
1878 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1879 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001880
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001881 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001882 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1883 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001884 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1885 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1886}
1887
deadbeefc5d0d952015-07-16 10:22:21 -07001888// Test that when only a TCP TURN server is available, we do NOT use it as
1889// a UDP STUN server, as this could leak our IP address. Thus we should only
1890// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001891TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001892 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001893 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001894 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001895 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1896
1897 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001898 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1899 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001900
Qingsi Wange53ac042018-05-08 11:55:07 -07001901 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001902 session_->StartGettingPorts();
1903
deadbeef7f1563f2017-09-15 17:40:01 -07001904 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1905 kDefaultAllocationTimeout, fake_clock);
1906 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001907 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001908 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1909 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1910 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001911 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1912 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1913}
1914
1915// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1916// TURN server is used as the STUN server and we get 'local', 'stun', and
1917// 'relay' candidates.
1918// TODO(deadbeef): Remove this test when support for non-shared socket mode
1919// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001920TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001921 AddInterface(kClientAddr);
1922 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001923 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001924 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1925
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001926 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001927
Qingsi Wange53ac042018-05-08 11:55:07 -07001928 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001929 session_->StartGettingPorts();
1930
deadbeef7f1563f2017-09-15 17:40:01 -07001931 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1932 kDefaultAllocationTimeout, fake_clock);
1933 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001934 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001935 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001936 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001937 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1938 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1939 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001940 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001941 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1942 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1943 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001944 // Not using shared socket, so the STUN request's server reflexive address
1945 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001946 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001947
deadbeefc5d0d952015-07-16 10:22:21 -07001948 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1949 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1950 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1951}
1952
1953// Test that even when both a STUN and TURN server are configured, the TURN
1954// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001955TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001956 AddInterface(kClientAddr);
1957 // Configure with STUN server but destroy it, so we can ensure that it's
1958 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001959 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001960 stun_server_.reset();
1961 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1962
1963 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001964 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1965 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001966
Qingsi Wange53ac042018-05-08 11:55:07 -07001967 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001968 session_->StartGettingPorts();
1969
pthatcher1749bc32017-02-08 13:18:00 -08001970 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1971 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001972 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001973 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001974 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1975 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1976 &stun_candidate));
1977 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1978 candidates_, "relay", "udp",
1979 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1980 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001981
1982 // Don't bother waiting for STUN timeout, since we already verified
1983 // that we got a STUN candidate from the TURN server.
1984}
1985
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001986// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1987// and fail to generate STUN candidate, local UDP candidate is generated
1988// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001989TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001990 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1991 PORTALLOCATOR_DISABLE_TCP |
1992 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001993 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001994 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kAnyAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001995 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001996 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001997 session_->StartGettingPorts();
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001998 ASSERT_EQ_SIMULATED_WAIT(1U, candidates_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 13:18:00 -08001999 fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00002000 // UDP ports on kClientAddr.
2001 EXPECT_EQ(1U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002002 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07002003 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08002004 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2005 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002006 EXPECT_EQ(1U, candidates_.size());
2007}
2008
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00002009// Test that any address ports that are redundant do not surface.
2010TEST_F(BasicPortAllocatorTest, RedundantAnyAddressPortsDoNotSurface) {
2011 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2012 PORTALLOCATOR_DISABLE_TCP |
2013 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2014 AddInterface(kClientAddr);
2015 // The any address ports will be duplicates of kClientAddr.
2016 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr.ipaddr());
2017 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2018 session_->StartGettingPorts();
2019 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2020 fake_clock);
2021 EXPECT_EQ(1U, ports_.size());
2022 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
2023}
2024
2025// Test that candidates from the any address ports are not pruned if the
2026// explicit binding to enumerated networks fails.
2027TEST_F(BasicPortAllocatorTest,
2028 CandidatesFromAnyAddressPortsCanSurfaceWhenExplicitBindingFails) {
2029 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2030 PORTALLOCATOR_DISABLE_TCP |
2031 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2032 AddInterface(kClientAddr);
2033 fss_->SetUnbindableIps({kClientAddr.ipaddr()});
2034 // The any address ports will be duplicates of kClientAddr, but the explict
2035 // binding will fail.
2036 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr.ipaddr());
2037 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2038 session_->StartGettingPorts();
2039 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2040 fake_clock);
2041 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kAnyAddr));
2042 EXPECT_EQ(1U, candidates_.size());
2043 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2044}
2045
2046// Test that for an endpoint whose network enumeration only reveals one address
2047// (kClientAddr), it can observe a different address when binding to the "any"
2048// address. BasicPortAllocator should detect this and surface candidates for
2049// each address.
2050TEST_F(BasicPortAllocatorTest,
2051 CandidatesFromAnyAddressPortsCanSurfaceIfNotRedundant) {
2052 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2053 PORTALLOCATOR_DISABLE_TCP |
2054 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2055 AddInterface(kClientAddr);
2056 // When bound to the any address, the port allocator should discover the
2057 // alternative local address.
2058 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr2.ipaddr());
2059 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2060 session_->StartGettingPorts();
2061 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2062 fake_clock);
2063 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kAnyAddr));
2064 EXPECT_EQ(2U, candidates_.size());
2065 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2066 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
2067}
2068
2069// Test that any address ports and their candidates are eventually signaled
2070// after the maximum wait interval for the completion of candidate allocation,
2071// when the any address ports and candidates are not redundant.
2072TEST_F(BasicPortAllocatorTest,
2073 GetAnyAddressPortsAfterMaximumWaitForCandidateAllocationDone) {
2074 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
2075 AddInterface(kClientAddr);
2076 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr2.ipaddr());
2077 // STUN binding request and TURN allocation request will time out.
2078 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
2079 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2080 session_->StartGettingPorts();
2081 SIMULATED_WAIT(false, kMaxWaitMsBeforeSignalingAnyAddressPortsAndCandidates,
2082 fake_clock);
2083 EXPECT_EQ(2U, candidates_.size());
2084 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2085 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
2086 SIMULATED_WAIT(false, 1, fake_clock);
2087 EXPECT_FALSE(candidate_allocation_done_);
2088 // Candidates from the any address ports.
2089 EXPECT_EQ(6U, candidates_.size());
2090 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
2091 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr2));
2092 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr));
2093 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr2));
2094}
2095
2096// Test that the TCP port with the wildcard address is signaled if no ports are
2097// bound to enuemrated networks.
2098TEST_F(BasicPortAllocatorTest,
2099 GetAnyAddressTcpPortWhenNoPortsBoundToEnumeratedNetworks) {
2100 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
2101 AddInterface(kClientAddr);
2102 fss_->SetUnbindableIps({kClientAddr.ipaddr()});
2103 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2104 session_->StartGettingPorts();
2105 SIMULATED_WAIT(false,
2106 kMaxWaitMsBeforeSignalingAnyAddressPortsAndCandidates + 1,
2107 fake_clock);
2108 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
2109}
2110
2111// Test that the STUN candidate from the any address port can still surface, if
2112// it is gathered after this port is signaled, .
2113TEST_F(BasicPortAllocatorTest,
2114 StunCandidateFromAnyAddressPortsGatheredLateCanBeSignaled) {
2115 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
2116 AddInterface(kClientAddr);
2117 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr2.ipaddr());
2118 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
2119 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr2);
2120 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2121 session_->StartGettingPorts();
2122 SIMULATED_WAIT(false,
2123 kMaxWaitMsBeforeSignalingAnyAddressPortsAndCandidates + 1000,
2124 fake_clock);
2125 EXPECT_FALSE(candidate_allocation_done_);
2126 EXPECT_EQ(4U, candidates_.size());
2127 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2128 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
2129 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
2130 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr2));
2131 fss_->ClearRules();
2132 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2133 kDefaultAllocationTimeout, fake_clock);
2134 EXPECT_EQ(7U, candidates_.size());
2135 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
2136 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr2));
2137 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr));
2138}
2139
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002140// Test that when the NetworkManager doesn't have permission to enumerate
2141// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2142// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002143TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002144 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2145 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002146 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07002147 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002148 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2149 PORTALLOCATOR_DISABLE_TCP |
2150 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2151 EXPECT_EQ(0U,
2152 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 11:55:07 -07002153 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002154 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002155 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002156 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2157 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002158 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002159 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002160 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002161}
2162
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002163// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002164TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002165 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2166 PORTALLOCATOR_ENABLE_IPV6 |
2167 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002168 AddInterface(kClientIPv6Addr);
2169 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002170 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002171 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002172 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002173 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002174 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002175 EXPECT_EQ(4U, ports_.size());
2176 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002177 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2178 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2179 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2180 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002181}
honghaiz98db68f2015-09-29 07:58:17 -07002182
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002183TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002184 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002185 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002186 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002187 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002188 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002189 EXPECT_EQ(2U, ports_.size());
2190 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002191 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002192
2193 // After stopping getting ports, adding a new interface will not start
2194 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002195 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002196 candidates_.clear();
2197 ports_.clear();
2198 candidate_allocation_done_ = false;
2199 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002200 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002201 EXPECT_EQ(0U, candidates_.size());
2202 EXPECT_EQ(0U, ports_.size());
2203}
2204
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002205TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002206 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002207 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002208 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002209 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002210 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002211 EXPECT_EQ(2U, ports_.size());
2212 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002213 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002214
2215 // After clearing getting ports, adding a new interface will start getting
2216 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002217 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002218 candidates_.clear();
2219 ports_.clear();
2220 candidate_allocation_done_ = false;
2221 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002222 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002223 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002224 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2225 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002226}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002227
2228// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2229// a pooled session is taken out of the pool.
2230TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2231 AddInterface(kClientAddr);
2232 int pool_size = 1;
2233 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002234 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002235 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002236 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002237 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2238 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002239 // Expect that when TakePooledSession is called,
2240 // UpdateTransportInformationInternal will be called and the
2241 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2242 // candidates.
2243 session_ =
2244 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2245 ASSERT_NE(nullptr, session_.get());
2246 auto ready_ports = session_->ReadyPorts();
2247 auto candidates = session_->ReadyCandidates();
2248 EXPECT_FALSE(ready_ports.empty());
2249 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002250 for (const PortInterface* port_interface : ready_ports) {
2251 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002252 EXPECT_EQ(kContentName, port->content_name());
2253 EXPECT_EQ(1, port->component());
2254 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2255 EXPECT_EQ(kIcePwd0, port->password());
2256 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002257 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002258 EXPECT_EQ(1, candidate.component());
2259 EXPECT_EQ(kIceUfrag0, candidate.username());
2260 EXPECT_EQ(kIcePwd0, candidate.password());
2261 }
2262}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002263
2264// Test that a new candidate filter takes effect even on already-gathered
2265// candidates.
2266TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2267 AddInterface(kClientAddr);
2268 int pool_size = 1;
2269 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002270 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002271 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2272 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002273 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2274 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002275 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2276 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2277 allocator_->set_candidate_filter(CF_RELAY);
2278 // Assume that when TakePooledSession is called, the candidate filter will be
2279 // applied to the pooled session. This is tested by PortAllocatorTest.
2280 session_ =
2281 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2282 ASSERT_NE(nullptr, session_.get());
2283 auto candidates = session_->ReadyCandidates();
2284 auto ports = session_->ReadyPorts();
2285 // Sanity check that the number of candidates and ports decreased.
2286 EXPECT_GT(initial_candidates_size, candidates.size());
2287 EXPECT_GT(initial_ports_size, ports.size());
2288 for (const PortInterface* port : ports) {
2289 // Expect only relay ports.
2290 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2291 }
2292 for (const Candidate& candidate : candidates) {
2293 // Expect only relay candidates now that the filter is applied.
2294 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2295 // Expect that the raddr is emptied due to the CF_RELAY filter.
2296 EXPECT_EQ(candidate.related_address(),
2297 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2298 }
2299}
2300
Qingsi Wang4ff54432018-03-01 18:25:20 -08002301TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2302 const int pool_size = 1;
2303 const int expected_stun_keepalive_interval = 123;
2304 AddInterface(kClientAddr);
2305 allocator_->SetConfiguration(allocator_->stun_servers(),
2306 allocator_->turn_servers(), pool_size, false,
2307 nullptr, expected_stun_keepalive_interval);
2308 auto* pooled_session = allocator_->GetPooledSession();
2309 ASSERT_NE(nullptr, pooled_session);
2310 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2311 kDefaultAllocationTimeout, fake_clock);
2312 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2313 expected_stun_keepalive_interval);
2314}
2315
2316TEST_F(BasicPortAllocatorTest,
2317 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2318 const int pool_size = 1;
2319 AddInterface(kClientAddr);
2320 allocator_->SetConfiguration(allocator_->stun_servers(),
2321 allocator_->turn_servers(), pool_size, false,
2322 nullptr, 123 /* stun keepalive interval */);
2323 auto* pooled_session = allocator_->GetPooledSession();
2324 ASSERT_NE(nullptr, pooled_session);
2325 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2326 kDefaultAllocationTimeout, fake_clock);
2327 const int expected_stun_keepalive_interval = 321;
2328 allocator_->SetConfiguration(allocator_->stun_servers(),
2329 allocator_->turn_servers(), pool_size, false,
2330 nullptr, expected_stun_keepalive_interval);
2331 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2332 expected_stun_keepalive_interval);
2333}
2334
2335TEST_F(BasicPortAllocatorTest,
2336 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2337 const int pool_size = 1;
2338 const int expected_stun_keepalive_interval = 123;
2339 AddInterface(kClientAddr);
2340 allocator_->set_flags(allocator().flags() |
2341 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2342 allocator_->SetConfiguration(allocator_->stun_servers(),
2343 allocator_->turn_servers(), pool_size, false,
2344 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002345 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002346 session_->StartGettingPorts();
2347 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2348 kDefaultAllocationTimeout, fake_clock);
2349 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2350 expected_stun_keepalive_interval);
2351}
2352
2353TEST_F(BasicPortAllocatorTest,
2354 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2355 const int pool_size = 1;
2356 const int expected_stun_keepalive_interval = 123;
2357 AddInterface(kClientAddr);
2358 allocator_->set_flags(allocator().flags() &
2359 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2360 allocator_->SetConfiguration(allocator_->stun_servers(),
2361 allocator_->turn_servers(), pool_size, false,
2362 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002363 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002364 session_->StartGettingPorts();
2365 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2366 kDefaultAllocationTimeout, fake_clock);
2367 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2368 expected_stun_keepalive_interval);
2369}
2370
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002371} // namespace cricket