blob: f39e61e3ee9c893c3162d0b9c8ea749976add6ef [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/basicpacketsocketfactory.h"
15#include "p2p/base/p2pconstants.h"
16#include "p2p/base/p2ptransportchannel.h"
Qingsi Wang4ff54432018-03-01 18:25:20 -080017#include "p2p/base/stunport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/testrelayserver.h"
19#include "p2p/base/teststunserver.h"
20#include "p2p/base/testturnserver.h"
21#include "p2p/client/basicportallocator.h"
22#include "rtc_base/fakeclock.h"
23#include "rtc_base/fakenetwork.h"
24#include "rtc_base/firewallsocketserver.h"
25#include "rtc_base/gunit.h"
26#include "rtc_base/helpers.h"
27#include "rtc_base/ipaddress.h"
28#include "rtc_base/logging.h"
29#include "rtc_base/natserver.h"
30#include "rtc_base/natsocketfactory.h"
31#include "rtc_base/nethelpers.h"
32#include "rtc_base/network.h"
33#include "rtc_base/socketaddress.h"
34#include "rtc_base/ssladapter.h"
35#include "rtc_base/thread.h"
36#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080038using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039using rtc::SocketAddress;
40using rtc::Thread;
41
Mirko Bonadei675513b2017-11-09 11:09:25 +010042#define MAYBE_SKIP_IPV4 \
43 if (!rtc::HasIPv4Enabled()) { \
44 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
45 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070046 }
47
deadbeefe97389c2016-12-23 01:43:45 -080048static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070050static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070051static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000053static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070054static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
55 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070056static const SocketAddress kClientIPv6Addr2(
57 "2401:fa00:4:2000:be30:5bff:fee5:c3",
58 0);
deadbeef3427f532017-07-26 16:09:33 -070059static const SocketAddress kClientIPv6Addr3(
60 "2401:fa00:4:3000:be30:5bff:fee5:c3",
61 0);
deadbeefc5d0d952015-07-16 10:22:21 -070062static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
63static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
65static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
66static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
67static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
68static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
69static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
70static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
71static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
72static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070073static const SocketAddress kTurnUdpIntIPv6Addr(
74 "2402:fb00:4:1000:be30:5bff:fee5:c3",
75 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070077static const SocketAddress kTurnTcpIntIPv6Addr(
78 "2402:fb00:4:2000:be30:5bff:fee5:c3",
79 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
81
82// Minimum and maximum port for port range tests.
83static const int kMinPort = 10000;
84static const int kMaxPort = 10099;
85
86// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070087static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088// Based on ICE_PWD_LENGTH
89static const char kIcePwd0[] = "TESTICEPWD00000000000000";
90
91static const char kContentName[] = "test content";
92
Honghai Zhangc67e0f52016-09-19 16:57:37 -070093static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094static const char kTurnUsername[] = "test";
95static const char kTurnPassword[] = "test";
96
pthatcher94a2f212017-02-08 14:42:22 -080097// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070098// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080099static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -0700100
Qingsi Wang4ff54432018-03-01 18:25:20 -0800101namespace {
102
103void CheckStunKeepaliveIntervalOfAllReadyPorts(
104 const cricket::PortAllocatorSession* allocator_session,
105 int expected) {
106 auto ready_ports = allocator_session->ReadyPorts();
107 for (const auto* port : ready_ports) {
108 if (port->Type() == cricket::STUN_PORT_TYPE ||
109 (port->Type() == cricket::LOCAL_PORT_TYPE &&
110 port->GetProtocol() == cricket::PROTO_UDP)) {
111 EXPECT_EQ(
112 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
113 expected);
114 }
115 }
116}
117
118} // namespace
119
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120namespace cricket {
121
122// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700123std::ostream& operator<<(std::ostream& os,
124 const std::vector<Candidate>& candidates) {
125 os << '[';
126 bool first = true;
127 for (const Candidate& c : candidates) {
128 if (!first) {
129 os << ", ";
130 }
131 os << c.ToString();
132 first = false;
Steve Anton6c38cc72017-11-29 10:25:58 -0800133 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700134 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 return os;
136}
137
pthatcher1749bc32017-02-08 13:18:00 -0800138class BasicPortAllocatorTestBase : public testing::Test,
139 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 public:
pthatcher1749bc32017-02-08 13:18:00 -0800141 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700142 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700144 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800145 // Note that the NAT is not used by default. ResetWithStunServerAndNat
146 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700147 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700148 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700149 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700150 relay_server_(Thread::Current(),
151 kRelayUdpIntAddr,
152 kRelayUdpExtAddr,
153 kRelayTcpIntAddr,
154 kRelayTcpExtAddr,
155 kRelaySslTcpIntAddr,
156 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
158 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 stun_servers.insert(kStunAddr);
161 // Passing the addresses of GTURN servers will enable GTURN in
162 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800163 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
164 // configuration should be blank by default (preferred), or it should use
165 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700166 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
167 kRelayUdpIntAddr, kRelayTcpIntAddr,
168 kRelaySslTcpIntAddr));
169 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 }
171
172 void AddInterface(const SocketAddress& addr) {
173 network_manager_.AddInterface(addr);
174 }
honghaiz8c404fa2015-09-28 07:59:43 -0700175 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
176 network_manager_.AddInterface(addr, if_name);
177 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800178 void AddInterface(const SocketAddress& addr,
179 const std::string& if_name,
180 rtc::AdapterType type) {
181 network_manager_.AddInterface(addr, if_name, type);
182 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800183 // The default route is the public address that STUN server will observe when
184 // the endpoint is sitting on the public internet and the local port is bound
185 // to the "any" address. This may be different from the default local address
186 // which the endpoint observes. This can occur if the route to the public
187 // endpoint like 8.8.8.8 (specified as the default local address) is
188 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700189 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
190 AddInterface(addr);
191 // When a binding comes from the any address, the |addr| will be used as the
192 // srflx address.
193 vss_->SetDefaultRoute(addr.ipaddr());
194 }
honghaiz8c404fa2015-09-28 07:59:43 -0700195 void RemoveInterface(const SocketAddress& addr) {
196 network_manager_.RemoveInterface(addr);
197 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 bool SetPortRange(int min_port, int max_port) {
199 return allocator_->SetPortRange(min_port, max_port);
200 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700201 // Endpoint is on the public network. No STUN or TURN.
202 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700203 allocator_.reset(new BasicPortAllocator(&network_manager_));
204 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700205 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700206 // Endpoint is behind a NAT, with STUN specified.
207 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
208 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700209 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700210 // Endpoint is on the public network, with STUN specified.
211 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
212 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700214 // Endpoint is on the public network, with TURN specified.
215 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
216 const rtc::SocketAddress& tcp_turn) {
217 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218 AddTurnServers(udp_turn, tcp_turn);
219 }
220
221 void AddTurnServers(const rtc::SocketAddress& udp_turn,
222 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700223 RelayServerConfig turn_server(RELAY_TURN);
224 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800225 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226
227 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800228 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 }
230 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800231 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 }
deadbeef653b8e02015-11-11 12:55:10 -0800233 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 }
235
236 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700237 session_ = CreateSession("session", component);
238 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700240 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 return true;
242 }
243
244 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700245 session_ = CreateSession("session", content_name, component);
246 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700248 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 return true;
250 }
251
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700252 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
253 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 return CreateSession(sid, kContentName, component);
255 }
256
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700257 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700258 const std::string& sid,
259 const std::string& content_name,
260 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000261 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
262 }
263
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700264 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700265 const std::string& sid,
266 const std::string& content_name,
267 int component,
268 const std::string& ice_ufrag,
269 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700270 std::unique_ptr<PortAllocatorSession> session =
271 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800273 &BasicPortAllocatorTestBase::OnPortReady);
274 session->SignalPortsPruned.connect(
275 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700276 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800277 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700278 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800279 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700280 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800281 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000282 return session;
283 }
284
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700285 // Return true if the addresses are the same, or the port is 0 in |pattern|
286 // (acting as a wildcard) and the IPs are the same.
287 // Even with a wildcard port, the port of the address should be nonzero if
288 // the IP is nonzero.
289 static bool AddressMatch(const SocketAddress& address,
290 const SocketAddress& pattern) {
291 return address.ipaddr() == pattern.ipaddr() &&
292 ((pattern.port() == 0 &&
293 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
294 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000295 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700296
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700297 // Returns the number of ports that have matching type, protocol and
298 // address.
299 static int CountPorts(const std::vector<PortInterface*>& ports,
300 const std::string& type,
301 ProtocolType protocol,
302 const SocketAddress& client_addr) {
303 return std::count_if(
304 ports.begin(), ports.end(),
305 [type, protocol, client_addr](PortInterface* port) {
306 return port->Type() == type && port->GetProtocol() == protocol &&
307 port->Network()->GetBestIP() == client_addr.ipaddr();
308 });
309 }
310
deadbeefe97389c2016-12-23 01:43:45 -0800311 static int CountCandidates(const std::vector<Candidate>& candidates,
312 const std::string& type,
313 const std::string& proto,
314 const SocketAddress& addr) {
315 return std::count_if(candidates.begin(), candidates.end(),
316 [type, proto, addr](const Candidate& c) {
317 return c.type() == type && c.protocol() == proto &&
318 AddressMatch(c.address(), addr);
319 });
320 }
321
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700322 // Find a candidate and return it.
323 static bool FindCandidate(const std::vector<Candidate>& candidates,
324 const std::string& type,
325 const std::string& proto,
326 const SocketAddress& addr,
327 Candidate* found) {
328 auto it = std::find_if(candidates.begin(), candidates.end(),
329 [type, proto, addr](const Candidate& c) {
330 return c.type() == type && c.protocol() == proto &&
331 AddressMatch(c.address(), addr);
332 });
333 if (it != candidates.end() && found) {
334 *found = *it;
335 }
336 return it != candidates.end();
337 }
338
339 // Convenience method to call FindCandidate with no return.
340 static bool HasCandidate(const std::vector<Candidate>& candidates,
341 const std::string& type,
342 const std::string& proto,
343 const SocketAddress& addr) {
344 return FindCandidate(candidates, type, proto, addr, nullptr);
345 }
346
347 // Version of HasCandidate that also takes a related address.
348 static bool HasCandidateWithRelatedAddr(
349 const std::vector<Candidate>& candidates,
350 const std::string& type,
351 const std::string& proto,
352 const SocketAddress& addr,
353 const SocketAddress& related_addr) {
354 auto it =
355 std::find_if(candidates.begin(), candidates.end(),
356 [type, proto, addr, related_addr](const Candidate& c) {
357 return c.type() == type && c.protocol() == proto &&
358 AddressMatch(c.address(), addr) &&
359 AddressMatch(c.related_address(), related_addr);
360 });
361 return it != candidates.end();
362 }
363
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 int min_port,
366 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000367 return (addr.port() >= min_port && addr.port() <= max_port);
368 }
369
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700370 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 // We should only get this callback once, except in the mux test where
372 // we have multiple port allocation sessions.
373 if (session == session_.get()) {
374 ASSERT_FALSE(candidate_allocation_done_);
375 candidate_allocation_done_ = true;
376 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700377 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378 }
379
380 // Check if all ports allocated have send-buffer size |expected|. If
381 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
382 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700383 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384 for (it = ports_.begin(); it < ports_.end(); ++it) {
385 int send_buffer_size;
386 if (expected == -1) {
387 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700388 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 EXPECT_EQ(0,
391 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000392 ASSERT_EQ(expected, send_buffer_size);
393 }
394 }
395 }
396
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700397 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
398
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000399 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700400 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700402 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100403 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000404 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700405 // Make sure the new port is added to ReadyPorts.
406 auto ready_ports = ses->ReadyPorts();
407 EXPECT_NE(ready_ports.end(),
408 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700410 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700411 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100412 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700413 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700414 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700415 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700416 new_end = std::remove(ports_.begin(), new_end, port);
417 // Make sure the pruned port is not in ReadyPorts.
418 EXPECT_EQ(ready_ports.end(),
419 std::find(ready_ports.begin(), ready_ports.end(), port));
420 }
421 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700422 }
423
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700424 void OnCandidatesReady(PortAllocatorSession* ses,
425 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700426 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100427 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700428 // Sanity check that the ICE component is set.
429 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
430 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000431 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700432 // Make sure the new candidates are added to Candidates.
433 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700434 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700435 EXPECT_NE(
436 ses_candidates.end(),
437 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
438 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439 }
440
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700441 void OnCandidatesRemoved(PortAllocatorSession* session,
442 const std::vector<Candidate>& removed_candidates) {
443 auto new_end = std::remove_if(
444 candidates_.begin(), candidates_.end(),
445 [removed_candidates](Candidate& candidate) {
446 for (const Candidate& removed_candidate : removed_candidates) {
447 if (candidate.MatchesForRemoval(removed_candidate)) {
448 return true;
449 }
450 }
451 return false;
452 });
453 candidates_.erase(new_end, candidates_.end());
454 }
455
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700456 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800457 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700458 RelayServerConfig server_config = allocator_->turn_servers()[i];
459 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700461 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000462 if (proto_addr.address == relay_port->address &&
463 proto_addr.proto == relay_port->proto)
464 return true;
465 }
466 }
467 return false;
468 }
469
Guo-wei Shieh11477022015-08-15 09:28:41 -0700470 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
471 bool with_nat) {
472 if (with_nat) {
473 nat_server_.reset(new rtc::NATServer(
474 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
475 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
476 } else {
477 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
478 }
479
480 ServerAddresses stun_servers;
481 if (!stun_server.IsNil()) {
482 stun_servers.insert(stun_server);
483 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700484 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700485 &network_manager_, nat_socket_factory_.get(), stun_servers));
Patrik Höglund3dc41062018-04-11 11:13:57 +0000486 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700487 }
488
pthatcher1749bc32017-02-08 13:18:00 -0800489 std::unique_ptr<rtc::VirtualSocketServer> vss_;
490 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700491 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800492 std::unique_ptr<rtc::NATServer> nat_server_;
493 rtc::NATSocketFactory nat_factory_;
494 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
495 std::unique_ptr<TestStunServer> stun_server_;
496 TestRelayServer relay_server_;
497 TestTurnServer turn_server_;
498 rtc::FakeNetworkManager network_manager_;
499 std::unique_ptr<BasicPortAllocator> allocator_;
500 std::unique_ptr<PortAllocatorSession> session_;
501 std::vector<PortInterface*> ports_;
502 std::vector<Candidate> candidates_;
503 bool candidate_allocation_done_;
504};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700505
pthatcher1749bc32017-02-08 13:18:00 -0800506class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
507};
508
509class FakeClockBase {
510 public:
511 rtc::ScopedFakeClock fake_clock;
512};
513
514class BasicPortAllocatorTest : public FakeClockBase,
515 public BasicPortAllocatorTestBase {
516 public:
517 // This function starts the port/address gathering and check the existence of
518 // candidates as specified. When |expect_stun_candidate| is true,
519 // |stun_candidate_addr| carries the expected reflective address, which is
520 // also the related address for TURN candidate if it is expected. Otherwise,
521 // it should be ignore.
522 void CheckDisableAdapterEnumeration(
523 uint32_t total_ports,
524 const rtc::IPAddress& host_candidate_addr,
525 const rtc::IPAddress& stun_candidate_addr,
526 const rtc::IPAddress& relay_candidate_udp_transport_addr,
527 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
528 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
529 rtc::IPAddress());
530 if (!session_) {
531 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
532 }
533 session_->set_flags(session_->flags() |
534 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
535 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
536 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700537 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800538 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
539 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700540
pthatcher1749bc32017-02-08 13:18:00 -0800541 uint32_t total_candidates = 0;
542 if (!host_candidate_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200543 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
544 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800545 ++total_candidates;
546 }
547 if (!stun_candidate_addr.IsNil()) {
548 rtc::SocketAddress related_address(host_candidate_addr, 0);
549 if (host_candidate_addr.IsNil()) {
550 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
551 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200552 EXPECT_TRUE(HasCandidateWithRelatedAddr(
553 candidates_, "stun", "udp",
554 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
pthatcher1749bc32017-02-08 13:18:00 -0800555 ++total_candidates;
556 }
557 if (!relay_candidate_udp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200558 EXPECT_TRUE(HasCandidateWithRelatedAddr(
559 candidates_, "relay", "udp",
560 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
561 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800562 ++total_candidates;
563 }
564 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200565 EXPECT_TRUE(HasCandidateWithRelatedAddr(
566 candidates_, "relay", "udp",
567 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
568 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800569 ++total_candidates;
570 }
571
572 EXPECT_EQ(total_candidates, candidates_.size());
573 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700574 }
575
576 void TestIPv6TurnPortPrunesIPv4TurnPort() {
577 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
578 // Add two IP addresses on the same interface.
579 AddInterface(kClientAddr, "net1");
580 AddInterface(kClientIPv6Addr, "net1");
581 allocator_.reset(new BasicPortAllocator(&network_manager_));
582 allocator_->SetConfiguration(allocator_->stun_servers(),
583 allocator_->turn_servers(), 0, true);
584 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
585 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
586
587 allocator_->set_step_delay(kMinimumStepDelay);
588 allocator_->set_flags(
589 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
590 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
591
592 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
593 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800594 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
595 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700596 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
597 EXPECT_EQ(3U, session_->ReadyPorts().size());
598 EXPECT_EQ(3U, ports_.size());
599 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
600 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
601 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
602 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
603
604 // Now that we remove candidates when a TURN port is pruned, there will be
605 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
606 EXPECT_EQ(3U, candidates_.size());
607 const std::vector<Candidate>& ready_candidates =
608 session_->ReadyCandidates();
609 EXPECT_EQ(3U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200610 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
611 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
612 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700613 }
614
pthatcher1749bc32017-02-08 13:18:00 -0800615 void TestUdpTurnPortPrunesTcpTurnPort() {
616 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
617 AddInterface(kClientAddr);
618 allocator_.reset(new BasicPortAllocator(&network_manager_));
619 allocator_->SetConfiguration(allocator_->stun_servers(),
620 allocator_->turn_servers(), 0, true);
621 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
622 allocator_->set_step_delay(kMinimumStepDelay);
623 allocator_->set_flags(allocator().flags() |
624 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
625 PORTALLOCATOR_DISABLE_TCP);
626
627 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
628 session_->StartGettingPorts();
629 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
630 kDefaultAllocationTimeout, fake_clock);
631 // Only 2 ports (one STUN and one TURN) are actually being used.
632 EXPECT_EQ(2U, session_->ReadyPorts().size());
633 // We have verified that each port, when it is added to |ports_|, it is
634 // found in |ready_ports|, and when it is pruned, it is not found in
635 // |ready_ports|, so we only need to verify the content in one of them.
636 EXPECT_EQ(2U, ports_.size());
637 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
638 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
639 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
640
641 // Now that we remove candidates when a TURN port is pruned, |candidates_|
642 // should only contains two candidates regardless whether the TCP TURN port
643 // is created before or after the UDP turn port.
644 EXPECT_EQ(2U, candidates_.size());
645 // There will only be 2 candidates in |ready_candidates| because it only
646 // includes the candidates in the ready ports.
647 const std::vector<Candidate>& ready_candidates =
648 session_->ReadyCandidates();
649 EXPECT_EQ(2U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200650 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
651 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
652 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800653 }
654
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700655 void TestEachInterfaceHasItsOwnTurnPorts() {
656 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
657 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
658 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
659 // Add two interfaces both having IPv4 and IPv6 addresses.
660 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
661 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
662 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
663 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
664 allocator_.reset(new BasicPortAllocator(&network_manager_));
665 allocator_->SetConfiguration(allocator_->stun_servers(),
666 allocator_->turn_servers(), 0, true);
667 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
668 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
669 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
670
671 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800672 allocator_->set_flags(
673 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
674 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700675 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
676 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800677 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
678 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700679 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
680 // use.
681 EXPECT_EQ(10U, session_->ReadyPorts().size());
682 EXPECT_EQ(10U, ports_.size());
683 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
684 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
685 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
686 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
687 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
688 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
689 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
690 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
691 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
692 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
693
694 // Now that we remove candidates when TURN ports are pruned, there will be
695 // exactly 10 candidates in |candidates_|.
696 EXPECT_EQ(10U, candidates_.size());
697 const std::vector<Candidate>& ready_candidates =
698 session_->ReadyCandidates();
699 EXPECT_EQ(10U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200700 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
701 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
702 EXPECT_TRUE(
703 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
704 EXPECT_TRUE(
705 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
706 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
707 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
708 EXPECT_TRUE(
709 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
710 EXPECT_TRUE(
711 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
712 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
713 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700714 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000715};
716
717// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700718TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000719 EXPECT_EQ(&network_manager_, allocator().network_manager());
720 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800721 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700722 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000723 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800724 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
725 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700726 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
727 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
728 EXPECT_TRUE(
729 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
730 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000732}
733
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800734// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700735TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800736 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
737 rtc::ADAPTER_TYPE_ETHERNET);
738 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
739 rtc::ADAPTER_TYPE_WIFI);
740 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
741 rtc::ADAPTER_TYPE_CELLULAR);
742 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
743 rtc::ADAPTER_TYPE_VPN);
744 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
745 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700746 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
747 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
748 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800750 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
751 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800752 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700753 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800754 EXPECT_LT(candidate.address().ip(), 0x12345604U);
755 }
756}
757
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700758TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800759 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
760 rtc::ADAPTER_TYPE_ETHERNET);
761 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
762 rtc::ADAPTER_TYPE_WIFI);
763 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
764 rtc::ADAPTER_TYPE_CELLULAR);
765 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
766 rtc::ADAPTER_TYPE_LOOPBACK |
767 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700768 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
769 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
770 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800771 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800772 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
773 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800774 EXPECT_EQ(1U, candidates_.size());
775 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
776}
777
deadbeef1c46a352017-09-27 11:24:05 -0700778// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
779// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
780TEST_F(BasicPortAllocatorTest,
781 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
782 SocketAddress wifi(IPAddress(0x12345600U), 0);
783 SocketAddress cell(IPAddress(0x12345601U), 0);
784 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
785 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
786 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-05-31 18:29:12 -0700787 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
788 cricket::PORTALLOCATOR_DISABLE_RELAY |
789 cricket::PORTALLOCATOR_DISABLE_TCP |
790 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
791 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
792 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800793 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
794 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700795 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700796 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200797 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700798}
honghaiz60347052016-05-31 18:29:12 -0700799
deadbeef1c46a352017-09-27 11:24:05 -0700800// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
801// both "unknown" and cell interfaces are available, only the unknown are used.
802// The unknown interface may be something that ultimately uses Wi-Fi, so we do
803// this to be on the safe side.
804TEST_F(BasicPortAllocatorTest,
805 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
806 SocketAddress cell(IPAddress(0x12345601U), 0);
807 SocketAddress unknown1(IPAddress(0x12345602U), 0);
808 SocketAddress unknown2(IPAddress(0x12345603U), 0);
809 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
810 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
811 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
812 // Disable all but UDP candidates to make the test simpler.
813 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
814 cricket::PORTALLOCATOR_DISABLE_RELAY |
815 cricket::PORTALLOCATOR_DISABLE_TCP |
816 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
817 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700818 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800819 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
820 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700821 // Should only get two candidates, none of which is cell.
honghaiz60347052016-05-31 18:29:12 -0700822 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200823 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
824 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
deadbeef1c46a352017-09-27 11:24:05 -0700825}
honghaiz60347052016-05-31 18:29:12 -0700826
deadbeef1c46a352017-09-27 11:24:05 -0700827// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
828// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
829// interface is used.
830TEST_F(BasicPortAllocatorTest,
831 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
832 SocketAddress wifi(IPAddress(0x12345600U), 0);
833 SocketAddress cellular(IPAddress(0x12345601U), 0);
834 SocketAddress unknown1(IPAddress(0x12345602U), 0);
835 SocketAddress unknown2(IPAddress(0x12345603U), 0);
836 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
837 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
838 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
839 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
840 // Disable all but UDP candidates to make the test simpler.
841 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
842 cricket::PORTALLOCATOR_DISABLE_RELAY |
843 cricket::PORTALLOCATOR_DISABLE_TCP |
844 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
845 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700846 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800847 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
848 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700849 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700850 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200851 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700852}
853
854// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
855// only interface available is cellular, it ends up used anyway. A costly
856// connection is always better than no connection.
857TEST_F(BasicPortAllocatorTest,
858 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
859 SocketAddress cellular(IPAddress(0x12345601U), 0);
860 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
861 // Disable all but UDP candidates to make the test simpler.
862 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
863 cricket::PORTALLOCATOR_DISABLE_RELAY |
864 cricket::PORTALLOCATOR_DISABLE_TCP |
865 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
866 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
867 session_->StartGettingPorts();
868 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
869 kDefaultAllocationTimeout, fake_clock);
870 // Make sure we got the cell candidate.
871 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200872 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
honghaiz60347052016-05-31 18:29:12 -0700873}
874
Yuwei Huangb181f712018-01-22 17:01:28 -0800875// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
876// a WiFi network with link-local IP address and a cellular network, then the
877// cellular candidate will still be gathered.
878TEST_F(BasicPortAllocatorTest,
879 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
880 SocketAddress wifi_link_local("169.254.0.1", 0);
881 SocketAddress cellular(IPAddress(0x12345601U), 0);
882 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
883 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
884
885 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
886 cricket::PORTALLOCATOR_DISABLE_RELAY |
887 cricket::PORTALLOCATOR_DISABLE_TCP |
888 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
889 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
890 session_->StartGettingPorts();
891 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
892 kDefaultAllocationTimeout, fake_clock);
893 // Make sure we got both wifi and cell candidates.
894 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200895 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
896 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
Yuwei Huangb181f712018-01-22 17:01:28 -0800897}
898
899// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
900// a WiFi network with link-local IP address, a WiFi network with a normal IP
901// address and a cellular network, then the cellular candidate will not be
902// gathered.
903TEST_F(BasicPortAllocatorTest,
904 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
905 SocketAddress wifi(IPAddress(0x12345600U), 0);
906 SocketAddress wifi_link_local("169.254.0.1", 0);
907 SocketAddress cellular(IPAddress(0x12345601U), 0);
908 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
909 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
910 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
911
912 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
913 cricket::PORTALLOCATOR_DISABLE_RELAY |
914 cricket::PORTALLOCATOR_DISABLE_TCP |
915 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
916 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
917 session_->StartGettingPorts();
918 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
919 kDefaultAllocationTimeout, fake_clock);
920 // Make sure we got only wifi candidates.
921 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200922 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
923 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
Yuwei Huangb181f712018-01-22 17:01:28 -0800924}
925
deadbeef3427f532017-07-26 16:09:33 -0700926// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
927// to gather candidates.
928TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
929 // Add three IPv6 network interfaces, but tell the allocator to only use two.
930 allocator().set_max_ipv6_networks(2);
931 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
932 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
933 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
934
935 // To simplify the test, only gather UDP host candidates.
936 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
937 PORTALLOCATOR_DISABLE_STUN |
938 PORTALLOCATOR_DISABLE_RELAY);
939
940 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
941 session_->StartGettingPorts();
942 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
943 kDefaultAllocationTimeout, fake_clock);
944 EXPECT_EQ(2U, candidates_.size());
945 // Ensure the expected two interfaces (eth0 and eth1) were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +0200946 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
947 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
deadbeef3427f532017-07-26 16:09:33 -0700948}
949
950// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
951// being used.
952TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
953 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
954 allocator().set_max_ipv6_networks(1);
955 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
956 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
957 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
958 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
959
960 // To simplify the test, only gather UDP host candidates.
961 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
962 PORTALLOCATOR_DISABLE_STUN |
963 PORTALLOCATOR_DISABLE_RELAY);
964
965 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
966 session_->StartGettingPorts();
967 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
968 kDefaultAllocationTimeout, fake_clock);
969 EXPECT_EQ(3U, candidates_.size());
970 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
971 // were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +0200972 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
973 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
974 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
deadbeef3427f532017-07-26 16:09:33 -0700975}
976
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700977// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700978TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800979 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
980 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700981 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
982 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
983 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700984 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800985 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
986 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700987 EXPECT_EQ(1U, candidates_.size());
988}
989
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000990// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700991TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000992 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700993 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000994 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700995 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
996 kDefaultAllocationTimeout, fake_clock);
997 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000998 EXPECT_EQ(4U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200999 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1000 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1001 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1002 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1003 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1004 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1005 EXPECT_TRUE(
1006 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001007}
1008
honghaiz8c404fa2015-09-28 07:59:43 -07001009// Test that when the same network interface is brought down and up, the
1010// port allocator session will restart a new allocation sequence if
1011// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001012TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001013 std::string if_name("test_net0");
1014 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001015 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001016 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001017 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1018 kDefaultAllocationTimeout, fake_clock);
1019 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001020 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001021 candidate_allocation_done_ = false;
1022 candidates_.clear();
1023 ports_.clear();
1024
deadbeef7f1563f2017-09-15 17:40:01 -07001025 // Disable socket creation to simulate the network interface being down. When
1026 // no network interfaces are available, BasicPortAllocator will fall back to
1027 // binding to the "ANY" address, so we need to make sure that fails too.
1028 fss_->set_tcp_sockets_enabled(false);
1029 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -07001030 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001031 SIMULATED_WAIT(false, 1000, fake_clock);
1032 EXPECT_EQ(0U, candidates_.size());
1033 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -07001034
1035 // When the same interfaces are added again, new candidates/ports should be
1036 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -07001037 fss_->set_tcp_sockets_enabled(true);
1038 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -07001039 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001040 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1041 kDefaultAllocationTimeout, fake_clock);
1042 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001043 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001044}
1045
1046// Test that when the same network interface is brought down and up, the
1047// port allocator session will not restart a new allocation sequence if
1048// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001049TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001050 std::string if_name("test_net0");
1051 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001052 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001053 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001054 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1055 kDefaultAllocationTimeout, fake_clock);
1056 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001057 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001058 session_->StopGettingPorts();
1059 candidates_.clear();
1060 ports_.clear();
1061
1062 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001063 // Wait one (simulated) second and then verify no new candidates have
1064 // appeared.
1065 SIMULATED_WAIT(false, 1000, fake_clock);
1066 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001067 EXPECT_EQ(0U, ports_.size());
1068
1069 // When the same interfaces are added again, new candidates/ports should not
1070 // be generated because the session has stopped.
1071 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001072 SIMULATED_WAIT(false, 1000, fake_clock);
1073 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001074 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001075}
1076
deadbeef1c46a352017-09-27 11:24:05 -07001077// Similar to the above tests, but tests a situation when sockets can't be
1078// bound to a network interface, then after a network change event can be.
1079// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1080TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1081 // Only test local ports to simplify test.
1082 ResetWithNoServersOrNat();
1083 // Provide a situation where the interface appears to be available, but
1084 // binding the sockets fails. See bug for description of when this can
1085 // happen.
1086 std::string if_name("test_net0");
1087 AddInterface(kClientAddr, if_name);
1088 fss_->set_tcp_sockets_enabled(false);
1089 fss_->set_udp_sockets_enabled(false);
1090 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1091 session_->StartGettingPorts();
1092 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1093 kDefaultAllocationTimeout, fake_clock);
1094 // Make sure we actually prevented candidates from being gathered (other than
1095 // a single TCP active candidate, since that doesn't require creating a
1096 // socket).
1097 ASSERT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001098 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001099 candidate_allocation_done_ = false;
1100
1101 // Now simulate the interface coming up, with the newfound ability to bind
1102 // sockets.
1103 fss_->set_tcp_sockets_enabled(true);
1104 fss_->set_udp_sockets_enabled(true);
1105 AddInterface(kClientAddr, if_name);
1106 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1107 kDefaultAllocationTimeout, fake_clock);
1108 // Should get UDP and TCP candidate.
1109 ASSERT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001110 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001111 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1112 // We should extend this test to also verify that a server candidate is
1113 // gathered.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001114 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001115}
1116
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001117// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001118TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001119 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001120 allocator_->set_step_delay(kDefaultStepDelay);
1121 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001122 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001123 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001124 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001125 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001126 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001127 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1128 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1129 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1130 EXPECT_TRUE(
1131 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
deadbeef1c5e6d02017-09-15 17:46:56 -07001132 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001133 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001134 EXPECT_EQ(4U, ports_.size());
1135 EXPECT_TRUE(candidate_allocation_done_);
1136 // If we Stop gathering now, we shouldn't get a second "done" callback.
1137 session_->StopGettingPorts();
1138}
1139
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001140TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001141 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001142 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001144 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1145 kDefaultAllocationTimeout, fake_clock);
1146 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001147 // If we Stop gathering now, we shouldn't get a second "done" callback.
1148 session_->StopGettingPorts();
1149
1150 // All ports should have unset send-buffer sizes.
1151 CheckSendBufferSizesOfAllPorts(-1);
1152}
1153
deadbeef7f1563f2017-09-15 17:40:01 -07001154// Tests that we can get callback after StopGetAllPorts when called in the
1155// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001156TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001158 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001160 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1161 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162 EXPECT_EQ(2U, ports_.size());
1163 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001164 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1165 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001166}
1167
1168// Test that we restrict client ports appropriately when a port range is set.
1169// We check the candidates for udp/stun/tcp ports, and the from address
1170// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001171TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 AddInterface(kClientAddr);
1173 // Check that an invalid port range fails.
1174 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1175 // Check that a null port range succeeds.
1176 EXPECT_TRUE(SetPortRange(0, 0));
1177 // Check that a valid port range succeeds.
1178 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001179 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001180 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001181 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1182 kDefaultAllocationTimeout, fake_clock);
1183 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001184 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001185
1186 int num_nonrelay_candidates = 0;
1187 for (const Candidate& candidate : candidates_) {
1188 // Check the port number for the UDP/STUN/TCP port objects.
1189 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001190 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 11:44:17 -07001191 ++num_nonrelay_candidates;
1192 }
1193 }
1194 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 // Check the port number used to connect to the relay server.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001196 EXPECT_TRUE(
1197 CheckPort(relay_server_.GetConnection(0).source(), kMinPort, kMaxPort));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001198}
1199
deadbeefe97389c2016-12-23 01:43:45 -08001200// Test that if we have no network adapters, we bind to the ANY address and
1201// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001202TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001203 // Default config uses GTURN and no NAT, so replace that with the
1204 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1205 ResetWithStunServerAndNat(kStunAddr);
1206 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1207 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1208 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1209 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1210 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1211 // TODO(deadbeef): Fix this.
1212 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001213 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001214 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001215 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1216 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001217 EXPECT_EQ(4U, ports_.size());
1218 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1219 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1220 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1221 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1222 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1223 // The "any" address port should be in the signaled ready ports, but the host
1224 // candidate for it is useless and shouldn't be signaled. So we only have
1225 // STUN/TURN candidates.
1226 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001227 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1228 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 01:43:45 -08001229 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1230 // server.
1231 EXPECT_EQ(2,
1232 CountCandidates(candidates_, "relay", "udp",
1233 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001234}
1235
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001236// Test that when enumeration is disabled, we should not have any ports when
1237// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001238TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001239 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001240 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001241 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001242 // Expect to see no ports and no candidates.
1243 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1244 rtc::IPAddress(), rtc::IPAddress());
1245}
1246
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001247// Test that even with multiple interfaces, the result should still be a single
1248// default private, one STUN and one TURN candidate since we bind to any address
1249// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001250TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001251 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001252 AddInterface(kPrivateAddr);
1253 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001254 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001255 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001256
1257 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1258 // address set and we have no IPv6 STUN server, there should be no IPv6
1259 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001260 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1261 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001262
1263 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1264 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1265 // TURN/UDP candidates.
1266 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001267 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1268 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001269}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001270
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001271// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1272// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001273TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001274 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001275 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001276 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001277 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001278 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1279 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1280 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1281 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001282 kTurnUdpExtAddr.ipaddr());
1283}
1284
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001285// Test that when adapter enumeration is disabled, for endpoints without
1286// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001287TEST_F(BasicPortAllocatorTest,
1288 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001289 ResetWithNoServersOrNat();
1290 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1291 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1292 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001293}
1294
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001295// Test that when adapter enumeration is disabled, with
1296// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1297// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001298TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001299 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1300 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001301 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1302 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001303 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1304 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001305 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001306 rtc::IPAddress(), rtc::IPAddress());
1307}
1308
1309// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001310// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1311// a NAT, there is no local candidate. However, this specified default route
1312// (kClientAddr) which was discovered when sending STUN requests, will become
1313// the srflx addresses.
1314TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315 BasicPortAllocatorTest,
Steve Anton6c38cc72017-11-29 10:25:58 -08001316 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001317 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001318 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001319 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1320 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001321 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1322 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001323 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1324 rtc::IPAddress(), rtc::IPAddress());
1325}
1326
1327// Test that when adapter enumeration is disabled, with
1328// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1329// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001330TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001331 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1332 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001333 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1334 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001335 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1336 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1337 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001338}
1339
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001340// Test that we disable relay over UDP, and only TCP is used when connecting to
1341// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001342TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001343 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001344 AddInterface(kClientAddr);
1345 ResetWithStunServerAndNat(kStunAddr);
1346 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001347 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1348 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1349 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1350 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001351
1352 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001353 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1354 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001355
1356 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1357 // TURN/TCP candidates.
1358 EXPECT_EQ(2U, ports_.size());
1359 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001360 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001361 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1362 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001363 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001364 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001365 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001366}
1367
Erik Språngefdce692015-06-05 09:41:26 +02001368// Disable for asan, see
1369// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1370#if !defined(ADDRESS_SANITIZER)
1371
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001372// Test that we can get OnCandidatesAllocationDone callback when all the ports
1373// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001374TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001375 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001376 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1377 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1378 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001379 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001380 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001381 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001382}
1383
1384// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001385TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001386 AddInterface(kClientAddr);
1387 fss_->set_udp_sockets_enabled(false);
1388 EXPECT_TRUE(CreateSession(1));
1389 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001390 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1391 kDefaultAllocationTimeout, fake_clock);
1392 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001393 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001394 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1395 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1396 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1397 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1398 EXPECT_TRUE(
1399 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001400}
1401
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001402#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001403
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001404// Test that we don't crash or malfunction if we can't create UDP sockets or
1405// listen on TCP sockets. We still give out a local TCP address, since
1406// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001407TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001408 AddInterface(kClientAddr);
1409 fss_->set_udp_sockets_enabled(false);
1410 fss_->set_tcp_listen_enabled(false);
1411 EXPECT_TRUE(CreateSession(1));
1412 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001413 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1414 kDefaultAllocationTimeout, fake_clock);
1415 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001417 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1418 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1419 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1420 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1421 EXPECT_TRUE(
1422 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001423}
1424
1425// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001426// TODO(deadbeef): Find a way to exit early here.
1427TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001428 AddInterface(kClientAddr);
1429 fss_->set_tcp_sockets_enabled(false);
1430 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001431 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001433 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001434 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001435 // In case of Relay, ports creation will succeed but sockets will fail.
1436 // There is no error reporting from RelayEntry to handle this failure.
1437}
1438
1439// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001440TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1442 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001443 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001445 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1446 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001447 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001448 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1449 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001451 // will be tried after about 3 seconds.
1452 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001453 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001454 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1455 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1456 EXPECT_TRUE(
1457 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1458 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001459 // We wait at least for a full STUN timeout, which
1460 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1461 // already passed (see above), we wait 3 seconds less than that.
1462 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1463 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001464}
1465
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001466TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001467 AddInterface(kClientAddr);
1468 AddInterface(kClientAddr2);
1469 // Allocating only host UDP ports. This is done purely for testing
1470 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001471 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1472 PORTALLOCATOR_DISABLE_RELAY);
1473 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001474 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001475 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1476 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001477 ASSERT_EQ(2U, candidates_.size());
1478 EXPECT_EQ(2U, ports_.size());
1479 // Candidates priorities should be different.
1480 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1481}
1482
1483// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001484TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001485 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001486 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001487 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001488 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1489 kDefaultAllocationTimeout, fake_clock);
1490 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001491 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001492 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001493}
1494
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001495// Test that the allocator session uses the candidate filter it's created with,
1496// rather than the filter of its parent allocator.
1497// The filter of the allocator should only affect the next gathering phase,
1498// according to JSEP, which means the *next* allocator session returned.
1499TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1500 AddInterface(kClientAddr);
1501 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1502 // Set candidate filter *after* creating the session. Should have no effect.
1503 allocator().set_candidate_filter(CF_RELAY);
1504 session_->StartGettingPorts();
1505 // 7 candidates and 4 ports is what we would normally get (see the
1506 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001507 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1508 kDefaultAllocationTimeout, fake_clock);
1509 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001510 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001511}
1512
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001513// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1514// This test also verifies that when the allocator is only allowed to use
1515// relay (i.e. IceTransportsType is relay), the raddr is an empty
1516// address with the correct family. This is to prevent any local
1517// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001518TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001519 AddInterface(kClientAddr);
1520 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001521 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001522 allocator().set_candidate_filter(CF_RELAY);
1523 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001524 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001525 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1526 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001527 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1528 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001529
1530 EXPECT_EQ(1U, candidates_.size());
1531 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001532 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1533 EXPECT_EQ(
1534 candidates_[0].related_address(),
1535 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001536}
1537
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001538TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001539 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001540 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1541 allocator().set_candidate_filter(CF_HOST);
1542 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001543 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001544 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1545 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001546 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1547 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001548 for (const Candidate& candidate : candidates_) {
1549 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001550 }
1551}
1552
1553// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001554TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001555 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001556 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001557
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001558 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1559 allocator().set_candidate_filter(CF_REFLEXIVE);
1560 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001561 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001562 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1563 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001564 // Host is behind NAT, no private address will be exposed. Hence only UDP
1565 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001566 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1567 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001568 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1569 EXPECT_EQ(
1570 candidates_[0].related_address(),
1571 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001572}
1573
1574// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001575TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001576 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001577 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1578 allocator().set_candidate_filter(CF_REFLEXIVE);
1579 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001580 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001581 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1582 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001583 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001584 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001585 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001586 for (const Candidate& candidate : candidates_) {
1587 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001588 }
1589}
1590
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001591// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001592TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001593 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001594 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001595 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001596 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1597 kDefaultAllocationTimeout, fake_clock);
1598 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001599 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1600 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1601 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001602 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001603 for (const Candidate& candidate : candidates_) {
1604 EXPECT_EQ(kIceUfrag0, candidate.username());
1605 EXPECT_EQ(kIcePwd0, candidate.password());
1606 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001607}
1608
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001609// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1610// is allocated for udp and stun. Also verify there is only one candidate
1611// (local) if stun candidate is same as local candidate, which will be the case
1612// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001613TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001614 AddInterface(kClientAddr);
1615 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001616 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1617 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001618 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001619 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1620 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001621 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001622 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001623 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1624 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001625}
1626
1627// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1628// is allocated for udp and stun. In this test we should expect both stun and
1629// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001630TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001631 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001632 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001633
1634 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001635 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1636 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001637 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001638 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1639 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001640 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001641 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1642 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1643 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001644 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1645 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001646 EXPECT_EQ(3U, candidates_.size());
1647}
1648
deadbeefc5d0d952015-07-16 10:22:21 -07001649// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001650TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001651 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001652 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001653 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001654
1655 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1656
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001657 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001659 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1660 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001661
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001662 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001663 session_->StartGettingPorts();
1664
deadbeef7f1563f2017-09-15 17:40:01 -07001665 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1666 kDefaultAllocationTimeout, fake_clock);
1667 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001669 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1670 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1671 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1672 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1673 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001674}
1675
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001676// Test that if prune_turn_ports is set, TCP TURN port will not be used
1677// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1678TEST_F(BasicPortAllocatorTest,
1679 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1680 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1681 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1682 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001683
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001684 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001685}
1686
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001687// Test that if prune_turn_ports is set, TCP TURN port will not be used
1688// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1689TEST_F(BasicPortAllocatorTest,
1690 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1691 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1692 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1693 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001694
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001695 TestUdpTurnPortPrunesTcpTurnPort();
1696}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001697
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001698// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1699// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1700TEST_F(BasicPortAllocatorTest,
1701 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1702 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1703 // first.
1704 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1705 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001706
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001707 TestIPv6TurnPortPrunesIPv4TurnPort();
1708}
1709
1710// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1711// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1712TEST_F(BasicPortAllocatorTest,
1713 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1714 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1715 // first.
1716 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1717 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1718
1719 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001720}
1721
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001722// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001723// will has its own set of TurnPorts based on their priorities, in the default
1724// case where no transit delay is set.
1725TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1726 TestEachInterfaceHasItsOwnTurnPorts();
1727}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001728
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001729// Tests that if prune_turn_ports is set, each network interface
1730// will has its own set of TurnPorts based on their priorities, given that
1731// IPv4/TCP TURN port becomes ready first.
1732TEST_F(BasicPortAllocatorTest,
1733 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1734 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1735 // becomes ready last.
1736 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1737 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1738 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1739 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001740
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001741 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001742}
1743
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001744// Testing DNS resolve for the TURN server, this will test AllocationSequence
1745// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001746// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1747// appears that it doesn't currently because of the DNS look up not
1748// using the fake clock.
1749TEST_F(BasicPortAllocatorTestWithRealClock,
1750 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001751 // This test relies on a real query for "localhost", so it won't work on an
1752 // IPv6-only machine.
1753 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001754 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001755 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001756 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001757 allocator_.reset(new BasicPortAllocator(&network_manager_));
1758 RelayServerConfig turn_server(RELAY_TURN);
1759 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001760 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001761 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001762 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001763 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001764
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001765 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001766 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001767 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1768 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001769
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001770 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001771 session_->StartGettingPorts();
1772
1773 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1774}
1775
1776// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1777// is allocated for udp/stun/turn. In this test we should expect all local,
1778// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001779TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001780 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001781 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001782
1783 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1784
1785 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001786 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1787 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001788
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001789 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001790 session_->StartGettingPorts();
1791
deadbeef7f1563f2017-09-15 17:40:01 -07001792 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1793 kDefaultAllocationTimeout, fake_clock);
1794 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001795 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001796 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1797 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1798 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1799 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1800 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001801 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1802 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001803 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001804 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1805 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001806 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1807 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1808}
1809
1810// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1811// server is also used as the STUN server, we should get 'local', 'stun', and
1812// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001813TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001814 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001815 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001816 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001817 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1818
1819 // Must set the step delay to 0 to make sure the relay allocation phase is
1820 // started before the STUN candidates are obtained, so that the STUN binding
1821 // response is processed when both StunPort and TurnPort exist to reproduce
1822 // webrtc issue 3537.
1823 allocator_->set_step_delay(0);
1824 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001825 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1826 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001827
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001828 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001829 session_->StartGettingPorts();
1830
deadbeef7f1563f2017-09-15 17:40:01 -07001831 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1832 kDefaultAllocationTimeout, fake_clock);
1833 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001834 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001835 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001836 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1837 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1838 &stun_candidate));
1839 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1840 candidates_, "relay", "udp",
1841 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1842 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001843
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001844 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001845 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1846 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001847 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1848 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1849}
1850
deadbeefc5d0d952015-07-16 10:22:21 -07001851// Test that when only a TCP TURN server is available, we do NOT use it as
1852// a UDP STUN server, as this could leak our IP address. Thus we should only
1853// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001854TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001855 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001856 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001857 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001858 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1859
1860 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001861 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1862 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001863
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001864 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001865 session_->StartGettingPorts();
1866
deadbeef7f1563f2017-09-15 17:40:01 -07001867 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1868 kDefaultAllocationTimeout, fake_clock);
1869 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001870 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001871 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1872 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1873 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001874 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1875 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1876}
1877
1878// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1879// TURN server is used as the STUN server and we get 'local', 'stun', and
1880// 'relay' candidates.
1881// TODO(deadbeef): Remove this test when support for non-shared socket mode
1882// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001883TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001884 AddInterface(kClientAddr);
1885 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001886 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001887 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1888
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001889 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001890
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001891 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001892 session_->StartGettingPorts();
1893
deadbeef7f1563f2017-09-15 17:40:01 -07001894 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1895 kDefaultAllocationTimeout, fake_clock);
1896 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001897 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001898 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001899 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001900 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1901 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1902 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001903 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001904 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1905 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1906 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001907 // Not using shared socket, so the STUN request's server reflexive address
1908 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001909 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001910
deadbeefc5d0d952015-07-16 10:22:21 -07001911 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1912 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1913 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1914}
1915
1916// Test that even when both a STUN and TURN server are configured, the TURN
1917// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001918TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001919 AddInterface(kClientAddr);
1920 // Configure with STUN server but destroy it, so we can ensure that it's
1921 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001922 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001923 stun_server_.reset();
1924 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1925
1926 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001927 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1928 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001929
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001930 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001931 session_->StartGettingPorts();
1932
pthatcher1749bc32017-02-08 13:18:00 -08001933 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1934 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001935 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001936 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001937 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1938 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1939 &stun_candidate));
1940 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1941 candidates_, "relay", "udp",
1942 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1943 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001944
1945 // Don't bother waiting for STUN timeout, since we already verified
1946 // that we got a STUN candidate from the TURN server.
1947}
1948
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001949// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1950// and fail to generate STUN candidate, local UDP candidate is generated
1951// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001952TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001953 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1954 PORTALLOCATOR_DISABLE_TCP |
1955 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001956 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1957 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001958 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001959 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001960 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1961 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001962 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001963 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001964 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001965 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1966 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001967 EXPECT_EQ(1U, candidates_.size());
1968}
1969
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001970// Test that when the NetworkManager doesn't have permission to enumerate
1971// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1972// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001973TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001974 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1975 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001976 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001977 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001978 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1979 PORTALLOCATOR_DISABLE_TCP |
1980 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1981 EXPECT_EQ(0U,
1982 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1983 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1984 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001985 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001986 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1987 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001988 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001989 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001990 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001991}
1992
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001993// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001994TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001995 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1996 PORTALLOCATOR_ENABLE_IPV6 |
1997 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001998 AddInterface(kClientIPv6Addr);
1999 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002000 allocator_->set_step_delay(kMinimumStepDelay);
2001 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002002 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002003 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002004 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002005 EXPECT_EQ(4U, ports_.size());
2006 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002007 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2008 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2009 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2010 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002011}
honghaiz98db68f2015-09-29 07:58:17 -07002012
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002013TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002014 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002015 allocator_->set_step_delay(kDefaultStepDelay);
2016 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002017 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002018 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002019 EXPECT_EQ(2U, ports_.size());
2020 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002021 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002022
2023 // After stopping getting ports, adding a new interface will not start
2024 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002025 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002026 candidates_.clear();
2027 ports_.clear();
2028 candidate_allocation_done_ = false;
2029 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002030 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002031 EXPECT_EQ(0U, candidates_.size());
2032 EXPECT_EQ(0U, ports_.size());
2033}
2034
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002035TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002036 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002037 allocator_->set_step_delay(kDefaultStepDelay);
2038 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002039 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002040 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002041 EXPECT_EQ(2U, ports_.size());
2042 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002043 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002044
2045 // After clearing getting ports, adding a new interface will start getting
2046 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002047 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002048 candidates_.clear();
2049 ports_.clear();
2050 candidate_allocation_done_ = false;
2051 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002052 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002053 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002054 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2055 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002056}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002057
2058// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2059// a pooled session is taken out of the pool.
2060TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2061 AddInterface(kClientAddr);
2062 int pool_size = 1;
2063 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002064 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002065 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002066 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002067 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2068 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002069 // Expect that when TakePooledSession is called,
2070 // UpdateTransportInformationInternal will be called and the
2071 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2072 // candidates.
2073 session_ =
2074 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2075 ASSERT_NE(nullptr, session_.get());
2076 auto ready_ports = session_->ReadyPorts();
2077 auto candidates = session_->ReadyCandidates();
2078 EXPECT_FALSE(ready_ports.empty());
2079 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002080 for (const PortInterface* port_interface : ready_ports) {
2081 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002082 EXPECT_EQ(kContentName, port->content_name());
2083 EXPECT_EQ(1, port->component());
2084 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2085 EXPECT_EQ(kIcePwd0, port->password());
2086 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002087 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002088 EXPECT_EQ(1, candidate.component());
2089 EXPECT_EQ(kIceUfrag0, candidate.username());
2090 EXPECT_EQ(kIcePwd0, candidate.password());
2091 }
2092}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002093
2094// Test that a new candidate filter takes effect even on already-gathered
2095// candidates.
2096TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2097 AddInterface(kClientAddr);
2098 int pool_size = 1;
2099 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002100 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002101 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2102 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002103 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2104 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002105 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2106 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2107 allocator_->set_candidate_filter(CF_RELAY);
2108 // Assume that when TakePooledSession is called, the candidate filter will be
2109 // applied to the pooled session. This is tested by PortAllocatorTest.
2110 session_ =
2111 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2112 ASSERT_NE(nullptr, session_.get());
2113 auto candidates = session_->ReadyCandidates();
2114 auto ports = session_->ReadyPorts();
2115 // Sanity check that the number of candidates and ports decreased.
2116 EXPECT_GT(initial_candidates_size, candidates.size());
2117 EXPECT_GT(initial_ports_size, ports.size());
2118 for (const PortInterface* port : ports) {
2119 // Expect only relay ports.
2120 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2121 }
2122 for (const Candidate& candidate : candidates) {
2123 // Expect only relay candidates now that the filter is applied.
2124 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2125 // Expect that the raddr is emptied due to the CF_RELAY filter.
2126 EXPECT_EQ(candidate.related_address(),
2127 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2128 }
2129}
2130
Qingsi Wang4ff54432018-03-01 18:25:20 -08002131TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2132 const int pool_size = 1;
2133 const int expected_stun_keepalive_interval = 123;
2134 AddInterface(kClientAddr);
2135 allocator_->SetConfiguration(allocator_->stun_servers(),
2136 allocator_->turn_servers(), pool_size, false,
2137 nullptr, expected_stun_keepalive_interval);
2138 auto* pooled_session = allocator_->GetPooledSession();
2139 ASSERT_NE(nullptr, pooled_session);
2140 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2141 kDefaultAllocationTimeout, fake_clock);
2142 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2143 expected_stun_keepalive_interval);
2144}
2145
2146TEST_F(BasicPortAllocatorTest,
2147 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2148 const int pool_size = 1;
2149 AddInterface(kClientAddr);
2150 allocator_->SetConfiguration(allocator_->stun_servers(),
2151 allocator_->turn_servers(), pool_size, false,
2152 nullptr, 123 /* stun keepalive interval */);
2153 auto* pooled_session = allocator_->GetPooledSession();
2154 ASSERT_NE(nullptr, pooled_session);
2155 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2156 kDefaultAllocationTimeout, fake_clock);
2157 const int expected_stun_keepalive_interval = 321;
2158 allocator_->SetConfiguration(allocator_->stun_servers(),
2159 allocator_->turn_servers(), pool_size, false,
2160 nullptr, expected_stun_keepalive_interval);
2161 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2162 expected_stun_keepalive_interval);
2163}
2164
2165TEST_F(BasicPortAllocatorTest,
2166 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2167 const int pool_size = 1;
2168 const int expected_stun_keepalive_interval = 123;
2169 AddInterface(kClientAddr);
2170 allocator_->set_flags(allocator().flags() |
2171 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2172 allocator_->SetConfiguration(allocator_->stun_servers(),
2173 allocator_->turn_servers(), pool_size, false,
2174 nullptr, expected_stun_keepalive_interval);
2175 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2176 session_->StartGettingPorts();
2177 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2178 kDefaultAllocationTimeout, fake_clock);
2179 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2180 expected_stun_keepalive_interval);
2181}
2182
2183TEST_F(BasicPortAllocatorTest,
2184 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2185 const int pool_size = 1;
2186 const int expected_stun_keepalive_interval = 123;
2187 AddInterface(kClientAddr);
2188 allocator_->set_flags(allocator().flags() &
2189 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2190 allocator_->SetConfiguration(allocator_->stun_servers(),
2191 allocator_->turn_servers(), pool_size, false,
2192 nullptr, expected_stun_keepalive_interval);
2193 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2194 session_->StartGettingPorts();
2195 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2196 kDefaultAllocationTimeout, fake_clock);
2197 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2198 expected_stun_keepalive_interval);
2199}
2200
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002201} // namespace cricket