blob: 6ed13b57984f07c54146e03b45598b836d843c8c [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/basicpacketsocketfactory.h"
15#include "p2p/base/p2pconstants.h"
16#include "p2p/base/p2ptransportchannel.h"
Qingsi Wang4ff54432018-03-01 18:25:20 -080017#include "p2p/base/stunport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/testrelayserver.h"
19#include "p2p/base/teststunserver.h"
20#include "p2p/base/testturnserver.h"
21#include "p2p/client/basicportallocator.h"
22#include "rtc_base/fakeclock.h"
23#include "rtc_base/fakenetwork.h"
24#include "rtc_base/firewallsocketserver.h"
25#include "rtc_base/gunit.h"
26#include "rtc_base/helpers.h"
27#include "rtc_base/ipaddress.h"
28#include "rtc_base/logging.h"
29#include "rtc_base/natserver.h"
30#include "rtc_base/natsocketfactory.h"
31#include "rtc_base/nethelpers.h"
32#include "rtc_base/network.h"
33#include "rtc_base/socketaddress.h"
34#include "rtc_base/ssladapter.h"
35#include "rtc_base/thread.h"
36#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080038using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039using rtc::SocketAddress;
40using rtc::Thread;
41
Mirko Bonadei675513b2017-11-09 11:09:25 +010042#define MAYBE_SKIP_IPV4 \
43 if (!rtc::HasIPv4Enabled()) { \
44 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
45 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070046 }
47
deadbeefe97389c2016-12-23 01:43:45 -080048static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070050static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070051static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000053static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070054static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
55 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070056static const SocketAddress kClientIPv6Addr2(
57 "2401:fa00:4:2000:be30:5bff:fee5:c3",
58 0);
deadbeef3427f532017-07-26 16:09:33 -070059static const SocketAddress kClientIPv6Addr3(
60 "2401:fa00:4:3000:be30:5bff:fee5:c3",
61 0);
deadbeefc5d0d952015-07-16 10:22:21 -070062static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
63static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
65static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
66static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
67static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
68static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
69static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
70static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
71static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
72static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070073static const SocketAddress kTurnUdpIntIPv6Addr(
74 "2402:fb00:4:1000:be30:5bff:fee5:c3",
75 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070077static const SocketAddress kTurnTcpIntIPv6Addr(
78 "2402:fb00:4:2000:be30:5bff:fee5:c3",
79 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
81
82// Minimum and maximum port for port range tests.
83static const int kMinPort = 10000;
84static const int kMaxPort = 10099;
85
86// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070087static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088// Based on ICE_PWD_LENGTH
89static const char kIcePwd0[] = "TESTICEPWD00000000000000";
90
91static const char kContentName[] = "test content";
92
Honghai Zhangc67e0f52016-09-19 16:57:37 -070093static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094static const char kTurnUsername[] = "test";
95static const char kTurnPassword[] = "test";
96
pthatcher94a2f212017-02-08 14:42:22 -080097// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070098// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080099static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -0700100
Qingsi Wang4ff54432018-03-01 18:25:20 -0800101namespace {
102
103void CheckStunKeepaliveIntervalOfAllReadyPorts(
104 const cricket::PortAllocatorSession* allocator_session,
105 int expected) {
106 auto ready_ports = allocator_session->ReadyPorts();
107 for (const auto* port : ready_ports) {
108 if (port->Type() == cricket::STUN_PORT_TYPE ||
109 (port->Type() == cricket::LOCAL_PORT_TYPE &&
110 port->GetProtocol() == cricket::PROTO_UDP)) {
111 EXPECT_EQ(
112 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
113 expected);
114 }
115 }
116}
117
118} // namespace
119
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120namespace cricket {
121
122// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700123std::ostream& operator<<(std::ostream& os,
124 const std::vector<Candidate>& candidates) {
125 os << '[';
126 bool first = true;
127 for (const Candidate& c : candidates) {
128 if (!first) {
129 os << ", ";
130 }
131 os << c.ToString();
132 first = false;
Steve Anton6c38cc72017-11-29 10:25:58 -0800133 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700134 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 return os;
136}
137
pthatcher1749bc32017-02-08 13:18:00 -0800138class BasicPortAllocatorTestBase : public testing::Test,
139 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 public:
pthatcher1749bc32017-02-08 13:18:00 -0800141 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700142 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700144 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800145 // Note that the NAT is not used by default. ResetWithStunServerAndNat
146 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700147 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700148 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700149 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700150 relay_server_(Thread::Current(),
151 kRelayUdpIntAddr,
152 kRelayUdpExtAddr,
153 kRelayTcpIntAddr,
154 kRelayTcpExtAddr,
155 kRelaySslTcpIntAddr,
156 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
158 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 stun_servers.insert(kStunAddr);
161 // Passing the addresses of GTURN servers will enable GTURN in
162 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800163 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
164 // configuration should be blank by default (preferred), or it should use
165 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700166 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
167 kRelayUdpIntAddr, kRelayTcpIntAddr,
168 kRelaySslTcpIntAddr));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700169 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700170 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 }
172
173 void AddInterface(const SocketAddress& addr) {
174 network_manager_.AddInterface(addr);
175 }
honghaiz8c404fa2015-09-28 07:59:43 -0700176 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
177 network_manager_.AddInterface(addr, if_name);
178 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800179 void AddInterface(const SocketAddress& addr,
180 const std::string& if_name,
181 rtc::AdapterType type) {
182 network_manager_.AddInterface(addr, if_name, type);
183 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800184 // The default route is the public address that STUN server will observe when
185 // the endpoint is sitting on the public internet and the local port is bound
186 // to the "any" address. This may be different from the default local address
187 // which the endpoint observes. This can occur if the route to the public
188 // endpoint like 8.8.8.8 (specified as the default local address) is
189 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700190 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
191 AddInterface(addr);
192 // When a binding comes from the any address, the |addr| will be used as the
193 // srflx address.
194 vss_->SetDefaultRoute(addr.ipaddr());
195 }
honghaiz8c404fa2015-09-28 07:59:43 -0700196 void RemoveInterface(const SocketAddress& addr) {
197 network_manager_.RemoveInterface(addr);
198 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 bool SetPortRange(int min_port, int max_port) {
200 return allocator_->SetPortRange(min_port, max_port);
201 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700202 // Endpoint is on the public network. No STUN or TURN.
203 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700204 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700205 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700206 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700207 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700208 // Endpoint is behind a NAT, with STUN specified.
209 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
210 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700211 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700212 // Endpoint is on the public network, with STUN specified.
213 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
214 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700216 // Endpoint is on the public network, with TURN specified.
217 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
218 const rtc::SocketAddress& tcp_turn) {
219 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 AddTurnServers(udp_turn, tcp_turn);
221 }
222
223 void AddTurnServers(const rtc::SocketAddress& udp_turn,
224 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 RelayServerConfig turn_server(RELAY_TURN);
226 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800227 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228
229 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800230 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 }
232 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800233 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 }
deadbeef653b8e02015-11-11 12:55:10 -0800235 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 }
237
238 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700239 session_ = CreateSession("session", component);
240 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700242 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000243 return true;
244 }
245
246 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700247 session_ = CreateSession("session", content_name, component);
248 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700250 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 return true;
252 }
253
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700254 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
255 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000256 return CreateSession(sid, kContentName, component);
257 }
258
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700259 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700260 const std::string& sid,
261 const std::string& content_name,
262 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000263 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
264 }
265
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700266 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700267 const std::string& sid,
268 const std::string& content_name,
269 int component,
270 const std::string& ice_ufrag,
271 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700272 std::unique_ptr<PortAllocatorSession> session =
273 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800275 &BasicPortAllocatorTestBase::OnPortReady);
276 session->SignalPortsPruned.connect(
277 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700278 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800279 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700280 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800281 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700282 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800283 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000284 return session;
285 }
286
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700287 // Return true if the addresses are the same, or the port is 0 in |pattern|
288 // (acting as a wildcard) and the IPs are the same.
289 // Even with a wildcard port, the port of the address should be nonzero if
290 // the IP is nonzero.
291 static bool AddressMatch(const SocketAddress& address,
292 const SocketAddress& pattern) {
293 return address.ipaddr() == pattern.ipaddr() &&
294 ((pattern.port() == 0 &&
295 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
296 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000297 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700298
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700299 // Returns the number of ports that have matching type, protocol and
300 // address.
301 static int CountPorts(const std::vector<PortInterface*>& ports,
302 const std::string& type,
303 ProtocolType protocol,
304 const SocketAddress& client_addr) {
305 return std::count_if(
306 ports.begin(), ports.end(),
307 [type, protocol, client_addr](PortInterface* port) {
308 return port->Type() == type && port->GetProtocol() == protocol &&
309 port->Network()->GetBestIP() == client_addr.ipaddr();
310 });
311 }
312
deadbeefe97389c2016-12-23 01:43:45 -0800313 static int CountCandidates(const std::vector<Candidate>& candidates,
314 const std::string& type,
315 const std::string& proto,
316 const SocketAddress& addr) {
317 return std::count_if(candidates.begin(), candidates.end(),
318 [type, proto, addr](const Candidate& c) {
319 return c.type() == type && c.protocol() == proto &&
320 AddressMatch(c.address(), addr);
321 });
322 }
323
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700324 // Find a candidate and return it.
325 static bool FindCandidate(const std::vector<Candidate>& candidates,
326 const std::string& type,
327 const std::string& proto,
328 const SocketAddress& addr,
329 Candidate* found) {
330 auto it = std::find_if(candidates.begin(), candidates.end(),
331 [type, proto, addr](const Candidate& c) {
332 return c.type() == type && c.protocol() == proto &&
333 AddressMatch(c.address(), addr);
334 });
335 if (it != candidates.end() && found) {
336 *found = *it;
337 }
338 return it != candidates.end();
339 }
340
341 // Convenience method to call FindCandidate with no return.
342 static bool HasCandidate(const std::vector<Candidate>& candidates,
343 const std::string& type,
344 const std::string& proto,
345 const SocketAddress& addr) {
346 return FindCandidate(candidates, type, proto, addr, nullptr);
347 }
348
349 // Version of HasCandidate that also takes a related address.
350 static bool HasCandidateWithRelatedAddr(
351 const std::vector<Candidate>& candidates,
352 const std::string& type,
353 const std::string& proto,
354 const SocketAddress& addr,
355 const SocketAddress& related_addr) {
356 auto it =
357 std::find_if(candidates.begin(), candidates.end(),
358 [type, proto, addr, related_addr](const Candidate& c) {
359 return c.type() == type && c.protocol() == proto &&
360 AddressMatch(c.address(), addr) &&
361 AddressMatch(c.related_address(), related_addr);
362 });
363 return it != candidates.end();
364 }
365
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700367 int min_port,
368 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 return (addr.port() >= min_port && addr.port() <= max_port);
370 }
371
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700372 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373 // We should only get this callback once, except in the mux test where
374 // we have multiple port allocation sessions.
375 if (session == session_.get()) {
376 ASSERT_FALSE(candidate_allocation_done_);
377 candidate_allocation_done_ = true;
378 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700379 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380 }
381
382 // Check if all ports allocated have send-buffer size |expected|. If
383 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
384 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700385 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 for (it = ports_.begin(); it < ports_.end(); ++it) {
387 int send_buffer_size;
388 if (expected == -1) {
389 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000391 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700392 EXPECT_EQ(0,
393 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 ASSERT_EQ(expected, send_buffer_size);
395 }
396 }
397 }
398
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700399 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
400
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700402 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700404 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100405 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000406 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700407 // Make sure the new port is added to ReadyPorts.
408 auto ready_ports = ses->ReadyPorts();
409 EXPECT_NE(ready_ports.end(),
410 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700412 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700413 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100414 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700415 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700416 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700417 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700418 new_end = std::remove(ports_.begin(), new_end, port);
419 // Make sure the pruned port is not in ReadyPorts.
420 EXPECT_EQ(ready_ports.end(),
421 std::find(ready_ports.begin(), ready_ports.end(), port));
422 }
423 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700424 }
425
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700426 void OnCandidatesReady(PortAllocatorSession* ses,
427 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700428 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100429 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700430 // Sanity check that the ICE component is set.
431 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
432 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000433 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700434 // Make sure the new candidates are added to Candidates.
435 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700436 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700437 EXPECT_NE(
438 ses_candidates.end(),
439 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
440 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000441 }
442
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700443 void OnCandidatesRemoved(PortAllocatorSession* session,
444 const std::vector<Candidate>& removed_candidates) {
445 auto new_end = std::remove_if(
446 candidates_.begin(), candidates_.end(),
447 [removed_candidates](Candidate& candidate) {
448 for (const Candidate& removed_candidate : removed_candidates) {
449 if (candidate.MatchesForRemoval(removed_candidate)) {
450 return true;
451 }
452 }
453 return false;
454 });
455 candidates_.erase(new_end, candidates_.end());
456 }
457
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700458 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800459 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700460 RelayServerConfig server_config = allocator_->turn_servers()[i];
461 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000462 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700463 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000464 if (proto_addr.address == relay_port->address &&
465 proto_addr.proto == relay_port->proto)
466 return true;
467 }
468 }
469 return false;
470 }
471
Guo-wei Shieh11477022015-08-15 09:28:41 -0700472 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
473 bool with_nat) {
474 if (with_nat) {
475 nat_server_.reset(new rtc::NATServer(
476 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
477 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
478 } else {
479 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
480 }
481
482 ServerAddresses stun_servers;
483 if (!stun_server.IsNil()) {
484 stun_servers.insert(stun_server);
485 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700486 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700487 &network_manager_, nat_socket_factory_.get(), stun_servers));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700488 allocator_->Initialize();
489 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700490 }
491
pthatcher1749bc32017-02-08 13:18:00 -0800492 std::unique_ptr<rtc::VirtualSocketServer> vss_;
493 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700494 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800495 std::unique_ptr<rtc::NATServer> nat_server_;
496 rtc::NATSocketFactory nat_factory_;
497 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
498 std::unique_ptr<TestStunServer> stun_server_;
499 TestRelayServer relay_server_;
500 TestTurnServer turn_server_;
501 rtc::FakeNetworkManager network_manager_;
502 std::unique_ptr<BasicPortAllocator> allocator_;
503 std::unique_ptr<PortAllocatorSession> session_;
504 std::vector<PortInterface*> ports_;
505 std::vector<Candidate> candidates_;
506 bool candidate_allocation_done_;
507};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700508
pthatcher1749bc32017-02-08 13:18:00 -0800509class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
510};
511
512class FakeClockBase {
513 public:
514 rtc::ScopedFakeClock fake_clock;
515};
516
517class BasicPortAllocatorTest : public FakeClockBase,
518 public BasicPortAllocatorTestBase {
519 public:
520 // This function starts the port/address gathering and check the existence of
521 // candidates as specified. When |expect_stun_candidate| is true,
522 // |stun_candidate_addr| carries the expected reflective address, which is
523 // also the related address for TURN candidate if it is expected. Otherwise,
524 // it should be ignore.
525 void CheckDisableAdapterEnumeration(
526 uint32_t total_ports,
527 const rtc::IPAddress& host_candidate_addr,
528 const rtc::IPAddress& stun_candidate_addr,
529 const rtc::IPAddress& relay_candidate_udp_transport_addr,
530 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
531 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
532 rtc::IPAddress());
533 if (!session_) {
534 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
535 }
536 session_->set_flags(session_->flags() |
537 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
538 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
539 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700540 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800541 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
542 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700543
pthatcher1749bc32017-02-08 13:18:00 -0800544 uint32_t total_candidates = 0;
545 if (!host_candidate_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200546 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
547 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800548 ++total_candidates;
549 }
550 if (!stun_candidate_addr.IsNil()) {
551 rtc::SocketAddress related_address(host_candidate_addr, 0);
552 if (host_candidate_addr.IsNil()) {
553 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
554 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200555 EXPECT_TRUE(HasCandidateWithRelatedAddr(
556 candidates_, "stun", "udp",
557 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
pthatcher1749bc32017-02-08 13:18:00 -0800558 ++total_candidates;
559 }
560 if (!relay_candidate_udp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200561 EXPECT_TRUE(HasCandidateWithRelatedAddr(
562 candidates_, "relay", "udp",
563 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
564 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800565 ++total_candidates;
566 }
567 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200568 EXPECT_TRUE(HasCandidateWithRelatedAddr(
569 candidates_, "relay", "udp",
570 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
571 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800572 ++total_candidates;
573 }
574
575 EXPECT_EQ(total_candidates, candidates_.size());
576 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700577 }
578
579 void TestIPv6TurnPortPrunesIPv4TurnPort() {
580 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
581 // Add two IP addresses on the same interface.
582 AddInterface(kClientAddr, "net1");
583 AddInterface(kClientIPv6Addr, "net1");
584 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700585 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700586 allocator_->SetConfiguration(allocator_->stun_servers(),
587 allocator_->turn_servers(), 0, true);
588 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
589 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
590
591 allocator_->set_step_delay(kMinimumStepDelay);
592 allocator_->set_flags(
593 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
594 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
595
596 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
597 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800598 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
599 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700600 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
601 EXPECT_EQ(3U, session_->ReadyPorts().size());
602 EXPECT_EQ(3U, ports_.size());
603 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
604 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
605 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
606 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
607
608 // Now that we remove candidates when a TURN port is pruned, there will be
609 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
610 EXPECT_EQ(3U, candidates_.size());
611 const std::vector<Candidate>& ready_candidates =
612 session_->ReadyCandidates();
613 EXPECT_EQ(3U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200614 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
615 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
616 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700617 }
618
pthatcher1749bc32017-02-08 13:18:00 -0800619 void TestUdpTurnPortPrunesTcpTurnPort() {
620 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
621 AddInterface(kClientAddr);
622 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700623 allocator_->Initialize();
pthatcher1749bc32017-02-08 13:18:00 -0800624 allocator_->SetConfiguration(allocator_->stun_servers(),
625 allocator_->turn_servers(), 0, true);
626 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
627 allocator_->set_step_delay(kMinimumStepDelay);
628 allocator_->set_flags(allocator().flags() |
629 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
630 PORTALLOCATOR_DISABLE_TCP);
631
632 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
633 session_->StartGettingPorts();
634 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
635 kDefaultAllocationTimeout, fake_clock);
636 // Only 2 ports (one STUN and one TURN) are actually being used.
637 EXPECT_EQ(2U, session_->ReadyPorts().size());
638 // We have verified that each port, when it is added to |ports_|, it is
639 // found in |ready_ports|, and when it is pruned, it is not found in
640 // |ready_ports|, so we only need to verify the content in one of them.
641 EXPECT_EQ(2U, ports_.size());
642 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
643 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
644 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
645
646 // Now that we remove candidates when a TURN port is pruned, |candidates_|
647 // should only contains two candidates regardless whether the TCP TURN port
648 // is created before or after the UDP turn port.
649 EXPECT_EQ(2U, candidates_.size());
650 // There will only be 2 candidates in |ready_candidates| because it only
651 // includes the candidates in the ready ports.
652 const std::vector<Candidate>& ready_candidates =
653 session_->ReadyCandidates();
654 EXPECT_EQ(2U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200655 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
656 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
657 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -0800658 }
659
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700660 void TestEachInterfaceHasItsOwnTurnPorts() {
661 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
662 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
663 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
664 // Add two interfaces both having IPv4 and IPv6 addresses.
665 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
666 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
667 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
668 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
669 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -0700670 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700671 allocator_->SetConfiguration(allocator_->stun_servers(),
672 allocator_->turn_servers(), 0, true);
673 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
674 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
675 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
676
677 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800678 allocator_->set_flags(
679 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
680 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700681 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
682 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800683 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
684 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700685 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
686 // use.
687 EXPECT_EQ(10U, session_->ReadyPorts().size());
688 EXPECT_EQ(10U, ports_.size());
689 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
690 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
691 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
692 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
693 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
694 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
695 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
696 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
697 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
698 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
699
700 // Now that we remove candidates when TURN ports are pruned, there will be
701 // exactly 10 candidates in |candidates_|.
702 EXPECT_EQ(10U, candidates_.size());
703 const std::vector<Candidate>& ready_candidates =
704 session_->ReadyCandidates();
705 EXPECT_EQ(10U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200706 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
707 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
708 EXPECT_TRUE(
709 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
710 EXPECT_TRUE(
711 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
712 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
713 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
714 EXPECT_TRUE(
715 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
716 EXPECT_TRUE(
717 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
718 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
719 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700720 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000721};
722
723// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700724TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000725 EXPECT_EQ(&network_manager_, allocator().network_manager());
726 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800727 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700728 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000729 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800730 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
731 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700732 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
733 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
734 EXPECT_TRUE(
735 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
736 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700737 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000738}
739
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800740// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700741TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800742 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
743 rtc::ADAPTER_TYPE_ETHERNET);
744 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
745 rtc::ADAPTER_TYPE_WIFI);
746 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
747 rtc::ADAPTER_TYPE_CELLULAR);
748 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
749 rtc::ADAPTER_TYPE_VPN);
750 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
751 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700752 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
753 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
754 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800755 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800756 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
757 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800758 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700759 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 EXPECT_LT(candidate.address().ip(), 0x12345604U);
761 }
762}
763
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700764TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800765 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
766 rtc::ADAPTER_TYPE_ETHERNET);
767 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
768 rtc::ADAPTER_TYPE_WIFI);
769 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
770 rtc::ADAPTER_TYPE_CELLULAR);
771 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
772 rtc::ADAPTER_TYPE_LOOPBACK |
773 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700774 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
775 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
776 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800777 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800778 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
779 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800780 EXPECT_EQ(1U, candidates_.size());
781 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
782}
783
deadbeef1c46a352017-09-27 11:24:05 -0700784// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
785// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
786TEST_F(BasicPortAllocatorTest,
787 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
788 SocketAddress wifi(IPAddress(0x12345600U), 0);
789 SocketAddress cell(IPAddress(0x12345601U), 0);
790 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
791 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
792 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-05-31 18:29:12 -0700793 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
794 cricket::PORTALLOCATOR_DISABLE_RELAY |
795 cricket::PORTALLOCATOR_DISABLE_TCP |
796 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
797 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
798 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800799 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
800 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700801 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700802 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200803 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700804}
honghaiz60347052016-05-31 18:29:12 -0700805
deadbeef1c46a352017-09-27 11:24:05 -0700806// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
807// both "unknown" and cell interfaces are available, only the unknown are used.
808// The unknown interface may be something that ultimately uses Wi-Fi, so we do
809// this to be on the safe side.
810TEST_F(BasicPortAllocatorTest,
811 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
812 SocketAddress cell(IPAddress(0x12345601U), 0);
813 SocketAddress unknown1(IPAddress(0x12345602U), 0);
814 SocketAddress unknown2(IPAddress(0x12345603U), 0);
815 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
816 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
817 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
818 // Disable all but UDP candidates to make the test simpler.
819 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
820 cricket::PORTALLOCATOR_DISABLE_RELAY |
821 cricket::PORTALLOCATOR_DISABLE_TCP |
822 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
823 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700824 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800825 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
826 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700827 // Should only get two candidates, none of which is cell.
honghaiz60347052016-05-31 18:29:12 -0700828 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200829 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
830 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
deadbeef1c46a352017-09-27 11:24:05 -0700831}
honghaiz60347052016-05-31 18:29:12 -0700832
deadbeef1c46a352017-09-27 11:24:05 -0700833// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
834// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
835// interface is used.
836TEST_F(BasicPortAllocatorTest,
837 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
838 SocketAddress wifi(IPAddress(0x12345600U), 0);
839 SocketAddress cellular(IPAddress(0x12345601U), 0);
840 SocketAddress unknown1(IPAddress(0x12345602U), 0);
841 SocketAddress unknown2(IPAddress(0x12345603U), 0);
842 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
843 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
844 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
845 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
846 // Disable all but UDP candidates to make the test simpler.
847 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
848 cricket::PORTALLOCATOR_DISABLE_RELAY |
849 cricket::PORTALLOCATOR_DISABLE_TCP |
850 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
851 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700852 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800853 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
854 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700855 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700856 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200857 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 11:24:05 -0700858}
859
860// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
861// only interface available is cellular, it ends up used anyway. A costly
862// connection is always better than no connection.
863TEST_F(BasicPortAllocatorTest,
864 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
865 SocketAddress cellular(IPAddress(0x12345601U), 0);
866 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
867 // Disable all but UDP candidates to make the test simpler.
868 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
869 cricket::PORTALLOCATOR_DISABLE_RELAY |
870 cricket::PORTALLOCATOR_DISABLE_TCP |
871 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
872 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
873 session_->StartGettingPorts();
874 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
875 kDefaultAllocationTimeout, fake_clock);
876 // Make sure we got the cell candidate.
877 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200878 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
honghaiz60347052016-05-31 18:29:12 -0700879}
880
Yuwei Huangb181f712018-01-22 17:01:28 -0800881// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
882// a WiFi network with link-local IP address and a cellular network, then the
883// cellular candidate will still be gathered.
884TEST_F(BasicPortAllocatorTest,
885 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
886 SocketAddress wifi_link_local("169.254.0.1", 0);
887 SocketAddress cellular(IPAddress(0x12345601U), 0);
888 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
889 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
890
891 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
892 cricket::PORTALLOCATOR_DISABLE_RELAY |
893 cricket::PORTALLOCATOR_DISABLE_TCP |
894 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
895 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
896 session_->StartGettingPorts();
897 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
898 kDefaultAllocationTimeout, fake_clock);
899 // Make sure we got both wifi and cell candidates.
900 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200901 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
902 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
Yuwei Huangb181f712018-01-22 17:01:28 -0800903}
904
905// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
906// a WiFi network with link-local IP address, a WiFi network with a normal IP
907// address and a cellular network, then the cellular candidate will not be
908// gathered.
909TEST_F(BasicPortAllocatorTest,
910 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
911 SocketAddress wifi(IPAddress(0x12345600U), 0);
912 SocketAddress wifi_link_local("169.254.0.1", 0);
913 SocketAddress cellular(IPAddress(0x12345601U), 0);
914 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
915 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
916 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
917
918 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
919 cricket::PORTALLOCATOR_DISABLE_RELAY |
920 cricket::PORTALLOCATOR_DISABLE_TCP |
921 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
922 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
923 session_->StartGettingPorts();
924 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
925 kDefaultAllocationTimeout, fake_clock);
926 // Make sure we got only wifi candidates.
927 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +0200928 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
929 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
Yuwei Huangb181f712018-01-22 17:01:28 -0800930}
931
deadbeef3427f532017-07-26 16:09:33 -0700932// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
933// to gather candidates.
934TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
935 // Add three IPv6 network interfaces, but tell the allocator to only use two.
936 allocator().set_max_ipv6_networks(2);
937 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
938 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
939 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
940
941 // To simplify the test, only gather UDP host candidates.
942 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
943 PORTALLOCATOR_DISABLE_STUN |
944 PORTALLOCATOR_DISABLE_RELAY);
945
946 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
947 session_->StartGettingPorts();
948 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
949 kDefaultAllocationTimeout, fake_clock);
950 EXPECT_EQ(2U, candidates_.size());
951 // Ensure the expected two interfaces (eth0 and eth1) were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +0200952 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
953 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
deadbeef3427f532017-07-26 16:09:33 -0700954}
955
956// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
957// being used.
958TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
959 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
960 allocator().set_max_ipv6_networks(1);
961 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
962 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
963 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
964 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
965
966 // To simplify the test, only gather UDP host candidates.
967 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
968 PORTALLOCATOR_DISABLE_STUN |
969 PORTALLOCATOR_DISABLE_RELAY);
970
971 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
972 session_->StartGettingPorts();
973 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
974 kDefaultAllocationTimeout, fake_clock);
975 EXPECT_EQ(3U, candidates_.size());
976 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
977 // were used.
Jonas Olssonabbe8412018-04-03 13:40:05 +0200978 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
979 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
980 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
deadbeef3427f532017-07-26 16:09:33 -0700981}
982
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700983// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700984TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800985 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
986 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700987 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
988 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
989 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700990 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800991 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
992 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700993 EXPECT_EQ(1U, candidates_.size());
994}
995
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000996// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700997TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000998 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700999 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001000 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001001 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1002 kDefaultAllocationTimeout, fake_clock);
1003 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001004 EXPECT_EQ(4U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001005 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1006 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1007 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1008 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1009 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1010 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1011 EXPECT_TRUE(
1012 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001013}
1014
honghaiz8c404fa2015-09-28 07:59:43 -07001015// Test that when the same network interface is brought down and up, the
1016// port allocator session will restart a new allocation sequence if
1017// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001018TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001019 std::string if_name("test_net0");
1020 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001021 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001022 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001023 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1024 kDefaultAllocationTimeout, fake_clock);
1025 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001026 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001027 candidate_allocation_done_ = false;
1028 candidates_.clear();
1029 ports_.clear();
1030
deadbeef7f1563f2017-09-15 17:40:01 -07001031 // Disable socket creation to simulate the network interface being down. When
1032 // no network interfaces are available, BasicPortAllocator will fall back to
1033 // binding to the "ANY" address, so we need to make sure that fails too.
1034 fss_->set_tcp_sockets_enabled(false);
1035 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -07001036 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001037 SIMULATED_WAIT(false, 1000, fake_clock);
1038 EXPECT_EQ(0U, candidates_.size());
1039 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -07001040
1041 // When the same interfaces are added again, new candidates/ports should be
1042 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -07001043 fss_->set_tcp_sockets_enabled(true);
1044 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -07001045 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001046 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1047 kDefaultAllocationTimeout, fake_clock);
1048 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001049 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001050}
1051
1052// Test that when the same network interface is brought down and up, the
1053// port allocator session will not restart a new allocation sequence if
1054// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001055TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001056 std::string if_name("test_net0");
1057 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001058 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001059 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001060 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1061 kDefaultAllocationTimeout, fake_clock);
1062 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001063 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001064 session_->StopGettingPorts();
1065 candidates_.clear();
1066 ports_.clear();
1067
1068 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001069 // Wait one (simulated) second and then verify no new candidates have
1070 // appeared.
1071 SIMULATED_WAIT(false, 1000, fake_clock);
1072 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001073 EXPECT_EQ(0U, ports_.size());
1074
1075 // When the same interfaces are added again, new candidates/ports should not
1076 // be generated because the session has stopped.
1077 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001078 SIMULATED_WAIT(false, 1000, fake_clock);
1079 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001080 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001081}
1082
deadbeef1c46a352017-09-27 11:24:05 -07001083// Similar to the above tests, but tests a situation when sockets can't be
1084// bound to a network interface, then after a network change event can be.
1085// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1086TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1087 // Only test local ports to simplify test.
1088 ResetWithNoServersOrNat();
1089 // Provide a situation where the interface appears to be available, but
1090 // binding the sockets fails. See bug for description of when this can
1091 // happen.
1092 std::string if_name("test_net0");
1093 AddInterface(kClientAddr, if_name);
1094 fss_->set_tcp_sockets_enabled(false);
1095 fss_->set_udp_sockets_enabled(false);
1096 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1097 session_->StartGettingPorts();
1098 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1099 kDefaultAllocationTimeout, fake_clock);
1100 // Make sure we actually prevented candidates from being gathered (other than
1101 // a single TCP active candidate, since that doesn't require creating a
1102 // socket).
1103 ASSERT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001104 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001105 candidate_allocation_done_ = false;
1106
1107 // Now simulate the interface coming up, with the newfound ability to bind
1108 // sockets.
1109 fss_->set_tcp_sockets_enabled(true);
1110 fss_->set_udp_sockets_enabled(true);
1111 AddInterface(kClientAddr, if_name);
1112 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1113 kDefaultAllocationTimeout, fake_clock);
1114 // Should get UDP and TCP candidate.
1115 ASSERT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001116 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001117 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1118 // We should extend this test to also verify that a server candidate is
1119 // gathered.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001120 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 11:24:05 -07001121}
1122
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001123// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001124TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001125 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001126 allocator_->set_step_delay(kDefaultStepDelay);
1127 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001128 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001129 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001130 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001131 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001132 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001133 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1134 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1135 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1136 EXPECT_TRUE(
1137 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
deadbeef1c5e6d02017-09-15 17:46:56 -07001138 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001139 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 EXPECT_EQ(4U, ports_.size());
1141 EXPECT_TRUE(candidate_allocation_done_);
1142 // If we Stop gathering now, we shouldn't get a second "done" callback.
1143 session_->StopGettingPorts();
1144}
1145
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001146TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001147 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001148 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001150 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1151 kDefaultAllocationTimeout, fake_clock);
1152 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001153 // If we Stop gathering now, we shouldn't get a second "done" callback.
1154 session_->StopGettingPorts();
1155
1156 // All ports should have unset send-buffer sizes.
1157 CheckSendBufferSizesOfAllPorts(-1);
1158}
1159
deadbeef7f1563f2017-09-15 17:40:01 -07001160// Tests that we can get callback after StopGetAllPorts when called in the
1161// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001162TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001163 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001164 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001165 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001166 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1167 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001168 EXPECT_EQ(2U, ports_.size());
1169 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001170 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1171 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172}
1173
1174// Test that we restrict client ports appropriately when a port range is set.
1175// We check the candidates for udp/stun/tcp ports, and the from address
1176// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001177TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001178 AddInterface(kClientAddr);
1179 // Check that an invalid port range fails.
1180 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1181 // Check that a null port range succeeds.
1182 EXPECT_TRUE(SetPortRange(0, 0));
1183 // Check that a valid port range succeeds.
1184 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001185 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001186 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001187 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1188 kDefaultAllocationTimeout, fake_clock);
1189 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001191
1192 int num_nonrelay_candidates = 0;
1193 for (const Candidate& candidate : candidates_) {
1194 // Check the port number for the UDP/STUN/TCP port objects.
1195 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001196 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 11:44:17 -07001197 ++num_nonrelay_candidates;
1198 }
1199 }
1200 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001201 // Check the port number used to connect to the relay server.
Jonas Olssonabbe8412018-04-03 13:40:05 +02001202 EXPECT_TRUE(
1203 CheckPort(relay_server_.GetConnection(0).source(), kMinPort, kMaxPort));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001204}
1205
deadbeefe97389c2016-12-23 01:43:45 -08001206// Test that if we have no network adapters, we bind to the ANY address and
1207// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001208TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001209 // Default config uses GTURN and no NAT, so replace that with the
1210 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1211 ResetWithStunServerAndNat(kStunAddr);
1212 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1213 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1214 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1215 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1216 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1217 // TODO(deadbeef): Fix this.
1218 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001219 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001220 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001221 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1222 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001223 EXPECT_EQ(4U, ports_.size());
1224 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1225 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1226 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1227 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1228 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1229 // The "any" address port should be in the signaled ready ports, but the host
1230 // candidate for it is useless and shouldn't be signaled. So we only have
1231 // STUN/TURN candidates.
1232 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001233 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1234 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 01:43:45 -08001235 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1236 // server.
1237 EXPECT_EQ(2,
1238 CountCandidates(candidates_, "relay", "udp",
1239 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001240}
1241
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001242// Test that when enumeration is disabled, we should not have any ports when
1243// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001244TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001245 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001246 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001247 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001248 // Expect to see no ports and no candidates.
1249 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1250 rtc::IPAddress(), rtc::IPAddress());
1251}
1252
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001253// Test that even with multiple interfaces, the result should still be a single
1254// default private, one STUN and one TURN candidate since we bind to any address
1255// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001256TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001257 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001258 AddInterface(kPrivateAddr);
1259 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001260 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001261 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001262
1263 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1264 // address set and we have no IPv6 STUN server, there should be no IPv6
1265 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001266 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1267 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001268
1269 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1270 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1271 // TURN/UDP candidates.
1272 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001273 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1274 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001275}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001276
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001277// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1278// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001279TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001280 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001281 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001282 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001283 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001284 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1285 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1286 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1287 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001288 kTurnUdpExtAddr.ipaddr());
1289}
1290
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001291// Test that when adapter enumeration is disabled, for endpoints without
1292// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001293TEST_F(BasicPortAllocatorTest,
1294 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001295 ResetWithNoServersOrNat();
1296 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1297 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1298 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001299}
1300
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001301// Test that when adapter enumeration is disabled, with
1302// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1303// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001304TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001305 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1306 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001307 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1308 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001309 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1310 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001311 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001312 rtc::IPAddress(), rtc::IPAddress());
1313}
1314
1315// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001316// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1317// a NAT, there is no local candidate. However, this specified default route
1318// (kClientAddr) which was discovered when sending STUN requests, will become
1319// the srflx addresses.
1320TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001321 BasicPortAllocatorTest,
Steve Anton6c38cc72017-11-29 10:25:58 -08001322 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001323 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001324 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001325 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1326 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001327 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1328 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001329 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1330 rtc::IPAddress(), rtc::IPAddress());
1331}
1332
1333// Test that when adapter enumeration is disabled, with
1334// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1335// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001337 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1338 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001339 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1340 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001341 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1342 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1343 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001344}
1345
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001346// Test that we disable relay over UDP, and only TCP is used when connecting to
1347// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001348TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001349 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001350 AddInterface(kClientAddr);
1351 ResetWithStunServerAndNat(kStunAddr);
1352 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001353 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1354 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1355 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1356 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001357
1358 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001359 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1360 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001361
1362 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1363 // TURN/TCP candidates.
1364 EXPECT_EQ(2U, ports_.size());
1365 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001366 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001367 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1368 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001369 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001370 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001371 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001372}
1373
Erik Språngefdce692015-06-05 09:41:26 +02001374// Disable for asan, see
1375// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1376#if !defined(ADDRESS_SANITIZER)
1377
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001378// Test that we can get OnCandidatesAllocationDone callback when all the ports
1379// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001380TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001381 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001382 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1383 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1384 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001385 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001386 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001387 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001388}
1389
1390// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001391TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001392 AddInterface(kClientAddr);
1393 fss_->set_udp_sockets_enabled(false);
1394 EXPECT_TRUE(CreateSession(1));
1395 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001396 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1397 kDefaultAllocationTimeout, fake_clock);
1398 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001399 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001400 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1401 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1402 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1403 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1404 EXPECT_TRUE(
1405 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001406}
1407
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001408#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001409
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410// Test that we don't crash or malfunction if we can't create UDP sockets or
1411// listen on TCP sockets. We still give out a local TCP address, since
1412// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001413TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 AddInterface(kClientAddr);
1415 fss_->set_udp_sockets_enabled(false);
1416 fss_->set_tcp_listen_enabled(false);
1417 EXPECT_TRUE(CreateSession(1));
1418 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001419 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1420 kDefaultAllocationTimeout, fake_clock);
1421 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001422 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001423 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1424 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
1425 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1426 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1427 EXPECT_TRUE(
1428 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001429}
1430
1431// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001432// TODO(deadbeef): Find a way to exit early here.
1433TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001434 AddInterface(kClientAddr);
1435 fss_->set_tcp_sockets_enabled(false);
1436 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001437 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001438 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001439 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001440 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441 // In case of Relay, ports creation will succeed but sockets will fail.
1442 // There is no error reporting from RelayEntry to handle this failure.
1443}
1444
1445// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001446TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001447 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1448 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001449 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001451 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1452 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001453 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001454 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1455 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001456 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001457 // will be tried after about 3 seconds.
1458 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001459 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001460 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpIntAddr));
1461 EXPECT_TRUE(HasCandidate(candidates_, "relay", "tcp", kRelayTcpIntAddr));
1462 EXPECT_TRUE(
1463 HasCandidate(candidates_, "relay", "ssltcp", kRelaySslTcpIntAddr));
1464 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", kRelayUdpExtAddr));
pthatcher94a2f212017-02-08 14:42:22 -08001465 // We wait at least for a full STUN timeout, which
1466 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1467 // already passed (see above), we wait 3 seconds less than that.
1468 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1469 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470}
1471
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001472TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001473 AddInterface(kClientAddr);
1474 AddInterface(kClientAddr2);
1475 // Allocating only host UDP ports. This is done purely for testing
1476 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001477 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1478 PORTALLOCATOR_DISABLE_RELAY);
1479 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001480 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001481 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1482 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001483 ASSERT_EQ(2U, candidates_.size());
1484 EXPECT_EQ(2U, ports_.size());
1485 // Candidates priorities should be different.
1486 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1487}
1488
1489// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001490TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001491 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001492 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001493 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001494 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1495 kDefaultAllocationTimeout, fake_clock);
1496 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001497 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001498 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001499}
1500
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001501// Test that the allocator session uses the candidate filter it's created with,
1502// rather than the filter of its parent allocator.
1503// The filter of the allocator should only affect the next gathering phase,
1504// according to JSEP, which means the *next* allocator session returned.
1505TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1506 AddInterface(kClientAddr);
1507 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1508 // Set candidate filter *after* creating the session. Should have no effect.
1509 allocator().set_candidate_filter(CF_RELAY);
1510 session_->StartGettingPorts();
1511 // 7 candidates and 4 ports is what we would normally get (see the
1512 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001513 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1514 kDefaultAllocationTimeout, fake_clock);
1515 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001516 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001517}
1518
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001519// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1520// This test also verifies that when the allocator is only allowed to use
1521// relay (i.e. IceTransportsType is relay), the raddr is an empty
1522// address with the correct family. This is to prevent any local
1523// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001524TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001525 AddInterface(kClientAddr);
1526 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001527 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001528 allocator().set_candidate_filter(CF_RELAY);
1529 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001530 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001531 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1532 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001533 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1534 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001535
1536 EXPECT_EQ(1U, candidates_.size());
1537 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001538 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1539 EXPECT_EQ(
1540 candidates_[0].related_address(),
1541 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001542}
1543
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001544TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001545 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001546 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1547 allocator().set_candidate_filter(CF_HOST);
1548 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001549 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001550 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1551 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001552 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1553 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001554 for (const Candidate& candidate : candidates_) {
1555 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001556 }
1557}
1558
1559// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001560TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001561 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001562 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001563
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001564 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1565 allocator().set_candidate_filter(CF_REFLEXIVE);
1566 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001567 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001568 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1569 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001570 // Host is behind NAT, no private address will be exposed. Hence only UDP
1571 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001572 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1573 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001574 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1575 EXPECT_EQ(
1576 candidates_[0].related_address(),
1577 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001578}
1579
1580// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001581TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001582 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001583 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1584 allocator().set_candidate_filter(CF_REFLEXIVE);
1585 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001586 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001587 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1588 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001589 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001590 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001591 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001592 for (const Candidate& candidate : candidates_) {
1593 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001594 }
1595}
1596
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001597// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001598TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001600 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001601 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001602 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1603 kDefaultAllocationTimeout, fake_clock);
1604 EXPECT_EQ(7U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001605 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1606 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1607 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001608 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001609 for (const Candidate& candidate : candidates_) {
1610 EXPECT_EQ(kIceUfrag0, candidate.username());
1611 EXPECT_EQ(kIcePwd0, candidate.password());
1612 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001613}
1614
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001615// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1616// is allocated for udp and stun. Also verify there is only one candidate
1617// (local) if stun candidate is same as local candidate, which will be the case
1618// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001619TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001620 AddInterface(kClientAddr);
1621 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001622 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1623 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001624 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001625 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1626 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001627 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001628 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 13:18:00 -08001629 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1630 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001631}
1632
1633// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1634// is allocated for udp and stun. In this test we should expect both stun and
1635// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001636TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001637 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001638 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001639
1640 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001641 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1642 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001643 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001644 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1645 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001646 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001647 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1648 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1649 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001650 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1651 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001652 EXPECT_EQ(3U, candidates_.size());
1653}
1654
deadbeefc5d0d952015-07-16 10:22:21 -07001655// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001656TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001657 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001659 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001660 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001661
1662 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1663
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001664 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001665 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001666 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1667 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001669 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001670 session_->StartGettingPorts();
1671
deadbeef7f1563f2017-09-15 17:40:01 -07001672 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1673 kDefaultAllocationTimeout, fake_clock);
1674 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001675 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001676 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1677 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1678 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1679 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1680 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001681}
1682
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001683// Test that if prune_turn_ports is set, TCP TURN port will not be used
1684// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1685TEST_F(BasicPortAllocatorTest,
1686 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1687 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1688 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1689 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001690
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001691 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001692}
1693
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001694// Test that if prune_turn_ports is set, TCP TURN port will not be used
1695// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1696TEST_F(BasicPortAllocatorTest,
1697 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1698 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1699 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1700 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001701
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001702 TestUdpTurnPortPrunesTcpTurnPort();
1703}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001704
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001705// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1706// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1707TEST_F(BasicPortAllocatorTest,
1708 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1709 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1710 // first.
1711 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1712 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001713
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001714 TestIPv6TurnPortPrunesIPv4TurnPort();
1715}
1716
1717// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1718// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1719TEST_F(BasicPortAllocatorTest,
1720 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1721 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1722 // first.
1723 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1724 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1725
1726 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001727}
1728
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001729// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001730// will has its own set of TurnPorts based on their priorities, in the default
1731// case where no transit delay is set.
1732TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1733 TestEachInterfaceHasItsOwnTurnPorts();
1734}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001735
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001736// Tests that if prune_turn_ports is set, each network interface
1737// will has its own set of TurnPorts based on their priorities, given that
1738// IPv4/TCP TURN port becomes ready first.
1739TEST_F(BasicPortAllocatorTest,
1740 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1741 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1742 // becomes ready last.
1743 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1744 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1745 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1746 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001747
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001748 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001749}
1750
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001751// Testing DNS resolve for the TURN server, this will test AllocationSequence
1752// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001753// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1754// appears that it doesn't currently because of the DNS look up not
1755// using the fake clock.
1756TEST_F(BasicPortAllocatorTestWithRealClock,
1757 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001758 // This test relies on a real query for "localhost", so it won't work on an
1759 // IPv6-only machine.
1760 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001761 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001762 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001763 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001764 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 16:57:45 -07001765 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001766 RelayServerConfig turn_server(RELAY_TURN);
1767 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001768 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001769 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001770 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001771 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001772
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001773 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001774 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001775 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1776 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001777
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001778 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001779 session_->StartGettingPorts();
1780
1781 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1782}
1783
1784// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1785// is allocated for udp/stun/turn. In this test we should expect all local,
1786// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001787TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001788 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001789 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001790
1791 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1792
1793 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001794 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1795 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001796
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001797 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001798 session_->StartGettingPorts();
1799
deadbeef7f1563f2017-09-15 17:40:01 -07001800 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1801 kDefaultAllocationTimeout, fake_clock);
1802 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001803 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001804 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1805 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1806 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1807 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1808 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 13:18:00 -08001809 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1810 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001811 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001812 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1813 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001814 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1815 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1816}
1817
1818// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1819// server is also used as the STUN server, we should get 'local', 'stun', and
1820// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001821TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001822 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001823 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001824 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001825 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1826
1827 // Must set the step delay to 0 to make sure the relay allocation phase is
1828 // started before the STUN candidates are obtained, so that the STUN binding
1829 // response is processed when both StunPort and TurnPort exist to reproduce
1830 // webrtc issue 3537.
1831 allocator_->set_step_delay(0);
1832 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001833 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1834 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001835
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001836 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001837 session_->StartGettingPorts();
1838
deadbeef7f1563f2017-09-15 17:40:01 -07001839 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1840 kDefaultAllocationTimeout, fake_clock);
1841 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001842 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001843 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001844 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1845 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1846 &stun_candidate));
1847 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1848 candidates_, "relay", "udp",
1849 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1850 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001851
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001852 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001853 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1854 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001855 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1856 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1857}
1858
deadbeefc5d0d952015-07-16 10:22:21 -07001859// Test that when only a TCP TURN server is available, we do NOT use it as
1860// a UDP STUN server, as this could leak our IP address. Thus we should only
1861// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001862TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001863 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001864 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001865 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001866 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1867
1868 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001869 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1870 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001871
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001872 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001873 session_->StartGettingPorts();
1874
deadbeef7f1563f2017-09-15 17:40:01 -07001875 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1876 kDefaultAllocationTimeout, fake_clock);
1877 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001878 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001879 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1880 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1881 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 10:22:21 -07001882 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1883 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1884}
1885
1886// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1887// TURN server is used as the STUN server and we get 'local', 'stun', and
1888// 'relay' candidates.
1889// TODO(deadbeef): Remove this test when support for non-shared socket mode
1890// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001891TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001892 AddInterface(kClientAddr);
1893 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001894 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001895 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1896
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001897 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001898
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001899 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001900 session_->StartGettingPorts();
1901
deadbeef7f1563f2017-09-15 17:40:01 -07001902 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1903 kDefaultAllocationTimeout, fake_clock);
1904 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001905 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001906 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001907 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001908 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1909 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1910 &stun_candidate));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001911 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001912 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1913 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1914 &turn_candidate));
deadbeefc5d0d952015-07-16 10:22:21 -07001915 // Not using shared socket, so the STUN request's server reflexive address
1916 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001917 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001918
deadbeefc5d0d952015-07-16 10:22:21 -07001919 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1920 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1921 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1922}
1923
1924// Test that even when both a STUN and TURN server are configured, the TURN
1925// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001926TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001927 AddInterface(kClientAddr);
1928 // Configure with STUN server but destroy it, so we can ensure that it's
1929 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001930 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001931 stun_server_.reset();
1932 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1933
1934 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001935 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1936 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001937
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001938 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001939 session_->StartGettingPorts();
1940
pthatcher1749bc32017-02-08 13:18:00 -08001941 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1942 fake_clock);
Jonas Olssonabbe8412018-04-03 13:40:05 +02001943 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001944 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 13:40:05 +02001945 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1946 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1947 &stun_candidate));
1948 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1949 candidates_, "relay", "udp",
1950 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1951 stun_candidate.address()));
deadbeefc5d0d952015-07-16 10:22:21 -07001952
1953 // Don't bother waiting for STUN timeout, since we already verified
1954 // that we got a STUN candidate from the TURN server.
1955}
1956
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001957// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1958// and fail to generate STUN candidate, local UDP candidate is generated
1959// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001960TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001961 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1962 PORTALLOCATOR_DISABLE_TCP |
1963 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001964 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1965 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001966 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001967 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001968 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1969 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001970 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001971 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001972 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001973 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1974 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001975 EXPECT_EQ(1U, candidates_.size());
1976}
1977
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001978// Test that when the NetworkManager doesn't have permission to enumerate
1979// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1980// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001981TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001982 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1983 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001984 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001985 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001986 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1987 PORTALLOCATOR_DISABLE_TCP |
1988 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1989 EXPECT_EQ(0U,
1990 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1991 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1992 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001993 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001994 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1995 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001996 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02001997 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001998 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001999}
2000
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002001// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002002TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002003 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2004 PORTALLOCATOR_ENABLE_IPV6 |
2005 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002006 AddInterface(kClientIPv6Addr);
2007 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002008 allocator_->set_step_delay(kMinimumStepDelay);
2009 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002010 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07002011 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08002012 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07002013 EXPECT_EQ(4U, ports_.size());
2014 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 13:40:05 +02002015 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2016 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2017 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2018 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002019}
honghaiz98db68f2015-09-29 07:58:17 -07002020
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002021TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002022 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002023 allocator_->set_step_delay(kDefaultStepDelay);
2024 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002025 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002026 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002027 EXPECT_EQ(2U, ports_.size());
2028 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002029 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002030
2031 // After stopping getting ports, adding a new interface will not start
2032 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002033 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002034 candidates_.clear();
2035 ports_.clear();
2036 candidate_allocation_done_ = false;
2037 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002038 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002039 EXPECT_EQ(0U, candidates_.size());
2040 EXPECT_EQ(0U, ports_.size());
2041}
2042
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002043TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002044 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002045 allocator_->set_step_delay(kDefaultStepDelay);
2046 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002047 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002048 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002049 EXPECT_EQ(2U, ports_.size());
2050 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002051 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002052
2053 // After clearing getting ports, adding a new interface will start getting
2054 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002055 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002056 candidates_.clear();
2057 ports_.clear();
2058 candidate_allocation_done_ = false;
2059 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002060 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002061 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002062 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2063 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002064}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002065
2066// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2067// a pooled session is taken out of the pool.
2068TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2069 AddInterface(kClientAddr);
2070 int pool_size = 1;
2071 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002072 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002073 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002074 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002075 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2076 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002077 // Expect that when TakePooledSession is called,
2078 // UpdateTransportInformationInternal will be called and the
2079 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2080 // candidates.
2081 session_ =
2082 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2083 ASSERT_NE(nullptr, session_.get());
2084 auto ready_ports = session_->ReadyPorts();
2085 auto candidates = session_->ReadyCandidates();
2086 EXPECT_FALSE(ready_ports.empty());
2087 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002088 for (const PortInterface* port_interface : ready_ports) {
2089 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002090 EXPECT_EQ(kContentName, port->content_name());
2091 EXPECT_EQ(1, port->component());
2092 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2093 EXPECT_EQ(kIcePwd0, port->password());
2094 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002095 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002096 EXPECT_EQ(1, candidate.component());
2097 EXPECT_EQ(kIceUfrag0, candidate.username());
2098 EXPECT_EQ(kIcePwd0, candidate.password());
2099 }
2100}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002101
2102// Test that a new candidate filter takes effect even on already-gathered
2103// candidates.
2104TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2105 AddInterface(kClientAddr);
2106 int pool_size = 1;
2107 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002108 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002109 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2110 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002111 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2112 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002113 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2114 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2115 allocator_->set_candidate_filter(CF_RELAY);
2116 // Assume that when TakePooledSession is called, the candidate filter will be
2117 // applied to the pooled session. This is tested by PortAllocatorTest.
2118 session_ =
2119 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2120 ASSERT_NE(nullptr, session_.get());
2121 auto candidates = session_->ReadyCandidates();
2122 auto ports = session_->ReadyPorts();
2123 // Sanity check that the number of candidates and ports decreased.
2124 EXPECT_GT(initial_candidates_size, candidates.size());
2125 EXPECT_GT(initial_ports_size, ports.size());
2126 for (const PortInterface* port : ports) {
2127 // Expect only relay ports.
2128 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2129 }
2130 for (const Candidate& candidate : candidates) {
2131 // Expect only relay candidates now that the filter is applied.
2132 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2133 // Expect that the raddr is emptied due to the CF_RELAY filter.
2134 EXPECT_EQ(candidate.related_address(),
2135 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2136 }
2137}
2138
Qingsi Wang4ff54432018-03-01 18:25:20 -08002139TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2140 const int pool_size = 1;
2141 const int expected_stun_keepalive_interval = 123;
2142 AddInterface(kClientAddr);
2143 allocator_->SetConfiguration(allocator_->stun_servers(),
2144 allocator_->turn_servers(), pool_size, false,
2145 nullptr, expected_stun_keepalive_interval);
2146 auto* pooled_session = allocator_->GetPooledSession();
2147 ASSERT_NE(nullptr, pooled_session);
2148 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2149 kDefaultAllocationTimeout, fake_clock);
2150 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2151 expected_stun_keepalive_interval);
2152}
2153
2154TEST_F(BasicPortAllocatorTest,
2155 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2156 const int pool_size = 1;
2157 AddInterface(kClientAddr);
2158 allocator_->SetConfiguration(allocator_->stun_servers(),
2159 allocator_->turn_servers(), pool_size, false,
2160 nullptr, 123 /* stun keepalive interval */);
2161 auto* pooled_session = allocator_->GetPooledSession();
2162 ASSERT_NE(nullptr, pooled_session);
2163 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2164 kDefaultAllocationTimeout, fake_clock);
2165 const int expected_stun_keepalive_interval = 321;
2166 allocator_->SetConfiguration(allocator_->stun_servers(),
2167 allocator_->turn_servers(), pool_size, false,
2168 nullptr, expected_stun_keepalive_interval);
2169 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2170 expected_stun_keepalive_interval);
2171}
2172
2173TEST_F(BasicPortAllocatorTest,
2174 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2175 const int pool_size = 1;
2176 const int expected_stun_keepalive_interval = 123;
2177 AddInterface(kClientAddr);
2178 allocator_->set_flags(allocator().flags() |
2179 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2180 allocator_->SetConfiguration(allocator_->stun_servers(),
2181 allocator_->turn_servers(), pool_size, false,
2182 nullptr, expected_stun_keepalive_interval);
2183 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2184 session_->StartGettingPorts();
2185 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2186 kDefaultAllocationTimeout, fake_clock);
2187 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2188 expected_stun_keepalive_interval);
2189}
2190
2191TEST_F(BasicPortAllocatorTest,
2192 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2193 const int pool_size = 1;
2194 const int expected_stun_keepalive_interval = 123;
2195 AddInterface(kClientAddr);
2196 allocator_->set_flags(allocator().flags() &
2197 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2198 allocator_->SetConfiguration(allocator_->stun_servers(),
2199 allocator_->turn_servers(), pool_size, false,
2200 nullptr, expected_stun_keepalive_interval);
2201 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2202 session_->StartGettingPorts();
2203 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2204 kDefaultAllocationTimeout, fake_clock);
2205 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2206 expected_stun_keepalive_interval);
2207}
2208
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002209} // namespace cricket