blob: b3e52ffec514886536e93c694b6d8a097c938fc3 [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"
Qingsi Wang7fc821d2018-07-12 12:54:53 -070037#include "system_wrappers/include/metrics_default.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080039using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040using rtc::SocketAddress;
41using rtc::Thread;
42
Mirko Bonadei675513b2017-11-09 11:09:25 +010043#define MAYBE_SKIP_IPV4 \
44 if (!rtc::HasIPv4Enabled()) { \
45 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
46 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070047 }
48
deadbeefe97389c2016-12-23 01:43:45 -080049static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070051static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070052static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000054static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070055static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
56 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070057static const SocketAddress kClientIPv6Addr2(
58 "2401:fa00:4:2000:be30:5bff:fee5:c3",
59 0);
deadbeef3427f532017-07-26 16:09:33 -070060static const SocketAddress kClientIPv6Addr3(
61 "2401:fa00:4:3000:be30:5bff:fee5:c3",
62 0);
deadbeefc5d0d952015-07-16 10:22:21 -070063static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
64static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
66static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
67static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
68static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
69static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
70static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
71static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
72static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
73static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070074static const SocketAddress kTurnUdpIntIPv6Addr(
75 "2402:fb00:4:1000:be30:5bff:fee5:c3",
76 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070078static const SocketAddress kTurnTcpIntIPv6Addr(
79 "2402:fb00:4:2000:be30:5bff:fee5:c3",
80 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
82
83// Minimum and maximum port for port range tests.
84static const int kMinPort = 10000;
85static const int kMaxPort = 10099;
86
87// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070088static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089// Based on ICE_PWD_LENGTH
90static const char kIcePwd0[] = "TESTICEPWD00000000000000";
91
92static const char kContentName[] = "test content";
93
Honghai Zhangc67e0f52016-09-19 16:57:37 -070094static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095static const char kTurnUsername[] = "test";
96static const char kTurnPassword[] = "test";
97
pthatcher94a2f212017-02-08 14:42:22 -080098// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070099// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -0800100static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -0700101
Qingsi Wang4ff54432018-03-01 18:25:20 -0800102namespace {
103
104void CheckStunKeepaliveIntervalOfAllReadyPorts(
105 const cricket::PortAllocatorSession* allocator_session,
106 int expected) {
107 auto ready_ports = allocator_session->ReadyPorts();
108 for (const auto* port : ready_ports) {
109 if (port->Type() == cricket::STUN_PORT_TYPE ||
110 (port->Type() == cricket::LOCAL_PORT_TYPE &&
111 port->GetProtocol() == cricket::PROTO_UDP)) {
112 EXPECT_EQ(
113 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
114 expected);
115 }
116 }
117}
118
119} // namespace
120
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121namespace cricket {
122
123// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700124std::ostream& operator<<(std::ostream& os,
125 const std::vector<Candidate>& candidates) {
126 os << '[';
127 bool first = true;
128 for (const Candidate& c : candidates) {
129 if (!first) {
130 os << ", ";
131 }
132 os << c.ToString();
133 first = false;
Steve Anton6c38cc72017-11-29 10:25:58 -0800134 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700135 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136 return os;
137}
138
pthatcher1749bc32017-02-08 13:18:00 -0800139class BasicPortAllocatorTestBase : public testing::Test,
140 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141 public:
pthatcher1749bc32017-02-08 13:18:00 -0800142 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700143 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700145 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800146 // Note that the NAT is not used by default. ResetWithStunServerAndNat
147 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700148 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700149 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700150 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700151 relay_server_(Thread::Current(),
152 kRelayUdpIntAddr,
153 kRelayUdpExtAddr,
154 kRelayTcpIntAddr,
155 kRelayTcpExtAddr,
156 kRelaySslTcpIntAddr,
157 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
159 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700160 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 stun_servers.insert(kStunAddr);
162 // Passing the addresses of GTURN servers will enable GTURN in
163 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800164 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
165 // configuration should be blank by default (preferred), or it should use
166 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700167 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
168 kRelayUdpIntAddr, kRelayTcpIntAddr,
169 kRelaySslTcpIntAddr));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700170 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700171 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wang7fc821d2018-07-12 12:54:53 -0700172 webrtc::metrics::Reset();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 }
174
175 void AddInterface(const SocketAddress& addr) {
176 network_manager_.AddInterface(addr);
177 }
honghaiz8c404fa2015-09-28 07:59:43 -0700178 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
179 network_manager_.AddInterface(addr, if_name);
180 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800181 void AddInterface(const SocketAddress& addr,
182 const std::string& if_name,
183 rtc::AdapterType type) {
184 network_manager_.AddInterface(addr, if_name, type);
185 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800186 // The default route is the public address that STUN server will observe when
187 // the endpoint is sitting on the public internet and the local port is bound
188 // to the "any" address. This may be different from the default local address
189 // which the endpoint observes. This can occur if the route to the public
190 // endpoint like 8.8.8.8 (specified as the default local address) is
191 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700192 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
193 AddInterface(addr);
194 // When a binding comes from the any address, the |addr| will be used as the
195 // srflx address.
196 vss_->SetDefaultRoute(addr.ipaddr());
197 }
honghaiz8c404fa2015-09-28 07:59:43 -0700198 void RemoveInterface(const SocketAddress& addr) {
199 network_manager_.RemoveInterface(addr);
200 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 bool SetPortRange(int min_port, int max_port) {
202 return allocator_->SetPortRange(min_port, max_port);
203 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700204 // Endpoint is on the public network. No STUN or TURN.
205 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700206 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700207 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700208 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700209 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700210 // Endpoint is behind a NAT, with STUN specified.
211 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
212 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700213 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700214 // Endpoint is on the public network, with STUN specified.
215 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
216 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700218 // Endpoint is on the public network, with TURN specified.
219 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
220 const rtc::SocketAddress& tcp_turn) {
221 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 AddTurnServers(udp_turn, tcp_turn);
223 }
224
225 void AddTurnServers(const rtc::SocketAddress& udp_turn,
226 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700227 RelayServerConfig turn_server(RELAY_TURN);
228 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800229 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000230
231 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800232 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 }
234 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800235 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 }
deadbeef653b8e02015-11-11 12:55:10 -0800237 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000238 }
239
240 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700241 session_ = CreateSession("session", component);
242 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 return true;
246 }
247
248 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700249 session_ = CreateSession("session", content_name, component);
250 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700252 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253 return true;
254 }
255
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700256 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
257 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000258 return CreateSession(sid, kContentName, component);
259 }
260
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700261 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700262 const std::string& sid,
263 const std::string& content_name,
264 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000265 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
266 }
267
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700268 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700269 const std::string& sid,
270 const std::string& content_name,
271 int component,
272 const std::string& ice_ufrag,
273 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700274 std::unique_ptr<PortAllocatorSession> session =
275 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800277 &BasicPortAllocatorTestBase::OnPortReady);
278 session->SignalPortsPruned.connect(
279 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700280 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800281 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700282 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800283 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700284 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800285 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000286 return session;
287 }
288
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700289 // Return true if the addresses are the same, or the port is 0 in |pattern|
290 // (acting as a wildcard) and the IPs are the same.
291 // Even with a wildcard port, the port of the address should be nonzero if
292 // the IP is nonzero.
293 static bool AddressMatch(const SocketAddress& address,
294 const SocketAddress& pattern) {
295 return address.ipaddr() == pattern.ipaddr() &&
296 ((pattern.port() == 0 &&
297 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
298 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000299 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700300
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700301 // Returns the number of ports that have matching type, protocol and
302 // address.
303 static int CountPorts(const std::vector<PortInterface*>& ports,
304 const std::string& type,
305 ProtocolType protocol,
306 const SocketAddress& client_addr) {
307 return std::count_if(
308 ports.begin(), ports.end(),
309 [type, protocol, client_addr](PortInterface* port) {
310 return port->Type() == type && port->GetProtocol() == protocol &&
311 port->Network()->GetBestIP() == client_addr.ipaddr();
312 });
313 }
314
deadbeefe97389c2016-12-23 01:43:45 -0800315 static int CountCandidates(const std::vector<Candidate>& candidates,
316 const std::string& type,
317 const std::string& proto,
318 const SocketAddress& addr) {
319 return std::count_if(candidates.begin(), candidates.end(),
320 [type, proto, addr](const Candidate& c) {
321 return c.type() == type && c.protocol() == proto &&
322 AddressMatch(c.address(), addr);
323 });
324 }
325
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700326 // Find a candidate and return it.
327 static bool FindCandidate(const std::vector<Candidate>& candidates,
328 const std::string& type,
329 const std::string& proto,
330 const SocketAddress& addr,
331 Candidate* found) {
332 auto it = std::find_if(candidates.begin(), candidates.end(),
333 [type, proto, addr](const Candidate& c) {
334 return c.type() == type && c.protocol() == proto &&
335 AddressMatch(c.address(), addr);
336 });
337 if (it != candidates.end() && found) {
338 *found = *it;
339 }
340 return it != candidates.end();
341 }
342
343 // Convenience method to call FindCandidate with no return.
344 static bool HasCandidate(const std::vector<Candidate>& candidates,
345 const std::string& type,
346 const std::string& proto,
347 const SocketAddress& addr) {
348 return FindCandidate(candidates, type, proto, addr, nullptr);
349 }
350
351 // Version of HasCandidate that also takes a related address.
352 static bool HasCandidateWithRelatedAddr(
353 const std::vector<Candidate>& candidates,
354 const std::string& type,
355 const std::string& proto,
356 const SocketAddress& addr,
357 const SocketAddress& related_addr) {
358 auto it =
359 std::find_if(candidates.begin(), candidates.end(),
360 [type, proto, addr, related_addr](const Candidate& c) {
361 return c.type() == type && c.protocol() == proto &&
362 AddressMatch(c.address(), addr) &&
363 AddressMatch(c.related_address(), related_addr);
364 });
365 return it != candidates.end();
366 }
367
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000368 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700369 int min_port,
370 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 return (addr.port() >= min_port && addr.port() <= max_port);
372 }
373
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700374 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375 // We should only get this callback once, except in the mux test where
376 // we have multiple port allocation sessions.
377 if (session == session_.get()) {
378 ASSERT_FALSE(candidate_allocation_done_);
379 candidate_allocation_done_ = true;
380 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700381 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000382 }
383
384 // Check if all ports allocated have send-buffer size |expected|. If
385 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
386 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700387 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 for (it = ports_.begin(); it < ports_.end(); ++it) {
389 int send_buffer_size;
390 if (expected == -1) {
391 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700392 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000393 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700394 EXPECT_EQ(0,
395 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000396 ASSERT_EQ(expected, send_buffer_size);
397 }
398 }
399 }
400
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700401 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
402
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700404 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000405
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700406 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100407 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700409 // Make sure the new port is added to ReadyPorts.
410 auto ready_ports = ses->ReadyPorts();
411 EXPECT_NE(ready_ports.end(),
412 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700414 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700415 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100416 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700417 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700418 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700419 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700420 new_end = std::remove(ports_.begin(), new_end, port);
421 // Make sure the pruned port is not in ReadyPorts.
422 EXPECT_EQ(ready_ports.end(),
423 std::find(ready_ports.begin(), ready_ports.end(), port));
424 }
425 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700426 }
427
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700428 void OnCandidatesReady(PortAllocatorSession* ses,
429 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700430 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100431 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700432 // Sanity check that the ICE component is set.
433 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
434 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000435 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700436 // Make sure the new candidates are added to Candidates.
437 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700438 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700439 EXPECT_NE(
440 ses_candidates.end(),
441 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
442 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000443 }
444
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700445 void OnCandidatesRemoved(PortAllocatorSession* session,
446 const std::vector<Candidate>& removed_candidates) {
447 auto new_end = std::remove_if(
448 candidates_.begin(), candidates_.end(),
449 [removed_candidates](Candidate& candidate) {
450 for (const Candidate& removed_candidate : removed_candidates) {
451 if (candidate.MatchesForRemoval(removed_candidate)) {
452 return true;
453 }
454 }
455 return false;
456 });
457 candidates_.erase(new_end, candidates_.end());
458 }
459
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700460 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800461 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700462 RelayServerConfig server_config = allocator_->turn_servers()[i];
463 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000464 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700465 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000466 if (proto_addr.address == relay_port->address &&
467 proto_addr.proto == relay_port->proto)
468 return true;
469 }
470 }
471 return false;
472 }
473
Guo-wei Shieh11477022015-08-15 09:28:41 -0700474 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
475 bool with_nat) {
476 if (with_nat) {
477 nat_server_.reset(new rtc::NATServer(
478 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
479 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
480 } else {
481 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
482 }
483
484 ServerAddresses stun_servers;
485 if (!stun_server.IsNil()) {
486 stun_servers.insert(stun_server);
487 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700488 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700489 &network_manager_, nat_socket_factory_.get(), stun_servers));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700490 allocator_->Initialize();
491 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700492 }
493
pthatcher1749bc32017-02-08 13:18:00 -0800494 std::unique_ptr<rtc::VirtualSocketServer> vss_;
495 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700496 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800497 std::unique_ptr<rtc::NATServer> nat_server_;
498 rtc::NATSocketFactory nat_factory_;
499 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
500 std::unique_ptr<TestStunServer> stun_server_;
501 TestRelayServer relay_server_;
502 TestTurnServer turn_server_;
503 rtc::FakeNetworkManager network_manager_;
504 std::unique_ptr<BasicPortAllocator> allocator_;
505 std::unique_ptr<PortAllocatorSession> session_;
506 std::vector<PortInterface*> ports_;
507 std::vector<Candidate> candidates_;
508 bool candidate_allocation_done_;
509};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700510
pthatcher1749bc32017-02-08 13:18:00 -0800511class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
512};
513
514class FakeClockBase {
515 public:
516 rtc::ScopedFakeClock fake_clock;
517};
518
519class BasicPortAllocatorTest : public FakeClockBase,
520 public BasicPortAllocatorTestBase {
521 public:
522 // This function starts the port/address gathering and check the existence of
523 // candidates as specified. When |expect_stun_candidate| is true,
524 // |stun_candidate_addr| carries the expected reflective address, which is
525 // also the related address for TURN candidate if it is expected. Otherwise,
526 // it should be ignore.
527 void CheckDisableAdapterEnumeration(
528 uint32_t total_ports,
529 const rtc::IPAddress& host_candidate_addr,
530 const rtc::IPAddress& stun_candidate_addr,
531 const rtc::IPAddress& relay_candidate_udp_transport_addr,
532 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
533 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
534 rtc::IPAddress());
535 if (!session_) {
Qingsi Wange53ac042018-05-08 11:55:07 -0700536 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 13:18:00 -0800537 }
538 session_->set_flags(session_->flags() |
539 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
540 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
541 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700542 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800543 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
544 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700545
pthatcher1749bc32017-02-08 13:18:00 -0800546 uint32_t total_candidates = 0;
547 if (!host_candidate_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200548 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
549 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800550 ++total_candidates;
551 }
552 if (!stun_candidate_addr.IsNil()) {
553 rtc::SocketAddress related_address(host_candidate_addr, 0);
554 if (host_candidate_addr.IsNil()) {
555 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
556 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200557 EXPECT_TRUE(HasCandidateWithRelatedAddr(
558 candidates_, "stun", "udp",
559 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
pthatcher1749bc32017-02-08 13:18:00 -0800560 ++total_candidates;
561 }
562 if (!relay_candidate_udp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200563 EXPECT_TRUE(HasCandidateWithRelatedAddr(
564 candidates_, "relay", "udp",
565 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
566 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800567 ++total_candidates;
568 }
569 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200570 EXPECT_TRUE(HasCandidateWithRelatedAddr(
571 candidates_, "relay", "udp",
572 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
573 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800574 ++total_candidates;
575 }
576
577 EXPECT_EQ(total_candidates, candidates_.size());
578 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700579 }
580
581 void TestIPv6TurnPortPrunesIPv4TurnPort() {
582 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
583 // Add two IP addresses on the same interface.
584 AddInterface(kClientAddr, "net1");
585 AddInterface(kClientIPv6Addr, "net1");
586 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700587 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700588 allocator_->SetConfiguration(allocator_->stun_servers(),
589 allocator_->turn_servers(), 0, true);
590 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
591 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
592
593 allocator_->set_step_delay(kMinimumStepDelay);
594 allocator_->set_flags(
595 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
596 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
597
Qingsi Wange53ac042018-05-08 11:55:07 -0700598 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700599 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800600 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
601 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700602 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
603 EXPECT_EQ(3U, session_->ReadyPorts().size());
604 EXPECT_EQ(3U, ports_.size());
605 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
606 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
607 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
608 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
609
610 // Now that we remove candidates when a TURN port is pruned, there will be
611 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
612 EXPECT_EQ(3U, candidates_.size());
613 const std::vector<Candidate>& ready_candidates =
614 session_->ReadyCandidates();
615 EXPECT_EQ(3U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200616 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
617 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
618 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700619 }
620
pthatcher1749bc32017-02-08 13:18:00 -0800621 void TestUdpTurnPortPrunesTcpTurnPort() {
622 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
623 AddInterface(kClientAddr);
624 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700625 allocator_->Initialize();
pthatcher1749bc32017-02-08 13:18:00 -0800626 allocator_->SetConfiguration(allocator_->stun_servers(),
627 allocator_->turn_servers(), 0, true);
628 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
629 allocator_->set_step_delay(kMinimumStepDelay);
630 allocator_->set_flags(allocator().flags() |
631 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
632 PORTALLOCATOR_DISABLE_TCP);
633
Qingsi Wange53ac042018-05-08 11:55:07 -0700634 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 13:18:00 -0800635 session_->StartGettingPorts();
636 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
637 kDefaultAllocationTimeout, fake_clock);
638 // Only 2 ports (one STUN and one TURN) are actually being used.
639 EXPECT_EQ(2U, session_->ReadyPorts().size());
640 // We have verified that each port, when it is added to |ports_|, it is
641 // found in |ready_ports|, and when it is pruned, it is not found in
642 // |ready_ports|, so we only need to verify the content in one of them.
643 EXPECT_EQ(2U, ports_.size());
644 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
645 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
646 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
647
648 // Now that we remove candidates when a TURN port is pruned, |candidates_|
649 // should only contains two candidates regardless whether the TCP TURN port
650 // is created before or after the UDP turn port.
651 EXPECT_EQ(2U, candidates_.size());
652 // There will only be 2 candidates in |ready_candidates| because it only
653 // includes the candidates in the ready ports.
654 const std::vector<Candidate>& ready_candidates =
655 session_->ReadyCandidates();
656 EXPECT_EQ(2U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200657 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
658 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
659 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800660 }
661
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700662 void TestEachInterfaceHasItsOwnTurnPorts() {
663 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
664 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
665 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
666 // Add two interfaces both having IPv4 and IPv6 addresses.
667 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
668 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
669 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
670 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
671 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700672 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700673 allocator_->SetConfiguration(allocator_->stun_servers(),
674 allocator_->turn_servers(), 0, true);
675 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
676 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
677 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
678
679 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800680 allocator_->set_flags(
681 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
682 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Qingsi Wange53ac042018-05-08 11:55:07 -0700683 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700684 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800685 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
686 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700687 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
688 // use.
689 EXPECT_EQ(10U, session_->ReadyPorts().size());
690 EXPECT_EQ(10U, ports_.size());
691 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
692 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
693 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
694 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
695 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
696 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
697 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
698 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
699 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
700 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
701
702 // Now that we remove candidates when TURN ports are pruned, there will be
703 // exactly 10 candidates in |candidates_|.
704 EXPECT_EQ(10U, candidates_.size());
705 const std::vector<Candidate>& ready_candidates =
706 session_->ReadyCandidates();
707 EXPECT_EQ(10U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200708 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
709 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
710 EXPECT_TRUE(
711 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
712 EXPECT_TRUE(
713 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
714 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
715 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
716 EXPECT_TRUE(
717 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
718 EXPECT_TRUE(
719 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
720 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
721 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700722 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000723};
724
725// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700726TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000727 EXPECT_EQ(&network_manager_, allocator().network_manager());
728 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800729 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700730 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000731 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800732 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
733 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700734 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
735 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
736 EXPECT_TRUE(
737 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
Qingsi Wange53ac042018-05-08 11:55:07 -0700738 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000740}
741
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800742// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700743TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800744 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
745 rtc::ADAPTER_TYPE_ETHERNET);
746 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
747 rtc::ADAPTER_TYPE_WIFI);
748 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
749 rtc::ADAPTER_TYPE_CELLULAR);
750 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
751 rtc::ADAPTER_TYPE_VPN);
752 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
753 rtc::ADAPTER_TYPE_LOOPBACK);
Qingsi Wange53ac042018-05-08 11:55:07 -0700754 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700755 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
756 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800757 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800758 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
759 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700761 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800762 EXPECT_LT(candidate.address().ip(), 0x12345604U);
763 }
764}
765
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700766TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800767 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
768 rtc::ADAPTER_TYPE_ETHERNET);
769 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
770 rtc::ADAPTER_TYPE_WIFI);
771 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
772 rtc::ADAPTER_TYPE_CELLULAR);
773 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
774 rtc::ADAPTER_TYPE_LOOPBACK |
775 rtc::ADAPTER_TYPE_WIFI);
Qingsi Wange53ac042018-05-08 11:55:07 -0700776 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700777 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
778 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800780 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
781 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800782 EXPECT_EQ(1U, candidates_.size());
783 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
784}
785
deadbeef1c46a352017-09-27 11:24:05 -0700786// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
787// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
788TEST_F(BasicPortAllocatorTest,
789 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
790 SocketAddress wifi(IPAddress(0x12345600U), 0);
791 SocketAddress cell(IPAddress(0x12345601U), 0);
792 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
793 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
794 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-05-31 18:29:12 -0700795 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
796 cricket::PORTALLOCATOR_DISABLE_RELAY |
797 cricket::PORTALLOCATOR_DISABLE_TCP |
798 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700799 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700800 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800801 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
802 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700803 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700804 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200805 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700806}
honghaiz60347052016-05-31 18:29:12 -0700807
deadbeef1c46a352017-09-27 11:24:05 -0700808// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
809// both "unknown" and cell interfaces are available, only the unknown are used.
810// The unknown interface may be something that ultimately uses Wi-Fi, so we do
811// this to be on the safe side.
812TEST_F(BasicPortAllocatorTest,
813 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
814 SocketAddress cell(IPAddress(0x12345601U), 0);
815 SocketAddress unknown1(IPAddress(0x12345602U), 0);
816 SocketAddress unknown2(IPAddress(0x12345603U), 0);
817 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
818 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
819 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
820 // Disable all but UDP candidates to make the test simpler.
821 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
822 cricket::PORTALLOCATOR_DISABLE_RELAY |
823 cricket::PORTALLOCATOR_DISABLE_TCP |
824 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700825 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700826 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800827 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
828 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700829 // Should only get two candidates, none of which is cell.
honghaiz60347052016-05-31 18:29:12 -0700830 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200831 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
832 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
deadbeef1c46a352017-09-27 11:24:05 -0700833}
honghaiz60347052016-05-31 18:29:12 -0700834
deadbeef1c46a352017-09-27 11:24:05 -0700835// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
836// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
837// interface is used.
838TEST_F(BasicPortAllocatorTest,
839 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
840 SocketAddress wifi(IPAddress(0x12345600U), 0);
841 SocketAddress cellular(IPAddress(0x12345601U), 0);
842 SocketAddress unknown1(IPAddress(0x12345602U), 0);
843 SocketAddress unknown2(IPAddress(0x12345603U), 0);
844 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
845 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
846 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
847 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
848 // Disable all but UDP candidates to make the test simpler.
849 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
850 cricket::PORTALLOCATOR_DISABLE_RELAY |
851 cricket::PORTALLOCATOR_DISABLE_TCP |
852 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700853 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700854 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800855 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
856 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700857 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700858 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200859 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700860}
861
862// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
863// only interface available is cellular, it ends up used anyway. A costly
864// connection is always better than no connection.
865TEST_F(BasicPortAllocatorTest,
866 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
867 SocketAddress cellular(IPAddress(0x12345601U), 0);
868 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
869 // Disable all but UDP candidates to make the test simpler.
870 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
871 cricket::PORTALLOCATOR_DISABLE_RELAY |
872 cricket::PORTALLOCATOR_DISABLE_TCP |
873 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700874 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 11:24:05 -0700875 session_->StartGettingPorts();
876 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
877 kDefaultAllocationTimeout, fake_clock);
878 // Make sure we got the cell candidate.
879 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200880 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
honghaiz60347052016-05-31 18:29:12 -0700881}
882
Yuwei Huangb181f712018-01-22 17:01:28 -0800883// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
884// a WiFi network with link-local IP address and a cellular network, then the
885// cellular candidate will still be gathered.
886TEST_F(BasicPortAllocatorTest,
887 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
888 SocketAddress wifi_link_local("169.254.0.1", 0);
889 SocketAddress cellular(IPAddress(0x12345601U), 0);
890 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
891 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
892
893 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
894 cricket::PORTALLOCATOR_DISABLE_RELAY |
895 cricket::PORTALLOCATOR_DISABLE_TCP |
896 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700897 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-22 17:01:28 -0800898 session_->StartGettingPorts();
899 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
900 kDefaultAllocationTimeout, fake_clock);
901 // Make sure we got both wifi and cell candidates.
902 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200903 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
904 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
Yuwei Huangb181f712018-01-22 17:01:28 -0800905}
906
907// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
908// a WiFi network with link-local IP address, a WiFi network with a normal IP
909// address and a cellular network, then the cellular candidate will not be
910// gathered.
911TEST_F(BasicPortAllocatorTest,
912 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
913 SocketAddress wifi(IPAddress(0x12345600U), 0);
914 SocketAddress wifi_link_local("169.254.0.1", 0);
915 SocketAddress cellular(IPAddress(0x12345601U), 0);
916 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
917 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
918 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
919
920 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
921 cricket::PORTALLOCATOR_DISABLE_RELAY |
922 cricket::PORTALLOCATOR_DISABLE_TCP |
923 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 11:55:07 -0700924 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-22 17:01:28 -0800925 session_->StartGettingPorts();
926 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
927 kDefaultAllocationTimeout, fake_clock);
928 // Make sure we got only wifi candidates.
929 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200930 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
931 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
Yuwei Huangb181f712018-01-22 17:01:28 -0800932}
933
Qingsi Wange53ac042018-05-08 11:55:07 -0700934// Test that the adapter types of the Ethernet and the VPN can be correctly
935// identified so that the Ethernet has a lower network cost than the VPN, and
936// the Ethernet is not filtered out if PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is
937// set.
938TEST_F(BasicPortAllocatorTest,
939 EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent) {
940 AddInterface(kClientAddr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
941 AddInterface(kClientAddr2, "tap0", rtc::ADAPTER_TYPE_VPN);
942 allocator().set_flags(PORTALLOCATOR_DISABLE_COSTLY_NETWORKS |
943 PORTALLOCATOR_DISABLE_RELAY |
944 PORTALLOCATOR_DISABLE_TCP);
945 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
946 session_->StartGettingPorts();
947 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
948 kDefaultAllocationTimeout, fake_clock);
949 // The VPN tap0 network should be filtered out as a costly network, and we
950 // should have a UDP port and a STUN port from the Ethernet eth0.
951 ASSERT_EQ(2U, ports_.size());
952 EXPECT_EQ(ports_[0]->Network()->name(), "eth0");
953 EXPECT_EQ(ports_[1]->Network()->name(), "eth0");
954}
955
deadbeef3427f532017-07-26 16:09:33 -0700956// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
957// to gather candidates.
958TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
959 // Add three IPv6 network interfaces, but tell the allocator to only use two.
960 allocator().set_max_ipv6_networks(2);
961 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
962 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
963 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
964
965 // To simplify the test, only gather UDP host candidates.
966 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
967 PORTALLOCATOR_DISABLE_STUN |
968 PORTALLOCATOR_DISABLE_RELAY);
969
Qingsi Wange53ac042018-05-08 11:55:07 -0700970 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 16:09:33 -0700971 session_->StartGettingPorts();
972 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
973 kDefaultAllocationTimeout, fake_clock);
974 EXPECT_EQ(2U, candidates_.size());
975 // Ensure the expected two interfaces (eth0 and eth1) were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +0200976 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
977 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
deadbeef3427f532017-07-26 16:09:33 -0700978}
979
980// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
981// being used.
982TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
983 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
984 allocator().set_max_ipv6_networks(1);
985 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
986 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
987 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
988 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
989
990 // To simplify the test, only gather UDP host candidates.
991 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
992 PORTALLOCATOR_DISABLE_STUN |
993 PORTALLOCATOR_DISABLE_RELAY);
994
Qingsi Wange53ac042018-05-08 11:55:07 -0700995 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 16:09:33 -0700996 session_->StartGettingPorts();
997 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
998 kDefaultAllocationTimeout, fake_clock);
999 EXPECT_EQ(3U, candidates_.size());
1000 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
1001 // were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001002 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
1003 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1004 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
deadbeef3427f532017-07-26 16:09:33 -07001005}
1006
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001007// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001008TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001009 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
1010 allocator_->SetNetworkIgnoreMask(0);
Qingsi Wange53ac042018-05-08 11:55:07 -07001011 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001012 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
1013 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001014 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001015 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1016 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001017 EXPECT_EQ(1U, candidates_.size());
1018}
1019
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001020// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001021TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001022 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001023 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001024 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001025 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1026 kDefaultAllocationTimeout, fake_clock);
1027 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001028 EXPECT_EQ(4U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001029 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1030 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1031 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1032 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1033 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1034 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1035 EXPECT_TRUE(
1036 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001037}
1038
honghaiz8c404fa2015-09-28 07:59:43 -07001039// Test that when the same network interface is brought down and up, the
1040// port allocator session will restart a new allocation sequence if
1041// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001042TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001043 std::string if_name("test_net0");
1044 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 11:55:07 -07001045 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001046 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001047 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1048 kDefaultAllocationTimeout, fake_clock);
1049 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001050 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001051 candidate_allocation_done_ = false;
1052 candidates_.clear();
1053 ports_.clear();
1054
deadbeef7f1563f2017-09-15 17:40:01 -07001055 // Disable socket creation to simulate the network interface being down. When
1056 // no network interfaces are available, BasicPortAllocator will fall back to
1057 // binding to the "ANY" address, so we need to make sure that fails too.
1058 fss_->set_tcp_sockets_enabled(false);
1059 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -07001060 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001061 SIMULATED_WAIT(false, 1000, fake_clock);
1062 EXPECT_EQ(0U, candidates_.size());
1063 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -07001064
1065 // When the same interfaces are added again, new candidates/ports should be
1066 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -07001067 fss_->set_tcp_sockets_enabled(true);
1068 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -07001069 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001070 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1071 kDefaultAllocationTimeout, fake_clock);
1072 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001073 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001074}
1075
1076// Test that when the same network interface is brought down and up, the
1077// port allocator session will not restart a new allocation sequence if
1078// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001079TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001080 std::string if_name("test_net0");
1081 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 11:55:07 -07001082 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001083 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001084 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1085 kDefaultAllocationTimeout, fake_clock);
1086 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001087 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001088 session_->StopGettingPorts();
1089 candidates_.clear();
1090 ports_.clear();
1091
1092 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001093 // Wait one (simulated) second and then verify no new candidates have
1094 // appeared.
1095 SIMULATED_WAIT(false, 1000, fake_clock);
1096 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001097 EXPECT_EQ(0U, ports_.size());
1098
1099 // When the same interfaces are added again, new candidates/ports should not
1100 // be generated because the session has stopped.
1101 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001102 SIMULATED_WAIT(false, 1000, fake_clock);
1103 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001104 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001105}
1106
deadbeef1c46a352017-09-27 11:24:05 -07001107// Similar to the above tests, but tests a situation when sockets can't be
1108// bound to a network interface, then after a network change event can be.
1109// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1110TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1111 // Only test local ports to simplify test.
1112 ResetWithNoServersOrNat();
1113 // Provide a situation where the interface appears to be available, but
1114 // binding the sockets fails. See bug for description of when this can
1115 // happen.
1116 std::string if_name("test_net0");
1117 AddInterface(kClientAddr, if_name);
1118 fss_->set_tcp_sockets_enabled(false);
1119 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001120 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 11:24:05 -07001121 session_->StartGettingPorts();
1122 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1123 kDefaultAllocationTimeout, fake_clock);
1124 // Make sure we actually prevented candidates from being gathered (other than
1125 // a single TCP active candidate, since that doesn't require creating a
1126 // socket).
1127 ASSERT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001128 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001129 candidate_allocation_done_ = false;
1130
1131 // Now simulate the interface coming up, with the newfound ability to bind
1132 // sockets.
1133 fss_->set_tcp_sockets_enabled(true);
1134 fss_->set_udp_sockets_enabled(true);
1135 AddInterface(kClientAddr, if_name);
1136 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1137 kDefaultAllocationTimeout, fake_clock);
1138 // Should get UDP and TCP candidate.
1139 ASSERT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001140 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001141 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1142 // We should extend this test to also verify that a server candidate is
1143 // gathered.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001144 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001145}
1146
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001147// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001148TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001150 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07001151 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 session_->StartGettingPorts();
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001153 // Host and STUN candidates from kClientAddr.
pthatcher1749bc32017-02-08 13:18:00 -08001154 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001155 // UDP and STUN ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 EXPECT_EQ(2U, ports_.size());
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001157 // Host, STUN and relay candidates from kClientAddr.
deadbeef1c5e6d02017-09-15 17:46:56 -07001158 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001159 // UDP, STUN and relay ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001160 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001161 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1162 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1163 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1164 EXPECT_TRUE(
1165 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001166 // One more TCP candidate from kClientAddr.
deadbeef1c5e6d02017-09-15 17:46:56 -07001167 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001168 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001169 EXPECT_EQ(4U, ports_.size());
1170 EXPECT_TRUE(candidate_allocation_done_);
1171 // If we Stop gathering now, we shouldn't get a second "done" callback.
1172 session_->StopGettingPorts();
1173}
1174
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001175TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001176 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001177 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001178 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001179 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1180 kDefaultAllocationTimeout, fake_clock);
1181 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001182 // If we Stop gathering now, we shouldn't get a second "done" callback.
1183 session_->StopGettingPorts();
1184
1185 // All ports should have unset send-buffer sizes.
1186 CheckSendBufferSizesOfAllPorts(-1);
1187}
1188
deadbeef7f1563f2017-09-15 17:40:01 -07001189// Tests that we can get callback after StopGetAllPorts when called in the
1190// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001191TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001193 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001194 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001195 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1196 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001197 EXPECT_EQ(2U, ports_.size());
1198 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001199 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1200 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001201}
1202
1203// Test that we restrict client ports appropriately when a port range is set.
1204// We check the candidates for udp/stun/tcp ports, and the from address
1205// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001206TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001207 AddInterface(kClientAddr);
1208 // Check that an invalid port range fails.
1209 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1210 // Check that a null port range succeeds.
1211 EXPECT_TRUE(SetPortRange(0, 0));
1212 // Check that a valid port range succeeds.
1213 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Qingsi Wange53ac042018-05-08 11:55:07 -07001214 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001215 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001216 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1217 kDefaultAllocationTimeout, fake_clock);
1218 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001219 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001220
1221 int num_nonrelay_candidates = 0;
1222 for (const Candidate& candidate : candidates_) {
1223 // Check the port number for the UDP/STUN/TCP port objects.
1224 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001225 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 11:44:17 -07001226 ++num_nonrelay_candidates;
1227 }
1228 }
1229 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230 // Check the port number used to connect to the relay server.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001231 EXPECT_TRUE(
1232 CheckPort(relay_server_.GetConnection(0).source(), kMinPort, kMaxPort));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001233}
1234
deadbeefe97389c2016-12-23 01:43:45 -08001235// Test that if we have no network adapters, we bind to the ANY address and
1236// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001237TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001238 // Default config uses GTURN and no NAT, so replace that with the
1239 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1240 ResetWithStunServerAndNat(kStunAddr);
1241 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1242 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1243 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1244 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1245 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1246 // TODO(deadbeef): Fix this.
1247 network_manager_.set_ipv6_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001248 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001249 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001250 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1251 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001252 EXPECT_EQ(4U, ports_.size());
1253 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1254 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1255 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1256 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1257 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1258 // The "any" address port should be in the signaled ready ports, but the host
1259 // candidate for it is useless and shouldn't be signaled. So we only have
1260 // STUN/TURN candidates.
1261 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001262 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1263 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 01:43:45 -08001264 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1265 // server.
1266 EXPECT_EQ(2,
1267 CountCandidates(candidates_, "relay", "udp",
1268 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001269}
1270
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001271// Test that when enumeration is disabled, we should not have any ports when
1272// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001273TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001274 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001275 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001276 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001277 // Expect to see no ports and no candidates.
1278 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1279 rtc::IPAddress(), rtc::IPAddress());
1280}
1281
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001282// Test that even with multiple interfaces, the result should still be a single
1283// default private, one STUN and one TURN candidate since we bind to any address
1284// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001285TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001286 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001287 AddInterface(kPrivateAddr);
1288 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001289 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001290 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001291
1292 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1293 // address set and we have no IPv6 STUN server, there should be no IPv6
1294 // candidates.
Qingsi Wange53ac042018-05-08 11:55:07 -07001295 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001296 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001297
1298 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1299 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1300 // TURN/UDP candidates.
1301 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001302 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1303 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001304}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001305
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001306// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1307// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001308TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001309 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001310 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001311 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001312 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001313 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1314 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1315 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1316 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001317 kTurnUdpExtAddr.ipaddr());
1318}
1319
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001320// Test that when adapter enumeration is disabled, for endpoints without
1321// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001322TEST_F(BasicPortAllocatorTest,
1323 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001324 ResetWithNoServersOrNat();
1325 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1326 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1327 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001328}
1329
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001330// Test that when adapter enumeration is disabled, with
1331// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1332// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001333TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001334 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1335 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001336 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001337 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001338 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1339 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001340 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001341 rtc::IPAddress(), rtc::IPAddress());
1342}
1343
1344// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001345// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1346// a NAT, there is no local candidate. However, this specified default route
1347// (kClientAddr) which was discovered when sending STUN requests, will become
1348// the srflx addresses.
Yves Gerey665174f2018-06-19 15:03:05 +02001349TEST_F(BasicPortAllocatorTest,
1350 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001351 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001352 AddInterfaceAsDefaultRoute(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001353 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001354 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001355 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1356 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001357 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1358 rtc::IPAddress(), rtc::IPAddress());
1359}
1360
1361// Test that when adapter enumeration is disabled, with
1362// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1363// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001364TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001365 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1366 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001367 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001368 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001369 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1370 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1371 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001372}
1373
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001374// Test that we disable relay over UDP, and only TCP is used when connecting to
1375// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001376TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001377 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001378 AddInterface(kClientAddr);
1379 ResetWithStunServerAndNat(kStunAddr);
1380 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001381 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001382 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1383 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1384 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001385
1386 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001387 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1388 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001389
1390 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1391 // TURN/TCP candidates.
1392 EXPECT_EQ(2U, ports_.size());
1393 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001394 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001395 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1396 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001397 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001398 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001399 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001400}
1401
Erik Språngefdce692015-06-05 09:41:26 +02001402// Disable for asan, see
1403// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1404#if !defined(ADDRESS_SANITIZER)
1405
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001406// Test that we can get OnCandidatesAllocationDone callback when all the ports
1407// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001408TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001409 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001410 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001411 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1412 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001414 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001415 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416}
1417
1418// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001419TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001420 AddInterface(kClientAddr);
1421 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001422 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001423 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001424 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1425 kDefaultAllocationTimeout, fake_clock);
1426 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001427 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001428 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1429 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1430 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1431 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1432 EXPECT_TRUE(
1433 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001434}
1435
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001436#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001437
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001438// Test that we don't crash or malfunction if we can't create UDP sockets or
1439// listen on TCP sockets. We still give out a local TCP address, since
1440// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001441TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001442 AddInterface(kClientAddr);
1443 fss_->set_udp_sockets_enabled(false);
1444 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001445 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001446 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001447 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1448 kDefaultAllocationTimeout, fake_clock);
1449 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001451 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1452 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1453 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1454 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1455 EXPECT_TRUE(
1456 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001457}
1458
1459// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001460// TODO(deadbeef): Find a way to exit early here.
1461TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001462 AddInterface(kClientAddr);
1463 fss_->set_tcp_sockets_enabled(false);
1464 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001465 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001466 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001467 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001468 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001469 // In case of Relay, ports creation will succeed but sockets will fail.
1470 // There is no error reporting from RelayEntry to handle this failure.
1471}
1472
1473// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001474TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001475 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001476 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kAnyAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001477 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001478 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001479 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001480 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1481 fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001482 // UDP and TCP ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001483 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001484 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1485 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001486 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001487 // will be tried after about 3 seconds.
1488 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001489 // UDP, TCP and relay ports on kClientAddr.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001490 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001491 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1492 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1493 EXPECT_TRUE(
1494 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1495 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001496 // We wait at least for a full STUN timeout, which
1497 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1498 // already passed (see above), we wait 3 seconds less than that.
1499 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1500 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001501}
1502
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001503TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001504 AddInterface(kClientAddr);
1505 AddInterface(kClientAddr2);
1506 // Allocating only host UDP ports. This is done purely for testing
1507 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001508 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1509 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001510 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001511 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001512 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1513 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001514 ASSERT_EQ(2U, candidates_.size());
1515 EXPECT_EQ(2U, ports_.size());
1516 // Candidates priorities should be different.
1517 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1518}
1519
1520// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001521TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001522 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001523 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001524 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001525 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1526 kDefaultAllocationTimeout, fake_clock);
1527 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001528 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001529 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001530}
1531
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001532// Test that the allocator session uses the candidate filter it's created with,
1533// rather than the filter of its parent allocator.
1534// The filter of the allocator should only affect the next gathering phase,
1535// according to JSEP, which means the *next* allocator session returned.
1536TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1537 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001538 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001539 // Set candidate filter *after* creating the session. Should have no effect.
1540 allocator().set_candidate_filter(CF_RELAY);
1541 session_->StartGettingPorts();
1542 // 7 candidates and 4 ports is what we would normally get (see the
1543 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001544 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1545 kDefaultAllocationTimeout, fake_clock);
1546 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001547 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001548}
1549
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001550// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1551// This test also verifies that when the allocator is only allowed to use
1552// relay (i.e. IceTransportsType is relay), the raddr is an empty
1553// address with the correct family. This is to prevent any local
1554// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001555TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001556 AddInterface(kClientAddr);
1557 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001558 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001559 allocator().set_candidate_filter(CF_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001560 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001561 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001562 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1563 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001564 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1565 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001566
1567 EXPECT_EQ(1U, candidates_.size());
1568 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001569 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1570 EXPECT_EQ(
1571 candidates_[0].related_address(),
1572 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001573}
1574
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001575TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001576 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001577 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1578 allocator().set_candidate_filter(CF_HOST);
Qingsi Wange53ac042018-05-08 11:55:07 -07001579 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001580 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001581 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1582 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001583 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1584 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001585 for (const Candidate& candidate : candidates_) {
1586 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001587 }
1588}
1589
1590// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001591TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001593 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001594
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001595 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1596 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001597 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001598 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001599 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1600 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001601 // Host is behind NAT, no private address will be exposed. Hence only UDP
1602 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001603 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1604 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001605 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1606 EXPECT_EQ(
1607 candidates_[0].related_address(),
1608 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001609}
1610
1611// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001612TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001613 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001614 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1615 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001616 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001617 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001618 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1619 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001620 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001621 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001622 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001623 for (const Candidate& candidate : candidates_) {
1624 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001625 }
1626}
1627
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001628// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001629TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001630 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001631 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001632 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001633 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1634 kDefaultAllocationTimeout, fake_clock);
1635 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001636 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1637 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1638 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001639 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001640 for (const Candidate& candidate : candidates_) {
1641 EXPECT_EQ(kIceUfrag0, candidate.username());
1642 EXPECT_EQ(kIcePwd0, candidate.password());
1643 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001644}
1645
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001646// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1647// is allocated for udp and stun. Also verify there is only one candidate
1648// (local) if stun candidate is same as local candidate, which will be the case
1649// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001650TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001651 AddInterface(kClientAddr);
1652 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001653 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001654 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001655 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001656 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1657 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001659 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001660 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1661 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001662}
1663
1664// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1665// is allocated for udp and stun. In this test we should expect both stun and
1666// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001667TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001669 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001670
1671 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001672 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001673 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001674 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001675 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1676 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001677 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001678 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1679 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1680 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001681 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1682 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001683 EXPECT_EQ(3U, candidates_.size());
1684}
1685
deadbeefc5d0d952015-07-16 10:22:21 -07001686// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001687TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001688 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001689 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001690 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001691 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001692
1693 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1694
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001695 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001696 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001697 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1698 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001699
Qingsi Wange53ac042018-05-08 11:55:07 -07001700 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001701 session_->StartGettingPorts();
1702
deadbeef7f1563f2017-09-15 17:40:01 -07001703 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1704 kDefaultAllocationTimeout, fake_clock);
1705 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001706 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001707 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1708 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1709 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1710 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1711 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001712}
1713
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001714// Test that if prune_turn_ports is set, TCP TURN port will not be used
1715// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1716TEST_F(BasicPortAllocatorTest,
1717 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1718 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1719 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1720 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001721
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001722 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001723}
1724
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001725// Test that if prune_turn_ports is set, TCP TURN port will not be used
1726// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1727TEST_F(BasicPortAllocatorTest,
1728 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1729 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1730 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1731 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001732
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001733 TestUdpTurnPortPrunesTcpTurnPort();
1734}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001735
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001736// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1737// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1738TEST_F(BasicPortAllocatorTest,
1739 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1740 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1741 // first.
1742 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1743 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001744
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001745 TestIPv6TurnPortPrunesIPv4TurnPort();
1746}
1747
1748// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1749// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1750TEST_F(BasicPortAllocatorTest,
1751 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1752 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1753 // first.
1754 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1755 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1756
1757 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001758}
1759
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001760// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001761// will has its own set of TurnPorts based on their priorities, in the default
1762// case where no transit delay is set.
1763TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1764 TestEachInterfaceHasItsOwnTurnPorts();
1765}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001766
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001767// Tests that if prune_turn_ports is set, each network interface
1768// will has its own set of TurnPorts based on their priorities, given that
1769// IPv4/TCP TURN port becomes ready first.
1770TEST_F(BasicPortAllocatorTest,
1771 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1772 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1773 // becomes ready last.
1774 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1775 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1776 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1777 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001778
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001779 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001780}
1781
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001782// Testing DNS resolve for the TURN server, this will test AllocationSequence
1783// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001784// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1785// appears that it doesn't currently because of the DNS look up not
1786// using the fake clock.
1787TEST_F(BasicPortAllocatorTestWithRealClock,
1788 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001789 // This test relies on a real query for "localhost", so it won't work on an
1790 // IPv6-only machine.
1791 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001792 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001793 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001794 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001795 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001796 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001797 RelayServerConfig turn_server(RELAY_TURN);
1798 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001799 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001800 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001801 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001802 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001803
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001804 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001805 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001806 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1807 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001808
Qingsi Wange53ac042018-05-08 11:55:07 -07001809 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001810 session_->StartGettingPorts();
1811
1812 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1813}
1814
1815// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1816// is allocated for udp/stun/turn. In this test we should expect all local,
1817// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001818TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001819 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001820 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001821
1822 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1823
1824 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001825 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1826 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001827
Qingsi Wange53ac042018-05-08 11:55:07 -07001828 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001829 session_->StartGettingPorts();
1830
deadbeef7f1563f2017-09-15 17:40:01 -07001831 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1832 kDefaultAllocationTimeout, fake_clock);
1833 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001834 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001835 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1836 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1837 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1838 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1839 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001840 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1841 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001842 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001843 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1844 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001845 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1846 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1847}
1848
1849// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1850// server is also used as the STUN server, we should get 'local', 'stun', and
1851// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001852TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001853 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001854 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001855 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001856 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1857
1858 // Must set the step delay to 0 to make sure the relay allocation phase is
1859 // started before the STUN candidates are obtained, so that the STUN binding
1860 // response is processed when both StunPort and TurnPort exist to reproduce
1861 // webrtc issue 3537.
1862 allocator_->set_step_delay(0);
1863 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001864 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1865 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001866
Qingsi Wange53ac042018-05-08 11:55:07 -07001867 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001868 session_->StartGettingPorts();
1869
deadbeef7f1563f2017-09-15 17:40:01 -07001870 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1871 kDefaultAllocationTimeout, fake_clock);
1872 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001873 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001874 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001875 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1876 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1877 &stun_candidate));
1878 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1879 candidates_, "relay", "udp",
1880 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1881 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001882
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001883 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001884 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1885 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001886 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1887 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1888}
1889
deadbeefc5d0d952015-07-16 10:22:21 -07001890// Test that when only a TCP TURN server is available, we do NOT use it as
1891// a UDP STUN server, as this could leak our IP address. Thus we should only
1892// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001893TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001894 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001895 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001896 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001897 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1898
1899 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001900 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1901 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001902
Qingsi Wange53ac042018-05-08 11:55:07 -07001903 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001904 session_->StartGettingPorts();
1905
deadbeef7f1563f2017-09-15 17:40:01 -07001906 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1907 kDefaultAllocationTimeout, fake_clock);
1908 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001909 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001910 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1911 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1912 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001913 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1914 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1915}
1916
1917// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1918// TURN server is used as the STUN server and we get 'local', 'stun', and
1919// 'relay' candidates.
1920// TODO(deadbeef): Remove this test when support for non-shared socket mode
1921// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001922TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001923 AddInterface(kClientAddr);
1924 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001925 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001926 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1927
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001928 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001929
Qingsi Wange53ac042018-05-08 11:55:07 -07001930 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001931 session_->StartGettingPorts();
1932
deadbeef7f1563f2017-09-15 17:40:01 -07001933 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1934 kDefaultAllocationTimeout, fake_clock);
1935 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001936 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001937 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001938 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001939 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1940 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1941 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001942 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001943 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1944 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1945 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001946 // Not using shared socket, so the STUN request's server reflexive address
1947 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001948 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001949
deadbeefc5d0d952015-07-16 10:22:21 -07001950 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1951 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1952 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1953}
1954
1955// Test that even when both a STUN and TURN server are configured, the TURN
1956// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001957TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001958 AddInterface(kClientAddr);
1959 // Configure with STUN server but destroy it, so we can ensure that it's
1960 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001961 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001962 stun_server_.reset();
1963 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1964
1965 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001966 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1967 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001968
Qingsi Wange53ac042018-05-08 11:55:07 -07001969 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001970 session_->StartGettingPorts();
1971
pthatcher1749bc32017-02-08 13:18:00 -08001972 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1973 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001974 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001975 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001976 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1977 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1978 &stun_candidate));
1979 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1980 candidates_, "relay", "udp",
1981 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1982 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001983
1984 // Don't bother waiting for STUN timeout, since we already verified
1985 // that we got a STUN candidate from the TURN server.
1986}
1987
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001988// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1989// and fail to generate STUN candidate, local UDP candidate is generated
1990// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001991TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001992 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1993 PORTALLOCATOR_DISABLE_TCP |
1994 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001995 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00001996 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kAnyAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001997 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001998 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001999 session_->StartGettingPorts();
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00002000 ASSERT_EQ_SIMULATED_WAIT(1U, candidates_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 13:18:00 -08002001 fake_clock);
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00002002 // UDP ports on kClientAddr.
2003 EXPECT_EQ(1U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002004 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07002005 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08002006 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2007 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002008 EXPECT_EQ(1U, candidates_.size());
2009}
2010
Mirko Bonadeiac5bbd92018-06-22 12:06:07 +00002011// Test that any address ports that are redundant do not surface.
2012TEST_F(BasicPortAllocatorTest, RedundantAnyAddressPortsDoNotSurface) {
2013 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2014 PORTALLOCATOR_DISABLE_TCP |
2015 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2016 AddInterface(kClientAddr);
2017 // The any address ports will be duplicates of kClientAddr.
2018 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr.ipaddr());
2019 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2020 session_->StartGettingPorts();
2021 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2022 fake_clock);
2023 EXPECT_EQ(1U, ports_.size());
2024 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
2025}
2026
2027// Test that candidates from the any address ports are not pruned if the
2028// explicit binding to enumerated networks fails.
2029TEST_F(BasicPortAllocatorTest,
2030 CandidatesFromAnyAddressPortsCanSurfaceWhenExplicitBindingFails) {
2031 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2032 PORTALLOCATOR_DISABLE_TCP |
2033 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2034 AddInterface(kClientAddr);
2035 fss_->SetUnbindableIps({kClientAddr.ipaddr()});
2036 // The any address ports will be duplicates of kClientAddr, but the explict
2037 // binding will fail.
2038 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr.ipaddr());
2039 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2040 session_->StartGettingPorts();
2041 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2042 fake_clock);
2043 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kAnyAddr));
2044 EXPECT_EQ(1U, candidates_.size());
2045 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2046}
2047
2048// Test that for an endpoint whose network enumeration only reveals one address
2049// (kClientAddr), it can observe a different address when binding to the "any"
2050// address. BasicPortAllocator should detect this and surface candidates for
2051// each address.
2052TEST_F(BasicPortAllocatorTest,
2053 CandidatesFromAnyAddressPortsCanSurfaceIfNotRedundant) {
2054 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2055 PORTALLOCATOR_DISABLE_TCP |
2056 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2057 AddInterface(kClientAddr);
2058 // When bound to the any address, the port allocator should discover the
2059 // alternative local address.
2060 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr2.ipaddr());
2061 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2062 session_->StartGettingPorts();
2063 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2064 fake_clock);
2065 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kAnyAddr));
2066 EXPECT_EQ(2U, candidates_.size());
2067 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2068 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
2069}
2070
2071// Test that any address ports and their candidates are eventually signaled
2072// after the maximum wait interval for the completion of candidate allocation,
2073// when the any address ports and candidates are not redundant.
2074TEST_F(BasicPortAllocatorTest,
2075 GetAnyAddressPortsAfterMaximumWaitForCandidateAllocationDone) {
2076 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
2077 AddInterface(kClientAddr);
2078 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr2.ipaddr());
2079 // STUN binding request and TURN allocation request will time out.
2080 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
2081 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2082 session_->StartGettingPorts();
2083 SIMULATED_WAIT(false, kMaxWaitMsBeforeSignalingAnyAddressPortsAndCandidates,
2084 fake_clock);
2085 EXPECT_EQ(2U, candidates_.size());
2086 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2087 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
2088 SIMULATED_WAIT(false, 1, fake_clock);
2089 EXPECT_FALSE(candidate_allocation_done_);
2090 // Candidates from the any address ports.
2091 EXPECT_EQ(6U, candidates_.size());
2092 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
2093 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr2));
2094 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr));
2095 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr2));
2096}
2097
2098// Test that the TCP port with the wildcard address is signaled if no ports are
2099// bound to enuemrated networks.
2100TEST_F(BasicPortAllocatorTest,
2101 GetAnyAddressTcpPortWhenNoPortsBoundToEnumeratedNetworks) {
2102 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
2103 AddInterface(kClientAddr);
2104 fss_->SetUnbindableIps({kClientAddr.ipaddr()});
2105 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2106 session_->StartGettingPorts();
2107 SIMULATED_WAIT(false,
2108 kMaxWaitMsBeforeSignalingAnyAddressPortsAndCandidates + 1,
2109 fake_clock);
2110 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
2111}
2112
2113// Test that the STUN candidate from the any address port can still surface, if
2114// it is gathered after this port is signaled, .
2115TEST_F(BasicPortAllocatorTest,
2116 StunCandidateFromAnyAddressPortsGatheredLateCanBeSignaled) {
2117 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
2118 AddInterface(kClientAddr);
2119 vss_->SetAlternativeLocalAddress(kAnyAddr.ipaddr(), kClientAddr2.ipaddr());
2120 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
2121 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr2);
2122 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2123 session_->StartGettingPorts();
2124 SIMULATED_WAIT(false,
2125 kMaxWaitMsBeforeSignalingAnyAddressPortsAndCandidates + 1000,
2126 fake_clock);
2127 EXPECT_FALSE(candidate_allocation_done_);
2128 EXPECT_EQ(4U, candidates_.size());
2129 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2130 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
2131 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
2132 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr2));
2133 fss_->ClearRules();
2134 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2135 kDefaultAllocationTimeout, fake_clock);
2136 EXPECT_EQ(7U, candidates_.size());
2137 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
2138 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr2));
2139 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr));
2140}
2141
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002142// Test that when the NetworkManager doesn't have permission to enumerate
2143// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2144// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002145TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002146 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2147 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002148 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07002149 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002150 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2151 PORTALLOCATOR_DISABLE_TCP |
2152 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2153 EXPECT_EQ(0U,
2154 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 11:55:07 -07002155 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002156 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002157 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002158 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2159 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002160 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002161 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002162 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002163}
2164
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002165// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002166TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002167 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2168 PORTALLOCATOR_ENABLE_IPV6 |
2169 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002170 AddInterface(kClientIPv6Addr);
2171 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002172 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002173 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002174 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002175 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002176 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002177 EXPECT_EQ(4U, ports_.size());
2178 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002179 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2180 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2181 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2182 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002183}
honghaiz98db68f2015-09-29 07:58:17 -07002184
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002185TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002186 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002187 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002188 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002189 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002190 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002191 EXPECT_EQ(2U, ports_.size());
2192 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002193 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002194
2195 // After stopping getting ports, adding a new interface will not start
2196 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002197 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002198 candidates_.clear();
2199 ports_.clear();
2200 candidate_allocation_done_ = false;
2201 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002202 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002203 EXPECT_EQ(0U, candidates_.size());
2204 EXPECT_EQ(0U, ports_.size());
2205}
2206
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002207TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002208 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002209 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002210 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002211 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002212 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002213 EXPECT_EQ(2U, ports_.size());
2214 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002215 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002216
2217 // After clearing getting ports, adding a new interface will start getting
2218 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002219 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002220 candidates_.clear();
2221 ports_.clear();
2222 candidate_allocation_done_ = false;
2223 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002224 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002225 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002226 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2227 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002228}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002229
2230// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2231// a pooled session is taken out of the pool.
2232TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2233 AddInterface(kClientAddr);
2234 int pool_size = 1;
2235 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002236 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002237 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002238 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002239 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2240 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002241 // Expect that when TakePooledSession is called,
2242 // UpdateTransportInformationInternal will be called and the
2243 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2244 // candidates.
2245 session_ =
2246 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2247 ASSERT_NE(nullptr, session_.get());
2248 auto ready_ports = session_->ReadyPorts();
2249 auto candidates = session_->ReadyCandidates();
2250 EXPECT_FALSE(ready_ports.empty());
2251 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002252 for (const PortInterface* port_interface : ready_ports) {
2253 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002254 EXPECT_EQ(kContentName, port->content_name());
2255 EXPECT_EQ(1, port->component());
2256 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2257 EXPECT_EQ(kIcePwd0, port->password());
2258 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002259 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002260 EXPECT_EQ(1, candidate.component());
2261 EXPECT_EQ(kIceUfrag0, candidate.username());
2262 EXPECT_EQ(kIcePwd0, candidate.password());
2263 }
2264}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002265
2266// Test that a new candidate filter takes effect even on already-gathered
2267// candidates.
2268TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2269 AddInterface(kClientAddr);
2270 int pool_size = 1;
2271 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002272 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002273 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2274 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002275 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2276 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002277 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2278 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2279 allocator_->set_candidate_filter(CF_RELAY);
2280 // Assume that when TakePooledSession is called, the candidate filter will be
2281 // applied to the pooled session. This is tested by PortAllocatorTest.
2282 session_ =
2283 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2284 ASSERT_NE(nullptr, session_.get());
2285 auto candidates = session_->ReadyCandidates();
2286 auto ports = session_->ReadyPorts();
2287 // Sanity check that the number of candidates and ports decreased.
2288 EXPECT_GT(initial_candidates_size, candidates.size());
2289 EXPECT_GT(initial_ports_size, ports.size());
2290 for (const PortInterface* port : ports) {
2291 // Expect only relay ports.
2292 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2293 }
2294 for (const Candidate& candidate : candidates) {
2295 // Expect only relay candidates now that the filter is applied.
2296 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2297 // Expect that the raddr is emptied due to the CF_RELAY filter.
2298 EXPECT_EQ(candidate.related_address(),
2299 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2300 }
2301}
2302
Qingsi Wang4ff54432018-03-01 18:25:20 -08002303TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2304 const int pool_size = 1;
2305 const int expected_stun_keepalive_interval = 123;
2306 AddInterface(kClientAddr);
2307 allocator_->SetConfiguration(allocator_->stun_servers(),
2308 allocator_->turn_servers(), pool_size, false,
2309 nullptr, expected_stun_keepalive_interval);
2310 auto* pooled_session = allocator_->GetPooledSession();
2311 ASSERT_NE(nullptr, pooled_session);
2312 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2313 kDefaultAllocationTimeout, fake_clock);
2314 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2315 expected_stun_keepalive_interval);
2316}
2317
2318TEST_F(BasicPortAllocatorTest,
2319 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2320 const int pool_size = 1;
2321 AddInterface(kClientAddr);
2322 allocator_->SetConfiguration(allocator_->stun_servers(),
2323 allocator_->turn_servers(), pool_size, false,
2324 nullptr, 123 /* stun keepalive interval */);
2325 auto* pooled_session = allocator_->GetPooledSession();
2326 ASSERT_NE(nullptr, pooled_session);
2327 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2328 kDefaultAllocationTimeout, fake_clock);
2329 const int expected_stun_keepalive_interval = 321;
2330 allocator_->SetConfiguration(allocator_->stun_servers(),
2331 allocator_->turn_servers(), pool_size, false,
2332 nullptr, expected_stun_keepalive_interval);
2333 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2334 expected_stun_keepalive_interval);
2335}
2336
2337TEST_F(BasicPortAllocatorTest,
2338 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2339 const int pool_size = 1;
2340 const int expected_stun_keepalive_interval = 123;
2341 AddInterface(kClientAddr);
2342 allocator_->set_flags(allocator().flags() |
2343 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2344 allocator_->SetConfiguration(allocator_->stun_servers(),
2345 allocator_->turn_servers(), pool_size, false,
2346 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002347 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002348 session_->StartGettingPorts();
2349 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2350 kDefaultAllocationTimeout, fake_clock);
2351 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2352 expected_stun_keepalive_interval);
2353}
2354
2355TEST_F(BasicPortAllocatorTest,
2356 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2357 const int pool_size = 1;
2358 const int expected_stun_keepalive_interval = 123;
2359 AddInterface(kClientAddr);
2360 allocator_->set_flags(allocator().flags() &
2361 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2362 allocator_->SetConfiguration(allocator_->stun_servers(),
2363 allocator_->turn_servers(), pool_size, false,
2364 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002365 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002366 session_->StartGettingPorts();
2367 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2368 kDefaultAllocationTimeout, fake_clock);
2369 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2370 expected_stun_keepalive_interval);
2371}
2372
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002373TEST_F(BasicPortAllocatorTest, IceRegatheringMetricsLoggedWhenNetworkChanges) {
2374 // Only test local ports to simplify test.
2375 ResetWithNoServersOrNat();
2376 AddInterface(kClientAddr, "test_net0");
2377 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2378 session_->StartGettingPorts();
2379 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2380 kDefaultAllocationTimeout, fake_clock);
2381 candidate_allocation_done_ = false;
2382 AddInterface(kClientAddr2, "test_net1");
2383 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2384 kDefaultAllocationTimeout, fake_clock);
2385 EXPECT_EQ(1, webrtc::metrics::NumEvents(
2386 "WebRTC.PeerConnection.IceRegatheringReason",
2387 static_cast<int>(IceRegatheringReason::NETWORK_CHANGE)));
2388}
2389
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002390} // namespace cricket