blob: 2b6dd3a9fcb9210b5f3b6c8285a778d80bd06160 [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();
pthatcher1749bc32017-02-08 13:18:00 -08001153 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001155 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001157 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1158 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1159 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1160 EXPECT_TRUE(
1161 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
deadbeef1c5e6d02017-09-15 17:46:56 -07001162 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001163 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001164 EXPECT_EQ(4U, ports_.size());
1165 EXPECT_TRUE(candidate_allocation_done_);
1166 // If we Stop gathering now, we shouldn't get a second "done" callback.
1167 session_->StopGettingPorts();
1168}
1169
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001170TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001171 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001172 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001173 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001174 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1175 kDefaultAllocationTimeout, fake_clock);
1176 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001177 // If we Stop gathering now, we shouldn't get a second "done" callback.
1178 session_->StopGettingPorts();
1179
1180 // All ports should have unset send-buffer sizes.
1181 CheckSendBufferSizesOfAllPorts(-1);
1182}
1183
deadbeef7f1563f2017-09-15 17:40:01 -07001184// Tests that we can get callback after StopGetAllPorts when called in the
1185// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001186TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001187 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001188 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001190 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1191 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 EXPECT_EQ(2U, ports_.size());
1193 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001194 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1195 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001196}
1197
1198// Test that we restrict client ports appropriately when a port range is set.
1199// We check the candidates for udp/stun/tcp ports, and the from address
1200// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001201TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001202 AddInterface(kClientAddr);
1203 // Check that an invalid port range fails.
1204 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1205 // Check that a null port range succeeds.
1206 EXPECT_TRUE(SetPortRange(0, 0));
1207 // Check that a valid port range succeeds.
1208 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Qingsi Wange53ac042018-05-08 11:55:07 -07001209 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001210 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001211 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1212 kDefaultAllocationTimeout, fake_clock);
1213 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001214 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001215
1216 int num_nonrelay_candidates = 0;
1217 for (const Candidate& candidate : candidates_) {
1218 // Check the port number for the UDP/STUN/TCP port objects.
1219 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001220 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 11:44:17 -07001221 ++num_nonrelay_candidates;
1222 }
1223 }
1224 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001225 // Check the port number used to connect to the relay server.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001226 EXPECT_TRUE(
1227 CheckPort(relay_server_.GetConnection(0).source(), kMinPort, kMaxPort));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001228}
1229
deadbeefe97389c2016-12-23 01:43:45 -08001230// Test that if we have no network adapters, we bind to the ANY address and
1231// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001232TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001233 // Default config uses GTURN and no NAT, so replace that with the
1234 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1235 ResetWithStunServerAndNat(kStunAddr);
1236 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1237 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1238 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1239 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1240 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1241 // TODO(deadbeef): Fix this.
1242 network_manager_.set_ipv6_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001243 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001244 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001245 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1246 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001247 EXPECT_EQ(4U, ports_.size());
1248 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1249 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1250 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1251 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1252 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1253 // The "any" address port should be in the signaled ready ports, but the host
1254 // candidate for it is useless and shouldn't be signaled. So we only have
1255 // STUN/TURN candidates.
1256 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001257 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1258 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 01:43:45 -08001259 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1260 // server.
1261 EXPECT_EQ(2,
1262 CountCandidates(candidates_, "relay", "udp",
1263 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001264}
1265
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001266// Test that when enumeration is disabled, we should not have any ports when
1267// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001268TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001269 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001270 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001271 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001272 // Expect to see no ports and no candidates.
1273 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1274 rtc::IPAddress(), rtc::IPAddress());
1275}
1276
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001277// Test that even with multiple interfaces, the result should still be a single
1278// default private, one STUN and one TURN candidate since we bind to any address
1279// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001280TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001281 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001282 AddInterface(kPrivateAddr);
1283 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001284 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001285 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001286
1287 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1288 // address set and we have no IPv6 STUN server, there should be no IPv6
1289 // candidates.
Qingsi Wange53ac042018-05-08 11:55:07 -07001290 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001291 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001292
1293 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1294 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1295 // TURN/UDP candidates.
1296 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001297 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1298 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001299}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001300
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001301// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1302// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001303TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001304 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001305 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001306 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001307 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001308 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1309 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1310 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1311 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001312 kTurnUdpExtAddr.ipaddr());
1313}
1314
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001315// Test that when adapter enumeration is disabled, for endpoints without
1316// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001317TEST_F(BasicPortAllocatorTest,
1318 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001319 ResetWithNoServersOrNat();
1320 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1321 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1322 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001323}
1324
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001325// Test that when adapter enumeration is disabled, with
1326// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1327// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001328TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001329 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1330 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001331 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001332 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001333 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1334 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001335 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001336 rtc::IPAddress(), rtc::IPAddress());
1337}
1338
1339// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001340// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1341// a NAT, there is no local candidate. However, this specified default route
1342// (kClientAddr) which was discovered when sending STUN requests, will become
1343// the srflx addresses.
Yves Gerey665174f2018-06-19 15:03:05 +02001344TEST_F(BasicPortAllocatorTest,
1345 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001346 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001347 AddInterfaceAsDefaultRoute(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001348 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001349 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001350 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1351 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001352 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1353 rtc::IPAddress(), rtc::IPAddress());
1354}
1355
1356// Test that when adapter enumeration is disabled, with
1357// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1358// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001359TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001360 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1361 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001362 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001363 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001364 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1365 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1366 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001367}
1368
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001369// Test that we disable relay over UDP, and only TCP is used when connecting to
1370// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001371TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001372 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001373 AddInterface(kClientAddr);
1374 ResetWithStunServerAndNat(kStunAddr);
1375 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001376 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001377 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1378 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1379 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001380
1381 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001382 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1383 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001384
1385 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1386 // TURN/TCP candidates.
1387 EXPECT_EQ(2U, ports_.size());
1388 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001389 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001390 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1391 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001392 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001393 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001394 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001395}
1396
Erik Språngefdce692015-06-05 09:41:26 +02001397// Disable for asan, see
1398// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1399#if !defined(ADDRESS_SANITIZER)
1400
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001401// Test that we can get OnCandidatesAllocationDone callback when all the ports
1402// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001403TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001404 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001405 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001406 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1407 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001408 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001409 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001411}
1412
1413// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001414TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001415 AddInterface(kClientAddr);
1416 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001417 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001418 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001419 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1420 kDefaultAllocationTimeout, fake_clock);
1421 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001422 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001423 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1424 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1425 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1426 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1427 EXPECT_TRUE(
1428 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001429}
1430
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001431#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001432
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001433// Test that we don't crash or malfunction if we can't create UDP sockets or
1434// listen on TCP sockets. We still give out a local TCP address, since
1435// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001436TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001437 AddInterface(kClientAddr);
1438 fss_->set_udp_sockets_enabled(false);
1439 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001440 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001442 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1443 kDefaultAllocationTimeout, fake_clock);
1444 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001445 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001446 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1447 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1448 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1449 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1450 EXPECT_TRUE(
1451 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001452}
1453
1454// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001455// TODO(deadbeef): Find a way to exit early here.
1456TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001457 AddInterface(kClientAddr);
1458 fss_->set_tcp_sockets_enabled(false);
1459 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 11:55:07 -07001460 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001461 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001462 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001463 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001464 // In case of Relay, ports creation will succeed but sockets will fail.
1465 // There is no error reporting from RelayEntry to handle this failure.
1466}
1467
1468// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001469TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1471 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001472 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001473 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001474 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1475 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001476 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001477 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1478 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001479 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001480 // will be tried after about 3 seconds.
1481 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001482 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001483 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1484 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1485 EXPECT_TRUE(
1486 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1487 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001488 // We wait at least for a full STUN timeout, which
1489 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1490 // already passed (see above), we wait 3 seconds less than that.
1491 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1492 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001493}
1494
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001495TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001496 AddInterface(kClientAddr);
1497 AddInterface(kClientAddr2);
1498 // Allocating only host UDP ports. This is done purely for testing
1499 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001500 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1501 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001502 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001503 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001504 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1505 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001506 ASSERT_EQ(2U, candidates_.size());
1507 EXPECT_EQ(2U, ports_.size());
1508 // Candidates priorities should be different.
1509 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1510}
1511
1512// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001513TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001514 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001515 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001516 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001517 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1518 kDefaultAllocationTimeout, fake_clock);
1519 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001520 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001521 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001522}
1523
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001524// Test that the allocator session uses the candidate filter it's created with,
1525// rather than the filter of its parent allocator.
1526// The filter of the allocator should only affect the next gathering phase,
1527// according to JSEP, which means the *next* allocator session returned.
1528TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1529 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001530 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001531 // Set candidate filter *after* creating the session. Should have no effect.
1532 allocator().set_candidate_filter(CF_RELAY);
1533 session_->StartGettingPorts();
1534 // 7 candidates and 4 ports is what we would normally get (see the
1535 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001536 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1537 kDefaultAllocationTimeout, fake_clock);
1538 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001539 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001540}
1541
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001542// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1543// This test also verifies that when the allocator is only allowed to use
1544// relay (i.e. IceTransportsType is relay), the raddr is an empty
1545// address with the correct family. This is to prevent any local
1546// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001547TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001548 AddInterface(kClientAddr);
1549 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001550 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001551 allocator().set_candidate_filter(CF_RELAY);
Qingsi Wange53ac042018-05-08 11:55:07 -07001552 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001553 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001554 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1555 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001556 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1557 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001558
1559 EXPECT_EQ(1U, candidates_.size());
1560 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001561 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1562 EXPECT_EQ(
1563 candidates_[0].related_address(),
1564 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001565}
1566
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001567TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001568 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001569 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1570 allocator().set_candidate_filter(CF_HOST);
Qingsi Wange53ac042018-05-08 11:55:07 -07001571 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001572 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001573 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1574 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001575 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1576 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001577 for (const Candidate& candidate : candidates_) {
1578 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001579 }
1580}
1581
1582// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001583TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001584 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001585 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001586
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001587 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1588 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001589 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001590 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001591 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1592 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001593 // Host is behind NAT, no private address will be exposed. Hence only UDP
1594 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001595 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1596 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001597 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1598 EXPECT_EQ(
1599 candidates_[0].related_address(),
1600 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001601}
1602
1603// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001604TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001605 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001606 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1607 allocator().set_candidate_filter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 11:55:07 -07001608 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001609 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001610 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1611 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001612 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001613 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001614 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001615 for (const Candidate& candidate : candidates_) {
1616 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001617 }
1618}
1619
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001620// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001621TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001622 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001623 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001624 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001625 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1626 kDefaultAllocationTimeout, fake_clock);
1627 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001628 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1629 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1630 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001631 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001632 for (const Candidate& candidate : candidates_) {
1633 EXPECT_EQ(kIceUfrag0, candidate.username());
1634 EXPECT_EQ(kIcePwd0, candidate.password());
1635 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001636}
1637
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001638// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1639// is allocated for udp and stun. Also verify there is only one candidate
1640// (local) if stun candidate is same as local candidate, which will be the case
1641// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001642TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001643 AddInterface(kClientAddr);
1644 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001645 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001646 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001647 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001648 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1649 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001650 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001651 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001652 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1653 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001654}
1655
1656// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1657// is allocated for udp and stun. In this test we should expect both stun and
1658// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001659TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001660 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001661 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001662
1663 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001664 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 11:55:07 -07001665 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001666 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001667 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1668 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001669 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001670 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1671 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1672 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001673 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1674 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001675 EXPECT_EQ(3U, candidates_.size());
1676}
1677
deadbeefc5d0d952015-07-16 10:22:21 -07001678// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001679TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001680 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001681 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001682 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001683 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001684
1685 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1686
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001687 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001688 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001689 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1690 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001691
Qingsi Wange53ac042018-05-08 11:55:07 -07001692 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001693 session_->StartGettingPorts();
1694
deadbeef7f1563f2017-09-15 17:40:01 -07001695 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1696 kDefaultAllocationTimeout, fake_clock);
1697 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001698 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001699 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1700 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1701 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1702 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1703 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001704}
1705
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001706// Test that if prune_turn_ports is set, TCP TURN port will not be used
1707// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1708TEST_F(BasicPortAllocatorTest,
1709 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1710 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1711 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1712 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001713
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001714 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001715}
1716
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001717// Test that if prune_turn_ports is set, TCP TURN port will not be used
1718// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1719TEST_F(BasicPortAllocatorTest,
1720 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1721 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1722 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1723 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001724
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001725 TestUdpTurnPortPrunesTcpTurnPort();
1726}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001727
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001728// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1729// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1730TEST_F(BasicPortAllocatorTest,
1731 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1732 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1733 // first.
1734 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1735 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001736
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001737 TestIPv6TurnPortPrunesIPv4TurnPort();
1738}
1739
1740// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1741// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1742TEST_F(BasicPortAllocatorTest,
1743 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1744 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1745 // first.
1746 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1747 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1748
1749 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001750}
1751
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001752// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001753// will has its own set of TurnPorts based on their priorities, in the default
1754// case where no transit delay is set.
1755TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1756 TestEachInterfaceHasItsOwnTurnPorts();
1757}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001758
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001759// Tests that if prune_turn_ports is set, each network interface
1760// will has its own set of TurnPorts based on their priorities, given that
1761// IPv4/TCP TURN port becomes ready first.
1762TEST_F(BasicPortAllocatorTest,
1763 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1764 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1765 // becomes ready last.
1766 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1767 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1768 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1769 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001770
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001771 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001772}
1773
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001774// Testing DNS resolve for the TURN server, this will test AllocationSequence
1775// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001776// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1777// appears that it doesn't currently because of the DNS look up not
1778// using the fake clock.
1779TEST_F(BasicPortAllocatorTestWithRealClock,
1780 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001781 // This test relies on a real query for "localhost", so it won't work on an
1782 // IPv6-only machine.
1783 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001784 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001785 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001786 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001787 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001788 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001789 RelayServerConfig turn_server(RELAY_TURN);
1790 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001791 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001792 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001793 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001794 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001795
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001796 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001797 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001798 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1799 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001800
Qingsi Wange53ac042018-05-08 11:55:07 -07001801 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001802 session_->StartGettingPorts();
1803
1804 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1805}
1806
1807// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1808// is allocated for udp/stun/turn. In this test we should expect all local,
1809// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001810TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001811 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001812 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001813
1814 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1815
1816 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001817 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1818 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001819
Qingsi Wange53ac042018-05-08 11:55:07 -07001820 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001821 session_->StartGettingPorts();
1822
deadbeef7f1563f2017-09-15 17:40:01 -07001823 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1824 kDefaultAllocationTimeout, fake_clock);
1825 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001826 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001827 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1828 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1829 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1830 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1831 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001832 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1833 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001834 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001835 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1836 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001837 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1838 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1839}
1840
1841// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1842// server is also used as the STUN server, we should get 'local', 'stun', and
1843// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001844TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001845 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001846 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001847 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001848 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1849
1850 // Must set the step delay to 0 to make sure the relay allocation phase is
1851 // started before the STUN candidates are obtained, so that the STUN binding
1852 // response is processed when both StunPort and TurnPort exist to reproduce
1853 // webrtc issue 3537.
1854 allocator_->set_step_delay(0);
1855 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001856 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1857 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001858
Qingsi Wange53ac042018-05-08 11:55:07 -07001859 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001860 session_->StartGettingPorts();
1861
deadbeef7f1563f2017-09-15 17:40:01 -07001862 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1863 kDefaultAllocationTimeout, fake_clock);
1864 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001865 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001866 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001867 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1868 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1869 &stun_candidate));
1870 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1871 candidates_, "relay", "udp",
1872 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1873 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001874
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001875 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001876 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1877 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001878 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1879 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1880}
1881
deadbeefc5d0d952015-07-16 10:22:21 -07001882// Test that when only a TCP TURN server is available, we do NOT use it as
1883// a UDP STUN server, as this could leak our IP address. Thus we should only
1884// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001885TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001886 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001887 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001888 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001889 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1890
1891 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001892 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1893 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001894
Qingsi Wange53ac042018-05-08 11:55:07 -07001895 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001896 session_->StartGettingPorts();
1897
deadbeef7f1563f2017-09-15 17:40:01 -07001898 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1899 kDefaultAllocationTimeout, fake_clock);
1900 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001901 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001902 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1903 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1904 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001905 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1906 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1907}
1908
1909// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1910// TURN server is used as the STUN server and we get 'local', 'stun', and
1911// 'relay' candidates.
1912// TODO(deadbeef): Remove this test when support for non-shared socket mode
1913// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001914TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001915 AddInterface(kClientAddr);
1916 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001917 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001918 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1919
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001920 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001921
Qingsi Wange53ac042018-05-08 11:55:07 -07001922 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001923 session_->StartGettingPorts();
1924
deadbeef7f1563f2017-09-15 17:40:01 -07001925 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1926 kDefaultAllocationTimeout, fake_clock);
1927 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001928 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001929 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001930 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001931 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1932 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1933 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001934 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001935 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1936 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1937 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001938 // Not using shared socket, so the STUN request's server reflexive address
1939 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001940 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001941
deadbeefc5d0d952015-07-16 10:22:21 -07001942 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1943 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1944 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1945}
1946
1947// Test that even when both a STUN and TURN server are configured, the TURN
1948// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001949TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001950 AddInterface(kClientAddr);
1951 // Configure with STUN server but destroy it, so we can ensure that it's
1952 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001953 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001954 stun_server_.reset();
1955 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1956
1957 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001958 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1959 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001960
Qingsi Wange53ac042018-05-08 11:55:07 -07001961 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001962 session_->StartGettingPorts();
1963
pthatcher1749bc32017-02-08 13:18:00 -08001964 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1965 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001966 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001967 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001968 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1969 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1970 &stun_candidate));
1971 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1972 candidates_, "relay", "udp",
1973 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1974 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001975
1976 // Don't bother waiting for STUN timeout, since we already verified
1977 // that we got a STUN candidate from the TURN server.
1978}
1979
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001980// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1981// and fail to generate STUN candidate, local UDP candidate is generated
1982// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001983TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001984 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1985 PORTALLOCATOR_DISABLE_TCP |
1986 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001987 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1988 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 11:55:07 -07001989 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001990 session_->StartGettingPorts();
Mirko Bonadei5f4d47b2018-08-22 17:41:22 +00001991 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 13:18:00 -08001992 fake_clock);
Mirko Bonadei5f4d47b2018-08-22 17:41:22 +00001993 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001994 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001995 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001996 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1997 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001998 EXPECT_EQ(1U, candidates_.size());
1999}
2000
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002001// Test that when the NetworkManager doesn't have permission to enumerate
2002// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2003// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002004TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002005 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2006 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002007 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07002008 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002009 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2010 PORTALLOCATOR_DISABLE_TCP |
2011 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2012 EXPECT_EQ(0U,
2013 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 11:55:07 -07002014 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002015 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002016 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002017 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2018 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08002019 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002020 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002021 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07002022}
2023
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002024// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002025TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002026 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2027 PORTALLOCATOR_ENABLE_IPV6 |
2028 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002029 AddInterface(kClientIPv6Addr);
2030 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002031 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002032 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002033 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002034 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002035 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002036 EXPECT_EQ(4U, ports_.size());
2037 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002038 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2039 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2040 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2041 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002042}
honghaiz98db68f2015-09-29 07:58:17 -07002043
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002044TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002045 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002046 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002047 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002048 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002049 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002050 EXPECT_EQ(2U, ports_.size());
2051 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002052 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002053
2054 // After stopping getting ports, adding a new interface will not start
2055 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002056 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002057 candidates_.clear();
2058 ports_.clear();
2059 candidate_allocation_done_ = false;
2060 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002061 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002062 EXPECT_EQ(0U, candidates_.size());
2063 EXPECT_EQ(0U, ports_.size());
2064}
2065
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002066TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002067 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002068 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 11:55:07 -07002069 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002070 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002071 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002072 EXPECT_EQ(2U, ports_.size());
2073 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002074 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002075
2076 // After clearing getting ports, adding a new interface will start getting
2077 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002078 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002079 candidates_.clear();
2080 ports_.clear();
2081 candidate_allocation_done_ = false;
2082 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002083 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002084 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002085 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2086 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002087}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002088
2089// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2090// a pooled session is taken out of the pool.
2091TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2092 AddInterface(kClientAddr);
2093 int pool_size = 1;
2094 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002095 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002096 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002097 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002098 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2099 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002100 // Expect that when TakePooledSession is called,
2101 // UpdateTransportInformationInternal will be called and the
2102 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2103 // candidates.
2104 session_ =
2105 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2106 ASSERT_NE(nullptr, session_.get());
2107 auto ready_ports = session_->ReadyPorts();
2108 auto candidates = session_->ReadyCandidates();
2109 EXPECT_FALSE(ready_ports.empty());
2110 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002111 for (const PortInterface* port_interface : ready_ports) {
2112 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002113 EXPECT_EQ(kContentName, port->content_name());
2114 EXPECT_EQ(1, port->component());
2115 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2116 EXPECT_EQ(kIcePwd0, port->password());
2117 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002118 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002119 EXPECT_EQ(1, candidate.component());
2120 EXPECT_EQ(kIceUfrag0, candidate.username());
2121 EXPECT_EQ(kIcePwd0, candidate.password());
2122 }
2123}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002124
2125// Test that a new candidate filter takes effect even on already-gathered
2126// candidates.
2127TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2128 AddInterface(kClientAddr);
2129 int pool_size = 1;
2130 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002131 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002132 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2133 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002134 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2135 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002136 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2137 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2138 allocator_->set_candidate_filter(CF_RELAY);
2139 // Assume that when TakePooledSession is called, the candidate filter will be
2140 // applied to the pooled session. This is tested by PortAllocatorTest.
2141 session_ =
2142 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2143 ASSERT_NE(nullptr, session_.get());
2144 auto candidates = session_->ReadyCandidates();
2145 auto ports = session_->ReadyPorts();
2146 // Sanity check that the number of candidates and ports decreased.
2147 EXPECT_GT(initial_candidates_size, candidates.size());
2148 EXPECT_GT(initial_ports_size, ports.size());
2149 for (const PortInterface* port : ports) {
2150 // Expect only relay ports.
2151 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2152 }
2153 for (const Candidate& candidate : candidates) {
2154 // Expect only relay candidates now that the filter is applied.
2155 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2156 // Expect that the raddr is emptied due to the CF_RELAY filter.
2157 EXPECT_EQ(candidate.related_address(),
2158 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2159 }
2160}
2161
Qingsi Wang4ff54432018-03-01 18:25:20 -08002162TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2163 const int pool_size = 1;
2164 const int expected_stun_keepalive_interval = 123;
2165 AddInterface(kClientAddr);
2166 allocator_->SetConfiguration(allocator_->stun_servers(),
2167 allocator_->turn_servers(), pool_size, false,
2168 nullptr, expected_stun_keepalive_interval);
2169 auto* pooled_session = allocator_->GetPooledSession();
2170 ASSERT_NE(nullptr, pooled_session);
2171 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2172 kDefaultAllocationTimeout, fake_clock);
2173 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2174 expected_stun_keepalive_interval);
2175}
2176
2177TEST_F(BasicPortAllocatorTest,
2178 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2179 const int pool_size = 1;
2180 AddInterface(kClientAddr);
2181 allocator_->SetConfiguration(allocator_->stun_servers(),
2182 allocator_->turn_servers(), pool_size, false,
2183 nullptr, 123 /* stun keepalive interval */);
2184 auto* pooled_session = allocator_->GetPooledSession();
2185 ASSERT_NE(nullptr, pooled_session);
2186 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2187 kDefaultAllocationTimeout, fake_clock);
2188 const int expected_stun_keepalive_interval = 321;
2189 allocator_->SetConfiguration(allocator_->stun_servers(),
2190 allocator_->turn_servers(), pool_size, false,
2191 nullptr, expected_stun_keepalive_interval);
2192 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2193 expected_stun_keepalive_interval);
2194}
2195
2196TEST_F(BasicPortAllocatorTest,
2197 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2198 const int pool_size = 1;
2199 const int expected_stun_keepalive_interval = 123;
2200 AddInterface(kClientAddr);
2201 allocator_->set_flags(allocator().flags() |
2202 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2203 allocator_->SetConfiguration(allocator_->stun_servers(),
2204 allocator_->turn_servers(), pool_size, false,
2205 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002206 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002207 session_->StartGettingPorts();
2208 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2209 kDefaultAllocationTimeout, fake_clock);
2210 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2211 expected_stun_keepalive_interval);
2212}
2213
2214TEST_F(BasicPortAllocatorTest,
2215 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2216 const int pool_size = 1;
2217 const int expected_stun_keepalive_interval = 123;
2218 AddInterface(kClientAddr);
2219 allocator_->set_flags(allocator().flags() &
2220 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2221 allocator_->SetConfiguration(allocator_->stun_servers(),
2222 allocator_->turn_servers(), pool_size, false,
2223 nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 11:55:07 -07002224 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-01 18:25:20 -08002225 session_->StartGettingPorts();
2226 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2227 kDefaultAllocationTimeout, fake_clock);
2228 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2229 expected_stun_keepalive_interval);
2230}
2231
Qingsi Wang7fc821d2018-07-12 12:54:53 -07002232TEST_F(BasicPortAllocatorTest, IceRegatheringMetricsLoggedWhenNetworkChanges) {
2233 // Only test local ports to simplify test.
2234 ResetWithNoServersOrNat();
2235 AddInterface(kClientAddr, "test_net0");
2236 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2237 session_->StartGettingPorts();
2238 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2239 kDefaultAllocationTimeout, fake_clock);
2240 candidate_allocation_done_ = false;
2241 AddInterface(kClientAddr2, "test_net1");
2242 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2243 kDefaultAllocationTimeout, fake_clock);
2244 EXPECT_EQ(1, webrtc::metrics::NumEvents(
2245 "WebRTC.PeerConnection.IceRegatheringReason",
2246 static_cast<int>(IceRegatheringReason::NETWORK_CHANGE)));
2247}
2248
Qingsi Wang09619332018-09-12 22:51:55 -07002249// Test that when an mDNS responder is present, the local address of a host
2250// candidate is masked by an mDNS hostname and the related address of any other
2251// type of candidates is set to 0.0.0.0 or ::0.
2252TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) {
2253 // Default config uses GTURN and no NAT, so replace that with the
2254 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
2255 ResetWithStunServerAndNat(kStunAddr);
2256 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
2257 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2258 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
2259
2260 ASSERT_EQ(&network_manager_, allocator().network_manager());
2261 network_manager_.CreateMDnsResponder();
2262 AddInterface(kClientAddr);
2263 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2264 session_->StartGettingPorts();
2265 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2266 kDefaultAllocationTimeout, fake_clock);
2267 EXPECT_EQ(5u, candidates_.size());
2268 int num_host_udp_candidates = 0;
2269 int num_host_tcp_candidates = 0;
2270 int num_srflx_candidates = 0;
2271 int num_relay_candidates = 0;
2272 for (const auto& candidate : candidates_) {
2273 if (candidate.type() == LOCAL_PORT_TYPE) {
2274 EXPECT_TRUE(candidate.address().IsUnresolvedIP());
2275 if (candidate.protocol() == UDP_PROTOCOL_NAME) {
2276 ++num_host_udp_candidates;
2277 } else {
2278 ++num_host_tcp_candidates;
2279 }
2280 } else {
2281 EXPECT_NE(PRFLX_PORT_TYPE, candidate.type());
2282 // The related address should be set to 0.0.0.0 or ::0 for srflx and
2283 // relay candidates.
2284 EXPECT_EQ(rtc::SocketAddress(), candidate.related_address());
2285 if (candidate.type() == STUN_PORT_TYPE) {
2286 ++num_srflx_candidates;
2287 } else {
2288 ++num_relay_candidates;
2289 }
2290 }
2291 }
2292 EXPECT_EQ(1, num_host_udp_candidates);
2293 EXPECT_EQ(1, num_host_tcp_candidates);
2294 EXPECT_EQ(1, num_srflx_candidates);
2295 EXPECT_EQ(2, num_relay_candidates);
2296}
2297
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002298} // namespace cricket