blob: a14132858685a92267bf3c018fb6313e8704b816 [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
deadbeef3427f532017-07-26 16:09:33 -0700852// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
853// to gather candidates.
854TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
855 // Add three IPv6 network interfaces, but tell the allocator to only use two.
856 allocator().set_max_ipv6_networks(2);
857 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
858 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
859 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
860
861 // To simplify the test, only gather UDP host candidates.
862 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
863 PORTALLOCATOR_DISABLE_STUN |
864 PORTALLOCATOR_DISABLE_RELAY);
865
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 EXPECT_EQ(2U, candidates_.size());
871 // Ensure the expected two interfaces (eth0 and eth1) were used.
872 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
873 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr2);
874}
875
876// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
877// being used.
878TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
879 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
880 allocator().set_max_ipv6_networks(1);
881 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
882 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
883 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
884 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
885
886 // To simplify the test, only gather UDP host candidates.
887 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
888 PORTALLOCATOR_DISABLE_STUN |
889 PORTALLOCATOR_DISABLE_RELAY);
890
891 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
892 session_->StartGettingPorts();
893 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
894 kDefaultAllocationTimeout, fake_clock);
895 EXPECT_EQ(3U, candidates_.size());
896 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
897 // were used.
898 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
899 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
900 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr2);
901}
902
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700903// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700904TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800905 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
906 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700907 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
908 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
909 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700910 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800911 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
912 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700913 EXPECT_EQ(1U, candidates_.size());
914}
915
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000916// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700917TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000918 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700919 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700921 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
922 kDefaultAllocationTimeout, fake_clock);
923 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000924 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700925 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
926 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
927 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
928 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
929 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
930 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
931 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
932 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933}
934
honghaiz8c404fa2015-09-28 07:59:43 -0700935// Test that when the same network interface is brought down and up, the
936// port allocator session will restart a new allocation sequence if
937// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700938TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700939 std::string if_name("test_net0");
940 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700941 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700942 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700943 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
944 kDefaultAllocationTimeout, fake_clock);
945 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700946 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700947 candidate_allocation_done_ = false;
948 candidates_.clear();
949 ports_.clear();
950
deadbeef7f1563f2017-09-15 17:40:01 -0700951 // Disable socket creation to simulate the network interface being down. When
952 // no network interfaces are available, BasicPortAllocator will fall back to
953 // binding to the "ANY" address, so we need to make sure that fails too.
954 fss_->set_tcp_sockets_enabled(false);
955 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 07:59:43 -0700956 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -0700957 SIMULATED_WAIT(false, 1000, fake_clock);
958 EXPECT_EQ(0U, candidates_.size());
959 ports_.clear();
honghaiz8c404fa2015-09-28 07:59:43 -0700960
961 // When the same interfaces are added again, new candidates/ports should be
962 // generated.
deadbeef7f1563f2017-09-15 17:40:01 -0700963 fss_->set_tcp_sockets_enabled(true);
964 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 07:59:43 -0700965 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -0700966 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
967 kDefaultAllocationTimeout, fake_clock);
968 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700969 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700970}
971
972// Test that when the same network interface is brought down and up, the
973// port allocator session will not restart a new allocation sequence if
974// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700975TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700976 std::string if_name("test_net0");
977 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700978 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700979 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -0700980 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
981 kDefaultAllocationTimeout, fake_clock);
982 EXPECT_EQ(7U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700983 EXPECT_EQ(4U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700984 session_->StopGettingPorts();
985 candidates_.clear();
986 ports_.clear();
987
988 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-15 17:40:01 -0700989 // Wait one (simulated) second and then verify no new candidates have
990 // appeared.
991 SIMULATED_WAIT(false, 1000, fake_clock);
992 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -0700993 EXPECT_EQ(0U, ports_.size());
994
995 // When the same interfaces are added again, new candidates/ports should not
996 // be generated because the session has stopped.
997 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-15 17:40:01 -0700998 SIMULATED_WAIT(false, 1000, fake_clock);
999 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001000 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 07:59:43 -07001001}
1002
deadbeef1c46a352017-09-27 11:24:05 -07001003// Similar to the above tests, but tests a situation when sockets can't be
1004// bound to a network interface, then after a network change event can be.
1005// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1006TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1007 // Only test local ports to simplify test.
1008 ResetWithNoServersOrNat();
1009 // Provide a situation where the interface appears to be available, but
1010 // binding the sockets fails. See bug for description of when this can
1011 // happen.
1012 std::string if_name("test_net0");
1013 AddInterface(kClientAddr, if_name);
1014 fss_->set_tcp_sockets_enabled(false);
1015 fss_->set_udp_sockets_enabled(false);
1016 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1017 session_->StartGettingPorts();
1018 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1019 kDefaultAllocationTimeout, fake_clock);
1020 // Make sure we actually prevented candidates from being gathered (other than
1021 // a single TCP active candidate, since that doesn't require creating a
1022 // socket).
1023 ASSERT_EQ(1U, candidates_.size());
1024 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1025 candidate_allocation_done_ = false;
1026
1027 // Now simulate the interface coming up, with the newfound ability to bind
1028 // sockets.
1029 fss_->set_tcp_sockets_enabled(true);
1030 fss_->set_udp_sockets_enabled(true);
1031 AddInterface(kClientAddr, if_name);
1032 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1033 kDefaultAllocationTimeout, fake_clock);
1034 // Should get UDP and TCP candidate.
1035 ASSERT_EQ(2U, candidates_.size());
1036 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1037 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1038 // We should extend this test to also verify that a server candidate is
1039 // gathered.
1040 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1041}
1042
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001043// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001044TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001045 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001046 allocator_->set_step_delay(kDefaultStepDelay);
1047 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001048 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001049 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001050 EXPECT_EQ(2U, ports_.size());
deadbeef1c5e6d02017-09-15 17:46:56 -07001051 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001053 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1054 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001055 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001056 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1057 kRelaySslTcpIntAddr);
deadbeef1c5e6d02017-09-15 17:46:56 -07001058 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 1500, fake_clock);
1059 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001060 EXPECT_EQ(4U, ports_.size());
1061 EXPECT_TRUE(candidate_allocation_done_);
1062 // If we Stop gathering now, we shouldn't get a second "done" callback.
1063 session_->StopGettingPorts();
1064}
1065
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001066TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001067 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001068 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001069 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001070 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1071 kDefaultAllocationTimeout, fake_clock);
1072 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001073 // If we Stop gathering now, we shouldn't get a second "done" callback.
1074 session_->StopGettingPorts();
1075
1076 // All ports should have unset send-buffer sizes.
1077 CheckSendBufferSizesOfAllPorts(-1);
1078}
1079
deadbeef7f1563f2017-09-15 17:40:01 -07001080// Tests that we can get callback after StopGetAllPorts when called in the
1081// middle of gathering.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001082TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001083 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001084 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001085 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001086 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1087 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001088 EXPECT_EQ(2U, ports_.size());
1089 session_->StopGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001090 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1091 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001092}
1093
1094// Test that we restrict client ports appropriately when a port range is set.
1095// We check the candidates for udp/stun/tcp ports, and the from address
1096// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001097TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001098 AddInterface(kClientAddr);
1099 // Check that an invalid port range fails.
1100 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1101 // Check that a null port range succeeds.
1102 EXPECT_TRUE(SetPortRange(0, 0));
1103 // Check that a valid port range succeeds.
1104 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001105 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001107 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1108 kDefaultAllocationTimeout, fake_clock);
1109 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001110 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001111
1112 int num_nonrelay_candidates = 0;
1113 for (const Candidate& candidate : candidates_) {
1114 // Check the port number for the UDP/STUN/TCP port objects.
1115 if (candidate.type() != RELAY_PORT_TYPE) {
1116 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
1117 ++num_nonrelay_candidates;
1118 }
1119 }
1120 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001121 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001122 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
1123 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001124}
1125
deadbeefe97389c2016-12-23 01:43:45 -08001126// Test that if we have no network adapters, we bind to the ANY address and
1127// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001128TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001129 // Default config uses GTURN and no NAT, so replace that with the
1130 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1131 ResetWithStunServerAndNat(kStunAddr);
1132 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1133 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1134 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1135 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1136 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1137 // TODO(deadbeef): Fix this.
1138 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001139 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001141 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1142 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001143 EXPECT_EQ(4U, ports_.size());
1144 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1145 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1146 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1147 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1148 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1149 // The "any" address port should be in the signaled ready ports, but the host
1150 // candidate for it is useless and shouldn't be signaled. So we only have
1151 // STUN/TURN candidates.
1152 EXPECT_EQ(3U, candidates_.size());
1153 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1154 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1155 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1156 // server.
1157 EXPECT_EQ(2,
1158 CountCandidates(candidates_, "relay", "udp",
1159 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001160}
1161
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001162// Test that when enumeration is disabled, we should not have any ports when
1163// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001164TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001165 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001166 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001167 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001168 // Expect to see no ports and no candidates.
1169 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1170 rtc::IPAddress(), rtc::IPAddress());
1171}
1172
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001173// Test that even with multiple interfaces, the result should still be a single
1174// default private, one STUN and one TURN candidate since we bind to any address
1175// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001176TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001177 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001178 AddInterface(kPrivateAddr);
1179 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001180 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001181 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001182
1183 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1184 // address set and we have no IPv6 STUN server, there should be no IPv6
1185 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001186 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1187 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001188
1189 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1190 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1191 // TURN/UDP candidates.
1192 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001193 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1194 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001195}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001196
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001197// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1198// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001199TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001200 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001201 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001202 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001203 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001204 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1205 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1206 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1207 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001208 kTurnUdpExtAddr.ipaddr());
1209}
1210
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001211// Test that when adapter enumeration is disabled, for endpoints without
1212// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001213TEST_F(BasicPortAllocatorTest,
1214 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001215 ResetWithNoServersOrNat();
1216 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1217 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1218 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001219}
1220
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001221// Test that when adapter enumeration is disabled, with
1222// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1223// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001224TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001225 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1226 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001227 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1228 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001229 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1230 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001231 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001232 rtc::IPAddress(), rtc::IPAddress());
1233}
1234
1235// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001236// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1237// a NAT, there is no local candidate. However, this specified default route
1238// (kClientAddr) which was discovered when sending STUN requests, will become
1239// the srflx addresses.
1240TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001241 BasicPortAllocatorTest,
Steve Anton6c38cc72017-11-29 10:25:58 -08001242 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001243 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001244 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001245 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1246 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001247 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1248 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001249 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1250 rtc::IPAddress(), rtc::IPAddress());
1251}
1252
1253// Test that when adapter enumeration is disabled, with
1254// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1255// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001256TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001257 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1258 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001259 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1260 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001261 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1262 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1263 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001264}
1265
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001266// Test that we disable relay over UDP, and only TCP is used when connecting to
1267// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001268TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001269 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001270 AddInterface(kClientAddr);
1271 ResetWithStunServerAndNat(kStunAddr);
1272 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001273 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1274 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1275 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1276 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001277
1278 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001279 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1280 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001281
1282 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1283 // TURN/TCP candidates.
1284 EXPECT_EQ(2U, ports_.size());
1285 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001286 Candidate turn_candidate;
1287 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1288 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001289 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001290 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1291 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001292}
1293
Erik Språngefdce692015-06-05 09:41:26 +02001294// Disable for asan, see
1295// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1296#if !defined(ADDRESS_SANITIZER)
1297
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001298// Test that we can get OnCandidatesAllocationDone callback when all the ports
1299// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001300TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001301 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001302 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1303 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1304 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001305 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001306 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001307 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001308}
1309
1310// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001311TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001312 AddInterface(kClientAddr);
1313 fss_->set_udp_sockets_enabled(false);
1314 EXPECT_TRUE(CreateSession(1));
1315 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001316 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1317 kDefaultAllocationTimeout, fake_clock);
1318 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001319 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001320 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1321 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1322 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1323 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1324 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1325 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001326}
1327
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001328#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001329
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001330// Test that we don't crash or malfunction if we can't create UDP sockets or
1331// listen on TCP sockets. We still give out a local TCP address, since
1332// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001333TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001334 AddInterface(kClientAddr);
1335 fss_->set_udp_sockets_enabled(false);
1336 fss_->set_tcp_listen_enabled(false);
1337 EXPECT_TRUE(CreateSession(1));
1338 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001339 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1340 kDefaultAllocationTimeout, fake_clock);
1341 EXPECT_EQ(5U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001342 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001343 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1344 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1345 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1346 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1347 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001348 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001349}
1350
1351// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001352// TODO(deadbeef): Find a way to exit early here.
1353TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001354 AddInterface(kClientAddr);
1355 fss_->set_tcp_sockets_enabled(false);
1356 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001357 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001358 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001359 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001360 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001361 // In case of Relay, ports creation will succeed but sockets will fail.
1362 // There is no error reporting from RelayEntry to handle this failure.
1363}
1364
1365// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001366TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001367 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1368 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001369 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001370 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001371 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1372 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001373 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001374 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1375 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001376 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001377 // will be tried after about 3 seconds.
1378 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001379 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001380 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1381 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1382 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1383 kRelaySslTcpIntAddr);
1384 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001385 // We wait at least for a full STUN timeout, which
1386 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1387 // already passed (see above), we wait 3 seconds less than that.
1388 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1389 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001390}
1391
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001392TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001393 AddInterface(kClientAddr);
1394 AddInterface(kClientAddr2);
1395 // Allocating only host UDP ports. This is done purely for testing
1396 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001397 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1398 PORTALLOCATOR_DISABLE_RELAY);
1399 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001400 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001401 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1402 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001403 ASSERT_EQ(2U, candidates_.size());
1404 EXPECT_EQ(2U, ports_.size());
1405 // Candidates priorities should be different.
1406 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1407}
1408
1409// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001410TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001411 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001412 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001414 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1415 kDefaultAllocationTimeout, fake_clock);
1416 EXPECT_EQ(7U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001417 EXPECT_EQ(4U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001418 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419}
1420
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001421// Test that the allocator session uses the candidate filter it's created with,
1422// rather than the filter of its parent allocator.
1423// The filter of the allocator should only affect the next gathering phase,
1424// according to JSEP, which means the *next* allocator session returned.
1425TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1426 AddInterface(kClientAddr);
1427 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1428 // Set candidate filter *after* creating the session. Should have no effect.
1429 allocator().set_candidate_filter(CF_RELAY);
1430 session_->StartGettingPorts();
1431 // 7 candidates and 4 ports is what we would normally get (see the
1432 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-15 17:40:01 -07001433 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1434 kDefaultAllocationTimeout, fake_clock);
1435 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001436 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001437}
1438
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001439// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1440// This test also verifies that when the allocator is only allowed to use
1441// relay (i.e. IceTransportsType is relay), the raddr is an empty
1442// address with the correct family. This is to prevent any local
1443// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001444TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001445 AddInterface(kClientAddr);
1446 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001447 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001448 allocator().set_candidate_filter(CF_RELAY);
1449 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001451 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1452 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001453 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1454 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001455
1456 EXPECT_EQ(1U, candidates_.size());
1457 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001458 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1459 EXPECT_EQ(
1460 candidates_[0].related_address(),
1461 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001462}
1463
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001464TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001465 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001466 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1467 allocator().set_candidate_filter(CF_HOST);
1468 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001469 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001470 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1471 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001472 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1473 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001474 for (const Candidate& candidate : candidates_) {
1475 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001476 }
1477}
1478
1479// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001480TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001481 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001482 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001483
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001484 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1485 allocator().set_candidate_filter(CF_REFLEXIVE);
1486 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001487 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001488 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1489 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001490 // Host is behind NAT, no private address will be exposed. Hence only UDP
1491 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001492 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1493 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001494 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1495 EXPECT_EQ(
1496 candidates_[0].related_address(),
1497 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001498}
1499
1500// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001501TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001502 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001503 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1504 allocator().set_candidate_filter(CF_REFLEXIVE);
1505 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001506 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001507 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1508 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001509 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001510 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001511 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001512 for (const Candidate& candidate : candidates_) {
1513 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001514 }
1515}
1516
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001517// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001518TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001519 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001520 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001521 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001522 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1523 kDefaultAllocationTimeout, fake_clock);
1524 EXPECT_EQ(7U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001525 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1526 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1527 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001528 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001529 for (const Candidate& candidate : candidates_) {
1530 EXPECT_EQ(kIceUfrag0, candidate.username());
1531 EXPECT_EQ(kIcePwd0, candidate.password());
1532 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001533}
1534
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001535// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1536// is allocated for udp and stun. Also verify there is only one candidate
1537// (local) if stun candidate is same as local candidate, which will be the case
1538// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001539TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001540 AddInterface(kClientAddr);
1541 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001542 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1543 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001544 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001545 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1546 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001547 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001548 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001549 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1550 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001551}
1552
1553// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1554// is allocated for udp and stun. In this test we should expect both stun and
1555// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001556TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001557 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001558 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001559
1560 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001561 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1562 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001563 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001564 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1565 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001566 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001567 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1568 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1569 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001570 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1571 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001572 EXPECT_EQ(3U, candidates_.size());
1573}
1574
deadbeefc5d0d952015-07-16 10:22:21 -07001575// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001576TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001577 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001578 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001579 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001580
1581 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1582
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001583 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001584 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001585 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1586 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001587
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001588 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001589 session_->StartGettingPorts();
1590
deadbeef7f1563f2017-09-15 17:40:01 -07001591 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1592 kDefaultAllocationTimeout, fake_clock);
1593 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001594 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001595 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1596 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1597 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1598 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1599 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001600}
1601
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001602// Test that if prune_turn_ports is set, TCP TURN port will not be used
1603// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1604TEST_F(BasicPortAllocatorTest,
1605 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1606 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1607 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1608 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001609
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001610 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001611}
1612
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001613// Test that if prune_turn_ports is set, TCP TURN port will not be used
1614// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1615TEST_F(BasicPortAllocatorTest,
1616 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1617 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1618 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1619 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001620
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001621 TestUdpTurnPortPrunesTcpTurnPort();
1622}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001623
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001624// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1625// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1626TEST_F(BasicPortAllocatorTest,
1627 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1628 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1629 // first.
1630 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1631 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001632
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001633 TestIPv6TurnPortPrunesIPv4TurnPort();
1634}
1635
1636// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1637// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1638TEST_F(BasicPortAllocatorTest,
1639 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1640 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1641 // first.
1642 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1643 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1644
1645 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001646}
1647
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001648// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001649// will has its own set of TurnPorts based on their priorities, in the default
1650// case where no transit delay is set.
1651TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1652 TestEachInterfaceHasItsOwnTurnPorts();
1653}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001654
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001655// Tests that if prune_turn_ports is set, each network interface
1656// will has its own set of TurnPorts based on their priorities, given that
1657// IPv4/TCP TURN port becomes ready first.
1658TEST_F(BasicPortAllocatorTest,
1659 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1660 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1661 // becomes ready last.
1662 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1663 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1664 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1665 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001666
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001667 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001668}
1669
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001670// Testing DNS resolve for the TURN server, this will test AllocationSequence
1671// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001672// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1673// appears that it doesn't currently because of the DNS look up not
1674// using the fake clock.
1675TEST_F(BasicPortAllocatorTestWithRealClock,
1676 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001677 // This test relies on a real query for "localhost", so it won't work on an
1678 // IPv6-only machine.
1679 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001680 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001681 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001682 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001683 allocator_.reset(new BasicPortAllocator(&network_manager_));
1684 RelayServerConfig turn_server(RELAY_TURN);
1685 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001686 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001687 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001688 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001689 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001690
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001691 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001692 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001693 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1694 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001695
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001696 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001697 session_->StartGettingPorts();
1698
1699 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1700}
1701
1702// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1703// is allocated for udp/stun/turn. In this test we should expect all local,
1704// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001705TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001706 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001707 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001708
1709 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1710
1711 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001712 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1713 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001714
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001715 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001716 session_->StartGettingPorts();
1717
deadbeef7f1563f2017-09-15 17:40:01 -07001718 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1719 kDefaultAllocationTimeout, fake_clock);
1720 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001721 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001722 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1723 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1724 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1725 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1726 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001727 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1728 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001729 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001730 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1731 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001732 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1733 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1734}
1735
1736// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1737// server is also used as the STUN server, we should get 'local', 'stun', and
1738// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001739TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001740 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001741 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001742 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001743 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1744
1745 // Must set the step delay to 0 to make sure the relay allocation phase is
1746 // started before the STUN candidates are obtained, so that the STUN binding
1747 // response is processed when both StunPort and TurnPort exist to reproduce
1748 // webrtc issue 3537.
1749 allocator_->set_step_delay(0);
1750 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001751 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1752 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001753
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001754 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001755 session_->StartGettingPorts();
1756
deadbeef7f1563f2017-09-15 17:40:01 -07001757 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1758 kDefaultAllocationTimeout, fake_clock);
1759 EXPECT_EQ(3U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001760 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1761 Candidate stun_candidate;
1762 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1763 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1764 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1765 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1766 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001767
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001768 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-15 17:40:01 -07001769 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1770 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001771 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1772 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1773}
1774
deadbeefc5d0d952015-07-16 10:22:21 -07001775// Test that when only a TCP TURN server is available, we do NOT use it as
1776// a UDP STUN server, as this could leak our IP address. Thus we should only
1777// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001778TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001779 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001780 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001781 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001782 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1783
1784 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001785 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1786 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001787
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001788 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001789 session_->StartGettingPorts();
1790
deadbeef7f1563f2017-09-15 17:40:01 -07001791 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1792 kDefaultAllocationTimeout, fake_clock);
1793 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001794 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001795 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1796 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1797 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001798 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1799 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1800}
1801
1802// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1803// TURN server is used as the STUN server and we get 'local', 'stun', and
1804// 'relay' candidates.
1805// TODO(deadbeef): Remove this test when support for non-shared socket mode
1806// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001807TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001808 AddInterface(kClientAddr);
1809 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001810 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001811 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1812
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001813 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001814
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001815 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001816 session_->StartGettingPorts();
1817
deadbeef7f1563f2017-09-15 17:40:01 -07001818 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1819 kDefaultAllocationTimeout, fake_clock);
1820 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 10:22:21 -07001821 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001822 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1823 Candidate stun_candidate;
1824 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1825 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1826 Candidate turn_candidate;
1827 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1828 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1829 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001830 // Not using shared socket, so the STUN request's server reflexive address
1831 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001832 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001833
deadbeefc5d0d952015-07-16 10:22:21 -07001834 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1835 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1836 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1837}
1838
1839// Test that even when both a STUN and TURN server are configured, the TURN
1840// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001841TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001842 AddInterface(kClientAddr);
1843 // Configure with STUN server but destroy it, so we can ensure that it's
1844 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001845 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001846 stun_server_.reset();
1847 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1848
1849 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001850 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1851 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001852
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001853 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001854 session_->StartGettingPorts();
1855
pthatcher1749bc32017-02-08 13:18:00 -08001856 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1857 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001858 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1859 Candidate stun_candidate;
1860 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1861 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1862 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1863 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1864 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001865
1866 // Don't bother waiting for STUN timeout, since we already verified
1867 // that we got a STUN candidate from the TURN server.
1868}
1869
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001870// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1871// and fail to generate STUN candidate, local UDP candidate is generated
1872// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001873TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001874 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1875 PORTALLOCATOR_DISABLE_TCP |
1876 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001877 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1878 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001879 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001880 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001881 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1882 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001883 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001884 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001885 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001886 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1887 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001888 EXPECT_EQ(1U, candidates_.size());
1889}
1890
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001891// Test that when the NetworkManager doesn't have permission to enumerate
1892// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1893// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001894TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001895 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1896 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001897 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001898 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001899 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1900 PORTALLOCATOR_DISABLE_TCP |
1901 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1902 EXPECT_EQ(0U,
1903 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1904 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1905 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001906 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001907 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1908 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001909 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001910 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001911 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001912}
1913
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001914// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001915TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001916 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1917 PORTALLOCATOR_ENABLE_IPV6 |
1918 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001919 AddInterface(kClientIPv6Addr);
1920 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001921 allocator_->set_step_delay(kMinimumStepDelay);
1922 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001923 session_->StartGettingPorts();
deadbeef7f1563f2017-09-15 17:40:01 -07001924 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 13:18:00 -08001925 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-15 17:40:01 -07001926 EXPECT_EQ(4U, ports_.size());
1927 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001928 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1929 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1930 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1931 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001932}
honghaiz98db68f2015-09-29 07:58:17 -07001933
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001934TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001935 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001936 allocator_->set_step_delay(kDefaultStepDelay);
1937 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001938 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001939 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001940 EXPECT_EQ(2U, ports_.size());
1941 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001942 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001943
1944 // After stopping getting ports, adding a new interface will not start
1945 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001946 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001947 candidates_.clear();
1948 ports_.clear();
1949 candidate_allocation_done_ = false;
1950 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001951 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001952 EXPECT_EQ(0U, candidates_.size());
1953 EXPECT_EQ(0U, ports_.size());
1954}
1955
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001956TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001957 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001958 allocator_->set_step_delay(kDefaultStepDelay);
1959 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001960 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001961 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001962 EXPECT_EQ(2U, ports_.size());
1963 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001964 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001965
1966 // After clearing getting ports, adding a new interface will start getting
1967 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001968 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001969 candidates_.clear();
1970 ports_.clear();
1971 candidate_allocation_done_ = false;
1972 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001973 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001974 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001975 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1976 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001977}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001978
1979// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1980// a pooled session is taken out of the pool.
1981TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1982 AddInterface(kClientAddr);
1983 int pool_size = 1;
1984 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001985 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001986 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001987 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001988 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1989 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001990 // Expect that when TakePooledSession is called,
1991 // UpdateTransportInformationInternal will be called and the
1992 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1993 // candidates.
1994 session_ =
1995 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1996 ASSERT_NE(nullptr, session_.get());
1997 auto ready_ports = session_->ReadyPorts();
1998 auto candidates = session_->ReadyCandidates();
1999 EXPECT_FALSE(ready_ports.empty());
2000 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002001 for (const PortInterface* port_interface : ready_ports) {
2002 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002003 EXPECT_EQ(kContentName, port->content_name());
2004 EXPECT_EQ(1, port->component());
2005 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2006 EXPECT_EQ(kIcePwd0, port->password());
2007 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002008 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002009 EXPECT_EQ(1, candidate.component());
2010 EXPECT_EQ(kIceUfrag0, candidate.username());
2011 EXPECT_EQ(kIcePwd0, candidate.password());
2012 }
2013}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002014
2015// Test that a new candidate filter takes effect even on already-gathered
2016// candidates.
2017TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2018 AddInterface(kClientAddr);
2019 int pool_size = 1;
2020 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07002021 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002022 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2023 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08002024 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2025 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002026 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2027 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2028 allocator_->set_candidate_filter(CF_RELAY);
2029 // Assume that when TakePooledSession is called, the candidate filter will be
2030 // applied to the pooled session. This is tested by PortAllocatorTest.
2031 session_ =
2032 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2033 ASSERT_NE(nullptr, session_.get());
2034 auto candidates = session_->ReadyCandidates();
2035 auto ports = session_->ReadyPorts();
2036 // Sanity check that the number of candidates and ports decreased.
2037 EXPECT_GT(initial_candidates_size, candidates.size());
2038 EXPECT_GT(initial_ports_size, ports.size());
2039 for (const PortInterface* port : ports) {
2040 // Expect only relay ports.
2041 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2042 }
2043 for (const Candidate& candidate : candidates) {
2044 // Expect only relay candidates now that the filter is applied.
2045 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2046 // Expect that the raddr is emptied due to the CF_RELAY filter.
2047 EXPECT_EQ(candidate.related_address(),
2048 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2049 }
2050}
2051
2052} // namespace cricket