blob: 253a15aaf3b2900bec6f9af290cb1e2c81d1ec2a [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"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021#include "webrtc/base/fakenetwork.h"
22#include "webrtc/base/firewallsocketserver.h"
23#include "webrtc/base/gunit.h"
24#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070025#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026#include "webrtc/base/logging.h"
27#include "webrtc/base/natserver.h"
28#include "webrtc/base/natsocketfactory.h"
29#include "webrtc/base/network.h"
30#include "webrtc/base/physicalsocketserver.h"
31#include "webrtc/base/socketaddress.h"
32#include "webrtc/base/ssladapter.h"
33#include "webrtc/base/thread.h"
34#include "webrtc/base/virtualsocketserver.h"
35
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080036using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037using rtc::SocketAddress;
38using rtc::Thread;
39
40static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070041static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070042static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000044static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070045static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
46 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070047static const SocketAddress kClientIPv6Addr2(
48 "2401:fa00:4:2000:be30:5bff:fee5:c3",
49 0);
deadbeefc5d0d952015-07-16 10:22:21 -070050static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
51static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
53static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
54static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
55static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
56static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
57static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
58static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
59static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
60static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070061static const SocketAddress kTurnUdpIntIPv6Addr(
62 "2402:fb00:4:1000:be30:5bff:fee5:c3",
63 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070065static const SocketAddress kTurnTcpIntIPv6Addr(
66 "2402:fb00:4:2000:be30:5bff:fee5:c3",
67 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
69
70// Minimum and maximum port for port range tests.
71static const int kMinPort = 10000;
72static const int kMaxPort = 10099;
73
74// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070075static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076// Based on ICE_PWD_LENGTH
77static const char kIcePwd0[] = "TESTICEPWD00000000000000";
78
79static const char kContentName[] = "test content";
80
Honghai Zhangc67e0f52016-09-19 16:57:37 -070081static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082static const char kTurnUsername[] = "test";
83static const char kTurnPassword[] = "test";
84
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070085// STUN timeout (with all retries) is 9500ms.
86// Add some margin of error for slow bots.
87// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
88// fix flaky tests.
89static const int kStunTimeoutMs = 15000;
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091namespace cricket {
92
93// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070094std::ostream& operator<<(std::ostream& os,
95 const std::vector<Candidate>& candidates) {
96 os << '[';
97 bool first = true;
98 for (const Candidate& c : candidates) {
99 if (!first) {
100 os << ", ";
101 }
102 os << c.ToString();
103 first = false;
104 };
105 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 return os;
107}
108
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700109class BasicPortAllocatorTest : public testing::Test,
110 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700112 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 : pss_(new rtc::PhysicalSocketServer),
114 vss_(new rtc::VirtualSocketServer(pss_.get())),
115 fss_(new rtc::FirewallSocketServer(vss_.get())),
116 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700117 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700118 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700119 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700120 relay_server_(Thread::Current(),
121 kRelayUdpIntAddr,
122 kRelayUdpExtAddr,
123 kRelayTcpIntAddr,
124 kRelayTcpExtAddr,
125 kRelaySslTcpIntAddr,
126 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
128 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700129 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130 stun_servers.insert(kStunAddr);
131 // Passing the addresses of GTURN servers will enable GTURN in
132 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700133 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
134 kRelayUdpIntAddr, kRelayTcpIntAddr,
135 kRelaySslTcpIntAddr));
136 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 }
138
139 void AddInterface(const SocketAddress& addr) {
140 network_manager_.AddInterface(addr);
141 }
honghaiz8c404fa2015-09-28 07:59:43 -0700142 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
143 network_manager_.AddInterface(addr, if_name);
144 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800145 void AddInterface(const SocketAddress& addr,
146 const std::string& if_name,
147 rtc::AdapterType type) {
148 network_manager_.AddInterface(addr, if_name, type);
149 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800150 // The default route is the public address that STUN server will observe when
151 // the endpoint is sitting on the public internet and the local port is bound
152 // to the "any" address. This may be different from the default local address
153 // which the endpoint observes. This can occur if the route to the public
154 // endpoint like 8.8.8.8 (specified as the default local address) is
155 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700156 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
157 AddInterface(addr);
158 // When a binding comes from the any address, the |addr| will be used as the
159 // srflx address.
160 vss_->SetDefaultRoute(addr.ipaddr());
161 }
honghaiz8c404fa2015-09-28 07:59:43 -0700162 void RemoveInterface(const SocketAddress& addr) {
163 network_manager_.RemoveInterface(addr);
164 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 bool SetPortRange(int min_port, int max_port) {
166 return allocator_->SetPortRange(min_port, max_port);
167 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700168 // Endpoint is on the public network. No STUN or TURN.
169 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700170 allocator_.reset(new BasicPortAllocator(&network_manager_));
171 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700172 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700173 // Endpoint is behind a NAT, with STUN specified.
174 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
175 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700176 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700177 // Endpoint is on the public network, with STUN specified.
178 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
179 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700181 // Endpoint is on the public network, with TURN specified.
182 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
183 const rtc::SocketAddress& tcp_turn) {
184 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 AddTurnServers(udp_turn, tcp_turn);
186 }
187
188 void AddTurnServers(const rtc::SocketAddress& udp_turn,
189 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700190 RelayServerConfig turn_server(RELAY_TURN);
191 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800192 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193
194 if (!udp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700195 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196 }
197 if (!tcp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700198 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 }
deadbeef653b8e02015-11-11 12:55:10 -0800200 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 }
202
203 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700204 session_ = CreateSession("session", component);
205 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700207 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 return true;
209 }
210
211 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700212 session_ = CreateSession("session", content_name, component);
213 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700215 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 return true;
217 }
218
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700219 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
220 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return CreateSession(sid, kContentName, component);
222 }
223
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700224 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 const std::string& sid,
226 const std::string& content_name,
227 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
229 }
230
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700231 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700232 const std::string& sid,
233 const std::string& content_name,
234 int component,
235 const std::string& ice_ufrag,
236 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700237 std::unique_ptr<PortAllocatorSession> session =
238 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700240 &BasicPortAllocatorTest::OnPortReady);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700241 session->SignalPortsPruned.connect(this,
242 &BasicPortAllocatorTest::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700243 session->SignalCandidatesReady.connect(
244 this, &BasicPortAllocatorTest::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700245 session->SignalCandidatesRemoved.connect(
246 this, &BasicPortAllocatorTest::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700247 session->SignalCandidatesAllocationDone.connect(
248 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 return session;
250 }
251
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700252 // Return true if the addresses are the same, or the port is 0 in |pattern|
253 // (acting as a wildcard) and the IPs are the same.
254 // Even with a wildcard port, the port of the address should be nonzero if
255 // the IP is nonzero.
256 static bool AddressMatch(const SocketAddress& address,
257 const SocketAddress& pattern) {
258 return address.ipaddr() == pattern.ipaddr() &&
259 ((pattern.port() == 0 &&
260 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
261 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700263
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700264 // Returns the number of ports that have matching type, protocol and
265 // address.
266 static int CountPorts(const std::vector<PortInterface*>& ports,
267 const std::string& type,
268 ProtocolType protocol,
269 const SocketAddress& client_addr) {
270 return std::count_if(
271 ports.begin(), ports.end(),
272 [type, protocol, client_addr](PortInterface* port) {
273 return port->Type() == type && port->GetProtocol() == protocol &&
274 port->Network()->GetBestIP() == client_addr.ipaddr();
275 });
276 }
277
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700278 // Find a candidate and return it.
279 static bool FindCandidate(const std::vector<Candidate>& candidates,
280 const std::string& type,
281 const std::string& proto,
282 const SocketAddress& addr,
283 Candidate* found) {
284 auto it = std::find_if(candidates.begin(), candidates.end(),
285 [type, proto, addr](const Candidate& c) {
286 return c.type() == type && c.protocol() == proto &&
287 AddressMatch(c.address(), addr);
288 });
289 if (it != candidates.end() && found) {
290 *found = *it;
291 }
292 return it != candidates.end();
293 }
294
295 // Convenience method to call FindCandidate with no return.
296 static bool HasCandidate(const std::vector<Candidate>& candidates,
297 const std::string& type,
298 const std::string& proto,
299 const SocketAddress& addr) {
300 return FindCandidate(candidates, type, proto, addr, nullptr);
301 }
302
303 // Version of HasCandidate that also takes a related address.
304 static bool HasCandidateWithRelatedAddr(
305 const std::vector<Candidate>& candidates,
306 const std::string& type,
307 const std::string& proto,
308 const SocketAddress& addr,
309 const SocketAddress& related_addr) {
310 auto it =
311 std::find_if(candidates.begin(), candidates.end(),
312 [type, proto, addr, related_addr](const Candidate& c) {
313 return c.type() == type && c.protocol() == proto &&
314 AddressMatch(c.address(), addr) &&
315 AddressMatch(c.related_address(), related_addr);
316 });
317 return it != candidates.end();
318 }
319
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000320 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700321 int min_port,
322 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323 return (addr.port() >= min_port && addr.port() <= max_port);
324 }
325
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700326 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000327 // We should only get this callback once, except in the mux test where
328 // we have multiple port allocation sessions.
329 if (session == session_.get()) {
330 ASSERT_FALSE(candidate_allocation_done_);
331 candidate_allocation_done_ = true;
332 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700333 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334 }
335
336 // Check if all ports allocated have send-buffer size |expected|. If
337 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
338 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700339 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000340 for (it = ports_.begin(); it < ports_.end(); ++it) {
341 int send_buffer_size;
342 if (expected == -1) {
343 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700344 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000345 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700346 EXPECT_EQ(0,
347 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000348 ASSERT_EQ(expected, send_buffer_size);
349 }
350 }
351 }
352
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700353 // This function starts the port/address gathering and check the existence of
354 // candidates as specified. When |expect_stun_candidate| is true,
355 // |stun_candidate_addr| carries the expected reflective address, which is
356 // also the related address for TURN candidate if it is expected. Otherwise,
357 // it should be ignore.
358 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200359 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700360 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700361 const rtc::IPAddress& stun_candidate_addr,
362 const rtc::IPAddress& relay_candidate_udp_transport_addr,
363 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800364 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
365 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700366 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700367 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700368 }
369 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700370 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
371 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700372 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000373 session_->StartGettingPorts();
374 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
375
Peter Boström0c4e06b2015-10-07 12:23:21 +0200376 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700377 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700378 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800379 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700380 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700381 }
382 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700383 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800384 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700385 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800386 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700387 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
388 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700389 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700390 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700391 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700392 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
393 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
394 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700395 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700396 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700397 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700398 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
399 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
400 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700401 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700402 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000403
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700404 EXPECT_EQ(total_candidates, candidates_.size());
405 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000406 }
407
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700408 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
409
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700411 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000412
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700413 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000414 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
415 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700416 // Make sure the new port is added to ReadyPorts.
417 auto ready_ports = ses->ReadyPorts();
418 EXPECT_NE(ready_ports.end(),
419 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000420 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700421 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700422 const std::vector<PortInterface*>& pruned_ports) {
423 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700424 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700425 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700426 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700427 new_end = std::remove(ports_.begin(), new_end, port);
428 // Make sure the pruned port is not in ReadyPorts.
429 EXPECT_EQ(ready_ports.end(),
430 std::find(ready_ports.begin(), ready_ports.end(), port));
431 }
432 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700433 }
434
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700435 void OnCandidatesReady(PortAllocatorSession* ses,
436 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700437 for (const Candidate& candidate : candidates) {
438 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
439 // Sanity check that the ICE component is set.
440 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
441 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700443 // Make sure the new candidates are added to Candidates.
444 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700445 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700446 EXPECT_NE(
447 ses_candidates.end(),
448 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
449 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000450 }
451
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700452 void OnCandidatesRemoved(PortAllocatorSession* session,
453 const std::vector<Candidate>& removed_candidates) {
454 auto new_end = std::remove_if(
455 candidates_.begin(), candidates_.end(),
456 [removed_candidates](Candidate& candidate) {
457 for (const Candidate& removed_candidate : removed_candidates) {
458 if (candidate.MatchesForRemoval(removed_candidate)) {
459 return true;
460 }
461 }
462 return false;
463 });
464 candidates_.erase(new_end, candidates_.end());
465 }
466
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700467 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800468 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700469 RelayServerConfig server_config = allocator_->turn_servers()[i];
470 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000471 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700472 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000473 if (proto_addr.address == relay_port->address &&
474 proto_addr.proto == relay_port->proto)
475 return true;
476 }
477 }
478 return false;
479 }
480
Guo-wei Shieh11477022015-08-15 09:28:41 -0700481 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
482 bool with_nat) {
483 if (with_nat) {
484 nat_server_.reset(new rtc::NATServer(
485 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
486 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
487 } else {
488 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
489 }
490
491 ServerAddresses stun_servers;
492 if (!stun_server.IsNil()) {
493 stun_servers.insert(stun_server);
494 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700495 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700496 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700497 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700498 }
499
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700500 void TestUdpTurnPortPrunesTcpTurnPort() {
501 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
502 AddInterface(kClientAddr);
503 allocator_.reset(new BasicPortAllocator(&network_manager_));
504 allocator_->SetConfiguration(allocator_->stun_servers(),
505 allocator_->turn_servers(), 0, true);
506 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
507 allocator_->set_step_delay(kMinimumStepDelay);
508 allocator_->set_flags(allocator().flags() |
509 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
510 PORTALLOCATOR_DISABLE_TCP);
511
512 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
513 session_->StartGettingPorts();
514 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
515 // Only 2 ports (one STUN and one TURN) are actually being used.
516 EXPECT_EQ(2U, session_->ReadyPorts().size());
517 // We have verified that each port, when it is added to |ports_|, it is
518 // found in |ready_ports|, and when it is pruned, it is not found in
519 // |ready_ports|, so we only need to verify the content in one of them.
520 EXPECT_EQ(2U, ports_.size());
521 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
522 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
523 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
524
525 // Now that we remove candidates when a TURN port is pruned, |candidates_|
526 // should only contains two candidates regardless whether the TCP TURN port
527 // is created before or after the UDP turn port.
528 EXPECT_EQ(2U, candidates_.size());
529 // There will only be 2 candidates in |ready_candidates| because it only
530 // includes the candidates in the ready ports.
531 const std::vector<Candidate>& ready_candidates =
532 session_->ReadyCandidates();
533 EXPECT_EQ(2U, ready_candidates.size());
534 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
535 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
536 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
537 }
538
539 void TestIPv6TurnPortPrunesIPv4TurnPort() {
540 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
541 // Add two IP addresses on the same interface.
542 AddInterface(kClientAddr, "net1");
543 AddInterface(kClientIPv6Addr, "net1");
544 allocator_.reset(new BasicPortAllocator(&network_manager_));
545 allocator_->SetConfiguration(allocator_->stun_servers(),
546 allocator_->turn_servers(), 0, true);
547 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
548 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
549
550 allocator_->set_step_delay(kMinimumStepDelay);
551 allocator_->set_flags(
552 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
553 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
554
555 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
556 session_->StartGettingPorts();
557 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
558 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
559 EXPECT_EQ(3U, session_->ReadyPorts().size());
560 EXPECT_EQ(3U, ports_.size());
561 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
562 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
563 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
564 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
565
566 // Now that we remove candidates when a TURN port is pruned, there will be
567 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
568 EXPECT_EQ(3U, candidates_.size());
569 const std::vector<Candidate>& ready_candidates =
570 session_->ReadyCandidates();
571 EXPECT_EQ(3U, ready_candidates.size());
572 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
573 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
574 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
575 }
576
577 void TestEachInterfaceHasItsOwnTurnPorts() {
578 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
579 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
580 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
581 // Add two interfaces both having IPv4 and IPv6 addresses.
582 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
583 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
584 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
585 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
586 allocator_.reset(new BasicPortAllocator(&network_manager_));
587 allocator_->SetConfiguration(allocator_->stun_servers(),
588 allocator_->turn_servers(), 0, true);
589 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
590 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
591 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
592
593 allocator_->set_step_delay(kMinimumStepDelay);
594 allocator_->set_flags(allocator().flags() |
595 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
596 PORTALLOCATOR_ENABLE_IPV6);
597 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
598 session_->StartGettingPorts();
599 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
600 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
601 // use.
602 EXPECT_EQ(10U, session_->ReadyPorts().size());
603 EXPECT_EQ(10U, ports_.size());
604 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
605 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
606 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
607 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
608 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
609 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
610 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
611 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
612 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
613 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
614
615 // Now that we remove candidates when TURN ports are pruned, there will be
616 // exactly 10 candidates in |candidates_|.
617 EXPECT_EQ(10U, candidates_.size());
618 const std::vector<Candidate>& ready_candidates =
619 session_->ReadyCandidates();
620 EXPECT_EQ(10U, ready_candidates.size());
621 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
622 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
623 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
624 kClientIPv6Addr);
625 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
626 kClientIPv6Addr2);
627 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
628 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
629 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
630 kClientIPv6Addr);
631 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
632 kClientIPv6Addr2);
633 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
634 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
635 }
636
kwiberg3ec46792016-04-27 07:22:53 -0700637 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
638 std::unique_ptr<rtc::VirtualSocketServer> vss_;
639 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000640 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700641 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000642 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700643 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700644 std::unique_ptr<TestStunServer> stun_server_;
645 TestRelayServer relay_server_;
646 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000647 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700648 std::unique_ptr<BasicPortAllocator> allocator_;
649 std::unique_ptr<PortAllocatorSession> session_;
650 std::vector<PortInterface*> ports_;
651 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000652 bool candidate_allocation_done_;
653};
654
655// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700656TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000657 EXPECT_EQ(&network_manager_, allocator().network_manager());
658 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800659 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700660 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000661 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800662 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
663 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700664 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
665 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
666 EXPECT_TRUE(
667 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
668 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700669 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670}
671
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800672// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700673TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800674 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
675 rtc::ADAPTER_TYPE_ETHERNET);
676 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
677 rtc::ADAPTER_TYPE_WIFI);
678 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
679 rtc::ADAPTER_TYPE_CELLULAR);
680 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
681 rtc::ADAPTER_TYPE_VPN);
682 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
683 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700684 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
685 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
686 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800687 session_->StartGettingPorts();
688 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
689 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700690 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 EXPECT_LT(candidate.address().ip(), 0x12345604U);
692 }
693}
694
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700695TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800696 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
697 rtc::ADAPTER_TYPE_ETHERNET);
698 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
699 rtc::ADAPTER_TYPE_WIFI);
700 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
701 rtc::ADAPTER_TYPE_CELLULAR);
702 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
703 rtc::ADAPTER_TYPE_LOOPBACK |
704 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700705 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
706 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
707 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 session_->StartGettingPorts();
709 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
710 EXPECT_EQ(1U, candidates_.size());
711 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
712}
713
honghaiz60347052016-05-31 18:29:12 -0700714// Test that high cost networks are filtered if the flag
715// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
716TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
717 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
718 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
719 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
720 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
721 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
722 // interface.
723 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
724 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
725 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
726 cricket::PORTALLOCATOR_DISABLE_RELAY |
727 cricket::PORTALLOCATOR_DISABLE_TCP |
728 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
729 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
730 session_->StartGettingPorts();
731 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
732 EXPECT_EQ(1U, candidates_.size());
733 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
734
735 // If both cellular and unknown interfaces are present, only gather on the
736 // unknown interfaces.
737 candidates_.clear();
738 candidate_allocation_done_ = false;
739 RemoveInterface(addr_wifi);
740 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
741 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
742 session_->StartGettingPorts();
743 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
744 EXPECT_EQ(2U, candidates_.size());
745 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
746 addr_unknown2.EqualIPs(candidates_[1].address())) ||
747 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
748 addr_unknown2.EqualIPs(candidates_[0].address())));
749
750 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
751 // Wi-Fi interface.
752 candidates_.clear();
753 candidate_allocation_done_ = false;
754 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
755 session_->StartGettingPorts();
756 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
757 EXPECT_EQ(1U, candidates_.size());
758 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
759}
760
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000761// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700762TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700763 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000764 session_->StartGettingPorts();
765 // Waiting for one second to make sure BasicPortAllocatorSession has not
766 // called OnAllocate multiple times. In old behavior it's called every 250ms.
767 // When there are no network interfaces, each execution of OnAllocate will
768 // result in SignalCandidatesAllocationDone signal.
769 rtc::Thread::Current()->ProcessMessages(1000);
770 EXPECT_TRUE(candidate_allocation_done_);
771 EXPECT_EQ(0U, candidates_.size());
772}
773
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700774// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700775TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
777 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700778 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
779 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
780 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700781 session_->StartGettingPorts();
782 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
783 EXPECT_EQ(1U, candidates_.size());
784}
785
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700787TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000788 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700789 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000790 session_->StartGettingPorts();
791 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
792 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700793 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
794 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
795 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
796 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
797 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
798 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
799 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
800 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000801 EXPECT_TRUE(candidate_allocation_done_);
802}
803
honghaiz8c404fa2015-09-28 07:59:43 -0700804// Test that when the same network interface is brought down and up, the
805// port allocator session will restart a new allocation sequence if
806// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700807TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700808 std::string if_name("test_net0");
809 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700810 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700811 session_->StartGettingPorts();
812 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
813 EXPECT_EQ(4U, ports_.size());
814 EXPECT_TRUE(candidate_allocation_done_);
815 candidate_allocation_done_ = false;
816 candidates_.clear();
817 ports_.clear();
818
819 RemoveInterface(kClientAddr);
820 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
821 EXPECT_EQ(0U, ports_.size());
822 EXPECT_FALSE(candidate_allocation_done_);
823
824 // When the same interfaces are added again, new candidates/ports should be
825 // generated.
826 AddInterface(kClientAddr, if_name);
827 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
828 EXPECT_EQ(4U, ports_.size());
829 EXPECT_TRUE(candidate_allocation_done_);
830}
831
832// Test that when the same network interface is brought down and up, the
833// port allocator session will not restart a new allocation sequence if
834// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700835TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700836 std::string if_name("test_net0");
837 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700838 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700839 session_->StartGettingPorts();
840 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
841 EXPECT_EQ(4U, ports_.size());
842 EXPECT_TRUE(candidate_allocation_done_);
843 session_->StopGettingPorts();
844 candidates_.clear();
845 ports_.clear();
846
847 RemoveInterface(kClientAddr);
848 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
849 EXPECT_EQ(0U, ports_.size());
850
851 // When the same interfaces are added again, new candidates/ports should not
852 // be generated because the session has stopped.
853 AddInterface(kClientAddr, if_name);
854 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
855 EXPECT_EQ(0U, ports_.size());
856 EXPECT_TRUE(candidate_allocation_done_);
857}
858
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000859// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700860TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000861 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700862 allocator_->set_step_delay(kDefaultStepDelay);
863 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864 session_->StartGettingPorts();
865 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
866 EXPECT_EQ(2U, ports_.size());
867 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
868 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700869 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
870 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000871 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700872 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
873 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000874 EXPECT_EQ(4U, ports_.size());
875 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700876 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
877 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000878 EXPECT_EQ(4U, ports_.size());
879 EXPECT_TRUE(candidate_allocation_done_);
880 // If we Stop gathering now, we shouldn't get a second "done" callback.
881 session_->StopGettingPorts();
882}
883
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700884TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000885 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700886 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000887 session_->StartGettingPorts();
888 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
889 EXPECT_TRUE(candidate_allocation_done_);
890 // If we Stop gathering now, we shouldn't get a second "done" callback.
891 session_->StopGettingPorts();
892
893 // All ports should have unset send-buffer sizes.
894 CheckSendBufferSizesOfAllPorts(-1);
895}
896
897// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700898TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000899 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700900 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000901 session_->StartGettingPorts();
902 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
903 EXPECT_EQ(2U, ports_.size());
904 session_->StopGettingPorts();
905 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
906}
907
908// Test that we restrict client ports appropriately when a port range is set.
909// We check the candidates for udp/stun/tcp ports, and the from address
910// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700911TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000912 AddInterface(kClientAddr);
913 // Check that an invalid port range fails.
914 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
915 // Check that a null port range succeeds.
916 EXPECT_TRUE(SetPortRange(0, 0));
917 // Check that a valid port range succeeds.
918 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700919 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 session_->StartGettingPorts();
921 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
922 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -0700923
924 int num_nonrelay_candidates = 0;
925 for (const Candidate& candidate : candidates_) {
926 // Check the port number for the UDP/STUN/TCP port objects.
927 if (candidate.type() != RELAY_PORT_TYPE) {
928 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
929 ++num_nonrelay_candidates;
930 }
931 }
932 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700934 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
935 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 EXPECT_TRUE(candidate_allocation_done_);
937}
938
939// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700940TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700941 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000942 session_->StartGettingPorts();
943 rtc::Thread::Current()->ProcessMessages(100);
944 // Without network adapter, we should not get any candidate.
945 EXPECT_EQ(0U, candidates_.size());
946 EXPECT_TRUE(candidate_allocation_done_);
947}
948
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700949// Test that when enumeration is disabled, we should not have any ports when
950// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700951TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700952 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700953 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700954 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700955 // Expect to see no ports and no candidates.
956 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
957 rtc::IPAddress(), rtc::IPAddress());
958}
959
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800960// Test that even with multiple interfaces, the result should still be a single
961// default private, one STUN and one TURN candidate since we bind to any address
962// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700963TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700964 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000965 AddInterface(kPrivateAddr);
966 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700967 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000968 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800969
970 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
971 // address set and we have no IPv6 STUN server, there should be no IPv6
972 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700973 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
974 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800975
976 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
977 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
978 // TURN/UDP candidates.
979 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800980 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
981 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700982}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000983
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800984// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
985// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700986TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700987 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800988 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700989 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700990 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800991 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
992 // private, STUN, TURN/UDP, and TURN/TCP candidates.
993 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
994 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700995 kTurnUdpExtAddr.ipaddr());
996}
997
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800998// Test that when adapter enumeration is disabled, for endpoints without
999// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001000TEST_F(BasicPortAllocatorTest,
1001 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001002 ResetWithNoServersOrNat();
1003 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1004 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1005 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001006}
1007
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001008// Test that when adapter enumeration is disabled, with
1009// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1010// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001011TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001012 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1013 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001014 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1015 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001016 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1017 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001018 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001019 rtc::IPAddress(), rtc::IPAddress());
1020}
1021
1022// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001023// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1024// a NAT, there is no local candidate. However, this specified default route
1025// (kClientAddr) which was discovered when sending STUN requests, will become
1026// the srflx addresses.
1027TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001028 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001029 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001030 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001031 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001032 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1033 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001034 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1035 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001036 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1037 rtc::IPAddress(), rtc::IPAddress());
1038}
1039
1040// Test that when adapter enumeration is disabled, with
1041// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1042// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001043TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001044 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1045 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001046 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1047 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001048 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1049 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1050 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001051}
1052
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001053// Test that we disable relay over UDP, and only TCP is used when connecting to
1054// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001055TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001056 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001057 AddInterface(kClientAddr);
1058 ResetWithStunServerAndNat(kStunAddr);
1059 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001060 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1061 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1062 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1063 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001064
1065 session_->StartGettingPorts();
1066 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1067
1068 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1069 // TURN/TCP candidates.
1070 EXPECT_EQ(2U, ports_.size());
1071 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001072 Candidate turn_candidate;
1073 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1074 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001075 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001076 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1077 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001078}
1079
Erik Språngefdce692015-06-05 09:41:26 +02001080// Disable for asan, see
1081// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1082#if !defined(ADDRESS_SANITIZER)
1083
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001084// Test that we can get OnCandidatesAllocationDone callback when all the ports
1085// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001086TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001087 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001088 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1089 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1090 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001091 session_->StartGettingPorts();
1092 rtc::Thread::Current()->ProcessMessages(100);
1093 EXPECT_EQ(0U, candidates_.size());
1094 EXPECT_TRUE(candidate_allocation_done_);
1095}
1096
1097// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001098TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001099 AddInterface(kClientAddr);
1100 fss_->set_udp_sockets_enabled(false);
1101 EXPECT_TRUE(CreateSession(1));
1102 session_->StartGettingPorts();
1103 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1104 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001105 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1106 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1107 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1108 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1109 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1110 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001111 EXPECT_TRUE(candidate_allocation_done_);
1112}
1113
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001114#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001115
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001116// Test that we don't crash or malfunction if we can't create UDP sockets or
1117// listen on TCP sockets. We still give out a local TCP address, since
1118// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001119TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001120 AddInterface(kClientAddr);
1121 fss_->set_udp_sockets_enabled(false);
1122 fss_->set_tcp_listen_enabled(false);
1123 EXPECT_TRUE(CreateSession(1));
1124 session_->StartGettingPorts();
1125 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1126 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001127 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1128 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1129 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1130 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1131 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001132 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001133 EXPECT_TRUE(candidate_allocation_done_);
1134}
1135
1136// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001137// TODO(deadbeef): Find a way to exit early here.
1138TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001139 AddInterface(kClientAddr);
1140 fss_->set_tcp_sockets_enabled(false);
1141 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001142 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143 session_->StartGettingPorts();
1144 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001145 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146 // In case of Relay, ports creation will succeed but sockets will fail.
1147 // There is no error reporting from RelayEntry to handle this failure.
1148}
1149
1150// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001151TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1153 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001154 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001155 session_->StartGettingPorts();
1156 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1157 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001158 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1159 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001160 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
1161 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -07001162 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
1163 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001164 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001165 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1166 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1167 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1168 kRelaySslTcpIntAddr);
1169 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -07001170 // Stun Timeout is 9.5sec.
1171 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
1172 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001173}
1174
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001175TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001176 AddInterface(kClientAddr);
1177 AddInterface(kClientAddr2);
1178 // Allocating only host UDP ports. This is done purely for testing
1179 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001180 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1181 PORTALLOCATOR_DISABLE_RELAY);
1182 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001183 session_->StartGettingPorts();
1184 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1185 ASSERT_EQ(2U, candidates_.size());
1186 EXPECT_EQ(2U, ports_.size());
1187 // Candidates priorities should be different.
1188 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1189}
1190
1191// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001192TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001193 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001194 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 session_->StartGettingPorts();
1196 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1197 EXPECT_EQ(4U, ports_.size());
1198 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001199 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001200}
1201
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001202// Test that the allocator session uses the candidate filter it's created with,
1203// rather than the filter of its parent allocator.
1204// The filter of the allocator should only affect the next gathering phase,
1205// according to JSEP, which means the *next* allocator session returned.
1206TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1207 AddInterface(kClientAddr);
1208 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1209 // Set candidate filter *after* creating the session. Should have no effect.
1210 allocator().set_candidate_filter(CF_RELAY);
1211 session_->StartGettingPorts();
1212 // 7 candidates and 4 ports is what we would normally get (see the
1213 // TestGetAllPorts* tests).
1214 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1215 EXPECT_EQ(4U, ports_.size());
1216 EXPECT_TRUE(candidate_allocation_done_);
1217}
1218
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001219// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1220// This test also verifies that when the allocator is only allowed to use
1221// relay (i.e. IceTransportsType is relay), the raddr is an empty
1222// address with the correct family. This is to prevent any local
1223// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001224TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001225 AddInterface(kClientAddr);
1226 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001227 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001228 allocator().set_candidate_filter(CF_RELAY);
1229 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230 session_->StartGettingPorts();
1231 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001232 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1233 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001234
1235 EXPECT_EQ(1U, candidates_.size());
1236 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001237 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1238 EXPECT_EQ(
1239 candidates_[0].related_address(),
1240 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241}
1242
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001243TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001244 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001245 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1246 allocator().set_candidate_filter(CF_HOST);
1247 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001248 session_->StartGettingPorts();
1249 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001250 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1251 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001252 for (const Candidate& candidate : candidates_) {
1253 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001254 }
1255}
1256
1257// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001258TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001259 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001260 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001261
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001262 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1263 allocator().set_candidate_filter(CF_REFLEXIVE);
1264 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001265 session_->StartGettingPorts();
1266 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1267 // Host is behind NAT, no private address will be exposed. Hence only UDP
1268 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001269 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1270 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001271 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1272 EXPECT_EQ(
1273 candidates_[0].related_address(),
1274 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001275}
1276
1277// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001278TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001279 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001280 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1281 allocator().set_candidate_filter(CF_REFLEXIVE);
1282 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001283 session_->StartGettingPorts();
1284 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1285 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001286 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001287 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001288 for (const Candidate& candidate : candidates_) {
1289 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001290 }
1291}
1292
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001293// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001294TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001295 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001296 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001297 session_->StartGettingPorts();
1298 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001299 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1300 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1301 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001302 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001303 for (const Candidate& candidate : candidates_) {
1304 EXPECT_EQ(kIceUfrag0, candidate.username());
1305 EXPECT_EQ(kIcePwd0, candidate.password());
1306 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001307 EXPECT_TRUE(candidate_allocation_done_);
1308}
1309
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001310// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1311// is allocated for udp and stun. Also verify there is only one candidate
1312// (local) if stun candidate is same as local candidate, which will be the case
1313// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001314TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001315 AddInterface(kClientAddr);
1316 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001317 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1318 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001319 session_->StartGettingPorts();
1320 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1321 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001322 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001323 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1324}
1325
1326// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1327// is allocated for udp and stun. In this test we should expect both stun and
1328// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001329TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001330 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001331 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332
1333 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001334 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1335 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001336 session_->StartGettingPorts();
1337 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1338 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001339 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1340 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1341 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001342 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1343 EXPECT_EQ(3U, candidates_.size());
1344}
1345
deadbeefc5d0d952015-07-16 10:22:21 -07001346// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001347TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001348 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001349 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001350 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001351
1352 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1353
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001354 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001355 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001356 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1357 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001358
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001359 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360 session_->StartGettingPorts();
1361
1362 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1363 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001364 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1365 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1366 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1367 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1368 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001369 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1370 EXPECT_EQ(3U, candidates_.size());
1371}
1372
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001373// Test that if prune_turn_ports is set, TCP TURN port will not be used
1374// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1375TEST_F(BasicPortAllocatorTest,
1376 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1377 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1378 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1379 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001380
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001381 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001382}
1383
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001384// Test that if prune_turn_ports is set, TCP TURN port will not be used
1385// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1386TEST_F(BasicPortAllocatorTest,
1387 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1388 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1389 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1390 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001391
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001392 TestUdpTurnPortPrunesTcpTurnPort();
1393}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001394
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001395// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1396// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1397TEST_F(BasicPortAllocatorTest,
1398 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1399 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1400 // first.
1401 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1402 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001403
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001404 TestIPv6TurnPortPrunesIPv4TurnPort();
1405}
1406
1407// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1408// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1409TEST_F(BasicPortAllocatorTest,
1410 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1411 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1412 // first.
1413 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1414 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1415
1416 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001417}
1418
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001419// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001420// will has its own set of TurnPorts based on their priorities, in the default
1421// case where no transit delay is set.
1422TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1423 TestEachInterfaceHasItsOwnTurnPorts();
1424}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001425
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001426// Tests that if prune_turn_ports is set, each network interface
1427// will has its own set of TurnPorts based on their priorities, given that
1428// IPv4/TCP TURN port becomes ready first.
1429TEST_F(BasicPortAllocatorTest,
1430 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1431 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1432 // becomes ready last.
1433 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1434 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1435 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1436 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001437
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001438 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001439}
1440
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441// Testing DNS resolve for the TURN server, this will test AllocationSequence
1442// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001443TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001445 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001446 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001447 allocator_.reset(new BasicPortAllocator(&network_manager_));
1448 RelayServerConfig turn_server(RELAY_TURN);
1449 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001450 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001451 turn_server.ports.push_back(
1452 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001453 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001454
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001455 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001456 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001457 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1458 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001459
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001460 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001461 session_->StartGettingPorts();
1462
1463 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1464}
1465
1466// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1467// is allocated for udp/stun/turn. In this test we should expect all local,
1468// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001469TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001471 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001472
1473 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1474
1475 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001476 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1477 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001478
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001479 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001480 session_->StartGettingPorts();
1481
1482 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1483 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001484 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1485 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1486 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1487 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1488 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001489 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1490 EXPECT_EQ(3U, candidates_.size());
1491 // Local port will be created first and then TURN port.
1492 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1493 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1494}
1495
1496// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1497// server is also used as the STUN server, we should get 'local', 'stun', and
1498// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001499TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001500 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001501 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001502 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001503 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1504
1505 // Must set the step delay to 0 to make sure the relay allocation phase is
1506 // started before the STUN candidates are obtained, so that the STUN binding
1507 // response is processed when both StunPort and TurnPort exist to reproduce
1508 // webrtc issue 3537.
1509 allocator_->set_step_delay(0);
1510 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001511 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1512 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001513
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001514 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001515 session_->StartGettingPorts();
1516
1517 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001518 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1519 Candidate stun_candidate;
1520 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1521 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1522 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1523 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1524 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001525
1526 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1527 EXPECT_EQ(3U, candidates_.size());
1528 // Local port will be created first and then TURN port.
1529 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1530 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1531}
1532
deadbeefc5d0d952015-07-16 10:22:21 -07001533// Test that when only a TCP TURN server is available, we do NOT use it as
1534// a UDP STUN server, as this could leak our IP address. Thus we should only
1535// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001536TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001537 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001538 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001539 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001540 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1541
1542 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001543 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1544 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001545
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001546 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001547 session_->StartGettingPorts();
1548
1549 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1550 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001551 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1552 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1553 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001554 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1555 EXPECT_EQ(2U, candidates_.size());
1556 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1557 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1558}
1559
1560// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1561// TURN server is used as the STUN server and we get 'local', 'stun', and
1562// 'relay' candidates.
1563// TODO(deadbeef): Remove this test when support for non-shared socket mode
1564// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001565TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001566 AddInterface(kClientAddr);
1567 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001568 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001569 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1570
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001571 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001572
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001573 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001574 session_->StartGettingPorts();
1575
1576 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1577 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001578 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1579 Candidate stun_candidate;
1580 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1581 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1582 Candidate turn_candidate;
1583 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1584 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1585 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001586 // Not using shared socket, so the STUN request's server reflexive address
1587 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001588 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001589
1590 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1591 EXPECT_EQ(3U, candidates_.size());
1592 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1593 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1594 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1595}
1596
1597// Test that even when both a STUN and TURN server are configured, the TURN
1598// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001599TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001600 AddInterface(kClientAddr);
1601 // Configure with STUN server but destroy it, so we can ensure that it's
1602 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001603 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001604 stun_server_.reset();
1605 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1606
1607 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001608 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1609 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001610
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001611 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001612 session_->StartGettingPorts();
1613
1614 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001615 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1616 Candidate stun_candidate;
1617 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1618 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1619 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1620 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1621 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001622
1623 // Don't bother waiting for STUN timeout, since we already verified
1624 // that we got a STUN candidate from the TURN server.
1625}
1626
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001627// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1628// and fail to generate STUN candidate, local UDP candidate is generated
1629// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001630TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001631 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1632 PORTALLOCATOR_DISABLE_TCP |
1633 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001634 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1635 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001636 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001637 session_->StartGettingPorts();
1638 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1639 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001640 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001641 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1642 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001643 EXPECT_EQ(1U, candidates_.size());
1644}
1645
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001646// Test that when the NetworkManager doesn't have permission to enumerate
1647// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1648// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001649TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001650 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1651 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001652 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001653 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001654 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1655 PORTALLOCATOR_DISABLE_TCP |
1656 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1657 EXPECT_EQ(0U,
1658 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1659 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1660 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001661 session_->StartGettingPorts();
1662 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001663 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001664 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001665 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001666}
1667
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001669TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001670 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1671 PORTALLOCATOR_ENABLE_IPV6 |
1672 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001673 AddInterface(kClientIPv6Addr);
1674 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001675 allocator_->set_step_delay(kMinimumStepDelay);
1676 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001677 session_->StartGettingPorts();
1678 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1679 EXPECT_EQ(4U, candidates_.size());
1680 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001681 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1682 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1683 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1684 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001685 EXPECT_EQ(4U, candidates_.size());
1686}
honghaiz98db68f2015-09-29 07:58:17 -07001687
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001688TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001689 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001690 allocator_->set_step_delay(kDefaultStepDelay);
1691 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001692 session_->StartGettingPorts();
1693 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1694 EXPECT_EQ(2U, ports_.size());
1695 session_->StopGettingPorts();
1696 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1697
1698 // After stopping getting ports, adding a new interface will not start
1699 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001700 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001701 candidates_.clear();
1702 ports_.clear();
1703 candidate_allocation_done_ = false;
1704 network_manager_.AddInterface(kClientAddr2);
1705 rtc::Thread::Current()->ProcessMessages(1000);
1706 EXPECT_EQ(0U, candidates_.size());
1707 EXPECT_EQ(0U, ports_.size());
1708}
1709
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001710TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001711 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001712 allocator_->set_step_delay(kDefaultStepDelay);
1713 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001714 session_->StartGettingPorts();
1715 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1716 EXPECT_EQ(2U, ports_.size());
1717 session_->ClearGettingPorts();
deadbeefb60a8192016-08-24 15:15:00 -07001718 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
honghaiz98db68f2015-09-29 07:58:17 -07001719
1720 // After clearing getting ports, adding a new interface will start getting
1721 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001722 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001723 candidates_.clear();
1724 ports_.clear();
1725 candidate_allocation_done_ = false;
1726 network_manager_.AddInterface(kClientAddr2);
1727 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1728 EXPECT_EQ(2U, ports_.size());
deadbeefb60a8192016-08-24 15:15:00 -07001729 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
honghaiz98db68f2015-09-29 07:58:17 -07001730}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001731
1732// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1733// a pooled session is taken out of the pool.
1734TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1735 AddInterface(kClientAddr);
1736 int pool_size = 1;
1737 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001738 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001739 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001740 ASSERT_NE(nullptr, peeked_session);
1741 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1742 kDefaultAllocationTimeout);
1743 // Expect that when TakePooledSession is called,
1744 // UpdateTransportInformationInternal will be called and the
1745 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1746 // candidates.
1747 session_ =
1748 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1749 ASSERT_NE(nullptr, session_.get());
1750 auto ready_ports = session_->ReadyPorts();
1751 auto candidates = session_->ReadyCandidates();
1752 EXPECT_FALSE(ready_ports.empty());
1753 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001754 for (const PortInterface* port_interface : ready_ports) {
1755 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001756 EXPECT_EQ(kContentName, port->content_name());
1757 EXPECT_EQ(1, port->component());
1758 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1759 EXPECT_EQ(kIcePwd0, port->password());
1760 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001761 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001762 EXPECT_EQ(1, candidate.component());
1763 EXPECT_EQ(kIceUfrag0, candidate.username());
1764 EXPECT_EQ(kIcePwd0, candidate.password());
1765 }
1766}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001767
1768// Test that a new candidate filter takes effect even on already-gathered
1769// candidates.
1770TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1771 AddInterface(kClientAddr);
1772 int pool_size = 1;
1773 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001774 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001775 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1776 ASSERT_NE(nullptr, peeked_session);
1777 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1778 kDefaultAllocationTimeout);
1779 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1780 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1781 allocator_->set_candidate_filter(CF_RELAY);
1782 // Assume that when TakePooledSession is called, the candidate filter will be
1783 // applied to the pooled session. This is tested by PortAllocatorTest.
1784 session_ =
1785 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1786 ASSERT_NE(nullptr, session_.get());
1787 auto candidates = session_->ReadyCandidates();
1788 auto ports = session_->ReadyPorts();
1789 // Sanity check that the number of candidates and ports decreased.
1790 EXPECT_GT(initial_candidates_size, candidates.size());
1791 EXPECT_GT(initial_ports_size, ports.size());
1792 for (const PortInterface* port : ports) {
1793 // Expect only relay ports.
1794 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1795 }
1796 for (const Candidate& candidate : candidates) {
1797 // Expect only relay candidates now that the filter is applied.
1798 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1799 // Expect that the raddr is emptied due to the CF_RELAY filter.
1800 EXPECT_EQ(candidate.related_address(),
1801 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1802 }
1803}
1804
1805} // namespace cricket