blob: b3d4f25b595055ea21c78543651c3cdff37dea40 [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.
1342TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343 BasicPortAllocatorTest,
Steve Anton6c38cc72017-11-29 10:25:58 -08001344 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001345 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001346 AddInterfaceAsDefaultRoute(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001347 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001348 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001349 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1350 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001351 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1352 rtc::IPAddress(), rtc::IPAddress());
1353}
1354
1355// Test that when adapter enumeration is disabled, with
1356// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1357// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001358TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001359 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1360 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001361 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001362 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001363 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1364 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1365 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001366}
1367
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001368// Test that we disable relay over UDP, and only TCP is used when connecting to
1369// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001370TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001371 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001372 AddInterface(kClientAddr);
1373 ResetWithStunServerAndNat(kStunAddr);
1374 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001375 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001376 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1377 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1378 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001379
1380 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001381 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1382 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001383
1384 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1385 // TURN/TCP candidates.
1386 EXPECT_EQ(2U, ports_.size());
1387 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001388 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001389 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1390 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001391 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001392 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001393 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001394}
1395
Erik Språngefdce692015-06-05 09:41:26 +02001396// Disable for asan, see
1397// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1398#if !defined(ADDRESS_SANITIZER)
1399
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001400// Test that we can get OnCandidatesAllocationDone callback when all the ports
1401// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001402TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001403 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001404 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001405 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1406 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001407 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001408 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001409 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410}
1411
1412// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001413TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 AddInterface(kClientAddr);
1415 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001416 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001417 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001418 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1419 kDefaultAllocationTimeout, fake_clock);
1420 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001422 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1423 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1424 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1425 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1426 EXPECT_TRUE(
1427 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001428}
1429
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001430#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001431
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432// Test that we don't crash or malfunction if we can't create UDP sockets or
1433// listen on TCP sockets. We still give out a local TCP address, since
1434// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001435TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001436 AddInterface(kClientAddr);
1437 fss_->set_udp_sockets_enabled(false);
1438 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001439 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001440 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001441 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1442 kDefaultAllocationTimeout, fake_clock);
1443 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001445 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1446 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1447 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1448 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1449 EXPECT_TRUE(
1450 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001451}
1452
1453// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001454// TODO(deadbeef): Find a way to exit early here.
1455TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001456 AddInterface(kClientAddr);
1457 fss_->set_tcp_sockets_enabled(false);
1458 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001459 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001460 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001461 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001462 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001463 // In case of Relay, ports creation will succeed but sockets will fail.
1464 // There is no error reporting from RelayEntry to handle this failure.
1465}
1466
1467// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001468TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001469 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1470 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001471 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001472 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001473 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1474 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001475 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001476 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1477 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001478 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001479 // will be tried after about 3 seconds.
1480 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001481 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001482 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1483 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1484 EXPECT_TRUE(
1485 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1486 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001487 // We wait at least for a full STUN timeout, which
1488 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1489 // already passed (see above), we wait 3 seconds less than that.
1490 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1491 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001492}
1493
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001494TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001495 AddInterface(kClientAddr);
1496 AddInterface(kClientAddr2);
1497 // Allocating only host UDP ports. This is done purely for testing
1498 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001499 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1500 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001501 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001502 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001503 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1504 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001505 ASSERT_EQ(2U, candidates_.size());
1506 EXPECT_EQ(2U, ports_.size());
1507 // Candidates priorities should be different.
1508 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1509}
1510
1511// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001512TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001513 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001514 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001515 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001516 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1517 kDefaultAllocationTimeout, fake_clock);
1518 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001519 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001520 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001521}
1522
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001523// Test that the allocator session uses the candidate filter it's created with,
1524// rather than the filter of its parent allocator.
1525// The filter of the allocator should only affect the next gathering phase,
1526// according to JSEP, which means the *next* allocator session returned.
1527TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1528 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001529 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001530 // Set candidate filter *after* creating the session. Should have no effect.
1531 allocator().set_candidate_filter(CF_RELAY);
1532 session_->StartGettingPorts();
1533 // 7 candidates and 4 ports is what we would normally get (see the
1534 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001535 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1536 kDefaultAllocationTimeout, fake_clock);
1537 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001538 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001539}
1540
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001541// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1542// This test also verifies that when the allocator is only allowed to use
1543// relay (i.e. IceTransportsType is relay), the raddr is an empty
1544// address with the correct family. This is to prevent any local
1545// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001546TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001547 AddInterface(kClientAddr);
1548 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001549 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001550 allocator().set_candidate_filter(CF_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001551 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001552 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001553 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1554 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001555 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1556 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001557
1558 EXPECT_EQ(1U, candidates_.size());
1559 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001560 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1561 EXPECT_EQ(
1562 candidates_[0].related_address(),
1563 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001564}
1565
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001566TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001567 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001568 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1569 allocator().set_candidate_filter(CF_HOST);
Qingsi Wange53ac042018-05-08 11:55:07 -07001570 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001571 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001572 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1573 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001574 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1575 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001576 for (const Candidate& candidate : candidates_) {
1577 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001578 }
1579}
1580
1581// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001582TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001583 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001584 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001585
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001586 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1587 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001588 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001589 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001590 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1591 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592 // Host is behind NAT, no private address will be exposed. Hence only UDP
1593 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001594 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1595 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001596 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1597 EXPECT_EQ(
1598 candidates_[0].related_address(),
1599 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001600}
1601
1602// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001603TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001604 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001605 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1606 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001607 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001608 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001609 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1610 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001611 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001612 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001613 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001614 for (const Candidate& candidate : candidates_) {
1615 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001616 }
1617}
1618
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001619// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001620TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001621 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001622 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001624 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1625 kDefaultAllocationTimeout, fake_clock);
1626 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001627 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1628 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1629 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001630 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001631 for (const Candidate& candidate : candidates_) {
1632 EXPECT_EQ(kIceUfrag0, candidate.username());
1633 EXPECT_EQ(kIcePwd0, candidate.password());
1634 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001635}
1636
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001637// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1638// is allocated for udp and stun. Also verify there is only one candidate
1639// (local) if stun candidate is same as local candidate, which will be the case
1640// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001641TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001642 AddInterface(kClientAddr);
1643 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001644 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001645 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001646 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001647 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1648 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001649 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001650 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001651 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1652 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001653}
1654
1655// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1656// is allocated for udp and stun. In this test we should expect both stun and
1657// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001658TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001659 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001660 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001661
1662 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001663 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001664 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001665 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001666 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1667 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001669 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1670 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1671 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001672 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1673 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001674 EXPECT_EQ(3U, candidates_.size());
1675}
1676
deadbeefc5d0d952015-07-16 10:22:21 -07001677// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001678TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001679 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001680 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001681 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001682 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001683
1684 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1685
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001686 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001687 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001688 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1689 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001690
Qingsi Wange53ac042018-05-08 11:55:07 -07001691 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001692 session_->StartGettingPorts();
1693
deadbeef7f1563f2017-09-15 17:40:01 -07001694 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1695 kDefaultAllocationTimeout, fake_clock);
1696 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001697 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001698 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1699 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1700 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1701 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1702 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001703}
1704
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001705// Test that if prune_turn_ports is set, TCP TURN port will not be used
1706// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1707TEST_F(BasicPortAllocatorTest,
1708 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1709 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1710 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1711 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001712
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001713 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001714}
1715
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001716// Test that if prune_turn_ports is set, TCP TURN port will not be used
1717// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1718TEST_F(BasicPortAllocatorTest,
1719 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1720 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1721 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1722 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001723
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001724 TestUdpTurnPortPrunesTcpTurnPort();
1725}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001726
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001727// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1728// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1729TEST_F(BasicPortAllocatorTest,
1730 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1731 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1732 // first.
1733 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1734 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001735
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001736 TestIPv6TurnPortPrunesIPv4TurnPort();
1737}
1738
1739// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1740// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1741TEST_F(BasicPortAllocatorTest,
1742 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1743 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1744 // first.
1745 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1746 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1747
1748 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001749}
1750
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001751// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001752// will has its own set of TurnPorts based on their priorities, in the default
1753// case where no transit delay is set.
1754TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1755 TestEachInterfaceHasItsOwnTurnPorts();
1756}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001757
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001758// Tests that if prune_turn_ports is set, each network interface
1759// will has its own set of TurnPorts based on their priorities, given that
1760// IPv4/TCP TURN port becomes ready first.
1761TEST_F(BasicPortAllocatorTest,
1762 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1763 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1764 // becomes ready last.
1765 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1766 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1767 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1768 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001769
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001770 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001771}
1772
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001773// Testing DNS resolve for the TURN server, this will test AllocationSequence
1774// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001775// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1776// appears that it doesn't currently because of the DNS look up not
1777// using the fake clock.
1778TEST_F(BasicPortAllocatorTestWithRealClock,
1779 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001780 // This test relies on a real query for "localhost", so it won't work on an
1781 // IPv6-only machine.
1782 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001783 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001784 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001785 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001786 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001787 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001788 RelayServerConfig turn_server(RELAY_TURN);
1789 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001790 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001791 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001792 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001793 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001794
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001795 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001796 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001797 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1798 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001799
Qingsi Wange53ac042018-05-08 11:55:07 -07001800 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001801 session_->StartGettingPorts();
1802
1803 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1804}
1805
1806// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1807// is allocated for udp/stun/turn. In this test we should expect all local,
1808// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001809TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001810 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001811 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001812
1813 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1814
1815 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001816 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1817 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001818
Qingsi Wange53ac042018-05-08 11:55:07 -07001819 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001820 session_->StartGettingPorts();
1821
deadbeef7f1563f2017-09-15 17:40:01 -07001822 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1823 kDefaultAllocationTimeout, fake_clock);
1824 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001825 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001826 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1827 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1828 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1829 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1830 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001831 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1832 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001833 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001834 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1835 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001836 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1837 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1838}
1839
1840// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1841// server is also used as the STUN server, we should get 'local', 'stun', and
1842// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001843TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001844 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001845 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001846 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001847 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1848
1849 // Must set the step delay to 0 to make sure the relay allocation phase is
1850 // started before the STUN candidates are obtained, so that the STUN binding
1851 // response is processed when both StunPort and TurnPort exist to reproduce
1852 // webrtc issue 3537.
1853 allocator_->set_step_delay(0);
1854 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001855 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1856 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001857
Qingsi Wange53ac042018-05-08 11:55:07 -07001858 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001859 session_->StartGettingPorts();
1860
deadbeef7f1563f2017-09-15 17:40:01 -07001861 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1862 kDefaultAllocationTimeout, fake_clock);
1863 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001864 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001865 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001866 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1867 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1868 &stun_candidate));
1869 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1870 candidates_, "relay", "udp",
1871 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1872 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001873
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001874 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001875 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1876 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001877 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1878 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1879}
1880
deadbeefc5d0d952015-07-16 10:22:21 -07001881// Test that when only a TCP TURN server is available, we do NOT use it as
1882// a UDP STUN server, as this could leak our IP address. Thus we should only
1883// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001884TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001885 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001886 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001887 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001888 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1889
1890 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001891 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1892 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001893
Qingsi Wange53ac042018-05-08 11:55:07 -07001894 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001895 session_->StartGettingPorts();
1896
deadbeef7f1563f2017-09-15 17:40:01 -07001897 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1898 kDefaultAllocationTimeout, fake_clock);
1899 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001900 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001901 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1902 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1903 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001904 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1905 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1906}
1907
1908// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1909// TURN server is used as the STUN server and we get 'local', 'stun', and
1910// 'relay' candidates.
1911// TODO(deadbeef): Remove this test when support for non-shared socket mode
1912// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001913TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001914 AddInterface(kClientAddr);
1915 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001916 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001917 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1918
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001919 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001920
Qingsi Wange53ac042018-05-08 11:55:07 -07001921 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001922 session_->StartGettingPorts();
1923
deadbeef7f1563f2017-09-15 17:40:01 -07001924 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1925 kDefaultAllocationTimeout, fake_clock);
1926 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001927 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001928 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001929 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001930 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1931 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1932 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001933 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001934 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1935 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1936 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001937 // Not using shared socket, so the STUN request's server reflexive address
1938 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001939 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001940
deadbeefc5d0d952015-07-16 10:22:21 -07001941 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1942 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1943 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1944}
1945
1946// Test that even when both a STUN and TURN server are configured, the TURN
1947// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001948TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001949 AddInterface(kClientAddr);
1950 // Configure with STUN server but destroy it, so we can ensure that it's
1951 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001952 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001953 stun_server_.reset();
1954 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1955
1956 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001957 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1958 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001959
Qingsi Wange53ac042018-05-08 11:55:07 -07001960 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001961 session_->StartGettingPorts();
1962
pthatcher1749bc32017-02-08 13:18:00 -08001963 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1964 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001965 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001966 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001967 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1968 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1969 &stun_candidate));
1970 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1971 candidates_, "relay", "udp",
1972 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1973 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001974
1975 // Don't bother waiting for STUN timeout, since we already verified
1976 // that we got a STUN candidate from the TURN server.
1977}
1978
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001979// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1980// and fail to generate STUN candidate, local UDP candidate is generated
1981// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001982TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001983 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1984 PORTALLOCATOR_DISABLE_TCP |
1985 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001986 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1987 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001988 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001989 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001990 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1991 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001992 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001993 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001994 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001995 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1996 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001997 EXPECT_EQ(1U, candidates_.size());
1998}
1999
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002000// Test that when the NetworkManager doesn't have permission to enumerate
2001// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2002// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002003TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002004 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2005 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002006 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07002007 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002008 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2009 PORTALLOCATOR_DISABLE_TCP |
2010 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2011 EXPECT_EQ(0U,
2012 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 11:55:07 -07002013 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002014 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002015 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002016 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2017 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002018 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002019 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002020 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002021}
2022
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002023// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002024TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002025 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2026 PORTALLOCATOR_ENABLE_IPV6 |
2027 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002028 AddInterface(kClientIPv6Addr);
2029 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002030 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002031 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002032 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002033 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002034 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002035 EXPECT_EQ(4U, ports_.size());
2036 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002037 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2038 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2039 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2040 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002041}
honghaiz98db68f2015-09-29 07:58:17 -07002042
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002043TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002044 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002045 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002046 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002047 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002048 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002049 EXPECT_EQ(2U, ports_.size());
2050 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002051 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002052
2053 // After stopping getting ports, adding a new interface will not start
2054 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002055 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002056 candidates_.clear();
2057 ports_.clear();
2058 candidate_allocation_done_ = false;
2059 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002060 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002061 EXPECT_EQ(0U, candidates_.size());
2062 EXPECT_EQ(0U, ports_.size());
2063}
2064
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002065TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002066 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002067 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002068 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002069 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002070 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002071 EXPECT_EQ(2U, ports_.size());
2072 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002073 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002074
2075 // After clearing getting ports, adding a new interface will start getting
2076 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002077 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002078 candidates_.clear();
2079 ports_.clear();
2080 candidate_allocation_done_ = false;
2081 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002082 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002083 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002084 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2085 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002086}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002087
2088// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2089// a pooled session is taken out of the pool.
2090TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2091 AddInterface(kClientAddr);
2092 int pool_size = 1;
2093 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002094 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002095 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002096 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002097 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2098 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002099 // Expect that when TakePooledSession is called,
2100 // UpdateTransportInformationInternal will be called and the
2101 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2102 // candidates.
2103 session_ =
2104 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2105 ASSERT_NE(nullptr, session_.get());
2106 auto ready_ports = session_->ReadyPorts();
2107 auto candidates = session_->ReadyCandidates();
2108 EXPECT_FALSE(ready_ports.empty());
2109 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002110 for (const PortInterface* port_interface : ready_ports) {
2111 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002112 EXPECT_EQ(kContentName, port->content_name());
2113 EXPECT_EQ(1, port->component());
2114 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2115 EXPECT_EQ(kIcePwd0, port->password());
2116 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002117 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002118 EXPECT_EQ(1, candidate.component());
2119 EXPECT_EQ(kIceUfrag0, candidate.username());
2120 EXPECT_EQ(kIcePwd0, candidate.password());
2121 }
2122}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002123
2124// Test that a new candidate filter takes effect even on already-gathered
2125// candidates.
2126TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2127 AddInterface(kClientAddr);
2128 int pool_size = 1;
2129 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002130 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002131 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2132 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002133 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2134 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002135 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2136 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2137 allocator_->set_candidate_filter(CF_RELAY);
2138 // Assume that when TakePooledSession is called, the candidate filter will be
2139 // applied to the pooled session. This is tested by PortAllocatorTest.
2140 session_ =
2141 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2142 ASSERT_NE(nullptr, session_.get());
2143 auto candidates = session_->ReadyCandidates();
2144 auto ports = session_->ReadyPorts();
2145 // Sanity check that the number of candidates and ports decreased.
2146 EXPECT_GT(initial_candidates_size, candidates.size());
2147 EXPECT_GT(initial_ports_size, ports.size());
2148 for (const PortInterface* port : ports) {
2149 // Expect only relay ports.
2150 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2151 }
2152 for (const Candidate& candidate : candidates) {
2153 // Expect only relay candidates now that the filter is applied.
2154 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2155 // Expect that the raddr is emptied due to the CF_RELAY filter.
2156 EXPECT_EQ(candidate.related_address(),
2157 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2158 }
2159}
2160
Qingsi Wang4ff54432018-03-01 18:25:20 -08002161TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2162 const int pool_size = 1;
2163 const int expected_stun_keepalive_interval = 123;
2164 AddInterface(kClientAddr);
2165 allocator_->SetConfiguration(allocator_->stun_servers(),
2166 allocator_->turn_servers(), pool_size, false,
2167 nullptr, expected_stun_keepalive_interval);
2168 auto* pooled_session = allocator_->GetPooledSession();
2169 ASSERT_NE(nullptr, pooled_session);
2170 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2171 kDefaultAllocationTimeout, fake_clock);
2172 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2173 expected_stun_keepalive_interval);
2174}
2175
2176TEST_F(BasicPortAllocatorTest,
2177 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2178 const int pool_size = 1;
2179 AddInterface(kClientAddr);
2180 allocator_->SetConfiguration(allocator_->stun_servers(),
2181 allocator_->turn_servers(), pool_size, false,
2182 nullptr, 123 /* stun keepalive interval */);
2183 auto* pooled_session = allocator_->GetPooledSession();
2184 ASSERT_NE(nullptr, pooled_session);
2185 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2186 kDefaultAllocationTimeout, fake_clock);
2187 const int expected_stun_keepalive_interval = 321;
2188 allocator_->SetConfiguration(allocator_->stun_servers(),
2189 allocator_->turn_servers(), pool_size, false,
2190 nullptr, expected_stun_keepalive_interval);
2191 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2192 expected_stun_keepalive_interval);
2193}
2194
2195TEST_F(BasicPortAllocatorTest,
2196 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2197 const int pool_size = 1;
2198 const int expected_stun_keepalive_interval = 123;
2199 AddInterface(kClientAddr);
2200 allocator_->set_flags(allocator().flags() |
2201 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2202 allocator_->SetConfiguration(allocator_->stun_servers(),
2203 allocator_->turn_servers(), pool_size, false,
2204 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002205 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002206 session_->StartGettingPorts();
2207 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2208 kDefaultAllocationTimeout, fake_clock);
2209 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2210 expected_stun_keepalive_interval);
2211}
2212
2213TEST_F(BasicPortAllocatorTest,
2214 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2215 const int pool_size = 1;
2216 const int expected_stun_keepalive_interval = 123;
2217 AddInterface(kClientAddr);
2218 allocator_->set_flags(allocator().flags() &
2219 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2220 allocator_->SetConfiguration(allocator_->stun_servers(),
2221 allocator_->turn_servers(), pool_size, false,
2222 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002223 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002224 session_->StartGettingPorts();
2225 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2226 kDefaultAllocationTimeout, fake_clock);
2227 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2228 expected_stun_keepalive_interval);
2229}
2230
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002231} // namespace cricket