blob: bb3e0d71a3228738efcbc863cabbee8d5997e170 [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
pthatcher1749bc32017-02-08 13:18:00 -080014#include "webrtc/base/fakeclock.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015#include "webrtc/base/fakenetwork.h"
16#include "webrtc/base/firewallsocketserver.h"
17#include "webrtc/base/gunit.h"
18#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070019#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020#include "webrtc/base/logging.h"
21#include "webrtc/base/natserver.h"
22#include "webrtc/base/natsocketfactory.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070023#include "webrtc/base/nethelpers.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#include "webrtc/base/network.h"
25#include "webrtc/base/physicalsocketserver.h"
26#include "webrtc/base/socketaddress.h"
27#include "webrtc/base/ssladapter.h"
28#include "webrtc/base/thread.h"
29#include "webrtc/base/virtualsocketserver.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070030#include "webrtc/p2p/base/basicpacketsocketfactory.h"
31#include "webrtc/p2p/base/p2pconstants.h"
32#include "webrtc/p2p/base/p2ptransportchannel.h"
33#include "webrtc/p2p/base/testrelayserver.h"
34#include "webrtc/p2p/base/teststunserver.h"
35#include "webrtc/p2p/base/testturnserver.h"
36#include "webrtc/p2p/client/basicportallocator.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080038using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039using rtc::SocketAddress;
40using rtc::Thread;
41
deadbeef9a6f4d42017-05-15 19:43:33 -070042#define MAYBE_SKIP_IPV4 \
43 if (!rtc::HasIPv4Enabled()) { \
44 LOG(LS_INFO) << "No IPv4... skipping"; \
45 return; \
46 }
47
deadbeefe97389c2016-12-23 01:43:45 -080048static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070050static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070051static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000053static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070054static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
55 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070056static const SocketAddress kClientIPv6Addr2(
57 "2401:fa00:4:2000:be30:5bff:fee5:c3",
58 0);
deadbeefc5d0d952015-07-16 10:22:21 -070059static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
60static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
62static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
63static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
64static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
65static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
66static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
67static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
68static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
69static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070070static const SocketAddress kTurnUdpIntIPv6Addr(
71 "2402:fb00:4:1000:be30:5bff:fee5:c3",
72 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070074static const SocketAddress kTurnTcpIntIPv6Addr(
75 "2402:fb00:4:2000:be30:5bff:fee5:c3",
76 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
78
79// Minimum and maximum port for port range tests.
80static const int kMinPort = 10000;
81static const int kMaxPort = 10099;
82
83// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070084static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085// Based on ICE_PWD_LENGTH
86static const char kIcePwd0[] = "TESTICEPWD00000000000000";
87
88static const char kContentName[] = "test content";
89
Honghai Zhangc67e0f52016-09-19 16:57:37 -070090static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091static const char kTurnUsername[] = "test";
92static const char kTurnPassword[] = "test";
93
pthatcher94a2f212017-02-08 14:42:22 -080094// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070095// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080096static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070097
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098namespace cricket {
99
100// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700101std::ostream& operator<<(std::ostream& os,
102 const std::vector<Candidate>& candidates) {
103 os << '[';
104 bool first = true;
105 for (const Candidate& c : candidates) {
106 if (!first) {
107 os << ", ";
108 }
109 os << c.ToString();
110 first = false;
111 };
112 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 return os;
114}
115
pthatcher1749bc32017-02-08 13:18:00 -0800116class BasicPortAllocatorTestBase : public testing::Test,
117 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118 public:
pthatcher1749bc32017-02-08 13:18:00 -0800119 BasicPortAllocatorTestBase()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 : pss_(new rtc::PhysicalSocketServer),
121 vss_(new rtc::VirtualSocketServer(pss_.get())),
122 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700123 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800124 // Note that the NAT is not used by default. ResetWithStunServerAndNat
125 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700126 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700127 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700128 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700129 relay_server_(Thread::Current(),
130 kRelayUdpIntAddr,
131 kRelayUdpExtAddr,
132 kRelayTcpIntAddr,
133 kRelayTcpExtAddr,
134 kRelaySslTcpIntAddr,
135 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
137 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700138 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 stun_servers.insert(kStunAddr);
140 // Passing the addresses of GTURN servers will enable GTURN in
141 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800142 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
143 // configuration should be blank by default (preferred), or it should use
144 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700145 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
146 kRelayUdpIntAddr, kRelayTcpIntAddr,
147 kRelaySslTcpIntAddr));
148 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000149 }
150
151 void AddInterface(const SocketAddress& addr) {
152 network_manager_.AddInterface(addr);
153 }
honghaiz8c404fa2015-09-28 07:59:43 -0700154 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
155 network_manager_.AddInterface(addr, if_name);
156 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800157 void AddInterface(const SocketAddress& addr,
158 const std::string& if_name,
159 rtc::AdapterType type) {
160 network_manager_.AddInterface(addr, if_name, type);
161 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800162 // The default route is the public address that STUN server will observe when
163 // the endpoint is sitting on the public internet and the local port is bound
164 // to the "any" address. This may be different from the default local address
165 // which the endpoint observes. This can occur if the route to the public
166 // endpoint like 8.8.8.8 (specified as the default local address) is
167 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700168 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
169 AddInterface(addr);
170 // When a binding comes from the any address, the |addr| will be used as the
171 // srflx address.
172 vss_->SetDefaultRoute(addr.ipaddr());
173 }
honghaiz8c404fa2015-09-28 07:59:43 -0700174 void RemoveInterface(const SocketAddress& addr) {
175 network_manager_.RemoveInterface(addr);
176 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 bool SetPortRange(int min_port, int max_port) {
178 return allocator_->SetPortRange(min_port, max_port);
179 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700180 // Endpoint is on the public network. No STUN or TURN.
181 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700182 allocator_.reset(new BasicPortAllocator(&network_manager_));
183 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700184 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700185 // Endpoint is behind a NAT, with STUN specified.
186 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
187 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700188 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700189 // Endpoint is on the public network, with STUN specified.
190 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
191 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700193 // Endpoint is on the public network, with TURN specified.
194 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
195 const rtc::SocketAddress& tcp_turn) {
196 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 AddTurnServers(udp_turn, tcp_turn);
198 }
199
200 void AddTurnServers(const rtc::SocketAddress& udp_turn,
201 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700202 RelayServerConfig turn_server(RELAY_TURN);
203 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800204 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205
206 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800207 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 }
209 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800210 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 }
deadbeef653b8e02015-11-11 12:55:10 -0800212 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 }
214
215 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700216 session_ = CreateSession("session", component);
217 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700219 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 return true;
221 }
222
223 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700224 session_ = CreateSession("session", content_name, component);
225 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700227 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228 return true;
229 }
230
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700231 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
232 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 return CreateSession(sid, kContentName, component);
234 }
235
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700236 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700237 const std::string& sid,
238 const std::string& content_name,
239 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
241 }
242
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700243 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 const std::string& sid,
245 const std::string& content_name,
246 int component,
247 const std::string& ice_ufrag,
248 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700249 std::unique_ptr<PortAllocatorSession> session =
250 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800252 &BasicPortAllocatorTestBase::OnPortReady);
253 session->SignalPortsPruned.connect(
254 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700255 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800256 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700257 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800258 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700259 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800260 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000261 return session;
262 }
263
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700264 // Return true if the addresses are the same, or the port is 0 in |pattern|
265 // (acting as a wildcard) and the IPs are the same.
266 // Even with a wildcard port, the port of the address should be nonzero if
267 // the IP is nonzero.
268 static bool AddressMatch(const SocketAddress& address,
269 const SocketAddress& pattern) {
270 return address.ipaddr() == pattern.ipaddr() &&
271 ((pattern.port() == 0 &&
272 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
273 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700275
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700276 // Returns the number of ports that have matching type, protocol and
277 // address.
278 static int CountPorts(const std::vector<PortInterface*>& ports,
279 const std::string& type,
280 ProtocolType protocol,
281 const SocketAddress& client_addr) {
282 return std::count_if(
283 ports.begin(), ports.end(),
284 [type, protocol, client_addr](PortInterface* port) {
285 return port->Type() == type && port->GetProtocol() == protocol &&
286 port->Network()->GetBestIP() == client_addr.ipaddr();
287 });
288 }
289
deadbeefe97389c2016-12-23 01:43:45 -0800290 static int CountCandidates(const std::vector<Candidate>& candidates,
291 const std::string& type,
292 const std::string& proto,
293 const SocketAddress& addr) {
294 return std::count_if(candidates.begin(), candidates.end(),
295 [type, proto, addr](const Candidate& c) {
296 return c.type() == type && c.protocol() == proto &&
297 AddressMatch(c.address(), addr);
298 });
299 }
300
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700301 // Find a candidate and return it.
302 static bool FindCandidate(const std::vector<Candidate>& candidates,
303 const std::string& type,
304 const std::string& proto,
305 const SocketAddress& addr,
306 Candidate* found) {
307 auto it = std::find_if(candidates.begin(), candidates.end(),
308 [type, proto, addr](const Candidate& c) {
309 return c.type() == type && c.protocol() == proto &&
310 AddressMatch(c.address(), addr);
311 });
312 if (it != candidates.end() && found) {
313 *found = *it;
314 }
315 return it != candidates.end();
316 }
317
318 // Convenience method to call FindCandidate with no return.
319 static bool HasCandidate(const std::vector<Candidate>& candidates,
320 const std::string& type,
321 const std::string& proto,
322 const SocketAddress& addr) {
323 return FindCandidate(candidates, type, proto, addr, nullptr);
324 }
325
326 // Version of HasCandidate that also takes a related address.
327 static bool HasCandidateWithRelatedAddr(
328 const std::vector<Candidate>& candidates,
329 const std::string& type,
330 const std::string& proto,
331 const SocketAddress& addr,
332 const SocketAddress& related_addr) {
333 auto it =
334 std::find_if(candidates.begin(), candidates.end(),
335 [type, proto, addr, related_addr](const Candidate& c) {
336 return c.type() == type && c.protocol() == proto &&
337 AddressMatch(c.address(), addr) &&
338 AddressMatch(c.related_address(), related_addr);
339 });
340 return it != candidates.end();
341 }
342
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700344 int min_port,
345 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000346 return (addr.port() >= min_port && addr.port() <= max_port);
347 }
348
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700349 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350 // We should only get this callback once, except in the mux test where
351 // we have multiple port allocation sessions.
352 if (session == session_.get()) {
353 ASSERT_FALSE(candidate_allocation_done_);
354 candidate_allocation_done_ = true;
355 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700356 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000357 }
358
359 // Check if all ports allocated have send-buffer size |expected|. If
360 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
361 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700362 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000363 for (it = ports_.begin(); it < ports_.end(); ++it) {
364 int send_buffer_size;
365 if (expected == -1) {
366 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700367 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000368 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700369 EXPECT_EQ(0,
370 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 ASSERT_EQ(expected, send_buffer_size);
372 }
373 }
374 }
375
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700376 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
377
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700379 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700381 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000382 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
383 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700384 // Make sure the new port is added to ReadyPorts.
385 auto ready_ports = ses->ReadyPorts();
386 EXPECT_NE(ready_ports.end(),
387 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700389 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700390 const std::vector<PortInterface*>& pruned_ports) {
391 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700392 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700393 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700394 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700395 new_end = std::remove(ports_.begin(), new_end, port);
396 // Make sure the pruned port is not in ReadyPorts.
397 EXPECT_EQ(ready_ports.end(),
398 std::find(ready_ports.begin(), ready_ports.end(), port));
399 }
400 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700401 }
402
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700403 void OnCandidatesReady(PortAllocatorSession* ses,
404 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700405 for (const Candidate& candidate : candidates) {
406 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
407 // Sanity check that the ICE component is set.
408 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
409 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700411 // Make sure the new candidates are added to Candidates.
412 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700413 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700414 EXPECT_NE(
415 ses_candidates.end(),
416 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
417 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 }
419
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700420 void OnCandidatesRemoved(PortAllocatorSession* session,
421 const std::vector<Candidate>& removed_candidates) {
422 auto new_end = std::remove_if(
423 candidates_.begin(), candidates_.end(),
424 [removed_candidates](Candidate& candidate) {
425 for (const Candidate& removed_candidate : removed_candidates) {
426 if (candidate.MatchesForRemoval(removed_candidate)) {
427 return true;
428 }
429 }
430 return false;
431 });
432 candidates_.erase(new_end, candidates_.end());
433 }
434
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700435 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800436 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700437 RelayServerConfig server_config = allocator_->turn_servers()[i];
438 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700440 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000441 if (proto_addr.address == relay_port->address &&
442 proto_addr.proto == relay_port->proto)
443 return true;
444 }
445 }
446 return false;
447 }
448
Guo-wei Shieh11477022015-08-15 09:28:41 -0700449 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
450 bool with_nat) {
451 if (with_nat) {
452 nat_server_.reset(new rtc::NATServer(
453 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
454 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
455 } else {
456 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
457 }
458
459 ServerAddresses stun_servers;
460 if (!stun_server.IsNil()) {
461 stun_servers.insert(stun_server);
462 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700463 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700464 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700465 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700466 }
467
pthatcher1749bc32017-02-08 13:18:00 -0800468 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
469 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
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700805// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700806TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800807 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
808 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700809 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
810 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
811 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700812 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800813 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
814 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700815 EXPECT_EQ(1U, candidates_.size());
816}
817
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000818// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700819TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000820 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700821 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000822 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800823 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
824 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000825 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700826 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
827 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
828 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
829 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
830 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
831 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
832 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
833 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000834 EXPECT_TRUE(candidate_allocation_done_);
835}
836
honghaiz8c404fa2015-09-28 07:59:43 -0700837// Test that when the same network interface is brought down and up, the
838// port allocator session will restart a new allocation sequence if
839// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700840TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700841 std::string if_name("test_net0");
842 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700843 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700844 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800845 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
846 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700847 EXPECT_EQ(4U, ports_.size());
848 EXPECT_TRUE(candidate_allocation_done_);
849 candidate_allocation_done_ = false;
850 candidates_.clear();
851 ports_.clear();
852
853 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800854 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
855 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700856 EXPECT_EQ(0U, ports_.size());
857 EXPECT_FALSE(candidate_allocation_done_);
858
859 // When the same interfaces are added again, new candidates/ports should be
860 // generated.
861 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800862 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
863 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700864 EXPECT_EQ(4U, ports_.size());
865 EXPECT_TRUE(candidate_allocation_done_);
866}
867
868// Test that when the same network interface is brought down and up, the
869// port allocator session will not restart a new allocation sequence if
870// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700871TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700872 std::string if_name("test_net0");
873 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700874 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700875 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800876 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
877 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700878 EXPECT_EQ(4U, ports_.size());
879 EXPECT_TRUE(candidate_allocation_done_);
880 session_->StopGettingPorts();
881 candidates_.clear();
882 ports_.clear();
883
884 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800885 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
886 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700887 EXPECT_EQ(0U, ports_.size());
888
889 // When the same interfaces are added again, new candidates/ports should not
890 // be generated because the session has stopped.
891 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800892 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
893 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700894 EXPECT_EQ(0U, ports_.size());
895 EXPECT_TRUE(candidate_allocation_done_);
896}
897
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700899TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700901 allocator_->set_step_delay(kDefaultStepDelay);
902 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800904 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000905 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800906 ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000907 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700908 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
909 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800910 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700911 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
912 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000913 EXPECT_EQ(4U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800914 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700915 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
916 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000917 EXPECT_EQ(4U, ports_.size());
918 EXPECT_TRUE(candidate_allocation_done_);
919 // If we Stop gathering now, we shouldn't get a second "done" callback.
920 session_->StopGettingPorts();
921}
922
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700923TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000924 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700925 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000926 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800927 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
928 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000929 EXPECT_TRUE(candidate_allocation_done_);
930 // If we Stop gathering now, we shouldn't get a second "done" callback.
931 session_->StopGettingPorts();
932
933 // All ports should have unset send-buffer sizes.
934 CheckSendBufferSizesOfAllPorts(-1);
935}
936
937// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700938TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000939 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700940 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000941 session_->StartGettingPorts();
942 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
943 EXPECT_EQ(2U, ports_.size());
944 session_->StopGettingPorts();
945 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
946}
947
948// Test that we restrict client ports appropriately when a port range is set.
949// We check the candidates for udp/stun/tcp ports, and the from address
950// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700951TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000952 AddInterface(kClientAddr);
953 // Check that an invalid port range fails.
954 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
955 // Check that a null port range succeeds.
956 EXPECT_TRUE(SetPortRange(0, 0));
957 // Check that a valid port range succeeds.
958 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700959 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000960 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800961 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
962 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000963 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -0700964
965 int num_nonrelay_candidates = 0;
966 for (const Candidate& candidate : candidates_) {
967 // Check the port number for the UDP/STUN/TCP port objects.
968 if (candidate.type() != RELAY_PORT_TYPE) {
969 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
970 ++num_nonrelay_candidates;
971 }
972 }
973 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000974 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700975 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
976 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000977 EXPECT_TRUE(candidate_allocation_done_);
978}
979
deadbeefe97389c2016-12-23 01:43:45 -0800980// Test that if we have no network adapters, we bind to the ANY address and
981// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700982TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -0800983 // Default config uses GTURN and no NAT, so replace that with the
984 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
985 ResetWithStunServerAndNat(kStunAddr);
986 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
987 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
988 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
989 // Disable IPv6, because our test infrastructure doesn't support having IPv4
990 // behind a NAT but IPv6 not, or having an IPv6 NAT.
991 // TODO(deadbeef): Fix this.
992 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700993 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000994 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800995 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
996 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -0800997 EXPECT_EQ(4U, ports_.size());
998 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
999 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1000 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1001 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1002 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1003 // The "any" address port should be in the signaled ready ports, but the host
1004 // candidate for it is useless and shouldn't be signaled. So we only have
1005 // STUN/TURN candidates.
1006 EXPECT_EQ(3U, candidates_.size());
1007 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1008 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1009 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1010 // server.
1011 EXPECT_EQ(2,
1012 CountCandidates(candidates_, "relay", "udp",
1013 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001014}
1015
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001016// Test that when enumeration is disabled, we should not have any ports when
1017// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001018TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001019 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001020 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001021 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001022 // Expect to see no ports and no candidates.
1023 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1024 rtc::IPAddress(), rtc::IPAddress());
1025}
1026
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001027// Test that even with multiple interfaces, the result should still be a single
1028// default private, one STUN and one TURN candidate since we bind to any address
1029// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001030TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001031 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001032 AddInterface(kPrivateAddr);
1033 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001034 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001035 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001036
1037 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1038 // address set and we have no IPv6 STUN server, there should be no IPv6
1039 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001040 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1041 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001042
1043 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1044 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1045 // TURN/UDP candidates.
1046 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001047 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1048 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001049}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001050
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001051// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1052// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001053TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001054 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001055 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001056 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001057 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001058 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1059 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1060 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1061 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001062 kTurnUdpExtAddr.ipaddr());
1063}
1064
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001065// Test that when adapter enumeration is disabled, for endpoints without
1066// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001067TEST_F(BasicPortAllocatorTest,
1068 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001069 ResetWithNoServersOrNat();
1070 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1071 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1072 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001073}
1074
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001075// Test that when adapter enumeration is disabled, with
1076// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1077// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001078TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001079 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1080 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001081 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1082 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001083 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1084 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001085 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001086 rtc::IPAddress(), rtc::IPAddress());
1087}
1088
1089// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001090// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1091// a NAT, there is no local candidate. However, this specified default route
1092// (kClientAddr) which was discovered when sending STUN requests, will become
1093// the srflx addresses.
1094TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001095 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001096 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001097 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001098 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001099 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1100 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001101 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1102 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001103 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1104 rtc::IPAddress(), rtc::IPAddress());
1105}
1106
1107// Test that when adapter enumeration is disabled, with
1108// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1109// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001110TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001111 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1112 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001113 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1114 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001115 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1116 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1117 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001118}
1119
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001120// Test that we disable relay over UDP, and only TCP is used when connecting to
1121// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001122TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001123 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001124 AddInterface(kClientAddr);
1125 ResetWithStunServerAndNat(kStunAddr);
1126 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001127 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1128 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1129 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1130 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001131
1132 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001133 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1134 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001135
1136 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1137 // TURN/TCP candidates.
1138 EXPECT_EQ(2U, ports_.size());
1139 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001140 Candidate turn_candidate;
1141 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1142 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001143 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001144 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1145 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001146}
1147
Erik Språngefdce692015-06-05 09:41:26 +02001148// Disable for asan, see
1149// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1150#if !defined(ADDRESS_SANITIZER)
1151
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152// Test that we can get OnCandidatesAllocationDone callback when all the ports
1153// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001154TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001155 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001156 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1157 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1158 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001160 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001161 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162}
1163
1164// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001165TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001166 AddInterface(kClientAddr);
1167 fss_->set_udp_sockets_enabled(false);
1168 EXPECT_TRUE(CreateSession(1));
1169 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001170 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1171 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001173 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1174 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1175 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1176 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1177 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1178 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001179 EXPECT_TRUE(candidate_allocation_done_);
1180}
1181
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001182#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001183
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001184// Test that we don't crash or malfunction if we can't create UDP sockets or
1185// listen on TCP sockets. We still give out a local TCP address, since
1186// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001187TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001188 AddInterface(kClientAddr);
1189 fss_->set_udp_sockets_enabled(false);
1190 fss_->set_tcp_listen_enabled(false);
1191 EXPECT_TRUE(CreateSession(1));
1192 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001193 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1194 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001196 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1197 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1198 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1199 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1200 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001201 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001202 EXPECT_TRUE(candidate_allocation_done_);
1203}
1204
1205// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001206// TODO(deadbeef): Find a way to exit early here.
1207TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001208 AddInterface(kClientAddr);
1209 fss_->set_tcp_sockets_enabled(false);
1210 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001211 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212 session_->StartGettingPorts();
1213 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001214 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001215 // In case of Relay, ports creation will succeed but sockets will fail.
1216 // There is no error reporting from RelayEntry to handle this failure.
1217}
1218
1219// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001220TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001221 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1222 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001223 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001224 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001225 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1226 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001227 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001228 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1229 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001231 // will be tried after about 3 seconds.
1232 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001233 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001234 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1235 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1236 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1237 kRelaySslTcpIntAddr);
1238 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001239 // We wait at least for a full STUN timeout, which
1240 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1241 // already passed (see above), we wait 3 seconds less than that.
1242 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1243 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001244}
1245
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001246TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001247 AddInterface(kClientAddr);
1248 AddInterface(kClientAddr2);
1249 // Allocating only host UDP ports. This is done purely for testing
1250 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001251 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1252 PORTALLOCATOR_DISABLE_RELAY);
1253 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001254 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001255 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1256 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001257 ASSERT_EQ(2U, candidates_.size());
1258 EXPECT_EQ(2U, ports_.size());
1259 // Candidates priorities should be different.
1260 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1261}
1262
1263// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001264TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001265 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001266 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001267 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001268 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1269 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001270 EXPECT_EQ(4U, ports_.size());
1271 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001272 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001273}
1274
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001275// Test that the allocator session uses the candidate filter it's created with,
1276// rather than the filter of its parent allocator.
1277// The filter of the allocator should only affect the next gathering phase,
1278// according to JSEP, which means the *next* allocator session returned.
1279TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1280 AddInterface(kClientAddr);
1281 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1282 // Set candidate filter *after* creating the session. Should have no effect.
1283 allocator().set_candidate_filter(CF_RELAY);
1284 session_->StartGettingPorts();
1285 // 7 candidates and 4 ports is what we would normally get (see the
1286 // TestGetAllPorts* tests).
pthatcher1749bc32017-02-08 13:18:00 -08001287 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1288 fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001289 EXPECT_EQ(4U, ports_.size());
1290 EXPECT_TRUE(candidate_allocation_done_);
1291}
1292
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001293// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1294// This test also verifies that when the allocator is only allowed to use
1295// relay (i.e. IceTransportsType is relay), the raddr is an empty
1296// address with the correct family. This is to prevent any local
1297// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001298TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001299 AddInterface(kClientAddr);
1300 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001301 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001302 allocator().set_candidate_filter(CF_RELAY);
1303 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001304 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001305 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1306 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001307 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1308 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001309
1310 EXPECT_EQ(1U, candidates_.size());
1311 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001312 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1313 EXPECT_EQ(
1314 candidates_[0].related_address(),
1315 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316}
1317
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001318TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001319 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001320 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1321 allocator().set_candidate_filter(CF_HOST);
1322 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001323 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001324 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1325 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001326 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1327 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001328 for (const Candidate& candidate : candidates_) {
1329 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001330 }
1331}
1332
1333// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001335 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001336 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001337
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001338 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1339 allocator().set_candidate_filter(CF_REFLEXIVE);
1340 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001341 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001342 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1343 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001344 // Host is behind NAT, no private address will be exposed. Hence only UDP
1345 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001346 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1347 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001348 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1349 EXPECT_EQ(
1350 candidates_[0].related_address(),
1351 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352}
1353
1354// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001355TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001356 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001357 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1358 allocator().set_candidate_filter(CF_REFLEXIVE);
1359 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001361 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1362 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001363 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001364 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001365 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001366 for (const Candidate& candidate : candidates_) {
1367 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001368 }
1369}
1370
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001371// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001372TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001373 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001374 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001375 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001376 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1377 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001378 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1379 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1380 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001381 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001382 for (const Candidate& candidate : candidates_) {
1383 EXPECT_EQ(kIceUfrag0, candidate.username());
1384 EXPECT_EQ(kIcePwd0, candidate.password());
1385 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001386 EXPECT_TRUE(candidate_allocation_done_);
1387}
1388
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001389// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1390// is allocated for udp and stun. Also verify there is only one candidate
1391// (local) if stun candidate is same as local candidate, which will be the case
1392// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001393TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001394 AddInterface(kClientAddr);
1395 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001396 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1397 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001399 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1400 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001401 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001402 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001403 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1404 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405}
1406
1407// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1408// is allocated for udp and stun. In this test we should expect both stun and
1409// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001410TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001411 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001412 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413
1414 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001415 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1416 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001417 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001418 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1419 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001420 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001421 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1422 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1423 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001424 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1425 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001426 EXPECT_EQ(3U, candidates_.size());
1427}
1428
deadbeefc5d0d952015-07-16 10:22:21 -07001429// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001430TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001431 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001433 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001434
1435 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1436
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001437 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001438 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001439 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1440 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001442 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001443 session_->StartGettingPorts();
1444
pthatcher1749bc32017-02-08 13:18:00 -08001445 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1446 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001447 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001448 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1449 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1450 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1451 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1452 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001453 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1454 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001455 EXPECT_EQ(3U, candidates_.size());
1456}
1457
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001458// Test that if prune_turn_ports is set, TCP TURN port will not be used
1459// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1460TEST_F(BasicPortAllocatorTest,
1461 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1462 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1463 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1464 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001465
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001466 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001467}
1468
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001469// Test that if prune_turn_ports is set, TCP TURN port will not be used
1470// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1471TEST_F(BasicPortAllocatorTest,
1472 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1473 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1474 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1475 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001476
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001477 TestUdpTurnPortPrunesTcpTurnPort();
1478}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001479
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001480// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1481// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1482TEST_F(BasicPortAllocatorTest,
1483 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1484 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1485 // first.
1486 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1487 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001488
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001489 TestIPv6TurnPortPrunesIPv4TurnPort();
1490}
1491
1492// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1493// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1494TEST_F(BasicPortAllocatorTest,
1495 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1496 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1497 // first.
1498 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1499 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1500
1501 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001502}
1503
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001504// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001505// will has its own set of TurnPorts based on their priorities, in the default
1506// case where no transit delay is set.
1507TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1508 TestEachInterfaceHasItsOwnTurnPorts();
1509}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001510
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001511// Tests that if prune_turn_ports is set, each network interface
1512// will has its own set of TurnPorts based on their priorities, given that
1513// IPv4/TCP TURN port becomes ready first.
1514TEST_F(BasicPortAllocatorTest,
1515 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1516 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1517 // becomes ready last.
1518 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1519 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1520 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1521 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001522
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001523 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001524}
1525
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001526// Testing DNS resolve for the TURN server, this will test AllocationSequence
1527// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001528// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1529// appears that it doesn't currently because of the DNS look up not
1530// using the fake clock.
1531TEST_F(BasicPortAllocatorTestWithRealClock,
1532 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001533 // This test relies on a real query for "localhost", so it won't work on an
1534 // IPv6-only machine.
1535 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001536 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001537 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001538 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001539 allocator_.reset(new BasicPortAllocator(&network_manager_));
1540 RelayServerConfig turn_server(RELAY_TURN);
1541 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001542 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001543 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001544 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001545 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001546
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001547 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001548 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001549 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1550 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001551
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001552 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001553 session_->StartGettingPorts();
1554
1555 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1556}
1557
1558// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1559// is allocated for udp/stun/turn. In this test we should expect all local,
1560// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001561TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001562 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001563 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001564
1565 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1566
1567 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001568 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1569 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001570
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001571 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001572 session_->StartGettingPorts();
1573
pthatcher1749bc32017-02-08 13:18:00 -08001574 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1575 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001576 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001577 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1578 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1579 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1580 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1581 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001582 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1583 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001584 EXPECT_EQ(3U, candidates_.size());
1585 // Local port will be created first and then TURN port.
1586 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1587 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1588}
1589
1590// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1591// server is also used as the STUN server, we should get 'local', 'stun', and
1592// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001593TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001594 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001595 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001596 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001597 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1598
1599 // Must set the step delay to 0 to make sure the relay allocation phase is
1600 // started before the STUN candidates are obtained, so that the STUN binding
1601 // response is processed when both StunPort and TurnPort exist to reproduce
1602 // webrtc issue 3537.
1603 allocator_->set_step_delay(0);
1604 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001605 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1606 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001607
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001608 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001609 session_->StartGettingPorts();
1610
pthatcher1749bc32017-02-08 13:18:00 -08001611 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1612 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001613 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1614 Candidate stun_candidate;
1615 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1616 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1617 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1618 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1619 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001620
pthatcher1749bc32017-02-08 13:18:00 -08001621 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1622 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623 EXPECT_EQ(3U, candidates_.size());
1624 // Local port will be created first and then TURN port.
1625 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1626 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1627}
1628
deadbeefc5d0d952015-07-16 10:22:21 -07001629// Test that when only a TCP TURN server is available, we do NOT use it as
1630// a UDP STUN server, as this could leak our IP address. Thus we should only
1631// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001632TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001633 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001634 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001635 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001636 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1637
1638 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001639 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1640 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001641
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001642 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001643 session_->StartGettingPorts();
1644
pthatcher1749bc32017-02-08 13:18:00 -08001645 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1646 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001647 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001648 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1649 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1650 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001651 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1652 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001653 EXPECT_EQ(2U, candidates_.size());
1654 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1655 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1656}
1657
1658// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1659// TURN server is used as the STUN server and we get 'local', 'stun', and
1660// 'relay' candidates.
1661// TODO(deadbeef): Remove this test when support for non-shared socket mode
1662// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001663TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001664 AddInterface(kClientAddr);
1665 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001666 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001667 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1668
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001669 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001670
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001671 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001672 session_->StartGettingPorts();
1673
pthatcher1749bc32017-02-08 13:18:00 -08001674 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1675 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001676 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001677 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1678 Candidate stun_candidate;
1679 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1680 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1681 Candidate turn_candidate;
1682 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1683 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1684 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001685 // Not using shared socket, so the STUN request's server reflexive address
1686 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001687 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001688
pthatcher1749bc32017-02-08 13:18:00 -08001689 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1690 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001691 EXPECT_EQ(3U, candidates_.size());
1692 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1693 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1694 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1695}
1696
1697// Test that even when both a STUN and TURN server are configured, the TURN
1698// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001699TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001700 AddInterface(kClientAddr);
1701 // Configure with STUN server but destroy it, so we can ensure that it's
1702 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001703 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001704 stun_server_.reset();
1705 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1706
1707 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001708 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1709 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001710
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001711 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001712 session_->StartGettingPorts();
1713
pthatcher1749bc32017-02-08 13:18:00 -08001714 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1715 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001716 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1717 Candidate stun_candidate;
1718 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1719 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1720 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1721 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1722 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001723
1724 // Don't bother waiting for STUN timeout, since we already verified
1725 // that we got a STUN candidate from the TURN server.
1726}
1727
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001728// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1729// and fail to generate STUN candidate, local UDP candidate is generated
1730// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001731TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001732 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1733 PORTALLOCATOR_DISABLE_TCP |
1734 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001735 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1736 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001737 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001738 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001739 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1740 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001741 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001742 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001743 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001744 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1745 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001746 EXPECT_EQ(1U, candidates_.size());
1747}
1748
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001749// Test that when the NetworkManager doesn't have permission to enumerate
1750// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1751// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001752TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001753 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1754 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001755 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001756 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001757 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1758 PORTALLOCATOR_DISABLE_TCP |
1759 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1760 EXPECT_EQ(0U,
1761 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1762 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1763 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001764 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001765 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1766 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001767 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001768 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001769 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001770}
1771
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001772// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001773TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001774 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1775 PORTALLOCATOR_ENABLE_IPV6 |
1776 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001777 AddInterface(kClientIPv6Addr);
1778 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001779 allocator_->set_step_delay(kMinimumStepDelay);
1780 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001781 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001782 ASSERT_EQ_SIMULATED_WAIT(4U, ports_.size(), kDefaultAllocationTimeout,
1783 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001784 EXPECT_EQ(4U, candidates_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001785 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1786 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001787 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1788 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1789 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1790 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001791 EXPECT_EQ(4U, candidates_.size());
1792}
honghaiz98db68f2015-09-29 07:58:17 -07001793
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001794TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001795 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001796 allocator_->set_step_delay(kDefaultStepDelay);
1797 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001798 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001799 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001800 EXPECT_EQ(2U, ports_.size());
1801 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001802 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001803
1804 // After stopping getting ports, adding a new interface will not start
1805 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001806 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001807 candidates_.clear();
1808 ports_.clear();
1809 candidate_allocation_done_ = false;
1810 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001811 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001812 EXPECT_EQ(0U, candidates_.size());
1813 EXPECT_EQ(0U, ports_.size());
1814}
1815
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001816TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001817 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001818 allocator_->set_step_delay(kDefaultStepDelay);
1819 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001820 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001821 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001822 EXPECT_EQ(2U, ports_.size());
1823 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001824 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001825
1826 // After clearing getting ports, adding a new interface will start getting
1827 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001828 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001829 candidates_.clear();
1830 ports_.clear();
1831 candidate_allocation_done_ = false;
1832 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001833 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001834 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001835 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1836 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001837}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001838
1839// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1840// a pooled session is taken out of the pool.
1841TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1842 AddInterface(kClientAddr);
1843 int pool_size = 1;
1844 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001845 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001846 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001847 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001848 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1849 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001850 // Expect that when TakePooledSession is called,
1851 // UpdateTransportInformationInternal will be called and the
1852 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1853 // candidates.
1854 session_ =
1855 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1856 ASSERT_NE(nullptr, session_.get());
1857 auto ready_ports = session_->ReadyPorts();
1858 auto candidates = session_->ReadyCandidates();
1859 EXPECT_FALSE(ready_ports.empty());
1860 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001861 for (const PortInterface* port_interface : ready_ports) {
1862 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001863 EXPECT_EQ(kContentName, port->content_name());
1864 EXPECT_EQ(1, port->component());
1865 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1866 EXPECT_EQ(kIcePwd0, port->password());
1867 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001868 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001869 EXPECT_EQ(1, candidate.component());
1870 EXPECT_EQ(kIceUfrag0, candidate.username());
1871 EXPECT_EQ(kIcePwd0, candidate.password());
1872 }
1873}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001874
1875// Test that a new candidate filter takes effect even on already-gathered
1876// candidates.
1877TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1878 AddInterface(kClientAddr);
1879 int pool_size = 1;
1880 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001881 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001882 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1883 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001884 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1885 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001886 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1887 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1888 allocator_->set_candidate_filter(CF_RELAY);
1889 // Assume that when TakePooledSession is called, the candidate filter will be
1890 // applied to the pooled session. This is tested by PortAllocatorTest.
1891 session_ =
1892 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1893 ASSERT_NE(nullptr, session_.get());
1894 auto candidates = session_->ReadyCandidates();
1895 auto ports = session_->ReadyPorts();
1896 // Sanity check that the number of candidates and ports decreased.
1897 EXPECT_GT(initial_candidates_size, candidates.size());
1898 EXPECT_GT(initial_ports_size, ports.size());
1899 for (const PortInterface* port : ports) {
1900 // Expect only relay ports.
1901 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1902 }
1903 for (const Candidate& candidate : candidates) {
1904 // Expect only relay candidates now that the filter is applied.
1905 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1906 // Expect that the raddr is emptied due to the CF_RELAY filter.
1907 EXPECT_EQ(candidate.related_address(),
1908 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1909 }
1910}
1911
1912} // namespace cricket