blob: 0bc68b2f0363c7808ce920ea5efc9447bc32268e [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/basicpacketsocketfactory.h"
kjellanderf4752772016-03-02 05:42:30 -080015#include "webrtc/p2p/base/p2pconstants.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/testrelayserver.h"
18#include "webrtc/p2p/base/teststunserver.h"
19#include "webrtc/p2p/base/testturnserver.h"
20#include "webrtc/p2p/client/basicportallocator.h"
21#include "webrtc/p2p/client/httpportallocator.h"
22#include "webrtc/base/fakenetwork.h"
23#include "webrtc/base/firewallsocketserver.h"
24#include "webrtc/base/gunit.h"
25#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070026#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/natserver.h"
29#include "webrtc/base/natsocketfactory.h"
30#include "webrtc/base/network.h"
31#include "webrtc/base/physicalsocketserver.h"
32#include "webrtc/base/socketaddress.h"
33#include "webrtc/base/ssladapter.h"
34#include "webrtc/base/thread.h"
35#include "webrtc/base/virtualsocketserver.h"
36
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080037using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038using rtc::SocketAddress;
39using rtc::Thread;
40
41static const SocketAddress kClientAddr("11.11.11.11", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070042static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000044static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070045static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
46 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070048static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
49static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
51static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
52static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
53static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
54static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
55static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
56static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
57static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
58static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
59static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
60static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
61
62// Minimum and maximum port for port range tests.
63static const int kMinPort = 10000;
64static const int kMaxPort = 10099;
65
66// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070067static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068// Based on ICE_PWD_LENGTH
69static const char kIcePwd0[] = "TESTICEPWD00000000000000";
70
71static const char kContentName[] = "test content";
72
73static const int kDefaultAllocationTimeout = 1000;
74static const char kTurnUsername[] = "test";
75static const char kTurnPassword[] = "test";
76
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070077// STUN timeout (with all retries) is 9500ms.
78// Add some margin of error for slow bots.
79// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
80// fix flaky tests.
81static const int kStunTimeoutMs = 15000;
82
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083namespace cricket {
84
85// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070086std::ostream& operator<<(std::ostream& os,
87 const std::vector<Candidate>& candidates) {
88 os << '[';
89 bool first = true;
90 for (const Candidate& c : candidates) {
91 if (!first) {
92 os << ", ";
93 }
94 os << c.ToString();
95 first = false;
96 };
97 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 return os;
99}
100
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700101class BasicPortAllocatorTest : public testing::Test,
102 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700104 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105 : pss_(new rtc::PhysicalSocketServer),
106 vss_(new rtc::VirtualSocketServer(pss_.get())),
107 fss_(new rtc::FirewallSocketServer(vss_.get())),
108 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700109 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700110 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700111 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700112 relay_server_(Thread::Current(),
113 kRelayUdpIntAddr,
114 kRelayUdpExtAddr,
115 kRelayTcpIntAddr,
116 kRelayTcpExtAddr,
117 kRelaySslTcpIntAddr,
118 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
120 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700121 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 stun_servers.insert(kStunAddr);
123 // Passing the addresses of GTURN servers will enable GTURN in
124 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700125 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
126 kRelayUdpIntAddr, kRelayTcpIntAddr,
127 kRelaySslTcpIntAddr));
128 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 }
130
131 void AddInterface(const SocketAddress& addr) {
132 network_manager_.AddInterface(addr);
133 }
honghaiz8c404fa2015-09-28 07:59:43 -0700134 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
135 network_manager_.AddInterface(addr, if_name);
136 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800137 void AddInterface(const SocketAddress& addr,
138 const std::string& if_name,
139 rtc::AdapterType type) {
140 network_manager_.AddInterface(addr, if_name, type);
141 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800142 // The default route is the public address that STUN server will observe when
143 // the endpoint is sitting on the public internet and the local port is bound
144 // to the "any" address. This may be different from the default local address
145 // which the endpoint observes. This can occur if the route to the public
146 // endpoint like 8.8.8.8 (specified as the default local address) is
147 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700148 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
149 AddInterface(addr);
150 // When a binding comes from the any address, the |addr| will be used as the
151 // srflx address.
152 vss_->SetDefaultRoute(addr.ipaddr());
153 }
honghaiz8c404fa2015-09-28 07:59:43 -0700154 void RemoveInterface(const SocketAddress& addr) {
155 network_manager_.RemoveInterface(addr);
156 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157 bool SetPortRange(int min_port, int max_port) {
158 return allocator_->SetPortRange(min_port, max_port);
159 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700160 // Endpoint is on the public network. No STUN or TURN.
161 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700162 allocator_.reset(new BasicPortAllocator(&network_manager_));
163 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700164 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700165 // Endpoint is behind a NAT, with STUN specified.
166 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
167 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700168 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700169 // Endpoint is on the public network, with STUN specified.
170 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
171 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700173 // Endpoint is on the public network, with TURN specified.
174 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
175 const rtc::SocketAddress& tcp_turn) {
176 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 AddTurnServers(udp_turn, tcp_turn);
178 }
179
180 void AddTurnServers(const rtc::SocketAddress& udp_turn,
181 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700182 RelayServerConfig turn_server(RELAY_TURN);
183 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800184 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185
186 if (!udp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800187 turn_server.ports.push_back(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700188 ProtocolAddress(kTurnUdpIntAddr, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 }
190 if (!tcp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800191 turn_server.ports.push_back(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700192 ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 }
deadbeef653b8e02015-11-11 12:55:10 -0800194 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000195 }
196
197 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700198 session_ = CreateSession("session", component);
199 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700201 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 return true;
203 }
204
205 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700206 session_ = CreateSession("session", content_name, component);
207 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700209 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 return true;
211 }
212
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700213 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
214 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 return CreateSession(sid, kContentName, component);
216 }
217
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700218 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700219 const std::string& sid,
220 const std::string& content_name,
221 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
223 }
224
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700225 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700226 const std::string& sid,
227 const std::string& content_name,
228 int component,
229 const std::string& ice_ufrag,
230 const std::string& ice_pwd) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700231 std::unique_ptr<PortAllocatorSession> session = allocator_->CreateSession(
232 sid, content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700234 &BasicPortAllocatorTest::OnPortReady);
235 session->SignalCandidatesReady.connect(
236 this, &BasicPortAllocatorTest::OnCandidatesReady);
237 session->SignalCandidatesAllocationDone.connect(
238 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 return session;
240 }
241
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700242 // Return true if the addresses are the same, or the port is 0 in |pattern|
243 // (acting as a wildcard) and the IPs are the same.
244 // Even with a wildcard port, the port of the address should be nonzero if
245 // the IP is nonzero.
246 static bool AddressMatch(const SocketAddress& address,
247 const SocketAddress& pattern) {
248 return address.ipaddr() == pattern.ipaddr() &&
249 ((pattern.port() == 0 &&
250 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
251 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000252 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700253
254 // Find a candidate and return it.
255 static bool FindCandidate(const std::vector<Candidate>& candidates,
256 const std::string& type,
257 const std::string& proto,
258 const SocketAddress& addr,
259 Candidate* found) {
260 auto it = std::find_if(candidates.begin(), candidates.end(),
261 [type, proto, addr](const Candidate& c) {
262 return c.type() == type && c.protocol() == proto &&
263 AddressMatch(c.address(), addr);
264 });
265 if (it != candidates.end() && found) {
266 *found = *it;
267 }
268 return it != candidates.end();
269 }
270
271 // Convenience method to call FindCandidate with no return.
272 static bool HasCandidate(const std::vector<Candidate>& candidates,
273 const std::string& type,
274 const std::string& proto,
275 const SocketAddress& addr) {
276 return FindCandidate(candidates, type, proto, addr, nullptr);
277 }
278
279 // Version of HasCandidate that also takes a related address.
280 static bool HasCandidateWithRelatedAddr(
281 const std::vector<Candidate>& candidates,
282 const std::string& type,
283 const std::string& proto,
284 const SocketAddress& addr,
285 const SocketAddress& related_addr) {
286 auto it =
287 std::find_if(candidates.begin(), candidates.end(),
288 [type, proto, addr, related_addr](const Candidate& c) {
289 return c.type() == type && c.protocol() == proto &&
290 AddressMatch(c.address(), addr) &&
291 AddressMatch(c.related_address(), related_addr);
292 });
293 return it != candidates.end();
294 }
295
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700297 int min_port,
298 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000299 return (addr.port() >= min_port && addr.port() <= max_port);
300 }
301
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700302 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000303 // We should only get this callback once, except in the mux test where
304 // we have multiple port allocation sessions.
305 if (session == session_.get()) {
306 ASSERT_FALSE(candidate_allocation_done_);
307 candidate_allocation_done_ = true;
308 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700309 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000310 }
311
312 // Check if all ports allocated have send-buffer size |expected|. If
313 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
314 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700315 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000316 for (it = ports_.begin(); it < ports_.end(); ++it) {
317 int send_buffer_size;
318 if (expected == -1) {
319 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700320 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000321 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700322 EXPECT_EQ(0,
323 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000324 ASSERT_EQ(expected, send_buffer_size);
325 }
326 }
327 }
328
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700329 // This function starts the port/address gathering and check the existence of
330 // candidates as specified. When |expect_stun_candidate| is true,
331 // |stun_candidate_addr| carries the expected reflective address, which is
332 // also the related address for TURN candidate if it is expected. Otherwise,
333 // it should be ignore.
334 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200335 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700336 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700337 const rtc::IPAddress& stun_candidate_addr,
338 const rtc::IPAddress& relay_candidate_udp_transport_addr,
339 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800340 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
341 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700342 if (!session_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700343 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700344 }
345 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700346 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
347 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700348 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000349 session_->StartGettingPorts();
350 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
351
Peter Boström0c4e06b2015-10-07 12:23:21 +0200352 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700353 if (!host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700354 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800355 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700356 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700357 }
358 if (!stun_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700359 rtc::SocketAddress related_address(host_candidate_addr, 0);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800360 if (host_candidate_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700361 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800362 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700363 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
364 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700365 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700366 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700367 if (!relay_candidate_udp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700368 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
369 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
370 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700371 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700372 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700373 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700374 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
375 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
376 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700377 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700378 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000379
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700380 EXPECT_EQ(total_candidates, candidates_.size());
381 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000382 }
383
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700385 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700387 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
389 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700390 // Make sure the new port is added to ReadyPorts.
391 auto ready_ports = ses->ReadyPorts();
392 EXPECT_NE(ready_ports.end(),
393 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700395 void OnCandidatesReady(PortAllocatorSession* ses,
396 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700397 for (const Candidate& candidate : candidates) {
398 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
399 // Sanity check that the ICE component is set.
400 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
401 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000402 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700403 // Make sure the new candidates are added to Candidates.
404 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700405 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700406 EXPECT_NE(
407 ses_candidates.end(),
408 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
409 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 }
411
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700412 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800413 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700414 RelayServerConfig server_config = allocator_->turn_servers()[i];
415 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000416 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700417 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 if (proto_addr.address == relay_port->address &&
419 proto_addr.proto == relay_port->proto)
420 return true;
421 }
422 }
423 return false;
424 }
425
Guo-wei Shieh11477022015-08-15 09:28:41 -0700426 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
427 bool with_nat) {
428 if (with_nat) {
429 nat_server_.reset(new rtc::NATServer(
430 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
431 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
432 } else {
433 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
434 }
435
436 ServerAddresses stun_servers;
437 if (!stun_server.IsNil()) {
438 stun_servers.insert(stun_server);
439 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700440 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700441 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700442 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700443 }
444
kwiberg3ec46792016-04-27 07:22:53 -0700445 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
446 std::unique_ptr<rtc::VirtualSocketServer> vss_;
447 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000448 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700449 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000450 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700451 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700452 std::unique_ptr<TestStunServer> stun_server_;
453 TestRelayServer relay_server_;
454 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000455 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700456 std::unique_ptr<BasicPortAllocator> allocator_;
457 std::unique_ptr<PortAllocatorSession> session_;
458 std::vector<PortInterface*> ports_;
459 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 bool candidate_allocation_done_;
461};
462
463// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700464TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000465 EXPECT_EQ(&network_manager_, allocator().network_manager());
466 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800467 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700468 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800470 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
471 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700472 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
473 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
474 EXPECT_TRUE(
475 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
476 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700477 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000478}
479
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800480// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700481TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800482 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
483 rtc::ADAPTER_TYPE_ETHERNET);
484 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
485 rtc::ADAPTER_TYPE_WIFI);
486 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
487 rtc::ADAPTER_TYPE_CELLULAR);
488 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
489 rtc::ADAPTER_TYPE_VPN);
490 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
491 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700492 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
493 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
494 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800495 session_->StartGettingPorts();
496 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
497 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700498 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800499 EXPECT_LT(candidate.address().ip(), 0x12345604U);
500 }
501}
502
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700503TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800504 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
505 rtc::ADAPTER_TYPE_ETHERNET);
506 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
507 rtc::ADAPTER_TYPE_WIFI);
508 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
509 rtc::ADAPTER_TYPE_CELLULAR);
510 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
511 rtc::ADAPTER_TYPE_LOOPBACK |
512 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700513 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
514 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
515 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800516 session_->StartGettingPorts();
517 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
518 EXPECT_EQ(1U, candidates_.size());
519 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
520}
521
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700523TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700524 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000525 session_->StartGettingPorts();
526 // Waiting for one second to make sure BasicPortAllocatorSession has not
527 // called OnAllocate multiple times. In old behavior it's called every 250ms.
528 // When there are no network interfaces, each execution of OnAllocate will
529 // result in SignalCandidatesAllocationDone signal.
530 rtc::Thread::Current()->ProcessMessages(1000);
531 EXPECT_TRUE(candidate_allocation_done_);
532 EXPECT_EQ(0U, candidates_.size());
533}
534
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700535// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700536TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800537 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
538 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700539 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
540 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
541 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700542 session_->StartGettingPorts();
543 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
544 EXPECT_EQ(1U, candidates_.size());
545}
546
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000547// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700548TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000549 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700550 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000551 session_->StartGettingPorts();
552 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
553 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700554 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
555 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
556 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
557 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
558 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
559 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
560 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
561 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000562 EXPECT_TRUE(candidate_allocation_done_);
563}
564
honghaiz8c404fa2015-09-28 07:59:43 -0700565// Test that when the same network interface is brought down and up, the
566// port allocator session will restart a new allocation sequence if
567// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700568TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700569 std::string if_name("test_net0");
570 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700571 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700572 session_->StartGettingPorts();
573 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
574 EXPECT_EQ(4U, ports_.size());
575 EXPECT_TRUE(candidate_allocation_done_);
576 candidate_allocation_done_ = false;
577 candidates_.clear();
578 ports_.clear();
579
580 RemoveInterface(kClientAddr);
581 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
582 EXPECT_EQ(0U, ports_.size());
583 EXPECT_FALSE(candidate_allocation_done_);
584
585 // When the same interfaces are added again, new candidates/ports should be
586 // generated.
587 AddInterface(kClientAddr, if_name);
588 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
589 EXPECT_EQ(4U, ports_.size());
590 EXPECT_TRUE(candidate_allocation_done_);
591}
592
593// Test that when the same network interface is brought down and up, the
594// port allocator session will not restart a new allocation sequence if
595// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700596TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700597 std::string if_name("test_net0");
598 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700599 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700600 session_->StartGettingPorts();
601 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
602 EXPECT_EQ(4U, ports_.size());
603 EXPECT_TRUE(candidate_allocation_done_);
604 session_->StopGettingPorts();
605 candidates_.clear();
606 ports_.clear();
607
608 RemoveInterface(kClientAddr);
609 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
610 EXPECT_EQ(0U, ports_.size());
611
612 // When the same interfaces are added again, new candidates/ports should not
613 // be generated because the session has stopped.
614 AddInterface(kClientAddr, if_name);
615 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
616 EXPECT_EQ(0U, ports_.size());
617 EXPECT_TRUE(candidate_allocation_done_);
618}
619
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000620// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700621TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000622 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700623 allocator_->set_step_delay(kDefaultStepDelay);
624 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000625 session_->StartGettingPorts();
626 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
627 EXPECT_EQ(2U, ports_.size());
628 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
629 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700630 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
631 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000632 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700633 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
634 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000635 EXPECT_EQ(4U, ports_.size());
636 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700637 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
638 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000639 EXPECT_EQ(4U, ports_.size());
640 EXPECT_TRUE(candidate_allocation_done_);
641 // If we Stop gathering now, we shouldn't get a second "done" callback.
642 session_->StopGettingPorts();
643}
644
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700645TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000646 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700647 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000648 session_->StartGettingPorts();
649 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
650 EXPECT_TRUE(candidate_allocation_done_);
651 // If we Stop gathering now, we shouldn't get a second "done" callback.
652 session_->StopGettingPorts();
653
654 // All ports should have unset send-buffer sizes.
655 CheckSendBufferSizesOfAllPorts(-1);
656}
657
658// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700659TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000660 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700661 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000662 session_->StartGettingPorts();
663 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
664 EXPECT_EQ(2U, ports_.size());
665 session_->StopGettingPorts();
666 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
667}
668
669// Test that we restrict client ports appropriately when a port range is set.
670// We check the candidates for udp/stun/tcp ports, and the from address
671// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700672TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000673 AddInterface(kClientAddr);
674 // Check that an invalid port range fails.
675 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
676 // Check that a null port range succeeds.
677 EXPECT_TRUE(SetPortRange(0, 0));
678 // Check that a valid port range succeeds.
679 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700680 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000681 session_->StartGettingPorts();
682 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
683 EXPECT_EQ(4U, ports_.size());
684 // Check the port number for the UDP port object.
685 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
686 // Check the port number for the STUN port object.
687 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
688 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700689 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
690 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000691 // Check the port number for the TCP port object.
692 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
693 EXPECT_TRUE(candidate_allocation_done_);
694}
695
696// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700697TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700698 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000699 session_->StartGettingPorts();
700 rtc::Thread::Current()->ProcessMessages(100);
701 // Without network adapter, we should not get any candidate.
702 EXPECT_EQ(0U, candidates_.size());
703 EXPECT_TRUE(candidate_allocation_done_);
704}
705
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700706// Test that when enumeration is disabled, we should not have any ports when
707// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700709 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700710 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700711 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700712 // Expect to see no ports and no candidates.
713 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
714 rtc::IPAddress(), rtc::IPAddress());
715}
716
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800717// Test that even with multiple interfaces, the result should still be a single
718// default private, one STUN and one TURN candidate since we bind to any address
719// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700721 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000722 AddInterface(kPrivateAddr);
723 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700724 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000725 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800726
727 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
728 // address set and we have no IPv6 STUN server, there should be no IPv6
729 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700730 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
731 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800732
733 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
734 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
735 // TURN/UDP candidates.
736 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800737 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
738 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700739}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000740
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800741// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
742// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700743TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700744 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800745 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700746 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700747 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800748 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
749 // private, STUN, TURN/UDP, and TURN/TCP candidates.
750 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
751 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700752 kTurnUdpExtAddr.ipaddr());
753}
754
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800755// Test that when adapter enumeration is disabled, for endpoints without
756// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700757TEST_F(BasicPortAllocatorTest,
758 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800759 ResetWithNoServersOrNat();
760 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
761 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
762 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700763}
764
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800765// Test that when adapter enumeration is disabled, with
766// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
767// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700768TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800769 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
770 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700771 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
772 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800773 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
774 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700775 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700776 rtc::IPAddress(), rtc::IPAddress());
777}
778
779// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800780// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
781// a NAT, there is no local candidate. However, this specified default route
782// (kClientAddr) which was discovered when sending STUN requests, will become
783// the srflx addresses.
784TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700785 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800786 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700787 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800788 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700789 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
790 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700791 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
792 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800793 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
794 rtc::IPAddress(), rtc::IPAddress());
795}
796
797// Test that when adapter enumeration is disabled, with
798// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
799// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700800TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800801 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
802 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700803 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
804 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800805 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
806 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
807 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000808}
809
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700810// Test that we disable relay over UDP, and only TCP is used when connecting to
811// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700812TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700813 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700814 AddInterface(kClientAddr);
815 ResetWithStunServerAndNat(kStunAddr);
816 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700817 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
818 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
819 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
820 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700821
822 session_->StartGettingPorts();
823 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
824
825 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
826 // TURN/TCP candidates.
827 EXPECT_EQ(2U, ports_.size());
828 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700829 Candidate turn_candidate;
830 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
831 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700832 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700833 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
834 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700835}
836
Erik Språngefdce692015-06-05 09:41:26 +0200837// Disable for asan, see
838// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
839#if !defined(ADDRESS_SANITIZER)
840
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000841// Test that we can get OnCandidatesAllocationDone callback when all the ports
842// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700843TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700845 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
846 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
847 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000848 session_->StartGettingPorts();
849 rtc::Thread::Current()->ProcessMessages(100);
850 EXPECT_EQ(0U, candidates_.size());
851 EXPECT_TRUE(candidate_allocation_done_);
852}
853
854// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700855TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000856 AddInterface(kClientAddr);
857 fss_->set_udp_sockets_enabled(false);
858 EXPECT_TRUE(CreateSession(1));
859 session_->StartGettingPorts();
860 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
861 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700862 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
863 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
864 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
865 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
866 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
867 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000868 EXPECT_TRUE(candidate_allocation_done_);
869}
870
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700871#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200872
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873// Test that we don't crash or malfunction if we can't create UDP sockets or
874// listen on TCP sockets. We still give out a local TCP address, since
875// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700876TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000877 AddInterface(kClientAddr);
878 fss_->set_udp_sockets_enabled(false);
879 fss_->set_tcp_listen_enabled(false);
880 EXPECT_TRUE(CreateSession(1));
881 session_->StartGettingPorts();
882 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
883 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700884 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
885 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
886 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
887 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
888 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700889 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000890 EXPECT_TRUE(candidate_allocation_done_);
891}
892
893// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700894// TODO(deadbeef): Find a way to exit early here.
895TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000896 AddInterface(kClientAddr);
897 fss_->set_tcp_sockets_enabled(false);
898 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700899 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 session_->StartGettingPorts();
901 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700902 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 // In case of Relay, ports creation will succeed but sockets will fail.
904 // There is no error reporting from RelayEntry to handle this failure.
905}
906
907// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700908TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000909 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
910 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700911 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000912 session_->StartGettingPorts();
913 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
914 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700915 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
916 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000917 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
918 // will be tried after 3 seconds.
919 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
920 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700921 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
922 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
923 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
924 kRelaySslTcpIntAddr);
925 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000926 // Stun Timeout is 9sec.
927 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
928}
929
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700930TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000931 AddInterface(kClientAddr);
932 AddInterface(kClientAddr2);
933 // Allocating only host UDP ports. This is done purely for testing
934 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700935 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
936 PORTALLOCATOR_DISABLE_RELAY);
937 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000938 session_->StartGettingPorts();
939 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
940 ASSERT_EQ(2U, candidates_.size());
941 EXPECT_EQ(2U, ports_.size());
942 // Candidates priorities should be different.
943 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
944}
945
946// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700947TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000948 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700949 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000950 session_->StartGettingPorts();
951 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
952 EXPECT_EQ(4U, ports_.size());
953 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700954 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955}
956
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700957// Test that the allocator session uses the candidate filter it's created with,
958// rather than the filter of its parent allocator.
959// The filter of the allocator should only affect the next gathering phase,
960// according to JSEP, which means the *next* allocator session returned.
961TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
962 AddInterface(kClientAddr);
963 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
964 // Set candidate filter *after* creating the session. Should have no effect.
965 allocator().set_candidate_filter(CF_RELAY);
966 session_->StartGettingPorts();
967 // 7 candidates and 4 ports is what we would normally get (see the
968 // TestGetAllPorts* tests).
969 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
970 EXPECT_EQ(4U, ports_.size());
971 EXPECT_TRUE(candidate_allocation_done_);
972}
973
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000974// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
975// This test also verifies that when the allocator is only allowed to use
976// relay (i.e. IceTransportsType is relay), the raddr is an empty
977// address with the correct family. This is to prevent any local
978// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700979TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 AddInterface(kClientAddr);
981 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700982 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700983 allocator().set_candidate_filter(CF_RELAY);
984 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000985 session_->StartGettingPorts();
986 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700987 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
988 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989
990 EXPECT_EQ(1U, candidates_.size());
991 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700992 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
993 EXPECT_EQ(
994 candidates_[0].related_address(),
995 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000996}
997
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700998TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000999 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001000 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1001 allocator().set_candidate_filter(CF_HOST);
1002 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001003 session_->StartGettingPorts();
1004 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001005 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1006 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001007 for (const Candidate& candidate : candidates_) {
1008 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001009 }
1010}
1011
1012// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001013TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001014 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001015 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001016
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001017 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1018 allocator().set_candidate_filter(CF_REFLEXIVE);
1019 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001020 session_->StartGettingPorts();
1021 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1022 // Host is behind NAT, no private address will be exposed. Hence only UDP
1023 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001024 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1025 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001026 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1027 EXPECT_EQ(
1028 candidates_[0].related_address(),
1029 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001030}
1031
1032// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001033TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001034 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001035 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1036 allocator().set_candidate_filter(CF_REFLEXIVE);
1037 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001038 session_->StartGettingPorts();
1039 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1040 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001041 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001042 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001043 for (const Candidate& candidate : candidates_) {
1044 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001045 }
1046}
1047
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001048// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001049TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001050 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001051 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 session_->StartGettingPorts();
1053 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001054 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1055 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1056 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001057 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001058 for (const Candidate& candidate : candidates_) {
1059 EXPECT_EQ(kIceUfrag0, candidate.username());
1060 EXPECT_EQ(kIcePwd0, candidate.password());
1061 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001062 EXPECT_TRUE(candidate_allocation_done_);
1063}
1064
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001065// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1066// is allocated for udp and stun. Also verify there is only one candidate
1067// (local) if stun candidate is same as local candidate, which will be the case
1068// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001069TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001070 AddInterface(kClientAddr);
1071 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001072 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1073 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001074 session_->StartGettingPorts();
1075 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1076 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001077 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001078 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1079}
1080
1081// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1082// is allocated for udp and stun. In this test we should expect both stun and
1083// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001084TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001085 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001086 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001087
1088 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001089 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1090 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001091 session_->StartGettingPorts();
1092 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1093 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001094 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1095 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1096 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001097 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1098 EXPECT_EQ(3U, candidates_.size());
1099}
1100
deadbeefc5d0d952015-07-16 10:22:21 -07001101// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001102TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001103 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001104 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001105 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106
1107 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1108
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001109 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001110 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001111 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1112 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001113
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001114 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001115 session_->StartGettingPorts();
1116
1117 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1118 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001119 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1120 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1121 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1122 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1123 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001124 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1125 EXPECT_EQ(3U, candidates_.size());
1126}
1127
1128// Testing DNS resolve for the TURN server, this will test AllocationSequence
1129// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001130TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001131 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001132 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001133 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001134 allocator_.reset(new BasicPortAllocator(&network_manager_));
1135 RelayServerConfig turn_server(RELAY_TURN);
1136 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001137 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001138 turn_server.ports.push_back(
1139 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001140 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001141
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001142 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001144 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1145 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001147 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001148 session_->StartGettingPorts();
1149
1150 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1151}
1152
1153// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1154// is allocated for udp/stun/turn. In this test we should expect all local,
1155// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001156TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001158 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159
1160 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1161
1162 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001163 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1164 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001165
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001166 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001167 session_->StartGettingPorts();
1168
1169 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1170 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001171 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1172 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1173 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1174 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1175 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001176 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1177 EXPECT_EQ(3U, candidates_.size());
1178 // Local port will be created first and then TURN port.
1179 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1180 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1181}
1182
1183// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1184// server is also used as the STUN server, we should get 'local', 'stun', and
1185// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001186TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001187 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001188 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001189 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1191
1192 // Must set the step delay to 0 to make sure the relay allocation phase is
1193 // started before the STUN candidates are obtained, so that the STUN binding
1194 // response is processed when both StunPort and TurnPort exist to reproduce
1195 // webrtc issue 3537.
1196 allocator_->set_step_delay(0);
1197 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001198 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1199 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001200
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001201 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001202 session_->StartGettingPorts();
1203
1204 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001205 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1206 Candidate stun_candidate;
1207 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1208 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1209 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1210 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1211 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212
1213 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1214 EXPECT_EQ(3U, candidates_.size());
1215 // Local port will be created first and then TURN port.
1216 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1217 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1218}
1219
deadbeefc5d0d952015-07-16 10:22:21 -07001220// Test that when only a TCP TURN server is available, we do NOT use it as
1221// a UDP STUN server, as this could leak our IP address. Thus we should only
1222// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001223TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001224 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001225 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001226 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001227 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1228
1229 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001230 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1231 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001232
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001233 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001234 session_->StartGettingPorts();
1235
1236 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1237 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001238 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1239 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1240 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001241 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1242 EXPECT_EQ(2U, candidates_.size());
1243 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1244 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1245}
1246
1247// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1248// TURN server is used as the STUN server and we get 'local', 'stun', and
1249// 'relay' candidates.
1250// TODO(deadbeef): Remove this test when support for non-shared socket mode
1251// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001252TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001253 AddInterface(kClientAddr);
1254 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001255 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001256 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1257
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001258 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001259
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001260 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001261 session_->StartGettingPorts();
1262
1263 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1264 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001265 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1266 Candidate stun_candidate;
1267 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1268 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1269 Candidate turn_candidate;
1270 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1271 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1272 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001273 // Not using shared socket, so the STUN request's server reflexive address
1274 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001275 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001276
1277 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1278 EXPECT_EQ(3U, candidates_.size());
1279 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1280 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1281 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1282}
1283
1284// Test that even when both a STUN and TURN server are configured, the TURN
1285// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001286TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001287 AddInterface(kClientAddr);
1288 // Configure with STUN server but destroy it, so we can ensure that it's
1289 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001290 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001291 stun_server_.reset();
1292 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1293
1294 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001295 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1296 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001297
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001298 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001299 session_->StartGettingPorts();
1300
1301 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001302 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1303 Candidate stun_candidate;
1304 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1305 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1306 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1307 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1308 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001309
1310 // Don't bother waiting for STUN timeout, since we already verified
1311 // that we got a STUN candidate from the TURN server.
1312}
1313
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001314// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1315// and fail to generate STUN candidate, local UDP candidate is generated
1316// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001317TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001318 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1319 PORTALLOCATOR_DISABLE_TCP |
1320 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001321 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1322 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001323 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001324 session_->StartGettingPorts();
1325 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1326 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001327 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001328 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1329 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001330 EXPECT_EQ(1U, candidates_.size());
1331}
1332
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001333// Test that when the NetworkManager doesn't have permission to enumerate
1334// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1335// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001337 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1338 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001339 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001340 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001341 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1342 PORTALLOCATOR_DISABLE_TCP |
1343 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1344 EXPECT_EQ(0U,
1345 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1346 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1347 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001348 session_->StartGettingPorts();
1349 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001350 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001351 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001352 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001353}
1354
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001355// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001356TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001357 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1358 PORTALLOCATOR_ENABLE_IPV6 |
1359 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360 AddInterface(kClientIPv6Addr);
1361 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001362 allocator_->set_step_delay(kMinimumStepDelay);
1363 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001364 session_->StartGettingPorts();
1365 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1366 EXPECT_EQ(4U, candidates_.size());
1367 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001368 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1369 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1370 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1371 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001372 EXPECT_EQ(4U, candidates_.size());
1373}
honghaiz98db68f2015-09-29 07:58:17 -07001374
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001375TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001376 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001377 allocator_->set_step_delay(kDefaultStepDelay);
1378 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001379 session_->StartGettingPorts();
1380 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1381 EXPECT_EQ(2U, ports_.size());
1382 session_->StopGettingPorts();
1383 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1384
1385 // After stopping getting ports, adding a new interface will not start
1386 // getting ports again.
1387 candidates_.clear();
1388 ports_.clear();
1389 candidate_allocation_done_ = false;
1390 network_manager_.AddInterface(kClientAddr2);
1391 rtc::Thread::Current()->ProcessMessages(1000);
1392 EXPECT_EQ(0U, candidates_.size());
1393 EXPECT_EQ(0U, ports_.size());
1394}
1395
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001396TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001397 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001398 allocator_->set_step_delay(kDefaultStepDelay);
1399 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001400 session_->StartGettingPorts();
1401 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1402 EXPECT_EQ(2U, ports_.size());
1403 session_->ClearGettingPorts();
1404 WAIT(candidate_allocation_done_, 1000);
1405 EXPECT_FALSE(candidate_allocation_done_);
1406
1407 // After clearing getting ports, adding a new interface will start getting
1408 // ports again.
1409 candidates_.clear();
1410 ports_.clear();
1411 candidate_allocation_done_ = false;
1412 network_manager_.AddInterface(kClientAddr2);
1413 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1414 EXPECT_EQ(2U, ports_.size());
1415}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001416
1417// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1418// a pooled session is taken out of the pool.
1419TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1420 AddInterface(kClientAddr);
1421 int pool_size = 1;
1422 allocator_->SetConfiguration(allocator_->stun_servers(),
1423 allocator_->turn_servers(), pool_size);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001424 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001425 ASSERT_NE(nullptr, peeked_session);
1426 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1427 kDefaultAllocationTimeout);
1428 // Expect that when TakePooledSession is called,
1429 // UpdateTransportInformationInternal will be called and the
1430 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1431 // candidates.
1432 session_ =
1433 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1434 ASSERT_NE(nullptr, session_.get());
1435 auto ready_ports = session_->ReadyPorts();
1436 auto candidates = session_->ReadyCandidates();
1437 EXPECT_FALSE(ready_ports.empty());
1438 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001439 for (const PortInterface* port_interface : ready_ports) {
1440 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001441 EXPECT_EQ(kContentName, port->content_name());
1442 EXPECT_EQ(1, port->component());
1443 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1444 EXPECT_EQ(kIcePwd0, port->password());
1445 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001446 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001447 EXPECT_EQ(1, candidate.component());
1448 EXPECT_EQ(kIceUfrag0, candidate.username());
1449 EXPECT_EQ(kIcePwd0, candidate.password());
1450 }
1451}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001452
1453// Test that a new candidate filter takes effect even on already-gathered
1454// candidates.
1455TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1456 AddInterface(kClientAddr);
1457 int pool_size = 1;
1458 allocator_->SetConfiguration(allocator_->stun_servers(),
1459 allocator_->turn_servers(), pool_size);
1460 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1461 ASSERT_NE(nullptr, peeked_session);
1462 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1463 kDefaultAllocationTimeout);
1464 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1465 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1466 allocator_->set_candidate_filter(CF_RELAY);
1467 // Assume that when TakePooledSession is called, the candidate filter will be
1468 // applied to the pooled session. This is tested by PortAllocatorTest.
1469 session_ =
1470 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1471 ASSERT_NE(nullptr, session_.get());
1472 auto candidates = session_->ReadyCandidates();
1473 auto ports = session_->ReadyPorts();
1474 // Sanity check that the number of candidates and ports decreased.
1475 EXPECT_GT(initial_candidates_size, candidates.size());
1476 EXPECT_GT(initial_ports_size, ports.size());
1477 for (const PortInterface* port : ports) {
1478 // Expect only relay ports.
1479 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1480 }
1481 for (const Candidate& candidate : candidates) {
1482 // Expect only relay candidates now that the filter is applied.
1483 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1484 // Expect that the raddr is emptied due to the CF_RELAY filter.
1485 EXPECT_EQ(candidate.related_address(),
1486 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1487 }
1488}
1489
1490} // namespace cricket