blob: 42699a9c89b266af585a6096fa3610ff5b862f0d [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();
pthatcher1749bc32017-02-08 13:18:00 -08001151 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001153 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001155 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1156 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1157 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1158 EXPECT_TRUE(
1159 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
deadbeef1c5e6d02017-09-15 17:46:56 -07001160 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001161 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162 EXPECT_EQ(4U, ports_.size());
1163 EXPECT_TRUE(candidate_allocation_done_);
1164 // If we Stop gathering now, we shouldn't get a second "done" callback.
1165 session_->StopGettingPorts();
1166}
1167
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001168TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001169 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001170 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001171 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001172 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1173 kDefaultAllocationTimeout, fake_clock);
1174 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001175 // If we Stop gathering now, we shouldn't get a second "done" callback.
1176 session_->StopGettingPorts();
1177
1178 // All ports should have unset send-buffer sizes.
1179 CheckSendBufferSizesOfAllPorts(-1);
1180}
1181
deadbeef7f1563f2017-09-15 17:40:01 -07001182// Tests that we can get callback after StopGetAllPorts when called in the
1183// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001184TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001185 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001186 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001187 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001188 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1189 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 EXPECT_EQ(2U, ports_.size());
1191 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001192 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1193 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001194}
1195
1196// Test that we restrict client ports appropriately when a port range is set.
1197// We check the candidates for udp/stun/tcp ports, and the from address
1198// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001199TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001200 AddInterface(kClientAddr);
1201 // Check that an invalid port range fails.
1202 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1203 // Check that a null port range succeeds.
1204 EXPECT_TRUE(SetPortRange(0, 0));
1205 // Check that a valid port range succeeds.
1206 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Qingsi Wange53ac042018-05-08 11:55:07 -07001207 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001208 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001209 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1210 kDefaultAllocationTimeout, fake_clock);
1211 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001213
1214 int num_nonrelay_candidates = 0;
1215 for (const Candidate& candidate : candidates_) {
1216 // Check the port number for the UDP/STUN/TCP port objects.
1217 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001218 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 11:44:17 -07001219 ++num_nonrelay_candidates;
1220 }
1221 }
1222 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001223 // Check the port number used to connect to the relay server.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001224 EXPECT_TRUE(
1225 CheckPort(relay_server_.GetConnection(0).source(), kMinPort, kMaxPort));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001226}
1227
deadbeefe97389c2016-12-23 01:43:45 -08001228// Test that if we have no network adapters, we bind to the ANY address and
1229// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001230TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001231 // Default config uses GTURN and no NAT, so replace that with the
1232 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1233 ResetWithStunServerAndNat(kStunAddr);
1234 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1235 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1236 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1237 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1238 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1239 // TODO(deadbeef): Fix this.
1240 network_manager_.set_ipv6_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001241 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001242 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001243 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1244 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001245 EXPECT_EQ(4U, ports_.size());
1246 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1247 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1248 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1249 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1250 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1251 // The "any" address port should be in the signaled ready ports, but the host
1252 // candidate for it is useless and shouldn't be signaled. So we only have
1253 // STUN/TURN candidates.
1254 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001255 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1256 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 01:43:45 -08001257 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1258 // server.
1259 EXPECT_EQ(2,
1260 CountCandidates(candidates_, "relay", "udp",
1261 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001262}
1263
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001264// Test that when enumeration is disabled, we should not have any ports when
1265// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001266TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001267 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001268 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001269 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001270 // Expect to see no ports and no candidates.
1271 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1272 rtc::IPAddress(), rtc::IPAddress());
1273}
1274
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001275// Test that even with multiple interfaces, the result should still be a single
1276// default private, one STUN and one TURN candidate since we bind to any address
1277// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001278TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001279 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001280 AddInterface(kPrivateAddr);
1281 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001282 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001283 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001284
1285 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1286 // address set and we have no IPv6 STUN server, there should be no IPv6
1287 // candidates.
Qingsi Wange53ac042018-05-08 11:55:07 -07001288 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001289 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001290
1291 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1292 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1293 // TURN/UDP candidates.
1294 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001295 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1296 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001297}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001298
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001299// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1300// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001301TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001302 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001303 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001304 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001305 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001306 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1307 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1308 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1309 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001310 kTurnUdpExtAddr.ipaddr());
1311}
1312
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001313// Test that when adapter enumeration is disabled, for endpoints without
1314// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315TEST_F(BasicPortAllocatorTest,
1316 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001317 ResetWithNoServersOrNat();
1318 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1319 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1320 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001321}
1322
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001323// Test that when adapter enumeration is disabled, with
1324// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1325// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001326TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001327 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1328 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001329 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001330 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001331 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1332 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001333 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001334 rtc::IPAddress(), rtc::IPAddress());
1335}
1336
1337// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001338// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1339// a NAT, there is no local candidate. However, this specified default route
1340// (kClientAddr) which was discovered when sending STUN requests, will become
1341// the srflx addresses.
Yves Gerey665174f2018-06-19 15:03:05 +02001342TEST_F(BasicPortAllocatorTest,
1343 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001344 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001345 AddInterfaceAsDefaultRoute(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001346 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001347 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001348 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1349 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001350 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1351 rtc::IPAddress(), rtc::IPAddress());
1352}
1353
1354// Test that when adapter enumeration is disabled, with
1355// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1356// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001357TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001358 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1359 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001360 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001361 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001362 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1363 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1364 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001365}
1366
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001367// Test that we disable relay over UDP, and only TCP is used when connecting to
1368// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001369TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001370 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001371 AddInterface(kClientAddr);
1372 ResetWithStunServerAndNat(kStunAddr);
1373 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001374 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001375 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1376 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1377 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001378
1379 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001380 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1381 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001382
1383 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1384 // TURN/TCP candidates.
1385 EXPECT_EQ(2U, ports_.size());
1386 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001387 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001388 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1389 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001390 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001391 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001392 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001393}
1394
Erik Språngefdce692015-06-05 09:41:26 +02001395// Disable for asan, see
1396// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1397#if !defined(ADDRESS_SANITIZER)
1398
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001399// Test that we can get OnCandidatesAllocationDone callback when all the ports
1400// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001401TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001402 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001403 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001404 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1405 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001406 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001407 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001408 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001409}
1410
1411// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001412TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 AddInterface(kClientAddr);
1414 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001415 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001417 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1418 kDefaultAllocationTimeout, fake_clock);
1419 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001420 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001421 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1422 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1423 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1424 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1425 EXPECT_TRUE(
1426 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001427}
1428
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001429#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001430
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001431// Test that we don't crash or malfunction if we can't create UDP sockets or
1432// listen on TCP sockets. We still give out a local TCP address, since
1433// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001434TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001435 AddInterface(kClientAddr);
1436 fss_->set_udp_sockets_enabled(false);
1437 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001438 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001439 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001440 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1441 kDefaultAllocationTimeout, fake_clock);
1442 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001443 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001444 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1445 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1446 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1447 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1448 EXPECT_TRUE(
1449 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450}
1451
1452// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001453// TODO(deadbeef): Find a way to exit early here.
1454TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001455 AddInterface(kClientAddr);
1456 fss_->set_tcp_sockets_enabled(false);
1457 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001458 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001459 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001460 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001461 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001462 // In case of Relay, ports creation will succeed but sockets will fail.
1463 // There is no error reporting from RelayEntry to handle this failure.
1464}
1465
1466// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001467TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001468 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1469 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001470 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001471 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001472 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1473 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001474 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001475 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1476 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001477 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001478 // will be tried after about 3 seconds.
1479 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001480 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001481 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1482 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1483 EXPECT_TRUE(
1484 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1485 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001486 // We wait at least for a full STUN timeout, which
1487 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1488 // already passed (see above), we wait 3 seconds less than that.
1489 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1490 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001491}
1492
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001493TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001494 AddInterface(kClientAddr);
1495 AddInterface(kClientAddr2);
1496 // Allocating only host UDP ports. This is done purely for testing
1497 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001498 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1499 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001500 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001501 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001502 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1503 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001504 ASSERT_EQ(2U, candidates_.size());
1505 EXPECT_EQ(2U, ports_.size());
1506 // Candidates priorities should be different.
1507 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1508}
1509
1510// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001511TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001512 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001513 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001514 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001515 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1516 kDefaultAllocationTimeout, fake_clock);
1517 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001518 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001519 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001520}
1521
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001522// Test that the allocator session uses the candidate filter it's created with,
1523// rather than the filter of its parent allocator.
1524// The filter of the allocator should only affect the next gathering phase,
1525// according to JSEP, which means the *next* allocator session returned.
1526TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1527 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001528 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001529 // Set candidate filter *after* creating the session. Should have no effect.
1530 allocator().set_candidate_filter(CF_RELAY);
1531 session_->StartGettingPorts();
1532 // 7 candidates and 4 ports is what we would normally get (see the
1533 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001534 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1535 kDefaultAllocationTimeout, fake_clock);
1536 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001537 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001538}
1539
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001540// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1541// This test also verifies that when the allocator is only allowed to use
1542// relay (i.e. IceTransportsType is relay), the raddr is an empty
1543// address with the correct family. This is to prevent any local
1544// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001545TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001546 AddInterface(kClientAddr);
1547 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001548 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001549 allocator().set_candidate_filter(CF_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001550 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001551 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001552 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1553 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001554 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1555 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001556
1557 EXPECT_EQ(1U, candidates_.size());
1558 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001559 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1560 EXPECT_EQ(
1561 candidates_[0].related_address(),
1562 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001563}
1564
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001565TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001566 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001567 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1568 allocator().set_candidate_filter(CF_HOST);
Qingsi Wange53ac042018-05-08 11:55:07 -07001569 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001570 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001571 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1572 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001573 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1574 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001575 for (const Candidate& candidate : candidates_) {
1576 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001577 }
1578}
1579
1580// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001581TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001582 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001583 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001584
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001585 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1586 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001587 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001588 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001589 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1590 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001591 // Host is behind NAT, no private address will be exposed. Hence only UDP
1592 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001593 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1594 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001595 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1596 EXPECT_EQ(
1597 candidates_[0].related_address(),
1598 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599}
1600
1601// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001602TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001603 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001604 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1605 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001606 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001607 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001608 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1609 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001610 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001611 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001612 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001613 for (const Candidate& candidate : candidates_) {
1614 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001615 }
1616}
1617
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001618// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001619TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001620 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001621 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001622 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001623 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1624 kDefaultAllocationTimeout, fake_clock);
1625 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001626 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1627 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1628 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001629 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001630 for (const Candidate& candidate : candidates_) {
1631 EXPECT_EQ(kIceUfrag0, candidate.username());
1632 EXPECT_EQ(kIcePwd0, candidate.password());
1633 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001634}
1635
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001636// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1637// is allocated for udp and stun. Also verify there is only one candidate
1638// (local) if stun candidate is same as local candidate, which will be the case
1639// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001640TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001641 AddInterface(kClientAddr);
1642 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001643 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001644 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001645 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001646 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1647 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001648 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001649 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001650 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1651 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001652}
1653
1654// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1655// is allocated for udp and stun. In this test we should expect both stun and
1656// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001657TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001659 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001660
1661 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001662 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001663 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001664 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001665 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1666 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001667 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001668 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1669 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1670 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001671 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1672 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001673 EXPECT_EQ(3U, candidates_.size());
1674}
1675
deadbeefc5d0d952015-07-16 10:22:21 -07001676// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001677TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001678 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001679 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001680 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001681 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001682
1683 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1684
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001685 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001686 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001687 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1688 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001689
Qingsi Wange53ac042018-05-08 11:55:07 -07001690 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001691 session_->StartGettingPorts();
1692
deadbeef7f1563f2017-09-15 17:40:01 -07001693 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1694 kDefaultAllocationTimeout, fake_clock);
1695 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001696 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001697 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1698 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1699 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1700 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1701 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001702}
1703
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001704// Test that if prune_turn_ports is set, TCP TURN port will not be used
1705// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1706TEST_F(BasicPortAllocatorTest,
1707 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1708 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1709 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1710 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001711
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001712 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001713}
1714
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001715// Test that if prune_turn_ports is set, TCP TURN port will not be used
1716// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1717TEST_F(BasicPortAllocatorTest,
1718 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1719 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1720 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1721 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001722
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001723 TestUdpTurnPortPrunesTcpTurnPort();
1724}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001725
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001726// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1727// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1728TEST_F(BasicPortAllocatorTest,
1729 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1730 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1731 // first.
1732 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1733 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001734
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001735 TestIPv6TurnPortPrunesIPv4TurnPort();
1736}
1737
1738// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1739// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1740TEST_F(BasicPortAllocatorTest,
1741 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1742 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1743 // first.
1744 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1745 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1746
1747 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001748}
1749
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001750// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001751// will has its own set of TurnPorts based on their priorities, in the default
1752// case where no transit delay is set.
1753TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1754 TestEachInterfaceHasItsOwnTurnPorts();
1755}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001756
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001757// Tests that if prune_turn_ports is set, each network interface
1758// will has its own set of TurnPorts based on their priorities, given that
1759// IPv4/TCP TURN port becomes ready first.
1760TEST_F(BasicPortAllocatorTest,
1761 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1762 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1763 // becomes ready last.
1764 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1765 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1766 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1767 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001768
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001769 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001770}
1771
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001772// Testing DNS resolve for the TURN server, this will test AllocationSequence
1773// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001774// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1775// appears that it doesn't currently because of the DNS look up not
1776// using the fake clock.
1777TEST_F(BasicPortAllocatorTestWithRealClock,
1778 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001779 // This test relies on a real query for "localhost", so it won't work on an
1780 // IPv6-only machine.
1781 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001782 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001783 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001784 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001785 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001786 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001787 RelayServerConfig turn_server(RELAY_TURN);
1788 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001789 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001790 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001791 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001792 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001793
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001794 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001795 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001796 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1797 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001798
Qingsi Wange53ac042018-05-08 11:55:07 -07001799 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001800 session_->StartGettingPorts();
1801
1802 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1803}
1804
1805// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1806// is allocated for udp/stun/turn. In this test we should expect all local,
1807// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001808TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001809 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001810 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001811
1812 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1813
1814 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001815 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1816 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001817
Qingsi Wange53ac042018-05-08 11:55:07 -07001818 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001819 session_->StartGettingPorts();
1820
deadbeef7f1563f2017-09-15 17:40:01 -07001821 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1822 kDefaultAllocationTimeout, fake_clock);
1823 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001824 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001825 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1826 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1827 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1828 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1829 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001830 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1831 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001832 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001833 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1834 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001835 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1836 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1837}
1838
1839// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1840// server is also used as the STUN server, we should get 'local', 'stun', and
1841// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001842TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001843 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001844 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001845 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001846 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1847
1848 // Must set the step delay to 0 to make sure the relay allocation phase is
1849 // started before the STUN candidates are obtained, so that the STUN binding
1850 // response is processed when both StunPort and TurnPort exist to reproduce
1851 // webrtc issue 3537.
1852 allocator_->set_step_delay(0);
1853 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001854 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1855 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001856
Qingsi Wange53ac042018-05-08 11:55:07 -07001857 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001858 session_->StartGettingPorts();
1859
deadbeef7f1563f2017-09-15 17:40:01 -07001860 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1861 kDefaultAllocationTimeout, fake_clock);
1862 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001863 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001864 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001865 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1866 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1867 &stun_candidate));
1868 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1869 candidates_, "relay", "udp",
1870 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1871 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001872
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001873 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001874 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1875 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001876 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1877 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1878}
1879
deadbeefc5d0d952015-07-16 10:22:21 -07001880// Test that when only a TCP TURN server is available, we do NOT use it as
1881// a UDP STUN server, as this could leak our IP address. Thus we should only
1882// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001883TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001884 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001885 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001886 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001887 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1888
1889 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001890 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1891 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001892
Qingsi Wange53ac042018-05-08 11:55:07 -07001893 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001894 session_->StartGettingPorts();
1895
deadbeef7f1563f2017-09-15 17:40:01 -07001896 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1897 kDefaultAllocationTimeout, fake_clock);
1898 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001899 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001900 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1901 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1902 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001903 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1904 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1905}
1906
1907// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1908// TURN server is used as the STUN server and we get 'local', 'stun', and
1909// 'relay' candidates.
1910// TODO(deadbeef): Remove this test when support for non-shared socket mode
1911// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001912TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001913 AddInterface(kClientAddr);
1914 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001915 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001916 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1917
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001918 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001919
Qingsi Wange53ac042018-05-08 11:55:07 -07001920 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001921 session_->StartGettingPorts();
1922
deadbeef7f1563f2017-09-15 17:40:01 -07001923 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1924 kDefaultAllocationTimeout, fake_clock);
1925 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001926 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001927 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001928 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001929 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1930 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1931 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001932 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001933 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1934 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1935 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001936 // Not using shared socket, so the STUN request's server reflexive address
1937 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001938 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001939
deadbeefc5d0d952015-07-16 10:22:21 -07001940 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1941 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1942 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1943}
1944
1945// Test that even when both a STUN and TURN server are configured, the TURN
1946// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001947TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001948 AddInterface(kClientAddr);
1949 // Configure with STUN server but destroy it, so we can ensure that it's
1950 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001951 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001952 stun_server_.reset();
1953 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1954
1955 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001956 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1957 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001958
Qingsi Wange53ac042018-05-08 11:55:07 -07001959 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001960 session_->StartGettingPorts();
1961
pthatcher1749bc32017-02-08 13:18:00 -08001962 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1963 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001964 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001965 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001966 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1967 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1968 &stun_candidate));
1969 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1970 candidates_, "relay", "udp",
1971 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1972 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001973
1974 // Don't bother waiting for STUN timeout, since we already verified
1975 // that we got a STUN candidate from the TURN server.
1976}
1977
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001978// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1979// and fail to generate STUN candidate, local UDP candidate is generated
1980// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001981TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001982 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1983 PORTALLOCATOR_DISABLE_TCP |
1984 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001985 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1986 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001987 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001988 session_->StartGettingPorts();
Mirko Bonadei056a68d2018-06-20 07:53:19 +00001989 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 13:18:00 -08001990 fake_clock);
Mirko Bonadei056a68d2018-06-20 07:53:19 +00001991 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001992 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001993 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001994 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1995 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001996 EXPECT_EQ(1U, candidates_.size());
1997}
1998
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001999// Test that when the NetworkManager doesn't have permission to enumerate
2000// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2001// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002002TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002003 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2004 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002005 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07002006 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002007 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2008 PORTALLOCATOR_DISABLE_TCP |
2009 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2010 EXPECT_EQ(0U,
2011 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 11:55:07 -07002012 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002013 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002014 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002015 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2016 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002017 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002018 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002019 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002020}
2021
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002022// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002023TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002024 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2025 PORTALLOCATOR_ENABLE_IPV6 |
2026 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002027 AddInterface(kClientIPv6Addr);
2028 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002029 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002030 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002031 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002032 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002033 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002034 EXPECT_EQ(4U, ports_.size());
2035 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002036 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2037 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2038 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2039 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002040}
honghaiz98db68f2015-09-29 07:58:17 -07002041
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002042TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002043 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002044 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002045 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002046 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002047 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002048 EXPECT_EQ(2U, ports_.size());
2049 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002050 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002051
2052 // After stopping getting ports, adding a new interface will not start
2053 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002054 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002055 candidates_.clear();
2056 ports_.clear();
2057 candidate_allocation_done_ = false;
2058 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002059 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002060 EXPECT_EQ(0U, candidates_.size());
2061 EXPECT_EQ(0U, ports_.size());
2062}
2063
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002064TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002065 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002066 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002067 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002068 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002069 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002070 EXPECT_EQ(2U, ports_.size());
2071 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002072 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002073
2074 // After clearing getting ports, adding a new interface will start getting
2075 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002076 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002077 candidates_.clear();
2078 ports_.clear();
2079 candidate_allocation_done_ = false;
2080 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002081 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002082 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002083 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2084 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002085}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002086
2087// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2088// a pooled session is taken out of the pool.
2089TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2090 AddInterface(kClientAddr);
2091 int pool_size = 1;
2092 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002093 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002094 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002095 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002096 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2097 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002098 // Expect that when TakePooledSession is called,
2099 // UpdateTransportInformationInternal will be called and the
2100 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2101 // candidates.
2102 session_ =
2103 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2104 ASSERT_NE(nullptr, session_.get());
2105 auto ready_ports = session_->ReadyPorts();
2106 auto candidates = session_->ReadyCandidates();
2107 EXPECT_FALSE(ready_ports.empty());
2108 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002109 for (const PortInterface* port_interface : ready_ports) {
2110 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002111 EXPECT_EQ(kContentName, port->content_name());
2112 EXPECT_EQ(1, port->component());
2113 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2114 EXPECT_EQ(kIcePwd0, port->password());
2115 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002116 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002117 EXPECT_EQ(1, candidate.component());
2118 EXPECT_EQ(kIceUfrag0, candidate.username());
2119 EXPECT_EQ(kIcePwd0, candidate.password());
2120 }
2121}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002122
2123// Test that a new candidate filter takes effect even on already-gathered
2124// candidates.
2125TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2126 AddInterface(kClientAddr);
2127 int pool_size = 1;
2128 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002129 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002130 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2131 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002132 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2133 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002134 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2135 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2136 allocator_->set_candidate_filter(CF_RELAY);
2137 // Assume that when TakePooledSession is called, the candidate filter will be
2138 // applied to the pooled session. This is tested by PortAllocatorTest.
2139 session_ =
2140 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2141 ASSERT_NE(nullptr, session_.get());
2142 auto candidates = session_->ReadyCandidates();
2143 auto ports = session_->ReadyPorts();
2144 // Sanity check that the number of candidates and ports decreased.
2145 EXPECT_GT(initial_candidates_size, candidates.size());
2146 EXPECT_GT(initial_ports_size, ports.size());
2147 for (const PortInterface* port : ports) {
2148 // Expect only relay ports.
2149 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2150 }
2151 for (const Candidate& candidate : candidates) {
2152 // Expect only relay candidates now that the filter is applied.
2153 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2154 // Expect that the raddr is emptied due to the CF_RELAY filter.
2155 EXPECT_EQ(candidate.related_address(),
2156 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2157 }
2158}
2159
Qingsi Wang4ff54432018-03-01 18:25:20 -08002160TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2161 const int pool_size = 1;
2162 const int expected_stun_keepalive_interval = 123;
2163 AddInterface(kClientAddr);
2164 allocator_->SetConfiguration(allocator_->stun_servers(),
2165 allocator_->turn_servers(), pool_size, false,
2166 nullptr, expected_stun_keepalive_interval);
2167 auto* pooled_session = allocator_->GetPooledSession();
2168 ASSERT_NE(nullptr, pooled_session);
2169 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2170 kDefaultAllocationTimeout, fake_clock);
2171 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2172 expected_stun_keepalive_interval);
2173}
2174
2175TEST_F(BasicPortAllocatorTest,
2176 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2177 const int pool_size = 1;
2178 AddInterface(kClientAddr);
2179 allocator_->SetConfiguration(allocator_->stun_servers(),
2180 allocator_->turn_servers(), pool_size, false,
2181 nullptr, 123 /* stun keepalive interval */);
2182 auto* pooled_session = allocator_->GetPooledSession();
2183 ASSERT_NE(nullptr, pooled_session);
2184 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2185 kDefaultAllocationTimeout, fake_clock);
2186 const int expected_stun_keepalive_interval = 321;
2187 allocator_->SetConfiguration(allocator_->stun_servers(),
2188 allocator_->turn_servers(), pool_size, false,
2189 nullptr, expected_stun_keepalive_interval);
2190 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2191 expected_stun_keepalive_interval);
2192}
2193
2194TEST_F(BasicPortAllocatorTest,
2195 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2196 const int pool_size = 1;
2197 const int expected_stun_keepalive_interval = 123;
2198 AddInterface(kClientAddr);
2199 allocator_->set_flags(allocator().flags() |
2200 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2201 allocator_->SetConfiguration(allocator_->stun_servers(),
2202 allocator_->turn_servers(), pool_size, false,
2203 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002204 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002205 session_->StartGettingPorts();
2206 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2207 kDefaultAllocationTimeout, fake_clock);
2208 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2209 expected_stun_keepalive_interval);
2210}
2211
2212TEST_F(BasicPortAllocatorTest,
2213 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2214 const int pool_size = 1;
2215 const int expected_stun_keepalive_interval = 123;
2216 AddInterface(kClientAddr);
2217 allocator_->set_flags(allocator().flags() &
2218 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2219 allocator_->SetConfiguration(allocator_->stun_servers(),
2220 allocator_->turn_servers(), pool_size, false,
2221 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002222 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002223 session_->StartGettingPorts();
2224 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2225 kDefaultAllocationTimeout, fake_clock);
2226 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2227 expected_stun_keepalive_interval);
2228}
2229
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002230} // namespace cricket