blob: 6d5d1086a9322e26ff9b1cf6d134f4862ce05507 [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
honghaiz60347052016-05-31 18:29:12 -0700522// Test that high cost networks are filtered if the flag
523// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
524TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
525 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
526 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
527 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
528 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
529 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
530 // interface.
531 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
532 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
533 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
534 cricket::PORTALLOCATOR_DISABLE_RELAY |
535 cricket::PORTALLOCATOR_DISABLE_TCP |
536 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
537 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
538 session_->StartGettingPorts();
539 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
540 EXPECT_EQ(1U, candidates_.size());
541 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
542
543 // If both cellular and unknown interfaces are present, only gather on the
544 // unknown interfaces.
545 candidates_.clear();
546 candidate_allocation_done_ = false;
547 RemoveInterface(addr_wifi);
548 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
549 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
550 session_->StartGettingPorts();
551 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
552 EXPECT_EQ(2U, candidates_.size());
553 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
554 addr_unknown2.EqualIPs(candidates_[1].address())) ||
555 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
556 addr_unknown2.EqualIPs(candidates_[0].address())));
557
558 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
559 // Wi-Fi interface.
560 candidates_.clear();
561 candidate_allocation_done_ = false;
562 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
563 session_->StartGettingPorts();
564 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
565 EXPECT_EQ(1U, candidates_.size());
566 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
567}
568
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700570TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700571 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000572 session_->StartGettingPorts();
573 // Waiting for one second to make sure BasicPortAllocatorSession has not
574 // called OnAllocate multiple times. In old behavior it's called every 250ms.
575 // When there are no network interfaces, each execution of OnAllocate will
576 // result in SignalCandidatesAllocationDone signal.
577 rtc::Thread::Current()->ProcessMessages(1000);
578 EXPECT_TRUE(candidate_allocation_done_);
579 EXPECT_EQ(0U, candidates_.size());
580}
581
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700582// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700583TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800584 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
585 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700586 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
587 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
588 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700589 session_->StartGettingPorts();
590 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
591 EXPECT_EQ(1U, candidates_.size());
592}
593
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000594// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700595TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000596 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700597 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000598 session_->StartGettingPorts();
599 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
600 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700601 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
602 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
603 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
604 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
605 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
606 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
607 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
608 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000609 EXPECT_TRUE(candidate_allocation_done_);
610}
611
honghaiz8c404fa2015-09-28 07:59:43 -0700612// Test that when the same network interface is brought down and up, the
613// port allocator session will restart a new allocation sequence if
614// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700615TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700616 std::string if_name("test_net0");
617 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700618 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700619 session_->StartGettingPorts();
620 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
621 EXPECT_EQ(4U, ports_.size());
622 EXPECT_TRUE(candidate_allocation_done_);
623 candidate_allocation_done_ = false;
624 candidates_.clear();
625 ports_.clear();
626
627 RemoveInterface(kClientAddr);
628 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
629 EXPECT_EQ(0U, ports_.size());
630 EXPECT_FALSE(candidate_allocation_done_);
631
632 // When the same interfaces are added again, new candidates/ports should be
633 // generated.
634 AddInterface(kClientAddr, if_name);
635 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
636 EXPECT_EQ(4U, ports_.size());
637 EXPECT_TRUE(candidate_allocation_done_);
638}
639
640// Test that when the same network interface is brought down and up, the
641// port allocator session will not restart a new allocation sequence if
642// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700643TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700644 std::string if_name("test_net0");
645 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700646 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700647 session_->StartGettingPorts();
648 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
649 EXPECT_EQ(4U, ports_.size());
650 EXPECT_TRUE(candidate_allocation_done_);
651 session_->StopGettingPorts();
652 candidates_.clear();
653 ports_.clear();
654
655 RemoveInterface(kClientAddr);
656 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
657 EXPECT_EQ(0U, ports_.size());
658
659 // When the same interfaces are added again, new candidates/ports should not
660 // be generated because the session has stopped.
661 AddInterface(kClientAddr, if_name);
662 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
663 EXPECT_EQ(0U, ports_.size());
664 EXPECT_TRUE(candidate_allocation_done_);
665}
666
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000667// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700668TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000669 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700670 allocator_->set_step_delay(kDefaultStepDelay);
671 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000672 session_->StartGettingPorts();
673 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
674 EXPECT_EQ(2U, ports_.size());
675 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
676 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700677 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
678 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000679 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700680 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
681 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000682 EXPECT_EQ(4U, ports_.size());
683 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700684 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
685 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000686 EXPECT_EQ(4U, ports_.size());
687 EXPECT_TRUE(candidate_allocation_done_);
688 // If we Stop gathering now, we shouldn't get a second "done" callback.
689 session_->StopGettingPorts();
690}
691
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700692TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000693 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700694 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000695 session_->StartGettingPorts();
696 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
697 EXPECT_TRUE(candidate_allocation_done_);
698 // If we Stop gathering now, we shouldn't get a second "done" callback.
699 session_->StopGettingPorts();
700
701 // All ports should have unset send-buffer sizes.
702 CheckSendBufferSizesOfAllPorts(-1);
703}
704
705// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700706TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000707 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700708 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000709 session_->StartGettingPorts();
710 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
711 EXPECT_EQ(2U, ports_.size());
712 session_->StopGettingPorts();
713 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
714}
715
716// Test that we restrict client ports appropriately when a port range is set.
717// We check the candidates for udp/stun/tcp ports, and the from address
718// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700719TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000720 AddInterface(kClientAddr);
721 // Check that an invalid port range fails.
722 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
723 // Check that a null port range succeeds.
724 EXPECT_TRUE(SetPortRange(0, 0));
725 // Check that a valid port range succeeds.
726 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700727 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000728 session_->StartGettingPorts();
729 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
730 EXPECT_EQ(4U, ports_.size());
731 // Check the port number for the UDP port object.
732 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
733 // Check the port number for the STUN port object.
734 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
735 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
737 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000738 // Check the port number for the TCP port object.
739 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
740 EXPECT_TRUE(candidate_allocation_done_);
741}
742
743// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700744TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700745 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000746 session_->StartGettingPorts();
747 rtc::Thread::Current()->ProcessMessages(100);
748 // Without network adapter, we should not get any candidate.
749 EXPECT_EQ(0U, candidates_.size());
750 EXPECT_TRUE(candidate_allocation_done_);
751}
752
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700753// Test that when enumeration is disabled, we should not have any ports when
754// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700755TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700756 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700757 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700758 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700759 // Expect to see no ports and no candidates.
760 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
761 rtc::IPAddress(), rtc::IPAddress());
762}
763
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800764// Test that even with multiple interfaces, the result should still be a single
765// default private, one STUN and one TURN candidate since we bind to any address
766// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700767TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700768 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000769 AddInterface(kPrivateAddr);
770 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700771 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000772 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800773
774 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
775 // address set and we have no IPv6 STUN server, there should be no IPv6
776 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700777 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
778 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800779
780 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
781 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
782 // TURN/UDP candidates.
783 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800784 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
785 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700786}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000787
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800788// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
789// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700790TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700791 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800792 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700793 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700794 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800795 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
796 // private, STUN, TURN/UDP, and TURN/TCP candidates.
797 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
798 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700799 kTurnUdpExtAddr.ipaddr());
800}
801
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800802// Test that when adapter enumeration is disabled, for endpoints without
803// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700804TEST_F(BasicPortAllocatorTest,
805 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800806 ResetWithNoServersOrNat();
807 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
808 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
809 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700810}
811
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800812// Test that when adapter enumeration is disabled, with
813// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
814// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700815TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800816 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
817 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700818 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
819 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800820 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
821 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700822 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700823 rtc::IPAddress(), rtc::IPAddress());
824}
825
826// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800827// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
828// a NAT, there is no local candidate. However, this specified default route
829// (kClientAddr) which was discovered when sending STUN requests, will become
830// the srflx addresses.
831TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700832 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800833 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700834 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800835 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700836 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
837 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700838 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
839 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800840 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
841 rtc::IPAddress(), rtc::IPAddress());
842}
843
844// Test that when adapter enumeration is disabled, with
845// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
846// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700847TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800848 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
849 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700850 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
851 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800852 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
853 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
854 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000855}
856
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700857// Test that we disable relay over UDP, and only TCP is used when connecting to
858// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700859TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700860 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700861 AddInterface(kClientAddr);
862 ResetWithStunServerAndNat(kStunAddr);
863 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700864 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
865 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
866 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
867 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700868
869 session_->StartGettingPorts();
870 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
871
872 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
873 // TURN/TCP candidates.
874 EXPECT_EQ(2U, ports_.size());
875 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700876 Candidate turn_candidate;
877 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
878 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700879 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700880 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
881 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700882}
883
Erik Språngefdce692015-06-05 09:41:26 +0200884// Disable for asan, see
885// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
886#if !defined(ADDRESS_SANITIZER)
887
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000888// Test that we can get OnCandidatesAllocationDone callback when all the ports
889// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700890TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000891 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700892 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
893 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
894 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 session_->StartGettingPorts();
896 rtc::Thread::Current()->ProcessMessages(100);
897 EXPECT_EQ(0U, candidates_.size());
898 EXPECT_TRUE(candidate_allocation_done_);
899}
900
901// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700902TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903 AddInterface(kClientAddr);
904 fss_->set_udp_sockets_enabled(false);
905 EXPECT_TRUE(CreateSession(1));
906 session_->StartGettingPorts();
907 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
908 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700909 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
910 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
911 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
912 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
913 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
914 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000915 EXPECT_TRUE(candidate_allocation_done_);
916}
917
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700918#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200919
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920// Test that we don't crash or malfunction if we can't create UDP sockets or
921// listen on TCP sockets. We still give out a local TCP address, since
922// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700923TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000924 AddInterface(kClientAddr);
925 fss_->set_udp_sockets_enabled(false);
926 fss_->set_tcp_listen_enabled(false);
927 EXPECT_TRUE(CreateSession(1));
928 session_->StartGettingPorts();
929 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
930 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700931 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
932 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
933 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
934 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
935 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700936 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000937 EXPECT_TRUE(candidate_allocation_done_);
938}
939
940// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700941// TODO(deadbeef): Find a way to exit early here.
942TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000943 AddInterface(kClientAddr);
944 fss_->set_tcp_sockets_enabled(false);
945 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700946 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000947 session_->StartGettingPorts();
948 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700949 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000950 // In case of Relay, ports creation will succeed but sockets will fail.
951 // There is no error reporting from RelayEntry to handle this failure.
952}
953
954// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700955TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000956 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
957 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700958 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000959 session_->StartGettingPorts();
960 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
961 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700962 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
963 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000964 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
965 // will be tried after 3 seconds.
deadbeef8271a7f2016-05-31 16:27:10 -0700966 // TODO(deadbeef): Use simulated clock here, waiting for exactly 3 seconds.
967 EXPECT_EQ_WAIT(6U, candidates_.size(), kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000968 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700969 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
970 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
971 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
972 kRelaySslTcpIntAddr);
973 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
deadbeef8271a7f2016-05-31 16:27:10 -0700974 // Stun Timeout is 9.5sec.
975 // TODO(deadbeef): Use simulated clock here, waiting exactly 6.5 seconds.
976 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000977}
978
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700979TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 AddInterface(kClientAddr);
981 AddInterface(kClientAddr2);
982 // Allocating only host UDP ports. This is done purely for testing
983 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700984 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
985 PORTALLOCATOR_DISABLE_RELAY);
986 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000987 session_->StartGettingPorts();
988 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
989 ASSERT_EQ(2U, candidates_.size());
990 EXPECT_EQ(2U, ports_.size());
991 // Candidates priorities should be different.
992 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
993}
994
995// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700996TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000997 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700998 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000999 session_->StartGettingPorts();
1000 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1001 EXPECT_EQ(4U, ports_.size());
1002 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001003 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001004}
1005
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001006// Test that the allocator session uses the candidate filter it's created with,
1007// rather than the filter of its parent allocator.
1008// The filter of the allocator should only affect the next gathering phase,
1009// according to JSEP, which means the *next* allocator session returned.
1010TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1011 AddInterface(kClientAddr);
1012 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1013 // Set candidate filter *after* creating the session. Should have no effect.
1014 allocator().set_candidate_filter(CF_RELAY);
1015 session_->StartGettingPorts();
1016 // 7 candidates and 4 ports is what we would normally get (see the
1017 // TestGetAllPorts* tests).
1018 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1019 EXPECT_EQ(4U, ports_.size());
1020 EXPECT_TRUE(candidate_allocation_done_);
1021}
1022
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001023// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1024// This test also verifies that when the allocator is only allowed to use
1025// relay (i.e. IceTransportsType is relay), the raddr is an empty
1026// address with the correct family. This is to prevent any local
1027// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001028TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001029 AddInterface(kClientAddr);
1030 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001031 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001032 allocator().set_candidate_filter(CF_RELAY);
1033 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001034 session_->StartGettingPorts();
1035 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001036 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1037 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001038
1039 EXPECT_EQ(1U, candidates_.size());
1040 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001041 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1042 EXPECT_EQ(
1043 candidates_[0].related_address(),
1044 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001045}
1046
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001047TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001048 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001049 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1050 allocator().set_candidate_filter(CF_HOST);
1051 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 session_->StartGettingPorts();
1053 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001054 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1055 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001056 for (const Candidate& candidate : candidates_) {
1057 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001058 }
1059}
1060
1061// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001062TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001063 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001064 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001065
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001066 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1067 allocator().set_candidate_filter(CF_REFLEXIVE);
1068 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001069 session_->StartGettingPorts();
1070 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1071 // Host is behind NAT, no private address will be exposed. Hence only UDP
1072 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001073 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1074 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001075 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1076 EXPECT_EQ(
1077 candidates_[0].related_address(),
1078 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001079}
1080
1081// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001082TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001083 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001084 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1085 allocator().set_candidate_filter(CF_REFLEXIVE);
1086 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001087 session_->StartGettingPorts();
1088 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1089 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001090 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001091 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001092 for (const Candidate& candidate : candidates_) {
1093 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001094 }
1095}
1096
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001097// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001098TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001099 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001100 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001101 session_->StartGettingPorts();
1102 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001103 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1104 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1105 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001107 for (const Candidate& candidate : candidates_) {
1108 EXPECT_EQ(kIceUfrag0, candidate.username());
1109 EXPECT_EQ(kIcePwd0, candidate.password());
1110 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001111 EXPECT_TRUE(candidate_allocation_done_);
1112}
1113
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001114// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1115// is allocated for udp and stun. Also verify there is only one candidate
1116// (local) if stun candidate is same as local candidate, which will be the case
1117// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001118TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001119 AddInterface(kClientAddr);
1120 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001121 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1122 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001123 session_->StartGettingPorts();
1124 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1125 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001126 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001127 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1128}
1129
1130// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1131// is allocated for udp and stun. In this test we should expect both stun and
1132// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001133TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001134 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001135 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001136
1137 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001138 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1139 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140 session_->StartGettingPorts();
1141 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1142 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001143 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1144 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1145 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1147 EXPECT_EQ(3U, candidates_.size());
1148}
1149
deadbeefc5d0d952015-07-16 10:22:21 -07001150// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001151TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001152 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001153 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001154 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001155
1156 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1157
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001158 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001160 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1161 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001163 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001164 session_->StartGettingPorts();
1165
1166 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1167 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001168 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1169 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1170 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1171 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1172 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001173 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1174 EXPECT_EQ(3U, candidates_.size());
1175}
1176
1177// Testing DNS resolve for the TURN server, this will test AllocationSequence
1178// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001179TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001180 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001181 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001182 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001183 allocator_.reset(new BasicPortAllocator(&network_manager_));
1184 RelayServerConfig turn_server(RELAY_TURN);
1185 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001186 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001187 turn_server.ports.push_back(
1188 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001189 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001191 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001193 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1194 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001196 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001197 session_->StartGettingPorts();
1198
1199 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1200}
1201
1202// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1203// is allocated for udp/stun/turn. In this test we should expect all local,
1204// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001205TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001206 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001207 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001208
1209 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1210
1211 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001212 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1213 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001214
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001215 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001216 session_->StartGettingPorts();
1217
1218 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1219 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001220 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1221 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1222 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1223 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1224 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001225 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1226 EXPECT_EQ(3U, candidates_.size());
1227 // Local port will be created first and then TURN port.
1228 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1229 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1230}
1231
1232// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1233// server is also used as the STUN server, we should get 'local', 'stun', and
1234// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001235TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001236 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001237 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001238 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001239 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1240
1241 // Must set the step delay to 0 to make sure the relay allocation phase is
1242 // started before the STUN candidates are obtained, so that the STUN binding
1243 // response is processed when both StunPort and TurnPort exist to reproduce
1244 // webrtc issue 3537.
1245 allocator_->set_step_delay(0);
1246 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001247 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1248 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001249
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001250 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001251 session_->StartGettingPorts();
1252
1253 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001254 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1255 Candidate stun_candidate;
1256 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1257 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1258 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1259 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1260 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001261
1262 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1263 EXPECT_EQ(3U, candidates_.size());
1264 // Local port will be created first and then TURN port.
1265 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1266 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1267}
1268
deadbeefc5d0d952015-07-16 10:22:21 -07001269// Test that when only a TCP TURN server is available, we do NOT use it as
1270// a UDP STUN server, as this could leak our IP address. Thus we should only
1271// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001272TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001273 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001274 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001275 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001276 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1277
1278 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001279 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1280 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001281
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001282 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001283 session_->StartGettingPorts();
1284
1285 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1286 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001287 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1288 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1289 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001290 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1291 EXPECT_EQ(2U, candidates_.size());
1292 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1293 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1294}
1295
1296// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1297// TURN server is used as the STUN server and we get 'local', 'stun', and
1298// 'relay' candidates.
1299// TODO(deadbeef): Remove this test when support for non-shared socket mode
1300// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001301TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001302 AddInterface(kClientAddr);
1303 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001304 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001305 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1306
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001307 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001308
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001309 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001310 session_->StartGettingPorts();
1311
1312 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1313 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001314 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1315 Candidate stun_candidate;
1316 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1317 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1318 Candidate turn_candidate;
1319 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1320 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1321 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001322 // Not using shared socket, so the STUN request's server reflexive address
1323 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001324 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001325
1326 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1327 EXPECT_EQ(3U, candidates_.size());
1328 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1329 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1330 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1331}
1332
1333// Test that even when both a STUN and TURN server are configured, the TURN
1334// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001335TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001336 AddInterface(kClientAddr);
1337 // Configure with STUN server but destroy it, so we can ensure that it's
1338 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001339 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001340 stun_server_.reset();
1341 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1342
1343 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001344 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1345 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001346
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001347 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001348 session_->StartGettingPorts();
1349
1350 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001351 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1352 Candidate stun_candidate;
1353 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1354 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1355 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1356 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1357 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001358
1359 // Don't bother waiting for STUN timeout, since we already verified
1360 // that we got a STUN candidate from the TURN server.
1361}
1362
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001363// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1364// and fail to generate STUN candidate, local UDP candidate is generated
1365// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001366TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001367 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1368 PORTALLOCATOR_DISABLE_TCP |
1369 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001370 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1371 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001372 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001373 session_->StartGettingPorts();
1374 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1375 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001376 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001377 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1378 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001379 EXPECT_EQ(1U, candidates_.size());
1380}
1381
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001382// Test that when the NetworkManager doesn't have permission to enumerate
1383// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1384// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001385TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001386 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1387 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001388 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001389 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001390 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1391 PORTALLOCATOR_DISABLE_TCP |
1392 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1393 EXPECT_EQ(0U,
1394 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1395 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1396 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001397 session_->StartGettingPorts();
1398 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001399 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001400 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001401 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001402}
1403
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001404// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001405TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001406 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1407 PORTALLOCATOR_ENABLE_IPV6 |
1408 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001409 AddInterface(kClientIPv6Addr);
1410 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001411 allocator_->set_step_delay(kMinimumStepDelay);
1412 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 session_->StartGettingPorts();
1414 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1415 EXPECT_EQ(4U, candidates_.size());
1416 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001417 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1418 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1419 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1420 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421 EXPECT_EQ(4U, candidates_.size());
1422}
honghaiz98db68f2015-09-29 07:58:17 -07001423
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001424TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001425 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001426 allocator_->set_step_delay(kDefaultStepDelay);
1427 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001428 session_->StartGettingPorts();
1429 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1430 EXPECT_EQ(2U, ports_.size());
1431 session_->StopGettingPorts();
1432 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1433
1434 // After stopping getting ports, adding a new interface will not start
1435 // getting ports again.
1436 candidates_.clear();
1437 ports_.clear();
1438 candidate_allocation_done_ = false;
1439 network_manager_.AddInterface(kClientAddr2);
1440 rtc::Thread::Current()->ProcessMessages(1000);
1441 EXPECT_EQ(0U, candidates_.size());
1442 EXPECT_EQ(0U, ports_.size());
1443}
1444
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001445TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001446 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001447 allocator_->set_step_delay(kDefaultStepDelay);
1448 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001449 session_->StartGettingPorts();
1450 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1451 EXPECT_EQ(2U, ports_.size());
1452 session_->ClearGettingPorts();
1453 WAIT(candidate_allocation_done_, 1000);
1454 EXPECT_FALSE(candidate_allocation_done_);
1455
1456 // After clearing getting ports, adding a new interface will start getting
1457 // ports again.
1458 candidates_.clear();
1459 ports_.clear();
1460 candidate_allocation_done_ = false;
1461 network_manager_.AddInterface(kClientAddr2);
1462 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1463 EXPECT_EQ(2U, ports_.size());
1464}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001465
1466// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1467// a pooled session is taken out of the pool.
1468TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1469 AddInterface(kClientAddr);
1470 int pool_size = 1;
1471 allocator_->SetConfiguration(allocator_->stun_servers(),
1472 allocator_->turn_servers(), pool_size);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001473 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001474 ASSERT_NE(nullptr, peeked_session);
1475 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1476 kDefaultAllocationTimeout);
1477 // Expect that when TakePooledSession is called,
1478 // UpdateTransportInformationInternal will be called and the
1479 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1480 // candidates.
1481 session_ =
1482 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1483 ASSERT_NE(nullptr, session_.get());
1484 auto ready_ports = session_->ReadyPorts();
1485 auto candidates = session_->ReadyCandidates();
1486 EXPECT_FALSE(ready_ports.empty());
1487 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001488 for (const PortInterface* port_interface : ready_ports) {
1489 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001490 EXPECT_EQ(kContentName, port->content_name());
1491 EXPECT_EQ(1, port->component());
1492 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1493 EXPECT_EQ(kIcePwd0, port->password());
1494 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001495 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001496 EXPECT_EQ(1, candidate.component());
1497 EXPECT_EQ(kIceUfrag0, candidate.username());
1498 EXPECT_EQ(kIcePwd0, candidate.password());
1499 }
1500}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001501
1502// Test that a new candidate filter takes effect even on already-gathered
1503// candidates.
1504TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1505 AddInterface(kClientAddr);
1506 int pool_size = 1;
1507 allocator_->SetConfiguration(allocator_->stun_servers(),
1508 allocator_->turn_servers(), pool_size);
1509 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1510 ASSERT_NE(nullptr, peeked_session);
1511 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1512 kDefaultAllocationTimeout);
1513 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1514 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1515 allocator_->set_candidate_filter(CF_RELAY);
1516 // Assume that when TakePooledSession is called, the candidate filter will be
1517 // applied to the pooled session. This is tested by PortAllocatorTest.
1518 session_ =
1519 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1520 ASSERT_NE(nullptr, session_.get());
1521 auto candidates = session_->ReadyCandidates();
1522 auto ports = session_->ReadyPorts();
1523 // Sanity check that the number of candidates and ports decreased.
1524 EXPECT_GT(initial_candidates_size, candidates.size());
1525 EXPECT_GT(initial_ports_size, ports.size());
1526 for (const PortInterface* port : ports) {
1527 // Expect only relay ports.
1528 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1529 }
1530 for (const Candidate& candidate : candidates) {
1531 // Expect only relay candidates now that the filter is applied.
1532 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1533 // Expect that the raddr is emptied due to the CF_RELAY filter.
1534 EXPECT_EQ(candidate.related_address(),
1535 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1536 }
1537}
1538
1539} // namespace cricket