blob: afd65a42c5e0b5ea9a626c9be04411956ffa8542 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/basicpacketsocketfactory.h"
kjellanderf4752772016-03-02 05:42:30 -080015#include "webrtc/p2p/base/p2pconstants.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/testrelayserver.h"
18#include "webrtc/p2p/base/teststunserver.h"
19#include "webrtc/p2p/base/testturnserver.h"
20#include "webrtc/p2p/client/basicportallocator.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021#include "webrtc/base/fakenetwork.h"
22#include "webrtc/base/firewallsocketserver.h"
23#include "webrtc/base/gunit.h"
24#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070025#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026#include "webrtc/base/logging.h"
27#include "webrtc/base/natserver.h"
28#include "webrtc/base/natsocketfactory.h"
29#include "webrtc/base/network.h"
30#include "webrtc/base/physicalsocketserver.h"
31#include "webrtc/base/socketaddress.h"
32#include "webrtc/base/ssladapter.h"
33#include "webrtc/base/thread.h"
34#include "webrtc/base/virtualsocketserver.h"
35
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080036using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037using rtc::SocketAddress;
38using rtc::Thread;
39
40static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070041static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070042static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000044static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070045static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
46 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070047static const SocketAddress kClientIPv6Addr2(
48 "2401:fa00:4:2000:be30:5bff:fee5:c3",
49 0);
deadbeefc5d0d952015-07-16 10:22:21 -070050static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
51static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
53static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
54static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
55static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
56static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
57static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
58static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
59static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
60static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070061static const SocketAddress kTurnUdpIntIPv6Addr(
62 "2402:fb00:4:1000:be30:5bff:fee5:c3",
63 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070065static const SocketAddress kTurnTcpIntIPv6Addr(
66 "2402:fb00:4:2000:be30:5bff:fee5:c3",
67 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
69
70// Minimum and maximum port for port range tests.
71static const int kMinPort = 10000;
72static const int kMaxPort = 10099;
73
74// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070075static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076// Based on ICE_PWD_LENGTH
77static const char kIcePwd0[] = "TESTICEPWD00000000000000";
78
79static const char kContentName[] = "test content";
80
Honghai Zhangc67e0f52016-09-19 16:57:37 -070081static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082static const char kTurnUsername[] = "test";
83static const char kTurnPassword[] = "test";
84
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070085// STUN timeout (with all retries) is 9500ms.
86// Add some margin of error for slow bots.
87// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
88// fix flaky tests.
89static const int kStunTimeoutMs = 15000;
90
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091namespace cricket {
92
93// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070094std::ostream& operator<<(std::ostream& os,
95 const std::vector<Candidate>& candidates) {
96 os << '[';
97 bool first = true;
98 for (const Candidate& c : candidates) {
99 if (!first) {
100 os << ", ";
101 }
102 os << c.ToString();
103 first = false;
104 };
105 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 return os;
107}
108
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700109class BasicPortAllocatorTest : public testing::Test,
110 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700112 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 : pss_(new rtc::PhysicalSocketServer),
114 vss_(new rtc::VirtualSocketServer(pss_.get())),
115 fss_(new rtc::FirewallSocketServer(vss_.get())),
116 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700117 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700118 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700119 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700120 relay_server_(Thread::Current(),
121 kRelayUdpIntAddr,
122 kRelayUdpExtAddr,
123 kRelayTcpIntAddr,
124 kRelayTcpExtAddr,
125 kRelaySslTcpIntAddr,
126 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
128 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700129 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130 stun_servers.insert(kStunAddr);
131 // Passing the addresses of GTURN servers will enable GTURN in
132 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700133 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
134 kRelayUdpIntAddr, kRelayTcpIntAddr,
135 kRelaySslTcpIntAddr));
136 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 }
138
139 void AddInterface(const SocketAddress& addr) {
140 network_manager_.AddInterface(addr);
141 }
honghaiz8c404fa2015-09-28 07:59:43 -0700142 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
143 network_manager_.AddInterface(addr, if_name);
144 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800145 void AddInterface(const SocketAddress& addr,
146 const std::string& if_name,
147 rtc::AdapterType type) {
148 network_manager_.AddInterface(addr, if_name, type);
149 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800150 // The default route is the public address that STUN server will observe when
151 // the endpoint is sitting on the public internet and the local port is bound
152 // to the "any" address. This may be different from the default local address
153 // which the endpoint observes. This can occur if the route to the public
154 // endpoint like 8.8.8.8 (specified as the default local address) is
155 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700156 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
157 AddInterface(addr);
158 // When a binding comes from the any address, the |addr| will be used as the
159 // srflx address.
160 vss_->SetDefaultRoute(addr.ipaddr());
161 }
honghaiz8c404fa2015-09-28 07:59:43 -0700162 void RemoveInterface(const SocketAddress& addr) {
163 network_manager_.RemoveInterface(addr);
164 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 bool SetPortRange(int min_port, int max_port) {
166 return allocator_->SetPortRange(min_port, max_port);
167 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700168 // Endpoint is on the public network. No STUN or TURN.
169 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700170 allocator_.reset(new BasicPortAllocator(&network_manager_));
171 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700172 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700173 // Endpoint is behind a NAT, with STUN specified.
174 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
175 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700176 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700177 // Endpoint is on the public network, with STUN specified.
178 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
179 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700181 // Endpoint is on the public network, with TURN specified.
182 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
183 const rtc::SocketAddress& tcp_turn) {
184 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 AddTurnServers(udp_turn, tcp_turn);
186 }
187
188 void AddTurnServers(const rtc::SocketAddress& udp_turn,
189 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700190 RelayServerConfig turn_server(RELAY_TURN);
191 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800192 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193
194 if (!udp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700195 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196 }
197 if (!tcp_turn.IsNil()) {
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700198 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 }
deadbeef653b8e02015-11-11 12:55:10 -0800200 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 }
202
203 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700204 session_ = CreateSession("session", component);
205 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700207 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 return true;
209 }
210
211 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700212 session_ = CreateSession("session", content_name, component);
213 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700215 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 return true;
217 }
218
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700219 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
220 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return CreateSession(sid, kContentName, component);
222 }
223
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700224 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700225 const std::string& sid,
226 const std::string& content_name,
227 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
229 }
230
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700231 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700232 const std::string& sid,
233 const std::string& content_name,
234 int component,
235 const std::string& ice_ufrag,
236 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700237 std::unique_ptr<PortAllocatorSession> session =
238 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700240 &BasicPortAllocatorTest::OnPortReady);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700241 session->SignalPortsPruned.connect(this,
242 &BasicPortAllocatorTest::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700243 session->SignalCandidatesReady.connect(
244 this, &BasicPortAllocatorTest::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700245 session->SignalCandidatesRemoved.connect(
246 this, &BasicPortAllocatorTest::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700247 session->SignalCandidatesAllocationDone.connect(
248 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 return session;
250 }
251
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700252 // Return true if the addresses are the same, or the port is 0 in |pattern|
253 // (acting as a wildcard) and the IPs are the same.
254 // Even with a wildcard port, the port of the address should be nonzero if
255 // the IP is nonzero.
256 static bool AddressMatch(const SocketAddress& address,
257 const SocketAddress& pattern) {
258 return address.ipaddr() == pattern.ipaddr() &&
259 ((pattern.port() == 0 &&
260 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
261 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700263
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700264 // Returns the number of ports that have matching type, protocol and
265 // address.
266 static int CountPorts(const std::vector<PortInterface*>& ports,
267 const std::string& type,
268 ProtocolType protocol,
269 const SocketAddress& client_addr) {
270 return std::count_if(
271 ports.begin(), ports.end(),
272 [type, protocol, client_addr](PortInterface* port) {
273 return port->Type() == type && port->GetProtocol() == protocol &&
274 port->Network()->GetBestIP() == client_addr.ipaddr();
275 });
276 }
277
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700278 // Find a candidate and return it.
279 static bool FindCandidate(const std::vector<Candidate>& candidates,
280 const std::string& type,
281 const std::string& proto,
282 const SocketAddress& addr,
283 Candidate* found) {
284 auto it = std::find_if(candidates.begin(), candidates.end(),
285 [type, proto, addr](const Candidate& c) {
286 return c.type() == type && c.protocol() == proto &&
287 AddressMatch(c.address(), addr);
288 });
289 if (it != candidates.end() && found) {
290 *found = *it;
291 }
292 return it != candidates.end();
293 }
294
295 // Convenience method to call FindCandidate with no return.
296 static bool HasCandidate(const std::vector<Candidate>& candidates,
297 const std::string& type,
298 const std::string& proto,
299 const SocketAddress& addr) {
300 return FindCandidate(candidates, type, proto, addr, nullptr);
301 }
302
303 // Version of HasCandidate that also takes a related address.
304 static bool HasCandidateWithRelatedAddr(
305 const std::vector<Candidate>& candidates,
306 const std::string& type,
307 const std::string& proto,
308 const SocketAddress& addr,
309 const SocketAddress& related_addr) {
310 auto it =
311 std::find_if(candidates.begin(), candidates.end(),
312 [type, proto, addr, related_addr](const Candidate& c) {
313 return c.type() == type && c.protocol() == proto &&
314 AddressMatch(c.address(), addr) &&
315 AddressMatch(c.related_address(), related_addr);
316 });
317 return it != candidates.end();
318 }
319
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000320 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700321 int min_port,
322 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323 return (addr.port() >= min_port && addr.port() <= max_port);
324 }
325
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700326 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000327 // We should only get this callback once, except in the mux test where
328 // we have multiple port allocation sessions.
329 if (session == session_.get()) {
330 ASSERT_FALSE(candidate_allocation_done_);
331 candidate_allocation_done_ = true;
332 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700333 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000334 }
335
336 // Check if all ports allocated have send-buffer size |expected|. If
337 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
338 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700339 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000340 for (it = ports_.begin(); it < ports_.end(); ++it) {
341 int send_buffer_size;
342 if (expected == -1) {
343 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700344 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000345 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700346 EXPECT_EQ(0,
347 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000348 ASSERT_EQ(expected, send_buffer_size);
349 }
350 }
351 }
352
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700353 // This function starts the port/address gathering and check the existence of
354 // candidates as specified. When |expect_stun_candidate| is true,
355 // |stun_candidate_addr| carries the expected reflective address, which is
356 // also the related address for TURN candidate if it is expected. Otherwise,
357 // it should be ignore.
358 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200359 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700360 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700361 const rtc::IPAddress& stun_candidate_addr,
362 const rtc::IPAddress& relay_candidate_udp_transport_addr,
363 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800364 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
365 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700366 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700367 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700368 }
369 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700370 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
371 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700372 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000373 session_->StartGettingPorts();
374 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
375
Peter Boström0c4e06b2015-10-07 12:23:21 +0200376 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700377 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700378 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800379 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700380 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700381 }
382 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700383 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800384 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700385 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800386 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700387 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
388 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700389 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700390 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700391 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700392 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
393 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
394 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700395 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700396 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700397 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700398 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
399 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
400 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700401 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700402 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000403
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700404 EXPECT_EQ(total_candidates, candidates_.size());
405 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000406 }
407
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700408 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
409
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700411 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000412
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700413 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000414 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
415 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700416 // Make sure the new port is added to ReadyPorts.
417 auto ready_ports = ses->ReadyPorts();
418 EXPECT_NE(ready_ports.end(),
419 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000420 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700421 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700422 const std::vector<PortInterface*>& pruned_ports) {
423 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700424 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700425 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700426 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700427 new_end = std::remove(ports_.begin(), new_end, port);
428 // Make sure the pruned port is not in ReadyPorts.
429 EXPECT_EQ(ready_ports.end(),
430 std::find(ready_ports.begin(), ready_ports.end(), port));
431 }
432 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700433 }
434
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700435 void OnCandidatesReady(PortAllocatorSession* ses,
436 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700437 for (const Candidate& candidate : candidates) {
438 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
439 // Sanity check that the ICE component is set.
440 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
441 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700443 // Make sure the new candidates are added to Candidates.
444 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700445 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700446 EXPECT_NE(
447 ses_candidates.end(),
448 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
449 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000450 }
451
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700452 void OnCandidatesRemoved(PortAllocatorSession* session,
453 const std::vector<Candidate>& removed_candidates) {
454 auto new_end = std::remove_if(
455 candidates_.begin(), candidates_.end(),
456 [removed_candidates](Candidate& candidate) {
457 for (const Candidate& removed_candidate : removed_candidates) {
458 if (candidate.MatchesForRemoval(removed_candidate)) {
459 return true;
460 }
461 }
462 return false;
463 });
464 candidates_.erase(new_end, candidates_.end());
465 }
466
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700467 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800468 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700469 RelayServerConfig server_config = allocator_->turn_servers()[i];
470 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000471 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700472 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000473 if (proto_addr.address == relay_port->address &&
474 proto_addr.proto == relay_port->proto)
475 return true;
476 }
477 }
478 return false;
479 }
480
Guo-wei Shieh11477022015-08-15 09:28:41 -0700481 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
482 bool with_nat) {
483 if (with_nat) {
484 nat_server_.reset(new rtc::NATServer(
485 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
486 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
487 } else {
488 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
489 }
490
491 ServerAddresses stun_servers;
492 if (!stun_server.IsNil()) {
493 stun_servers.insert(stun_server);
494 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700495 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700496 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700497 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700498 }
499
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700500 void TestUdpTurnPortPrunesTcpTurnPort() {
501 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
502 AddInterface(kClientAddr);
503 allocator_.reset(new BasicPortAllocator(&network_manager_));
504 allocator_->SetConfiguration(allocator_->stun_servers(),
505 allocator_->turn_servers(), 0, true);
506 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
507 allocator_->set_step_delay(kMinimumStepDelay);
508 allocator_->set_flags(allocator().flags() |
509 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
510 PORTALLOCATOR_DISABLE_TCP);
511
512 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
513 session_->StartGettingPorts();
514 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
515 // Only 2 ports (one STUN and one TURN) are actually being used.
516 EXPECT_EQ(2U, session_->ReadyPorts().size());
517 // We have verified that each port, when it is added to |ports_|, it is
518 // found in |ready_ports|, and when it is pruned, it is not found in
519 // |ready_ports|, so we only need to verify the content in one of them.
520 EXPECT_EQ(2U, ports_.size());
521 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
522 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
523 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
524
525 // Now that we remove candidates when a TURN port is pruned, |candidates_|
526 // should only contains two candidates regardless whether the TCP TURN port
527 // is created before or after the UDP turn port.
528 EXPECT_EQ(2U, candidates_.size());
529 // There will only be 2 candidates in |ready_candidates| because it only
530 // includes the candidates in the ready ports.
531 const std::vector<Candidate>& ready_candidates =
532 session_->ReadyCandidates();
533 EXPECT_EQ(2U, ready_candidates.size());
534 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
535 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
536 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
537 }
538
539 void TestIPv6TurnPortPrunesIPv4TurnPort() {
540 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
541 // Add two IP addresses on the same interface.
542 AddInterface(kClientAddr, "net1");
543 AddInterface(kClientIPv6Addr, "net1");
544 allocator_.reset(new BasicPortAllocator(&network_manager_));
545 allocator_->SetConfiguration(allocator_->stun_servers(),
546 allocator_->turn_servers(), 0, true);
547 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
548 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
549
550 allocator_->set_step_delay(kMinimumStepDelay);
551 allocator_->set_flags(
552 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
553 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
554
555 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
556 session_->StartGettingPorts();
557 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
558 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
559 EXPECT_EQ(3U, session_->ReadyPorts().size());
560 EXPECT_EQ(3U, ports_.size());
561 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
562 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
563 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
564 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
565
566 // Now that we remove candidates when a TURN port is pruned, there will be
567 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
568 EXPECT_EQ(3U, candidates_.size());
569 const std::vector<Candidate>& ready_candidates =
570 session_->ReadyCandidates();
571 EXPECT_EQ(3U, ready_candidates.size());
572 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
573 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
574 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
575 }
576
577 void TestEachInterfaceHasItsOwnTurnPorts() {
578 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
579 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
580 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
581 // Add two interfaces both having IPv4 and IPv6 addresses.
582 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
583 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
584 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
585 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
586 allocator_.reset(new BasicPortAllocator(&network_manager_));
587 allocator_->SetConfiguration(allocator_->stun_servers(),
588 allocator_->turn_servers(), 0, true);
589 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
590 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
591 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
592
593 allocator_->set_step_delay(kMinimumStepDelay);
594 allocator_->set_flags(allocator().flags() |
595 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
596 PORTALLOCATOR_ENABLE_IPV6);
597 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
598 session_->StartGettingPorts();
599 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
600 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
601 // use.
602 EXPECT_EQ(10U, session_->ReadyPorts().size());
603 EXPECT_EQ(10U, ports_.size());
604 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
605 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
606 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
607 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
608 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
609 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
610 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
611 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
612 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
613 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
614
615 // Now that we remove candidates when TURN ports are pruned, there will be
616 // exactly 10 candidates in |candidates_|.
617 EXPECT_EQ(10U, candidates_.size());
618 const std::vector<Candidate>& ready_candidates =
619 session_->ReadyCandidates();
620 EXPECT_EQ(10U, ready_candidates.size());
621 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
622 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
623 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
624 kClientIPv6Addr);
625 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
626 kClientIPv6Addr2);
627 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
628 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
629 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
630 kClientIPv6Addr);
631 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
632 kClientIPv6Addr2);
633 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
634 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
635 }
636
kwiberg3ec46792016-04-27 07:22:53 -0700637 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
638 std::unique_ptr<rtc::VirtualSocketServer> vss_;
639 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000640 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700641 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000642 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700643 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700644 std::unique_ptr<TestStunServer> stun_server_;
645 TestRelayServer relay_server_;
646 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000647 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700648 std::unique_ptr<BasicPortAllocator> allocator_;
649 std::unique_ptr<PortAllocatorSession> session_;
650 std::vector<PortInterface*> ports_;
651 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000652 bool candidate_allocation_done_;
653};
654
655// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700656TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000657 EXPECT_EQ(&network_manager_, allocator().network_manager());
658 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800659 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700660 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000661 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800662 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
663 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700664 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
665 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
666 EXPECT_TRUE(
667 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
668 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700669 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670}
671
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800672// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700673TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800674 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
675 rtc::ADAPTER_TYPE_ETHERNET);
676 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
677 rtc::ADAPTER_TYPE_WIFI);
678 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
679 rtc::ADAPTER_TYPE_CELLULAR);
680 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
681 rtc::ADAPTER_TYPE_VPN);
682 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
683 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700684 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
685 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
686 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800687 session_->StartGettingPorts();
688 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
689 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700690 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 EXPECT_LT(candidate.address().ip(), 0x12345604U);
692 }
693}
694
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700695TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800696 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
697 rtc::ADAPTER_TYPE_ETHERNET);
698 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
699 rtc::ADAPTER_TYPE_WIFI);
700 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
701 rtc::ADAPTER_TYPE_CELLULAR);
702 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
703 rtc::ADAPTER_TYPE_LOOPBACK |
704 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700705 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
706 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
707 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 session_->StartGettingPorts();
709 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
710 EXPECT_EQ(1U, candidates_.size());
711 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
712}
713
honghaiz60347052016-05-31 18:29:12 -0700714// Test that high cost networks are filtered if the flag
715// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
716TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
717 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
718 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
719 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
720 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
721 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
722 // interface.
723 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
724 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
725 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
726 cricket::PORTALLOCATOR_DISABLE_RELAY |
727 cricket::PORTALLOCATOR_DISABLE_TCP |
728 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
729 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
730 session_->StartGettingPorts();
731 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
732 EXPECT_EQ(1U, candidates_.size());
733 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
734
735 // If both cellular and unknown interfaces are present, only gather on the
736 // unknown interfaces.
737 candidates_.clear();
738 candidate_allocation_done_ = false;
739 RemoveInterface(addr_wifi);
740 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
741 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
742 session_->StartGettingPorts();
743 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
744 EXPECT_EQ(2U, candidates_.size());
745 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
746 addr_unknown2.EqualIPs(candidates_[1].address())) ||
747 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
748 addr_unknown2.EqualIPs(candidates_[0].address())));
749
750 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
751 // Wi-Fi interface.
752 candidates_.clear();
753 candidate_allocation_done_ = false;
754 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
755 session_->StartGettingPorts();
756 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
757 EXPECT_EQ(1U, candidates_.size());
758 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
759}
760
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000761// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700762TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700763 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000764 session_->StartGettingPorts();
765 // Waiting for one second to make sure BasicPortAllocatorSession has not
766 // called OnAllocate multiple times. In old behavior it's called every 250ms.
767 // When there are no network interfaces, each execution of OnAllocate will
768 // result in SignalCandidatesAllocationDone signal.
769 rtc::Thread::Current()->ProcessMessages(1000);
770 EXPECT_TRUE(candidate_allocation_done_);
771 EXPECT_EQ(0U, candidates_.size());
772}
773
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700774// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700775TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
777 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700778 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
779 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
780 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700781 session_->StartGettingPorts();
782 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
783 EXPECT_EQ(1U, candidates_.size());
784}
785
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700787TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000788 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700789 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000790 session_->StartGettingPorts();
791 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
792 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700793 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
794 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
795 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
796 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
797 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
798 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
799 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
800 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000801 EXPECT_TRUE(candidate_allocation_done_);
802}
803
honghaiz8c404fa2015-09-28 07:59:43 -0700804// Test that when the same network interface is brought down and up, the
805// port allocator session will restart a new allocation sequence if
806// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700807TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700808 std::string if_name("test_net0");
809 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700810 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700811 session_->StartGettingPorts();
812 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
813 EXPECT_EQ(4U, ports_.size());
814 EXPECT_TRUE(candidate_allocation_done_);
815 candidate_allocation_done_ = false;
816 candidates_.clear();
817 ports_.clear();
818
819 RemoveInterface(kClientAddr);
820 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
821 EXPECT_EQ(0U, ports_.size());
822 EXPECT_FALSE(candidate_allocation_done_);
823
824 // When the same interfaces are added again, new candidates/ports should be
825 // generated.
826 AddInterface(kClientAddr, if_name);
827 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
828 EXPECT_EQ(4U, ports_.size());
829 EXPECT_TRUE(candidate_allocation_done_);
830}
831
832// Test that when the same network interface is brought down and up, the
833// port allocator session will not restart a new allocation sequence if
834// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700835TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700836 std::string if_name("test_net0");
837 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700838 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700839 session_->StartGettingPorts();
840 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
841 EXPECT_EQ(4U, ports_.size());
842 EXPECT_TRUE(candidate_allocation_done_);
843 session_->StopGettingPorts();
844 candidates_.clear();
845 ports_.clear();
846
847 RemoveInterface(kClientAddr);
848 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
849 EXPECT_EQ(0U, ports_.size());
850
851 // When the same interfaces are added again, new candidates/ports should not
852 // be generated because the session has stopped.
853 AddInterface(kClientAddr, if_name);
854 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
855 EXPECT_EQ(0U, ports_.size());
856 EXPECT_TRUE(candidate_allocation_done_);
857}
858
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000859// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700860TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000861 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700862 allocator_->set_step_delay(kDefaultStepDelay);
863 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864 session_->StartGettingPorts();
865 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
866 EXPECT_EQ(2U, ports_.size());
867 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
868 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700869 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
870 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000871 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700872 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
873 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000874 EXPECT_EQ(4U, ports_.size());
875 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700876 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
877 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000878 EXPECT_EQ(4U, ports_.size());
879 EXPECT_TRUE(candidate_allocation_done_);
880 // If we Stop gathering now, we shouldn't get a second "done" callback.
881 session_->StopGettingPorts();
882}
883
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700884TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000885 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700886 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000887 session_->StartGettingPorts();
888 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
889 EXPECT_TRUE(candidate_allocation_done_);
890 // If we Stop gathering now, we shouldn't get a second "done" callback.
891 session_->StopGettingPorts();
892
893 // All ports should have unset send-buffer sizes.
894 CheckSendBufferSizesOfAllPorts(-1);
895}
896
897// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700898TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000899 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700900 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000901 session_->StartGettingPorts();
902 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
903 EXPECT_EQ(2U, ports_.size());
904 session_->StopGettingPorts();
905 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
906}
907
908// Test that we restrict client ports appropriately when a port range is set.
909// We check the candidates for udp/stun/tcp ports, and the from address
910// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700911TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000912 AddInterface(kClientAddr);
913 // Check that an invalid port range fails.
914 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
915 // Check that a null port range succeeds.
916 EXPECT_TRUE(SetPortRange(0, 0));
917 // Check that a valid port range succeeds.
918 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700919 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 session_->StartGettingPorts();
921 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
922 EXPECT_EQ(4U, ports_.size());
923 // Check the port number for the UDP port object.
924 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
925 // Check the port number for the STUN port object.
926 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
927 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700928 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
929 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930 // Check the port number for the TCP port object.
931 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
932 EXPECT_TRUE(candidate_allocation_done_);
933}
934
935// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700936TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700937 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000938 session_->StartGettingPorts();
939 rtc::Thread::Current()->ProcessMessages(100);
940 // Without network adapter, we should not get any candidate.
941 EXPECT_EQ(0U, candidates_.size());
942 EXPECT_TRUE(candidate_allocation_done_);
943}
944
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700945// Test that when enumeration is disabled, we should not have any ports when
946// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700947TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700948 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700949 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700950 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700951 // Expect to see no ports and no candidates.
952 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
953 rtc::IPAddress(), rtc::IPAddress());
954}
955
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800956// Test that even with multiple interfaces, the result should still be a single
957// default private, one STUN and one TURN candidate since we bind to any address
958// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700959TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700960 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000961 AddInterface(kPrivateAddr);
962 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700963 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000964 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800965
966 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
967 // address set and we have no IPv6 STUN server, there should be no IPv6
968 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700969 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
970 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800971
972 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
973 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
974 // TURN/UDP candidates.
975 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800976 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
977 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700978}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000979
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800980// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
981// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700982TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700983 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800984 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700985 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700986 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800987 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
988 // private, STUN, TURN/UDP, and TURN/TCP candidates.
989 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
990 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700991 kTurnUdpExtAddr.ipaddr());
992}
993
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800994// Test that when adapter enumeration is disabled, for endpoints without
995// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700996TEST_F(BasicPortAllocatorTest,
997 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800998 ResetWithNoServersOrNat();
999 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1000 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1001 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001002}
1003
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001004// Test that when adapter enumeration is disabled, with
1005// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1006// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001007TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001008 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1009 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001010 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1011 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001012 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1013 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001014 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001015 rtc::IPAddress(), rtc::IPAddress());
1016}
1017
1018// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001019// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1020// a NAT, there is no local candidate. However, this specified default route
1021// (kClientAddr) which was discovered when sending STUN requests, will become
1022// the srflx addresses.
1023TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001024 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001025 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001026 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001027 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001028 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1029 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001030 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1031 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001032 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1033 rtc::IPAddress(), rtc::IPAddress());
1034}
1035
1036// Test that when adapter enumeration is disabled, with
1037// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1038// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001039TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001040 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1041 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001042 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1043 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001044 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1045 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1046 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001047}
1048
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001049// Test that we disable relay over UDP, and only TCP is used when connecting to
1050// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001051TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001052 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001053 AddInterface(kClientAddr);
1054 ResetWithStunServerAndNat(kStunAddr);
1055 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001056 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1057 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1058 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1059 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001060
1061 session_->StartGettingPorts();
1062 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1063
1064 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1065 // TURN/TCP candidates.
1066 EXPECT_EQ(2U, ports_.size());
1067 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001068 Candidate turn_candidate;
1069 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1070 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001071 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001072 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1073 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001074}
1075
Erik Språngefdce692015-06-05 09:41:26 +02001076// Disable for asan, see
1077// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1078#if !defined(ADDRESS_SANITIZER)
1079
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001080// Test that we can get OnCandidatesAllocationDone callback when all the ports
1081// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001082TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001083 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001084 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1085 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1086 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001087 session_->StartGettingPorts();
1088 rtc::Thread::Current()->ProcessMessages(100);
1089 EXPECT_EQ(0U, candidates_.size());
1090 EXPECT_TRUE(candidate_allocation_done_);
1091}
1092
1093// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001094TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001095 AddInterface(kClientAddr);
1096 fss_->set_udp_sockets_enabled(false);
1097 EXPECT_TRUE(CreateSession(1));
1098 session_->StartGettingPorts();
1099 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1100 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001101 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1102 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1103 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1104 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1105 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1106 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001107 EXPECT_TRUE(candidate_allocation_done_);
1108}
1109
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001110#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001111
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001112// Test that we don't crash or malfunction if we can't create UDP sockets or
1113// listen on TCP sockets. We still give out a local TCP address, since
1114// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001115TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001116 AddInterface(kClientAddr);
1117 fss_->set_udp_sockets_enabled(false);
1118 fss_->set_tcp_listen_enabled(false);
1119 EXPECT_TRUE(CreateSession(1));
1120 session_->StartGettingPorts();
1121 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1122 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001123 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1124 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1125 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1126 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1127 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001128 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001129 EXPECT_TRUE(candidate_allocation_done_);
1130}
1131
1132// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001133// TODO(deadbeef): Find a way to exit early here.
1134TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001135 AddInterface(kClientAddr);
1136 fss_->set_tcp_sockets_enabled(false);
1137 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001138 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001139 session_->StartGettingPorts();
1140 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001141 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001142 // In case of Relay, ports creation will succeed but sockets will fail.
1143 // There is no error reporting from RelayEntry to handle this failure.
1144}
1145
1146// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001147TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001148 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1149 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001150 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001151 session_->StartGettingPorts();
1152 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1153 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001154 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1155 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
1157 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -07001158 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
1159 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001160 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001161 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1162 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1163 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1164 kRelaySslTcpIntAddr);
1165 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -07001166 // Stun Timeout is 9.5sec.
1167 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
1168 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001169}
1170
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001171TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 AddInterface(kClientAddr);
1173 AddInterface(kClientAddr2);
1174 // Allocating only host UDP ports. This is done purely for testing
1175 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001176 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1177 PORTALLOCATOR_DISABLE_RELAY);
1178 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001179 session_->StartGettingPorts();
1180 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1181 ASSERT_EQ(2U, candidates_.size());
1182 EXPECT_EQ(2U, ports_.size());
1183 // Candidates priorities should be different.
1184 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1185}
1186
1187// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001188TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001190 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001191 session_->StartGettingPorts();
1192 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1193 EXPECT_EQ(4U, ports_.size());
1194 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001195 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001196}
1197
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001198// Test that the allocator session uses the candidate filter it's created with,
1199// rather than the filter of its parent allocator.
1200// The filter of the allocator should only affect the next gathering phase,
1201// according to JSEP, which means the *next* allocator session returned.
1202TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1203 AddInterface(kClientAddr);
1204 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1205 // Set candidate filter *after* creating the session. Should have no effect.
1206 allocator().set_candidate_filter(CF_RELAY);
1207 session_->StartGettingPorts();
1208 // 7 candidates and 4 ports is what we would normally get (see the
1209 // TestGetAllPorts* tests).
1210 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1211 EXPECT_EQ(4U, ports_.size());
1212 EXPECT_TRUE(candidate_allocation_done_);
1213}
1214
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001215// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1216// This test also verifies that when the allocator is only allowed to use
1217// relay (i.e. IceTransportsType is relay), the raddr is an empty
1218// address with the correct family. This is to prevent any local
1219// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001220TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001221 AddInterface(kClientAddr);
1222 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001223 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001224 allocator().set_candidate_filter(CF_RELAY);
1225 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001226 session_->StartGettingPorts();
1227 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001228 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1229 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230
1231 EXPECT_EQ(1U, candidates_.size());
1232 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001233 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1234 EXPECT_EQ(
1235 candidates_[0].related_address(),
1236 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001237}
1238
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001239TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001240 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001241 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1242 allocator().set_candidate_filter(CF_HOST);
1243 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001244 session_->StartGettingPorts();
1245 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001246 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1247 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001248 for (const Candidate& candidate : candidates_) {
1249 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001250 }
1251}
1252
1253// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001254TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001255 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001256 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001257
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001258 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1259 allocator().set_candidate_filter(CF_REFLEXIVE);
1260 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001261 session_->StartGettingPorts();
1262 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1263 // Host is behind NAT, no private address will be exposed. Hence only UDP
1264 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001265 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1266 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001267 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1268 EXPECT_EQ(
1269 candidates_[0].related_address(),
1270 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001271}
1272
1273// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001274TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001275 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001276 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1277 allocator().set_candidate_filter(CF_REFLEXIVE);
1278 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001279 session_->StartGettingPorts();
1280 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1281 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001282 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001283 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001284 for (const Candidate& candidate : candidates_) {
1285 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001286 }
1287}
1288
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001289// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001290TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001291 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001292 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001293 session_->StartGettingPorts();
1294 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001295 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1296 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1297 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001298 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001299 for (const Candidate& candidate : candidates_) {
1300 EXPECT_EQ(kIceUfrag0, candidate.username());
1301 EXPECT_EQ(kIcePwd0, candidate.password());
1302 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001303 EXPECT_TRUE(candidate_allocation_done_);
1304}
1305
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001306// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1307// is allocated for udp and stun. Also verify there is only one candidate
1308// (local) if stun candidate is same as local candidate, which will be the case
1309// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001310TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001311 AddInterface(kClientAddr);
1312 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001313 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1314 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001315 session_->StartGettingPorts();
1316 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1317 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001318 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001319 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1320}
1321
1322// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1323// is allocated for udp and stun. In this test we should expect both stun and
1324// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001325TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001326 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001327 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001328
1329 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001330 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1331 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001332 session_->StartGettingPorts();
1333 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1334 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001335 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1336 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1337 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001338 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1339 EXPECT_EQ(3U, candidates_.size());
1340}
1341
deadbeefc5d0d952015-07-16 10:22:21 -07001342// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001344 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001345 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001346 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001347
1348 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1349
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001350 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001351 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001352 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1353 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001354
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001355 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001356 session_->StartGettingPorts();
1357
1358 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1359 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001360 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1361 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1362 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1363 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1364 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001365 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1366 EXPECT_EQ(3U, candidates_.size());
1367}
1368
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001369// Test that if prune_turn_ports is set, TCP TURN port will not be used
1370// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1371TEST_F(BasicPortAllocatorTest,
1372 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1373 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1374 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1375 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001376
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001377 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001378}
1379
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001380// Test that if prune_turn_ports is set, TCP TURN port will not be used
1381// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1382TEST_F(BasicPortAllocatorTest,
1383 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1384 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1385 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1386 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001387
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001388 TestUdpTurnPortPrunesTcpTurnPort();
1389}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001390
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001391// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1392// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1393TEST_F(BasicPortAllocatorTest,
1394 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1395 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1396 // first.
1397 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1398 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001399
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001400 TestIPv6TurnPortPrunesIPv4TurnPort();
1401}
1402
1403// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1404// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1405TEST_F(BasicPortAllocatorTest,
1406 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1407 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1408 // first.
1409 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1410 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1411
1412 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001413}
1414
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001415// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001416// will has its own set of TurnPorts based on their priorities, in the default
1417// case where no transit delay is set.
1418TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1419 TestEachInterfaceHasItsOwnTurnPorts();
1420}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001421
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001422// Tests that if prune_turn_ports is set, each network interface
1423// will has its own set of TurnPorts based on their priorities, given that
1424// IPv4/TCP TURN port becomes ready first.
1425TEST_F(BasicPortAllocatorTest,
1426 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1427 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1428 // becomes ready last.
1429 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1430 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1431 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1432 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001433
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001434 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001435}
1436
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001437// Testing DNS resolve for the TURN server, this will test AllocationSequence
1438// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001439TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001440 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001441 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001442 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001443 allocator_.reset(new BasicPortAllocator(&network_manager_));
1444 RelayServerConfig turn_server(RELAY_TURN);
1445 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001446 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001447 turn_server.ports.push_back(
1448 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001449 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001450
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001451 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001452 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001453 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1454 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001455
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001456 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001457 session_->StartGettingPorts();
1458
1459 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1460}
1461
1462// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1463// is allocated for udp/stun/turn. In this test we should expect all local,
1464// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001465TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001466 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001467 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001468
1469 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1470
1471 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001472 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1473 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001474
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001475 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001476 session_->StartGettingPorts();
1477
1478 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1479 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001480 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1481 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1482 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1483 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1484 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001485 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1486 EXPECT_EQ(3U, candidates_.size());
1487 // Local port will be created first and then TURN port.
1488 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1489 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1490}
1491
1492// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1493// server is also used as the STUN server, we should get 'local', 'stun', and
1494// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001495TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001496 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001497 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001498 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001499 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1500
1501 // Must set the step delay to 0 to make sure the relay allocation phase is
1502 // started before the STUN candidates are obtained, so that the STUN binding
1503 // response is processed when both StunPort and TurnPort exist to reproduce
1504 // webrtc issue 3537.
1505 allocator_->set_step_delay(0);
1506 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001507 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1508 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001509
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001510 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001511 session_->StartGettingPorts();
1512
1513 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001514 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1515 Candidate stun_candidate;
1516 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1517 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1518 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1519 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1520 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001521
1522 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1523 EXPECT_EQ(3U, candidates_.size());
1524 // Local port will be created first and then TURN port.
1525 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1526 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1527}
1528
deadbeefc5d0d952015-07-16 10:22:21 -07001529// Test that when only a TCP TURN server is available, we do NOT use it as
1530// a UDP STUN server, as this could leak our IP address. Thus we should only
1531// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001532TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001533 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001534 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001535 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001536 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1537
1538 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001539 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1540 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001541
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001542 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001543 session_->StartGettingPorts();
1544
1545 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1546 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001547 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1548 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1549 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001550 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1551 EXPECT_EQ(2U, candidates_.size());
1552 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1553 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1554}
1555
1556// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1557// TURN server is used as the STUN server and we get 'local', 'stun', and
1558// 'relay' candidates.
1559// TODO(deadbeef): Remove this test when support for non-shared socket mode
1560// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001561TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001562 AddInterface(kClientAddr);
1563 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001564 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001565 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1566
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001567 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001568
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001569 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001570 session_->StartGettingPorts();
1571
1572 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1573 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001574 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1575 Candidate stun_candidate;
1576 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1577 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1578 Candidate turn_candidate;
1579 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1580 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1581 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001582 // Not using shared socket, so the STUN request's server reflexive address
1583 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001584 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001585
1586 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1587 EXPECT_EQ(3U, candidates_.size());
1588 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1589 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1590 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1591}
1592
1593// Test that even when both a STUN and TURN server are configured, the TURN
1594// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001595TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001596 AddInterface(kClientAddr);
1597 // Configure with STUN server but destroy it, so we can ensure that it's
1598 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001599 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001600 stun_server_.reset();
1601 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1602
1603 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001604 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1605 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001606
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001607 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001608 session_->StartGettingPorts();
1609
1610 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001611 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1612 Candidate stun_candidate;
1613 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1614 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1615 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1616 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1617 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001618
1619 // Don't bother waiting for STUN timeout, since we already verified
1620 // that we got a STUN candidate from the TURN server.
1621}
1622
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001623// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1624// and fail to generate STUN candidate, local UDP candidate is generated
1625// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001626TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001627 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1628 PORTALLOCATOR_DISABLE_TCP |
1629 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001630 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1631 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001632 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001633 session_->StartGettingPorts();
1634 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1635 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001636 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001637 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1638 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001639 EXPECT_EQ(1U, candidates_.size());
1640}
1641
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001642// Test that when the NetworkManager doesn't have permission to enumerate
1643// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1644// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001645TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001646 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1647 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001648 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001649 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001650 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1651 PORTALLOCATOR_DISABLE_TCP |
1652 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1653 EXPECT_EQ(0U,
1654 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1655 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1656 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001657 session_->StartGettingPorts();
1658 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001659 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001660 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001661 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001662}
1663
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001664// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001665TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001666 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1667 PORTALLOCATOR_ENABLE_IPV6 |
1668 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001669 AddInterface(kClientIPv6Addr);
1670 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001671 allocator_->set_step_delay(kMinimumStepDelay);
1672 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001673 session_->StartGettingPorts();
1674 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1675 EXPECT_EQ(4U, candidates_.size());
1676 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001677 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1678 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1679 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1680 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001681 EXPECT_EQ(4U, candidates_.size());
1682}
honghaiz98db68f2015-09-29 07:58:17 -07001683
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001684TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001685 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001686 allocator_->set_step_delay(kDefaultStepDelay);
1687 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001688 session_->StartGettingPorts();
1689 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1690 EXPECT_EQ(2U, ports_.size());
1691 session_->StopGettingPorts();
1692 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1693
1694 // After stopping getting ports, adding a new interface will not start
1695 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001696 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001697 candidates_.clear();
1698 ports_.clear();
1699 candidate_allocation_done_ = false;
1700 network_manager_.AddInterface(kClientAddr2);
1701 rtc::Thread::Current()->ProcessMessages(1000);
1702 EXPECT_EQ(0U, candidates_.size());
1703 EXPECT_EQ(0U, ports_.size());
1704}
1705
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001706TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001707 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001708 allocator_->set_step_delay(kDefaultStepDelay);
1709 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001710 session_->StartGettingPorts();
1711 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1712 EXPECT_EQ(2U, ports_.size());
1713 session_->ClearGettingPorts();
deadbeefb60a8192016-08-24 15:15:00 -07001714 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
honghaiz98db68f2015-09-29 07:58:17 -07001715
1716 // After clearing getting ports, adding a new interface will start getting
1717 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001718 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001719 candidates_.clear();
1720 ports_.clear();
1721 candidate_allocation_done_ = false;
1722 network_manager_.AddInterface(kClientAddr2);
1723 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1724 EXPECT_EQ(2U, ports_.size());
deadbeefb60a8192016-08-24 15:15:00 -07001725 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
honghaiz98db68f2015-09-29 07:58:17 -07001726}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001727
1728// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1729// a pooled session is taken out of the pool.
1730TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1731 AddInterface(kClientAddr);
1732 int pool_size = 1;
1733 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001734 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001735 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001736 ASSERT_NE(nullptr, peeked_session);
1737 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1738 kDefaultAllocationTimeout);
1739 // Expect that when TakePooledSession is called,
1740 // UpdateTransportInformationInternal will be called and the
1741 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1742 // candidates.
1743 session_ =
1744 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1745 ASSERT_NE(nullptr, session_.get());
1746 auto ready_ports = session_->ReadyPorts();
1747 auto candidates = session_->ReadyCandidates();
1748 EXPECT_FALSE(ready_ports.empty());
1749 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001750 for (const PortInterface* port_interface : ready_ports) {
1751 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001752 EXPECT_EQ(kContentName, port->content_name());
1753 EXPECT_EQ(1, port->component());
1754 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1755 EXPECT_EQ(kIcePwd0, port->password());
1756 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001757 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001758 EXPECT_EQ(1, candidate.component());
1759 EXPECT_EQ(kIceUfrag0, candidate.username());
1760 EXPECT_EQ(kIcePwd0, candidate.password());
1761 }
1762}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001763
1764// Test that a new candidate filter takes effect even on already-gathered
1765// candidates.
1766TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1767 AddInterface(kClientAddr);
1768 int pool_size = 1;
1769 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001770 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001771 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1772 ASSERT_NE(nullptr, peeked_session);
1773 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1774 kDefaultAllocationTimeout);
1775 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1776 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1777 allocator_->set_candidate_filter(CF_RELAY);
1778 // Assume that when TakePooledSession is called, the candidate filter will be
1779 // applied to the pooled session. This is tested by PortAllocatorTest.
1780 session_ =
1781 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1782 ASSERT_NE(nullptr, session_.get());
1783 auto candidates = session_->ReadyCandidates();
1784 auto ports = session_->ReadyPorts();
1785 // Sanity check that the number of candidates and ports decreased.
1786 EXPECT_GT(initial_candidates_size, candidates.size());
1787 EXPECT_GT(initial_ports_size, ports.size());
1788 for (const PortInterface* port : ports) {
1789 // Expect only relay ports.
1790 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1791 }
1792 for (const Candidate& candidate : candidates) {
1793 // Expect only relay candidates now that the filter is applied.
1794 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1795 // Expect that the raddr is emptied due to the CF_RELAY filter.
1796 EXPECT_EQ(candidate.related_address(),
1797 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1798 }
1799}
1800
1801} // namespace cricket