blob: 6977c59469ef1dad965ce2dc5a93df78f2800728 [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
deadbeef9a6f4d42017-05-15 19:43:33 -070041#define MAYBE_SKIP_IPV4 \
42 if (!rtc::HasIPv4Enabled()) { \
43 LOG(LS_INFO) << "No IPv4... skipping"; \
44 return; \
45 }
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;
113 };
114 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) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
384 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) {
392 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) {
407 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
408 // 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
honghaiz60347052016-05-31 18:29:12 -0700755// Test that high cost networks are filtered if the flag
756// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
757TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
758 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
759 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
760 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
761 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
762 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
763 // interface.
764 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
765 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
766 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
767 cricket::PORTALLOCATOR_DISABLE_RELAY |
768 cricket::PORTALLOCATOR_DISABLE_TCP |
769 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
770 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
771 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800772 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
773 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700774 EXPECT_EQ(1U, candidates_.size());
775 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
776
777 // If both cellular and unknown interfaces are present, only gather on the
778 // unknown interfaces.
779 candidates_.clear();
780 candidate_allocation_done_ = false;
781 RemoveInterface(addr_wifi);
782 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
783 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
784 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800785 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
786 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700787 EXPECT_EQ(2U, candidates_.size());
788 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
789 addr_unknown2.EqualIPs(candidates_[1].address())) ||
790 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
791 addr_unknown2.EqualIPs(candidates_[0].address())));
792
793 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
794 // Wi-Fi interface.
795 candidates_.clear();
796 candidate_allocation_done_ = false;
797 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
798 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800799 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
800 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700801 EXPECT_EQ(1U, candidates_.size());
802 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
803}
804
deadbeef3427f532017-07-26 16:09:33 -0700805// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
806// to gather candidates.
807TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
808 // Add three IPv6 network interfaces, but tell the allocator to only use two.
809 allocator().set_max_ipv6_networks(2);
810 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
811 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
812 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
813
814 // To simplify the test, only gather UDP host candidates.
815 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
816 PORTALLOCATOR_DISABLE_STUN |
817 PORTALLOCATOR_DISABLE_RELAY);
818
819 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
820 session_->StartGettingPorts();
821 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
822 kDefaultAllocationTimeout, fake_clock);
823 EXPECT_EQ(2U, candidates_.size());
824 // Ensure the expected two interfaces (eth0 and eth1) were used.
825 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
826 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr2);
827}
828
829// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
830// being used.
831TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
832 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
833 allocator().set_max_ipv6_networks(1);
834 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
835 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
836 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
837 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
838
839 // To simplify the test, only gather UDP host candidates.
840 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
841 PORTALLOCATOR_DISABLE_STUN |
842 PORTALLOCATOR_DISABLE_RELAY);
843
844 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
845 session_->StartGettingPorts();
846 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
847 kDefaultAllocationTimeout, fake_clock);
848 EXPECT_EQ(3U, candidates_.size());
849 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
850 // were used.
851 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
852 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
853 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr2);
854}
855
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700856// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700857TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800858 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
859 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700860 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
861 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
862 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700863 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800864 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
865 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700866 EXPECT_EQ(1U, candidates_.size());
867}
868
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000869// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700870TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000871 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700872 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800874 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
875 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000876 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700877 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
878 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
879 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
880 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
881 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
882 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
883 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
884 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000885 EXPECT_TRUE(candidate_allocation_done_);
886}
887
honghaiz8c404fa2015-09-28 07:59:43 -0700888// Test that when the same network interface is brought down and up, the
889// port allocator session will restart a new allocation sequence if
890// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700891TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700892 std::string if_name("test_net0");
893 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700894 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700895 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800896 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
897 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700898 EXPECT_EQ(4U, ports_.size());
899 EXPECT_TRUE(candidate_allocation_done_);
900 candidate_allocation_done_ = false;
901 candidates_.clear();
902 ports_.clear();
903
904 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800905 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
906 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700907 EXPECT_EQ(0U, ports_.size());
908 EXPECT_FALSE(candidate_allocation_done_);
909
910 // When the same interfaces are added again, new candidates/ports should be
911 // generated.
912 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800913 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
914 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700915 EXPECT_EQ(4U, ports_.size());
916 EXPECT_TRUE(candidate_allocation_done_);
917}
918
919// Test that when the same network interface is brought down and up, the
920// port allocator session will not restart a new allocation sequence if
921// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700922TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700923 std::string if_name("test_net0");
924 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700925 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700926 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800927 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
928 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700929 EXPECT_EQ(4U, ports_.size());
930 EXPECT_TRUE(candidate_allocation_done_);
931 session_->StopGettingPorts();
932 candidates_.clear();
933 ports_.clear();
934
935 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800936 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
937 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700938 EXPECT_EQ(0U, ports_.size());
939
940 // When the same interfaces are added again, new candidates/ports should not
941 // be generated because the session has stopped.
942 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800943 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
944 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700945 EXPECT_EQ(0U, ports_.size());
946 EXPECT_TRUE(candidate_allocation_done_);
947}
948
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000949// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700950TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000951 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700952 allocator_->set_step_delay(kDefaultStepDelay);
953 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000954 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800955 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000956 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800957 ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000958 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700959 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
960 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800961 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700962 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
963 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000964 EXPECT_EQ(4U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800965 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700966 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
967 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000968 EXPECT_EQ(4U, ports_.size());
969 EXPECT_TRUE(candidate_allocation_done_);
970 // If we Stop gathering now, we shouldn't get a second "done" callback.
971 session_->StopGettingPorts();
972}
973
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700974TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000975 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700976 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000977 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800978 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
979 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 EXPECT_TRUE(candidate_allocation_done_);
981 // If we Stop gathering now, we shouldn't get a second "done" callback.
982 session_->StopGettingPorts();
983
984 // All ports should have unset send-buffer sizes.
985 CheckSendBufferSizesOfAllPorts(-1);
986}
987
988// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700989TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000990 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700991 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000992 session_->StartGettingPorts();
993 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
994 EXPECT_EQ(2U, ports_.size());
995 session_->StopGettingPorts();
996 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
997}
998
999// Test that we restrict client ports appropriately when a port range is set.
1000// We check the candidates for udp/stun/tcp ports, and the from address
1001// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001002TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001003 AddInterface(kClientAddr);
1004 // Check that an invalid port range fails.
1005 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1006 // Check that a null port range succeeds.
1007 EXPECT_TRUE(SetPortRange(0, 0));
1008 // Check that a valid port range succeeds.
1009 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001010 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001011 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001012 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1013 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001014 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -07001015
1016 int num_nonrelay_candidates = 0;
1017 for (const Candidate& candidate : candidates_) {
1018 // Check the port number for the UDP/STUN/TCP port objects.
1019 if (candidate.type() != RELAY_PORT_TYPE) {
1020 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
1021 ++num_nonrelay_candidates;
1022 }
1023 }
1024 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001025 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001026 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
1027 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001028 EXPECT_TRUE(candidate_allocation_done_);
1029}
1030
deadbeefe97389c2016-12-23 01:43:45 -08001031// Test that if we have no network adapters, we bind to the ANY address and
1032// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001033TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -08001034 // Default config uses GTURN and no NAT, so replace that with the
1035 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1036 ResetWithStunServerAndNat(kStunAddr);
1037 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1038 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1039 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1040 // Disable IPv6, because our test infrastructure doesn't support having IPv4
1041 // behind a NAT but IPv6 not, or having an IPv6 NAT.
1042 // TODO(deadbeef): Fix this.
1043 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001044 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001045 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001046 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1047 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -08001048 EXPECT_EQ(4U, ports_.size());
1049 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1050 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1051 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1052 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1053 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1054 // The "any" address port should be in the signaled ready ports, but the host
1055 // candidate for it is useless and shouldn't be signaled. So we only have
1056 // STUN/TURN candidates.
1057 EXPECT_EQ(3U, candidates_.size());
1058 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1059 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1060 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1061 // server.
1062 EXPECT_EQ(2,
1063 CountCandidates(candidates_, "relay", "udp",
1064 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001065}
1066
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001067// Test that when enumeration is disabled, we should not have any ports when
1068// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001069TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001070 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001071 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001072 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001073 // Expect to see no ports and no candidates.
1074 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1075 rtc::IPAddress(), rtc::IPAddress());
1076}
1077
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001078// Test that even with multiple interfaces, the result should still be a single
1079// default private, one STUN and one TURN candidate since we bind to any address
1080// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001081TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001082 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001083 AddInterface(kPrivateAddr);
1084 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001085 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001086 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001087
1088 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1089 // address set and we have no IPv6 STUN server, there should be no IPv6
1090 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001091 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1092 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001093
1094 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1095 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1096 // TURN/UDP candidates.
1097 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001098 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1099 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001100}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001101
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001102// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1103// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001104TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001105 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001106 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001107 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001108 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001109 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1110 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1111 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1112 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001113 kTurnUdpExtAddr.ipaddr());
1114}
1115
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001116// Test that when adapter enumeration is disabled, for endpoints without
1117// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001118TEST_F(BasicPortAllocatorTest,
1119 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001120 ResetWithNoServersOrNat();
1121 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1122 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1123 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001124}
1125
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001126// Test that when adapter enumeration is disabled, with
1127// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1128// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001129TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001130 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1131 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001132 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1133 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001134 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1135 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001136 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001137 rtc::IPAddress(), rtc::IPAddress());
1138}
1139
1140// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001141// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1142// a NAT, there is no local candidate. However, this specified default route
1143// (kClientAddr) which was discovered when sending STUN requests, will become
1144// the srflx addresses.
1145TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001146 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001147 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001148 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001149 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001150 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1151 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001152 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1153 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001154 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1155 rtc::IPAddress(), rtc::IPAddress());
1156}
1157
1158// Test that when adapter enumeration is disabled, with
1159// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1160// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001161TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001162 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1163 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001164 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1165 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001166 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1167 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1168 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001169}
1170
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001171// Test that we disable relay over UDP, and only TCP is used when connecting to
1172// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001173TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001174 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001175 AddInterface(kClientAddr);
1176 ResetWithStunServerAndNat(kStunAddr);
1177 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001178 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1179 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1180 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1181 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001182
1183 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001184 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1185 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001186
1187 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1188 // TURN/TCP candidates.
1189 EXPECT_EQ(2U, ports_.size());
1190 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001191 Candidate turn_candidate;
1192 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1193 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001194 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001195 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1196 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001197}
1198
Erik Språngefdce692015-06-05 09:41:26 +02001199// Disable for asan, see
1200// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1201#if !defined(ADDRESS_SANITIZER)
1202
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001203// Test that we can get OnCandidatesAllocationDone callback when all the ports
1204// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001205TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001206 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001207 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1208 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1209 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001210 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001211 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001213}
1214
1215// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001216TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001217 AddInterface(kClientAddr);
1218 fss_->set_udp_sockets_enabled(false);
1219 EXPECT_TRUE(CreateSession(1));
1220 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001221 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1222 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001223 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001224 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1225 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1226 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1227 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1228 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1229 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230 EXPECT_TRUE(candidate_allocation_done_);
1231}
1232
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001233#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001234
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001235// Test that we don't crash or malfunction if we can't create UDP sockets or
1236// listen on TCP sockets. We still give out a local TCP address, since
1237// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001238TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001239 AddInterface(kClientAddr);
1240 fss_->set_udp_sockets_enabled(false);
1241 fss_->set_tcp_listen_enabled(false);
1242 EXPECT_TRUE(CreateSession(1));
1243 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001244 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1245 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001246 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001247 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1248 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1249 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1250 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1251 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001252 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001253 EXPECT_TRUE(candidate_allocation_done_);
1254}
1255
1256// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001257// TODO(deadbeef): Find a way to exit early here.
1258TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001259 AddInterface(kClientAddr);
1260 fss_->set_tcp_sockets_enabled(false);
1261 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001262 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001263 session_->StartGettingPorts();
1264 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001265 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001266 // In case of Relay, ports creation will succeed but sockets will fail.
1267 // There is no error reporting from RelayEntry to handle this failure.
1268}
1269
1270// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001271TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001272 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1273 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001274 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001275 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001276 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1277 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001278 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001279 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1280 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001281 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001282 // will be tried after about 3 seconds.
1283 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001284 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001285 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1286 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1287 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1288 kRelaySslTcpIntAddr);
1289 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001290 // We wait at least for a full STUN timeout, which
1291 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1292 // already passed (see above), we wait 3 seconds less than that.
1293 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1294 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001295}
1296
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001297TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001298 AddInterface(kClientAddr);
1299 AddInterface(kClientAddr2);
1300 // Allocating only host UDP ports. This is done purely for testing
1301 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001302 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1303 PORTALLOCATOR_DISABLE_RELAY);
1304 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
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_,
1307 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001308 ASSERT_EQ(2U, candidates_.size());
1309 EXPECT_EQ(2U, ports_.size());
1310 // Candidates priorities should be different.
1311 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1312}
1313
1314// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001317 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001318 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001319 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1320 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001321 EXPECT_EQ(4U, ports_.size());
1322 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001323 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001324}
1325
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001326// Test that the allocator session uses the candidate filter it's created with,
1327// rather than the filter of its parent allocator.
1328// The filter of the allocator should only affect the next gathering phase,
1329// according to JSEP, which means the *next* allocator session returned.
1330TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1331 AddInterface(kClientAddr);
1332 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1333 // Set candidate filter *after* creating the session. Should have no effect.
1334 allocator().set_candidate_filter(CF_RELAY);
1335 session_->StartGettingPorts();
1336 // 7 candidates and 4 ports is what we would normally get (see the
1337 // TestGetAllPorts* tests).
pthatcher1749bc32017-02-08 13:18:00 -08001338 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1339 fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001340 EXPECT_EQ(4U, ports_.size());
1341 EXPECT_TRUE(candidate_allocation_done_);
1342}
1343
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001344// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1345// This test also verifies that when the allocator is only allowed to use
1346// relay (i.e. IceTransportsType is relay), the raddr is an empty
1347// address with the correct family. This is to prevent any local
1348// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001349TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001350 AddInterface(kClientAddr);
1351 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001352 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001353 allocator().set_candidate_filter(CF_RELAY);
1354 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001355 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001356 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1357 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001358 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1359 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360
1361 EXPECT_EQ(1U, candidates_.size());
1362 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001363 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1364 EXPECT_EQ(
1365 candidates_[0].related_address(),
1366 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001367}
1368
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001369TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001370 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001371 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1372 allocator().set_candidate_filter(CF_HOST);
1373 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001374 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001375 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1376 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001377 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1378 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001379 for (const Candidate& candidate : candidates_) {
1380 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001381 }
1382}
1383
1384// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001385TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001386 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001387 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001388
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001389 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1390 allocator().set_candidate_filter(CF_REFLEXIVE);
1391 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001392 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001393 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1394 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001395 // Host is behind NAT, no private address will be exposed. Hence only UDP
1396 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001397 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1398 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001399 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1400 EXPECT_EQ(
1401 candidates_[0].related_address(),
1402 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001403}
1404
1405// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001406TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001407 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001408 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1409 allocator().set_candidate_filter(CF_REFLEXIVE);
1410 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001411 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001412 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1413 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001415 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001417 for (const Candidate& candidate : candidates_) {
1418 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419 }
1420}
1421
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001422// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001423TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001424 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001425 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001426 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001427 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1428 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001429 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1430 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1431 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001433 for (const Candidate& candidate : candidates_) {
1434 EXPECT_EQ(kIceUfrag0, candidate.username());
1435 EXPECT_EQ(kIcePwd0, candidate.password());
1436 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001437 EXPECT_TRUE(candidate_allocation_done_);
1438}
1439
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001440// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1441// is allocated for udp and stun. Also verify there is only one candidate
1442// (local) if stun candidate is same as local candidate, which will be the case
1443// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001444TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001445 AddInterface(kClientAddr);
1446 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001447 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1448 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001449 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001450 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1451 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001452 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001453 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001454 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1455 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001456}
1457
1458// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1459// is allocated for udp and stun. In this test we should expect both stun and
1460// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001461TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001462 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001463 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001464
1465 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001466 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1467 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001468 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001469 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1470 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001471 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001472 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1473 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1474 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001475 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1476 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001477 EXPECT_EQ(3U, candidates_.size());
1478}
1479
deadbeefc5d0d952015-07-16 10:22:21 -07001480// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001481TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001482 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001483 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001484 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001485
1486 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1487
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001488 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001489 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001490 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1491 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001492
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001493 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001494 session_->StartGettingPorts();
1495
pthatcher1749bc32017-02-08 13:18:00 -08001496 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1497 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001498 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001499 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1500 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1501 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1502 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1503 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001504 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1505 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001506 EXPECT_EQ(3U, candidates_.size());
1507}
1508
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001509// Test that if prune_turn_ports is set, TCP TURN port will not be used
1510// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1511TEST_F(BasicPortAllocatorTest,
1512 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1513 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1514 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1515 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001516
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001517 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001518}
1519
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001520// Test that if prune_turn_ports is set, TCP TURN port will not be used
1521// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1522TEST_F(BasicPortAllocatorTest,
1523 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1524 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1525 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1526 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001527
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001528 TestUdpTurnPortPrunesTcpTurnPort();
1529}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001530
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001531// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1532// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1533TEST_F(BasicPortAllocatorTest,
1534 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1535 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1536 // first.
1537 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1538 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001539
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001540 TestIPv6TurnPortPrunesIPv4TurnPort();
1541}
1542
1543// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1544// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1545TEST_F(BasicPortAllocatorTest,
1546 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1547 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1548 // first.
1549 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1550 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1551
1552 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001553}
1554
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001555// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001556// will has its own set of TurnPorts based on their priorities, in the default
1557// case where no transit delay is set.
1558TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1559 TestEachInterfaceHasItsOwnTurnPorts();
1560}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001561
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001562// Tests that if prune_turn_ports is set, each network interface
1563// will has its own set of TurnPorts based on their priorities, given that
1564// IPv4/TCP TURN port becomes ready first.
1565TEST_F(BasicPortAllocatorTest,
1566 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1567 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1568 // becomes ready last.
1569 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1570 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1571 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1572 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001573
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001574 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001575}
1576
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001577// Testing DNS resolve for the TURN server, this will test AllocationSequence
1578// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001579// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1580// appears that it doesn't currently because of the DNS look up not
1581// using the fake clock.
1582TEST_F(BasicPortAllocatorTestWithRealClock,
1583 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001584 // This test relies on a real query for "localhost", so it won't work on an
1585 // IPv6-only machine.
1586 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001587 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001588 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001589 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001590 allocator_.reset(new BasicPortAllocator(&network_manager_));
1591 RelayServerConfig turn_server(RELAY_TURN);
1592 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001593 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001594 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001595 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001596 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001597
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001598 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001600 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1601 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001602
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001603 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001604 session_->StartGettingPorts();
1605
1606 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1607}
1608
1609// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1610// is allocated for udp/stun/turn. In this test we should expect all local,
1611// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001612TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001613 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001614 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001615
1616 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1617
1618 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001619 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1620 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001621
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001622 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623 session_->StartGettingPorts();
1624
pthatcher1749bc32017-02-08 13:18:00 -08001625 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1626 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001627 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001628 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1629 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1630 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1631 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1632 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001633 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1634 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001635 EXPECT_EQ(3U, candidates_.size());
1636 // Local port will be created first and then TURN port.
1637 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1638 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1639}
1640
1641// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1642// server is also used as the STUN server, we should get 'local', 'stun', and
1643// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001644TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001645 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001646 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001647 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001648 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1649
1650 // Must set the step delay to 0 to make sure the relay allocation phase is
1651 // started before the STUN candidates are obtained, so that the STUN binding
1652 // response is processed when both StunPort and TurnPort exist to reproduce
1653 // webrtc issue 3537.
1654 allocator_->set_step_delay(0);
1655 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001656 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1657 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001658
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001659 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001660 session_->StartGettingPorts();
1661
pthatcher1749bc32017-02-08 13:18:00 -08001662 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1663 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001664 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1665 Candidate stun_candidate;
1666 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1667 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1668 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1669 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1670 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001671
pthatcher1749bc32017-02-08 13:18:00 -08001672 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1673 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001674 EXPECT_EQ(3U, candidates_.size());
1675 // Local port will be created first and then TURN port.
1676 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1677 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1678}
1679
deadbeefc5d0d952015-07-16 10:22:21 -07001680// Test that when only a TCP TURN server is available, we do NOT use it as
1681// a UDP STUN server, as this could leak our IP address. Thus we should only
1682// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001683TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001684 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001685 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001686 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001687 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1688
1689 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001690 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1691 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001692
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001693 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001694 session_->StartGettingPorts();
1695
pthatcher1749bc32017-02-08 13:18:00 -08001696 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1697 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001698 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001699 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1700 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1701 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001702 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1703 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001704 EXPECT_EQ(2U, candidates_.size());
1705 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1706 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1707}
1708
1709// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1710// TURN server is used as the STUN server and we get 'local', 'stun', and
1711// 'relay' candidates.
1712// TODO(deadbeef): Remove this test when support for non-shared socket mode
1713// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001714TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001715 AddInterface(kClientAddr);
1716 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001717 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001718 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1719
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001720 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001721
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001722 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001723 session_->StartGettingPorts();
1724
pthatcher1749bc32017-02-08 13:18:00 -08001725 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1726 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001727 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001728 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1729 Candidate stun_candidate;
1730 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1731 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1732 Candidate turn_candidate;
1733 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1734 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1735 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001736 // Not using shared socket, so the STUN request's server reflexive address
1737 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001738 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001739
pthatcher1749bc32017-02-08 13:18:00 -08001740 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1741 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001742 EXPECT_EQ(3U, candidates_.size());
1743 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1744 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1745 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1746}
1747
1748// Test that even when both a STUN and TURN server are configured, the TURN
1749// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001750TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001751 AddInterface(kClientAddr);
1752 // Configure with STUN server but destroy it, so we can ensure that it's
1753 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001754 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001755 stun_server_.reset();
1756 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1757
1758 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001759 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1760 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001761
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001762 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001763 session_->StartGettingPorts();
1764
pthatcher1749bc32017-02-08 13:18:00 -08001765 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1766 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001767 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1768 Candidate stun_candidate;
1769 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1770 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1771 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1772 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1773 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001774
1775 // Don't bother waiting for STUN timeout, since we already verified
1776 // that we got a STUN candidate from the TURN server.
1777}
1778
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001779// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1780// and fail to generate STUN candidate, local UDP candidate is generated
1781// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001782TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001783 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1784 PORTALLOCATOR_DISABLE_TCP |
1785 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001786 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1787 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001788 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001789 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001790 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1791 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001792 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001793 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001794 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001795 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1796 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001797 EXPECT_EQ(1U, candidates_.size());
1798}
1799
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001800// Test that when the NetworkManager doesn't have permission to enumerate
1801// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1802// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001803TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001804 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1805 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001806 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001807 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001808 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1809 PORTALLOCATOR_DISABLE_TCP |
1810 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1811 EXPECT_EQ(0U,
1812 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1813 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1814 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001815 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001816 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1817 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001818 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001819 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001820 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001821}
1822
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001823// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001824TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001825 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1826 PORTALLOCATOR_ENABLE_IPV6 |
1827 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001828 AddInterface(kClientIPv6Addr);
1829 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001830 allocator_->set_step_delay(kMinimumStepDelay);
1831 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001832 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001833 ASSERT_EQ_SIMULATED_WAIT(4U, ports_.size(), kDefaultAllocationTimeout,
1834 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001835 EXPECT_EQ(4U, candidates_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001836 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1837 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001838 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1839 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1840 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1841 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001842 EXPECT_EQ(4U, candidates_.size());
1843}
honghaiz98db68f2015-09-29 07:58:17 -07001844
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001845TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001846 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001847 allocator_->set_step_delay(kDefaultStepDelay);
1848 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001849 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001850 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001851 EXPECT_EQ(2U, ports_.size());
1852 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001853 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001854
1855 // After stopping getting ports, adding a new interface will not start
1856 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001857 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001858 candidates_.clear();
1859 ports_.clear();
1860 candidate_allocation_done_ = false;
1861 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001862 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001863 EXPECT_EQ(0U, candidates_.size());
1864 EXPECT_EQ(0U, ports_.size());
1865}
1866
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001867TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001868 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001869 allocator_->set_step_delay(kDefaultStepDelay);
1870 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001871 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001872 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001873 EXPECT_EQ(2U, ports_.size());
1874 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001875 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001876
1877 // After clearing getting ports, adding a new interface will start getting
1878 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001879 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001880 candidates_.clear();
1881 ports_.clear();
1882 candidate_allocation_done_ = false;
1883 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001884 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001885 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001886 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1887 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001888}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001889
1890// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1891// a pooled session is taken out of the pool.
1892TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1893 AddInterface(kClientAddr);
1894 int pool_size = 1;
1895 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001896 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001897 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001898 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001899 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1900 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001901 // Expect that when TakePooledSession is called,
1902 // UpdateTransportInformationInternal will be called and the
1903 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1904 // candidates.
1905 session_ =
1906 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1907 ASSERT_NE(nullptr, session_.get());
1908 auto ready_ports = session_->ReadyPorts();
1909 auto candidates = session_->ReadyCandidates();
1910 EXPECT_FALSE(ready_ports.empty());
1911 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001912 for (const PortInterface* port_interface : ready_ports) {
1913 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001914 EXPECT_EQ(kContentName, port->content_name());
1915 EXPECT_EQ(1, port->component());
1916 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1917 EXPECT_EQ(kIcePwd0, port->password());
1918 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001919 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001920 EXPECT_EQ(1, candidate.component());
1921 EXPECT_EQ(kIceUfrag0, candidate.username());
1922 EXPECT_EQ(kIcePwd0, candidate.password());
1923 }
1924}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001925
1926// Test that a new candidate filter takes effect even on already-gathered
1927// candidates.
1928TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1929 AddInterface(kClientAddr);
1930 int pool_size = 1;
1931 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001932 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001933 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1934 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001935 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1936 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001937 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1938 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1939 allocator_->set_candidate_filter(CF_RELAY);
1940 // Assume that when TakePooledSession is called, the candidate filter will be
1941 // applied to the pooled session. This is tested by PortAllocatorTest.
1942 session_ =
1943 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1944 ASSERT_NE(nullptr, session_.get());
1945 auto candidates = session_->ReadyCandidates();
1946 auto ports = session_->ReadyPorts();
1947 // Sanity check that the number of candidates and ports decreased.
1948 EXPECT_GT(initial_candidates_size, candidates.size());
1949 EXPECT_GT(initial_ports_size, ports.size());
1950 for (const PortInterface* port : ports) {
1951 // Expect only relay ports.
1952 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1953 }
1954 for (const Candidate& candidate : candidates) {
1955 // Expect only relay candidates now that the filter is applied.
1956 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1957 // Expect that the raddr is emptied due to the CF_RELAY filter.
1958 EXPECT_EQ(candidate.related_address(),
1959 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1960 }
1961}
1962
1963} // namespace cricket