blob: 29d29ce1f4d7429b59be2ad9aec8e75f30df2c6d [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"
pthatcher1749bc32017-02-08 13:18:00 -080021#include "webrtc/base/fakeclock.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022#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
deadbeefe97389c2016-12-23 01:43:45 -080041static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070043static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070044static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000046static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070047static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
48 0);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070049static const SocketAddress kClientIPv6Addr2(
50 "2401:fa00:4:2000:be30:5bff:fee5:c3",
51 0);
deadbeefc5d0d952015-07-16 10:22:21 -070052static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
53static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
55static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
56static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
57static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
58static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
59static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
60static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
61static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
62static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070063static const SocketAddress kTurnUdpIntIPv6Addr(
64 "2402:fb00:4:1000:be30:5bff:fee5:c3",
65 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070067static const SocketAddress kTurnTcpIntIPv6Addr(
68 "2402:fb00:4:2000:be30:5bff:fee5:c3",
69 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
71
72// Minimum and maximum port for port range tests.
73static const int kMinPort = 10000;
74static const int kMaxPort = 10099;
75
76// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 10:13:32 -070077static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078// Based on ICE_PWD_LENGTH
79static const char kIcePwd0[] = "TESTICEPWD00000000000000";
80
81static const char kContentName[] = "test content";
82
Honghai Zhangc67e0f52016-09-19 16:57:37 -070083static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084static const char kTurnUsername[] = "test";
85static const char kTurnPassword[] = "test";
86
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070087// STUN timeout (with all retries) is 9500ms.
88// Add some margin of error for slow bots.
89// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
90// fix flaky tests.
91static const int kStunTimeoutMs = 15000;
92
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093namespace cricket {
94
95// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070096std::ostream& operator<<(std::ostream& os,
97 const std::vector<Candidate>& candidates) {
98 os << '[';
99 bool first = true;
100 for (const Candidate& c : candidates) {
101 if (!first) {
102 os << ", ";
103 }
104 os << c.ToString();
105 first = false;
106 };
107 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 return os;
109}
110
pthatcher1749bc32017-02-08 13:18:00 -0800111class BasicPortAllocatorTestBase : public testing::Test,
112 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 public:
pthatcher1749bc32017-02-08 13:18:00 -0800114 BasicPortAllocatorTestBase()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 : pss_(new rtc::PhysicalSocketServer),
116 vss_(new rtc::VirtualSocketServer(pss_.get())),
117 fss_(new rtc::FirewallSocketServer(vss_.get())),
118 ss_scope_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800119 // Note that the NAT is not used by default. ResetWithStunServerAndNat
120 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700121 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700122 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700123 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700124 relay_server_(Thread::Current(),
125 kRelayUdpIntAddr,
126 kRelayUdpExtAddr,
127 kRelayTcpIntAddr,
128 kRelayTcpExtAddr,
129 kRelaySslTcpIntAddr,
130 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
132 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700133 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134 stun_servers.insert(kStunAddr);
135 // Passing the addresses of GTURN servers will enable GTURN in
136 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800137 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
138 // configuration should be blank by default (preferred), or it should use
139 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700140 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
141 kRelayUdpIntAddr, kRelayTcpIntAddr,
142 kRelaySslTcpIntAddr));
143 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144 }
145
146 void AddInterface(const SocketAddress& addr) {
147 network_manager_.AddInterface(addr);
148 }
honghaiz8c404fa2015-09-28 07:59:43 -0700149 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
150 network_manager_.AddInterface(addr, if_name);
151 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800152 void AddInterface(const SocketAddress& addr,
153 const std::string& if_name,
154 rtc::AdapterType type) {
155 network_manager_.AddInterface(addr, if_name, type);
156 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800157 // The default route is the public address that STUN server will observe when
158 // the endpoint is sitting on the public internet and the local port is bound
159 // to the "any" address. This may be different from the default local address
160 // which the endpoint observes. This can occur if the route to the public
161 // endpoint like 8.8.8.8 (specified as the default local address) is
162 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700163 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
164 AddInterface(addr);
165 // When a binding comes from the any address, the |addr| will be used as the
166 // srflx address.
167 vss_->SetDefaultRoute(addr.ipaddr());
168 }
honghaiz8c404fa2015-09-28 07:59:43 -0700169 void RemoveInterface(const SocketAddress& addr) {
170 network_manager_.RemoveInterface(addr);
171 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000172 bool SetPortRange(int min_port, int max_port) {
173 return allocator_->SetPortRange(min_port, max_port);
174 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700175 // Endpoint is on the public network. No STUN or TURN.
176 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700177 allocator_.reset(new BasicPortAllocator(&network_manager_));
178 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700179 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700180 // Endpoint is behind a NAT, with STUN specified.
181 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
182 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700183 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700184 // Endpoint is on the public network, with STUN specified.
185 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
186 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700188 // Endpoint is on the public network, with TURN specified.
189 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
190 const rtc::SocketAddress& tcp_turn) {
191 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 AddTurnServers(udp_turn, tcp_turn);
193 }
194
195 void AddTurnServers(const rtc::SocketAddress& udp_turn,
196 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700197 RelayServerConfig turn_server(RELAY_TURN);
198 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800199 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200
201 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800202 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203 }
204 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800205 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 }
deadbeef653b8e02015-11-11 12:55:10 -0800207 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000208 }
209
210 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700211 session_ = CreateSession("session", component);
212 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700214 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215 return true;
216 }
217
218 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700219 session_ = CreateSession("session", content_name, component);
220 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700222 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000223 return true;
224 }
225
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700226 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
227 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000228 return CreateSession(sid, kContentName, component);
229 }
230
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700231 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700232 const std::string& sid,
233 const std::string& content_name,
234 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
236 }
237
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700238 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700239 const std::string& sid,
240 const std::string& content_name,
241 int component,
242 const std::string& ice_ufrag,
243 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700244 std::unique_ptr<PortAllocatorSession> session =
245 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800247 &BasicPortAllocatorTestBase::OnPortReady);
248 session->SignalPortsPruned.connect(
249 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700250 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800251 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700252 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800253 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700254 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800255 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000256 return session;
257 }
258
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700259 // Return true if the addresses are the same, or the port is 0 in |pattern|
260 // (acting as a wildcard) and the IPs are the same.
261 // Even with a wildcard port, the port of the address should be nonzero if
262 // the IP is nonzero.
263 static bool AddressMatch(const SocketAddress& address,
264 const SocketAddress& pattern) {
265 return address.ipaddr() == pattern.ipaddr() &&
266 ((pattern.port() == 0 &&
267 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
268 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000269 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700270
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700271 // Returns the number of ports that have matching type, protocol and
272 // address.
273 static int CountPorts(const std::vector<PortInterface*>& ports,
274 const std::string& type,
275 ProtocolType protocol,
276 const SocketAddress& client_addr) {
277 return std::count_if(
278 ports.begin(), ports.end(),
279 [type, protocol, client_addr](PortInterface* port) {
280 return port->Type() == type && port->GetProtocol() == protocol &&
281 port->Network()->GetBestIP() == client_addr.ipaddr();
282 });
283 }
284
deadbeefe97389c2016-12-23 01:43:45 -0800285 static int CountCandidates(const std::vector<Candidate>& candidates,
286 const std::string& type,
287 const std::string& proto,
288 const SocketAddress& addr) {
289 return std::count_if(candidates.begin(), candidates.end(),
290 [type, proto, addr](const Candidate& c) {
291 return c.type() == type && c.protocol() == proto &&
292 AddressMatch(c.address(), addr);
293 });
294 }
295
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700296 // Find a candidate and return it.
297 static bool FindCandidate(const std::vector<Candidate>& candidates,
298 const std::string& type,
299 const std::string& proto,
300 const SocketAddress& addr,
301 Candidate* found) {
302 auto it = std::find_if(candidates.begin(), candidates.end(),
303 [type, proto, addr](const Candidate& c) {
304 return c.type() == type && c.protocol() == proto &&
305 AddressMatch(c.address(), addr);
306 });
307 if (it != candidates.end() && found) {
308 *found = *it;
309 }
310 return it != candidates.end();
311 }
312
313 // Convenience method to call FindCandidate with no return.
314 static bool HasCandidate(const std::vector<Candidate>& candidates,
315 const std::string& type,
316 const std::string& proto,
317 const SocketAddress& addr) {
318 return FindCandidate(candidates, type, proto, addr, nullptr);
319 }
320
321 // Version of HasCandidate that also takes a related address.
322 static bool HasCandidateWithRelatedAddr(
323 const std::vector<Candidate>& candidates,
324 const std::string& type,
325 const std::string& proto,
326 const SocketAddress& addr,
327 const SocketAddress& related_addr) {
328 auto it =
329 std::find_if(candidates.begin(), candidates.end(),
330 [type, proto, addr, related_addr](const Candidate& c) {
331 return c.type() == type && c.protocol() == proto &&
332 AddressMatch(c.address(), addr) &&
333 AddressMatch(c.related_address(), related_addr);
334 });
335 return it != candidates.end();
336 }
337
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000338 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700339 int min_port,
340 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341 return (addr.port() >= min_port && addr.port() <= max_port);
342 }
343
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700344 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000345 // We should only get this callback once, except in the mux test where
346 // we have multiple port allocation sessions.
347 if (session == session_.get()) {
348 ASSERT_FALSE(candidate_allocation_done_);
349 candidate_allocation_done_ = true;
350 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700351 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000352 }
353
354 // Check if all ports allocated have send-buffer size |expected|. If
355 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
356 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700357 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000358 for (it = ports_.begin(); it < ports_.end(); ++it) {
359 int send_buffer_size;
360 if (expected == -1) {
361 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700362 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000363 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700364 EXPECT_EQ(0,
365 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366 ASSERT_EQ(expected, send_buffer_size);
367 }
368 }
369 }
370
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700371 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
372
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700374 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700376 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000377 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
378 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700379 // Make sure the new port is added to ReadyPorts.
380 auto ready_ports = ses->ReadyPorts();
381 EXPECT_NE(ready_ports.end(),
382 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700384 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700385 const std::vector<PortInterface*>& pruned_ports) {
386 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700387 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700388 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700389 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700390 new_end = std::remove(ports_.begin(), new_end, port);
391 // Make sure the pruned port is not in ReadyPorts.
392 EXPECT_EQ(ready_ports.end(),
393 std::find(ready_ports.begin(), ready_ports.end(), port));
394 }
395 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700396 }
397
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700398 void OnCandidatesReady(PortAllocatorSession* ses,
399 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700400 for (const Candidate& candidate : candidates) {
401 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
402 // Sanity check that the ICE component is set.
403 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
404 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000405 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700406 // Make sure the new candidates are added to Candidates.
407 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700408 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700409 EXPECT_NE(
410 ses_candidates.end(),
411 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
412 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413 }
414
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700415 void OnCandidatesRemoved(PortAllocatorSession* session,
416 const std::vector<Candidate>& removed_candidates) {
417 auto new_end = std::remove_if(
418 candidates_.begin(), candidates_.end(),
419 [removed_candidates](Candidate& candidate) {
420 for (const Candidate& removed_candidate : removed_candidates) {
421 if (candidate.MatchesForRemoval(removed_candidate)) {
422 return true;
423 }
424 }
425 return false;
426 });
427 candidates_.erase(new_end, candidates_.end());
428 }
429
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700430 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800431 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700432 RelayServerConfig server_config = allocator_->turn_servers()[i];
433 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000434 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700435 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000436 if (proto_addr.address == relay_port->address &&
437 proto_addr.proto == relay_port->proto)
438 return true;
439 }
440 }
441 return false;
442 }
443
Guo-wei Shieh11477022015-08-15 09:28:41 -0700444 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
445 bool with_nat) {
446 if (with_nat) {
447 nat_server_.reset(new rtc::NATServer(
448 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
449 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
450 } else {
451 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
452 }
453
454 ServerAddresses stun_servers;
455 if (!stun_server.IsNil()) {
456 stun_servers.insert(stun_server);
457 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700458 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700459 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700460 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700461 }
462
pthatcher1749bc32017-02-08 13:18:00 -0800463 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
464 std::unique_ptr<rtc::VirtualSocketServer> vss_;
465 std::unique_ptr<rtc::FirewallSocketServer> fss_;
466 rtc::SocketServerScope ss_scope_;
467 std::unique_ptr<rtc::NATServer> nat_server_;
468 rtc::NATSocketFactory nat_factory_;
469 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
470 std::unique_ptr<TestStunServer> stun_server_;
471 TestRelayServer relay_server_;
472 TestTurnServer turn_server_;
473 rtc::FakeNetworkManager network_manager_;
474 std::unique_ptr<BasicPortAllocator> allocator_;
475 std::unique_ptr<PortAllocatorSession> session_;
476 std::vector<PortInterface*> ports_;
477 std::vector<Candidate> candidates_;
478 bool candidate_allocation_done_;
479};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700480
pthatcher1749bc32017-02-08 13:18:00 -0800481class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
482};
483
484class FakeClockBase {
485 public:
486 rtc::ScopedFakeClock fake_clock;
487};
488
489class BasicPortAllocatorTest : public FakeClockBase,
490 public BasicPortAllocatorTestBase {
491 public:
492 // This function starts the port/address gathering and check the existence of
493 // candidates as specified. When |expect_stun_candidate| is true,
494 // |stun_candidate_addr| carries the expected reflective address, which is
495 // also the related address for TURN candidate if it is expected. Otherwise,
496 // it should be ignore.
497 void CheckDisableAdapterEnumeration(
498 uint32_t total_ports,
499 const rtc::IPAddress& host_candidate_addr,
500 const rtc::IPAddress& stun_candidate_addr,
501 const rtc::IPAddress& relay_candidate_udp_transport_addr,
502 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
503 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
504 rtc::IPAddress());
505 if (!session_) {
506 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
507 }
508 session_->set_flags(session_->flags() |
509 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
510 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
511 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700512 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800513 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
514 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700515
pthatcher1749bc32017-02-08 13:18:00 -0800516 uint32_t total_candidates = 0;
517 if (!host_candidate_addr.IsNil()) {
518 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
519 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
520 ++total_candidates;
521 }
522 if (!stun_candidate_addr.IsNil()) {
523 rtc::SocketAddress related_address(host_candidate_addr, 0);
524 if (host_candidate_addr.IsNil()) {
525 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
526 }
527 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
528 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
529 ++total_candidates;
530 }
531 if (!relay_candidate_udp_transport_addr.IsNil()) {
532 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
533 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
534 rtc::SocketAddress(stun_candidate_addr, 0));
535 ++total_candidates;
536 }
537 if (!relay_candidate_tcp_transport_addr.IsNil()) {
538 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
539 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
540 rtc::SocketAddress(stun_candidate_addr, 0));
541 ++total_candidates;
542 }
543
544 EXPECT_EQ(total_candidates, candidates_.size());
545 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700546 }
547
548 void TestIPv6TurnPortPrunesIPv4TurnPort() {
549 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
550 // Add two IP addresses on the same interface.
551 AddInterface(kClientAddr, "net1");
552 AddInterface(kClientIPv6Addr, "net1");
553 allocator_.reset(new BasicPortAllocator(&network_manager_));
554 allocator_->SetConfiguration(allocator_->stun_servers(),
555 allocator_->turn_servers(), 0, true);
556 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
557 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
558
559 allocator_->set_step_delay(kMinimumStepDelay);
560 allocator_->set_flags(
561 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
562 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
563
564 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
565 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800566 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
567 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700568 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
569 EXPECT_EQ(3U, session_->ReadyPorts().size());
570 EXPECT_EQ(3U, ports_.size());
571 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
572 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
573 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
574 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
575
576 // Now that we remove candidates when a TURN port is pruned, there will be
577 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
578 EXPECT_EQ(3U, candidates_.size());
579 const std::vector<Candidate>& ready_candidates =
580 session_->ReadyCandidates();
581 EXPECT_EQ(3U, ready_candidates.size());
582 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
583 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
584 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
585 }
586
pthatcher1749bc32017-02-08 13:18:00 -0800587 void TestUdpTurnPortPrunesTcpTurnPort() {
588 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
589 AddInterface(kClientAddr);
590 allocator_.reset(new BasicPortAllocator(&network_manager_));
591 allocator_->SetConfiguration(allocator_->stun_servers(),
592 allocator_->turn_servers(), 0, true);
593 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
594 allocator_->set_step_delay(kMinimumStepDelay);
595 allocator_->set_flags(allocator().flags() |
596 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
597 PORTALLOCATOR_DISABLE_TCP);
598
599 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
600 session_->StartGettingPorts();
601 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
602 kDefaultAllocationTimeout, fake_clock);
603 // Only 2 ports (one STUN and one TURN) are actually being used.
604 EXPECT_EQ(2U, session_->ReadyPorts().size());
605 // We have verified that each port, when it is added to |ports_|, it is
606 // found in |ready_ports|, and when it is pruned, it is not found in
607 // |ready_ports|, so we only need to verify the content in one of them.
608 EXPECT_EQ(2U, ports_.size());
609 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
610 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
611 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
612
613 // Now that we remove candidates when a TURN port is pruned, |candidates_|
614 // should only contains two candidates regardless whether the TCP TURN port
615 // is created before or after the UDP turn port.
616 EXPECT_EQ(2U, candidates_.size());
617 // There will only be 2 candidates in |ready_candidates| because it only
618 // includes the candidates in the ready ports.
619 const std::vector<Candidate>& ready_candidates =
620 session_->ReadyCandidates();
621 EXPECT_EQ(2U, ready_candidates.size());
622 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
623 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
624 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
625 }
626
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700627 void TestEachInterfaceHasItsOwnTurnPorts() {
628 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
629 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
630 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
631 // Add two interfaces both having IPv4 and IPv6 addresses.
632 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
633 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
634 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
635 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
636 allocator_.reset(new BasicPortAllocator(&network_manager_));
637 allocator_->SetConfiguration(allocator_->stun_servers(),
638 allocator_->turn_servers(), 0, true);
639 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
640 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
641 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
642
643 allocator_->set_step_delay(kMinimumStepDelay);
644 allocator_->set_flags(allocator().flags() |
645 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
646 PORTALLOCATOR_ENABLE_IPV6);
647 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
648 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800649 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
650 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700651 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
652 // use.
653 EXPECT_EQ(10U, session_->ReadyPorts().size());
654 EXPECT_EQ(10U, ports_.size());
655 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
656 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
657 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
658 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
659 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
660 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
661 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
662 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
663 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
664 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
665
666 // Now that we remove candidates when TURN ports are pruned, there will be
667 // exactly 10 candidates in |candidates_|.
668 EXPECT_EQ(10U, candidates_.size());
669 const std::vector<Candidate>& ready_candidates =
670 session_->ReadyCandidates();
671 EXPECT_EQ(10U, ready_candidates.size());
672 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
673 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
674 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
675 kClientIPv6Addr);
676 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
677 kClientIPv6Addr2);
678 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
679 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
680 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
681 kClientIPv6Addr);
682 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
683 kClientIPv6Addr2);
684 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
685 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
686 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000687};
688
689// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700690TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000691 EXPECT_EQ(&network_manager_, allocator().network_manager());
692 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800693 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700694 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000695 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800696 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
697 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700698 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
699 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
700 EXPECT_TRUE(
701 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
702 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700703 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000704}
705
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800706// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700707TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
709 rtc::ADAPTER_TYPE_ETHERNET);
710 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
711 rtc::ADAPTER_TYPE_WIFI);
712 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
713 rtc::ADAPTER_TYPE_CELLULAR);
714 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
715 rtc::ADAPTER_TYPE_VPN);
716 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
717 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700718 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
719 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
720 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800721 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800722 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
723 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700725 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 EXPECT_LT(candidate.address().ip(), 0x12345604U);
727 }
728}
729
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800731 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
732 rtc::ADAPTER_TYPE_ETHERNET);
733 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
734 rtc::ADAPTER_TYPE_WIFI);
735 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
736 rtc::ADAPTER_TYPE_CELLULAR);
737 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
738 rtc::ADAPTER_TYPE_LOOPBACK |
739 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700740 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
741 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
742 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800743 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800744 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
745 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800746 EXPECT_EQ(1U, candidates_.size());
747 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
748}
749
honghaiz60347052016-05-31 18:29:12 -0700750// Test that high cost networks are filtered if the flag
751// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
752TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
753 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
754 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
755 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
756 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
757 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
758 // interface.
759 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
760 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
761 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
762 cricket::PORTALLOCATOR_DISABLE_RELAY |
763 cricket::PORTALLOCATOR_DISABLE_TCP |
764 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
765 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
766 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800767 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
768 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700769 EXPECT_EQ(1U, candidates_.size());
770 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
771
772 // If both cellular and unknown interfaces are present, only gather on the
773 // unknown interfaces.
774 candidates_.clear();
775 candidate_allocation_done_ = false;
776 RemoveInterface(addr_wifi);
777 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
778 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
779 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800780 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
781 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700782 EXPECT_EQ(2U, candidates_.size());
783 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
784 addr_unknown2.EqualIPs(candidates_[1].address())) ||
785 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
786 addr_unknown2.EqualIPs(candidates_[0].address())));
787
788 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
789 // Wi-Fi interface.
790 candidates_.clear();
791 candidate_allocation_done_ = false;
792 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
793 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800794 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
795 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700796 EXPECT_EQ(1U, candidates_.size());
797 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
798}
799
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700800// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700801TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800802 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
803 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700804 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
805 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
806 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700807 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800808 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
809 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700810 EXPECT_EQ(1U, candidates_.size());
811}
812
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000813// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700814TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700816 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000817 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800818 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
819 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000820 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700821 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
822 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
823 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
824 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
825 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
826 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
827 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
828 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000829 EXPECT_TRUE(candidate_allocation_done_);
830}
831
honghaiz8c404fa2015-09-28 07:59:43 -0700832// Test that when the same network interface is brought down and up, the
833// port allocator session will restart a new allocation sequence if
834// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700835TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700836 std::string if_name("test_net0");
837 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700838 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700839 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800840 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
841 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700842 EXPECT_EQ(4U, ports_.size());
843 EXPECT_TRUE(candidate_allocation_done_);
844 candidate_allocation_done_ = false;
845 candidates_.clear();
846 ports_.clear();
847
848 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800849 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
850 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700851 EXPECT_EQ(0U, ports_.size());
852 EXPECT_FALSE(candidate_allocation_done_);
853
854 // When the same interfaces are added again, new candidates/ports should be
855 // generated.
856 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800857 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
858 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700859 EXPECT_EQ(4U, ports_.size());
860 EXPECT_TRUE(candidate_allocation_done_);
861}
862
863// Test that when the same network interface is brought down and up, the
864// port allocator session will not restart a new allocation sequence if
865// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700866TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700867 std::string if_name("test_net0");
868 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700869 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700870 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800871 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
872 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700873 EXPECT_EQ(4U, ports_.size());
874 EXPECT_TRUE(candidate_allocation_done_);
875 session_->StopGettingPorts();
876 candidates_.clear();
877 ports_.clear();
878
879 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800880 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
881 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700882 EXPECT_EQ(0U, ports_.size());
883
884 // When the same interfaces are added again, new candidates/ports should not
885 // be generated because the session has stopped.
886 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800887 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
888 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700889 EXPECT_EQ(0U, ports_.size());
890 EXPECT_TRUE(candidate_allocation_done_);
891}
892
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000893// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700894TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700896 allocator_->set_step_delay(kDefaultStepDelay);
897 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800899 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800901 ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000902 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700903 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
904 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800905 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700906 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
907 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000908 EXPECT_EQ(4U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800909 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700910 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
911 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000912 EXPECT_EQ(4U, ports_.size());
913 EXPECT_TRUE(candidate_allocation_done_);
914 // If we Stop gathering now, we shouldn't get a second "done" callback.
915 session_->StopGettingPorts();
916}
917
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700918TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000919 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700920 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000921 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800922 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
923 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000924 EXPECT_TRUE(candidate_allocation_done_);
925 // If we Stop gathering now, we shouldn't get a second "done" callback.
926 session_->StopGettingPorts();
927
928 // All ports should have unset send-buffer sizes.
929 CheckSendBufferSizesOfAllPorts(-1);
930}
931
932// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700933TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000934 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700935 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 session_->StartGettingPorts();
937 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
938 EXPECT_EQ(2U, ports_.size());
939 session_->StopGettingPorts();
940 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
941}
942
943// Test that we restrict client ports appropriately when a port range is set.
944// We check the candidates for udp/stun/tcp ports, and the from address
945// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700946TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000947 AddInterface(kClientAddr);
948 // Check that an invalid port range fails.
949 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
950 // Check that a null port range succeeds.
951 EXPECT_TRUE(SetPortRange(0, 0));
952 // Check that a valid port range succeeds.
953 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700954 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800956 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
957 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000958 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -0700959
960 int num_nonrelay_candidates = 0;
961 for (const Candidate& candidate : candidates_) {
962 // Check the port number for the UDP/STUN/TCP port objects.
963 if (candidate.type() != RELAY_PORT_TYPE) {
964 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
965 ++num_nonrelay_candidates;
966 }
967 }
968 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000969 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700970 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
971 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000972 EXPECT_TRUE(candidate_allocation_done_);
973}
974
deadbeefe97389c2016-12-23 01:43:45 -0800975// Test that if we have no network adapters, we bind to the ANY address and
976// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700977TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -0800978 // Default config uses GTURN and no NAT, so replace that with the
979 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
980 ResetWithStunServerAndNat(kStunAddr);
981 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
982 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
983 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
984 // Disable IPv6, because our test infrastructure doesn't support having IPv4
985 // behind a NAT but IPv6 not, or having an IPv6 NAT.
986 // TODO(deadbeef): Fix this.
987 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700988 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800990 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
991 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -0800992 EXPECT_EQ(4U, ports_.size());
993 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
994 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
995 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
996 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
997 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
998 // The "any" address port should be in the signaled ready ports, but the host
999 // candidate for it is useless and shouldn't be signaled. So we only have
1000 // STUN/TURN candidates.
1001 EXPECT_EQ(3U, candidates_.size());
1002 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1003 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1004 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1005 // server.
1006 EXPECT_EQ(2,
1007 CountCandidates(candidates_, "relay", "udp",
1008 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001009}
1010
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001011// Test that when enumeration is disabled, we should not have any ports when
1012// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001013TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001014 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001015 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001016 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001017 // Expect to see no ports and no candidates.
1018 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1019 rtc::IPAddress(), rtc::IPAddress());
1020}
1021
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001022// Test that even with multiple interfaces, the result should still be a single
1023// default private, one STUN and one TURN candidate since we bind to any address
1024// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001025TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001026 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001027 AddInterface(kPrivateAddr);
1028 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001029 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001030 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001031
1032 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1033 // address set and we have no IPv6 STUN server, there should be no IPv6
1034 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001035 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1036 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001037
1038 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1039 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1040 // TURN/UDP candidates.
1041 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001042 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1043 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001044}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001045
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001046// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1047// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001048TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001049 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001050 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001051 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001052 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001053 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1054 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1055 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1056 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001057 kTurnUdpExtAddr.ipaddr());
1058}
1059
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001060// Test that when adapter enumeration is disabled, for endpoints without
1061// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001062TEST_F(BasicPortAllocatorTest,
1063 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001064 ResetWithNoServersOrNat();
1065 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1066 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1067 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001068}
1069
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001070// Test that when adapter enumeration is disabled, with
1071// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1072// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001073TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001074 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1075 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001076 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1077 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001078 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1079 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001080 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001081 rtc::IPAddress(), rtc::IPAddress());
1082}
1083
1084// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001085// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1086// a NAT, there is no local candidate. However, this specified default route
1087// (kClientAddr) which was discovered when sending STUN requests, will become
1088// the srflx addresses.
1089TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001090 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001091 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001092 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001093 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001094 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1095 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001096 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1097 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001098 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1099 rtc::IPAddress(), rtc::IPAddress());
1100}
1101
1102// Test that when adapter enumeration is disabled, with
1103// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1104// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001105TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001106 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1107 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001108 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1109 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001110 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1111 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1112 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001113}
1114
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001115// Test that we disable relay over UDP, and only TCP is used when connecting to
1116// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001117TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001118 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001119 AddInterface(kClientAddr);
1120 ResetWithStunServerAndNat(kStunAddr);
1121 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001122 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1123 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1124 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1125 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001126
1127 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001128 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1129 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001130
1131 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1132 // TURN/TCP candidates.
1133 EXPECT_EQ(2U, ports_.size());
1134 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001135 Candidate turn_candidate;
1136 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1137 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001138 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001139 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1140 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001141}
1142
Erik Språngefdce692015-06-05 09:41:26 +02001143// Disable for asan, see
1144// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1145#if !defined(ADDRESS_SANITIZER)
1146
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001147// Test that we can get OnCandidatesAllocationDone callback when all the ports
1148// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001149TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001150 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001151 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1152 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1153 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001155 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157}
1158
1159// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001160TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001161 AddInterface(kClientAddr);
1162 fss_->set_udp_sockets_enabled(false);
1163 EXPECT_TRUE(CreateSession(1));
1164 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001165 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1166 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001167 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001168 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1169 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1170 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1171 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1172 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1173 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001174 EXPECT_TRUE(candidate_allocation_done_);
1175}
1176
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001177#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001178
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001179// Test that we don't crash or malfunction if we can't create UDP sockets or
1180// listen on TCP sockets. We still give out a local TCP address, since
1181// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001182TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001183 AddInterface(kClientAddr);
1184 fss_->set_udp_sockets_enabled(false);
1185 fss_->set_tcp_listen_enabled(false);
1186 EXPECT_TRUE(CreateSession(1));
1187 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001188 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1189 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001191 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1192 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1193 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1194 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1195 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001196 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001197 EXPECT_TRUE(candidate_allocation_done_);
1198}
1199
1200// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001201// TODO(deadbeef): Find a way to exit early here.
1202TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001203 AddInterface(kClientAddr);
1204 fss_->set_tcp_sockets_enabled(false);
1205 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001206 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001207 session_->StartGettingPorts();
1208 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001209 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001210 // In case of Relay, ports creation will succeed but sockets will fail.
1211 // There is no error reporting from RelayEntry to handle this failure.
1212}
1213
1214// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001215TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001216 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1217 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001218 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001219 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001220 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1221 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001222 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001223 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1224 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001225 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001226 // will be tried after about 3 seconds.
1227 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001228 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001229 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1230 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1231 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1232 kRelaySslTcpIntAddr);
1233 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001234 // We wait at least for a full STUN timeout, which is currently 9.5
1235 // seconds. But since 3-3.5 seconds already passed (see above), we
1236 // only need 6.5 more seconds.
1237 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 6500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001238}
1239
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001240TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241 AddInterface(kClientAddr);
1242 AddInterface(kClientAddr2);
1243 // Allocating only host UDP ports. This is done purely for testing
1244 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001245 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1246 PORTALLOCATOR_DISABLE_RELAY);
1247 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001248 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001249 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1250 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001251 ASSERT_EQ(2U, candidates_.size());
1252 EXPECT_EQ(2U, ports_.size());
1253 // Candidates priorities should be different.
1254 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1255}
1256
1257// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001258TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001259 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001260 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001261 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001262 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1263 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001264 EXPECT_EQ(4U, ports_.size());
1265 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001266 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001267}
1268
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001269// Test that the allocator session uses the candidate filter it's created with,
1270// rather than the filter of its parent allocator.
1271// The filter of the allocator should only affect the next gathering phase,
1272// according to JSEP, which means the *next* allocator session returned.
1273TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1274 AddInterface(kClientAddr);
1275 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1276 // Set candidate filter *after* creating the session. Should have no effect.
1277 allocator().set_candidate_filter(CF_RELAY);
1278 session_->StartGettingPorts();
1279 // 7 candidates and 4 ports is what we would normally get (see the
1280 // TestGetAllPorts* tests).
pthatcher1749bc32017-02-08 13:18:00 -08001281 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1282 fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001283 EXPECT_EQ(4U, ports_.size());
1284 EXPECT_TRUE(candidate_allocation_done_);
1285}
1286
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001287// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1288// This test also verifies that when the allocator is only allowed to use
1289// relay (i.e. IceTransportsType is relay), the raddr is an empty
1290// address with the correct family. This is to prevent any local
1291// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001292TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001293 AddInterface(kClientAddr);
1294 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001295 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001296 allocator().set_candidate_filter(CF_RELAY);
1297 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001298 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001299 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1300 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001301 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1302 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001303
1304 EXPECT_EQ(1U, candidates_.size());
1305 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001306 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1307 EXPECT_EQ(
1308 candidates_[0].related_address(),
1309 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001310}
1311
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001312TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001313 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001314 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1315 allocator().set_candidate_filter(CF_HOST);
1316 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001317 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001318 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1319 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001320 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1321 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001322 for (const Candidate& candidate : candidates_) {
1323 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001324 }
1325}
1326
1327// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001328TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001329 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001330 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001331
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001332 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1333 allocator().set_candidate_filter(CF_REFLEXIVE);
1334 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001335 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001336 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1337 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001338 // Host is behind NAT, no private address will be exposed. Hence only UDP
1339 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001340 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1341 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001342 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1343 EXPECT_EQ(
1344 candidates_[0].related_address(),
1345 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001346}
1347
1348// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001349TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001350 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001351 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1352 allocator().set_candidate_filter(CF_REFLEXIVE);
1353 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001354 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001355 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1356 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001357 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001358 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001359 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001360 for (const Candidate& candidate : candidates_) {
1361 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001362 }
1363}
1364
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001365// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001366TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001367 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001368 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001369 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001370 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1371 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001372 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1373 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1374 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001375 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001376 for (const Candidate& candidate : candidates_) {
1377 EXPECT_EQ(kIceUfrag0, candidate.username());
1378 EXPECT_EQ(kIcePwd0, candidate.password());
1379 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001380 EXPECT_TRUE(candidate_allocation_done_);
1381}
1382
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001383// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1384// is allocated for udp and stun. Also verify there is only one candidate
1385// (local) if stun candidate is same as local candidate, which will be the case
1386// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001387TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001388 AddInterface(kClientAddr);
1389 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001390 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1391 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001392 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001393 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1394 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001395 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001396 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001397 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1398 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001399}
1400
1401// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1402// is allocated for udp and stun. In this test we should expect both stun and
1403// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001404TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001406 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001407
1408 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001409 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1410 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001411 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001412 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1413 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001415 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1416 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1417 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001418 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1419 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001420 EXPECT_EQ(3U, candidates_.size());
1421}
1422
deadbeefc5d0d952015-07-16 10:22:21 -07001423// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001424TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001425 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001426 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001427 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001428
1429 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1430
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001431 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001432 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001433 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1434 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001435
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001436 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001437 session_->StartGettingPorts();
1438
pthatcher1749bc32017-02-08 13:18:00 -08001439 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1440 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001441 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001442 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1443 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1444 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1445 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1446 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001447 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1448 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001449 EXPECT_EQ(3U, candidates_.size());
1450}
1451
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001452// Test that if prune_turn_ports is set, TCP TURN port will not be used
1453// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1454TEST_F(BasicPortAllocatorTest,
1455 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1456 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1457 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1458 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001459
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001460 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001461}
1462
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001463// Test that if prune_turn_ports is set, TCP TURN port will not be used
1464// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1465TEST_F(BasicPortAllocatorTest,
1466 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1467 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1468 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1469 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001470
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001471 TestUdpTurnPortPrunesTcpTurnPort();
1472}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001473
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001474// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1475// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1476TEST_F(BasicPortAllocatorTest,
1477 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1478 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1479 // first.
1480 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1481 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001482
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001483 TestIPv6TurnPortPrunesIPv4TurnPort();
1484}
1485
1486// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1487// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1488TEST_F(BasicPortAllocatorTest,
1489 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1490 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1491 // first.
1492 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1493 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1494
1495 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001496}
1497
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001498// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001499// will has its own set of TurnPorts based on their priorities, in the default
1500// case where no transit delay is set.
1501TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1502 TestEachInterfaceHasItsOwnTurnPorts();
1503}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001504
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001505// Tests that if prune_turn_ports is set, each network interface
1506// will has its own set of TurnPorts based on their priorities, given that
1507// IPv4/TCP TURN port becomes ready first.
1508TEST_F(BasicPortAllocatorTest,
1509 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1510 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1511 // becomes ready last.
1512 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1513 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1514 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1515 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001516
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001517 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001518}
1519
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001520// Testing DNS resolve for the TURN server, this will test AllocationSequence
1521// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001522// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1523// appears that it doesn't currently because of the DNS look up not
1524// using the fake clock.
1525TEST_F(BasicPortAllocatorTestWithRealClock,
1526 TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001527 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001528 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001529 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001530 allocator_.reset(new BasicPortAllocator(&network_manager_));
1531 RelayServerConfig turn_server(RELAY_TURN);
1532 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001533 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001534 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001535 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001536 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001537
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001538 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001539 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001540 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1541 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001542
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001543 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001544 session_->StartGettingPorts();
1545
1546 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1547}
1548
1549// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1550// is allocated for udp/stun/turn. In this test we should expect all local,
1551// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001552TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001553 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001554 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001555
1556 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1557
1558 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001559 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1560 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001561
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001562 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001563 session_->StartGettingPorts();
1564
pthatcher1749bc32017-02-08 13:18:00 -08001565 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1566 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001567 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001568 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1569 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1570 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1571 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1572 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001573 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1574 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001575 EXPECT_EQ(3U, candidates_.size());
1576 // Local port will be created first and then TURN port.
1577 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1578 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1579}
1580
1581// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1582// server is also used as the STUN server, we should get 'local', 'stun', and
1583// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001584TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001585 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001586 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001587 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001588 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1589
1590 // Must set the step delay to 0 to make sure the relay allocation phase is
1591 // started before the STUN candidates are obtained, so that the STUN binding
1592 // response is processed when both StunPort and TurnPort exist to reproduce
1593 // webrtc issue 3537.
1594 allocator_->set_step_delay(0);
1595 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001596 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1597 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001598
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001599 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001600 session_->StartGettingPorts();
1601
pthatcher1749bc32017-02-08 13:18:00 -08001602 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1603 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001604 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1605 Candidate stun_candidate;
1606 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1607 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1608 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1609 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1610 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001611
pthatcher1749bc32017-02-08 13:18:00 -08001612 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1613 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001614 EXPECT_EQ(3U, candidates_.size());
1615 // Local port will be created first and then TURN port.
1616 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1617 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1618}
1619
deadbeefc5d0d952015-07-16 10:22:21 -07001620// Test that when only a TCP TURN server is available, we do NOT use it as
1621// a UDP STUN server, as this could leak our IP address. Thus we should only
1622// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001623TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001624 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001625 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001626 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001627 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1628
1629 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001630 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1631 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001632
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001633 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001634 session_->StartGettingPorts();
1635
pthatcher1749bc32017-02-08 13:18:00 -08001636 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1637 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001638 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001639 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1640 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1641 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001642 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1643 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001644 EXPECT_EQ(2U, candidates_.size());
1645 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1646 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1647}
1648
1649// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1650// TURN server is used as the STUN server and we get 'local', 'stun', and
1651// 'relay' candidates.
1652// TODO(deadbeef): Remove this test when support for non-shared socket mode
1653// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001654TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001655 AddInterface(kClientAddr);
1656 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001657 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001658 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1659
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001660 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001661
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001662 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001663 session_->StartGettingPorts();
1664
pthatcher1749bc32017-02-08 13:18:00 -08001665 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1666 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001667 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001668 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1669 Candidate stun_candidate;
1670 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1671 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1672 Candidate turn_candidate;
1673 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1674 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1675 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001676 // Not using shared socket, so the STUN request's server reflexive address
1677 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001678 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001679
pthatcher1749bc32017-02-08 13:18:00 -08001680 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1681 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001682 EXPECT_EQ(3U, candidates_.size());
1683 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1684 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1685 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1686}
1687
1688// Test that even when both a STUN and TURN server are configured, the TURN
1689// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001690TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001691 AddInterface(kClientAddr);
1692 // Configure with STUN server but destroy it, so we can ensure that it's
1693 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001694 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001695 stun_server_.reset();
1696 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1697
1698 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001699 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1700 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001701
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001702 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001703 session_->StartGettingPorts();
1704
pthatcher1749bc32017-02-08 13:18:00 -08001705 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1706 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001707 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1708 Candidate stun_candidate;
1709 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1710 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1711 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1712 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1713 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001714
1715 // Don't bother waiting for STUN timeout, since we already verified
1716 // that we got a STUN candidate from the TURN server.
1717}
1718
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001719// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1720// and fail to generate STUN candidate, local UDP candidate is generated
1721// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001722TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001723 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1724 PORTALLOCATOR_DISABLE_TCP |
1725 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001726 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1727 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001728 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001729 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001730 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1731 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001732 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001733 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001734 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001735 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1736 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001737 EXPECT_EQ(1U, candidates_.size());
1738}
1739
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001740// Test that when the NetworkManager doesn't have permission to enumerate
1741// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1742// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001743TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001744 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1745 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001746 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001747 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001748 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1749 PORTALLOCATOR_DISABLE_TCP |
1750 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1751 EXPECT_EQ(0U,
1752 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1753 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1754 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001755 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001756 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1757 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001758 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001759 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001760 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001761}
1762
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001763// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001764TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001765 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1766 PORTALLOCATOR_ENABLE_IPV6 |
1767 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001768 AddInterface(kClientIPv6Addr);
1769 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001770 allocator_->set_step_delay(kMinimumStepDelay);
1771 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001772 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001773 ASSERT_EQ_SIMULATED_WAIT(4U, ports_.size(), kDefaultAllocationTimeout,
1774 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001775 EXPECT_EQ(4U, candidates_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001776 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1777 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001778 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1779 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1780 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1781 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001782 EXPECT_EQ(4U, candidates_.size());
1783}
honghaiz98db68f2015-09-29 07:58:17 -07001784
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001785TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001786 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001787 allocator_->set_step_delay(kDefaultStepDelay);
1788 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001789 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001790 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001791 EXPECT_EQ(2U, ports_.size());
1792 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001793 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001794
1795 // After stopping getting ports, adding a new interface will not start
1796 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001797 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001798 candidates_.clear();
1799 ports_.clear();
1800 candidate_allocation_done_ = false;
1801 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001802 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001803 EXPECT_EQ(0U, candidates_.size());
1804 EXPECT_EQ(0U, ports_.size());
1805}
1806
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001807TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001808 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001809 allocator_->set_step_delay(kDefaultStepDelay);
1810 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001811 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001812 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001813 EXPECT_EQ(2U, ports_.size());
1814 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001815 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001816
1817 // After clearing getting ports, adding a new interface will start getting
1818 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001819 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001820 candidates_.clear();
1821 ports_.clear();
1822 candidate_allocation_done_ = false;
1823 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001824 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001825 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001826 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1827 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001828}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001829
1830// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1831// a pooled session is taken out of the pool.
1832TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1833 AddInterface(kClientAddr);
1834 int pool_size = 1;
1835 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001836 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001837 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001838 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001839 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1840 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001841 // Expect that when TakePooledSession is called,
1842 // UpdateTransportInformationInternal will be called and the
1843 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1844 // candidates.
1845 session_ =
1846 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1847 ASSERT_NE(nullptr, session_.get());
1848 auto ready_ports = session_->ReadyPorts();
1849 auto candidates = session_->ReadyCandidates();
1850 EXPECT_FALSE(ready_ports.empty());
1851 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001852 for (const PortInterface* port_interface : ready_ports) {
1853 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001854 EXPECT_EQ(kContentName, port->content_name());
1855 EXPECT_EQ(1, port->component());
1856 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1857 EXPECT_EQ(kIcePwd0, port->password());
1858 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001859 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001860 EXPECT_EQ(1, candidate.component());
1861 EXPECT_EQ(kIceUfrag0, candidate.username());
1862 EXPECT_EQ(kIcePwd0, candidate.password());
1863 }
1864}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001865
1866// Test that a new candidate filter takes effect even on already-gathered
1867// candidates.
1868TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1869 AddInterface(kClientAddr);
1870 int pool_size = 1;
1871 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001872 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001873 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1874 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001875 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1876 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001877 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1878 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1879 allocator_->set_candidate_filter(CF_RELAY);
1880 // Assume that when TakePooledSession is called, the candidate filter will be
1881 // applied to the pooled session. This is tested by PortAllocatorTest.
1882 session_ =
1883 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1884 ASSERT_NE(nullptr, session_.get());
1885 auto candidates = session_->ReadyCandidates();
1886 auto ports = session_->ReadyPorts();
1887 // Sanity check that the number of candidates and ports decreased.
1888 EXPECT_GT(initial_candidates_size, candidates.size());
1889 EXPECT_GT(initial_ports_size, ports.size());
1890 for (const PortInterface* port : ports) {
1891 // Expect only relay ports.
1892 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1893 }
1894 for (const Candidate& candidate : candidates) {
1895 // Expect only relay candidates now that the filter is applied.
1896 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1897 // Expect that the raddr is emptied due to the CF_RELAY filter.
1898 EXPECT_EQ(candidate.related_address(),
1899 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1900 }
1901}
1902
1903} // namespace cricket