blob: bf288c9e6801a49fa0f30bc290d08b549a7b820d [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"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025#include "webrtc/base/socketaddress.h"
26#include "webrtc/base/ssladapter.h"
27#include "webrtc/base/thread.h"
28#include "webrtc/base/virtualsocketserver.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070029#include "webrtc/p2p/base/basicpacketsocketfactory.h"
30#include "webrtc/p2p/base/p2pconstants.h"
31#include "webrtc/p2p/base/p2ptransportchannel.h"
32#include "webrtc/p2p/base/testrelayserver.h"
33#include "webrtc/p2p/base/teststunserver.h"
34#include "webrtc/p2p/base/testturnserver.h"
35#include "webrtc/p2p/client/basicportallocator.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080037using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038using rtc::SocketAddress;
39using rtc::Thread;
40
deadbeef9a6f4d42017-05-15 19:43:33 -070041#define MAYBE_SKIP_IPV4 \
42 if (!rtc::HasIPv4Enabled()) { \
43 LOG(LS_INFO) << "No IPv4... skipping"; \
44 return; \
45 }
46
deadbeefe97389c2016-12-23 01:43:45 -080047static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070049static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070050static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000052static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070053static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
54 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070055static const SocketAddress kClientIPv6Addr2(
56 "2401:fa00:4:2000:be30:5bff:fee5:c3",
57 0);
deadbeefc5d0d952015-07-16 10:22:21 -070058static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
59static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
61static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
62static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
63static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
64static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
65static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
66static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
67static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
68static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070069static const SocketAddress kTurnUdpIntIPv6Addr(
70 "2402:fb00:4:1000:be30:5bff:fee5:c3",
71 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070073static const SocketAddress kTurnTcpIntIPv6Addr(
74 "2402:fb00:4:2000:be30:5bff:fee5:c3",
75 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
77
78// Minimum and maximum port for port range tests.
79static const int kMinPort = 10000;
80static const int kMaxPort = 10099;
81
82// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070083static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084// Based on ICE_PWD_LENGTH
85static const char kIcePwd0[] = "TESTICEPWD00000000000000";
86
87static const char kContentName[] = "test content";
88
Honghai Zhangc67e0f52016-09-19 16:57:37 -070089static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090static const char kTurnUsername[] = "test";
91static const char kTurnPassword[] = "test";
92
pthatcher94a2f212017-02-08 14:42:22 -080093// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070094// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080095static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070096
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097namespace cricket {
98
99// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700100std::ostream& operator<<(std::ostream& os,
101 const std::vector<Candidate>& candidates) {
102 os << '[';
103 bool first = true;
104 for (const Candidate& c : candidates) {
105 if (!first) {
106 os << ", ";
107 }
108 os << c.ToString();
109 first = false;
110 };
111 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 return os;
113}
114
pthatcher1749bc32017-02-08 13:18:00 -0800115class BasicPortAllocatorTestBase : public testing::Test,
116 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000117 public:
pthatcher1749bc32017-02-08 13:18:00 -0800118 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-16 18:00:06 -0700119 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700121 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800122 // Note that the NAT is not used by default. ResetWithStunServerAndNat
123 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700124 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700125 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700126 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700127 relay_server_(Thread::Current(),
128 kRelayUdpIntAddr,
129 kRelayUdpExtAddr,
130 kRelayTcpIntAddr,
131 kRelayTcpExtAddr,
132 kRelaySslTcpIntAddr,
133 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
135 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700136 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 stun_servers.insert(kStunAddr);
138 // Passing the addresses of GTURN servers will enable GTURN in
139 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800140 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
141 // configuration should be blank by default (preferred), or it should use
142 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700143 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
144 kRelayUdpIntAddr, kRelayTcpIntAddr,
145 kRelaySslTcpIntAddr));
146 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 }
148
149 void AddInterface(const SocketAddress& addr) {
150 network_manager_.AddInterface(addr);
151 }
honghaiz8c404fa2015-09-28 07:59:43 -0700152 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
153 network_manager_.AddInterface(addr, if_name);
154 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800155 void AddInterface(const SocketAddress& addr,
156 const std::string& if_name,
157 rtc::AdapterType type) {
158 network_manager_.AddInterface(addr, if_name, type);
159 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800160 // The default route is the public address that STUN server will observe when
161 // the endpoint is sitting on the public internet and the local port is bound
162 // to the "any" address. This may be different from the default local address
163 // which the endpoint observes. This can occur if the route to the public
164 // endpoint like 8.8.8.8 (specified as the default local address) is
165 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700166 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
167 AddInterface(addr);
168 // When a binding comes from the any address, the |addr| will be used as the
169 // srflx address.
170 vss_->SetDefaultRoute(addr.ipaddr());
171 }
honghaiz8c404fa2015-09-28 07:59:43 -0700172 void RemoveInterface(const SocketAddress& addr) {
173 network_manager_.RemoveInterface(addr);
174 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175 bool SetPortRange(int min_port, int max_port) {
176 return allocator_->SetPortRange(min_port, max_port);
177 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700178 // Endpoint is on the public network. No STUN or TURN.
179 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700180 allocator_.reset(new BasicPortAllocator(&network_manager_));
181 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700182 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700183 // Endpoint is behind a NAT, with STUN specified.
184 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
185 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700186 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700187 // Endpoint is on the public network, with STUN specified.
188 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
189 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700191 // Endpoint is on the public network, with TURN specified.
192 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
193 const rtc::SocketAddress& tcp_turn) {
194 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000195 AddTurnServers(udp_turn, tcp_turn);
196 }
197
198 void AddTurnServers(const rtc::SocketAddress& udp_turn,
199 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700200 RelayServerConfig turn_server(RELAY_TURN);
201 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800202 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203
204 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800205 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 }
207 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800208 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 }
deadbeef653b8e02015-11-11 12:55:10 -0800210 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 }
212
213 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700214 session_ = CreateSession("session", component);
215 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700217 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000218 return true;
219 }
220
221 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700222 session_ = CreateSession("session", content_name, component);
223 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 return true;
227 }
228
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700229 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
230 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 return CreateSession(sid, kContentName, component);
232 }
233
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700234 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700235 const std::string& sid,
236 const std::string& content_name,
237 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000238 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
239 }
240
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700241 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700242 const std::string& sid,
243 const std::string& content_name,
244 int component,
245 const std::string& ice_ufrag,
246 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700247 std::unique_ptr<PortAllocatorSession> session =
248 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800250 &BasicPortAllocatorTestBase::OnPortReady);
251 session->SignalPortsPruned.connect(
252 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700253 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800254 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700255 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800256 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700257 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800258 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000259 return session;
260 }
261
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700262 // Return true if the addresses are the same, or the port is 0 in |pattern|
263 // (acting as a wildcard) and the IPs are the same.
264 // Even with a wildcard port, the port of the address should be nonzero if
265 // the IP is nonzero.
266 static bool AddressMatch(const SocketAddress& address,
267 const SocketAddress& pattern) {
268 return address.ipaddr() == pattern.ipaddr() &&
269 ((pattern.port() == 0 &&
270 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
271 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700273
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700274 // Returns the number of ports that have matching type, protocol and
275 // address.
276 static int CountPorts(const std::vector<PortInterface*>& ports,
277 const std::string& type,
278 ProtocolType protocol,
279 const SocketAddress& client_addr) {
280 return std::count_if(
281 ports.begin(), ports.end(),
282 [type, protocol, client_addr](PortInterface* port) {
283 return port->Type() == type && port->GetProtocol() == protocol &&
284 port->Network()->GetBestIP() == client_addr.ipaddr();
285 });
286 }
287
deadbeefe97389c2016-12-23 01:43:45 -0800288 static int CountCandidates(const std::vector<Candidate>& candidates,
289 const std::string& type,
290 const std::string& proto,
291 const SocketAddress& addr) {
292 return std::count_if(candidates.begin(), candidates.end(),
293 [type, proto, addr](const Candidate& c) {
294 return c.type() == type && c.protocol() == proto &&
295 AddressMatch(c.address(), addr);
296 });
297 }
298
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700299 // Find a candidate and return it.
300 static bool FindCandidate(const std::vector<Candidate>& candidates,
301 const std::string& type,
302 const std::string& proto,
303 const SocketAddress& addr,
304 Candidate* found) {
305 auto it = std::find_if(candidates.begin(), candidates.end(),
306 [type, proto, addr](const Candidate& c) {
307 return c.type() == type && c.protocol() == proto &&
308 AddressMatch(c.address(), addr);
309 });
310 if (it != candidates.end() && found) {
311 *found = *it;
312 }
313 return it != candidates.end();
314 }
315
316 // Convenience method to call FindCandidate with no return.
317 static bool HasCandidate(const std::vector<Candidate>& candidates,
318 const std::string& type,
319 const std::string& proto,
320 const SocketAddress& addr) {
321 return FindCandidate(candidates, type, proto, addr, nullptr);
322 }
323
324 // Version of HasCandidate that also takes a related address.
325 static bool HasCandidateWithRelatedAddr(
326 const std::vector<Candidate>& candidates,
327 const std::string& type,
328 const std::string& proto,
329 const SocketAddress& addr,
330 const SocketAddress& related_addr) {
331 auto it =
332 std::find_if(candidates.begin(), candidates.end(),
333 [type, proto, addr, related_addr](const Candidate& c) {
334 return c.type() == type && c.protocol() == proto &&
335 AddressMatch(c.address(), addr) &&
336 AddressMatch(c.related_address(), related_addr);
337 });
338 return it != candidates.end();
339 }
340
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700342 int min_port,
343 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 return (addr.port() >= min_port && addr.port() <= max_port);
345 }
346
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700347 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000348 // We should only get this callback once, except in the mux test where
349 // we have multiple port allocation sessions.
350 if (session == session_.get()) {
351 ASSERT_FALSE(candidate_allocation_done_);
352 candidate_allocation_done_ = true;
353 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700354 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000355 }
356
357 // Check if all ports allocated have send-buffer size |expected|. If
358 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
359 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700360 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000361 for (it = ports_.begin(); it < ports_.end(); ++it) {
362 int send_buffer_size;
363 if (expected == -1) {
364 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700365 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700367 EXPECT_EQ(0,
368 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 ASSERT_EQ(expected, send_buffer_size);
370 }
371 }
372 }
373
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700374 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
375
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000376 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700377 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700379 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
381 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700382 // Make sure the new port is added to ReadyPorts.
383 auto ready_ports = ses->ReadyPorts();
384 EXPECT_NE(ready_ports.end(),
385 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700387 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700388 const std::vector<PortInterface*>& pruned_ports) {
389 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700390 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700391 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700392 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700393 new_end = std::remove(ports_.begin(), new_end, port);
394 // Make sure the pruned port is not in ReadyPorts.
395 EXPECT_EQ(ready_ports.end(),
396 std::find(ready_ports.begin(), ready_ports.end(), port));
397 }
398 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700399 }
400
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700401 void OnCandidatesReady(PortAllocatorSession* ses,
402 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700403 for (const Candidate& candidate : candidates) {
404 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
405 // Sanity check that the ICE component is set.
406 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
407 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700409 // Make sure the new candidates are added to Candidates.
410 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700411 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700412 EXPECT_NE(
413 ses_candidates.end(),
414 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
415 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000416 }
417
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700418 void OnCandidatesRemoved(PortAllocatorSession* session,
419 const std::vector<Candidate>& removed_candidates) {
420 auto new_end = std::remove_if(
421 candidates_.begin(), candidates_.end(),
422 [removed_candidates](Candidate& candidate) {
423 for (const Candidate& removed_candidate : removed_candidates) {
424 if (candidate.MatchesForRemoval(removed_candidate)) {
425 return true;
426 }
427 }
428 return false;
429 });
430 candidates_.erase(new_end, candidates_.end());
431 }
432
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700433 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800434 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700435 RelayServerConfig server_config = allocator_->turn_servers()[i];
436 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000437 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700438 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439 if (proto_addr.address == relay_port->address &&
440 proto_addr.proto == relay_port->proto)
441 return true;
442 }
443 }
444 return false;
445 }
446
Guo-wei Shieh11477022015-08-15 09:28:41 -0700447 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
448 bool with_nat) {
449 if (with_nat) {
450 nat_server_.reset(new rtc::NATServer(
451 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
452 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
453 } else {
454 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
455 }
456
457 ServerAddresses stun_servers;
458 if (!stun_server.IsNil()) {
459 stun_servers.insert(stun_server);
460 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700461 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700462 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700463 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700464 }
465
pthatcher1749bc32017-02-08 13:18:00 -0800466 std::unique_ptr<rtc::VirtualSocketServer> vss_;
467 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700468 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800469 std::unique_ptr<rtc::NATServer> nat_server_;
470 rtc::NATSocketFactory nat_factory_;
471 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
472 std::unique_ptr<TestStunServer> stun_server_;
473 TestRelayServer relay_server_;
474 TestTurnServer turn_server_;
475 rtc::FakeNetworkManager network_manager_;
476 std::unique_ptr<BasicPortAllocator> allocator_;
477 std::unique_ptr<PortAllocatorSession> session_;
478 std::vector<PortInterface*> ports_;
479 std::vector<Candidate> candidates_;
480 bool candidate_allocation_done_;
481};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700482
pthatcher1749bc32017-02-08 13:18:00 -0800483class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
484};
485
486class FakeClockBase {
487 public:
488 rtc::ScopedFakeClock fake_clock;
489};
490
491class BasicPortAllocatorTest : public FakeClockBase,
492 public BasicPortAllocatorTestBase {
493 public:
494 // This function starts the port/address gathering and check the existence of
495 // candidates as specified. When |expect_stun_candidate| is true,
496 // |stun_candidate_addr| carries the expected reflective address, which is
497 // also the related address for TURN candidate if it is expected. Otherwise,
498 // it should be ignore.
499 void CheckDisableAdapterEnumeration(
500 uint32_t total_ports,
501 const rtc::IPAddress& host_candidate_addr,
502 const rtc::IPAddress& stun_candidate_addr,
503 const rtc::IPAddress& relay_candidate_udp_transport_addr,
504 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
505 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
506 rtc::IPAddress());
507 if (!session_) {
508 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
509 }
510 session_->set_flags(session_->flags() |
511 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
512 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
513 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700514 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800515 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
516 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700517
pthatcher1749bc32017-02-08 13:18:00 -0800518 uint32_t total_candidates = 0;
519 if (!host_candidate_addr.IsNil()) {
520 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
521 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
522 ++total_candidates;
523 }
524 if (!stun_candidate_addr.IsNil()) {
525 rtc::SocketAddress related_address(host_candidate_addr, 0);
526 if (host_candidate_addr.IsNil()) {
527 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
528 }
529 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
530 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
531 ++total_candidates;
532 }
533 if (!relay_candidate_udp_transport_addr.IsNil()) {
534 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
535 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
536 rtc::SocketAddress(stun_candidate_addr, 0));
537 ++total_candidates;
538 }
539 if (!relay_candidate_tcp_transport_addr.IsNil()) {
540 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
541 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
542 rtc::SocketAddress(stun_candidate_addr, 0));
543 ++total_candidates;
544 }
545
546 EXPECT_EQ(total_candidates, candidates_.size());
547 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700548 }
549
550 void TestIPv6TurnPortPrunesIPv4TurnPort() {
551 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
552 // Add two IP addresses on the same interface.
553 AddInterface(kClientAddr, "net1");
554 AddInterface(kClientIPv6Addr, "net1");
555 allocator_.reset(new BasicPortAllocator(&network_manager_));
556 allocator_->SetConfiguration(allocator_->stun_servers(),
557 allocator_->turn_servers(), 0, true);
558 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
559 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
560
561 allocator_->set_step_delay(kMinimumStepDelay);
562 allocator_->set_flags(
563 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
564 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
565
566 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
567 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800568 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
569 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700570 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
571 EXPECT_EQ(3U, session_->ReadyPorts().size());
572 EXPECT_EQ(3U, ports_.size());
573 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
574 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
575 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
576 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
577
578 // Now that we remove candidates when a TURN port is pruned, there will be
579 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
580 EXPECT_EQ(3U, candidates_.size());
581 const std::vector<Candidate>& ready_candidates =
582 session_->ReadyCandidates();
583 EXPECT_EQ(3U, ready_candidates.size());
584 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
585 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
586 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
587 }
588
pthatcher1749bc32017-02-08 13:18:00 -0800589 void TestUdpTurnPortPrunesTcpTurnPort() {
590 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
591 AddInterface(kClientAddr);
592 allocator_.reset(new BasicPortAllocator(&network_manager_));
593 allocator_->SetConfiguration(allocator_->stun_servers(),
594 allocator_->turn_servers(), 0, true);
595 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
596 allocator_->set_step_delay(kMinimumStepDelay);
597 allocator_->set_flags(allocator().flags() |
598 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
599 PORTALLOCATOR_DISABLE_TCP);
600
601 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
602 session_->StartGettingPorts();
603 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
604 kDefaultAllocationTimeout, fake_clock);
605 // Only 2 ports (one STUN and one TURN) are actually being used.
606 EXPECT_EQ(2U, session_->ReadyPorts().size());
607 // We have verified that each port, when it is added to |ports_|, it is
608 // found in |ready_ports|, and when it is pruned, it is not found in
609 // |ready_ports|, so we only need to verify the content in one of them.
610 EXPECT_EQ(2U, ports_.size());
611 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
612 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
613 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
614
615 // Now that we remove candidates when a TURN port is pruned, |candidates_|
616 // should only contains two candidates regardless whether the TCP TURN port
617 // is created before or after the UDP turn port.
618 EXPECT_EQ(2U, candidates_.size());
619 // There will only be 2 candidates in |ready_candidates| because it only
620 // includes the candidates in the ready ports.
621 const std::vector<Candidate>& ready_candidates =
622 session_->ReadyCandidates();
623 EXPECT_EQ(2U, ready_candidates.size());
624 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
625 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
626 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
627 }
628
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700629 void TestEachInterfaceHasItsOwnTurnPorts() {
630 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
631 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
632 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
633 // Add two interfaces both having IPv4 and IPv6 addresses.
634 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
635 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
636 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
637 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
638 allocator_.reset(new BasicPortAllocator(&network_manager_));
639 allocator_->SetConfiguration(allocator_->stun_servers(),
640 allocator_->turn_servers(), 0, true);
641 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
642 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
643 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
644
645 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800646 allocator_->set_flags(
647 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
648 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700649 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
650 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800651 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
652 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700653 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
654 // use.
655 EXPECT_EQ(10U, session_->ReadyPorts().size());
656 EXPECT_EQ(10U, ports_.size());
657 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
658 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
659 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
660 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
661 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
662 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
663 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
664 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
665 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
666 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
667
668 // Now that we remove candidates when TURN ports are pruned, there will be
669 // exactly 10 candidates in |candidates_|.
670 EXPECT_EQ(10U, candidates_.size());
671 const std::vector<Candidate>& ready_candidates =
672 session_->ReadyCandidates();
673 EXPECT_EQ(10U, ready_candidates.size());
674 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
675 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
676 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
677 kClientIPv6Addr);
678 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
679 kClientIPv6Addr2);
680 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
681 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
682 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
683 kClientIPv6Addr);
684 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
685 kClientIPv6Addr2);
686 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
687 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
688 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000689};
690
691// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700692TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000693 EXPECT_EQ(&network_manager_, allocator().network_manager());
694 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800695 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700696 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000697 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800698 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
699 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700700 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
701 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
702 EXPECT_TRUE(
703 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
704 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700705 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000706}
707
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700709TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800710 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
711 rtc::ADAPTER_TYPE_ETHERNET);
712 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
713 rtc::ADAPTER_TYPE_WIFI);
714 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
715 rtc::ADAPTER_TYPE_CELLULAR);
716 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
717 rtc::ADAPTER_TYPE_VPN);
718 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
719 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700720 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
721 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
722 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800724 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
725 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700727 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800728 EXPECT_LT(candidate.address().ip(), 0x12345604U);
729 }
730}
731
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700732TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800733 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
734 rtc::ADAPTER_TYPE_ETHERNET);
735 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
736 rtc::ADAPTER_TYPE_WIFI);
737 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
738 rtc::ADAPTER_TYPE_CELLULAR);
739 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
740 rtc::ADAPTER_TYPE_LOOPBACK |
741 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700742 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
743 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
744 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800745 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800746 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
747 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800748 EXPECT_EQ(1U, candidates_.size());
749 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
750}
751
honghaiz60347052016-05-31 18:29:12 -0700752// Test that high cost networks are filtered if the flag
753// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
754TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
755 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
756 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
757 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
758 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
759 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
760 // interface.
761 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
762 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
763 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
764 cricket::PORTALLOCATOR_DISABLE_RELAY |
765 cricket::PORTALLOCATOR_DISABLE_TCP |
766 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
767 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
768 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800769 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
770 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700771 EXPECT_EQ(1U, candidates_.size());
772 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
773
774 // If both cellular and unknown interfaces are present, only gather on the
775 // unknown interfaces.
776 candidates_.clear();
777 candidate_allocation_done_ = false;
778 RemoveInterface(addr_wifi);
779 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
780 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
781 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800782 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
783 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700784 EXPECT_EQ(2U, candidates_.size());
785 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
786 addr_unknown2.EqualIPs(candidates_[1].address())) ||
787 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
788 addr_unknown2.EqualIPs(candidates_[0].address())));
789
790 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
791 // Wi-Fi interface.
792 candidates_.clear();
793 candidate_allocation_done_ = false;
794 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
795 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800796 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
797 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700798 EXPECT_EQ(1U, candidates_.size());
799 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
800}
801
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700802// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700803TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800804 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
805 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700806 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
807 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
808 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700809 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800810 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
811 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700812 EXPECT_EQ(1U, candidates_.size());
813}
814
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700816TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000817 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700818 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000819 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800820 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
821 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000822 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700823 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
824 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
825 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
826 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
827 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
828 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
829 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
830 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000831 EXPECT_TRUE(candidate_allocation_done_);
832}
833
honghaiz8c404fa2015-09-28 07:59:43 -0700834// Test that when the same network interface is brought down and up, the
835// port allocator session will restart a new allocation sequence if
836// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700837TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700838 std::string if_name("test_net0");
839 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700840 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700841 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800842 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
843 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700844 EXPECT_EQ(4U, ports_.size());
845 EXPECT_TRUE(candidate_allocation_done_);
846 candidate_allocation_done_ = false;
847 candidates_.clear();
848 ports_.clear();
849
850 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800851 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
852 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700853 EXPECT_EQ(0U, ports_.size());
854 EXPECT_FALSE(candidate_allocation_done_);
855
856 // When the same interfaces are added again, new candidates/ports should be
857 // generated.
858 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800859 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
860 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700861 EXPECT_EQ(4U, ports_.size());
862 EXPECT_TRUE(candidate_allocation_done_);
863}
864
865// Test that when the same network interface is brought down and up, the
866// port allocator session will not restart a new allocation sequence if
867// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700868TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700869 std::string if_name("test_net0");
870 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700871 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700872 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800873 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
874 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700875 EXPECT_EQ(4U, ports_.size());
876 EXPECT_TRUE(candidate_allocation_done_);
877 session_->StopGettingPorts();
878 candidates_.clear();
879 ports_.clear();
880
881 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800882 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
883 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700884 EXPECT_EQ(0U, ports_.size());
885
886 // When the same interfaces are added again, new candidates/ports should not
887 // be generated because the session has stopped.
888 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800889 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
890 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700891 EXPECT_EQ(0U, ports_.size());
892 EXPECT_TRUE(candidate_allocation_done_);
893}
894
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700896TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000897 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700898 allocator_->set_step_delay(kDefaultStepDelay);
899 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800901 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000902 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800903 ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000904 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700905 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
906 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800907 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700908 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
909 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000910 EXPECT_EQ(4U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800911 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700912 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
913 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000914 EXPECT_EQ(4U, ports_.size());
915 EXPECT_TRUE(candidate_allocation_done_);
916 // If we Stop gathering now, we shouldn't get a second "done" callback.
917 session_->StopGettingPorts();
918}
919
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700920TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000921 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700922 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000923 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800924 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
925 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000926 EXPECT_TRUE(candidate_allocation_done_);
927 // If we Stop gathering now, we shouldn't get a second "done" callback.
928 session_->StopGettingPorts();
929
930 // All ports should have unset send-buffer sizes.
931 CheckSendBufferSizesOfAllPorts(-1);
932}
933
934// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700935TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700937 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000938 session_->StartGettingPorts();
939 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
940 EXPECT_EQ(2U, ports_.size());
941 session_->StopGettingPorts();
942 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
943}
944
945// Test that we restrict client ports appropriately when a port range is set.
946// We check the candidates for udp/stun/tcp ports, and the from address
947// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700948TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000949 AddInterface(kClientAddr);
950 // Check that an invalid port range fails.
951 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
952 // Check that a null port range succeeds.
953 EXPECT_TRUE(SetPortRange(0, 0));
954 // Check that a valid port range succeeds.
955 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700956 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000957 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800958 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
959 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000960 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -0700961
962 int num_nonrelay_candidates = 0;
963 for (const Candidate& candidate : candidates_) {
964 // Check the port number for the UDP/STUN/TCP port objects.
965 if (candidate.type() != RELAY_PORT_TYPE) {
966 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
967 ++num_nonrelay_candidates;
968 }
969 }
970 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000971 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700972 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
973 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000974 EXPECT_TRUE(candidate_allocation_done_);
975}
976
deadbeefe97389c2016-12-23 01:43:45 -0800977// Test that if we have no network adapters, we bind to the ANY address and
978// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700979TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -0800980 // Default config uses GTURN and no NAT, so replace that with the
981 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
982 ResetWithStunServerAndNat(kStunAddr);
983 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
984 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
985 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
986 // Disable IPv6, because our test infrastructure doesn't support having IPv4
987 // behind a NAT but IPv6 not, or having an IPv6 NAT.
988 // TODO(deadbeef): Fix this.
989 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700990 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800992 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
993 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -0800994 EXPECT_EQ(4U, ports_.size());
995 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
996 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
997 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
998 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
999 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1000 // The "any" address port should be in the signaled ready ports, but the host
1001 // candidate for it is useless and shouldn't be signaled. So we only have
1002 // STUN/TURN candidates.
1003 EXPECT_EQ(3U, candidates_.size());
1004 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1005 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1006 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1007 // server.
1008 EXPECT_EQ(2,
1009 CountCandidates(candidates_, "relay", "udp",
1010 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001011}
1012
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001013// Test that when enumeration is disabled, we should not have any ports when
1014// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001015TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001016 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001017 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001018 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001019 // Expect to see no ports and no candidates.
1020 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1021 rtc::IPAddress(), rtc::IPAddress());
1022}
1023
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001024// Test that even with multiple interfaces, the result should still be a single
1025// default private, one STUN and one TURN candidate since we bind to any address
1026// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001027TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001028 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001029 AddInterface(kPrivateAddr);
1030 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001031 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001032 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001033
1034 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1035 // address set and we have no IPv6 STUN server, there should be no IPv6
1036 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001037 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1038 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001039
1040 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1041 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1042 // TURN/UDP candidates.
1043 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001044 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1045 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001046}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001047
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001048// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1049// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001050TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001051 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001052 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001053 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001054 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001055 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1056 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1057 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1058 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001059 kTurnUdpExtAddr.ipaddr());
1060}
1061
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001062// Test that when adapter enumeration is disabled, for endpoints without
1063// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001064TEST_F(BasicPortAllocatorTest,
1065 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001066 ResetWithNoServersOrNat();
1067 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1068 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1069 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001070}
1071
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001072// Test that when adapter enumeration is disabled, with
1073// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1074// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001075TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001076 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1077 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001078 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1079 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001080 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1081 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001082 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001083 rtc::IPAddress(), rtc::IPAddress());
1084}
1085
1086// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001087// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1088// a NAT, there is no local candidate. However, this specified default route
1089// (kClientAddr) which was discovered when sending STUN requests, will become
1090// the srflx addresses.
1091TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001092 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001093 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001094 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001095 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001096 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1097 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001098 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1099 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001100 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1101 rtc::IPAddress(), rtc::IPAddress());
1102}
1103
1104// Test that when adapter enumeration is disabled, with
1105// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1106// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001107TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001108 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1109 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001110 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1111 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001112 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1113 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1114 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001115}
1116
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001117// Test that we disable relay over UDP, and only TCP is used when connecting to
1118// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001119TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001120 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001121 AddInterface(kClientAddr);
1122 ResetWithStunServerAndNat(kStunAddr);
1123 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001124 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1125 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1126 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1127 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001128
1129 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001130 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1131 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001132
1133 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1134 // TURN/TCP candidates.
1135 EXPECT_EQ(2U, ports_.size());
1136 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001137 Candidate turn_candidate;
1138 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1139 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001140 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001141 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1142 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001143}
1144
Erik Språngefdce692015-06-05 09:41:26 +02001145// Disable for asan, see
1146// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1147#if !defined(ADDRESS_SANITIZER)
1148
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149// Test that we can get OnCandidatesAllocationDone callback when all the ports
1150// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001151TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001153 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1154 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1155 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001157 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001158 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159}
1160
1161// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001162TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001163 AddInterface(kClientAddr);
1164 fss_->set_udp_sockets_enabled(false);
1165 EXPECT_TRUE(CreateSession(1));
1166 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001167 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1168 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001169 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001170 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1171 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1172 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1173 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1174 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1175 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001176 EXPECT_TRUE(candidate_allocation_done_);
1177}
1178
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001179#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001180
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001181// Test that we don't crash or malfunction if we can't create UDP sockets or
1182// listen on TCP sockets. We still give out a local TCP address, since
1183// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001184TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001185 AddInterface(kClientAddr);
1186 fss_->set_udp_sockets_enabled(false);
1187 fss_->set_tcp_listen_enabled(false);
1188 EXPECT_TRUE(CreateSession(1));
1189 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001190 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1191 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001193 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1194 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1195 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1196 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1197 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001198 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001199 EXPECT_TRUE(candidate_allocation_done_);
1200}
1201
1202// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001203// TODO(deadbeef): Find a way to exit early here.
1204TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001205 AddInterface(kClientAddr);
1206 fss_->set_tcp_sockets_enabled(false);
1207 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001208 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001209 session_->StartGettingPorts();
1210 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001211 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212 // In case of Relay, ports creation will succeed but sockets will fail.
1213 // There is no error reporting from RelayEntry to handle this failure.
1214}
1215
1216// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001217TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001218 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1219 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001220 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001221 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001222 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1223 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001224 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001225 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1226 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001227 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001228 // will be tried after about 3 seconds.
1229 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001231 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1232 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1233 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1234 kRelaySslTcpIntAddr);
1235 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001236 // We wait at least for a full STUN timeout, which
1237 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1238 // already passed (see above), we wait 3 seconds less than that.
1239 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1240 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241}
1242
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001243TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001244 AddInterface(kClientAddr);
1245 AddInterface(kClientAddr2);
1246 // Allocating only host UDP ports. This is done purely for testing
1247 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001248 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1249 PORTALLOCATOR_DISABLE_RELAY);
1250 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001251 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001252 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1253 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001254 ASSERT_EQ(2U, candidates_.size());
1255 EXPECT_EQ(2U, ports_.size());
1256 // Candidates priorities should be different.
1257 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1258}
1259
1260// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001261TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001262 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001263 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001264 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001265 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1266 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001267 EXPECT_EQ(4U, ports_.size());
1268 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001269 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001270}
1271
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001272// Test that the allocator session uses the candidate filter it's created with,
1273// rather than the filter of its parent allocator.
1274// The filter of the allocator should only affect the next gathering phase,
1275// according to JSEP, which means the *next* allocator session returned.
1276TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1277 AddInterface(kClientAddr);
1278 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1279 // Set candidate filter *after* creating the session. Should have no effect.
1280 allocator().set_candidate_filter(CF_RELAY);
1281 session_->StartGettingPorts();
1282 // 7 candidates and 4 ports is what we would normally get (see the
1283 // TestGetAllPorts* tests).
pthatcher1749bc32017-02-08 13:18:00 -08001284 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1285 fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001286 EXPECT_EQ(4U, ports_.size());
1287 EXPECT_TRUE(candidate_allocation_done_);
1288}
1289
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001290// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1291// This test also verifies that when the allocator is only allowed to use
1292// relay (i.e. IceTransportsType is relay), the raddr is an empty
1293// address with the correct family. This is to prevent any local
1294// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001295TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001296 AddInterface(kClientAddr);
1297 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001298 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001299 allocator().set_candidate_filter(CF_RELAY);
1300 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001301 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001302 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1303 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001304 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1305 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001306
1307 EXPECT_EQ(1U, candidates_.size());
1308 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001309 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1310 EXPECT_EQ(
1311 candidates_[0].related_address(),
1312 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001313}
1314
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001317 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1318 allocator().set_candidate_filter(CF_HOST);
1319 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001320 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001321 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1322 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001323 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1324 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001325 for (const Candidate& candidate : candidates_) {
1326 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001327 }
1328}
1329
1330// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001331TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001333 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001334
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001335 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1336 allocator().set_candidate_filter(CF_REFLEXIVE);
1337 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001338 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001339 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1340 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001341 // Host is behind NAT, no private address will be exposed. Hence only UDP
1342 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1344 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001345 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1346 EXPECT_EQ(
1347 candidates_[0].related_address(),
1348 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001349}
1350
1351// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001352TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001353 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001354 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1355 allocator().set_candidate_filter(CF_REFLEXIVE);
1356 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001357 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001358 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1359 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001361 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001362 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001363 for (const Candidate& candidate : candidates_) {
1364 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001365 }
1366}
1367
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001368// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001369TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001370 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001371 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001372 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001373 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1374 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001375 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1376 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1377 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001378 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001379 for (const Candidate& candidate : candidates_) {
1380 EXPECT_EQ(kIceUfrag0, candidate.username());
1381 EXPECT_EQ(kIcePwd0, candidate.password());
1382 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001383 EXPECT_TRUE(candidate_allocation_done_);
1384}
1385
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001386// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1387// is allocated for udp and stun. Also verify there is only one candidate
1388// (local) if stun candidate is same as local candidate, which will be the case
1389// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001390TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001391 AddInterface(kClientAddr);
1392 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001393 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1394 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001395 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001396 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1397 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001399 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001400 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1401 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001402}
1403
1404// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1405// is allocated for udp and stun. In this test we should expect both stun and
1406// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001407TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001408 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001409 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410
1411 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001412 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1413 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001415 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1416 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001417 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001418 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1419 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1420 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001421 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1422 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001423 EXPECT_EQ(3U, candidates_.size());
1424}
1425
deadbeefc5d0d952015-07-16 10:22:21 -07001426// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001427TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001428 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001429 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001430 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001431
1432 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1433
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001434 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001435 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001436 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1437 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001438
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001439 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001440 session_->StartGettingPorts();
1441
pthatcher1749bc32017-02-08 13:18:00 -08001442 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1443 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001445 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1446 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1447 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1448 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1449 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001450 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1451 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001452 EXPECT_EQ(3U, candidates_.size());
1453}
1454
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001455// Test that if prune_turn_ports is set, TCP TURN port will not be used
1456// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1457TEST_F(BasicPortAllocatorTest,
1458 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1459 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1460 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1461 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001462
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001463 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001464}
1465
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001466// Test that if prune_turn_ports is set, TCP TURN port will not be used
1467// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1468TEST_F(BasicPortAllocatorTest,
1469 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1470 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1471 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1472 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001473
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001474 TestUdpTurnPortPrunesTcpTurnPort();
1475}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001476
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001477// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1478// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1479TEST_F(BasicPortAllocatorTest,
1480 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1481 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1482 // first.
1483 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1484 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001485
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001486 TestIPv6TurnPortPrunesIPv4TurnPort();
1487}
1488
1489// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1490// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1491TEST_F(BasicPortAllocatorTest,
1492 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1493 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1494 // first.
1495 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1496 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1497
1498 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001499}
1500
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001501// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001502// will has its own set of TurnPorts based on their priorities, in the default
1503// case where no transit delay is set.
1504TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1505 TestEachInterfaceHasItsOwnTurnPorts();
1506}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001507
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001508// Tests that if prune_turn_ports is set, each network interface
1509// will has its own set of TurnPorts based on their priorities, given that
1510// IPv4/TCP TURN port becomes ready first.
1511TEST_F(BasicPortAllocatorTest,
1512 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1513 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1514 // becomes ready last.
1515 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1516 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1517 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1518 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001519
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001520 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001521}
1522
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001523// Testing DNS resolve for the TURN server, this will test AllocationSequence
1524// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001525// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1526// appears that it doesn't currently because of the DNS look up not
1527// using the fake clock.
1528TEST_F(BasicPortAllocatorTestWithRealClock,
1529 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-15 19:43:33 -07001530 // This test relies on a real query for "localhost", so it won't work on an
1531 // IPv6-only machine.
1532 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001533 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001534 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001535 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001536 allocator_.reset(new BasicPortAllocator(&network_manager_));
1537 RelayServerConfig turn_server(RELAY_TURN);
1538 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001539 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001540 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001541 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001542 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001543
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001544 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001545 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001546 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1547 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001548
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001549 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001550 session_->StartGettingPorts();
1551
1552 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1553}
1554
1555// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1556// is allocated for udp/stun/turn. In this test we should expect all local,
1557// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001558TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001559 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001560 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001561
1562 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1563
1564 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001565 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1566 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001567
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001568 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001569 session_->StartGettingPorts();
1570
pthatcher1749bc32017-02-08 13:18:00 -08001571 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1572 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001573 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001574 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1575 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1576 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1577 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1578 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001579 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1580 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001581 EXPECT_EQ(3U, candidates_.size());
1582 // Local port will be created first and then TURN port.
1583 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1584 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1585}
1586
1587// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1588// server is also used as the STUN server, we should get 'local', 'stun', and
1589// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001590TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001591 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001592 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001593 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001594 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1595
1596 // Must set the step delay to 0 to make sure the relay allocation phase is
1597 // started before the STUN candidates are obtained, so that the STUN binding
1598 // response is processed when both StunPort and TurnPort exist to reproduce
1599 // webrtc issue 3537.
1600 allocator_->set_step_delay(0);
1601 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001602 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1603 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001604
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001605 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001606 session_->StartGettingPorts();
1607
pthatcher1749bc32017-02-08 13:18:00 -08001608 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1609 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001610 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1611 Candidate stun_candidate;
1612 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1613 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1614 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1615 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1616 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001617
pthatcher1749bc32017-02-08 13:18:00 -08001618 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1619 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001620 EXPECT_EQ(3U, candidates_.size());
1621 // Local port will be created first and then TURN port.
1622 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1623 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1624}
1625
deadbeefc5d0d952015-07-16 10:22:21 -07001626// Test that when only a TCP TURN server is available, we do NOT use it as
1627// a UDP STUN server, as this could leak our IP address. Thus we should only
1628// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001629TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001630 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001631 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001632 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001633 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1634
1635 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001636 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1637 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001638
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001639 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001640 session_->StartGettingPorts();
1641
pthatcher1749bc32017-02-08 13:18:00 -08001642 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1643 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001644 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001645 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1646 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1647 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001648 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1649 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001650 EXPECT_EQ(2U, candidates_.size());
1651 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1652 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1653}
1654
1655// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1656// TURN server is used as the STUN server and we get 'local', 'stun', and
1657// 'relay' candidates.
1658// TODO(deadbeef): Remove this test when support for non-shared socket mode
1659// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001660TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001661 AddInterface(kClientAddr);
1662 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001663 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001664 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1665
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001666 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001667
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001668 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001669 session_->StartGettingPorts();
1670
pthatcher1749bc32017-02-08 13:18:00 -08001671 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1672 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001673 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001674 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1675 Candidate stun_candidate;
1676 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1677 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1678 Candidate turn_candidate;
1679 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1680 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1681 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001682 // Not using shared socket, so the STUN request's server reflexive address
1683 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001684 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001685
pthatcher1749bc32017-02-08 13:18:00 -08001686 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1687 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001688 EXPECT_EQ(3U, candidates_.size());
1689 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1690 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1691 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1692}
1693
1694// Test that even when both a STUN and TURN server are configured, the TURN
1695// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001696TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001697 AddInterface(kClientAddr);
1698 // Configure with STUN server but destroy it, so we can ensure that it's
1699 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001700 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001701 stun_server_.reset();
1702 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1703
1704 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001705 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1706 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001707
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001708 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001709 session_->StartGettingPorts();
1710
pthatcher1749bc32017-02-08 13:18:00 -08001711 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1712 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001713 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1714 Candidate stun_candidate;
1715 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1716 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1717 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1718 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1719 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001720
1721 // Don't bother waiting for STUN timeout, since we already verified
1722 // that we got a STUN candidate from the TURN server.
1723}
1724
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001725// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1726// and fail to generate STUN candidate, local UDP candidate is generated
1727// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001728TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001729 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1730 PORTALLOCATOR_DISABLE_TCP |
1731 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001732 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1733 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001734 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001735 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001736 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1737 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001738 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001739 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001740 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001741 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1742 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001743 EXPECT_EQ(1U, candidates_.size());
1744}
1745
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001746// Test that when the NetworkManager doesn't have permission to enumerate
1747// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1748// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001749TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001750 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1751 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001752 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001753 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001754 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1755 PORTALLOCATOR_DISABLE_TCP |
1756 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1757 EXPECT_EQ(0U,
1758 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1759 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1760 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001761 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001762 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1763 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001764 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001765 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001766 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001767}
1768
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001769// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001770TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001771 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1772 PORTALLOCATOR_ENABLE_IPV6 |
1773 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001774 AddInterface(kClientIPv6Addr);
1775 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001776 allocator_->set_step_delay(kMinimumStepDelay);
1777 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001778 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001779 ASSERT_EQ_SIMULATED_WAIT(4U, ports_.size(), kDefaultAllocationTimeout,
1780 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001781 EXPECT_EQ(4U, candidates_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001782 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1783 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001784 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1785 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1786 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1787 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001788 EXPECT_EQ(4U, candidates_.size());
1789}
honghaiz98db68f2015-09-29 07:58:17 -07001790
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001791TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001792 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001793 allocator_->set_step_delay(kDefaultStepDelay);
1794 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001795 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001796 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001797 EXPECT_EQ(2U, ports_.size());
1798 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001799 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001800
1801 // After stopping getting ports, adding a new interface will not start
1802 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001803 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001804 candidates_.clear();
1805 ports_.clear();
1806 candidate_allocation_done_ = false;
1807 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001808 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001809 EXPECT_EQ(0U, candidates_.size());
1810 EXPECT_EQ(0U, ports_.size());
1811}
1812
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001813TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001814 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001815 allocator_->set_step_delay(kDefaultStepDelay);
1816 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001817 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001818 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001819 EXPECT_EQ(2U, ports_.size());
1820 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001821 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001822
1823 // After clearing getting ports, adding a new interface will start getting
1824 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001825 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001826 candidates_.clear();
1827 ports_.clear();
1828 candidate_allocation_done_ = false;
1829 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001830 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001831 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001832 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1833 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001834}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001835
1836// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1837// a pooled session is taken out of the pool.
1838TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1839 AddInterface(kClientAddr);
1840 int pool_size = 1;
1841 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001842 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001843 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001844 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001845 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1846 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001847 // Expect that when TakePooledSession is called,
1848 // UpdateTransportInformationInternal will be called and the
1849 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1850 // candidates.
1851 session_ =
1852 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1853 ASSERT_NE(nullptr, session_.get());
1854 auto ready_ports = session_->ReadyPorts();
1855 auto candidates = session_->ReadyCandidates();
1856 EXPECT_FALSE(ready_ports.empty());
1857 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001858 for (const PortInterface* port_interface : ready_ports) {
1859 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001860 EXPECT_EQ(kContentName, port->content_name());
1861 EXPECT_EQ(1, port->component());
1862 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1863 EXPECT_EQ(kIcePwd0, port->password());
1864 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001865 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001866 EXPECT_EQ(1, candidate.component());
1867 EXPECT_EQ(kIceUfrag0, candidate.username());
1868 EXPECT_EQ(kIcePwd0, candidate.password());
1869 }
1870}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001871
1872// Test that a new candidate filter takes effect even on already-gathered
1873// candidates.
1874TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1875 AddInterface(kClientAddr);
1876 int pool_size = 1;
1877 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001878 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001879 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1880 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001881 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1882 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001883 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1884 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1885 allocator_->set_candidate_filter(CF_RELAY);
1886 // Assume that when TakePooledSession is called, the candidate filter will be
1887 // applied to the pooled session. This is tested by PortAllocatorTest.
1888 session_ =
1889 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1890 ASSERT_NE(nullptr, session_.get());
1891 auto candidates = session_->ReadyCandidates();
1892 auto ports = session_->ReadyPorts();
1893 // Sanity check that the number of candidates and ports decreased.
1894 EXPECT_GT(initial_candidates_size, candidates.size());
1895 EXPECT_GT(initial_ports_size, ports.size());
1896 for (const PortInterface* port : ports) {
1897 // Expect only relay ports.
1898 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1899 }
1900 for (const Candidate& candidate : candidates) {
1901 // Expect only relay candidates now that the filter is applied.
1902 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1903 // Expect that the raddr is emptied due to the CF_RELAY filter.
1904 EXPECT_EQ(candidate.related_address(),
1905 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1906 }
1907}
1908
1909} // namespace cricket