blob: df6eb4cb151921f4a89e242eee7da3d94e14b98b [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/basicpacketsocketfactory.h"
15#include "p2p/base/p2pconstants.h"
16#include "p2p/base/p2ptransportchannel.h"
Qingsi Wang4ff54432018-03-01 18:25:20 -080017#include "p2p/base/stunport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/testrelayserver.h"
19#include "p2p/base/teststunserver.h"
20#include "p2p/base/testturnserver.h"
21#include "p2p/client/basicportallocator.h"
22#include "rtc_base/fakeclock.h"
23#include "rtc_base/fakenetwork.h"
24#include "rtc_base/firewallsocketserver.h"
25#include "rtc_base/gunit.h"
26#include "rtc_base/helpers.h"
27#include "rtc_base/ipaddress.h"
28#include "rtc_base/logging.h"
29#include "rtc_base/natserver.h"
30#include "rtc_base/natsocketfactory.h"
31#include "rtc_base/nethelpers.h"
32#include "rtc_base/network.h"
33#include "rtc_base/socketaddress.h"
34#include "rtc_base/ssladapter.h"
35#include "rtc_base/thread.h"
36#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080038using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039using rtc::SocketAddress;
40using rtc::Thread;
41
Mirko Bonadei675513b2017-11-09 11:09:25 +010042#define MAYBE_SKIP_IPV4 \
43 if (!rtc::HasIPv4Enabled()) { \
44 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
45 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070046 }
47
deadbeefe97389c2016-12-23 01:43:45 -080048static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070050static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070051static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000053static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070054static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
55 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070056static const SocketAddress kClientIPv6Addr2(
57 "2401:fa00:4:2000:be30:5bff:fee5:c3",
58 0);
deadbeef3427f532017-07-26 16:09:33 -070059static const SocketAddress kClientIPv6Addr3(
60 "2401:fa00:4:3000:be30:5bff:fee5:c3",
61 0);
deadbeefc5d0d952015-07-16 10:22:21 -070062static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
63static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
65static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
66static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
67static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
68static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
69static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
70static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
71static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
72static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070073static const SocketAddress kTurnUdpIntIPv6Addr(
74 "2402:fb00:4:1000:be30:5bff:fee5:c3",
75 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070077static const SocketAddress kTurnTcpIntIPv6Addr(
78 "2402:fb00:4:2000:be30:5bff:fee5:c3",
79 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
81
82// Minimum and maximum port for port range tests.
83static const int kMinPort = 10000;
84static const int kMaxPort = 10099;
85
86// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070087static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088// Based on ICE_PWD_LENGTH
89static const char kIcePwd0[] = "TESTICEPWD00000000000000";
90
91static const char kContentName[] = "test content";
92
Honghai Zhangc67e0f52016-09-19 16:57:37 -070093static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094static const char kTurnUsername[] = "test";
95static const char kTurnPassword[] = "test";
96
pthatcher94a2f212017-02-08 14:42:22 -080097// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070098// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080099static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -0700100
Qingsi Wang4ff54432018-03-01 18:25:20 -0800101namespace {
102
103void CheckStunKeepaliveIntervalOfAllReadyPorts(
104 const cricket::PortAllocatorSession* allocator_session,
105 int expected) {
106 auto ready_ports = allocator_session->ReadyPorts();
107 for (const auto* port : ready_ports) {
108 if (port->Type() == cricket::STUN_PORT_TYPE ||
109 (port->Type() == cricket::LOCAL_PORT_TYPE &&
110 port->GetProtocol() == cricket::PROTO_UDP)) {
111 EXPECT_EQ(
112 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
113 expected);
114 }
115 }
116}
117
118} // namespace
119
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120namespace cricket {
121
122// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700123std::ostream& operator<<(std::ostream& os,
124 const std::vector<Candidate>& candidates) {
125 os << '[';
126 bool first = true;
127 for (const Candidate& c : candidates) {
128 if (!first) {
129 os << ", ";
130 }
131 os << c.ToString();
132 first = false;
Steve Anton6c38cc72017-11-29 10:25:58 -0800133 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700134 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 return os;
136}
137
pthatcher1749bc32017-02-08 13:18:00 -0800138class BasicPortAllocatorTestBase : public testing::Test,
139 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 public:
pthatcher1749bc32017-02-08 13:18:00 -0800141 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700142 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700144 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800145 // Note that the NAT is not used by default. ResetWithStunServerAndNat
146 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700147 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700148 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700149 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700150 relay_server_(Thread::Current(),
151 kRelayUdpIntAddr,
152 kRelayUdpExtAddr,
153 kRelayTcpIntAddr,
154 kRelayTcpExtAddr,
155 kRelaySslTcpIntAddr,
156 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
158 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700159 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 stun_servers.insert(kStunAddr);
161 // Passing the addresses of GTURN servers will enable GTURN in
162 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800163 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
164 // configuration should be blank by default (preferred), or it should use
165 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700166 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
167 kRelayUdpIntAddr, kRelayTcpIntAddr,
168 kRelaySslTcpIntAddr));
169 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 }
171
172 void AddInterface(const SocketAddress& addr) {
173 network_manager_.AddInterface(addr);
174 }
honghaiz8c404fa2015-09-28 07:59:43 -0700175 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
176 network_manager_.AddInterface(addr, if_name);
177 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800178 void AddInterface(const SocketAddress& addr,
179 const std::string& if_name,
180 rtc::AdapterType type) {
181 network_manager_.AddInterface(addr, if_name, type);
182 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800183 // The default route is the public address that STUN server will observe when
184 // the endpoint is sitting on the public internet and the local port is bound
185 // to the "any" address. This may be different from the default local address
186 // which the endpoint observes. This can occur if the route to the public
187 // endpoint like 8.8.8.8 (specified as the default local address) is
188 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700189 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
190 AddInterface(addr);
191 // When a binding comes from the any address, the |addr| will be used as the
192 // srflx address.
193 vss_->SetDefaultRoute(addr.ipaddr());
194 }
honghaiz8c404fa2015-09-28 07:59:43 -0700195 void RemoveInterface(const SocketAddress& addr) {
196 network_manager_.RemoveInterface(addr);
197 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 bool SetPortRange(int min_port, int max_port) {
199 return allocator_->SetPortRange(min_port, max_port);
200 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700201 // Endpoint is on the public network. No STUN or TURN.
202 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700203 allocator_.reset(new BasicPortAllocator(&network_manager_));
204 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700205 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700206 // Endpoint is behind a NAT, with STUN specified.
207 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
208 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700209 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700210 // Endpoint is on the public network, with STUN specified.
211 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
212 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700214 // Endpoint is on the public network, with TURN specified.
215 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
216 const rtc::SocketAddress& tcp_turn) {
217 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218 AddTurnServers(udp_turn, tcp_turn);
219 }
220
221 void AddTurnServers(const rtc::SocketAddress& udp_turn,
222 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700223 RelayServerConfig turn_server(RELAY_TURN);
224 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800225 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226
227 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800228 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 }
230 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800231 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 }
deadbeef653b8e02015-11-11 12:55:10 -0800233 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 }
235
236 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700237 session_ = CreateSession("session", component);
238 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700240 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 return true;
242 }
243
244 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700245 session_ = CreateSession("session", content_name, component);
246 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700248 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 return true;
250 }
251
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700252 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
253 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 return CreateSession(sid, kContentName, component);
255 }
256
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700257 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700258 const std::string& sid,
259 const std::string& content_name,
260 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000261 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
262 }
263
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700264 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700265 const std::string& sid,
266 const std::string& content_name,
267 int component,
268 const std::string& ice_ufrag,
269 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700270 std::unique_ptr<PortAllocatorSession> session =
271 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800273 &BasicPortAllocatorTestBase::OnPortReady);
274 session->SignalPortsPruned.connect(
275 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700276 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800277 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700278 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800279 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700280 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800281 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000282 return session;
283 }
284
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700285 // Return true if the addresses are the same, or the port is 0 in |pattern|
286 // (acting as a wildcard) and the IPs are the same.
287 // Even with a wildcard port, the port of the address should be nonzero if
288 // the IP is nonzero.
289 static bool AddressMatch(const SocketAddress& address,
290 const SocketAddress& pattern) {
291 return address.ipaddr() == pattern.ipaddr() &&
292 ((pattern.port() == 0 &&
293 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
294 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000295 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700296
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700297 // Returns the number of ports that have matching type, protocol and
298 // address.
299 static int CountPorts(const std::vector<PortInterface*>& ports,
300 const std::string& type,
301 ProtocolType protocol,
302 const SocketAddress& client_addr) {
303 return std::count_if(
304 ports.begin(), ports.end(),
305 [type, protocol, client_addr](PortInterface* port) {
306 return port->Type() == type && port->GetProtocol() == protocol &&
307 port->Network()->GetBestIP() == client_addr.ipaddr();
308 });
309 }
310
deadbeefe97389c2016-12-23 01:43:45 -0800311 static int CountCandidates(const std::vector<Candidate>& candidates,
312 const std::string& type,
313 const std::string& proto,
314 const SocketAddress& addr) {
315 return std::count_if(candidates.begin(), candidates.end(),
316 [type, proto, addr](const Candidate& c) {
317 return c.type() == type && c.protocol() == proto &&
318 AddressMatch(c.address(), addr);
319 });
320 }
321
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700322 // Find a candidate and return it.
323 static bool FindCandidate(const std::vector<Candidate>& candidates,
324 const std::string& type,
325 const std::string& proto,
326 const SocketAddress& addr,
327 Candidate* found) {
328 auto it = std::find_if(candidates.begin(), candidates.end(),
329 [type, proto, addr](const Candidate& c) {
330 return c.type() == type && c.protocol() == proto &&
331 AddressMatch(c.address(), addr);
332 });
333 if (it != candidates.end() && found) {
334 *found = *it;
335 }
336 return it != candidates.end();
337 }
338
339 // Convenience method to call FindCandidate with no return.
340 static bool HasCandidate(const std::vector<Candidate>& candidates,
341 const std::string& type,
342 const std::string& proto,
343 const SocketAddress& addr) {
344 return FindCandidate(candidates, type, proto, addr, nullptr);
345 }
346
347 // Version of HasCandidate that also takes a related address.
348 static bool HasCandidateWithRelatedAddr(
349 const std::vector<Candidate>& candidates,
350 const std::string& type,
351 const std::string& proto,
352 const SocketAddress& addr,
353 const SocketAddress& related_addr) {
354 auto it =
355 std::find_if(candidates.begin(), candidates.end(),
356 [type, proto, addr, related_addr](const Candidate& c) {
357 return c.type() == type && c.protocol() == proto &&
358 AddressMatch(c.address(), addr) &&
359 AddressMatch(c.related_address(), related_addr);
360 });
361 return it != candidates.end();
362 }
363
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 int min_port,
366 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000367 return (addr.port() >= min_port && addr.port() <= max_port);
368 }
369
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700370 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 // We should only get this callback once, except in the mux test where
372 // we have multiple port allocation sessions.
373 if (session == session_.get()) {
374 ASSERT_FALSE(candidate_allocation_done_);
375 candidate_allocation_done_ = true;
376 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700377 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378 }
379
380 // Check if all ports allocated have send-buffer size |expected|. If
381 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
382 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700383 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384 for (it = ports_.begin(); it < ports_.end(); ++it) {
385 int send_buffer_size;
386 if (expected == -1) {
387 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700388 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 EXPECT_EQ(0,
391 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000392 ASSERT_EQ(expected, send_buffer_size);
393 }
394 }
395 }
396
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700397 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
398
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000399 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700400 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700402 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100403 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000404 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700405 // Make sure the new port is added to ReadyPorts.
406 auto ready_ports = ses->ReadyPorts();
407 EXPECT_NE(ready_ports.end(),
408 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700410 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700411 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100412 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700413 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700414 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700415 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700416 new_end = std::remove(ports_.begin(), new_end, port);
417 // Make sure the pruned port is not in ReadyPorts.
418 EXPECT_EQ(ready_ports.end(),
419 std::find(ready_ports.begin(), ready_ports.end(), port));
420 }
421 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700422 }
423
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700424 void OnCandidatesReady(PortAllocatorSession* ses,
425 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700426 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100427 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700428 // Sanity check that the ICE component is set.
429 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
430 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000431 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700432 // Make sure the new candidates are added to Candidates.
433 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700434 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700435 EXPECT_NE(
436 ses_candidates.end(),
437 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
438 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439 }
440
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700441 void OnCandidatesRemoved(PortAllocatorSession* session,
442 const std::vector<Candidate>& removed_candidates) {
443 auto new_end = std::remove_if(
444 candidates_.begin(), candidates_.end(),
445 [removed_candidates](Candidate& candidate) {
446 for (const Candidate& removed_candidate : removed_candidates) {
447 if (candidate.MatchesForRemoval(removed_candidate)) {
448 return true;
449 }
450 }
451 return false;
452 });
453 candidates_.erase(new_end, candidates_.end());
454 }
455
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700456 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800457 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700458 RelayServerConfig server_config = allocator_->turn_servers()[i];
459 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700461 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000462 if (proto_addr.address == relay_port->address &&
463 proto_addr.proto == relay_port->proto)
464 return true;
465 }
466 }
467 return false;
468 }
469
Guo-wei Shieh11477022015-08-15 09:28:41 -0700470 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
471 bool with_nat) {
472 if (with_nat) {
473 nat_server_.reset(new rtc::NATServer(
474 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
475 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
476 } else {
477 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
478 }
479
480 ServerAddresses stun_servers;
481 if (!stun_server.IsNil()) {
482 stun_servers.insert(stun_server);
483 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700484 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700485 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700486 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700487 }
488
pthatcher1749bc32017-02-08 13:18:00 -0800489 std::unique_ptr<rtc::VirtualSocketServer> vss_;
490 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700491 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800492 std::unique_ptr<rtc::NATServer> nat_server_;
493 rtc::NATSocketFactory nat_factory_;
494 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
495 std::unique_ptr<TestStunServer> stun_server_;
496 TestRelayServer relay_server_;
497 TestTurnServer turn_server_;
498 rtc::FakeNetworkManager network_manager_;
499 std::unique_ptr<BasicPortAllocator> allocator_;
500 std::unique_ptr<PortAllocatorSession> session_;
501 std::vector<PortInterface*> ports_;
502 std::vector<Candidate> candidates_;
503 bool candidate_allocation_done_;
504};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700505
pthatcher1749bc32017-02-08 13:18:00 -0800506class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
507};
508
509class FakeClockBase {
510 public:
511 rtc::ScopedFakeClock fake_clock;
512};
513
514class BasicPortAllocatorTest : public FakeClockBase,
515 public BasicPortAllocatorTestBase {
516 public:
517 // This function starts the port/address gathering and check the existence of
518 // candidates as specified. When |expect_stun_candidate| is true,
519 // |stun_candidate_addr| carries the expected reflective address, which is
520 // also the related address for TURN candidate if it is expected. Otherwise,
521 // it should be ignore.
522 void CheckDisableAdapterEnumeration(
523 uint32_t total_ports,
524 const rtc::IPAddress& host_candidate_addr,
525 const rtc::IPAddress& stun_candidate_addr,
526 const rtc::IPAddress& relay_candidate_udp_transport_addr,
527 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
528 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
529 rtc::IPAddress());
530 if (!session_) {
531 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
532 }
533 session_->set_flags(session_->flags() |
534 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
535 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
536 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700537 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800538 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
539 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700540
pthatcher1749bc32017-02-08 13:18:00 -0800541 uint32_t total_candidates = 0;
542 if (!host_candidate_addr.IsNil()) {
543 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
544 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
545 ++total_candidates;
546 }
547 if (!stun_candidate_addr.IsNil()) {
548 rtc::SocketAddress related_address(host_candidate_addr, 0);
549 if (host_candidate_addr.IsNil()) {
550 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
551 }
552 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
553 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
554 ++total_candidates;
555 }
556 if (!relay_candidate_udp_transport_addr.IsNil()) {
557 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
558 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
559 rtc::SocketAddress(stun_candidate_addr, 0));
560 ++total_candidates;
561 }
562 if (!relay_candidate_tcp_transport_addr.IsNil()) {
563 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
564 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
565 rtc::SocketAddress(stun_candidate_addr, 0));
566 ++total_candidates;
567 }
568
569 EXPECT_EQ(total_candidates, candidates_.size());
570 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700571 }
572
573 void TestIPv6TurnPortPrunesIPv4TurnPort() {
574 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
575 // Add two IP addresses on the same interface.
576 AddInterface(kClientAddr, "net1");
577 AddInterface(kClientIPv6Addr, "net1");
578 allocator_.reset(new BasicPortAllocator(&network_manager_));
579 allocator_->SetConfiguration(allocator_->stun_servers(),
580 allocator_->turn_servers(), 0, true);
581 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
582 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
583
584 allocator_->set_step_delay(kMinimumStepDelay);
585 allocator_->set_flags(
586 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
587 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
588
589 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
590 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800591 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
592 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700593 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
594 EXPECT_EQ(3U, session_->ReadyPorts().size());
595 EXPECT_EQ(3U, ports_.size());
596 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
597 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
598 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
599 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
600
601 // Now that we remove candidates when a TURN port is pruned, there will be
602 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
603 EXPECT_EQ(3U, candidates_.size());
604 const std::vector<Candidate>& ready_candidates =
605 session_->ReadyCandidates();
606 EXPECT_EQ(3U, ready_candidates.size());
607 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
608 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
609 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
610 }
611
pthatcher1749bc32017-02-08 13:18:00 -0800612 void TestUdpTurnPortPrunesTcpTurnPort() {
613 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
614 AddInterface(kClientAddr);
615 allocator_.reset(new BasicPortAllocator(&network_manager_));
616 allocator_->SetConfiguration(allocator_->stun_servers(),
617 allocator_->turn_servers(), 0, true);
618 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
619 allocator_->set_step_delay(kMinimumStepDelay);
620 allocator_->set_flags(allocator().flags() |
621 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
622 PORTALLOCATOR_DISABLE_TCP);
623
624 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
625 session_->StartGettingPorts();
626 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
627 kDefaultAllocationTimeout, fake_clock);
628 // Only 2 ports (one STUN and one TURN) are actually being used.
629 EXPECT_EQ(2U, session_->ReadyPorts().size());
630 // We have verified that each port, when it is added to |ports_|, it is
631 // found in |ready_ports|, and when it is pruned, it is not found in
632 // |ready_ports|, so we only need to verify the content in one of them.
633 EXPECT_EQ(2U, ports_.size());
634 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
635 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
636 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
637
638 // Now that we remove candidates when a TURN port is pruned, |candidates_|
639 // should only contains two candidates regardless whether the TCP TURN port
640 // is created before or after the UDP turn port.
641 EXPECT_EQ(2U, candidates_.size());
642 // There will only be 2 candidates in |ready_candidates| because it only
643 // includes the candidates in the ready ports.
644 const std::vector<Candidate>& ready_candidates =
645 session_->ReadyCandidates();
646 EXPECT_EQ(2U, ready_candidates.size());
647 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
648 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
649 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
650 }
651
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700652 void TestEachInterfaceHasItsOwnTurnPorts() {
653 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
654 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
655 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
656 // Add two interfaces both having IPv4 and IPv6 addresses.
657 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
658 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
659 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
660 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
661 allocator_.reset(new BasicPortAllocator(&network_manager_));
662 allocator_->SetConfiguration(allocator_->stun_servers(),
663 allocator_->turn_servers(), 0, true);
664 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
665 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
666 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
667
668 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800669 allocator_->set_flags(
670 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
671 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700672 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
673 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800674 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
675 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700676 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
677 // use.
678 EXPECT_EQ(10U, session_->ReadyPorts().size());
679 EXPECT_EQ(10U, ports_.size());
680 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
681 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
682 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
683 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
684 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
685 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
686 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
687 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
688 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
689 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
690
691 // Now that we remove candidates when TURN ports are pruned, there will be
692 // exactly 10 candidates in |candidates_|.
693 EXPECT_EQ(10U, candidates_.size());
694 const std::vector<Candidate>& ready_candidates =
695 session_->ReadyCandidates();
696 EXPECT_EQ(10U, ready_candidates.size());
697 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
698 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
699 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
700 kClientIPv6Addr);
701 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
702 kClientIPv6Addr2);
703 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
704 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
705 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
706 kClientIPv6Addr);
707 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
708 kClientIPv6Addr2);
709 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
710 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
711 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000712};
713
714// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700715TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000716 EXPECT_EQ(&network_manager_, allocator().network_manager());
717 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800718 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700719 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000720 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800721 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
722 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700723 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
724 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
725 EXPECT_TRUE(
726 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
727 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700728 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000729}
730
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800731// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700732TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800733 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
734 rtc::ADAPTER_TYPE_ETHERNET);
735 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
736 rtc::ADAPTER_TYPE_WIFI);
737 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
738 rtc::ADAPTER_TYPE_CELLULAR);
739 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
740 rtc::ADAPTER_TYPE_VPN);
741 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
742 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700743 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
744 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
745 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800746 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800747 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
748 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700750 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 EXPECT_LT(candidate.address().ip(), 0x12345604U);
752 }
753}
754
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700755TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
757 rtc::ADAPTER_TYPE_ETHERNET);
758 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
759 rtc::ADAPTER_TYPE_WIFI);
760 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
761 rtc::ADAPTER_TYPE_CELLULAR);
762 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
763 rtc::ADAPTER_TYPE_LOOPBACK |
764 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700765 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
766 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
767 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800769 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
770 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800771 EXPECT_EQ(1U, candidates_.size());
772 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
773}
774
deadbeef1c46a352017-09-27 11:24:05 -0700775// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
776// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
777TEST_F(BasicPortAllocatorTest,
778 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
779 SocketAddress wifi(IPAddress(0x12345600U), 0);
780 SocketAddress cell(IPAddress(0x12345601U), 0);
781 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
782 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
783 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-05-31 18:29:12 -0700784 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
785 cricket::PORTALLOCATOR_DISABLE_RELAY |
786 cricket::PORTALLOCATOR_DISABLE_TCP |
787 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
788 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
789 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800790 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
791 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700792 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700793 EXPECT_EQ(1U, candidates_.size());
deadbeef1c46a352017-09-27 11:24:05 -0700794 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi);
795}
honghaiz60347052016-05-31 18:29:12 -0700796
deadbeef1c46a352017-09-27 11:24:05 -0700797// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
798// both "unknown" and cell interfaces are available, only the unknown are used.
799// The unknown interface may be something that ultimately uses Wi-Fi, so we do
800// this to be on the safe side.
801TEST_F(BasicPortAllocatorTest,
802 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
803 SocketAddress cell(IPAddress(0x12345601U), 0);
804 SocketAddress unknown1(IPAddress(0x12345602U), 0);
805 SocketAddress unknown2(IPAddress(0x12345603U), 0);
806 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
807 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
808 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
809 // Disable all but UDP candidates to make the test simpler.
810 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
811 cricket::PORTALLOCATOR_DISABLE_RELAY |
812 cricket::PORTALLOCATOR_DISABLE_TCP |
813 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
814 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700815 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800816 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
817 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700818 // Should only get two candidates, none of which is cell.
honghaiz60347052016-05-31 18:29:12 -0700819 EXPECT_EQ(2U, candidates_.size());
deadbeef1c46a352017-09-27 11:24:05 -0700820 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", unknown1);
821 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", unknown2);
822}
honghaiz60347052016-05-31 18:29:12 -0700823
deadbeef1c46a352017-09-27 11:24:05 -0700824// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
825// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
826// interface is used.
827TEST_F(BasicPortAllocatorTest,
828 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
829 SocketAddress wifi(IPAddress(0x12345600U), 0);
830 SocketAddress cellular(IPAddress(0x12345601U), 0);
831 SocketAddress unknown1(IPAddress(0x12345602U), 0);
832 SocketAddress unknown2(IPAddress(0x12345603U), 0);
833 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
834 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
835 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
836 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
837 // Disable all but UDP candidates to make the test simpler.
838 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
839 cricket::PORTALLOCATOR_DISABLE_RELAY |
840 cricket::PORTALLOCATOR_DISABLE_TCP |
841 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
842 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700843 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800844 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
845 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700846 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700847 EXPECT_EQ(1U, candidates_.size());
deadbeef1c46a352017-09-27 11:24:05 -0700848 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi);
849}
850
851// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
852// only interface available is cellular, it ends up used anyway. A costly
853// connection is always better than no connection.
854TEST_F(BasicPortAllocatorTest,
855 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
856 SocketAddress cellular(IPAddress(0x12345601U), 0);
857 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
858 // Disable all but UDP candidates to make the test simpler.
859 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
860 cricket::PORTALLOCATOR_DISABLE_RELAY |
861 cricket::PORTALLOCATOR_DISABLE_TCP |
862 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
863 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
864 session_->StartGettingPorts();
865 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
866 kDefaultAllocationTimeout, fake_clock);
867 // Make sure we got the cell candidate.
868 EXPECT_EQ(1U, candidates_.size());
869 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", cellular);
honghaiz60347052016-05-31 18:29:12 -0700870}
871
Yuwei Huangb181f712018-01-22 17:01:28 -0800872// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
873// a WiFi network with link-local IP address and a cellular network, then the
874// cellular candidate will still be gathered.
875TEST_F(BasicPortAllocatorTest,
876 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
877 SocketAddress wifi_link_local("169.254.0.1", 0);
878 SocketAddress cellular(IPAddress(0x12345601U), 0);
879 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
880 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
881
882 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
883 cricket::PORTALLOCATOR_DISABLE_RELAY |
884 cricket::PORTALLOCATOR_DISABLE_TCP |
885 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
886 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
887 session_->StartGettingPorts();
888 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
889 kDefaultAllocationTimeout, fake_clock);
890 // Make sure we got both wifi and cell candidates.
891 EXPECT_EQ(2U, candidates_.size());
892 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi_link_local);
893 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", cellular);
894}
895
896// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
897// a WiFi network with link-local IP address, a WiFi network with a normal IP
898// address and a cellular network, then the cellular candidate will not be
899// gathered.
900TEST_F(BasicPortAllocatorTest,
901 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
902 SocketAddress wifi(IPAddress(0x12345600U), 0);
903 SocketAddress wifi_link_local("169.254.0.1", 0);
904 SocketAddress cellular(IPAddress(0x12345601U), 0);
905 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
906 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
907 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
908
909 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
910 cricket::PORTALLOCATOR_DISABLE_RELAY |
911 cricket::PORTALLOCATOR_DISABLE_TCP |
912 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
913 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
914 session_->StartGettingPorts();
915 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
916 kDefaultAllocationTimeout, fake_clock);
917 // Make sure we got only wifi candidates.
918 EXPECT_EQ(2U, candidates_.size());
919 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi);
920 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi_link_local);
921}
922
deadbeef3427f532017-07-26 16:09:33 -0700923// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
924// to gather candidates.
925TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
926 // Add three IPv6 network interfaces, but tell the allocator to only use two.
927 allocator().set_max_ipv6_networks(2);
928 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
929 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
930 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
931
932 // To simplify the test, only gather UDP host candidates.
933 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
934 PORTALLOCATOR_DISABLE_STUN |
935 PORTALLOCATOR_DISABLE_RELAY);
936
937 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
938 session_->StartGettingPorts();
939 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
940 kDefaultAllocationTimeout, fake_clock);
941 EXPECT_EQ(2U, candidates_.size());
942 // Ensure the expected two interfaces (eth0 and eth1) were used.
943 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
944 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr2);
945}
946
947// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
948// being used.
949TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
950 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
951 allocator().set_max_ipv6_networks(1);
952 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
953 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
954 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
955 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
956
957 // To simplify the test, only gather UDP host candidates.
958 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
959 PORTALLOCATOR_DISABLE_STUN |
960 PORTALLOCATOR_DISABLE_RELAY);
961
962 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
963 session_->StartGettingPorts();
964 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
965 kDefaultAllocationTimeout, fake_clock);
966 EXPECT_EQ(3U, candidates_.size());
967 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
968 // were used.
969 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
970 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
971 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr2);
972}
973
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700974// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700975TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800976 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
977 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700978 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
979 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
980 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700981 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800982 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
983 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700984 EXPECT_EQ(1U, candidates_.size());
985}
986
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000987// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700988TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700990 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700992 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
993 kDefaultAllocationTimeout, fake_clock);
994 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000995 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700996 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
997 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
998 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
999 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1000 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1001 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1002 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1003 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001004}
1005
honghaiz8c404fa2015-09-28 07:59:43 -07001006// Test that when the same network interface is brought down and up, the
1007// port allocator session will restart a new allocation sequence if
1008// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001009TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001010 std::string if_name("test_net0");
1011 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001012 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001013 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001014 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1015 kDefaultAllocationTimeout, fake_clock);
1016 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001017 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001018 candidate_allocation_done_ = false;
1019 candidates_.clear();
1020 ports_.clear();
1021
deadbeef7f1563f2017-09-15 17:40:01 -07001022 // Disable socket creation to simulate the network interface being down. When
1023 // no network interfaces are available, BasicPortAllocator will fall back to
1024 // binding to the "ANY" address, so we need to make sure that fails too.
1025 fss_->set_tcp_sockets_enabled(false);
1026 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -07001027 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001028 SIMULATED_WAIT(false, 1000, fake_clock);
1029 EXPECT_EQ(0U, candidates_.size());
1030 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -07001031
1032 // When the same interfaces are added again, new candidates/ports should be
1033 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -07001034 fss_->set_tcp_sockets_enabled(true);
1035 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -07001036 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001037 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1038 kDefaultAllocationTimeout, fake_clock);
1039 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001040 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001041}
1042
1043// Test that when the same network interface is brought down and up, the
1044// port allocator session will not restart a new allocation sequence if
1045// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001046TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001047 std::string if_name("test_net0");
1048 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001049 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001050 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001051 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1052 kDefaultAllocationTimeout, fake_clock);
1053 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001054 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001055 session_->StopGettingPorts();
1056 candidates_.clear();
1057 ports_.clear();
1058
1059 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001060 // Wait one (simulated) second and then verify no new candidates have
1061 // appeared.
1062 SIMULATED_WAIT(false, 1000, fake_clock);
1063 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001064 EXPECT_EQ(0U, ports_.size());
1065
1066 // When the same interfaces are added again, new candidates/ports should not
1067 // be generated because the session has stopped.
1068 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001069 SIMULATED_WAIT(false, 1000, fake_clock);
1070 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001071 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001072}
1073
deadbeef1c46a352017-09-27 11:24:05 -07001074// Similar to the above tests, but tests a situation when sockets can't be
1075// bound to a network interface, then after a network change event can be.
1076// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1077TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1078 // Only test local ports to simplify test.
1079 ResetWithNoServersOrNat();
1080 // Provide a situation where the interface appears to be available, but
1081 // binding the sockets fails. See bug for description of when this can
1082 // happen.
1083 std::string if_name("test_net0");
1084 AddInterface(kClientAddr, if_name);
1085 fss_->set_tcp_sockets_enabled(false);
1086 fss_->set_udp_sockets_enabled(false);
1087 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1088 session_->StartGettingPorts();
1089 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1090 kDefaultAllocationTimeout, fake_clock);
1091 // Make sure we actually prevented candidates from being gathered (other than
1092 // a single TCP active candidate, since that doesn't require creating a
1093 // socket).
1094 ASSERT_EQ(1U, candidates_.size());
1095 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1096 candidate_allocation_done_ = false;
1097
1098 // Now simulate the interface coming up, with the newfound ability to bind
1099 // sockets.
1100 fss_->set_tcp_sockets_enabled(true);
1101 fss_->set_udp_sockets_enabled(true);
1102 AddInterface(kClientAddr, if_name);
1103 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1104 kDefaultAllocationTimeout, fake_clock);
1105 // Should get UDP and TCP candidate.
1106 ASSERT_EQ(2U, candidates_.size());
1107 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1108 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1109 // We should extend this test to also verify that a server candidate is
1110 // gathered.
1111 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1112}
1113
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001114// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001115TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001116 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001117 allocator_->set_step_delay(kDefaultStepDelay);
1118 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001119 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001120 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001121 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001122 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001123 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001124 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1125 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001126 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001127 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1128 kRelaySslTcpIntAddr);
deadbeef1c5e6d02017-09-15 17:46:56 -07001129 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
1130 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001131 EXPECT_EQ(4U, ports_.size());
1132 EXPECT_TRUE(candidate_allocation_done_);
1133 // If we Stop gathering now, we shouldn't get a second "done" callback.
1134 session_->StopGettingPorts();
1135}
1136
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001137TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001138 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001139 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001141 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1142 kDefaultAllocationTimeout, fake_clock);
1143 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001144 // If we Stop gathering now, we shouldn't get a second "done" callback.
1145 session_->StopGettingPorts();
1146
1147 // All ports should have unset send-buffer sizes.
1148 CheckSendBufferSizesOfAllPorts(-1);
1149}
1150
deadbeef7f1563f2017-09-15 17:40:01 -07001151// Tests that we can get callback after StopGetAllPorts when called in the
1152// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001153TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001155 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001157 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1158 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 EXPECT_EQ(2U, ports_.size());
1160 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001161 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1162 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001163}
1164
1165// Test that we restrict client ports appropriately when a port range is set.
1166// We check the candidates for udp/stun/tcp ports, and the from address
1167// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001168TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001169 AddInterface(kClientAddr);
1170 // Check that an invalid port range fails.
1171 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1172 // Check that a null port range succeeds.
1173 EXPECT_TRUE(SetPortRange(0, 0));
1174 // Check that a valid port range succeeds.
1175 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001176 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001177 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001178 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1179 kDefaultAllocationTimeout, fake_clock);
1180 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001181 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001182
1183 int num_nonrelay_candidates = 0;
1184 for (const Candidate& candidate : candidates_) {
1185 // Check the port number for the UDP/STUN/TCP port objects.
1186 if (candidate.type() != RELAY_PORT_TYPE) {
1187 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
1188 ++num_nonrelay_candidates;
1189 }
1190 }
1191 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001193 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
1194 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195}
1196
deadbeefe97389c2016-12-23 01:43:45 -08001197// Test that if we have no network adapters, we bind to the ANY address and
1198// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001199TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001200 // Default config uses GTURN and no NAT, so replace that with the
1201 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1202 ResetWithStunServerAndNat(kStunAddr);
1203 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1204 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1205 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1206 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1207 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1208 // TODO(deadbeef): Fix this.
1209 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001210 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001211 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001212 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1213 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001214 EXPECT_EQ(4U, ports_.size());
1215 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1216 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1217 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1218 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1219 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1220 // The "any" address port should be in the signaled ready ports, but the host
1221 // candidate for it is useless and shouldn't be signaled. So we only have
1222 // STUN/TURN candidates.
1223 EXPECT_EQ(3U, candidates_.size());
1224 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1225 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1226 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1227 // server.
1228 EXPECT_EQ(2,
1229 CountCandidates(candidates_, "relay", "udp",
1230 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001231}
1232
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001233// Test that when enumeration is disabled, we should not have any ports when
1234// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001235TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001236 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001237 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001238 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001239 // Expect to see no ports and no candidates.
1240 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1241 rtc::IPAddress(), rtc::IPAddress());
1242}
1243
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001244// Test that even with multiple interfaces, the result should still be a single
1245// default private, one STUN and one TURN candidate since we bind to any address
1246// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001247TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001248 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001249 AddInterface(kPrivateAddr);
1250 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001251 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001252 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001253
1254 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1255 // address set and we have no IPv6 STUN server, there should be no IPv6
1256 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001257 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1258 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001259
1260 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1261 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1262 // TURN/UDP candidates.
1263 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001264 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1265 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001266}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001267
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001268// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1269// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001270TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001271 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001272 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001273 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001274 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001275 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1276 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1277 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1278 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001279 kTurnUdpExtAddr.ipaddr());
1280}
1281
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001282// Test that when adapter enumeration is disabled, for endpoints without
1283// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001284TEST_F(BasicPortAllocatorTest,
1285 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001286 ResetWithNoServersOrNat();
1287 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1288 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1289 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001290}
1291
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001292// Test that when adapter enumeration is disabled, with
1293// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1294// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001295TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001296 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1297 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001298 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1299 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001300 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1301 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001302 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001303 rtc::IPAddress(), rtc::IPAddress());
1304}
1305
1306// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001307// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1308// a NAT, there is no local candidate. However, this specified default route
1309// (kClientAddr) which was discovered when sending STUN requests, will become
1310// the srflx addresses.
1311TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001312 BasicPortAllocatorTest,
Steve Anton6c38cc72017-11-29 10:25:58 -08001313 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001314 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001315 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001316 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1317 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001318 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1319 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001320 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1321 rtc::IPAddress(), rtc::IPAddress());
1322}
1323
1324// Test that when adapter enumeration is disabled, with
1325// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1326// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001327TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001328 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1329 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001330 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1331 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001332 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1333 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1334 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001335}
1336
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001337// Test that we disable relay over UDP, and only TCP is used when connecting to
1338// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001339TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001340 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001341 AddInterface(kClientAddr);
1342 ResetWithStunServerAndNat(kStunAddr);
1343 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001344 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1345 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1346 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1347 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001348
1349 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001350 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1351 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001352
1353 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1354 // TURN/TCP candidates.
1355 EXPECT_EQ(2U, ports_.size());
1356 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001357 Candidate turn_candidate;
1358 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1359 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001360 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001361 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1362 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001363}
1364
Erik Språngefdce692015-06-05 09:41:26 +02001365// Disable for asan, see
1366// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1367#if !defined(ADDRESS_SANITIZER)
1368
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001369// Test that we can get OnCandidatesAllocationDone callback when all the ports
1370// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001371TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001372 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001373 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1374 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1375 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001376 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001377 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001378 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001379}
1380
1381// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001382TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001383 AddInterface(kClientAddr);
1384 fss_->set_udp_sockets_enabled(false);
1385 EXPECT_TRUE(CreateSession(1));
1386 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001387 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1388 kDefaultAllocationTimeout, fake_clock);
1389 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001390 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001391 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1392 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1393 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1394 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1395 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1396 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001397}
1398
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001399#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001400
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001401// Test that we don't crash or malfunction if we can't create UDP sockets or
1402// listen on TCP sockets. We still give out a local TCP address, since
1403// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001404TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405 AddInterface(kClientAddr);
1406 fss_->set_udp_sockets_enabled(false);
1407 fss_->set_tcp_listen_enabled(false);
1408 EXPECT_TRUE(CreateSession(1));
1409 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001410 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1411 kDefaultAllocationTimeout, fake_clock);
1412 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001414 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1415 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1416 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1417 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1418 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001419 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001420}
1421
1422// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001423// TODO(deadbeef): Find a way to exit early here.
1424TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001425 AddInterface(kClientAddr);
1426 fss_->set_tcp_sockets_enabled(false);
1427 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001428 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001429 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001430 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001431 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432 // In case of Relay, ports creation will succeed but sockets will fail.
1433 // There is no error reporting from RelayEntry to handle this failure.
1434}
1435
1436// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001437TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001438 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1439 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001440 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001442 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1443 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001445 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1446 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001447 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001448 // will be tried after about 3 seconds.
1449 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001451 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1452 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1453 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1454 kRelaySslTcpIntAddr);
1455 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001456 // We wait at least for a full STUN timeout, which
1457 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1458 // already passed (see above), we wait 3 seconds less than that.
1459 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1460 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001461}
1462
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001463TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001464 AddInterface(kClientAddr);
1465 AddInterface(kClientAddr2);
1466 // Allocating only host UDP ports. This is done purely for testing
1467 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001468 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1469 PORTALLOCATOR_DISABLE_RELAY);
1470 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001471 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001472 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1473 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001474 ASSERT_EQ(2U, candidates_.size());
1475 EXPECT_EQ(2U, ports_.size());
1476 // Candidates priorities should be different.
1477 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1478}
1479
1480// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001481TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001482 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001483 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001484 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001485 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1486 kDefaultAllocationTimeout, fake_clock);
1487 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001488 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001489 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001490}
1491
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001492// Test that the allocator session uses the candidate filter it's created with,
1493// rather than the filter of its parent allocator.
1494// The filter of the allocator should only affect the next gathering phase,
1495// according to JSEP, which means the *next* allocator session returned.
1496TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1497 AddInterface(kClientAddr);
1498 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1499 // Set candidate filter *after* creating the session. Should have no effect.
1500 allocator().set_candidate_filter(CF_RELAY);
1501 session_->StartGettingPorts();
1502 // 7 candidates and 4 ports is what we would normally get (see the
1503 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001504 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1505 kDefaultAllocationTimeout, fake_clock);
1506 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001507 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001508}
1509
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001510// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1511// This test also verifies that when the allocator is only allowed to use
1512// relay (i.e. IceTransportsType is relay), the raddr is an empty
1513// address with the correct family. This is to prevent any local
1514// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001515TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001516 AddInterface(kClientAddr);
1517 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001518 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001519 allocator().set_candidate_filter(CF_RELAY);
1520 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001521 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001522 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1523 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001524 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1525 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001526
1527 EXPECT_EQ(1U, candidates_.size());
1528 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001529 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1530 EXPECT_EQ(
1531 candidates_[0].related_address(),
1532 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001533}
1534
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001535TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001536 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001537 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1538 allocator().set_candidate_filter(CF_HOST);
1539 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001540 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001541 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1542 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001543 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1544 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001545 for (const Candidate& candidate : candidates_) {
1546 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001547 }
1548}
1549
1550// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001551TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001552 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001553 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001554
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001555 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1556 allocator().set_candidate_filter(CF_REFLEXIVE);
1557 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001558 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001559 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1560 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001561 // Host is behind NAT, no private address will be exposed. Hence only UDP
1562 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001563 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1564 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001565 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1566 EXPECT_EQ(
1567 candidates_[0].related_address(),
1568 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001569}
1570
1571// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001572TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001573 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001574 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1575 allocator().set_candidate_filter(CF_REFLEXIVE);
1576 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001577 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001578 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1579 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001580 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001581 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001582 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001583 for (const Candidate& candidate : candidates_) {
1584 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001585 }
1586}
1587
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001588// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001589TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001590 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001591 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001593 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1594 kDefaultAllocationTimeout, fake_clock);
1595 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001596 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1597 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1598 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001600 for (const Candidate& candidate : candidates_) {
1601 EXPECT_EQ(kIceUfrag0, candidate.username());
1602 EXPECT_EQ(kIcePwd0, candidate.password());
1603 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001604}
1605
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001606// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1607// is allocated for udp and stun. Also verify there is only one candidate
1608// (local) if stun candidate is same as local candidate, which will be the case
1609// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001610TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001611 AddInterface(kClientAddr);
1612 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001613 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1614 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001615 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001616 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1617 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001618 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001619 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001620 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1621 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001622}
1623
1624// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1625// is allocated for udp and stun. In this test we should expect both stun and
1626// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001627TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001628 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001629 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001630
1631 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001632 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1633 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001634 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001635 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1636 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001637 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001638 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1639 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1640 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001641 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1642 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001643 EXPECT_EQ(3U, candidates_.size());
1644}
1645
deadbeefc5d0d952015-07-16 10:22:21 -07001646// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001647TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001648 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001649 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001650 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001651
1652 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1653
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001654 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001655 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001656 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1657 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001659 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001660 session_->StartGettingPorts();
1661
deadbeef7f1563f2017-09-15 17:40:01 -07001662 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1663 kDefaultAllocationTimeout, fake_clock);
1664 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001665 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001666 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1667 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1668 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1669 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1670 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001671}
1672
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001673// Test that if prune_turn_ports is set, TCP TURN port will not be used
1674// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1675TEST_F(BasicPortAllocatorTest,
1676 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1677 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1678 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1679 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001680
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001681 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001682}
1683
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001684// Test that if prune_turn_ports is set, TCP TURN port will not be used
1685// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1686TEST_F(BasicPortAllocatorTest,
1687 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1688 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1689 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1690 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001691
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001692 TestUdpTurnPortPrunesTcpTurnPort();
1693}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001694
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001695// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1696// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1697TEST_F(BasicPortAllocatorTest,
1698 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1699 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1700 // first.
1701 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1702 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001703
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001704 TestIPv6TurnPortPrunesIPv4TurnPort();
1705}
1706
1707// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1708// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1709TEST_F(BasicPortAllocatorTest,
1710 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1711 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1712 // first.
1713 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1714 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1715
1716 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001717}
1718
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001719// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001720// will has its own set of TurnPorts based on their priorities, in the default
1721// case where no transit delay is set.
1722TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1723 TestEachInterfaceHasItsOwnTurnPorts();
1724}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001725
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001726// Tests that if prune_turn_ports is set, each network interface
1727// will has its own set of TurnPorts based on their priorities, given that
1728// IPv4/TCP TURN port becomes ready first.
1729TEST_F(BasicPortAllocatorTest,
1730 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1731 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1732 // becomes ready last.
1733 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1734 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1735 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1736 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001737
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001738 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001739}
1740
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001741// Testing DNS resolve for the TURN server, this will test AllocationSequence
1742// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001743// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1744// appears that it doesn't currently because of the DNS look up not
1745// using the fake clock.
1746TEST_F(BasicPortAllocatorTestWithRealClock,
1747 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001748 // This test relies on a real query for "localhost", so it won't work on an
1749 // IPv6-only machine.
1750 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001751 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001752 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001753 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001754 allocator_.reset(new BasicPortAllocator(&network_manager_));
1755 RelayServerConfig turn_server(RELAY_TURN);
1756 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001757 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001758 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001759 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001760 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001761
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001762 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001763 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001764 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1765 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001766
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001767 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001768 session_->StartGettingPorts();
1769
1770 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1771}
1772
1773// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1774// is allocated for udp/stun/turn. In this test we should expect all local,
1775// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001776TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001777 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001778 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001779
1780 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1781
1782 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001783 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1784 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001785
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001786 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001787 session_->StartGettingPorts();
1788
deadbeef7f1563f2017-09-15 17:40:01 -07001789 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1790 kDefaultAllocationTimeout, fake_clock);
1791 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001792 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001793 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1794 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1795 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1796 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1797 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001798 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1799 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001800 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001801 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1802 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001803 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1804 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1805}
1806
1807// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1808// server is also used as the STUN server, we should get 'local', 'stun', and
1809// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001810TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001811 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001812 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001813 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001814 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1815
1816 // Must set the step delay to 0 to make sure the relay allocation phase is
1817 // started before the STUN candidates are obtained, so that the STUN binding
1818 // response is processed when both StunPort and TurnPort exist to reproduce
1819 // webrtc issue 3537.
1820 allocator_->set_step_delay(0);
1821 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001822 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1823 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001824
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001825 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001826 session_->StartGettingPorts();
1827
deadbeef7f1563f2017-09-15 17:40:01 -07001828 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1829 kDefaultAllocationTimeout, fake_clock);
1830 EXPECT_EQ(3U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001831 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1832 Candidate stun_candidate;
1833 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1834 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1835 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1836 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1837 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001838
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001839 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001840 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1841 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001842 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1843 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1844}
1845
deadbeefc5d0d952015-07-16 10:22:21 -07001846// Test that when only a TCP TURN server is available, we do NOT use it as
1847// a UDP STUN server, as this could leak our IP address. Thus we should only
1848// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001849TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001850 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001851 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001852 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001853 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1854
1855 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001856 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1857 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001858
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001859 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001860 session_->StartGettingPorts();
1861
deadbeef7f1563f2017-09-15 17:40:01 -07001862 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1863 kDefaultAllocationTimeout, fake_clock);
1864 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001865 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001866 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1867 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1868 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001869 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1870 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1871}
1872
1873// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1874// TURN server is used as the STUN server and we get 'local', 'stun', and
1875// 'relay' candidates.
1876// TODO(deadbeef): Remove this test when support for non-shared socket mode
1877// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001878TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001879 AddInterface(kClientAddr);
1880 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001881 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001882 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1883
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001884 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001885
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001886 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001887 session_->StartGettingPorts();
1888
deadbeef7f1563f2017-09-15 17:40:01 -07001889 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1890 kDefaultAllocationTimeout, fake_clock);
1891 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001892 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001893 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1894 Candidate stun_candidate;
1895 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1896 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1897 Candidate turn_candidate;
1898 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1899 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1900 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001901 // Not using shared socket, so the STUN request's server reflexive address
1902 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001903 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001904
deadbeefc5d0d952015-07-16 10:22:21 -07001905 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1906 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1907 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1908}
1909
1910// Test that even when both a STUN and TURN server are configured, the TURN
1911// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001912TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001913 AddInterface(kClientAddr);
1914 // Configure with STUN server but destroy it, so we can ensure that it's
1915 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001916 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001917 stun_server_.reset();
1918 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1919
1920 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001921 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1922 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001923
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001924 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001925 session_->StartGettingPorts();
1926
pthatcher1749bc32017-02-08 13:18:00 -08001927 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1928 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001929 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1930 Candidate stun_candidate;
1931 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1932 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1933 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1934 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1935 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001936
1937 // Don't bother waiting for STUN timeout, since we already verified
1938 // that we got a STUN candidate from the TURN server.
1939}
1940
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001941// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1942// and fail to generate STUN candidate, local UDP candidate is generated
1943// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001944TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001945 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1946 PORTALLOCATOR_DISABLE_TCP |
1947 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001948 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1949 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001950 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001951 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001952 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1953 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001954 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001955 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001956 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001957 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1958 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001959 EXPECT_EQ(1U, candidates_.size());
1960}
1961
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001962// Test that when the NetworkManager doesn't have permission to enumerate
1963// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1964// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001965TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001966 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1967 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001968 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001969 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001970 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1971 PORTALLOCATOR_DISABLE_TCP |
1972 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1973 EXPECT_EQ(0U,
1974 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1975 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1976 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001977 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001978 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1979 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001980 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001981 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001982 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001983}
1984
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001985// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001986TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001987 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1988 PORTALLOCATOR_ENABLE_IPV6 |
1989 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001990 AddInterface(kClientIPv6Addr);
1991 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001992 allocator_->set_step_delay(kMinimumStepDelay);
1993 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001994 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001995 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08001996 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07001997 EXPECT_EQ(4U, ports_.size());
1998 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001999 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
2000 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
2001 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
2002 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002003}
honghaiz98db68f2015-09-29 07:58:17 -07002004
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002005TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002006 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002007 allocator_->set_step_delay(kDefaultStepDelay);
2008 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002009 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002010 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002011 EXPECT_EQ(2U, ports_.size());
2012 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002013 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002014
2015 // After stopping getting ports, adding a new interface will not start
2016 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002017 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002018 candidates_.clear();
2019 ports_.clear();
2020 candidate_allocation_done_ = false;
2021 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002022 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002023 EXPECT_EQ(0U, candidates_.size());
2024 EXPECT_EQ(0U, ports_.size());
2025}
2026
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002027TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002028 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002029 allocator_->set_step_delay(kDefaultStepDelay);
2030 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002031 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002032 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002033 EXPECT_EQ(2U, ports_.size());
2034 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002035 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002036
2037 // After clearing getting ports, adding a new interface will start getting
2038 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002039 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002040 candidates_.clear();
2041 ports_.clear();
2042 candidate_allocation_done_ = false;
2043 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002044 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002045 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002046 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2047 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002048}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002049
2050// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2051// a pooled session is taken out of the pool.
2052TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2053 AddInterface(kClientAddr);
2054 int pool_size = 1;
2055 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002056 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002057 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002058 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002059 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2060 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002061 // Expect that when TakePooledSession is called,
2062 // UpdateTransportInformationInternal will be called and the
2063 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2064 // candidates.
2065 session_ =
2066 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2067 ASSERT_NE(nullptr, session_.get());
2068 auto ready_ports = session_->ReadyPorts();
2069 auto candidates = session_->ReadyCandidates();
2070 EXPECT_FALSE(ready_ports.empty());
2071 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002072 for (const PortInterface* port_interface : ready_ports) {
2073 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002074 EXPECT_EQ(kContentName, port->content_name());
2075 EXPECT_EQ(1, port->component());
2076 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2077 EXPECT_EQ(kIcePwd0, port->password());
2078 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002079 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002080 EXPECT_EQ(1, candidate.component());
2081 EXPECT_EQ(kIceUfrag0, candidate.username());
2082 EXPECT_EQ(kIcePwd0, candidate.password());
2083 }
2084}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002085
2086// Test that a new candidate filter takes effect even on already-gathered
2087// candidates.
2088TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2089 AddInterface(kClientAddr);
2090 int pool_size = 1;
2091 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002092 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002093 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2094 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002095 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2096 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002097 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2098 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2099 allocator_->set_candidate_filter(CF_RELAY);
2100 // Assume that when TakePooledSession is called, the candidate filter will be
2101 // applied to the pooled session. This is tested by PortAllocatorTest.
2102 session_ =
2103 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2104 ASSERT_NE(nullptr, session_.get());
2105 auto candidates = session_->ReadyCandidates();
2106 auto ports = session_->ReadyPorts();
2107 // Sanity check that the number of candidates and ports decreased.
2108 EXPECT_GT(initial_candidates_size, candidates.size());
2109 EXPECT_GT(initial_ports_size, ports.size());
2110 for (const PortInterface* port : ports) {
2111 // Expect only relay ports.
2112 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2113 }
2114 for (const Candidate& candidate : candidates) {
2115 // Expect only relay candidates now that the filter is applied.
2116 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2117 // Expect that the raddr is emptied due to the CF_RELAY filter.
2118 EXPECT_EQ(candidate.related_address(),
2119 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2120 }
2121}
2122
Qingsi Wang4ff54432018-03-01 18:25:20 -08002123TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2124 const int pool_size = 1;
2125 const int expected_stun_keepalive_interval = 123;
2126 AddInterface(kClientAddr);
2127 allocator_->SetConfiguration(allocator_->stun_servers(),
2128 allocator_->turn_servers(), pool_size, false,
2129 nullptr, expected_stun_keepalive_interval);
2130 auto* pooled_session = allocator_->GetPooledSession();
2131 ASSERT_NE(nullptr, pooled_session);
2132 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2133 kDefaultAllocationTimeout, fake_clock);
2134 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2135 expected_stun_keepalive_interval);
2136}
2137
2138TEST_F(BasicPortAllocatorTest,
2139 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2140 const int pool_size = 1;
2141 AddInterface(kClientAddr);
2142 allocator_->SetConfiguration(allocator_->stun_servers(),
2143 allocator_->turn_servers(), pool_size, false,
2144 nullptr, 123 /* stun keepalive interval */);
2145 auto* pooled_session = allocator_->GetPooledSession();
2146 ASSERT_NE(nullptr, pooled_session);
2147 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2148 kDefaultAllocationTimeout, fake_clock);
2149 const int expected_stun_keepalive_interval = 321;
2150 allocator_->SetConfiguration(allocator_->stun_servers(),
2151 allocator_->turn_servers(), pool_size, false,
2152 nullptr, expected_stun_keepalive_interval);
2153 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2154 expected_stun_keepalive_interval);
2155}
2156
2157TEST_F(BasicPortAllocatorTest,
2158 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2159 const int pool_size = 1;
2160 const int expected_stun_keepalive_interval = 123;
2161 AddInterface(kClientAddr);
2162 allocator_->set_flags(allocator().flags() |
2163 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2164 allocator_->SetConfiguration(allocator_->stun_servers(),
2165 allocator_->turn_servers(), pool_size, false,
2166 nullptr, expected_stun_keepalive_interval);
2167 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2168 session_->StartGettingPorts();
2169 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2170 kDefaultAllocationTimeout, fake_clock);
2171 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2172 expected_stun_keepalive_interval);
2173}
2174
2175TEST_F(BasicPortAllocatorTest,
2176 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2177 const int pool_size = 1;
2178 const int expected_stun_keepalive_interval = 123;
2179 AddInterface(kClientAddr);
2180 allocator_->set_flags(allocator().flags() &
2181 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2182 allocator_->SetConfiguration(allocator_->stun_servers(),
2183 allocator_->turn_servers(), pool_size, false,
2184 nullptr, expected_stun_keepalive_interval);
2185 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2186 session_->StartGettingPorts();
2187 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2188 kDefaultAllocationTimeout, fake_clock);
2189 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2190 expected_stun_keepalive_interval);
2191}
2192
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002193} // namespace cricket