blob: 4ace7c8a3c20496f7038b7300888bca6db7d6c38 [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
deadbeefe97389c2016-12-23 01:43:45 -080040static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070042static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070043static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000045static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070046static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
47 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070048static const SocketAddress kClientIPv6Addr2(
49 "2401:fa00:4:2000:be30:5bff:fee5:c3",
50 0);
deadbeefc5d0d952015-07-16 10:22:21 -070051static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
52static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
54static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
55static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
56static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
57static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
58static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
59static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
60static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
61static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070062static const SocketAddress kTurnUdpIntIPv6Addr(
63 "2402:fb00:4:1000:be30:5bff:fee5:c3",
64 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070066static const SocketAddress kTurnTcpIntIPv6Addr(
67 "2402:fb00:4:2000:be30:5bff:fee5:c3",
68 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
70
71// Minimum and maximum port for port range tests.
72static const int kMinPort = 10000;
73static const int kMaxPort = 10099;
74
75// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070076static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077// Based on ICE_PWD_LENGTH
78static const char kIcePwd0[] = "TESTICEPWD00000000000000";
79
80static const char kContentName[] = "test content";
81
Honghai Zhangc67e0f52016-09-19 16:57:37 -070082static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083static const char kTurnUsername[] = "test";
84static const char kTurnPassword[] = "test";
85
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070086// STUN timeout (with all retries) is 9500ms.
87// Add some margin of error for slow bots.
88// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
89// fix flaky tests.
90static const int kStunTimeoutMs = 15000;
91
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092namespace cricket {
93
94// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070095std::ostream& operator<<(std::ostream& os,
96 const std::vector<Candidate>& candidates) {
97 os << '[';
98 bool first = true;
99 for (const Candidate& c : candidates) {
100 if (!first) {
101 os << ", ";
102 }
103 os << c.ToString();
104 first = false;
105 };
106 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 return os;
108}
109
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700110class BasicPortAllocatorTest : public testing::Test,
111 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700113 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000114 : pss_(new rtc::PhysicalSocketServer),
115 vss_(new rtc::VirtualSocketServer(pss_.get())),
116 fss_(new rtc::FirewallSocketServer(vss_.get())),
117 ss_scope_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800118 // Note that the NAT is not used by default. ResetWithStunServerAndNat
119 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700120 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700121 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700122 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700123 relay_server_(Thread::Current(),
124 kRelayUdpIntAddr,
125 kRelayUdpExtAddr,
126 kRelayTcpIntAddr,
127 kRelayTcpExtAddr,
128 kRelaySslTcpIntAddr,
129 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
131 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700132 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 stun_servers.insert(kStunAddr);
134 // Passing the addresses of GTURN servers will enable GTURN in
135 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800136 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
137 // configuration should be blank by default (preferred), or it should use
138 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700139 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
140 kRelayUdpIntAddr, kRelayTcpIntAddr,
141 kRelaySslTcpIntAddr));
142 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 }
144
145 void AddInterface(const SocketAddress& addr) {
146 network_manager_.AddInterface(addr);
147 }
honghaiz8c404fa2015-09-28 07:59:43 -0700148 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
149 network_manager_.AddInterface(addr, if_name);
150 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800151 void AddInterface(const SocketAddress& addr,
152 const std::string& if_name,
153 rtc::AdapterType type) {
154 network_manager_.AddInterface(addr, if_name, type);
155 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800156 // The default route is the public address that STUN server will observe when
157 // the endpoint is sitting on the public internet and the local port is bound
158 // to the "any" address. This may be different from the default local address
159 // which the endpoint observes. This can occur if the route to the public
160 // endpoint like 8.8.8.8 (specified as the default local address) is
161 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700162 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
163 AddInterface(addr);
164 // When a binding comes from the any address, the |addr| will be used as the
165 // srflx address.
166 vss_->SetDefaultRoute(addr.ipaddr());
167 }
honghaiz8c404fa2015-09-28 07:59:43 -0700168 void RemoveInterface(const SocketAddress& addr) {
169 network_manager_.RemoveInterface(addr);
170 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 bool SetPortRange(int min_port, int max_port) {
172 return allocator_->SetPortRange(min_port, max_port);
173 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700174 // Endpoint is on the public network. No STUN or TURN.
175 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700176 allocator_.reset(new BasicPortAllocator(&network_manager_));
177 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700178 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700179 // Endpoint is behind a NAT, with STUN specified.
180 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
181 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700182 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700183 // Endpoint is on the public network, with STUN specified.
184 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
185 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700187 // Endpoint is on the public network, with TURN specified.
188 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
189 const rtc::SocketAddress& tcp_turn) {
190 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 AddTurnServers(udp_turn, tcp_turn);
192 }
193
194 void AddTurnServers(const rtc::SocketAddress& udp_turn,
195 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700196 RelayServerConfig turn_server(RELAY_TURN);
197 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800198 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199
200 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800201 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 }
203 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800204 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 }
deadbeef653b8e02015-11-11 12:55:10 -0800206 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 }
208
209 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700210 session_ = CreateSession("session", component);
211 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700213 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 return true;
215 }
216
217 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700218 session_ = CreateSession("session", content_name, component);
219 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700221 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 return true;
223 }
224
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
226 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000227 return CreateSession(sid, kContentName, component);
228 }
229
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700230 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700231 const std::string& sid,
232 const std::string& content_name,
233 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
235 }
236
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700237 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700238 const std::string& sid,
239 const std::string& content_name,
240 int component,
241 const std::string& ice_ufrag,
242 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700243 std::unique_ptr<PortAllocatorSession> session =
244 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700246 &BasicPortAllocatorTest::OnPortReady);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700247 session->SignalPortsPruned.connect(this,
248 &BasicPortAllocatorTest::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700249 session->SignalCandidatesReady.connect(
250 this, &BasicPortAllocatorTest::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700251 session->SignalCandidatesRemoved.connect(
252 this, &BasicPortAllocatorTest::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700253 session->SignalCandidatesAllocationDone.connect(
254 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255 return session;
256 }
257
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700258 // Return true if the addresses are the same, or the port is 0 in |pattern|
259 // (acting as a wildcard) and the IPs are the same.
260 // Even with a wildcard port, the port of the address should be nonzero if
261 // the IP is nonzero.
262 static bool AddressMatch(const SocketAddress& address,
263 const SocketAddress& pattern) {
264 return address.ipaddr() == pattern.ipaddr() &&
265 ((pattern.port() == 0 &&
266 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
267 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700269
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700270 // Returns the number of ports that have matching type, protocol and
271 // address.
272 static int CountPorts(const std::vector<PortInterface*>& ports,
273 const std::string& type,
274 ProtocolType protocol,
275 const SocketAddress& client_addr) {
276 return std::count_if(
277 ports.begin(), ports.end(),
278 [type, protocol, client_addr](PortInterface* port) {
279 return port->Type() == type && port->GetProtocol() == protocol &&
280 port->Network()->GetBestIP() == client_addr.ipaddr();
281 });
282 }
283
deadbeefe97389c2016-12-23 01:43:45 -0800284 static int CountCandidates(const std::vector<Candidate>& candidates,
285 const std::string& type,
286 const std::string& proto,
287 const SocketAddress& addr) {
288 return std::count_if(candidates.begin(), candidates.end(),
289 [type, proto, addr](const Candidate& c) {
290 return c.type() == type && c.protocol() == proto &&
291 AddressMatch(c.address(), addr);
292 });
293 }
294
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700295 // Find a candidate and return it.
296 static bool FindCandidate(const std::vector<Candidate>& candidates,
297 const std::string& type,
298 const std::string& proto,
299 const SocketAddress& addr,
300 Candidate* found) {
301 auto it = std::find_if(candidates.begin(), candidates.end(),
302 [type, proto, addr](const Candidate& c) {
303 return c.type() == type && c.protocol() == proto &&
304 AddressMatch(c.address(), addr);
305 });
306 if (it != candidates.end() && found) {
307 *found = *it;
308 }
309 return it != candidates.end();
310 }
311
312 // Convenience method to call FindCandidate with no return.
313 static bool HasCandidate(const std::vector<Candidate>& candidates,
314 const std::string& type,
315 const std::string& proto,
316 const SocketAddress& addr) {
317 return FindCandidate(candidates, type, proto, addr, nullptr);
318 }
319
320 // Version of HasCandidate that also takes a related address.
321 static bool HasCandidateWithRelatedAddr(
322 const std::vector<Candidate>& candidates,
323 const std::string& type,
324 const std::string& proto,
325 const SocketAddress& addr,
326 const SocketAddress& related_addr) {
327 auto it =
328 std::find_if(candidates.begin(), candidates.end(),
329 [type, proto, addr, related_addr](const Candidate& c) {
330 return c.type() == type && c.protocol() == proto &&
331 AddressMatch(c.address(), addr) &&
332 AddressMatch(c.related_address(), related_addr);
333 });
334 return it != candidates.end();
335 }
336
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000337 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700338 int min_port,
339 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000340 return (addr.port() >= min_port && addr.port() <= max_port);
341 }
342
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700343 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 // We should only get this callback once, except in the mux test where
345 // we have multiple port allocation sessions.
346 if (session == session_.get()) {
347 ASSERT_FALSE(candidate_allocation_done_);
348 candidate_allocation_done_ = true;
349 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700350 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000351 }
352
353 // Check if all ports allocated have send-buffer size |expected|. If
354 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
355 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700356 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000357 for (it = ports_.begin(); it < ports_.end(); ++it) {
358 int send_buffer_size;
359 if (expected == -1) {
360 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700361 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000362 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700363 EXPECT_EQ(0,
364 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000365 ASSERT_EQ(expected, send_buffer_size);
366 }
367 }
368 }
369
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700370 // This function starts the port/address gathering and check the existence of
371 // candidates as specified. When |expect_stun_candidate| is true,
372 // |stun_candidate_addr| carries the expected reflective address, which is
373 // also the related address for TURN candidate if it is expected. Otherwise,
374 // it should be ignore.
375 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200376 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700377 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700378 const rtc::IPAddress& stun_candidate_addr,
379 const rtc::IPAddress& relay_candidate_udp_transport_addr,
380 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800381 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
382 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700383 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700384 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700385 }
386 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700387 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
388 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700389 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000390 session_->StartGettingPorts();
391 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
392
Peter Boström0c4e06b2015-10-07 12:23:21 +0200393 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700394 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700395 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800396 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700397 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700398 }
399 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700400 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800401 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700402 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800403 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700404 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
405 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700406 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700407 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700408 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700409 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
410 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
411 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700412 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700413 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700414 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700415 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
416 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
417 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700418 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700419 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000420
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700421 EXPECT_EQ(total_candidates, candidates_.size());
422 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000423 }
424
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700425 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
426
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000427 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700428 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000429
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700430 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000431 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
432 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700433 // Make sure the new port is added to ReadyPorts.
434 auto ready_ports = ses->ReadyPorts();
435 EXPECT_NE(ready_ports.end(),
436 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000437 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700438 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700439 const std::vector<PortInterface*>& pruned_ports) {
440 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700441 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700442 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700443 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700444 new_end = std::remove(ports_.begin(), new_end, port);
445 // Make sure the pruned port is not in ReadyPorts.
446 EXPECT_EQ(ready_ports.end(),
447 std::find(ready_ports.begin(), ready_ports.end(), port));
448 }
449 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700450 }
451
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700452 void OnCandidatesReady(PortAllocatorSession* ses,
453 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700454 for (const Candidate& candidate : candidates) {
455 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
456 // Sanity check that the ICE component is set.
457 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
458 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000459 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700460 // Make sure the new candidates are added to Candidates.
461 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700462 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700463 EXPECT_NE(
464 ses_candidates.end(),
465 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
466 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000467 }
468
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700469 void OnCandidatesRemoved(PortAllocatorSession* session,
470 const std::vector<Candidate>& removed_candidates) {
471 auto new_end = std::remove_if(
472 candidates_.begin(), candidates_.end(),
473 [removed_candidates](Candidate& candidate) {
474 for (const Candidate& removed_candidate : removed_candidates) {
475 if (candidate.MatchesForRemoval(removed_candidate)) {
476 return true;
477 }
478 }
479 return false;
480 });
481 candidates_.erase(new_end, candidates_.end());
482 }
483
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700484 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800485 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700486 RelayServerConfig server_config = allocator_->turn_servers()[i];
487 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000488 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700489 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000490 if (proto_addr.address == relay_port->address &&
491 proto_addr.proto == relay_port->proto)
492 return true;
493 }
494 }
495 return false;
496 }
497
Guo-wei Shieh11477022015-08-15 09:28:41 -0700498 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
499 bool with_nat) {
500 if (with_nat) {
501 nat_server_.reset(new rtc::NATServer(
502 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
503 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
504 } else {
505 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
506 }
507
508 ServerAddresses stun_servers;
509 if (!stun_server.IsNil()) {
510 stun_servers.insert(stun_server);
511 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700512 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700513 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700514 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700515 }
516
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700517 void TestUdpTurnPortPrunesTcpTurnPort() {
518 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
519 AddInterface(kClientAddr);
520 allocator_.reset(new BasicPortAllocator(&network_manager_));
521 allocator_->SetConfiguration(allocator_->stun_servers(),
522 allocator_->turn_servers(), 0, true);
523 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
524 allocator_->set_step_delay(kMinimumStepDelay);
525 allocator_->set_flags(allocator().flags() |
526 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
527 PORTALLOCATOR_DISABLE_TCP);
528
529 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
530 session_->StartGettingPorts();
531 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
532 // Only 2 ports (one STUN and one TURN) are actually being used.
533 EXPECT_EQ(2U, session_->ReadyPorts().size());
534 // We have verified that each port, when it is added to |ports_|, it is
535 // found in |ready_ports|, and when it is pruned, it is not found in
536 // |ready_ports|, so we only need to verify the content in one of them.
537 EXPECT_EQ(2U, ports_.size());
538 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
539 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
540 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
541
542 // Now that we remove candidates when a TURN port is pruned, |candidates_|
543 // should only contains two candidates regardless whether the TCP TURN port
544 // is created before or after the UDP turn port.
545 EXPECT_EQ(2U, candidates_.size());
546 // There will only be 2 candidates in |ready_candidates| because it only
547 // includes the candidates in the ready ports.
548 const std::vector<Candidate>& ready_candidates =
549 session_->ReadyCandidates();
550 EXPECT_EQ(2U, ready_candidates.size());
551 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
552 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
553 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
554 }
555
556 void TestIPv6TurnPortPrunesIPv4TurnPort() {
557 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
558 // Add two IP addresses on the same interface.
559 AddInterface(kClientAddr, "net1");
560 AddInterface(kClientIPv6Addr, "net1");
561 allocator_.reset(new BasicPortAllocator(&network_manager_));
562 allocator_->SetConfiguration(allocator_->stun_servers(),
563 allocator_->turn_servers(), 0, true);
564 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
565 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
566
567 allocator_->set_step_delay(kMinimumStepDelay);
568 allocator_->set_flags(
569 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
570 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
571
572 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
573 session_->StartGettingPorts();
574 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
575 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
576 EXPECT_EQ(3U, session_->ReadyPorts().size());
577 EXPECT_EQ(3U, ports_.size());
578 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
579 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
580 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
581 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
582
583 // Now that we remove candidates when a TURN port is pruned, there will be
584 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
585 EXPECT_EQ(3U, candidates_.size());
586 const std::vector<Candidate>& ready_candidates =
587 session_->ReadyCandidates();
588 EXPECT_EQ(3U, ready_candidates.size());
589 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
590 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
591 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
592 }
593
594 void TestEachInterfaceHasItsOwnTurnPorts() {
595 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
596 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
597 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
598 // Add two interfaces both having IPv4 and IPv6 addresses.
599 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
600 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
601 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
602 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
603 allocator_.reset(new BasicPortAllocator(&network_manager_));
604 allocator_->SetConfiguration(allocator_->stun_servers(),
605 allocator_->turn_servers(), 0, true);
606 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
607 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
608 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
609
610 allocator_->set_step_delay(kMinimumStepDelay);
611 allocator_->set_flags(allocator().flags() |
612 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
613 PORTALLOCATOR_ENABLE_IPV6);
614 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
615 session_->StartGettingPorts();
616 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
617 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
618 // use.
619 EXPECT_EQ(10U, session_->ReadyPorts().size());
620 EXPECT_EQ(10U, ports_.size());
621 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
622 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
623 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
624 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
625 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
626 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
627 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
628 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
629 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
630 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
631
632 // Now that we remove candidates when TURN ports are pruned, there will be
633 // exactly 10 candidates in |candidates_|.
634 EXPECT_EQ(10U, candidates_.size());
635 const std::vector<Candidate>& ready_candidates =
636 session_->ReadyCandidates();
637 EXPECT_EQ(10U, ready_candidates.size());
638 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
639 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
640 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
641 kClientIPv6Addr);
642 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
643 kClientIPv6Addr2);
644 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
645 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
646 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
647 kClientIPv6Addr);
648 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
649 kClientIPv6Addr2);
650 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
651 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
652 }
653
kwiberg3ec46792016-04-27 07:22:53 -0700654 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
655 std::unique_ptr<rtc::VirtualSocketServer> vss_;
656 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000657 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700658 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000659 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700660 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700661 std::unique_ptr<TestStunServer> stun_server_;
662 TestRelayServer relay_server_;
663 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000664 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700665 std::unique_ptr<BasicPortAllocator> allocator_;
666 std::unique_ptr<PortAllocatorSession> session_;
667 std::vector<PortInterface*> ports_;
668 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000669 bool candidate_allocation_done_;
670};
671
672// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700673TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000674 EXPECT_EQ(&network_manager_, allocator().network_manager());
675 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800676 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700677 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000678 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800679 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
680 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700681 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
682 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
683 EXPECT_TRUE(
684 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
685 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700686 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000687}
688
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800689// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700690TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
692 rtc::ADAPTER_TYPE_ETHERNET);
693 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
694 rtc::ADAPTER_TYPE_WIFI);
695 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
696 rtc::ADAPTER_TYPE_CELLULAR);
697 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
698 rtc::ADAPTER_TYPE_VPN);
699 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
700 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700701 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
702 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
703 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800704 session_->StartGettingPorts();
705 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
706 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700707 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 EXPECT_LT(candidate.address().ip(), 0x12345604U);
709 }
710}
711
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700712TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800713 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
714 rtc::ADAPTER_TYPE_ETHERNET);
715 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
716 rtc::ADAPTER_TYPE_WIFI);
717 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
718 rtc::ADAPTER_TYPE_CELLULAR);
719 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
720 rtc::ADAPTER_TYPE_LOOPBACK |
721 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700722 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
723 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
724 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800725 session_->StartGettingPorts();
726 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
727 EXPECT_EQ(1U, candidates_.size());
728 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
729}
730
honghaiz60347052016-05-31 18:29:12 -0700731// Test that high cost networks are filtered if the flag
732// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
733TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
734 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
735 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
736 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
737 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
738 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
739 // interface.
740 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
741 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
742 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
743 cricket::PORTALLOCATOR_DISABLE_RELAY |
744 cricket::PORTALLOCATOR_DISABLE_TCP |
745 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
746 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
747 session_->StartGettingPorts();
748 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
749 EXPECT_EQ(1U, candidates_.size());
750 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
751
752 // If both cellular and unknown interfaces are present, only gather on the
753 // unknown interfaces.
754 candidates_.clear();
755 candidate_allocation_done_ = false;
756 RemoveInterface(addr_wifi);
757 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
758 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
759 session_->StartGettingPorts();
760 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
761 EXPECT_EQ(2U, candidates_.size());
762 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
763 addr_unknown2.EqualIPs(candidates_[1].address())) ||
764 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
765 addr_unknown2.EqualIPs(candidates_[0].address())));
766
767 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
768 // Wi-Fi interface.
769 candidates_.clear();
770 candidate_allocation_done_ = false;
771 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
772 session_->StartGettingPorts();
773 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
774 EXPECT_EQ(1U, candidates_.size());
775 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
776}
777
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700778// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700779TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800780 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
781 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700782 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
783 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
784 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700785 session_->StartGettingPorts();
786 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
787 EXPECT_EQ(1U, candidates_.size());
788}
789
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000790// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700791TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000792 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700793 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000794 session_->StartGettingPorts();
795 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
796 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700797 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
798 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
799 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
800 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
801 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
802 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
803 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
804 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000805 EXPECT_TRUE(candidate_allocation_done_);
806}
807
honghaiz8c404fa2015-09-28 07:59:43 -0700808// Test that when the same network interface is brought down and up, the
809// port allocator session will restart a new allocation sequence if
810// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700811TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700812 std::string if_name("test_net0");
813 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700814 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700815 session_->StartGettingPorts();
816 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
817 EXPECT_EQ(4U, ports_.size());
818 EXPECT_TRUE(candidate_allocation_done_);
819 candidate_allocation_done_ = false;
820 candidates_.clear();
821 ports_.clear();
822
823 RemoveInterface(kClientAddr);
824 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
825 EXPECT_EQ(0U, ports_.size());
826 EXPECT_FALSE(candidate_allocation_done_);
827
828 // When the same interfaces are added again, new candidates/ports should be
829 // generated.
830 AddInterface(kClientAddr, if_name);
831 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
832 EXPECT_EQ(4U, ports_.size());
833 EXPECT_TRUE(candidate_allocation_done_);
834}
835
836// Test that when the same network interface is brought down and up, the
837// port allocator session will not restart a new allocation sequence if
838// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700839TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700840 std::string if_name("test_net0");
841 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700842 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700843 session_->StartGettingPorts();
844 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
845 EXPECT_EQ(4U, ports_.size());
846 EXPECT_TRUE(candidate_allocation_done_);
847 session_->StopGettingPorts();
848 candidates_.clear();
849 ports_.clear();
850
851 RemoveInterface(kClientAddr);
852 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
853 EXPECT_EQ(0U, ports_.size());
854
855 // When the same interfaces are added again, new candidates/ports should not
856 // be generated because the session has stopped.
857 AddInterface(kClientAddr, if_name);
858 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
859 EXPECT_EQ(0U, ports_.size());
860 EXPECT_TRUE(candidate_allocation_done_);
861}
862
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000863// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700864TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000865 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700866 allocator_->set_step_delay(kDefaultStepDelay);
867 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000868 session_->StartGettingPorts();
869 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
870 EXPECT_EQ(2U, ports_.size());
871 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
872 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700873 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
874 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000875 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700876 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
877 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000878 EXPECT_EQ(4U, ports_.size());
879 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700880 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
881 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000882 EXPECT_EQ(4U, ports_.size());
883 EXPECT_TRUE(candidate_allocation_done_);
884 // If we Stop gathering now, we shouldn't get a second "done" callback.
885 session_->StopGettingPorts();
886}
887
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700888TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000889 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700890 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000891 session_->StartGettingPorts();
892 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
893 EXPECT_TRUE(candidate_allocation_done_);
894 // If we Stop gathering now, we shouldn't get a second "done" callback.
895 session_->StopGettingPorts();
896
897 // All ports should have unset send-buffer sizes.
898 CheckSendBufferSizesOfAllPorts(-1);
899}
900
901// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700902TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700904 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000905 session_->StartGettingPorts();
906 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
907 EXPECT_EQ(2U, ports_.size());
908 session_->StopGettingPorts();
909 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
910}
911
912// Test that we restrict client ports appropriately when a port range is set.
913// We check the candidates for udp/stun/tcp ports, and the from address
914// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700915TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000916 AddInterface(kClientAddr);
917 // Check that an invalid port range fails.
918 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
919 // Check that a null port range succeeds.
920 EXPECT_TRUE(SetPortRange(0, 0));
921 // Check that a valid port range succeeds.
922 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700923 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000924 session_->StartGettingPorts();
925 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
926 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -0700927
928 int num_nonrelay_candidates = 0;
929 for (const Candidate& candidate : candidates_) {
930 // Check the port number for the UDP/STUN/TCP port objects.
931 if (candidate.type() != RELAY_PORT_TYPE) {
932 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
933 ++num_nonrelay_candidates;
934 }
935 }
936 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000937 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700938 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
939 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940 EXPECT_TRUE(candidate_allocation_done_);
941}
942
deadbeefe97389c2016-12-23 01:43:45 -0800943// Test that if we have no network adapters, we bind to the ANY address and
944// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700945TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -0800946 // Default config uses GTURN and no NAT, so replace that with the
947 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
948 ResetWithStunServerAndNat(kStunAddr);
949 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
950 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
951 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
952 // Disable IPv6, because our test infrastructure doesn't support having IPv4
953 // behind a NAT but IPv6 not, or having an IPv6 NAT.
954 // TODO(deadbeef): Fix this.
955 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700956 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000957 session_->StartGettingPorts();
deadbeefe97389c2016-12-23 01:43:45 -0800958 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
959 EXPECT_EQ(4U, ports_.size());
960 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
961 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
962 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
963 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
964 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
965 // The "any" address port should be in the signaled ready ports, but the host
966 // candidate for it is useless and shouldn't be signaled. So we only have
967 // STUN/TURN candidates.
968 EXPECT_EQ(3U, candidates_.size());
969 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
970 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
971 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
972 // server.
973 EXPECT_EQ(2,
974 CountCandidates(candidates_, "relay", "udp",
975 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000976}
977
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700978// Test that when enumeration is disabled, we should not have any ports when
979// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700980TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700981 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700982 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700983 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700984 // Expect to see no ports and no candidates.
985 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
986 rtc::IPAddress(), rtc::IPAddress());
987}
988
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800989// Test that even with multiple interfaces, the result should still be a single
990// default private, one STUN and one TURN candidate since we bind to any address
991// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700992TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700993 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000994 AddInterface(kPrivateAddr);
995 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700996 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000997 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800998
999 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1000 // address set and we have no IPv6 STUN server, there should be no IPv6
1001 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001002 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1003 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001004
1005 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1006 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1007 // TURN/UDP candidates.
1008 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001009 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1010 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001011}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001012
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001013// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1014// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001015TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001016 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001017 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001018 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001019 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001020 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1021 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1022 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1023 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001024 kTurnUdpExtAddr.ipaddr());
1025}
1026
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001027// Test that when adapter enumeration is disabled, for endpoints without
1028// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001029TEST_F(BasicPortAllocatorTest,
1030 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001031 ResetWithNoServersOrNat();
1032 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1033 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1034 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001035}
1036
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001037// Test that when adapter enumeration is disabled, with
1038// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1039// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001040TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001041 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1042 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001043 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1044 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001045 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1046 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001047 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001048 rtc::IPAddress(), rtc::IPAddress());
1049}
1050
1051// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001052// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1053// a NAT, there is no local candidate. However, this specified default route
1054// (kClientAddr) which was discovered when sending STUN requests, will become
1055// the srflx addresses.
1056TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001057 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001058 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001059 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001060 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001061 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1062 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001063 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1064 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001065 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1066 rtc::IPAddress(), rtc::IPAddress());
1067}
1068
1069// Test that when adapter enumeration is disabled, with
1070// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1071// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001072TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001073 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1074 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001075 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1076 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001077 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1078 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1079 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001080}
1081
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001082// Test that we disable relay over UDP, and only TCP is used when connecting to
1083// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001084TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001085 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001086 AddInterface(kClientAddr);
1087 ResetWithStunServerAndNat(kStunAddr);
1088 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001089 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1090 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1091 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1092 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001093
1094 session_->StartGettingPorts();
1095 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1096
1097 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1098 // TURN/TCP candidates.
1099 EXPECT_EQ(2U, ports_.size());
1100 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001101 Candidate turn_candidate;
1102 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1103 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001104 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001105 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1106 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001107}
1108
Erik Språngefdce692015-06-05 09:41:26 +02001109// Disable for asan, see
1110// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1111#if !defined(ADDRESS_SANITIZER)
1112
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001113// Test that we can get OnCandidatesAllocationDone callback when all the ports
1114// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001115TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001116 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001117 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1118 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1119 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001120 session_->StartGettingPorts();
1121 rtc::Thread::Current()->ProcessMessages(100);
1122 EXPECT_EQ(0U, candidates_.size());
1123 EXPECT_TRUE(candidate_allocation_done_);
1124}
1125
1126// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001127TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001128 AddInterface(kClientAddr);
1129 fss_->set_udp_sockets_enabled(false);
1130 EXPECT_TRUE(CreateSession(1));
1131 session_->StartGettingPorts();
1132 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1133 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001134 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1135 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1136 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1137 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1138 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1139 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 EXPECT_TRUE(candidate_allocation_done_);
1141}
1142
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001143#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001144
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145// Test that we don't crash or malfunction if we can't create UDP sockets or
1146// listen on TCP sockets. We still give out a local TCP address, since
1147// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001148TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149 AddInterface(kClientAddr);
1150 fss_->set_udp_sockets_enabled(false);
1151 fss_->set_tcp_listen_enabled(false);
1152 EXPECT_TRUE(CreateSession(1));
1153 session_->StartGettingPorts();
1154 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
1155 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001156 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1157 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1158 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1159 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1160 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001161 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162 EXPECT_TRUE(candidate_allocation_done_);
1163}
1164
1165// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001166// TODO(deadbeef): Find a way to exit early here.
1167TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001168 AddInterface(kClientAddr);
1169 fss_->set_tcp_sockets_enabled(false);
1170 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001171 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 session_->StartGettingPorts();
1173 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001174 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001175 // In case of Relay, ports creation will succeed but sockets will fail.
1176 // There is no error reporting from RelayEntry to handle this failure.
1177}
1178
1179// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001180TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001181 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1182 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001183 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001184 session_->StartGettingPorts();
1185 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1186 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001187 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1188 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
1190 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -07001191 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
1192 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001193 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001194 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1195 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1196 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1197 kRelaySslTcpIntAddr);
1198 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -07001199 // Stun Timeout is 9.5sec.
1200 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
1201 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001202}
1203
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001204TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001205 AddInterface(kClientAddr);
1206 AddInterface(kClientAddr2);
1207 // Allocating only host UDP ports. This is done purely for testing
1208 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001209 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1210 PORTALLOCATOR_DISABLE_RELAY);
1211 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212 session_->StartGettingPorts();
1213 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1214 ASSERT_EQ(2U, candidates_.size());
1215 EXPECT_EQ(2U, ports_.size());
1216 // Candidates priorities should be different.
1217 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1218}
1219
1220// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001221TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001222 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001223 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001224 session_->StartGettingPorts();
1225 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1226 EXPECT_EQ(4U, ports_.size());
1227 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001228 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001229}
1230
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001231// Test that the allocator session uses the candidate filter it's created with,
1232// rather than the filter of its parent allocator.
1233// The filter of the allocator should only affect the next gathering phase,
1234// according to JSEP, which means the *next* allocator session returned.
1235TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1236 AddInterface(kClientAddr);
1237 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1238 // Set candidate filter *after* creating the session. Should have no effect.
1239 allocator().set_candidate_filter(CF_RELAY);
1240 session_->StartGettingPorts();
1241 // 7 candidates and 4 ports is what we would normally get (see the
1242 // TestGetAllPorts* tests).
1243 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1244 EXPECT_EQ(4U, ports_.size());
1245 EXPECT_TRUE(candidate_allocation_done_);
1246}
1247
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001248// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1249// This test also verifies that when the allocator is only allowed to use
1250// relay (i.e. IceTransportsType is relay), the raddr is an empty
1251// address with the correct family. This is to prevent any local
1252// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001253TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001254 AddInterface(kClientAddr);
1255 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001256 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001257 allocator().set_candidate_filter(CF_RELAY);
1258 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001259 session_->StartGettingPorts();
1260 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001261 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1262 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001263
1264 EXPECT_EQ(1U, candidates_.size());
1265 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001266 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1267 EXPECT_EQ(
1268 candidates_[0].related_address(),
1269 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001270}
1271
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001272TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001273 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001274 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1275 allocator().set_candidate_filter(CF_HOST);
1276 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001277 session_->StartGettingPorts();
1278 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001279 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1280 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001281 for (const Candidate& candidate : candidates_) {
1282 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001283 }
1284}
1285
1286// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001287TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001288 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001289 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001290
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001291 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1292 allocator().set_candidate_filter(CF_REFLEXIVE);
1293 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001294 session_->StartGettingPorts();
1295 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1296 // Host is behind NAT, no private address will be exposed. Hence only UDP
1297 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001298 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1299 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001300 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1301 EXPECT_EQ(
1302 candidates_[0].related_address(),
1303 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001304}
1305
1306// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001307TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001308 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001309 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1310 allocator().set_candidate_filter(CF_REFLEXIVE);
1311 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001312 session_->StartGettingPorts();
1313 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1314 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001315 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001317 for (const Candidate& candidate : candidates_) {
1318 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001319 }
1320}
1321
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001322// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001323TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001324 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001325 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001326 session_->StartGettingPorts();
1327 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001328 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1329 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1330 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001331 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001332 for (const Candidate& candidate : candidates_) {
1333 EXPECT_EQ(kIceUfrag0, candidate.username());
1334 EXPECT_EQ(kIcePwd0, candidate.password());
1335 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001336 EXPECT_TRUE(candidate_allocation_done_);
1337}
1338
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001339// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1340// is allocated for udp and stun. Also verify there is only one candidate
1341// (local) if stun candidate is same as local candidate, which will be the case
1342// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001343TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001344 AddInterface(kClientAddr);
1345 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001346 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1347 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001348 session_->StartGettingPorts();
1349 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1350 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001351 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1353}
1354
1355// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1356// is allocated for udp and stun. In this test we should expect both stun and
1357// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001358TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001359 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001360 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001361
1362 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001363 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1364 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001365 session_->StartGettingPorts();
1366 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1367 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001368 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1369 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1370 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001371 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1372 EXPECT_EQ(3U, candidates_.size());
1373}
1374
deadbeefc5d0d952015-07-16 10:22:21 -07001375// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001376TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001377 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001378 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001379 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001380
1381 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1382
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001383 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001384 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001385 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1386 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001387
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001388 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001389 session_->StartGettingPorts();
1390
1391 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1392 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001393 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1394 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1395 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1396 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1397 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1399 EXPECT_EQ(3U, candidates_.size());
1400}
1401
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001402// Test that if prune_turn_ports is set, TCP TURN port will not be used
1403// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1404TEST_F(BasicPortAllocatorTest,
1405 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1406 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1407 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1408 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001409
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001410 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001411}
1412
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001413// Test that if prune_turn_ports is set, TCP TURN port will not be used
1414// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1415TEST_F(BasicPortAllocatorTest,
1416 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1417 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1418 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1419 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001420
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001421 TestUdpTurnPortPrunesTcpTurnPort();
1422}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001423
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001424// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1425// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1426TEST_F(BasicPortAllocatorTest,
1427 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1428 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1429 // first.
1430 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1431 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001432
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001433 TestIPv6TurnPortPrunesIPv4TurnPort();
1434}
1435
1436// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1437// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1438TEST_F(BasicPortAllocatorTest,
1439 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1440 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1441 // first.
1442 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1443 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1444
1445 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001446}
1447
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001448// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001449// will has its own set of TurnPorts based on their priorities, in the default
1450// case where no transit delay is set.
1451TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1452 TestEachInterfaceHasItsOwnTurnPorts();
1453}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001454
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001455// Tests that if prune_turn_ports is set, each network interface
1456// will has its own set of TurnPorts based on their priorities, given that
1457// IPv4/TCP TURN port becomes ready first.
1458TEST_F(BasicPortAllocatorTest,
1459 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1460 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1461 // becomes ready last.
1462 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1463 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1464 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1465 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001466
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001467 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001468}
1469
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470// Testing DNS resolve for the TURN server, this will test AllocationSequence
1471// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001472TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001473 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001474 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001475 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001476 allocator_.reset(new BasicPortAllocator(&network_manager_));
1477 RelayServerConfig turn_server(RELAY_TURN);
1478 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001479 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001480 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001481 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001482 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001483
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001484 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001485 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001486 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1487 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001488
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001489 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001490 session_->StartGettingPorts();
1491
1492 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1493}
1494
1495// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1496// is allocated for udp/stun/turn. In this test we should expect all local,
1497// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001498TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001499 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001500 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001501
1502 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1503
1504 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001505 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1506 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001507
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001508 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001509 session_->StartGettingPorts();
1510
1511 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1512 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001513 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1514 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1515 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1516 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1517 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001518 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1519 EXPECT_EQ(3U, candidates_.size());
1520 // Local port will be created first and then TURN port.
1521 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1522 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1523}
1524
1525// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1526// server is also used as the STUN server, we should get 'local', 'stun', and
1527// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001528TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001529 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001530 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001531 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001532 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1533
1534 // Must set the step delay to 0 to make sure the relay allocation phase is
1535 // started before the STUN candidates are obtained, so that the STUN binding
1536 // response is processed when both StunPort and TurnPort exist to reproduce
1537 // webrtc issue 3537.
1538 allocator_->set_step_delay(0);
1539 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001540 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1541 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001542
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001543 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001544 session_->StartGettingPorts();
1545
1546 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001547 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1548 Candidate stun_candidate;
1549 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1550 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1551 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1552 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1553 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001554
1555 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1556 EXPECT_EQ(3U, candidates_.size());
1557 // Local port will be created first and then TURN port.
1558 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1559 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1560}
1561
deadbeefc5d0d952015-07-16 10:22:21 -07001562// Test that when only a TCP TURN server is available, we do NOT use it as
1563// a UDP STUN server, as this could leak our IP address. Thus we should only
1564// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001565TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001566 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001567 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001568 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001569 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1570
1571 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001572 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1573 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001574
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001575 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001576 session_->StartGettingPorts();
1577
1578 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1579 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001580 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1581 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1582 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001583 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1584 EXPECT_EQ(2U, candidates_.size());
1585 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1586 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1587}
1588
1589// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1590// TURN server is used as the STUN server and we get 'local', 'stun', and
1591// 'relay' candidates.
1592// TODO(deadbeef): Remove this test when support for non-shared socket mode
1593// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001594TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001595 AddInterface(kClientAddr);
1596 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001597 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001598 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1599
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001600 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001601
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001602 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001603 session_->StartGettingPorts();
1604
1605 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1606 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001607 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1608 Candidate stun_candidate;
1609 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1610 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1611 Candidate turn_candidate;
1612 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1613 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1614 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001615 // Not using shared socket, so the STUN request's server reflexive address
1616 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001617 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001618
1619 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1620 EXPECT_EQ(3U, candidates_.size());
1621 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1622 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1623 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1624}
1625
1626// Test that even when both a STUN and TURN server are configured, the TURN
1627// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001628TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001629 AddInterface(kClientAddr);
1630 // Configure with STUN server but destroy it, so we can ensure that it's
1631 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001632 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001633 stun_server_.reset();
1634 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1635
1636 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001637 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1638 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001639
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001640 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001641 session_->StartGettingPorts();
1642
1643 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001644 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1645 Candidate stun_candidate;
1646 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1647 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1648 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1649 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1650 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001651
1652 // Don't bother waiting for STUN timeout, since we already verified
1653 // that we got a STUN candidate from the TURN server.
1654}
1655
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001656// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1657// and fail to generate STUN candidate, local UDP candidate is generated
1658// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001659TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001660 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1661 PORTALLOCATOR_DISABLE_TCP |
1662 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001663 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1664 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001665 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001666 session_->StartGettingPorts();
1667 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1668 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001669 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001670 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1671 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001672 EXPECT_EQ(1U, candidates_.size());
1673}
1674
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001675// Test that when the NetworkManager doesn't have permission to enumerate
1676// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1677// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001678TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001679 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1680 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001681 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001682 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001683 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1684 PORTALLOCATOR_DISABLE_TCP |
1685 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1686 EXPECT_EQ(0U,
1687 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1688 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1689 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001690 session_->StartGettingPorts();
1691 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001692 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001693 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001694 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001695}
1696
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001697// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001698TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001699 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1700 PORTALLOCATOR_ENABLE_IPV6 |
1701 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001702 AddInterface(kClientIPv6Addr);
1703 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001704 allocator_->set_step_delay(kMinimumStepDelay);
1705 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001706 session_->StartGettingPorts();
1707 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1708 EXPECT_EQ(4U, candidates_.size());
1709 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001710 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1711 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1712 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1713 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001714 EXPECT_EQ(4U, candidates_.size());
1715}
honghaiz98db68f2015-09-29 07:58:17 -07001716
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001717TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001718 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001719 allocator_->set_step_delay(kDefaultStepDelay);
1720 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001721 session_->StartGettingPorts();
1722 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1723 EXPECT_EQ(2U, ports_.size());
1724 session_->StopGettingPorts();
1725 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1726
1727 // After stopping getting ports, adding a new interface will not start
1728 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001729 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001730 candidates_.clear();
1731 ports_.clear();
1732 candidate_allocation_done_ = false;
1733 network_manager_.AddInterface(kClientAddr2);
1734 rtc::Thread::Current()->ProcessMessages(1000);
1735 EXPECT_EQ(0U, candidates_.size());
1736 EXPECT_EQ(0U, ports_.size());
1737}
1738
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001739TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001740 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001741 allocator_->set_step_delay(kDefaultStepDelay);
1742 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001743 session_->StartGettingPorts();
1744 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1745 EXPECT_EQ(2U, ports_.size());
1746 session_->ClearGettingPorts();
deadbeefb60a8192016-08-24 15:15:00 -07001747 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
honghaiz98db68f2015-09-29 07:58:17 -07001748
1749 // After clearing getting ports, adding a new interface will start getting
1750 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001751 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001752 candidates_.clear();
1753 ports_.clear();
1754 candidate_allocation_done_ = false;
1755 network_manager_.AddInterface(kClientAddr2);
1756 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1757 EXPECT_EQ(2U, ports_.size());
deadbeefb60a8192016-08-24 15:15:00 -07001758 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
honghaiz98db68f2015-09-29 07:58:17 -07001759}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001760
1761// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1762// a pooled session is taken out of the pool.
1763TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1764 AddInterface(kClientAddr);
1765 int pool_size = 1;
1766 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001767 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001768 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001769 ASSERT_NE(nullptr, peeked_session);
1770 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1771 kDefaultAllocationTimeout);
1772 // Expect that when TakePooledSession is called,
1773 // UpdateTransportInformationInternal will be called and the
1774 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1775 // candidates.
1776 session_ =
1777 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1778 ASSERT_NE(nullptr, session_.get());
1779 auto ready_ports = session_->ReadyPorts();
1780 auto candidates = session_->ReadyCandidates();
1781 EXPECT_FALSE(ready_ports.empty());
1782 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001783 for (const PortInterface* port_interface : ready_ports) {
1784 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001785 EXPECT_EQ(kContentName, port->content_name());
1786 EXPECT_EQ(1, port->component());
1787 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1788 EXPECT_EQ(kIcePwd0, port->password());
1789 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001790 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001791 EXPECT_EQ(1, candidate.component());
1792 EXPECT_EQ(kIceUfrag0, candidate.username());
1793 EXPECT_EQ(kIcePwd0, candidate.password());
1794 }
1795}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001796
1797// Test that a new candidate filter takes effect even on already-gathered
1798// candidates.
1799TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1800 AddInterface(kClientAddr);
1801 int pool_size = 1;
1802 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001803 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001804 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1805 ASSERT_NE(nullptr, peeked_session);
1806 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1807 kDefaultAllocationTimeout);
1808 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1809 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1810 allocator_->set_candidate_filter(CF_RELAY);
1811 // Assume that when TakePooledSession is called, the candidate filter will be
1812 // applied to the pooled session. This is tested by PortAllocatorTest.
1813 session_ =
1814 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1815 ASSERT_NE(nullptr, session_.get());
1816 auto candidates = session_->ReadyCandidates();
1817 auto ports = session_->ReadyPorts();
1818 // Sanity check that the number of candidates and ports decreased.
1819 EXPECT_GT(initial_candidates_size, candidates.size());
1820 EXPECT_GT(initial_ports_size, ports.size());
1821 for (const PortInterface* port : ports) {
1822 // Expect only relay ports.
1823 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1824 }
1825 for (const Candidate& candidate : candidates) {
1826 // Expect only relay candidates now that the filter is applied.
1827 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1828 // Expect that the raddr is emptied due to the CF_RELAY filter.
1829 EXPECT_EQ(candidate.related_address(),
1830 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1831 }
1832}
1833
1834} // namespace cricket