blob: d5b525477967703e2b98728b613fd4e0424aa02d [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"
17#include "p2p/base/testrelayserver.h"
18#include "p2p/base/teststunserver.h"
19#include "p2p/base/testturnserver.h"
20#include "p2p/client/basicportallocator.h"
21#include "rtc_base/fakeclock.h"
22#include "rtc_base/fakenetwork.h"
23#include "rtc_base/firewallsocketserver.h"
24#include "rtc_base/gunit.h"
25#include "rtc_base/helpers.h"
26#include "rtc_base/ipaddress.h"
27#include "rtc_base/logging.h"
28#include "rtc_base/natserver.h"
29#include "rtc_base/natsocketfactory.h"
30#include "rtc_base/nethelpers.h"
31#include "rtc_base/network.h"
32#include "rtc_base/socketaddress.h"
33#include "rtc_base/ssladapter.h"
34#include "rtc_base/thread.h"
35#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080037using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038using rtc::SocketAddress;
39using rtc::Thread;
40
Mirko Bonadei675513b2017-11-09 11:09:25 +010041#define MAYBE_SKIP_IPV4 \
42 if (!rtc::HasIPv4Enabled()) { \
43 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
44 return; \
deadbeef9a6f4d42017-05-15 19:43:33 -070045 }
46
deadbeefe97389c2016-12-23 01:43:45 -080047static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070049static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070050static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000052static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070053static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
54 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070055static const SocketAddress kClientIPv6Addr2(
56 "2401:fa00:4:2000:be30:5bff:fee5:c3",
57 0);
deadbeef3427f532017-07-26 16:09:33 -070058static const SocketAddress kClientIPv6Addr3(
59 "2401:fa00:4:3000:be30:5bff:fee5:c3",
60 0);
deadbeefc5d0d952015-07-16 10:22:21 -070061static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
62static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
64static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
65static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
66static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
67static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
68static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
69static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
70static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
71static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070072static const SocketAddress kTurnUdpIntIPv6Addr(
73 "2402:fb00:4:1000:be30:5bff:fee5:c3",
74 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070076static const SocketAddress kTurnTcpIntIPv6Addr(
77 "2402:fb00:4:2000:be30:5bff:fee5:c3",
78 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
80
81// Minimum and maximum port for port range tests.
82static const int kMinPort = 10000;
83static const int kMaxPort = 10099;
84
85// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070086static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087// Based on ICE_PWD_LENGTH
88static const char kIcePwd0[] = "TESTICEPWD00000000000000";
89
90static const char kContentName[] = "test content";
91
Honghai Zhangc67e0f52016-09-19 16:57:37 -070092static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093static const char kTurnUsername[] = "test";
94static const char kTurnPassword[] = "test";
95
pthatcher94a2f212017-02-08 14:42:22 -080096// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070097// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080098static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070099
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100namespace cricket {
101
102// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700103std::ostream& operator<<(std::ostream& os,
104 const std::vector<Candidate>& candidates) {
105 os << '[';
106 bool first = true;
107 for (const Candidate& c : candidates) {
108 if (!first) {
109 os << ", ";
110 }
111 os << c.ToString();
112 first = false;
Steve Anton6c38cc72017-11-29 10:25:58 -0800113 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700114 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 return os;
116}
117
pthatcher1749bc32017-02-08 13:18:00 -0800118class BasicPortAllocatorTestBase : public testing::Test,
119 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 public:
pthatcher1749bc32017-02-08 13:18:00 -0800121 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700122 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700124 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800125 // Note that the NAT is not used by default. ResetWithStunServerAndNat
126 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700127 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700128 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700129 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700130 relay_server_(Thread::Current(),
131 kRelayUdpIntAddr,
132 kRelayUdpExtAddr,
133 kRelayTcpIntAddr,
134 kRelayTcpExtAddr,
135 kRelaySslTcpIntAddr,
136 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
138 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700139 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 stun_servers.insert(kStunAddr);
141 // Passing the addresses of GTURN servers will enable GTURN in
142 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800143 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
144 // configuration should be blank by default (preferred), or it should use
145 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700146 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
147 kRelayUdpIntAddr, kRelayTcpIntAddr,
148 kRelaySslTcpIntAddr));
149 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150 }
151
152 void AddInterface(const SocketAddress& addr) {
153 network_manager_.AddInterface(addr);
154 }
honghaiz8c404fa2015-09-28 07:59:43 -0700155 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
156 network_manager_.AddInterface(addr, if_name);
157 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800158 void AddInterface(const SocketAddress& addr,
159 const std::string& if_name,
160 rtc::AdapterType type) {
161 network_manager_.AddInterface(addr, if_name, type);
162 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800163 // The default route is the public address that STUN server will observe when
164 // the endpoint is sitting on the public internet and the local port is bound
165 // to the "any" address. This may be different from the default local address
166 // which the endpoint observes. This can occur if the route to the public
167 // endpoint like 8.8.8.8 (specified as the default local address) is
168 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700169 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
170 AddInterface(addr);
171 // When a binding comes from the any address, the |addr| will be used as the
172 // srflx address.
173 vss_->SetDefaultRoute(addr.ipaddr());
174 }
honghaiz8c404fa2015-09-28 07:59:43 -0700175 void RemoveInterface(const SocketAddress& addr) {
176 network_manager_.RemoveInterface(addr);
177 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000178 bool SetPortRange(int min_port, int max_port) {
179 return allocator_->SetPortRange(min_port, max_port);
180 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700181 // Endpoint is on the public network. No STUN or TURN.
182 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700183 allocator_.reset(new BasicPortAllocator(&network_manager_));
184 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700185 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700186 // Endpoint is behind a NAT, with STUN specified.
187 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
188 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700189 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700190 // Endpoint is on the public network, with STUN specified.
191 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
192 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700194 // Endpoint is on the public network, with TURN specified.
195 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
196 const rtc::SocketAddress& tcp_turn) {
197 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 AddTurnServers(udp_turn, tcp_turn);
199 }
200
201 void AddTurnServers(const rtc::SocketAddress& udp_turn,
202 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700203 RelayServerConfig turn_server(RELAY_TURN);
204 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800205 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206
207 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800208 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 }
210 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800211 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 }
deadbeef653b8e02015-11-11 12:55:10 -0800213 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 }
215
216 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700217 session_ = CreateSession("session", component);
218 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700220 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return true;
222 }
223
224 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 session_ = CreateSession("session", content_name, component);
226 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700228 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 return true;
230 }
231
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700232 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
233 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 return CreateSession(sid, kContentName, component);
235 }
236
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700237 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700238 const std::string& sid,
239 const std::string& content_name,
240 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
242 }
243
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700244 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700245 const std::string& sid,
246 const std::string& content_name,
247 int component,
248 const std::string& ice_ufrag,
249 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700250 std::unique_ptr<PortAllocatorSession> session =
251 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800253 &BasicPortAllocatorTestBase::OnPortReady);
254 session->SignalPortsPruned.connect(
255 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700256 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800257 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700258 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800259 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700260 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800261 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 return session;
263 }
264
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700265 // Return true if the addresses are the same, or the port is 0 in |pattern|
266 // (acting as a wildcard) and the IPs are the same.
267 // Even with a wildcard port, the port of the address should be nonzero if
268 // the IP is nonzero.
269 static bool AddressMatch(const SocketAddress& address,
270 const SocketAddress& pattern) {
271 return address.ipaddr() == pattern.ipaddr() &&
272 ((pattern.port() == 0 &&
273 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
274 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700276
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700277 // Returns the number of ports that have matching type, protocol and
278 // address.
279 static int CountPorts(const std::vector<PortInterface*>& ports,
280 const std::string& type,
281 ProtocolType protocol,
282 const SocketAddress& client_addr) {
283 return std::count_if(
284 ports.begin(), ports.end(),
285 [type, protocol, client_addr](PortInterface* port) {
286 return port->Type() == type && port->GetProtocol() == protocol &&
287 port->Network()->GetBestIP() == client_addr.ipaddr();
288 });
289 }
290
deadbeefe97389c2016-12-23 01:43:45 -0800291 static int CountCandidates(const std::vector<Candidate>& candidates,
292 const std::string& type,
293 const std::string& proto,
294 const SocketAddress& addr) {
295 return std::count_if(candidates.begin(), candidates.end(),
296 [type, proto, addr](const Candidate& c) {
297 return c.type() == type && c.protocol() == proto &&
298 AddressMatch(c.address(), addr);
299 });
300 }
301
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700302 // Find a candidate and return it.
303 static bool FindCandidate(const std::vector<Candidate>& candidates,
304 const std::string& type,
305 const std::string& proto,
306 const SocketAddress& addr,
307 Candidate* found) {
308 auto it = std::find_if(candidates.begin(), candidates.end(),
309 [type, proto, addr](const Candidate& c) {
310 return c.type() == type && c.protocol() == proto &&
311 AddressMatch(c.address(), addr);
312 });
313 if (it != candidates.end() && found) {
314 *found = *it;
315 }
316 return it != candidates.end();
317 }
318
319 // Convenience method to call FindCandidate with no return.
320 static bool HasCandidate(const std::vector<Candidate>& candidates,
321 const std::string& type,
322 const std::string& proto,
323 const SocketAddress& addr) {
324 return FindCandidate(candidates, type, proto, addr, nullptr);
325 }
326
327 // Version of HasCandidate that also takes a related address.
328 static bool HasCandidateWithRelatedAddr(
329 const std::vector<Candidate>& candidates,
330 const std::string& type,
331 const std::string& proto,
332 const SocketAddress& addr,
333 const SocketAddress& related_addr) {
334 auto it =
335 std::find_if(candidates.begin(), candidates.end(),
336 [type, proto, addr, related_addr](const Candidate& c) {
337 return c.type() == type && c.protocol() == proto &&
338 AddressMatch(c.address(), addr) &&
339 AddressMatch(c.related_address(), related_addr);
340 });
341 return it != candidates.end();
342 }
343
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700345 int min_port,
346 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000347 return (addr.port() >= min_port && addr.port() <= max_port);
348 }
349
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700350 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000351 // We should only get this callback once, except in the mux test where
352 // we have multiple port allocation sessions.
353 if (session == session_.get()) {
354 ASSERT_FALSE(candidate_allocation_done_);
355 candidate_allocation_done_ = true;
356 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700357 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000358 }
359
360 // Check if all ports allocated have send-buffer size |expected|. If
361 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
362 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700363 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 for (it = ports_.begin(); it < ports_.end(); ++it) {
365 int send_buffer_size;
366 if (expected == -1) {
367 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700368 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700370 EXPECT_EQ(0,
371 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000372 ASSERT_EQ(expected, send_buffer_size);
373 }
374 }
375 }
376
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700377 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
378
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000379 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700380 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700382 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100383 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700385 // Make sure the new port is added to ReadyPorts.
386 auto ready_ports = ses->ReadyPorts();
387 EXPECT_NE(ready_ports.end(),
388 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000389 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700390 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700391 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100392 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700393 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700394 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700395 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700396 new_end = std::remove(ports_.begin(), new_end, port);
397 // Make sure the pruned port is not in ReadyPorts.
398 EXPECT_EQ(ready_ports.end(),
399 std::find(ready_ports.begin(), ready_ports.end(), port));
400 }
401 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700402 }
403
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700404 void OnCandidatesReady(PortAllocatorSession* ses,
405 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700406 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100407 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700408 // Sanity check that the ICE component is set.
409 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
410 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700412 // Make sure the new candidates are added to Candidates.
413 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700414 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700415 EXPECT_NE(
416 ses_candidates.end(),
417 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
418 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000419 }
420
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700421 void OnCandidatesRemoved(PortAllocatorSession* session,
422 const std::vector<Candidate>& removed_candidates) {
423 auto new_end = std::remove_if(
424 candidates_.begin(), candidates_.end(),
425 [removed_candidates](Candidate& candidate) {
426 for (const Candidate& removed_candidate : removed_candidates) {
427 if (candidate.MatchesForRemoval(removed_candidate)) {
428 return true;
429 }
430 }
431 return false;
432 });
433 candidates_.erase(new_end, candidates_.end());
434 }
435
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700436 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800437 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700438 RelayServerConfig server_config = allocator_->turn_servers()[i];
439 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000440 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700441 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 if (proto_addr.address == relay_port->address &&
443 proto_addr.proto == relay_port->proto)
444 return true;
445 }
446 }
447 return false;
448 }
449
Guo-wei Shieh11477022015-08-15 09:28:41 -0700450 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
451 bool with_nat) {
452 if (with_nat) {
453 nat_server_.reset(new rtc::NATServer(
454 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
455 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
456 } else {
457 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
458 }
459
460 ServerAddresses stun_servers;
461 if (!stun_server.IsNil()) {
462 stun_servers.insert(stun_server);
463 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700464 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700465 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700466 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700467 }
468
pthatcher1749bc32017-02-08 13:18:00 -0800469 std::unique_ptr<rtc::VirtualSocketServer> vss_;
470 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700471 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800472 std::unique_ptr<rtc::NATServer> nat_server_;
473 rtc::NATSocketFactory nat_factory_;
474 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
475 std::unique_ptr<TestStunServer> stun_server_;
476 TestRelayServer relay_server_;
477 TestTurnServer turn_server_;
478 rtc::FakeNetworkManager network_manager_;
479 std::unique_ptr<BasicPortAllocator> allocator_;
480 std::unique_ptr<PortAllocatorSession> session_;
481 std::vector<PortInterface*> ports_;
482 std::vector<Candidate> candidates_;
483 bool candidate_allocation_done_;
484};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700485
pthatcher1749bc32017-02-08 13:18:00 -0800486class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
487};
488
489class FakeClockBase {
490 public:
491 rtc::ScopedFakeClock fake_clock;
492};
493
494class BasicPortAllocatorTest : public FakeClockBase,
495 public BasicPortAllocatorTestBase {
496 public:
497 // This function starts the port/address gathering and check the existence of
498 // candidates as specified. When |expect_stun_candidate| is true,
499 // |stun_candidate_addr| carries the expected reflective address, which is
500 // also the related address for TURN candidate if it is expected. Otherwise,
501 // it should be ignore.
502 void CheckDisableAdapterEnumeration(
503 uint32_t total_ports,
504 const rtc::IPAddress& host_candidate_addr,
505 const rtc::IPAddress& stun_candidate_addr,
506 const rtc::IPAddress& relay_candidate_udp_transport_addr,
507 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
508 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
509 rtc::IPAddress());
510 if (!session_) {
511 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
512 }
513 session_->set_flags(session_->flags() |
514 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
515 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
516 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700517 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800518 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
519 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700520
pthatcher1749bc32017-02-08 13:18:00 -0800521 uint32_t total_candidates = 0;
522 if (!host_candidate_addr.IsNil()) {
523 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
524 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
525 ++total_candidates;
526 }
527 if (!stun_candidate_addr.IsNil()) {
528 rtc::SocketAddress related_address(host_candidate_addr, 0);
529 if (host_candidate_addr.IsNil()) {
530 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
531 }
532 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
533 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
534 ++total_candidates;
535 }
536 if (!relay_candidate_udp_transport_addr.IsNil()) {
537 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
538 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
539 rtc::SocketAddress(stun_candidate_addr, 0));
540 ++total_candidates;
541 }
542 if (!relay_candidate_tcp_transport_addr.IsNil()) {
543 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
544 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
545 rtc::SocketAddress(stun_candidate_addr, 0));
546 ++total_candidates;
547 }
548
549 EXPECT_EQ(total_candidates, candidates_.size());
550 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700551 }
552
553 void TestIPv6TurnPortPrunesIPv4TurnPort() {
554 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
555 // Add two IP addresses on the same interface.
556 AddInterface(kClientAddr, "net1");
557 AddInterface(kClientIPv6Addr, "net1");
558 allocator_.reset(new BasicPortAllocator(&network_manager_));
559 allocator_->SetConfiguration(allocator_->stun_servers(),
560 allocator_->turn_servers(), 0, true);
561 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
562 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
563
564 allocator_->set_step_delay(kMinimumStepDelay);
565 allocator_->set_flags(
566 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
567 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
568
569 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
570 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800571 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
572 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700573 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
574 EXPECT_EQ(3U, session_->ReadyPorts().size());
575 EXPECT_EQ(3U, ports_.size());
576 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
577 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
578 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
579 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
580
581 // Now that we remove candidates when a TURN port is pruned, there will be
582 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
583 EXPECT_EQ(3U, candidates_.size());
584 const std::vector<Candidate>& ready_candidates =
585 session_->ReadyCandidates();
586 EXPECT_EQ(3U, ready_candidates.size());
587 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
588 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
589 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
590 }
591
pthatcher1749bc32017-02-08 13:18:00 -0800592 void TestUdpTurnPortPrunesTcpTurnPort() {
593 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
594 AddInterface(kClientAddr);
595 allocator_.reset(new BasicPortAllocator(&network_manager_));
596 allocator_->SetConfiguration(allocator_->stun_servers(),
597 allocator_->turn_servers(), 0, true);
598 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
599 allocator_->set_step_delay(kMinimumStepDelay);
600 allocator_->set_flags(allocator().flags() |
601 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
602 PORTALLOCATOR_DISABLE_TCP);
603
604 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
605 session_->StartGettingPorts();
606 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
607 kDefaultAllocationTimeout, fake_clock);
608 // Only 2 ports (one STUN and one TURN) are actually being used.
609 EXPECT_EQ(2U, session_->ReadyPorts().size());
610 // We have verified that each port, when it is added to |ports_|, it is
611 // found in |ready_ports|, and when it is pruned, it is not found in
612 // |ready_ports|, so we only need to verify the content in one of them.
613 EXPECT_EQ(2U, ports_.size());
614 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
615 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
616 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
617
618 // Now that we remove candidates when a TURN port is pruned, |candidates_|
619 // should only contains two candidates regardless whether the TCP TURN port
620 // is created before or after the UDP turn port.
621 EXPECT_EQ(2U, candidates_.size());
622 // There will only be 2 candidates in |ready_candidates| because it only
623 // includes the candidates in the ready ports.
624 const std::vector<Candidate>& ready_candidates =
625 session_->ReadyCandidates();
626 EXPECT_EQ(2U, ready_candidates.size());
627 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
628 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
629 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
630 }
631
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700632 void TestEachInterfaceHasItsOwnTurnPorts() {
633 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
634 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
635 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
636 // Add two interfaces both having IPv4 and IPv6 addresses.
637 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
638 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
639 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
640 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
641 allocator_.reset(new BasicPortAllocator(&network_manager_));
642 allocator_->SetConfiguration(allocator_->stun_servers(),
643 allocator_->turn_servers(), 0, true);
644 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
645 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
646 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
647
648 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800649 allocator_->set_flags(
650 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
651 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700652 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
653 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800654 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
655 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700656 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
657 // use.
658 EXPECT_EQ(10U, session_->ReadyPorts().size());
659 EXPECT_EQ(10U, ports_.size());
660 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
661 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
662 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
663 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
664 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
665 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
666 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
667 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
668 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
669 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
670
671 // Now that we remove candidates when TURN ports are pruned, there will be
672 // exactly 10 candidates in |candidates_|.
673 EXPECT_EQ(10U, candidates_.size());
674 const std::vector<Candidate>& ready_candidates =
675 session_->ReadyCandidates();
676 EXPECT_EQ(10U, ready_candidates.size());
677 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
678 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
679 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
680 kClientIPv6Addr);
681 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
682 kClientIPv6Addr2);
683 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
684 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
685 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
686 kClientIPv6Addr);
687 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
688 kClientIPv6Addr2);
689 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
690 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
691 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000692};
693
694// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700695TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000696 EXPECT_EQ(&network_manager_, allocator().network_manager());
697 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800698 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700699 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000700 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800701 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
702 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700703 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
704 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
705 EXPECT_TRUE(
706 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
707 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000709}
710
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800711// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700712TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800713 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
714 rtc::ADAPTER_TYPE_ETHERNET);
715 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
716 rtc::ADAPTER_TYPE_WIFI);
717 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
718 rtc::ADAPTER_TYPE_CELLULAR);
719 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
720 rtc::ADAPTER_TYPE_VPN);
721 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
722 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700723 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
724 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
725 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800727 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
728 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800729 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700730 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800731 EXPECT_LT(candidate.address().ip(), 0x12345604U);
732 }
733}
734
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700735TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800736 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
737 rtc::ADAPTER_TYPE_ETHERNET);
738 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
739 rtc::ADAPTER_TYPE_WIFI);
740 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
741 rtc::ADAPTER_TYPE_CELLULAR);
742 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
743 rtc::ADAPTER_TYPE_LOOPBACK |
744 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700745 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
746 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
747 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800748 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800749 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
750 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 EXPECT_EQ(1U, candidates_.size());
752 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
753}
754
deadbeef1c46a352017-09-27 11:24:05 -0700755// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
756// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
757TEST_F(BasicPortAllocatorTest,
758 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
759 SocketAddress wifi(IPAddress(0x12345600U), 0);
760 SocketAddress cell(IPAddress(0x12345601U), 0);
761 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
762 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
763 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-05-31 18:29:12 -0700764 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
765 cricket::PORTALLOCATOR_DISABLE_RELAY |
766 cricket::PORTALLOCATOR_DISABLE_TCP |
767 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
768 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
769 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800770 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
771 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700772 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700773 EXPECT_EQ(1U, candidates_.size());
deadbeef1c46a352017-09-27 11:24:05 -0700774 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi);
775}
honghaiz60347052016-05-31 18:29:12 -0700776
deadbeef1c46a352017-09-27 11:24:05 -0700777// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
778// both "unknown" and cell interfaces are available, only the unknown are used.
779// The unknown interface may be something that ultimately uses Wi-Fi, so we do
780// this to be on the safe side.
781TEST_F(BasicPortAllocatorTest,
782 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
783 SocketAddress cell(IPAddress(0x12345601U), 0);
784 SocketAddress unknown1(IPAddress(0x12345602U), 0);
785 SocketAddress unknown2(IPAddress(0x12345603U), 0);
786 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
787 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
788 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
789 // Disable all but UDP candidates to make the test simpler.
790 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
791 cricket::PORTALLOCATOR_DISABLE_RELAY |
792 cricket::PORTALLOCATOR_DISABLE_TCP |
793 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
794 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700795 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800796 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
797 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700798 // Should only get two candidates, none of which is cell.
honghaiz60347052016-05-31 18:29:12 -0700799 EXPECT_EQ(2U, candidates_.size());
deadbeef1c46a352017-09-27 11:24:05 -0700800 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", unknown1);
801 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", unknown2);
802}
honghaiz60347052016-05-31 18:29:12 -0700803
deadbeef1c46a352017-09-27 11:24:05 -0700804// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
805// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
806// interface is used.
807TEST_F(BasicPortAllocatorTest,
808 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
809 SocketAddress wifi(IPAddress(0x12345600U), 0);
810 SocketAddress cellular(IPAddress(0x12345601U), 0);
811 SocketAddress unknown1(IPAddress(0x12345602U), 0);
812 SocketAddress unknown2(IPAddress(0x12345603U), 0);
813 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
814 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
815 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
816 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
817 // Disable all but UDP candidates to make the test simpler.
818 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
819 cricket::PORTALLOCATOR_DISABLE_RELAY |
820 cricket::PORTALLOCATOR_DISABLE_TCP |
821 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
822 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-05-31 18:29:12 -0700823 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800824 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
825 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 11:24:05 -0700826 // Should only get one Wi-Fi candidate.
honghaiz60347052016-05-31 18:29:12 -0700827 EXPECT_EQ(1U, candidates_.size());
deadbeef1c46a352017-09-27 11:24:05 -0700828 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi);
829}
830
831// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
832// only interface available is cellular, it ends up used anyway. A costly
833// connection is always better than no connection.
834TEST_F(BasicPortAllocatorTest,
835 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
836 SocketAddress cellular(IPAddress(0x12345601U), 0);
837 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
838 // Disable all but UDP candidates to make the test simpler.
839 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
840 cricket::PORTALLOCATOR_DISABLE_RELAY |
841 cricket::PORTALLOCATOR_DISABLE_TCP |
842 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
843 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
844 session_->StartGettingPorts();
845 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
846 kDefaultAllocationTimeout, fake_clock);
847 // Make sure we got the cell candidate.
848 EXPECT_EQ(1U, candidates_.size());
849 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", cellular);
honghaiz60347052016-05-31 18:29:12 -0700850}
851
Yuwei Huangb181f712018-01-22 17:01:28 -0800852// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
853// a WiFi network with link-local IP address and a cellular network, then the
854// cellular candidate will still be gathered.
855TEST_F(BasicPortAllocatorTest,
856 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
857 SocketAddress wifi_link_local("169.254.0.1", 0);
858 SocketAddress cellular(IPAddress(0x12345601U), 0);
859 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
860 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
861
862 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
863 cricket::PORTALLOCATOR_DISABLE_RELAY |
864 cricket::PORTALLOCATOR_DISABLE_TCP |
865 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
866 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
867 session_->StartGettingPorts();
868 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
869 kDefaultAllocationTimeout, fake_clock);
870 // Make sure we got both wifi and cell candidates.
871 EXPECT_EQ(2U, candidates_.size());
872 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi_link_local);
873 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", cellular);
874}
875
876// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
877// a WiFi network with link-local IP address, a WiFi network with a normal IP
878// address and a cellular network, then the cellular candidate will not be
879// gathered.
880TEST_F(BasicPortAllocatorTest,
881 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
882 SocketAddress wifi(IPAddress(0x12345600U), 0);
883 SocketAddress wifi_link_local("169.254.0.1", 0);
884 SocketAddress cellular(IPAddress(0x12345601U), 0);
885 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
886 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
887 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
888
889 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
890 cricket::PORTALLOCATOR_DISABLE_RELAY |
891 cricket::PORTALLOCATOR_DISABLE_TCP |
892 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
893 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
894 session_->StartGettingPorts();
895 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
896 kDefaultAllocationTimeout, fake_clock);
897 // Make sure we got only wifi candidates.
898 EXPECT_EQ(2U, candidates_.size());
899 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi);
900 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", wifi_link_local);
901}
902
deadbeef3427f532017-07-26 16:09:33 -0700903// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
904// to gather candidates.
905TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
906 // Add three IPv6 network interfaces, but tell the allocator to only use two.
907 allocator().set_max_ipv6_networks(2);
908 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
909 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
910 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
911
912 // To simplify the test, only gather UDP host candidates.
913 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
914 PORTALLOCATOR_DISABLE_STUN |
915 PORTALLOCATOR_DISABLE_RELAY);
916
917 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
918 session_->StartGettingPorts();
919 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
920 kDefaultAllocationTimeout, fake_clock);
921 EXPECT_EQ(2U, candidates_.size());
922 // Ensure the expected two interfaces (eth0 and eth1) were used.
923 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
924 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr2);
925}
926
927// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
928// being used.
929TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
930 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
931 allocator().set_max_ipv6_networks(1);
932 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
933 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
934 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
935 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
936
937 // To simplify the test, only gather UDP host candidates.
938 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
939 PORTALLOCATOR_DISABLE_STUN |
940 PORTALLOCATOR_DISABLE_RELAY);
941
942 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
943 session_->StartGettingPorts();
944 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
945 kDefaultAllocationTimeout, fake_clock);
946 EXPECT_EQ(3U, candidates_.size());
947 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
948 // were used.
949 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
950 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
951 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr2);
952}
953
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700954// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700955TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800956 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
957 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700958 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
959 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
960 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700961 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800962 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
963 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700964 EXPECT_EQ(1U, candidates_.size());
965}
966
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000967// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700968TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000969 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700970 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000971 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700972 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
973 kDefaultAllocationTimeout, fake_clock);
974 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000975 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700976 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
977 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
978 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
979 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
980 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
981 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
982 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
983 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000984}
985
honghaiz8c404fa2015-09-28 07:59:43 -0700986// Test that when the same network interface is brought down and up, the
987// port allocator session will restart a new allocation sequence if
988// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700989TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700990 std::string if_name("test_net0");
991 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700992 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700993 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700994 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
995 kDefaultAllocationTimeout, fake_clock);
996 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700997 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700998 candidate_allocation_done_ = false;
999 candidates_.clear();
1000 ports_.clear();
1001
deadbeef7f1563f2017-09-15 17:40:01 -07001002 // Disable socket creation to simulate the network interface being down. When
1003 // no network interfaces are available, BasicPortAllocator will fall back to
1004 // binding to the "ANY" address, so we need to make sure that fails too.
1005 fss_->set_tcp_sockets_enabled(false);
1006 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -07001007 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001008 SIMULATED_WAIT(false, 1000, fake_clock);
1009 EXPECT_EQ(0U, candidates_.size());
1010 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -07001011
1012 // When the same interfaces are added again, new candidates/ports should be
1013 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -07001014 fss_->set_tcp_sockets_enabled(true);
1015 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -07001016 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001017 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1018 kDefaultAllocationTimeout, fake_clock);
1019 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001020 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001021}
1022
1023// Test that when the same network interface is brought down and up, the
1024// port allocator session will not restart a new allocation sequence if
1025// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001026TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -07001027 std::string if_name("test_net0");
1028 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001029 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -07001030 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001031 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1032 kDefaultAllocationTimeout, fake_clock);
1033 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001034 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001035 session_->StopGettingPorts();
1036 candidates_.clear();
1037 ports_.clear();
1038
1039 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -07001040 // Wait one (simulated) second and then verify no new candidates have
1041 // appeared.
1042 SIMULATED_WAIT(false, 1000, fake_clock);
1043 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001044 EXPECT_EQ(0U, ports_.size());
1045
1046 // When the same interfaces are added again, new candidates/ports should not
1047 // be generated because the session has stopped.
1048 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -07001049 SIMULATED_WAIT(false, 1000, fake_clock);
1050 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001051 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001052}
1053
deadbeef1c46a352017-09-27 11:24:05 -07001054// Similar to the above tests, but tests a situation when sockets can't be
1055// bound to a network interface, then after a network change event can be.
1056// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1057TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1058 // Only test local ports to simplify test.
1059 ResetWithNoServersOrNat();
1060 // Provide a situation where the interface appears to be available, but
1061 // binding the sockets fails. See bug for description of when this can
1062 // happen.
1063 std::string if_name("test_net0");
1064 AddInterface(kClientAddr, if_name);
1065 fss_->set_tcp_sockets_enabled(false);
1066 fss_->set_udp_sockets_enabled(false);
1067 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1068 session_->StartGettingPorts();
1069 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1070 kDefaultAllocationTimeout, fake_clock);
1071 // Make sure we actually prevented candidates from being gathered (other than
1072 // a single TCP active candidate, since that doesn't require creating a
1073 // socket).
1074 ASSERT_EQ(1U, candidates_.size());
1075 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1076 candidate_allocation_done_ = false;
1077
1078 // Now simulate the interface coming up, with the newfound ability to bind
1079 // sockets.
1080 fss_->set_tcp_sockets_enabled(true);
1081 fss_->set_udp_sockets_enabled(true);
1082 AddInterface(kClientAddr, if_name);
1083 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1084 kDefaultAllocationTimeout, fake_clock);
1085 // Should get UDP and TCP candidate.
1086 ASSERT_EQ(2U, candidates_.size());
1087 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1088 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1089 // We should extend this test to also verify that a server candidate is
1090 // gathered.
1091 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1092}
1093
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001094// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001095TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001096 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001097 allocator_->set_step_delay(kDefaultStepDelay);
1098 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001099 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001100 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001101 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001102 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001103 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001104 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1105 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001106 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001107 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1108 kRelaySslTcpIntAddr);
deadbeef1c5e6d02017-09-15 17:46:56 -07001109 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
1110 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001111 EXPECT_EQ(4U, ports_.size());
1112 EXPECT_TRUE(candidate_allocation_done_);
1113 // If we Stop gathering now, we shouldn't get a second "done" callback.
1114 session_->StopGettingPorts();
1115}
1116
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001117TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001118 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001119 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001120 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001121 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1122 kDefaultAllocationTimeout, fake_clock);
1123 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001124 // If we Stop gathering now, we shouldn't get a second "done" callback.
1125 session_->StopGettingPorts();
1126
1127 // All ports should have unset send-buffer sizes.
1128 CheckSendBufferSizesOfAllPorts(-1);
1129}
1130
deadbeef7f1563f2017-09-15 17:40:01 -07001131// Tests that we can get callback after StopGetAllPorts when called in the
1132// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001133TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001134 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001135 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001136 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001137 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1138 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001139 EXPECT_EQ(2U, ports_.size());
1140 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001141 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1142 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143}
1144
1145// Test that we restrict client ports appropriately when a port range is set.
1146// We check the candidates for udp/stun/tcp ports, and the from address
1147// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001148TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149 AddInterface(kClientAddr);
1150 // Check that an invalid port range fails.
1151 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1152 // Check that a null port range succeeds.
1153 EXPECT_TRUE(SetPortRange(0, 0));
1154 // Check that a valid port range succeeds.
1155 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001156 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001158 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1159 kDefaultAllocationTimeout, fake_clock);
1160 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001161 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001162
1163 int num_nonrelay_candidates = 0;
1164 for (const Candidate& candidate : candidates_) {
1165 // Check the port number for the UDP/STUN/TCP port objects.
1166 if (candidate.type() != RELAY_PORT_TYPE) {
1167 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
1168 ++num_nonrelay_candidates;
1169 }
1170 }
1171 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001173 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
1174 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001175}
1176
deadbeefe97389c2016-12-23 01:43:45 -08001177// Test that if we have no network adapters, we bind to the ANY address and
1178// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001179TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001180 // Default config uses GTURN and no NAT, so replace that with the
1181 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1182 ResetWithStunServerAndNat(kStunAddr);
1183 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1184 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1185 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1186 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1187 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1188 // TODO(deadbeef): Fix this.
1189 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001190 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001191 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001192 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1193 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001194 EXPECT_EQ(4U, ports_.size());
1195 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1196 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1197 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1198 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1199 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1200 // The "any" address port should be in the signaled ready ports, but the host
1201 // candidate for it is useless and shouldn't be signaled. So we only have
1202 // STUN/TURN candidates.
1203 EXPECT_EQ(3U, candidates_.size());
1204 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1205 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1206 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1207 // server.
1208 EXPECT_EQ(2,
1209 CountCandidates(candidates_, "relay", "udp",
1210 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001211}
1212
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001213// Test that when enumeration is disabled, we should not have any ports when
1214// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001215TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001216 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001217 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001218 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001219 // Expect to see no ports and no candidates.
1220 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1221 rtc::IPAddress(), rtc::IPAddress());
1222}
1223
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001224// Test that even with multiple interfaces, the result should still be a single
1225// default private, one STUN and one TURN candidate since we bind to any address
1226// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001227TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001228 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001229 AddInterface(kPrivateAddr);
1230 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001231 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001232 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001233
1234 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1235 // address set and we have no IPv6 STUN server, there should be no IPv6
1236 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001237 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1238 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001239
1240 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1241 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1242 // TURN/UDP candidates.
1243 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001244 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1245 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001246}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001247
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001248// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1249// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001250TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001251 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001252 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001253 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001254 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001255 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1256 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1257 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1258 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001259 kTurnUdpExtAddr.ipaddr());
1260}
1261
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001262// Test that when adapter enumeration is disabled, for endpoints without
1263// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001264TEST_F(BasicPortAllocatorTest,
1265 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001266 ResetWithNoServersOrNat();
1267 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1268 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1269 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001270}
1271
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001272// Test that when adapter enumeration is disabled, with
1273// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1274// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001275TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001276 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1277 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001278 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1279 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001280 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1281 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001282 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001283 rtc::IPAddress(), rtc::IPAddress());
1284}
1285
1286// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001287// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1288// a NAT, there is no local candidate. However, this specified default route
1289// (kClientAddr) which was discovered when sending STUN requests, will become
1290// the srflx addresses.
1291TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001292 BasicPortAllocatorTest,
Steve Anton6c38cc72017-11-29 10:25:58 -08001293 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001294 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001295 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001296 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1297 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001298 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1299 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001300 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1301 rtc::IPAddress(), rtc::IPAddress());
1302}
1303
1304// Test that when adapter enumeration is disabled, with
1305// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1306// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001307TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001308 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1309 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001310 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1311 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001312 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1313 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1314 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001315}
1316
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001317// Test that we disable relay over UDP, and only TCP is used when connecting to
1318// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001319TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001320 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001321 AddInterface(kClientAddr);
1322 ResetWithStunServerAndNat(kStunAddr);
1323 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001324 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1325 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1326 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1327 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001328
1329 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001330 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1331 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001332
1333 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1334 // TURN/TCP candidates.
1335 EXPECT_EQ(2U, ports_.size());
1336 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001337 Candidate turn_candidate;
1338 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1339 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001340 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001341 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1342 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001343}
1344
Erik Språngefdce692015-06-05 09:41:26 +02001345// Disable for asan, see
1346// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1347#if !defined(ADDRESS_SANITIZER)
1348
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001349// Test that we can get OnCandidatesAllocationDone callback when all the ports
1350// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001351TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001353 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1354 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1355 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001356 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001357 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001358 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001359}
1360
1361// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001362TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001363 AddInterface(kClientAddr);
1364 fss_->set_udp_sockets_enabled(false);
1365 EXPECT_TRUE(CreateSession(1));
1366 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001367 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1368 kDefaultAllocationTimeout, fake_clock);
1369 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001370 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001371 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1372 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1373 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1374 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1375 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1376 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001377}
1378
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001379#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001380
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001381// Test that we don't crash or malfunction if we can't create UDP sockets or
1382// listen on TCP sockets. We still give out a local TCP address, since
1383// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001384TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001385 AddInterface(kClientAddr);
1386 fss_->set_udp_sockets_enabled(false);
1387 fss_->set_tcp_listen_enabled(false);
1388 EXPECT_TRUE(CreateSession(1));
1389 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001390 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1391 kDefaultAllocationTimeout, fake_clock);
1392 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001393 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001394 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1395 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1396 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1397 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1398 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001399 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001400}
1401
1402// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001403// TODO(deadbeef): Find a way to exit early here.
1404TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405 AddInterface(kClientAddr);
1406 fss_->set_tcp_sockets_enabled(false);
1407 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001408 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001409 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001410 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001411 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001412 // In case of Relay, ports creation will succeed but sockets will fail.
1413 // There is no error reporting from RelayEntry to handle this failure.
1414}
1415
1416// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001417TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001418 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1419 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001420 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001422 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1423 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001424 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001425 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1426 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001427 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001428 // will be tried after about 3 seconds.
1429 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001430 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001431 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1432 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1433 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1434 kRelaySslTcpIntAddr);
1435 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001436 // We wait at least for a full STUN timeout, which
1437 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1438 // already passed (see above), we wait 3 seconds less than that.
1439 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1440 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441}
1442
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001443TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 AddInterface(kClientAddr);
1445 AddInterface(kClientAddr2);
1446 // Allocating only host UDP ports. This is done purely for testing
1447 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001448 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1449 PORTALLOCATOR_DISABLE_RELAY);
1450 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001451 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001452 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1453 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001454 ASSERT_EQ(2U, candidates_.size());
1455 EXPECT_EQ(2U, ports_.size());
1456 // Candidates priorities should be different.
1457 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1458}
1459
1460// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001461TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001462 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001463 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001464 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001465 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1466 kDefaultAllocationTimeout, fake_clock);
1467 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001468 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001469 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470}
1471
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001472// Test that the allocator session uses the candidate filter it's created with,
1473// rather than the filter of its parent allocator.
1474// The filter of the allocator should only affect the next gathering phase,
1475// according to JSEP, which means the *next* allocator session returned.
1476TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1477 AddInterface(kClientAddr);
1478 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1479 // Set candidate filter *after* creating the session. Should have no effect.
1480 allocator().set_candidate_filter(CF_RELAY);
1481 session_->StartGettingPorts();
1482 // 7 candidates and 4 ports is what we would normally get (see the
1483 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001484 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1485 kDefaultAllocationTimeout, fake_clock);
1486 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001487 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001488}
1489
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001490// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1491// This test also verifies that when the allocator is only allowed to use
1492// relay (i.e. IceTransportsType is relay), the raddr is an empty
1493// address with the correct family. This is to prevent any local
1494// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001495TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001496 AddInterface(kClientAddr);
1497 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001498 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001499 allocator().set_candidate_filter(CF_RELAY);
1500 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001501 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001502 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1503 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001504 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1505 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001506
1507 EXPECT_EQ(1U, candidates_.size());
1508 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001509 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1510 EXPECT_EQ(
1511 candidates_[0].related_address(),
1512 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001513}
1514
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001515TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001516 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001517 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1518 allocator().set_candidate_filter(CF_HOST);
1519 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001520 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001521 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1522 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001523 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1524 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001525 for (const Candidate& candidate : candidates_) {
1526 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001527 }
1528}
1529
1530// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001531TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001532 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001533 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001534
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001535 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1536 allocator().set_candidate_filter(CF_REFLEXIVE);
1537 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001538 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001539 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1540 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001541 // Host is behind NAT, no private address will be exposed. Hence only UDP
1542 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001543 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1544 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001545 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1546 EXPECT_EQ(
1547 candidates_[0].related_address(),
1548 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001549}
1550
1551// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001552TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001553 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001554 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1555 allocator().set_candidate_filter(CF_REFLEXIVE);
1556 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001557 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001558 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1559 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001560 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001561 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001562 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001563 for (const Candidate& candidate : candidates_) {
1564 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001565 }
1566}
1567
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001568// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001569TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001570 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001571 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001572 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001573 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1574 kDefaultAllocationTimeout, fake_clock);
1575 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001576 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1577 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1578 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001579 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001580 for (const Candidate& candidate : candidates_) {
1581 EXPECT_EQ(kIceUfrag0, candidate.username());
1582 EXPECT_EQ(kIcePwd0, candidate.password());
1583 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001584}
1585
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001586// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1587// is allocated for udp and stun. Also verify there is only one candidate
1588// (local) if stun candidate is same as local candidate, which will be the case
1589// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001590TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001591 AddInterface(kClientAddr);
1592 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001593 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1594 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001595 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001596 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1597 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001598 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001599 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001600 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1601 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001602}
1603
1604// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1605// is allocated for udp and stun. In this test we should expect both stun and
1606// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001607TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001608 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001609 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001610
1611 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001612 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1613 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001614 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001615 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1616 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001617 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001618 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1619 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1620 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001621 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1622 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623 EXPECT_EQ(3U, candidates_.size());
1624}
1625
deadbeefc5d0d952015-07-16 10:22:21 -07001626// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001627TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001628 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001629 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001630 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001631
1632 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1633
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001634 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001635 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001636 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1637 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001638
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001639 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001640 session_->StartGettingPorts();
1641
deadbeef7f1563f2017-09-15 17:40:01 -07001642 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1643 kDefaultAllocationTimeout, fake_clock);
1644 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001645 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001646 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1647 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1648 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1649 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1650 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001651}
1652
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001653// Test that if prune_turn_ports is set, TCP TURN port will not be used
1654// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1655TEST_F(BasicPortAllocatorTest,
1656 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1657 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1658 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1659 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001660
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001661 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001662}
1663
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001664// Test that if prune_turn_ports is set, TCP TURN port will not be used
1665// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1666TEST_F(BasicPortAllocatorTest,
1667 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1668 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1669 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1670 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001671
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001672 TestUdpTurnPortPrunesTcpTurnPort();
1673}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001674
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001675// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1676// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1677TEST_F(BasicPortAllocatorTest,
1678 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1679 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1680 // first.
1681 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1682 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001683
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001684 TestIPv6TurnPortPrunesIPv4TurnPort();
1685}
1686
1687// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1688// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1689TEST_F(BasicPortAllocatorTest,
1690 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1691 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1692 // first.
1693 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1694 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1695
1696 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001697}
1698
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001699// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001700// will has its own set of TurnPorts based on their priorities, in the default
1701// case where no transit delay is set.
1702TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1703 TestEachInterfaceHasItsOwnTurnPorts();
1704}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001705
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001706// Tests that if prune_turn_ports is set, each network interface
1707// will has its own set of TurnPorts based on their priorities, given that
1708// IPv4/TCP TURN port becomes ready first.
1709TEST_F(BasicPortAllocatorTest,
1710 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1711 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1712 // becomes ready last.
1713 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1714 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1715 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1716 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001717
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001718 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001719}
1720
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001721// Testing DNS resolve for the TURN server, this will test AllocationSequence
1722// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001723// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1724// appears that it doesn't currently because of the DNS look up not
1725// using the fake clock.
1726TEST_F(BasicPortAllocatorTestWithRealClock,
1727 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001728 // This test relies on a real query for "localhost", so it won't work on an
1729 // IPv6-only machine.
1730 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001731 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001732 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001733 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001734 allocator_.reset(new BasicPortAllocator(&network_manager_));
1735 RelayServerConfig turn_server(RELAY_TURN);
1736 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001737 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001738 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001739 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001740 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001741
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001742 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001743 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001744 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1745 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001746
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001747 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001748 session_->StartGettingPorts();
1749
1750 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1751}
1752
1753// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1754// is allocated for udp/stun/turn. In this test we should expect all local,
1755// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001756TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001757 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001758 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001759
1760 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1761
1762 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001763 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1764 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001765
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001766 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001767 session_->StartGettingPorts();
1768
deadbeef7f1563f2017-09-15 17:40:01 -07001769 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1770 kDefaultAllocationTimeout, fake_clock);
1771 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001772 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001773 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1774 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1775 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1776 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1777 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001778 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1779 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001780 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001781 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1782 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001783 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1784 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1785}
1786
1787// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1788// server is also used as the STUN server, we should get 'local', 'stun', and
1789// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001790TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001791 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001792 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001793 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001794 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1795
1796 // Must set the step delay to 0 to make sure the relay allocation phase is
1797 // started before the STUN candidates are obtained, so that the STUN binding
1798 // response is processed when both StunPort and TurnPort exist to reproduce
1799 // webrtc issue 3537.
1800 allocator_->set_step_delay(0);
1801 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001802 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1803 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001804
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001805 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001806 session_->StartGettingPorts();
1807
deadbeef7f1563f2017-09-15 17:40:01 -07001808 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1809 kDefaultAllocationTimeout, fake_clock);
1810 EXPECT_EQ(3U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001811 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1812 Candidate stun_candidate;
1813 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1814 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1815 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1816 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1817 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001818
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001819 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001820 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1821 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001822 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1823 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1824}
1825
deadbeefc5d0d952015-07-16 10:22:21 -07001826// Test that when only a TCP TURN server is available, we do NOT use it as
1827// a UDP STUN server, as this could leak our IP address. Thus we should only
1828// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001829TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001830 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001831 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001832 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001833 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1834
1835 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001836 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1837 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001838
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001839 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001840 session_->StartGettingPorts();
1841
deadbeef7f1563f2017-09-15 17:40:01 -07001842 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1843 kDefaultAllocationTimeout, fake_clock);
1844 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001845 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001846 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1847 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1848 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001849 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1850 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1851}
1852
1853// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1854// TURN server is used as the STUN server and we get 'local', 'stun', and
1855// 'relay' candidates.
1856// TODO(deadbeef): Remove this test when support for non-shared socket mode
1857// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001858TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001859 AddInterface(kClientAddr);
1860 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001861 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001862 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1863
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001864 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001865
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001866 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001867 session_->StartGettingPorts();
1868
deadbeef7f1563f2017-09-15 17:40:01 -07001869 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1870 kDefaultAllocationTimeout, fake_clock);
1871 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001872 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001873 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1874 Candidate stun_candidate;
1875 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1876 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1877 Candidate turn_candidate;
1878 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1879 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1880 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001881 // Not using shared socket, so the STUN request's server reflexive address
1882 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001883 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001884
deadbeefc5d0d952015-07-16 10:22:21 -07001885 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1886 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1887 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1888}
1889
1890// Test that even when both a STUN and TURN server are configured, the TURN
1891// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001892TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001893 AddInterface(kClientAddr);
1894 // Configure with STUN server but destroy it, so we can ensure that it's
1895 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001896 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001897 stun_server_.reset();
1898 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1899
1900 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001901 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1902 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001903
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001904 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001905 session_->StartGettingPorts();
1906
pthatcher1749bc32017-02-08 13:18:00 -08001907 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1908 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001909 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1910 Candidate stun_candidate;
1911 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1912 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1913 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1914 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1915 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001916
1917 // Don't bother waiting for STUN timeout, since we already verified
1918 // that we got a STUN candidate from the TURN server.
1919}
1920
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001921// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1922// and fail to generate STUN candidate, local UDP candidate is generated
1923// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001924TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001925 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1926 PORTALLOCATOR_DISABLE_TCP |
1927 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001928 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1929 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001930 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001931 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001932 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1933 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001934 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001935 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001936 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001937 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1938 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001939 EXPECT_EQ(1U, candidates_.size());
1940}
1941
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001942// Test that when the NetworkManager doesn't have permission to enumerate
1943// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1944// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001945TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001946 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1947 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001948 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001949 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001950 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1951 PORTALLOCATOR_DISABLE_TCP |
1952 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1953 EXPECT_EQ(0U,
1954 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1955 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1956 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001957 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001958 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1959 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001960 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001961 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001962 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001963}
1964
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001965// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001966TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001967 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1968 PORTALLOCATOR_ENABLE_IPV6 |
1969 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001970 AddInterface(kClientIPv6Addr);
1971 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001972 allocator_->set_step_delay(kMinimumStepDelay);
1973 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001974 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001975 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08001976 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07001977 EXPECT_EQ(4U, ports_.size());
1978 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001979 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1980 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1981 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1982 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001983}
honghaiz98db68f2015-09-29 07:58:17 -07001984
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001985TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001986 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001987 allocator_->set_step_delay(kDefaultStepDelay);
1988 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001989 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001990 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001991 EXPECT_EQ(2U, ports_.size());
1992 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001993 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001994
1995 // After stopping getting ports, adding a new interface will not start
1996 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001997 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001998 candidates_.clear();
1999 ports_.clear();
2000 candidate_allocation_done_ = false;
2001 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002002 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002003 EXPECT_EQ(0U, candidates_.size());
2004 EXPECT_EQ(0U, ports_.size());
2005}
2006
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002007TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07002008 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002009 allocator_->set_step_delay(kDefaultStepDelay);
2010 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07002011 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002012 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002013 EXPECT_EQ(2U, ports_.size());
2014 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08002015 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002016
2017 // After clearing getting ports, adding a new interface will start getting
2018 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07002019 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07002020 candidates_.clear();
2021 ports_.clear();
2022 candidate_allocation_done_ = false;
2023 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08002024 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002025 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08002026 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2027 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07002028}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002029
2030// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2031// a pooled session is taken out of the pool.
2032TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2033 AddInterface(kClientAddr);
2034 int pool_size = 1;
2035 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002036 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002037 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002038 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002039 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2040 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002041 // Expect that when TakePooledSession is called,
2042 // UpdateTransportInformationInternal will be called and the
2043 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2044 // candidates.
2045 session_ =
2046 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2047 ASSERT_NE(nullptr, session_.get());
2048 auto ready_ports = session_->ReadyPorts();
2049 auto candidates = session_->ReadyCandidates();
2050 EXPECT_FALSE(ready_ports.empty());
2051 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002052 for (const PortInterface* port_interface : ready_ports) {
2053 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002054 EXPECT_EQ(kContentName, port->content_name());
2055 EXPECT_EQ(1, port->component());
2056 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2057 EXPECT_EQ(kIcePwd0, port->password());
2058 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002059 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002060 EXPECT_EQ(1, candidate.component());
2061 EXPECT_EQ(kIceUfrag0, candidate.username());
2062 EXPECT_EQ(kIcePwd0, candidate.password());
2063 }
2064}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002065
2066// Test that a new candidate filter takes effect even on already-gathered
2067// candidates.
2068TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2069 AddInterface(kClientAddr);
2070 int pool_size = 1;
2071 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002072 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002073 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2074 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002075 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2076 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002077 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2078 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2079 allocator_->set_candidate_filter(CF_RELAY);
2080 // Assume that when TakePooledSession is called, the candidate filter will be
2081 // applied to the pooled session. This is tested by PortAllocatorTest.
2082 session_ =
2083 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2084 ASSERT_NE(nullptr, session_.get());
2085 auto candidates = session_->ReadyCandidates();
2086 auto ports = session_->ReadyPorts();
2087 // Sanity check that the number of candidates and ports decreased.
2088 EXPECT_GT(initial_candidates_size, candidates.size());
2089 EXPECT_GT(initial_ports_size, ports.size());
2090 for (const PortInterface* port : ports) {
2091 // Expect only relay ports.
2092 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2093 }
2094 for (const Candidate& candidate : candidates) {
2095 // Expect only relay candidates now that the filter is applied.
2096 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2097 // Expect that the raddr is emptied due to the CF_RELAY filter.
2098 EXPECT_EQ(candidate.related_address(),
2099 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2100 }
2101}
2102
2103} // namespace cricket