blob: 9f91aa0857bee39608387646aff002576fefdb35 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/basicpacketsocketfactory.h"
kjellanderf4752772016-03-02 05:42:30 -080015#include "webrtc/p2p/base/p2pconstants.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/testrelayserver.h"
18#include "webrtc/p2p/base/teststunserver.h"
19#include "webrtc/p2p/base/testturnserver.h"
20#include "webrtc/p2p/client/basicportallocator.h"
21#include "webrtc/p2p/client/httpportallocator.h"
22#include "webrtc/base/fakenetwork.h"
23#include "webrtc/base/firewallsocketserver.h"
24#include "webrtc/base/gunit.h"
25#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070026#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/natserver.h"
29#include "webrtc/base/natsocketfactory.h"
30#include "webrtc/base/network.h"
31#include "webrtc/base/physicalsocketserver.h"
32#include "webrtc/base/socketaddress.h"
33#include "webrtc/base/ssladapter.h"
34#include "webrtc/base/thread.h"
35#include "webrtc/base/virtualsocketserver.h"
36
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080037using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038using rtc::SocketAddress;
39using rtc::Thread;
40
41static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070042static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070043static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000045static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070046static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
47 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070048static const SocketAddress kClientIPv6Addr2(
49 "2401:fa00:4:2000:be30:5bff:fee5:c3",
50 0);
deadbeefc5d0d952015-07-16 10:22:21 -070051static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
52static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
54static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
55static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
56static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
57static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
58static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
59static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
60static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
61static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070062static const SocketAddress kTurnUdpIntIPv6Addr(
63 "2402:fb00:4:1000:be30:5bff:fee5:c3",
64 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070066static const SocketAddress kTurnTcpIntIPv6Addr(
67 "2402:fb00:4:2000:be30:5bff:fee5:c3",
68 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
70
71// Minimum and maximum port for port range tests.
72static const int kMinPort = 10000;
73static const int kMaxPort = 10099;
74
75// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070076static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077// Based on ICE_PWD_LENGTH
78static const char kIcePwd0[] = "TESTICEPWD00000000000000";
79
80static const char kContentName[] = "test content";
81
82static const int kDefaultAllocationTimeout = 1000;
83static const char kTurnUsername[] = "test";
84static const char kTurnPassword[] = "test";
85
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070086// STUN timeout (with all retries) is 9500ms.
87// Add some margin of error for slow bots.
88// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
89// fix flaky tests.
90static const int kStunTimeoutMs = 15000;
91
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092namespace cricket {
93
94// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070095std::ostream& operator<<(std::ostream& os,
96 const std::vector<Candidate>& candidates) {
97 os << '[';
98 bool first = true;
99 for (const Candidate& c : candidates) {
100 if (!first) {
101 os << ", ";
102 }
103 os << c.ToString();
104 first = false;
105 };
106 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 return os;
108}
109
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700110class BasicPortAllocatorTest : public testing::Test,
111 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700113 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 : pss_(new rtc::PhysicalSocketServer),
115 vss_(new rtc::VirtualSocketServer(pss_.get())),
116 fss_(new rtc::FirewallSocketServer(vss_.get())),
117 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700118 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700119 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700120 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700121 relay_server_(Thread::Current(),
122 kRelayUdpIntAddr,
123 kRelayUdpExtAddr,
124 kRelayTcpIntAddr,
125 kRelayTcpExtAddr,
126 kRelaySslTcpIntAddr,
127 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
129 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700130 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131 stun_servers.insert(kStunAddr);
132 // Passing the addresses of GTURN servers will enable GTURN in
133 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700134 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
135 kRelayUdpIntAddr, kRelayTcpIntAddr,
136 kRelaySslTcpIntAddr));
137 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 }
139
140 void AddInterface(const SocketAddress& addr) {
141 network_manager_.AddInterface(addr);
142 }
honghaiz8c404fa2015-09-28 07:59:43 -0700143 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
144 network_manager_.AddInterface(addr, if_name);
145 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800146 void AddInterface(const SocketAddress& addr,
147 const std::string& if_name,
148 rtc::AdapterType type) {
149 network_manager_.AddInterface(addr, if_name, type);
150 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800151 // The default route is the public address that STUN server will observe when
152 // the endpoint is sitting on the public internet and the local port is bound
153 // to the "any" address. This may be different from the default local address
154 // which the endpoint observes. This can occur if the route to the public
155 // endpoint like 8.8.8.8 (specified as the default local address) is
156 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700157 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
158 AddInterface(addr);
159 // When a binding comes from the any address, the |addr| will be used as the
160 // srflx address.
161 vss_->SetDefaultRoute(addr.ipaddr());
162 }
honghaiz8c404fa2015-09-28 07:59:43 -0700163 void RemoveInterface(const SocketAddress& addr) {
164 network_manager_.RemoveInterface(addr);
165 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 bool SetPortRange(int min_port, int max_port) {
167 return allocator_->SetPortRange(min_port, max_port);
168 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700169 // Endpoint is on the public network. No STUN or TURN.
170 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700171 allocator_.reset(new BasicPortAllocator(&network_manager_));
172 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700173 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700174 // Endpoint is behind a NAT, with STUN specified.
175 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
176 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700177 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700178 // Endpoint is on the public network, with STUN specified.
179 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
180 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700182 // Endpoint is on the public network, with TURN specified.
183 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
184 const rtc::SocketAddress& tcp_turn) {
185 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 AddTurnServers(udp_turn, tcp_turn);
187 }
188
189 void AddTurnServers(const rtc::SocketAddress& udp_turn,
190 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700191 RelayServerConfig turn_server(RELAY_TURN);
192 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800193 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194
195 if (!udp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700196 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197 }
198 if (!tcp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700199 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 }
deadbeef653b8e02015-11-11 12:55:10 -0800201 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 }
203
204 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700205 session_ = CreateSession("session", component);
206 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700208 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000209 return true;
210 }
211
212 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700213 session_ = CreateSession("session", content_name, component);
214 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700216 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217 return true;
218 }
219
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700220 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
221 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 return CreateSession(sid, kContentName, component);
223 }
224
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700226 const std::string& sid,
227 const std::string& content_name,
228 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
230 }
231
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700232 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700233 const std::string& sid,
234 const std::string& content_name,
235 int component,
236 const std::string& ice_ufrag,
237 const std::string& ice_pwd) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700238 std::unique_ptr<PortAllocatorSession> session = allocator_->CreateSession(
239 sid, content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700241 &BasicPortAllocatorTest::OnPortReady);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700242 session->SignalPortsPruned.connect(this,
243 &BasicPortAllocatorTest::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 session->SignalCandidatesReady.connect(
245 this, &BasicPortAllocatorTest::OnCandidatesReady);
246 session->SignalCandidatesAllocationDone.connect(
247 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000248 return session;
249 }
250
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700251 // Return true if the addresses are the same, or the port is 0 in |pattern|
252 // (acting as a wildcard) and the IPs are the same.
253 // Even with a wildcard port, the port of the address should be nonzero if
254 // the IP is nonzero.
255 static bool AddressMatch(const SocketAddress& address,
256 const SocketAddress& pattern) {
257 return address.ipaddr() == pattern.ipaddr() &&
258 ((pattern.port() == 0 &&
259 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
260 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000261 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700262
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700263 // Returns the number of ports that have matching type, protocol and
264 // address.
265 static int CountPorts(const std::vector<PortInterface*>& ports,
266 const std::string& type,
267 ProtocolType protocol,
268 const SocketAddress& client_addr) {
269 return std::count_if(
270 ports.begin(), ports.end(),
271 [type, protocol, client_addr](PortInterface* port) {
272 return port->Type() == type && port->GetProtocol() == protocol &&
273 port->Network()->GetBestIP() == client_addr.ipaddr();
274 });
275 }
276
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700277 // Find a candidate and return it.
278 static bool FindCandidate(const std::vector<Candidate>& candidates,
279 const std::string& type,
280 const std::string& proto,
281 const SocketAddress& addr,
282 Candidate* found) {
283 auto it = std::find_if(candidates.begin(), candidates.end(),
284 [type, proto, addr](const Candidate& c) {
285 return c.type() == type && c.protocol() == proto &&
286 AddressMatch(c.address(), addr);
287 });
288 if (it != candidates.end() && found) {
289 *found = *it;
290 }
291 return it != candidates.end();
292 }
293
294 // Convenience method to call FindCandidate with no return.
295 static bool HasCandidate(const std::vector<Candidate>& candidates,
296 const std::string& type,
297 const std::string& proto,
298 const SocketAddress& addr) {
299 return FindCandidate(candidates, type, proto, addr, nullptr);
300 }
301
302 // Version of HasCandidate that also takes a related address.
303 static bool HasCandidateWithRelatedAddr(
304 const std::vector<Candidate>& candidates,
305 const std::string& type,
306 const std::string& proto,
307 const SocketAddress& addr,
308 const SocketAddress& related_addr) {
309 auto it =
310 std::find_if(candidates.begin(), candidates.end(),
311 [type, proto, addr, related_addr](const Candidate& c) {
312 return c.type() == type && c.protocol() == proto &&
313 AddressMatch(c.address(), addr) &&
314 AddressMatch(c.related_address(), related_addr);
315 });
316 return it != candidates.end();
317 }
318
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000319 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700320 int min_port,
321 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000322 return (addr.port() >= min_port && addr.port() <= max_port);
323 }
324
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700325 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000326 // We should only get this callback once, except in the mux test where
327 // we have multiple port allocation sessions.
328 if (session == session_.get()) {
329 ASSERT_FALSE(candidate_allocation_done_);
330 candidate_allocation_done_ = true;
331 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700332 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000333 }
334
335 // Check if all ports allocated have send-buffer size |expected|. If
336 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
337 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700338 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000339 for (it = ports_.begin(); it < ports_.end(); ++it) {
340 int send_buffer_size;
341 if (expected == -1) {
342 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700343 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700345 EXPECT_EQ(0,
346 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000347 ASSERT_EQ(expected, send_buffer_size);
348 }
349 }
350 }
351
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700352 // This function starts the port/address gathering and check the existence of
353 // candidates as specified. When |expect_stun_candidate| is true,
354 // |stun_candidate_addr| carries the expected reflective address, which is
355 // also the related address for TURN candidate if it is expected. Otherwise,
356 // it should be ignore.
357 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200358 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700359 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700360 const rtc::IPAddress& stun_candidate_addr,
361 const rtc::IPAddress& relay_candidate_udp_transport_addr,
362 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800363 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
364 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700365 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700366 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700367 }
368 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700369 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
370 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700371 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000372 session_->StartGettingPorts();
373 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
374
Peter Boström0c4e06b2015-10-07 12:23:21 +0200375 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700376 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700377 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800378 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700379 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700380 }
381 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700382 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800383 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700384 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800385 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700386 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
387 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700388 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700389 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700390 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700391 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
392 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
393 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700394 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700395 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700396 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700397 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
398 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
399 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700400 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700401 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000402
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700403 EXPECT_EQ(total_candidates, candidates_.size());
404 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000405 }
406
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000407 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700408 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700410 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
412 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700413 // Make sure the new port is added to ReadyPorts.
414 auto ready_ports = ses->ReadyPorts();
415 EXPECT_NE(ready_ports.end(),
416 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000417 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700418 void OnPortsPruned(PortAllocatorSession* ses,
419 const std::vector<PortInterface*>& ports_pruned) {
420 LOG(LS_INFO) << "Number of ports pruned: " << ports_pruned.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700421 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700422 auto new_end = ports_.end();
423 for (PortInterface* port : ports_pruned) {
424 new_end = std::remove(ports_.begin(), new_end, port);
425 // Make sure the pruned port is not in ReadyPorts.
426 EXPECT_EQ(ready_ports.end(),
427 std::find(ready_ports.begin(), ready_ports.end(), port));
428 }
429 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700430 }
431
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700432 void OnCandidatesReady(PortAllocatorSession* ses,
433 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700434 for (const Candidate& candidate : candidates) {
435 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
436 // Sanity check that the ICE component is set.
437 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
438 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000439 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700440 // Make sure the new candidates are added to Candidates.
441 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700442 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700443 EXPECT_NE(
444 ses_candidates.end(),
445 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
446 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000447 }
448
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700449 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800450 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700451 RelayServerConfig server_config = allocator_->turn_servers()[i];
452 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000453 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700454 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000455 if (proto_addr.address == relay_port->address &&
456 proto_addr.proto == relay_port->proto)
457 return true;
458 }
459 }
460 return false;
461 }
462
Guo-wei Shieh11477022015-08-15 09:28:41 -0700463 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
464 bool with_nat) {
465 if (with_nat) {
466 nat_server_.reset(new rtc::NATServer(
467 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
468 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
469 } else {
470 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
471 }
472
473 ServerAddresses stun_servers;
474 if (!stun_server.IsNil()) {
475 stun_servers.insert(stun_server);
476 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700477 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700478 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700479 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700480 }
481
kwiberg3ec46792016-04-27 07:22:53 -0700482 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
483 std::unique_ptr<rtc::VirtualSocketServer> vss_;
484 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000485 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700486 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000487 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700488 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700489 std::unique_ptr<TestStunServer> stun_server_;
490 TestRelayServer relay_server_;
491 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000492 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700493 std::unique_ptr<BasicPortAllocator> allocator_;
494 std::unique_ptr<PortAllocatorSession> session_;
495 std::vector<PortInterface*> ports_;
496 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000497 bool candidate_allocation_done_;
498};
499
500// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700501TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000502 EXPECT_EQ(&network_manager_, allocator().network_manager());
503 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800504 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700505 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000506 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800507 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
508 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700509 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
510 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
511 EXPECT_TRUE(
512 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
513 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700514 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000515}
516
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800517// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700518TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800519 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
520 rtc::ADAPTER_TYPE_ETHERNET);
521 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
522 rtc::ADAPTER_TYPE_WIFI);
523 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
524 rtc::ADAPTER_TYPE_CELLULAR);
525 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
526 rtc::ADAPTER_TYPE_VPN);
527 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
528 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700529 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
530 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
531 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800532 session_->StartGettingPorts();
533 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
534 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700535 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800536 EXPECT_LT(candidate.address().ip(), 0x12345604U);
537 }
538}
539
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700540TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800541 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
542 rtc::ADAPTER_TYPE_ETHERNET);
543 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
544 rtc::ADAPTER_TYPE_WIFI);
545 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
546 rtc::ADAPTER_TYPE_CELLULAR);
547 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
548 rtc::ADAPTER_TYPE_LOOPBACK |
549 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700550 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
551 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
552 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800553 session_->StartGettingPorts();
554 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
555 EXPECT_EQ(1U, candidates_.size());
556 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
557}
558
honghaiz60347052016-05-31 18:29:12 -0700559// Test that high cost networks are filtered if the flag
560// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
561TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
562 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
563 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
564 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
565 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
566 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
567 // interface.
568 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
569 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
570 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
571 cricket::PORTALLOCATOR_DISABLE_RELAY |
572 cricket::PORTALLOCATOR_DISABLE_TCP |
573 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
574 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
575 session_->StartGettingPorts();
576 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
577 EXPECT_EQ(1U, candidates_.size());
578 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
579
580 // If both cellular and unknown interfaces are present, only gather on the
581 // unknown interfaces.
582 candidates_.clear();
583 candidate_allocation_done_ = false;
584 RemoveInterface(addr_wifi);
585 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
586 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
587 session_->StartGettingPorts();
588 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
589 EXPECT_EQ(2U, candidates_.size());
590 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
591 addr_unknown2.EqualIPs(candidates_[1].address())) ||
592 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
593 addr_unknown2.EqualIPs(candidates_[0].address())));
594
595 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
596 // Wi-Fi interface.
597 candidates_.clear();
598 candidate_allocation_done_ = false;
599 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
600 session_->StartGettingPorts();
601 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
602 EXPECT_EQ(1U, candidates_.size());
603 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
604}
605
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000606// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700607TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700608 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000609 session_->StartGettingPorts();
610 // Waiting for one second to make sure BasicPortAllocatorSession has not
611 // called OnAllocate multiple times. In old behavior it's called every 250ms.
612 // When there are no network interfaces, each execution of OnAllocate will
613 // result in SignalCandidatesAllocationDone signal.
614 rtc::Thread::Current()->ProcessMessages(1000);
615 EXPECT_TRUE(candidate_allocation_done_);
616 EXPECT_EQ(0U, candidates_.size());
617}
618
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700619// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700620TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800621 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
622 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700623 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
624 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
625 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700626 session_->StartGettingPorts();
627 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
628 EXPECT_EQ(1U, candidates_.size());
629}
630
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000631// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700632TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000633 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700634 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000635 session_->StartGettingPorts();
636 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
637 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700638 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
639 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
640 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
641 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
642 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
643 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
644 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
645 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000646 EXPECT_TRUE(candidate_allocation_done_);
647}
648
honghaiz8c404fa2015-09-28 07:59:43 -0700649// Test that when the same network interface is brought down and up, the
650// port allocator session will restart a new allocation sequence if
651// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700652TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700653 std::string if_name("test_net0");
654 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700655 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700656 session_->StartGettingPorts();
657 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
658 EXPECT_EQ(4U, ports_.size());
659 EXPECT_TRUE(candidate_allocation_done_);
660 candidate_allocation_done_ = false;
661 candidates_.clear();
662 ports_.clear();
663
664 RemoveInterface(kClientAddr);
665 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
666 EXPECT_EQ(0U, ports_.size());
667 EXPECT_FALSE(candidate_allocation_done_);
668
669 // When the same interfaces are added again, new candidates/ports should be
670 // generated.
671 AddInterface(kClientAddr, if_name);
672 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
673 EXPECT_EQ(4U, ports_.size());
674 EXPECT_TRUE(candidate_allocation_done_);
675}
676
677// Test that when the same network interface is brought down and up, the
678// port allocator session will not restart a new allocation sequence if
679// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700680TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700681 std::string if_name("test_net0");
682 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700683 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700684 session_->StartGettingPorts();
685 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
686 EXPECT_EQ(4U, ports_.size());
687 EXPECT_TRUE(candidate_allocation_done_);
688 session_->StopGettingPorts();
689 candidates_.clear();
690 ports_.clear();
691
692 RemoveInterface(kClientAddr);
693 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
694 EXPECT_EQ(0U, ports_.size());
695
696 // When the same interfaces are added again, new candidates/ports should not
697 // be generated because the session has stopped.
698 AddInterface(kClientAddr, if_name);
699 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
700 EXPECT_EQ(0U, ports_.size());
701 EXPECT_TRUE(candidate_allocation_done_);
702}
703
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000704// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700705TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000706 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700707 allocator_->set_step_delay(kDefaultStepDelay);
708 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000709 session_->StartGettingPorts();
710 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
711 EXPECT_EQ(2U, ports_.size());
712 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
713 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700714 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
715 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000716 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700717 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
718 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000719 EXPECT_EQ(4U, ports_.size());
720 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700721 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
722 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000723 EXPECT_EQ(4U, ports_.size());
724 EXPECT_TRUE(candidate_allocation_done_);
725 // If we Stop gathering now, we shouldn't get a second "done" callback.
726 session_->StopGettingPorts();
727}
728
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700729TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000730 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700731 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000732 session_->StartGettingPorts();
733 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
734 EXPECT_TRUE(candidate_allocation_done_);
735 // If we Stop gathering now, we shouldn't get a second "done" callback.
736 session_->StopGettingPorts();
737
738 // All ports should have unset send-buffer sizes.
739 CheckSendBufferSizesOfAllPorts(-1);
740}
741
742// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700743TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000744 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700745 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000746 session_->StartGettingPorts();
747 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
748 EXPECT_EQ(2U, ports_.size());
749 session_->StopGettingPorts();
750 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
751}
752
753// Test that we restrict client ports appropriately when a port range is set.
754// We check the candidates for udp/stun/tcp ports, and the from address
755// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700756TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000757 AddInterface(kClientAddr);
758 // Check that an invalid port range fails.
759 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
760 // Check that a null port range succeeds.
761 EXPECT_TRUE(SetPortRange(0, 0));
762 // Check that a valid port range succeeds.
763 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700764 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000765 session_->StartGettingPorts();
766 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
767 EXPECT_EQ(4U, ports_.size());
768 // Check the port number for the UDP port object.
769 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
770 // Check the port number for the STUN port object.
771 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
772 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700773 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
774 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000775 // Check the port number for the TCP port object.
776 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
777 EXPECT_TRUE(candidate_allocation_done_);
778}
779
780// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700781TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700782 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000783 session_->StartGettingPorts();
784 rtc::Thread::Current()->ProcessMessages(100);
785 // Without network adapter, we should not get any candidate.
786 EXPECT_EQ(0U, candidates_.size());
787 EXPECT_TRUE(candidate_allocation_done_);
788}
789
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700790// Test that when enumeration is disabled, we should not have any ports when
791// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700792TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700793 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700794 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700795 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700796 // Expect to see no ports and no candidates.
797 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
798 rtc::IPAddress(), rtc::IPAddress());
799}
800
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800801// Test that even with multiple interfaces, the result should still be a single
802// default private, one STUN and one TURN candidate since we bind to any address
803// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700804TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700805 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000806 AddInterface(kPrivateAddr);
807 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700808 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000809 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800810
811 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
812 // address set and we have no IPv6 STUN server, there should be no IPv6
813 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700814 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
815 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800816
817 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
818 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
819 // TURN/UDP candidates.
820 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800821 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
822 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700823}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000824
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800825// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
826// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700827TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700828 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800829 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700830 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700831 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800832 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
833 // private, STUN, TURN/UDP, and TURN/TCP candidates.
834 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
835 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700836 kTurnUdpExtAddr.ipaddr());
837}
838
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800839// Test that when adapter enumeration is disabled, for endpoints without
840// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700841TEST_F(BasicPortAllocatorTest,
842 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800843 ResetWithNoServersOrNat();
844 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
845 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
846 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700847}
848
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800849// Test that when adapter enumeration is disabled, with
850// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
851// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700852TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800853 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
854 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700855 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
856 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800857 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
858 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700859 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700860 rtc::IPAddress(), rtc::IPAddress());
861}
862
863// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800864// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
865// a NAT, there is no local candidate. However, this specified default route
866// (kClientAddr) which was discovered when sending STUN requests, will become
867// the srflx addresses.
868TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700869 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800870 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700871 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800872 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700873 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
874 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700875 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
876 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800877 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
878 rtc::IPAddress(), rtc::IPAddress());
879}
880
881// Test that when adapter enumeration is disabled, with
882// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
883// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700884TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800885 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
886 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700887 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
888 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800889 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
890 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
891 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000892}
893
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700894// Test that we disable relay over UDP, and only TCP is used when connecting to
895// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700896TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700897 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700898 AddInterface(kClientAddr);
899 ResetWithStunServerAndNat(kStunAddr);
900 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700901 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
902 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
903 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
904 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700905
906 session_->StartGettingPorts();
907 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
908
909 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
910 // TURN/TCP candidates.
911 EXPECT_EQ(2U, ports_.size());
912 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700913 Candidate turn_candidate;
914 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
915 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700916 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700917 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
918 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700919}
920
Erik Språngefdce692015-06-05 09:41:26 +0200921// Disable for asan, see
922// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
923#if !defined(ADDRESS_SANITIZER)
924
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000925// Test that we can get OnCandidatesAllocationDone callback when all the ports
926// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700927TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000928 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700929 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
930 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
931 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000932 session_->StartGettingPorts();
933 rtc::Thread::Current()->ProcessMessages(100);
934 EXPECT_EQ(0U, candidates_.size());
935 EXPECT_TRUE(candidate_allocation_done_);
936}
937
938// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700939TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940 AddInterface(kClientAddr);
941 fss_->set_udp_sockets_enabled(false);
942 EXPECT_TRUE(CreateSession(1));
943 session_->StartGettingPorts();
944 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
945 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700946 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
947 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
948 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
949 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
950 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
951 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000952 EXPECT_TRUE(candidate_allocation_done_);
953}
954
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700955#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200956
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000957// Test that we don't crash or malfunction if we can't create UDP sockets or
958// listen on TCP sockets. We still give out a local TCP address, since
959// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700960TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000961 AddInterface(kClientAddr);
962 fss_->set_udp_sockets_enabled(false);
963 fss_->set_tcp_listen_enabled(false);
964 EXPECT_TRUE(CreateSession(1));
965 session_->StartGettingPorts();
966 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
967 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700968 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
969 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
970 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
971 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
972 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700973 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000974 EXPECT_TRUE(candidate_allocation_done_);
975}
976
977// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700978// TODO(deadbeef): Find a way to exit early here.
979TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 AddInterface(kClientAddr);
981 fss_->set_tcp_sockets_enabled(false);
982 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700983 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000984 session_->StartGettingPorts();
985 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700986 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000987 // In case of Relay, ports creation will succeed but sockets will fail.
988 // There is no error reporting from RelayEntry to handle this failure.
989}
990
991// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700992TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000993 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
994 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700995 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000996 session_->StartGettingPorts();
997 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
998 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700999 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1000 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001001 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
1002 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -07001003 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
1004 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001005 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001006 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1007 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1008 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1009 kRelaySslTcpIntAddr);
1010 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -07001011 // Stun Timeout is 9.5sec.
1012 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
1013 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001014}
1015
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001016TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001017 AddInterface(kClientAddr);
1018 AddInterface(kClientAddr2);
1019 // Allocating only host UDP ports. This is done purely for testing
1020 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001021 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1022 PORTALLOCATOR_DISABLE_RELAY);
1023 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001024 session_->StartGettingPorts();
1025 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1026 ASSERT_EQ(2U, candidates_.size());
1027 EXPECT_EQ(2U, ports_.size());
1028 // Candidates priorities should be different.
1029 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1030}
1031
1032// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001033TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001034 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001035 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001036 session_->StartGettingPorts();
1037 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1038 EXPECT_EQ(4U, ports_.size());
1039 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001040 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001041}
1042
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001043// Test that the allocator session uses the candidate filter it's created with,
1044// rather than the filter of its parent allocator.
1045// The filter of the allocator should only affect the next gathering phase,
1046// according to JSEP, which means the *next* allocator session returned.
1047TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1048 AddInterface(kClientAddr);
1049 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1050 // Set candidate filter *after* creating the session. Should have no effect.
1051 allocator().set_candidate_filter(CF_RELAY);
1052 session_->StartGettingPorts();
1053 // 7 candidates and 4 ports is what we would normally get (see the
1054 // TestGetAllPorts* tests).
1055 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1056 EXPECT_EQ(4U, ports_.size());
1057 EXPECT_TRUE(candidate_allocation_done_);
1058}
1059
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001060// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1061// This test also verifies that when the allocator is only allowed to use
1062// relay (i.e. IceTransportsType is relay), the raddr is an empty
1063// address with the correct family. This is to prevent any local
1064// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001065TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001066 AddInterface(kClientAddr);
1067 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001068 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001069 allocator().set_candidate_filter(CF_RELAY);
1070 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001071 session_->StartGettingPorts();
1072 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001073 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1074 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001075
1076 EXPECT_EQ(1U, candidates_.size());
1077 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001078 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1079 EXPECT_EQ(
1080 candidates_[0].related_address(),
1081 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001082}
1083
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001084TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001085 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001086 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1087 allocator().set_candidate_filter(CF_HOST);
1088 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001089 session_->StartGettingPorts();
1090 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001091 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1092 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001093 for (const Candidate& candidate : candidates_) {
1094 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001095 }
1096}
1097
1098// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001099TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001100 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001101 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001102
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001103 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1104 allocator().set_candidate_filter(CF_REFLEXIVE);
1105 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106 session_->StartGettingPorts();
1107 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1108 // Host is behind NAT, no private address will be exposed. Hence only UDP
1109 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001110 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1111 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001112 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1113 EXPECT_EQ(
1114 candidates_[0].related_address(),
1115 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001116}
1117
1118// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001119TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001120 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001121 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1122 allocator().set_candidate_filter(CF_REFLEXIVE);
1123 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001124 session_->StartGettingPorts();
1125 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1126 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001127 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001128 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001129 for (const Candidate& candidate : candidates_) {
1130 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001131 }
1132}
1133
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001134// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001135TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001136 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001137 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001138 session_->StartGettingPorts();
1139 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001140 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1141 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1142 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001144 for (const Candidate& candidate : candidates_) {
1145 EXPECT_EQ(kIceUfrag0, candidate.username());
1146 EXPECT_EQ(kIcePwd0, candidate.password());
1147 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001148 EXPECT_TRUE(candidate_allocation_done_);
1149}
1150
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001151// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1152// is allocated for udp and stun. Also verify there is only one candidate
1153// (local) if stun candidate is same as local candidate, which will be the case
1154// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001155TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 AddInterface(kClientAddr);
1157 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001158 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1159 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001160 session_->StartGettingPorts();
1161 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1162 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001163 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001164 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1165}
1166
1167// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1168// is allocated for udp and stun. In this test we should expect both stun and
1169// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001170TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001171 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001172 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001173
1174 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001175 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1176 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001177 session_->StartGettingPorts();
1178 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1179 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001180 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1181 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1182 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001183 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1184 EXPECT_EQ(3U, candidates_.size());
1185}
1186
deadbeefc5d0d952015-07-16 10:22:21 -07001187// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001188TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001189 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001191 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192
1193 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1194
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001195 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001196 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001197 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1198 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001199
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001200 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001201 session_->StartGettingPorts();
1202
1203 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1204 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001205 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1206 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1207 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1208 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1209 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001210 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1211 EXPECT_EQ(3U, candidates_.size());
1212}
1213
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001214// Test that if prune_turn_ports is set, TCP TurnPort will not
1215// be used if UDP TurnPort is used.
1216TEST_F(BasicPortAllocatorTest, TestUdpTurnPortPrunesTcpTurnPorts) {
1217 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1218 AddInterface(kClientAddr);
1219 allocator_.reset(new BasicPortAllocator(&network_manager_));
1220 allocator_->SetConfiguration(allocator_->stun_servers(),
1221 allocator_->turn_servers(), 0, true);
1222 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1223 allocator_->set_step_delay(kMinimumStepDelay);
1224 allocator_->set_flags(allocator().flags() |
1225 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1226 PORTALLOCATOR_DISABLE_TCP);
1227
1228 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1229 session_->StartGettingPorts();
1230 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1231 // Only 2 ports (one STUN and one TURN) are actually being used.
1232 EXPECT_EQ(2U, session_->ReadyPorts().size());
1233 // We have verified that each port, when it is added to |ports_|, it is found
1234 // in |ready_ports|, and when it is pruned, it is not found in |ready_ports|,
1235 // so we only need to verify the content in one of them.
1236 EXPECT_EQ(2U, ports_.size());
1237 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
1238 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
1239 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
1240
1241 // We don't remove candidates, so the size of |candidates_| will depend on
1242 // when the TCP TURN port becomes ready. If it is ready after the UDP TURN
1243 // port becomes ready, its candidates will be used there will be 3 candidates.
1244 // Otherwise there will be only 2 candidates.
1245 EXPECT_LE(2U, candidates_.size());
1246 // There will only be 2 candidates in |ready_candidates| because it only
1247 // includes the candidates in the ready ports.
1248 const std::vector<Candidate>& ready_candidates = session_->ReadyCandidates();
1249 EXPECT_EQ(2U, ready_candidates.size());
1250 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
1251 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
1252 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1253}
1254
1255// Tests that if prune_turn_ports is set, IPv4 TurnPort will not
1256// be used if IPv6 TurnPort is used.
1257TEST_F(BasicPortAllocatorTest, TestIPv6TurnPortPrunesIPv4TurnPorts) {
1258 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
1259 // Add two IP addresses on the same interface.
1260 AddInterface(kClientAddr, "net1");
1261 AddInterface(kClientIPv6Addr, "net1");
1262 allocator_.reset(new BasicPortAllocator(&network_manager_));
1263 allocator_->SetConfiguration(allocator_->stun_servers(),
1264 allocator_->turn_servers(), 0, true);
1265 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
1266
1267 allocator_->set_step_delay(kMinimumStepDelay);
1268 allocator_->set_flags(allocator().flags() |
1269 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1270 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
1271
1272 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1273 session_->StartGettingPorts();
1274 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1275 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
1276 EXPECT_EQ(3U, session_->ReadyPorts().size());
1277 EXPECT_EQ(3U, ports_.size());
1278 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
1279 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
1280 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
1281 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
1282
1283 // We don't remove candidates, so there may be more than 3 elemenets in
1284 // |candidates_|, although |ready_candidates| only includes the candidates
1285 // in |ready_ports|.
1286 EXPECT_LE(3U, candidates_.size());
1287 const std::vector<Candidate>& ready_candidates = session_->ReadyCandidates();
1288 EXPECT_EQ(3U, ready_candidates.size());
1289 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
1290 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
1291 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1292}
1293
1294// Test has an assert error on win_x64_dbg and win_dbg. See: webrtc:6068
1295#if defined(WEBRTC_WIN)
1296#define MAYBE_TestEachInterfaceHasItsOwnTurnPorts \
1297 DISABLED_TestEachInterfaceHasItsOwnTurnPort
1298#else
1299#define MAYBE_TestEachInterfaceHasItsOwnTurnPorts \
1300 TestEachInterfaceHasItsOwnTurnPorts
1301#endif
1302// Tests that if prune_turn_ports is set, each network interface
1303// will has its own set of TurnPorts based on their priorities.
1304TEST_F(BasicPortAllocatorTest, MAYBE_TestEachInterfaceHasItsOwnTurnPorts) {
1305 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1306 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
1307 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
1308 // Add two interfaces both having IPv4 and IPv6 addresses.
1309 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
1310 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
1311 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
1312 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
1313 allocator_.reset(new BasicPortAllocator(&network_manager_));
1314 allocator_->SetConfiguration(allocator_->stun_servers(),
1315 allocator_->turn_servers(), 0, true);
1316 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
1317 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1318 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1319
1320 allocator_->set_step_delay(kMinimumStepDelay);
1321 allocator_->set_flags(allocator().flags() |
1322 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1323 PORTALLOCATOR_ENABLE_IPV6);
1324 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1325 session_->StartGettingPorts();
1326 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1327 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to use.
1328 EXPECT_EQ(10U, session_->ReadyPorts().size());
1329 EXPECT_EQ(10U, ports_.size());
1330 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
1331 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
1332 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
1333 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
1334 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
1335 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
1336 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
1337 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
1338 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
1339 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
1340
1341 // We don't remove candidates, so there may be more than 10 candidates
1342 // in |candidates_|.
1343 EXPECT_LE(10U, candidates_.size());
1344 const std::vector<Candidate>& ready_candidates = session_->ReadyCandidates();
1345 EXPECT_EQ(10U, ready_candidates.size());
1346 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
1347 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
1348 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientIPv6Addr);
1349 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
1350 kClientIPv6Addr2);
1351 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
1352 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
1353 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientIPv6Addr);
1354 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
1355 kClientIPv6Addr2);
1356 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
1357 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1358}
1359
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360// Testing DNS resolve for the TURN server, this will test AllocationSequence
1361// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001362TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001363 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001364 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001365 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001366 allocator_.reset(new BasicPortAllocator(&network_manager_));
1367 RelayServerConfig turn_server(RELAY_TURN);
1368 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001369 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001370 turn_server.ports.push_back(
1371 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001372 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001373
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001374 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001375 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001376 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1377 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001378
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001379 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001380 session_->StartGettingPorts();
1381
1382 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1383}
1384
1385// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1386// is allocated for udp/stun/turn. In this test we should expect all local,
1387// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001388TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001389 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001390 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001391
1392 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1393
1394 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001395 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1396 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001397
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001398 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001399 session_->StartGettingPorts();
1400
1401 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1402 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001403 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1404 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1405 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1406 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1407 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001408 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1409 EXPECT_EQ(3U, candidates_.size());
1410 // Local port will be created first and then TURN port.
1411 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1412 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1413}
1414
1415// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1416// server is also used as the STUN server, we should get 'local', 'stun', and
1417// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001418TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001420 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001421 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001422 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1423
1424 // Must set the step delay to 0 to make sure the relay allocation phase is
1425 // started before the STUN candidates are obtained, so that the STUN binding
1426 // response is processed when both StunPort and TurnPort exist to reproduce
1427 // webrtc issue 3537.
1428 allocator_->set_step_delay(0);
1429 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001430 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1431 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001433 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001434 session_->StartGettingPorts();
1435
1436 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001437 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1438 Candidate stun_candidate;
1439 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1440 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1441 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1442 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1443 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001444
1445 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1446 EXPECT_EQ(3U, candidates_.size());
1447 // Local port will be created first and then TURN port.
1448 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1449 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1450}
1451
deadbeefc5d0d952015-07-16 10:22:21 -07001452// Test that when only a TCP TURN server is available, we do NOT use it as
1453// a UDP STUN server, as this could leak our IP address. Thus we should only
1454// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001455TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001456 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001457 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001458 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001459 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1460
1461 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001462 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1463 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001464
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001465 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001466 session_->StartGettingPorts();
1467
1468 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1469 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001470 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1471 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1472 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001473 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1474 EXPECT_EQ(2U, candidates_.size());
1475 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1476 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1477}
1478
1479// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1480// TURN server is used as the STUN server and we get 'local', 'stun', and
1481// 'relay' candidates.
1482// TODO(deadbeef): Remove this test when support for non-shared socket mode
1483// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001484TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001485 AddInterface(kClientAddr);
1486 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001487 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001488 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1489
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001490 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001491
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001492 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001493 session_->StartGettingPorts();
1494
1495 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1496 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001497 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1498 Candidate stun_candidate;
1499 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1500 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1501 Candidate turn_candidate;
1502 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1503 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1504 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001505 // Not using shared socket, so the STUN request's server reflexive address
1506 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001507 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001508
1509 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1510 EXPECT_EQ(3U, candidates_.size());
1511 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1512 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1513 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1514}
1515
1516// Test that even when both a STUN and TURN server are configured, the TURN
1517// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001518TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001519 AddInterface(kClientAddr);
1520 // Configure with STUN server but destroy it, so we can ensure that it's
1521 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001522 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001523 stun_server_.reset();
1524 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1525
1526 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001527 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1528 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001529
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001530 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001531 session_->StartGettingPorts();
1532
1533 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001534 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1535 Candidate stun_candidate;
1536 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1537 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1538 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1539 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1540 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001541
1542 // Don't bother waiting for STUN timeout, since we already verified
1543 // that we got a STUN candidate from the TURN server.
1544}
1545
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001546// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1547// and fail to generate STUN candidate, local UDP candidate is generated
1548// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001549TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001550 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1551 PORTALLOCATOR_DISABLE_TCP |
1552 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001553 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1554 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001555 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001556 session_->StartGettingPorts();
1557 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1558 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001559 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001560 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1561 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001562 EXPECT_EQ(1U, candidates_.size());
1563}
1564
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001565// Test that when the NetworkManager doesn't have permission to enumerate
1566// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1567// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001568TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001569 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1570 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001571 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001572 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001573 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1574 PORTALLOCATOR_DISABLE_TCP |
1575 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1576 EXPECT_EQ(0U,
1577 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1578 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1579 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001580 session_->StartGettingPorts();
1581 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001582 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001583 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001584 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001585}
1586
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001587// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001588TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001589 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1590 PORTALLOCATOR_ENABLE_IPV6 |
1591 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592 AddInterface(kClientIPv6Addr);
1593 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001594 allocator_->set_step_delay(kMinimumStepDelay);
1595 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001596 session_->StartGettingPorts();
1597 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1598 EXPECT_EQ(4U, candidates_.size());
1599 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001600 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1601 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1602 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1603 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001604 EXPECT_EQ(4U, candidates_.size());
1605}
honghaiz98db68f2015-09-29 07:58:17 -07001606
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001607TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001608 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001609 allocator_->set_step_delay(kDefaultStepDelay);
1610 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001611 session_->StartGettingPorts();
1612 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1613 EXPECT_EQ(2U, ports_.size());
1614 session_->StopGettingPorts();
1615 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1616
1617 // After stopping getting ports, adding a new interface will not start
1618 // getting ports again.
1619 candidates_.clear();
1620 ports_.clear();
1621 candidate_allocation_done_ = false;
1622 network_manager_.AddInterface(kClientAddr2);
1623 rtc::Thread::Current()->ProcessMessages(1000);
1624 EXPECT_EQ(0U, candidates_.size());
1625 EXPECT_EQ(0U, ports_.size());
1626}
1627
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001628TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001629 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001630 allocator_->set_step_delay(kDefaultStepDelay);
1631 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001632 session_->StartGettingPorts();
1633 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1634 EXPECT_EQ(2U, ports_.size());
1635 session_->ClearGettingPorts();
1636 WAIT(candidate_allocation_done_, 1000);
1637 EXPECT_FALSE(candidate_allocation_done_);
1638
1639 // After clearing getting ports, adding a new interface will start getting
1640 // ports again.
1641 candidates_.clear();
1642 ports_.clear();
1643 candidate_allocation_done_ = false;
1644 network_manager_.AddInterface(kClientAddr2);
1645 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1646 EXPECT_EQ(2U, ports_.size());
1647}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001648
1649// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1650// a pooled session is taken out of the pool.
1651TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1652 AddInterface(kClientAddr);
1653 int pool_size = 1;
1654 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001655 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001656 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001657 ASSERT_NE(nullptr, peeked_session);
1658 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1659 kDefaultAllocationTimeout);
1660 // Expect that when TakePooledSession is called,
1661 // UpdateTransportInformationInternal will be called and the
1662 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1663 // candidates.
1664 session_ =
1665 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1666 ASSERT_NE(nullptr, session_.get());
1667 auto ready_ports = session_->ReadyPorts();
1668 auto candidates = session_->ReadyCandidates();
1669 EXPECT_FALSE(ready_ports.empty());
1670 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001671 for (const PortInterface* port_interface : ready_ports) {
1672 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001673 EXPECT_EQ(kContentName, port->content_name());
1674 EXPECT_EQ(1, port->component());
1675 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1676 EXPECT_EQ(kIcePwd0, port->password());
1677 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001678 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001679 EXPECT_EQ(1, candidate.component());
1680 EXPECT_EQ(kIceUfrag0, candidate.username());
1681 EXPECT_EQ(kIcePwd0, candidate.password());
1682 }
1683}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001684
1685// Test that a new candidate filter takes effect even on already-gathered
1686// candidates.
1687TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1688 AddInterface(kClientAddr);
1689 int pool_size = 1;
1690 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001691 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001692 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1693 ASSERT_NE(nullptr, peeked_session);
1694 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1695 kDefaultAllocationTimeout);
1696 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1697 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1698 allocator_->set_candidate_filter(CF_RELAY);
1699 // Assume that when TakePooledSession is called, the candidate filter will be
1700 // applied to the pooled session. This is tested by PortAllocatorTest.
1701 session_ =
1702 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1703 ASSERT_NE(nullptr, session_.get());
1704 auto candidates = session_->ReadyCandidates();
1705 auto ports = session_->ReadyPorts();
1706 // Sanity check that the number of candidates and ports decreased.
1707 EXPECT_GT(initial_candidates_size, candidates.size());
1708 EXPECT_GT(initial_ports_size, ports.size());
1709 for (const PortInterface* port : ports) {
1710 // Expect only relay ports.
1711 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1712 }
1713 for (const Candidate& candidate : candidates) {
1714 // Expect only relay candidates now that the filter is applied.
1715 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1716 // Expect that the raddr is emptied due to the CF_RELAY filter.
1717 EXPECT_EQ(candidate.related_address(),
1718 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1719 }
1720}
1721
1722} // namespace cricket