blob: cf967a8ae13ae3fcc943f5ae2cf352e7e7853647 [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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/basicpacketsocketfactory.h"
kjellanderf4752772016-03-02 05:42:30 -080015#include "webrtc/p2p/base/p2pconstants.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/testrelayserver.h"
18#include "webrtc/p2p/base/teststunserver.h"
19#include "webrtc/p2p/base/testturnserver.h"
20#include "webrtc/p2p/client/basicportallocator.h"
21#include "webrtc/p2p/client/httpportallocator.h"
22#include "webrtc/base/fakenetwork.h"
23#include "webrtc/base/firewallsocketserver.h"
24#include "webrtc/base/gunit.h"
25#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070026#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/natserver.h"
29#include "webrtc/base/natsocketfactory.h"
30#include "webrtc/base/network.h"
31#include "webrtc/base/physicalsocketserver.h"
32#include "webrtc/base/socketaddress.h"
33#include "webrtc/base/ssladapter.h"
34#include "webrtc/base/thread.h"
35#include "webrtc/base/virtualsocketserver.h"
36
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
41static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070042static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070043static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000045static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070046static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
47 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070048static const SocketAddress kClientIPv6Addr2(
49 "2401:fa00:4:2000:be30:5bff:fee5:c3",
50 0);
deadbeefc5d0d952015-07-16 10:22:21 -070051static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
52static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
54static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
55static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
56static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
57static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
58static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
59static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
60static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
61static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070062static const SocketAddress kTurnUdpIntIPv6Addr(
63 "2402:fb00:4:1000:be30:5bff:fee5:c3",
64 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070066static const SocketAddress kTurnTcpIntIPv6Addr(
67 "2402:fb00:4:2000:be30:5bff:fee5:c3",
68 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
70
71// Minimum and maximum port for port range tests.
72static const int kMinPort = 10000;
73static const int kMaxPort = 10099;
74
75// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070076static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077// Based on ICE_PWD_LENGTH
78static const char kIcePwd0[] = "TESTICEPWD00000000000000";
79
80static const char kContentName[] = "test content";
81
Honghai Zhangc67e0f52016-09-19 16:57:37 -070082static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083static const char kTurnUsername[] = "test";
84static const char kTurnPassword[] = "test";
85
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070086// STUN timeout (with all retries) is 9500ms.
87// Add some margin of error for slow bots.
88// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
89// fix flaky tests.
90static const int kStunTimeoutMs = 15000;
91
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092namespace cricket {
93
94// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070095std::ostream& operator<<(std::ostream& os,
96 const std::vector<Candidate>& candidates) {
97 os << '[';
98 bool first = true;
99 for (const Candidate& c : candidates) {
100 if (!first) {
101 os << ", ";
102 }
103 os << c.ToString();
104 first = false;
105 };
106 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 return os;
108}
109
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700110class BasicPortAllocatorTest : public testing::Test,
111 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700113 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 : pss_(new rtc::PhysicalSocketServer),
115 vss_(new rtc::VirtualSocketServer(pss_.get())),
116 fss_(new rtc::FirewallSocketServer(vss_.get())),
117 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700118 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700119 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700120 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700121 relay_server_(Thread::Current(),
122 kRelayUdpIntAddr,
123 kRelayUdpExtAddr,
124 kRelayTcpIntAddr,
125 kRelayTcpExtAddr,
126 kRelaySslTcpIntAddr,
127 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
129 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700130 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131 stun_servers.insert(kStunAddr);
132 // Passing the addresses of GTURN servers will enable GTURN in
133 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700134 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
135 kRelayUdpIntAddr, kRelayTcpIntAddr,
136 kRelaySslTcpIntAddr));
137 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 }
139
140 void AddInterface(const SocketAddress& addr) {
141 network_manager_.AddInterface(addr);
142 }
honghaiz8c404fa2015-09-28 07:59:43 -0700143 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
144 network_manager_.AddInterface(addr, if_name);
145 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800146 void AddInterface(const SocketAddress& addr,
147 const std::string& if_name,
148 rtc::AdapterType type) {
149 network_manager_.AddInterface(addr, if_name, type);
150 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800151 // The default route is the public address that STUN server will observe when
152 // the endpoint is sitting on the public internet and the local port is bound
153 // to the "any" address. This may be different from the default local address
154 // which the endpoint observes. This can occur if the route to the public
155 // endpoint like 8.8.8.8 (specified as the default local address) is
156 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700157 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
158 AddInterface(addr);
159 // When a binding comes from the any address, the |addr| will be used as the
160 // srflx address.
161 vss_->SetDefaultRoute(addr.ipaddr());
162 }
honghaiz8c404fa2015-09-28 07:59:43 -0700163 void RemoveInterface(const SocketAddress& addr) {
164 network_manager_.RemoveInterface(addr);
165 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 bool SetPortRange(int min_port, int max_port) {
167 return allocator_->SetPortRange(min_port, max_port);
168 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700169 // Endpoint is on the public network. No STUN or TURN.
170 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700171 allocator_.reset(new BasicPortAllocator(&network_manager_));
172 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700173 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700174 // Endpoint is behind a NAT, with STUN specified.
175 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
176 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700177 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700178 // Endpoint is on the public network, with STUN specified.
179 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
180 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700182 // Endpoint is on the public network, with TURN specified.
183 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
184 const rtc::SocketAddress& tcp_turn) {
185 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 AddTurnServers(udp_turn, tcp_turn);
187 }
188
189 void AddTurnServers(const rtc::SocketAddress& udp_turn,
190 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700191 RelayServerConfig turn_server(RELAY_TURN);
192 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800193 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194
195 if (!udp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700196 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 }
198 if (!tcp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700199 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 }
deadbeef653b8e02015-11-11 12:55:10 -0800201 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 }
203
204 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700205 session_ = CreateSession("session", component);
206 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700208 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 return true;
210 }
211
212 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700213 session_ = CreateSession("session", content_name, component);
214 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700216 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217 return true;
218 }
219
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700220 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
221 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 return CreateSession(sid, kContentName, component);
223 }
224
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700226 const std::string& sid,
227 const std::string& content_name,
228 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
230 }
231
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700232 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700233 const std::string& sid,
234 const std::string& content_name,
235 int component,
236 const std::string& ice_ufrag,
237 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700238 std::unique_ptr<PortAllocatorSession> session =
239 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700241 &BasicPortAllocatorTest::OnPortReady);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700242 session->SignalPortsPruned.connect(this,
243 &BasicPortAllocatorTest::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 session->SignalCandidatesReady.connect(
245 this, &BasicPortAllocatorTest::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700246 session->SignalCandidatesRemoved.connect(
247 this, &BasicPortAllocatorTest::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700248 session->SignalCandidatesAllocationDone.connect(
249 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000250 return session;
251 }
252
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700253 // Return true if the addresses are the same, or the port is 0 in |pattern|
254 // (acting as a wildcard) and the IPs are the same.
255 // Even with a wildcard port, the port of the address should be nonzero if
256 // the IP is nonzero.
257 static bool AddressMatch(const SocketAddress& address,
258 const SocketAddress& pattern) {
259 return address.ipaddr() == pattern.ipaddr() &&
260 ((pattern.port() == 0 &&
261 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
262 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000263 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700264
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700265 // Returns the number of ports that have matching type, protocol and
266 // address.
267 static int CountPorts(const std::vector<PortInterface*>& ports,
268 const std::string& type,
269 ProtocolType protocol,
270 const SocketAddress& client_addr) {
271 return std::count_if(
272 ports.begin(), ports.end(),
273 [type, protocol, client_addr](PortInterface* port) {
274 return port->Type() == type && port->GetProtocol() == protocol &&
275 port->Network()->GetBestIP() == client_addr.ipaddr();
276 });
277 }
278
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700279 // Find a candidate and return it.
280 static bool FindCandidate(const std::vector<Candidate>& candidates,
281 const std::string& type,
282 const std::string& proto,
283 const SocketAddress& addr,
284 Candidate* found) {
285 auto it = std::find_if(candidates.begin(), candidates.end(),
286 [type, proto, addr](const Candidate& c) {
287 return c.type() == type && c.protocol() == proto &&
288 AddressMatch(c.address(), addr);
289 });
290 if (it != candidates.end() && found) {
291 *found = *it;
292 }
293 return it != candidates.end();
294 }
295
296 // Convenience method to call FindCandidate with no return.
297 static bool HasCandidate(const std::vector<Candidate>& candidates,
298 const std::string& type,
299 const std::string& proto,
300 const SocketAddress& addr) {
301 return FindCandidate(candidates, type, proto, addr, nullptr);
302 }
303
304 // Version of HasCandidate that also takes a related address.
305 static bool HasCandidateWithRelatedAddr(
306 const std::vector<Candidate>& candidates,
307 const std::string& type,
308 const std::string& proto,
309 const SocketAddress& addr,
310 const SocketAddress& related_addr) {
311 auto it =
312 std::find_if(candidates.begin(), candidates.end(),
313 [type, proto, addr, related_addr](const Candidate& c) {
314 return c.type() == type && c.protocol() == proto &&
315 AddressMatch(c.address(), addr) &&
316 AddressMatch(c.related_address(), related_addr);
317 });
318 return it != candidates.end();
319 }
320
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000321 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700322 int min_port,
323 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000324 return (addr.port() >= min_port && addr.port() <= max_port);
325 }
326
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700327 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000328 // We should only get this callback once, except in the mux test where
329 // we have multiple port allocation sessions.
330 if (session == session_.get()) {
331 ASSERT_FALSE(candidate_allocation_done_);
332 candidate_allocation_done_ = true;
333 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700334 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000335 }
336
337 // Check if all ports allocated have send-buffer size |expected|. If
338 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
339 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700340 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341 for (it = ports_.begin(); it < ports_.end(); ++it) {
342 int send_buffer_size;
343 if (expected == -1) {
344 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700345 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000346 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700347 EXPECT_EQ(0,
348 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000349 ASSERT_EQ(expected, send_buffer_size);
350 }
351 }
352 }
353
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700354 // This function starts the port/address gathering and check the existence of
355 // candidates as specified. When |expect_stun_candidate| is true,
356 // |stun_candidate_addr| carries the expected reflective address, which is
357 // also the related address for TURN candidate if it is expected. Otherwise,
358 // it should be ignore.
359 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200360 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700361 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700362 const rtc::IPAddress& stun_candidate_addr,
363 const rtc::IPAddress& relay_candidate_udp_transport_addr,
364 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800365 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
366 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700367 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700368 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700369 }
370 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700371 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
372 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700373 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000374 session_->StartGettingPorts();
375 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
376
Peter Boström0c4e06b2015-10-07 12:23:21 +0200377 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700378 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700379 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800380 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700381 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700382 }
383 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700384 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800385 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700386 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800387 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700388 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
389 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700390 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700391 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700392 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700393 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
394 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
395 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700396 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700397 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700398 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700399 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
400 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
401 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700402 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700403 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000404
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700405 EXPECT_EQ(total_candidates, candidates_.size());
406 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000407 }
408
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700409 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
410
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700412 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700414 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000415 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
416 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700417 // Make sure the new port is added to ReadyPorts.
418 auto ready_ports = ses->ReadyPorts();
419 EXPECT_NE(ready_ports.end(),
420 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000421 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700422 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700423 const std::vector<PortInterface*>& pruned_ports) {
424 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700425 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700426 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700427 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700428 new_end = std::remove(ports_.begin(), new_end, port);
429 // Make sure the pruned port is not in ReadyPorts.
430 EXPECT_EQ(ready_ports.end(),
431 std::find(ready_ports.begin(), ready_ports.end(), port));
432 }
433 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700434 }
435
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700436 void OnCandidatesReady(PortAllocatorSession* ses,
437 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700438 for (const Candidate& candidate : candidates) {
439 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
440 // Sanity check that the ICE component is set.
441 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
442 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000443 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700444 // Make sure the new candidates are added to Candidates.
445 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700446 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700447 EXPECT_NE(
448 ses_candidates.end(),
449 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
450 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000451 }
452
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700453 void OnCandidatesRemoved(PortAllocatorSession* session,
454 const std::vector<Candidate>& removed_candidates) {
455 auto new_end = std::remove_if(
456 candidates_.begin(), candidates_.end(),
457 [removed_candidates](Candidate& candidate) {
458 for (const Candidate& removed_candidate : removed_candidates) {
459 if (candidate.MatchesForRemoval(removed_candidate)) {
460 return true;
461 }
462 }
463 return false;
464 });
465 candidates_.erase(new_end, candidates_.end());
466 }
467
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700468 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800469 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700470 RelayServerConfig server_config = allocator_->turn_servers()[i];
471 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000472 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700473 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000474 if (proto_addr.address == relay_port->address &&
475 proto_addr.proto == relay_port->proto)
476 return true;
477 }
478 }
479 return false;
480 }
481
Guo-wei Shieh11477022015-08-15 09:28:41 -0700482 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
483 bool with_nat) {
484 if (with_nat) {
485 nat_server_.reset(new rtc::NATServer(
486 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
487 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
488 } else {
489 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
490 }
491
492 ServerAddresses stun_servers;
493 if (!stun_server.IsNil()) {
494 stun_servers.insert(stun_server);
495 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700496 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700497 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700498 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700499 }
500
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700501 void TestUdpTurnPortPrunesTcpTurnPort() {
502 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
503 AddInterface(kClientAddr);
504 allocator_.reset(new BasicPortAllocator(&network_manager_));
505 allocator_->SetConfiguration(allocator_->stun_servers(),
506 allocator_->turn_servers(), 0, true);
507 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
508 allocator_->set_step_delay(kMinimumStepDelay);
509 allocator_->set_flags(allocator().flags() |
510 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
511 PORTALLOCATOR_DISABLE_TCP);
512
513 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
514 session_->StartGettingPorts();
515 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
516 // Only 2 ports (one STUN and one TURN) are actually being used.
517 EXPECT_EQ(2U, session_->ReadyPorts().size());
518 // We have verified that each port, when it is added to |ports_|, it is
519 // found in |ready_ports|, and when it is pruned, it is not found in
520 // |ready_ports|, so we only need to verify the content in one of them.
521 EXPECT_EQ(2U, ports_.size());
522 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
523 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
524 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
525
526 // Now that we remove candidates when a TURN port is pruned, |candidates_|
527 // should only contains two candidates regardless whether the TCP TURN port
528 // is created before or after the UDP turn port.
529 EXPECT_EQ(2U, candidates_.size());
530 // There will only be 2 candidates in |ready_candidates| because it only
531 // includes the candidates in the ready ports.
532 const std::vector<Candidate>& ready_candidates =
533 session_->ReadyCandidates();
534 EXPECT_EQ(2U, ready_candidates.size());
535 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
536 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
537 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
538 }
539
540 void TestIPv6TurnPortPrunesIPv4TurnPort() {
541 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
542 // Add two IP addresses on the same interface.
543 AddInterface(kClientAddr, "net1");
544 AddInterface(kClientIPv6Addr, "net1");
545 allocator_.reset(new BasicPortAllocator(&network_manager_));
546 allocator_->SetConfiguration(allocator_->stun_servers(),
547 allocator_->turn_servers(), 0, true);
548 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
549 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
550
551 allocator_->set_step_delay(kMinimumStepDelay);
552 allocator_->set_flags(
553 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
554 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
555
556 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
557 session_->StartGettingPorts();
558 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
559 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
560 EXPECT_EQ(3U, session_->ReadyPorts().size());
561 EXPECT_EQ(3U, ports_.size());
562 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
563 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
564 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
565 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
566
567 // Now that we remove candidates when a TURN port is pruned, there will be
568 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
569 EXPECT_EQ(3U, candidates_.size());
570 const std::vector<Candidate>& ready_candidates =
571 session_->ReadyCandidates();
572 EXPECT_EQ(3U, ready_candidates.size());
573 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
574 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
575 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
576 }
577
578 void TestEachInterfaceHasItsOwnTurnPorts() {
579 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
580 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
581 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
582 // Add two interfaces both having IPv4 and IPv6 addresses.
583 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
584 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
585 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
586 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
587 allocator_.reset(new BasicPortAllocator(&network_manager_));
588 allocator_->SetConfiguration(allocator_->stun_servers(),
589 allocator_->turn_servers(), 0, true);
590 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
591 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
592 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
593
594 allocator_->set_step_delay(kMinimumStepDelay);
595 allocator_->set_flags(allocator().flags() |
596 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
597 PORTALLOCATOR_ENABLE_IPV6);
598 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
599 session_->StartGettingPorts();
600 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
601 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
602 // use.
603 EXPECT_EQ(10U, session_->ReadyPorts().size());
604 EXPECT_EQ(10U, ports_.size());
605 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
606 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
607 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
608 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
609 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
610 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
611 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
612 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
613 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
614 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
615
616 // Now that we remove candidates when TURN ports are pruned, there will be
617 // exactly 10 candidates in |candidates_|.
618 EXPECT_EQ(10U, candidates_.size());
619 const std::vector<Candidate>& ready_candidates =
620 session_->ReadyCandidates();
621 EXPECT_EQ(10U, ready_candidates.size());
622 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
623 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
624 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
625 kClientIPv6Addr);
626 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
627 kClientIPv6Addr2);
628 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
629 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
630 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
631 kClientIPv6Addr);
632 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
633 kClientIPv6Addr2);
634 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
635 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
636 }
637
kwiberg3ec46792016-04-27 07:22:53 -0700638 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
639 std::unique_ptr<rtc::VirtualSocketServer> vss_;
640 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000641 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700642 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000643 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700644 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700645 std::unique_ptr<TestStunServer> stun_server_;
646 TestRelayServer relay_server_;
647 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000648 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700649 std::unique_ptr<BasicPortAllocator> allocator_;
650 std::unique_ptr<PortAllocatorSession> session_;
651 std::vector<PortInterface*> ports_;
652 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000653 bool candidate_allocation_done_;
654};
655
656// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700657TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000658 EXPECT_EQ(&network_manager_, allocator().network_manager());
659 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800660 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700661 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000662 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800663 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
664 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700665 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
666 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
667 EXPECT_TRUE(
668 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
669 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700670 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000671}
672
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800673// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700674TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800675 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
676 rtc::ADAPTER_TYPE_ETHERNET);
677 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
678 rtc::ADAPTER_TYPE_WIFI);
679 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
680 rtc::ADAPTER_TYPE_CELLULAR);
681 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
682 rtc::ADAPTER_TYPE_VPN);
683 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
684 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700685 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
686 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
687 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800688 session_->StartGettingPorts();
689 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
690 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700691 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800692 EXPECT_LT(candidate.address().ip(), 0x12345604U);
693 }
694}
695
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700696TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800697 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
698 rtc::ADAPTER_TYPE_ETHERNET);
699 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
700 rtc::ADAPTER_TYPE_WIFI);
701 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
702 rtc::ADAPTER_TYPE_CELLULAR);
703 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
704 rtc::ADAPTER_TYPE_LOOPBACK |
705 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700706 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
707 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
708 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800709 session_->StartGettingPorts();
710 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
711 EXPECT_EQ(1U, candidates_.size());
712 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
713}
714
honghaiz60347052016-05-31 18:29:12 -0700715// Test that high cost networks are filtered if the flag
716// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
717TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
718 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
719 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
720 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
721 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
722 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
723 // interface.
724 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
725 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
726 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
727 cricket::PORTALLOCATOR_DISABLE_RELAY |
728 cricket::PORTALLOCATOR_DISABLE_TCP |
729 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
730 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
731 session_->StartGettingPorts();
732 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
733 EXPECT_EQ(1U, candidates_.size());
734 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
735
736 // If both cellular and unknown interfaces are present, only gather on the
737 // unknown interfaces.
738 candidates_.clear();
739 candidate_allocation_done_ = false;
740 RemoveInterface(addr_wifi);
741 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
742 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
743 session_->StartGettingPorts();
744 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
745 EXPECT_EQ(2U, candidates_.size());
746 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
747 addr_unknown2.EqualIPs(candidates_[1].address())) ||
748 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
749 addr_unknown2.EqualIPs(candidates_[0].address())));
750
751 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
752 // Wi-Fi interface.
753 candidates_.clear();
754 candidate_allocation_done_ = false;
755 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
756 session_->StartGettingPorts();
757 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
758 EXPECT_EQ(1U, candidates_.size());
759 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
760}
761
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000762// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700763TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700764 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000765 session_->StartGettingPorts();
766 // Waiting for one second to make sure BasicPortAllocatorSession has not
767 // called OnAllocate multiple times. In old behavior it's called every 250ms.
768 // When there are no network interfaces, each execution of OnAllocate will
769 // result in SignalCandidatesAllocationDone signal.
770 rtc::Thread::Current()->ProcessMessages(1000);
771 EXPECT_TRUE(candidate_allocation_done_);
772 EXPECT_EQ(0U, candidates_.size());
773}
774
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700775// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700776TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800777 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
778 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700779 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
780 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
781 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700782 session_->StartGettingPorts();
783 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
784 EXPECT_EQ(1U, candidates_.size());
785}
786
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000787// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700788TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000789 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700790 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000791 session_->StartGettingPorts();
792 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
793 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700794 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
795 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
796 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
797 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
798 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
799 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
800 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
801 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000802 EXPECT_TRUE(candidate_allocation_done_);
803}
804
honghaiz8c404fa2015-09-28 07:59:43 -0700805// Test that when the same network interface is brought down and up, the
806// port allocator session will restart a new allocation sequence if
807// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700808TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700809 std::string if_name("test_net0");
810 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700811 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700812 session_->StartGettingPorts();
813 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
814 EXPECT_EQ(4U, ports_.size());
815 EXPECT_TRUE(candidate_allocation_done_);
816 candidate_allocation_done_ = false;
817 candidates_.clear();
818 ports_.clear();
819
820 RemoveInterface(kClientAddr);
821 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
822 EXPECT_EQ(0U, ports_.size());
823 EXPECT_FALSE(candidate_allocation_done_);
824
825 // When the same interfaces are added again, new candidates/ports should be
826 // generated.
827 AddInterface(kClientAddr, if_name);
828 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
829 EXPECT_EQ(4U, ports_.size());
830 EXPECT_TRUE(candidate_allocation_done_);
831}
832
833// Test that when the same network interface is brought down and up, the
834// port allocator session will not restart a new allocation sequence if
835// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700836TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700837 std::string if_name("test_net0");
838 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700839 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700840 session_->StartGettingPorts();
841 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
842 EXPECT_EQ(4U, ports_.size());
843 EXPECT_TRUE(candidate_allocation_done_);
844 session_->StopGettingPorts();
845 candidates_.clear();
846 ports_.clear();
847
848 RemoveInterface(kClientAddr);
849 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
850 EXPECT_EQ(0U, ports_.size());
851
852 // When the same interfaces are added again, new candidates/ports should not
853 // be generated because the session has stopped.
854 AddInterface(kClientAddr, if_name);
855 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
856 EXPECT_EQ(0U, ports_.size());
857 EXPECT_TRUE(candidate_allocation_done_);
858}
859
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000860// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700861TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000862 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700863 allocator_->set_step_delay(kDefaultStepDelay);
864 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000865 session_->StartGettingPorts();
866 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
867 EXPECT_EQ(2U, ports_.size());
868 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
869 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700870 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
871 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000872 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700873 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
874 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000875 EXPECT_EQ(4U, ports_.size());
876 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700877 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
878 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000879 EXPECT_EQ(4U, ports_.size());
880 EXPECT_TRUE(candidate_allocation_done_);
881 // If we Stop gathering now, we shouldn't get a second "done" callback.
882 session_->StopGettingPorts();
883}
884
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700885TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000886 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700887 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000888 session_->StartGettingPorts();
889 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
890 EXPECT_TRUE(candidate_allocation_done_);
891 // If we Stop gathering now, we shouldn't get a second "done" callback.
892 session_->StopGettingPorts();
893
894 // All ports should have unset send-buffer sizes.
895 CheckSendBufferSizesOfAllPorts(-1);
896}
897
898// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700899TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700901 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000902 session_->StartGettingPorts();
903 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
904 EXPECT_EQ(2U, ports_.size());
905 session_->StopGettingPorts();
906 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
907}
908
909// Test that we restrict client ports appropriately when a port range is set.
910// We check the candidates for udp/stun/tcp ports, and the from address
911// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700912TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000913 AddInterface(kClientAddr);
914 // Check that an invalid port range fails.
915 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
916 // Check that a null port range succeeds.
917 EXPECT_TRUE(SetPortRange(0, 0));
918 // Check that a valid port range succeeds.
919 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700920 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000921 session_->StartGettingPorts();
922 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
923 EXPECT_EQ(4U, ports_.size());
924 // Check the port number for the UDP port object.
925 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
926 // Check the port number for the STUN port object.
927 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
928 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700929 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
930 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000931 // Check the port number for the TCP port object.
932 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
933 EXPECT_TRUE(candidate_allocation_done_);
934}
935
936// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700937TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700938 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000939 session_->StartGettingPorts();
940 rtc::Thread::Current()->ProcessMessages(100);
941 // Without network adapter, we should not get any candidate.
942 EXPECT_EQ(0U, candidates_.size());
943 EXPECT_TRUE(candidate_allocation_done_);
944}
945
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700946// Test that when enumeration is disabled, we should not have any ports when
947// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700948TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700949 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700950 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700951 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700952 // Expect to see no ports and no candidates.
953 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
954 rtc::IPAddress(), rtc::IPAddress());
955}
956
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800957// Test that even with multiple interfaces, the result should still be a single
958// default private, one STUN and one TURN candidate since we bind to any address
959// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700960TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700961 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000962 AddInterface(kPrivateAddr);
963 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700964 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000965 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800966
967 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
968 // address set and we have no IPv6 STUN server, there should be no IPv6
969 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700970 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
971 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800972
973 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
974 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
975 // TURN/UDP candidates.
976 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800977 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
978 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700979}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000980
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800981// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
982// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700983TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700984 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800985 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700986 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700987 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800988 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
989 // private, STUN, TURN/UDP, and TURN/TCP candidates.
990 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
991 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700992 kTurnUdpExtAddr.ipaddr());
993}
994
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800995// Test that when adapter enumeration is disabled, for endpoints without
996// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700997TEST_F(BasicPortAllocatorTest,
998 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800999 ResetWithNoServersOrNat();
1000 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1001 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1002 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001003}
1004
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001005// Test that when adapter enumeration is disabled, with
1006// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1007// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001008TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001009 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1010 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001011 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1012 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001013 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1014 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001015 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001016 rtc::IPAddress(), rtc::IPAddress());
1017}
1018
1019// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001020// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1021// a NAT, there is no local candidate. However, this specified default route
1022// (kClientAddr) which was discovered when sending STUN requests, will become
1023// the srflx addresses.
1024TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001025 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001026 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001027 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001028 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001029 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1030 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001031 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1032 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001033 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1034 rtc::IPAddress(), rtc::IPAddress());
1035}
1036
1037// Test that when adapter enumeration is disabled, with
1038// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1039// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001040TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001041 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1042 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001043 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1044 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001045 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1046 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1047 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001048}
1049
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001050// Test that we disable relay over UDP, and only TCP is used when connecting to
1051// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001052TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001053 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001054 AddInterface(kClientAddr);
1055 ResetWithStunServerAndNat(kStunAddr);
1056 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001057 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1058 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1059 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1060 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001061
1062 session_->StartGettingPorts();
1063 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1064
1065 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1066 // TURN/TCP candidates.
1067 EXPECT_EQ(2U, ports_.size());
1068 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001069 Candidate turn_candidate;
1070 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1071 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001072 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001073 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1074 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001075}
1076
Erik Språngefdce692015-06-05 09:41:26 +02001077// Disable for asan, see
1078// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1079#if !defined(ADDRESS_SANITIZER)
1080
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001081// Test that we can get OnCandidatesAllocationDone callback when all the ports
1082// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001083TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001084 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001085 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1086 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1087 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001088 session_->StartGettingPorts();
1089 rtc::Thread::Current()->ProcessMessages(100);
1090 EXPECT_EQ(0U, candidates_.size());
1091 EXPECT_TRUE(candidate_allocation_done_);
1092}
1093
1094// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001095TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001096 AddInterface(kClientAddr);
1097 fss_->set_udp_sockets_enabled(false);
1098 EXPECT_TRUE(CreateSession(1));
1099 session_->StartGettingPorts();
1100 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1101 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001102 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1103 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1104 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1105 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1106 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1107 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001108 EXPECT_TRUE(candidate_allocation_done_);
1109}
1110
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001111#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001112
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001113// Test that we don't crash or malfunction if we can't create UDP sockets or
1114// listen on TCP sockets. We still give out a local TCP address, since
1115// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001116TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001117 AddInterface(kClientAddr);
1118 fss_->set_udp_sockets_enabled(false);
1119 fss_->set_tcp_listen_enabled(false);
1120 EXPECT_TRUE(CreateSession(1));
1121 session_->StartGettingPorts();
1122 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1123 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001124 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1125 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1126 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1127 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1128 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001129 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001130 EXPECT_TRUE(candidate_allocation_done_);
1131}
1132
1133// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001134// TODO(deadbeef): Find a way to exit early here.
1135TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001136 AddInterface(kClientAddr);
1137 fss_->set_tcp_sockets_enabled(false);
1138 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001139 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 session_->StartGettingPorts();
1141 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001142 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143 // In case of Relay, ports creation will succeed but sockets will fail.
1144 // There is no error reporting from RelayEntry to handle this failure.
1145}
1146
1147// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001148TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1150 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001151 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 session_->StartGettingPorts();
1153 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1154 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001155 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1156 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
1158 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -07001159 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
1160 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001161 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001162 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1163 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1164 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1165 kRelaySslTcpIntAddr);
1166 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -07001167 // Stun Timeout is 9.5sec.
1168 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
1169 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001170}
1171
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001172TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001173 AddInterface(kClientAddr);
1174 AddInterface(kClientAddr2);
1175 // Allocating only host UDP ports. This is done purely for testing
1176 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001177 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1178 PORTALLOCATOR_DISABLE_RELAY);
1179 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001180 session_->StartGettingPorts();
1181 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1182 ASSERT_EQ(2U, candidates_.size());
1183 EXPECT_EQ(2U, ports_.size());
1184 // Candidates priorities should be different.
1185 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1186}
1187
1188// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001189TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001191 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 session_->StartGettingPorts();
1193 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1194 EXPECT_EQ(4U, ports_.size());
1195 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001196 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001197}
1198
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001199// Test that the allocator session uses the candidate filter it's created with,
1200// rather than the filter of its parent allocator.
1201// The filter of the allocator should only affect the next gathering phase,
1202// according to JSEP, which means the *next* allocator session returned.
1203TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1204 AddInterface(kClientAddr);
1205 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1206 // Set candidate filter *after* creating the session. Should have no effect.
1207 allocator().set_candidate_filter(CF_RELAY);
1208 session_->StartGettingPorts();
1209 // 7 candidates and 4 ports is what we would normally get (see the
1210 // TestGetAllPorts* tests).
1211 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1212 EXPECT_EQ(4U, ports_.size());
1213 EXPECT_TRUE(candidate_allocation_done_);
1214}
1215
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001216// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1217// This test also verifies that when the allocator is only allowed to use
1218// relay (i.e. IceTransportsType is relay), the raddr is an empty
1219// address with the correct family. This is to prevent any local
1220// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001221TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001222 AddInterface(kClientAddr);
1223 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001224 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001225 allocator().set_candidate_filter(CF_RELAY);
1226 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001227 session_->StartGettingPorts();
1228 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001229 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1230 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001231
1232 EXPECT_EQ(1U, candidates_.size());
1233 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001234 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1235 EXPECT_EQ(
1236 candidates_[0].related_address(),
1237 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001238}
1239
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001240TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001242 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1243 allocator().set_candidate_filter(CF_HOST);
1244 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001245 session_->StartGettingPorts();
1246 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001247 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1248 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001249 for (const Candidate& candidate : candidates_) {
1250 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001251 }
1252}
1253
1254// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001255TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001256 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001257 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001258
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001259 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1260 allocator().set_candidate_filter(CF_REFLEXIVE);
1261 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001262 session_->StartGettingPorts();
1263 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1264 // Host is behind NAT, no private address will be exposed. Hence only UDP
1265 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001266 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1267 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001268 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1269 EXPECT_EQ(
1270 candidates_[0].related_address(),
1271 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001272}
1273
1274// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001275TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001276 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001277 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1278 allocator().set_candidate_filter(CF_REFLEXIVE);
1279 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001280 session_->StartGettingPorts();
1281 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1282 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001283 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001284 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001285 for (const Candidate& candidate : candidates_) {
1286 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001287 }
1288}
1289
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001290// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001291TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001292 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001293 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001294 session_->StartGettingPorts();
1295 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001296 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1297 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1298 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001299 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001300 for (const Candidate& candidate : candidates_) {
1301 EXPECT_EQ(kIceUfrag0, candidate.username());
1302 EXPECT_EQ(kIcePwd0, candidate.password());
1303 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001304 EXPECT_TRUE(candidate_allocation_done_);
1305}
1306
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001307// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1308// is allocated for udp and stun. Also verify there is only one candidate
1309// (local) if stun candidate is same as local candidate, which will be the case
1310// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001311TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001312 AddInterface(kClientAddr);
1313 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001314 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1315 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316 session_->StartGettingPorts();
1317 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1318 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001319 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001320 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1321}
1322
1323// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1324// is allocated for udp and stun. In this test we should expect both stun and
1325// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001326TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001327 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001328 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001329
1330 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001331 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1332 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001333 session_->StartGettingPorts();
1334 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1335 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001336 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1337 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1338 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001339 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1340 EXPECT_EQ(3U, candidates_.size());
1341}
1342
deadbeefc5d0d952015-07-16 10:22:21 -07001343// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001344TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001345 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001346 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001347 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001348
1349 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1350
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001351 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001353 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1354 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001355
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001356 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001357 session_->StartGettingPorts();
1358
1359 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1360 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001361 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1362 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1363 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1364 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1365 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001366 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1367 EXPECT_EQ(3U, candidates_.size());
1368}
1369
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001370// Test that if prune_turn_ports is set, TCP TURN port will not be used
1371// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1372TEST_F(BasicPortAllocatorTest,
1373 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1374 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1375 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1376 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001377
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001378 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001379}
1380
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001381// Test that if prune_turn_ports is set, TCP TURN port will not be used
1382// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1383TEST_F(BasicPortAllocatorTest,
1384 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1385 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1386 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1387 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001388
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001389 TestUdpTurnPortPrunesTcpTurnPort();
1390}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001391
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001392// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1393// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1394TEST_F(BasicPortAllocatorTest,
1395 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1396 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1397 // first.
1398 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1399 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001400
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001401 TestIPv6TurnPortPrunesIPv4TurnPort();
1402}
1403
1404// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1405// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1406TEST_F(BasicPortAllocatorTest,
1407 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1408 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1409 // first.
1410 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1411 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1412
1413 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001414}
1415
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001416// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001417// will has its own set of TurnPorts based on their priorities, in the default
1418// case where no transit delay is set.
1419TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1420 TestEachInterfaceHasItsOwnTurnPorts();
1421}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001422
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001423// Tests that if prune_turn_ports is set, each network interface
1424// will has its own set of TurnPorts based on their priorities, given that
1425// IPv4/TCP TURN port becomes ready first.
1426TEST_F(BasicPortAllocatorTest,
1427 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1428 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1429 // becomes ready last.
1430 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1431 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1432 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1433 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001434
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001435 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001436}
1437
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001438// Testing DNS resolve for the TURN server, this will test AllocationSequence
1439// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001440TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001442 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001443 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001444 allocator_.reset(new BasicPortAllocator(&network_manager_));
1445 RelayServerConfig turn_server(RELAY_TURN);
1446 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001447 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001448 turn_server.ports.push_back(
1449 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001450 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001451
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001452 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001453 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001454 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1455 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001456
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001457 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001458 session_->StartGettingPorts();
1459
1460 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1461}
1462
1463// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1464// is allocated for udp/stun/turn. In this test we should expect all local,
1465// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001466TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001467 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001468 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001469
1470 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1471
1472 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001473 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1474 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001475
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001476 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001477 session_->StartGettingPorts();
1478
1479 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1480 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001481 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1482 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1483 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1484 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1485 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001486 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1487 EXPECT_EQ(3U, candidates_.size());
1488 // Local port will be created first and then TURN port.
1489 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1490 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1491}
1492
1493// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1494// server is also used as the STUN server, we should get 'local', 'stun', and
1495// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001496TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001497 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001498 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001499 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001500 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1501
1502 // Must set the step delay to 0 to make sure the relay allocation phase is
1503 // started before the STUN candidates are obtained, so that the STUN binding
1504 // response is processed when both StunPort and TurnPort exist to reproduce
1505 // webrtc issue 3537.
1506 allocator_->set_step_delay(0);
1507 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001508 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1509 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001510
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001511 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001512 session_->StartGettingPorts();
1513
1514 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001515 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1516 Candidate stun_candidate;
1517 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1518 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1519 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1520 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1521 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001522
1523 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1524 EXPECT_EQ(3U, candidates_.size());
1525 // Local port will be created first and then TURN port.
1526 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1527 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1528}
1529
deadbeefc5d0d952015-07-16 10:22:21 -07001530// Test that when only a TCP TURN server is available, we do NOT use it as
1531// a UDP STUN server, as this could leak our IP address. Thus we should only
1532// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001533TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001534 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001535 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001536 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001537 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1538
1539 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001540 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1541 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001542
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001543 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001544 session_->StartGettingPorts();
1545
1546 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1547 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001548 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1549 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1550 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001551 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1552 EXPECT_EQ(2U, candidates_.size());
1553 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1554 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1555}
1556
1557// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1558// TURN server is used as the STUN server and we get 'local', 'stun', and
1559// 'relay' candidates.
1560// TODO(deadbeef): Remove this test when support for non-shared socket mode
1561// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001562TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001563 AddInterface(kClientAddr);
1564 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001565 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001566 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1567
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001568 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001569
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001570 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001571 session_->StartGettingPorts();
1572
1573 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1574 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001575 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1576 Candidate stun_candidate;
1577 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1578 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1579 Candidate turn_candidate;
1580 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1581 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1582 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001583 // Not using shared socket, so the STUN request's server reflexive address
1584 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001585 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001586
1587 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1588 EXPECT_EQ(3U, candidates_.size());
1589 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1590 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1591 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1592}
1593
1594// Test that even when both a STUN and TURN server are configured, the TURN
1595// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001596TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001597 AddInterface(kClientAddr);
1598 // Configure with STUN server but destroy it, so we can ensure that it's
1599 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001600 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001601 stun_server_.reset();
1602 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1603
1604 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001605 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1606 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001607
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001608 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001609 session_->StartGettingPorts();
1610
1611 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001612 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1613 Candidate stun_candidate;
1614 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1615 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1616 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1617 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1618 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001619
1620 // Don't bother waiting for STUN timeout, since we already verified
1621 // that we got a STUN candidate from the TURN server.
1622}
1623
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001624// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1625// and fail to generate STUN candidate, local UDP candidate is generated
1626// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001627TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001628 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1629 PORTALLOCATOR_DISABLE_TCP |
1630 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001631 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1632 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001633 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001634 session_->StartGettingPorts();
1635 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1636 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001637 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001638 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1639 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001640 EXPECT_EQ(1U, candidates_.size());
1641}
1642
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001643// Test that when the NetworkManager doesn't have permission to enumerate
1644// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1645// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001646TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001647 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1648 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001649 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001650 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001651 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1652 PORTALLOCATOR_DISABLE_TCP |
1653 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1654 EXPECT_EQ(0U,
1655 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1656 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1657 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001658 session_->StartGettingPorts();
1659 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001660 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001661 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001662 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001663}
1664
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001665// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001666TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001667 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1668 PORTALLOCATOR_ENABLE_IPV6 |
1669 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001670 AddInterface(kClientIPv6Addr);
1671 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001672 allocator_->set_step_delay(kMinimumStepDelay);
1673 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001674 session_->StartGettingPorts();
1675 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1676 EXPECT_EQ(4U, candidates_.size());
1677 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001678 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1679 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1680 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1681 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001682 EXPECT_EQ(4U, candidates_.size());
1683}
honghaiz98db68f2015-09-29 07:58:17 -07001684
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001685TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001686 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001687 allocator_->set_step_delay(kDefaultStepDelay);
1688 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001689 session_->StartGettingPorts();
1690 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1691 EXPECT_EQ(2U, ports_.size());
1692 session_->StopGettingPorts();
1693 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1694
1695 // After stopping getting ports, adding a new interface will not start
1696 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001697 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001698 candidates_.clear();
1699 ports_.clear();
1700 candidate_allocation_done_ = false;
1701 network_manager_.AddInterface(kClientAddr2);
1702 rtc::Thread::Current()->ProcessMessages(1000);
1703 EXPECT_EQ(0U, candidates_.size());
1704 EXPECT_EQ(0U, ports_.size());
1705}
1706
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001707TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001708 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001709 allocator_->set_step_delay(kDefaultStepDelay);
1710 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001711 session_->StartGettingPorts();
1712 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1713 EXPECT_EQ(2U, ports_.size());
1714 session_->ClearGettingPorts();
deadbeefb60a8192016-08-24 15:15:00 -07001715 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
honghaiz98db68f2015-09-29 07:58:17 -07001716
1717 // After clearing getting ports, adding a new interface will start getting
1718 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001719 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001720 candidates_.clear();
1721 ports_.clear();
1722 candidate_allocation_done_ = false;
1723 network_manager_.AddInterface(kClientAddr2);
1724 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1725 EXPECT_EQ(2U, ports_.size());
deadbeefb60a8192016-08-24 15:15:00 -07001726 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
honghaiz98db68f2015-09-29 07:58:17 -07001727}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001728
1729// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1730// a pooled session is taken out of the pool.
1731TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1732 AddInterface(kClientAddr);
1733 int pool_size = 1;
1734 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001735 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001736 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001737 ASSERT_NE(nullptr, peeked_session);
1738 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1739 kDefaultAllocationTimeout);
1740 // Expect that when TakePooledSession is called,
1741 // UpdateTransportInformationInternal will be called and the
1742 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1743 // candidates.
1744 session_ =
1745 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1746 ASSERT_NE(nullptr, session_.get());
1747 auto ready_ports = session_->ReadyPorts();
1748 auto candidates = session_->ReadyCandidates();
1749 EXPECT_FALSE(ready_ports.empty());
1750 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001751 for (const PortInterface* port_interface : ready_ports) {
1752 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001753 EXPECT_EQ(kContentName, port->content_name());
1754 EXPECT_EQ(1, port->component());
1755 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1756 EXPECT_EQ(kIcePwd0, port->password());
1757 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001758 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001759 EXPECT_EQ(1, candidate.component());
1760 EXPECT_EQ(kIceUfrag0, candidate.username());
1761 EXPECT_EQ(kIcePwd0, candidate.password());
1762 }
1763}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001764
1765// Test that a new candidate filter takes effect even on already-gathered
1766// candidates.
1767TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1768 AddInterface(kClientAddr);
1769 int pool_size = 1;
1770 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001771 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001772 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1773 ASSERT_NE(nullptr, peeked_session);
1774 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1775 kDefaultAllocationTimeout);
1776 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1777 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1778 allocator_->set_candidate_filter(CF_RELAY);
1779 // Assume that when TakePooledSession is called, the candidate filter will be
1780 // applied to the pooled session. This is tested by PortAllocatorTest.
1781 session_ =
1782 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1783 ASSERT_NE(nullptr, session_.get());
1784 auto candidates = session_->ReadyCandidates();
1785 auto ports = session_->ReadyPorts();
1786 // Sanity check that the number of candidates and ports decreased.
1787 EXPECT_GT(initial_candidates_size, candidates.size());
1788 EXPECT_GT(initial_ports_size, ports.size());
1789 for (const PortInterface* port : ports) {
1790 // Expect only relay ports.
1791 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1792 }
1793 for (const Candidate& candidate : candidates) {
1794 // Expect only relay candidates now that the filter is applied.
1795 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1796 // Expect that the raddr is emptied due to the CF_RELAY filter.
1797 EXPECT_EQ(candidate.related_address(),
1798 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1799 }
1800}
1801
1802} // namespace cricket