blob: 6cc2259f0fe3b0cd5c199c8e07def71fc950ac2c [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
pthatcher94a2f212017-02-08 14:42:22 -080087// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070088// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 14:42:22 -080089static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070090
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091namespace cricket {
92
93// Helper for dumping candidates
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -070094std::ostream& operator<<(std::ostream& os,
95 const std::vector<Candidate>& candidates) {
96 os << '[';
97 bool first = true;
98 for (const Candidate& c : candidates) {
99 if (!first) {
100 os << ", ";
101 }
102 os << c.ToString();
103 first = false;
104 };
105 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106 return os;
107}
108
pthatcher1749bc32017-02-08 13:18:00 -0800109class BasicPortAllocatorTestBase : public testing::Test,
110 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 public:
pthatcher1749bc32017-02-08 13:18:00 -0800112 BasicPortAllocatorTestBase()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 : pss_(new rtc::PhysicalSocketServer),
114 vss_(new rtc::VirtualSocketServer(pss_.get())),
115 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 05:25:41 -0700116 thread_(fss_.get()),
deadbeefe97389c2016-12-23 01:43:45 -0800117 // Note that the NAT is not used by default. ResetWithStunServerAndNat
118 // must be called.
deadbeefc5d0d952015-07-16 10:22:21 -0700119 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700120 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700121 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700122 relay_server_(Thread::Current(),
123 kRelayUdpIntAddr,
124 kRelayUdpExtAddr,
125 kRelayTcpIntAddr,
126 kRelayTcpExtAddr,
127 kRelaySslTcpIntAddr,
128 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
130 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700131 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 stun_servers.insert(kStunAddr);
133 // Passing the addresses of GTURN servers will enable GTURN in
134 // Basicportallocator.
deadbeefe97389c2016-12-23 01:43:45 -0800135 // TODO(deadbeef): Stop using GTURN by default in this test... Either the
136 // configuration should be blank by default (preferred), or it should use
137 // TURN instead.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700138 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
139 kRelayUdpIntAddr, kRelayTcpIntAddr,
140 kRelaySslTcpIntAddr));
141 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142 }
143
144 void AddInterface(const SocketAddress& addr) {
145 network_manager_.AddInterface(addr);
146 }
honghaiz8c404fa2015-09-28 07:59:43 -0700147 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
148 network_manager_.AddInterface(addr, if_name);
149 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800150 void AddInterface(const SocketAddress& addr,
151 const std::string& if_name,
152 rtc::AdapterType type) {
153 network_manager_.AddInterface(addr, if_name, type);
154 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800155 // The default route is the public address that STUN server will observe when
156 // the endpoint is sitting on the public internet and the local port is bound
157 // to the "any" address. This may be different from the default local address
158 // which the endpoint observes. This can occur if the route to the public
159 // endpoint like 8.8.8.8 (specified as the default local address) is
160 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700161 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
162 AddInterface(addr);
163 // When a binding comes from the any address, the |addr| will be used as the
164 // srflx address.
165 vss_->SetDefaultRoute(addr.ipaddr());
166 }
honghaiz8c404fa2015-09-28 07:59:43 -0700167 void RemoveInterface(const SocketAddress& addr) {
168 network_manager_.RemoveInterface(addr);
169 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 bool SetPortRange(int min_port, int max_port) {
171 return allocator_->SetPortRange(min_port, max_port);
172 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700173 // Endpoint is on the public network. No STUN or TURN.
174 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700175 allocator_.reset(new BasicPortAllocator(&network_manager_));
176 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700177 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700178 // Endpoint is behind a NAT, with STUN specified.
179 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
180 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700181 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700182 // Endpoint is on the public network, with STUN specified.
183 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
184 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700186 // Endpoint is on the public network, with TURN specified.
187 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
188 const rtc::SocketAddress& tcp_turn) {
189 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 AddTurnServers(udp_turn, tcp_turn);
191 }
192
193 void AddTurnServers(const rtc::SocketAddress& udp_turn,
194 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700195 RelayServerConfig turn_server(RELAY_TURN);
196 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800197 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198
199 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800200 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000201 }
202 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 05:17:23 -0800203 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 }
deadbeef653b8e02015-11-11 12:55:10 -0800205 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 }
207
208 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700209 session_ = CreateSession("session", component);
210 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700212 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 return true;
214 }
215
216 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700217 session_ = CreateSession("session", content_name, component);
218 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700220 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000221 return true;
222 }
223
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700224 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
225 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000226 return CreateSession(sid, kContentName, component);
227 }
228
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700229 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700230 const std::string& sid,
231 const std::string& content_name,
232 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
234 }
235
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700236 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700237 const std::string& sid,
238 const std::string& content_name,
239 int component,
240 const std::string& ice_ufrag,
241 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 12:37:42 -0700242 std::unique_ptr<PortAllocatorSession> session =
243 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000244 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 13:18:00 -0800245 &BasicPortAllocatorTestBase::OnPortReady);
246 session->SignalPortsPruned.connect(
247 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700248 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800249 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700250 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800251 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700252 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 13:18:00 -0800253 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000254 return session;
255 }
256
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700257 // Return true if the addresses are the same, or the port is 0 in |pattern|
258 // (acting as a wildcard) and the IPs are the same.
259 // Even with a wildcard port, the port of the address should be nonzero if
260 // the IP is nonzero.
261 static bool AddressMatch(const SocketAddress& address,
262 const SocketAddress& pattern) {
263 return address.ipaddr() == pattern.ipaddr() &&
264 ((pattern.port() == 0 &&
265 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
266 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000267 }
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700268
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700269 // Returns the number of ports that have matching type, protocol and
270 // address.
271 static int CountPorts(const std::vector<PortInterface*>& ports,
272 const std::string& type,
273 ProtocolType protocol,
274 const SocketAddress& client_addr) {
275 return std::count_if(
276 ports.begin(), ports.end(),
277 [type, protocol, client_addr](PortInterface* port) {
278 return port->Type() == type && port->GetProtocol() == protocol &&
279 port->Network()->GetBestIP() == client_addr.ipaddr();
280 });
281 }
282
deadbeefe97389c2016-12-23 01:43:45 -0800283 static int CountCandidates(const std::vector<Candidate>& candidates,
284 const std::string& type,
285 const std::string& proto,
286 const SocketAddress& addr) {
287 return std::count_if(candidates.begin(), candidates.end(),
288 [type, proto, addr](const Candidate& c) {
289 return c.type() == type && c.protocol() == proto &&
290 AddressMatch(c.address(), addr);
291 });
292 }
293
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700294 // Find a candidate and return it.
295 static bool FindCandidate(const std::vector<Candidate>& candidates,
296 const std::string& type,
297 const std::string& proto,
298 const SocketAddress& addr,
299 Candidate* found) {
300 auto it = std::find_if(candidates.begin(), candidates.end(),
301 [type, proto, addr](const Candidate& c) {
302 return c.type() == type && c.protocol() == proto &&
303 AddressMatch(c.address(), addr);
304 });
305 if (it != candidates.end() && found) {
306 *found = *it;
307 }
308 return it != candidates.end();
309 }
310
311 // Convenience method to call FindCandidate with no return.
312 static bool HasCandidate(const std::vector<Candidate>& candidates,
313 const std::string& type,
314 const std::string& proto,
315 const SocketAddress& addr) {
316 return FindCandidate(candidates, type, proto, addr, nullptr);
317 }
318
319 // Version of HasCandidate that also takes a related address.
320 static bool HasCandidateWithRelatedAddr(
321 const std::vector<Candidate>& candidates,
322 const std::string& type,
323 const std::string& proto,
324 const SocketAddress& addr,
325 const SocketAddress& related_addr) {
326 auto it =
327 std::find_if(candidates.begin(), candidates.end(),
328 [type, proto, addr, related_addr](const Candidate& c) {
329 return c.type() == type && c.protocol() == proto &&
330 AddressMatch(c.address(), addr) &&
331 AddressMatch(c.related_address(), related_addr);
332 });
333 return it != candidates.end();
334 }
335
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700337 int min_port,
338 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000339 return (addr.port() >= min_port && addr.port() <= max_port);
340 }
341
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700342 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343 // We should only get this callback once, except in the mux test where
344 // we have multiple port allocation sessions.
345 if (session == session_.get()) {
346 ASSERT_FALSE(candidate_allocation_done_);
347 candidate_allocation_done_ = true;
348 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700349 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350 }
351
352 // Check if all ports allocated have send-buffer size |expected|. If
353 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
354 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700355 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000356 for (it = ports_.begin(); it < ports_.end(); ++it) {
357 int send_buffer_size;
358 if (expected == -1) {
359 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700360 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000361 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700362 EXPECT_EQ(0,
363 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 ASSERT_EQ(expected, send_buffer_size);
365 }
366 }
367 }
368
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700369 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
370
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 protected:
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700372 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700374 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
376 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700377 // Make sure the new port is added to ReadyPorts.
378 auto ready_ports = ses->ReadyPorts();
379 EXPECT_NE(ready_ports.end(),
380 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 }
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700382 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-28 18:06:15 -0700383 const std::vector<PortInterface*>& pruned_ports) {
384 LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700385 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700386 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700387 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700388 new_end = std::remove(ports_.begin(), new_end, port);
389 // Make sure the pruned port is not in ReadyPorts.
390 EXPECT_EQ(ready_ports.end(),
391 std::find(ready_ports.begin(), ready_ports.end(), port));
392 }
393 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700394 }
395
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700396 void OnCandidatesReady(PortAllocatorSession* ses,
397 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700398 for (const Candidate& candidate : candidates) {
399 LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
400 // Sanity check that the ICE component is set.
401 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
402 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700404 // Make sure the new candidates are added to Candidates.
405 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700406 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700407 EXPECT_NE(
408 ses_candidates.end(),
409 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
410 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 }
412
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700413 void OnCandidatesRemoved(PortAllocatorSession* session,
414 const std::vector<Candidate>& removed_candidates) {
415 auto new_end = std::remove_if(
416 candidates_.begin(), candidates_.end(),
417 [removed_candidates](Candidate& candidate) {
418 for (const Candidate& removed_candidate : removed_candidates) {
419 if (candidate.MatchesForRemoval(removed_candidate)) {
420 return true;
421 }
422 }
423 return false;
424 });
425 candidates_.erase(new_end, candidates_.end());
426 }
427
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700428 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800429 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700430 RelayServerConfig server_config = allocator_->turn_servers()[i];
431 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000432 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700433 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000434 if (proto_addr.address == relay_port->address &&
435 proto_addr.proto == relay_port->proto)
436 return true;
437 }
438 }
439 return false;
440 }
441
Guo-wei Shieh11477022015-08-15 09:28:41 -0700442 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
443 bool with_nat) {
444 if (with_nat) {
445 nat_server_.reset(new rtc::NATServer(
446 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
447 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
448 } else {
449 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
450 }
451
452 ServerAddresses stun_servers;
453 if (!stun_server.IsNil()) {
454 stun_servers.insert(stun_server);
455 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700456 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700457 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700458 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700459 }
460
pthatcher1749bc32017-02-08 13:18:00 -0800461 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
462 std::unique_ptr<rtc::VirtualSocketServer> vss_;
463 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700464 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 13:18:00 -0800465 std::unique_ptr<rtc::NATServer> nat_server_;
466 rtc::NATSocketFactory nat_factory_;
467 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
468 std::unique_ptr<TestStunServer> stun_server_;
469 TestRelayServer relay_server_;
470 TestTurnServer turn_server_;
471 rtc::FakeNetworkManager network_manager_;
472 std::unique_ptr<BasicPortAllocator> allocator_;
473 std::unique_ptr<PortAllocatorSession> session_;
474 std::vector<PortInterface*> ports_;
475 std::vector<Candidate> candidates_;
476 bool candidate_allocation_done_;
477};
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700478
pthatcher1749bc32017-02-08 13:18:00 -0800479class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
480};
481
482class FakeClockBase {
483 public:
484 rtc::ScopedFakeClock fake_clock;
485};
486
487class BasicPortAllocatorTest : public FakeClockBase,
488 public BasicPortAllocatorTestBase {
489 public:
490 // This function starts the port/address gathering and check the existence of
491 // candidates as specified. When |expect_stun_candidate| is true,
492 // |stun_candidate_addr| carries the expected reflective address, which is
493 // also the related address for TURN candidate if it is expected. Otherwise,
494 // it should be ignore.
495 void CheckDisableAdapterEnumeration(
496 uint32_t total_ports,
497 const rtc::IPAddress& host_candidate_addr,
498 const rtc::IPAddress& stun_candidate_addr,
499 const rtc::IPAddress& relay_candidate_udp_transport_addr,
500 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
501 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
502 rtc::IPAddress());
503 if (!session_) {
504 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
505 }
506 session_->set_flags(session_->flags() |
507 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
508 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
509 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700510 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800511 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
512 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700513
pthatcher1749bc32017-02-08 13:18:00 -0800514 uint32_t total_candidates = 0;
515 if (!host_candidate_addr.IsNil()) {
516 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp",
517 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
518 ++total_candidates;
519 }
520 if (!stun_candidate_addr.IsNil()) {
521 rtc::SocketAddress related_address(host_candidate_addr, 0);
522 if (host_candidate_addr.IsNil()) {
523 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
524 }
525 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "stun", "udp",
526 rtc::SocketAddress(stun_candidate_addr, 0), related_address);
527 ++total_candidates;
528 }
529 if (!relay_candidate_udp_transport_addr.IsNil()) {
530 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
531 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
532 rtc::SocketAddress(stun_candidate_addr, 0));
533 ++total_candidates;
534 }
535 if (!relay_candidate_tcp_transport_addr.IsNil()) {
536 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
537 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
538 rtc::SocketAddress(stun_candidate_addr, 0));
539 ++total_candidates;
540 }
541
542 EXPECT_EQ(total_candidates, candidates_.size());
543 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700544 }
545
546 void TestIPv6TurnPortPrunesIPv4TurnPort() {
547 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
548 // Add two IP addresses on the same interface.
549 AddInterface(kClientAddr, "net1");
550 AddInterface(kClientIPv6Addr, "net1");
551 allocator_.reset(new BasicPortAllocator(&network_manager_));
552 allocator_->SetConfiguration(allocator_->stun_servers(),
553 allocator_->turn_servers(), 0, true);
554 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
555 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
556
557 allocator_->set_step_delay(kMinimumStepDelay);
558 allocator_->set_flags(
559 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
560 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
561
562 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
563 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800564 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
565 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700566 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
567 EXPECT_EQ(3U, session_->ReadyPorts().size());
568 EXPECT_EQ(3U, ports_.size());
569 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
570 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
571 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
572 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
573
574 // Now that we remove candidates when a TURN port is pruned, there will be
575 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
576 EXPECT_EQ(3U, candidates_.size());
577 const std::vector<Candidate>& ready_candidates =
578 session_->ReadyCandidates();
579 EXPECT_EQ(3U, ready_candidates.size());
580 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
581 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
582 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
583 }
584
pthatcher1749bc32017-02-08 13:18:00 -0800585 void TestUdpTurnPortPrunesTcpTurnPort() {
586 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
587 AddInterface(kClientAddr);
588 allocator_.reset(new BasicPortAllocator(&network_manager_));
589 allocator_->SetConfiguration(allocator_->stun_servers(),
590 allocator_->turn_servers(), 0, true);
591 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
592 allocator_->set_step_delay(kMinimumStepDelay);
593 allocator_->set_flags(allocator().flags() |
594 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
595 PORTALLOCATOR_DISABLE_TCP);
596
597 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
598 session_->StartGettingPorts();
599 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
600 kDefaultAllocationTimeout, fake_clock);
601 // Only 2 ports (one STUN and one TURN) are actually being used.
602 EXPECT_EQ(2U, session_->ReadyPorts().size());
603 // We have verified that each port, when it is added to |ports_|, it is
604 // found in |ready_ports|, and when it is pruned, it is not found in
605 // |ready_ports|, so we only need to verify the content in one of them.
606 EXPECT_EQ(2U, ports_.size());
607 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
608 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
609 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
610
611 // Now that we remove candidates when a TURN port is pruned, |candidates_|
612 // should only contains two candidates regardless whether the TCP TURN port
613 // is created before or after the UDP turn port.
614 EXPECT_EQ(2U, candidates_.size());
615 // There will only be 2 candidates in |ready_candidates| because it only
616 // includes the candidates in the ready ports.
617 const std::vector<Candidate>& ready_candidates =
618 session_->ReadyCandidates();
619 EXPECT_EQ(2U, ready_candidates.size());
620 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
621 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
622 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
623 }
624
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700625 void TestEachInterfaceHasItsOwnTurnPorts() {
626 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
627 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
628 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
629 // Add two interfaces both having IPv4 and IPv6 addresses.
630 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
631 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
632 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
633 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
634 allocator_.reset(new BasicPortAllocator(&network_manager_));
635 allocator_->SetConfiguration(allocator_->stun_servers(),
636 allocator_->turn_servers(), 0, true);
637 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
638 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
639 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
640
641 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 14:40:51 -0800642 allocator_->set_flags(
643 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
644 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700645 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
646 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800647 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
648 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700649 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
650 // use.
651 EXPECT_EQ(10U, session_->ReadyPorts().size());
652 EXPECT_EQ(10U, ports_.size());
653 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
654 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
655 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
656 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
657 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
658 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
659 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
660 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
661 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
662 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
663
664 // Now that we remove candidates when TURN ports are pruned, there will be
665 // exactly 10 candidates in |candidates_|.
666 EXPECT_EQ(10U, candidates_.size());
667 const std::vector<Candidate>& ready_candidates =
668 session_->ReadyCandidates();
669 EXPECT_EQ(10U, ready_candidates.size());
670 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr);
671 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp", kClientAddr2);
672 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
673 kClientIPv6Addr);
674 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "udp",
675 kClientIPv6Addr2);
676 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr);
677 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp", kClientAddr2);
678 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
679 kClientIPv6Addr);
680 EXPECT_PRED4(HasCandidate, ready_candidates, "local", "tcp",
681 kClientIPv6Addr2);
682 EXPECT_PRED4(HasCandidate, ready_candidates, "relay", "udp",
683 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
684 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000685};
686
687// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700688TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000689 EXPECT_EQ(&network_manager_, allocator().network_manager());
690 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800691 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700692 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000693 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800694 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
695 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700696 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
697 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
698 EXPECT_TRUE(
699 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
700 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700701 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000702}
703
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800704// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700705TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800706 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
707 rtc::ADAPTER_TYPE_ETHERNET);
708 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
709 rtc::ADAPTER_TYPE_WIFI);
710 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
711 rtc::ADAPTER_TYPE_CELLULAR);
712 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
713 rtc::ADAPTER_TYPE_VPN);
714 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
715 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700716 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
717 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
718 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800719 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800720 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
721 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800722 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700723 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 EXPECT_LT(candidate.address().ip(), 0x12345604U);
725 }
726}
727
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700728TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800729 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
730 rtc::ADAPTER_TYPE_ETHERNET);
731 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
732 rtc::ADAPTER_TYPE_WIFI);
733 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
734 rtc::ADAPTER_TYPE_CELLULAR);
735 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
736 rtc::ADAPTER_TYPE_LOOPBACK |
737 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700738 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
739 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
740 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800741 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800742 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
743 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800744 EXPECT_EQ(1U, candidates_.size());
745 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
746}
747
honghaiz60347052016-05-31 18:29:12 -0700748// Test that high cost networks are filtered if the flag
749// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set.
750TEST_F(BasicPortAllocatorTest, TestGatherLowCostNetworkOnly) {
751 SocketAddress addr_wifi(IPAddress(0x12345600U), 0);
752 SocketAddress addr_cellular(IPAddress(0x12345601U), 0);
753 SocketAddress addr_unknown1(IPAddress(0x12345602U), 0);
754 SocketAddress addr_unknown2(IPAddress(0x12345603U), 0);
755 // If both Wi-Fi and cellular interfaces are present, only gather on the Wi-Fi
756 // interface.
757 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
758 AddInterface(addr_cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
759 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
760 cricket::PORTALLOCATOR_DISABLE_RELAY |
761 cricket::PORTALLOCATOR_DISABLE_TCP |
762 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
763 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
764 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800765 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
766 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700767 EXPECT_EQ(1U, candidates_.size());
768 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
769
770 // If both cellular and unknown interfaces are present, only gather on the
771 // unknown interfaces.
772 candidates_.clear();
773 candidate_allocation_done_ = false;
774 RemoveInterface(addr_wifi);
775 AddInterface(addr_unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
776 AddInterface(addr_unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
777 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800778 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
779 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700780 EXPECT_EQ(2U, candidates_.size());
781 EXPECT_TRUE((addr_unknown1.EqualIPs(candidates_[0].address()) &&
782 addr_unknown2.EqualIPs(candidates_[1].address())) ||
783 (addr_unknown1.EqualIPs(candidates_[1].address()) &&
784 addr_unknown2.EqualIPs(candidates_[0].address())));
785
786 // If Wi-Fi, cellular, unknown interfaces are all present, only gather on the
787 // Wi-Fi interface.
788 candidates_.clear();
789 candidate_allocation_done_ = false;
790 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
791 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800792 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
793 kDefaultAllocationTimeout, fake_clock);
honghaiz60347052016-05-31 18:29:12 -0700794 EXPECT_EQ(1U, candidates_.size());
795 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address()));
796}
797
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700798// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700799TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800800 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
801 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700802 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
803 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
804 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700805 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800806 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
807 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700808 EXPECT_EQ(1U, candidates_.size());
809}
810
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000811// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700812TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000813 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700814 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800816 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
817 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000818 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700819 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
820 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
821 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
822 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
823 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
824 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
825 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
826 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000827 EXPECT_TRUE(candidate_allocation_done_);
828}
829
honghaiz8c404fa2015-09-28 07:59:43 -0700830// Test that when the same network interface is brought down and up, the
831// port allocator session will restart a new allocation sequence if
832// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700833TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700834 std::string if_name("test_net0");
835 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700836 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700837 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800838 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
839 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700840 EXPECT_EQ(4U, ports_.size());
841 EXPECT_TRUE(candidate_allocation_done_);
842 candidate_allocation_done_ = false;
843 candidates_.clear();
844 ports_.clear();
845
846 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800847 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
848 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700849 EXPECT_EQ(0U, ports_.size());
850 EXPECT_FALSE(candidate_allocation_done_);
851
852 // When the same interfaces are added again, new candidates/ports should be
853 // generated.
854 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800855 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
856 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700857 EXPECT_EQ(4U, ports_.size());
858 EXPECT_TRUE(candidate_allocation_done_);
859}
860
861// Test that when the same network interface is brought down and up, the
862// port allocator session will not restart a new allocation sequence if
863// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700864TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700865 std::string if_name("test_net0");
866 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700867 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700868 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800869 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
870 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700871 EXPECT_EQ(4U, ports_.size());
872 EXPECT_TRUE(candidate_allocation_done_);
873 session_->StopGettingPorts();
874 candidates_.clear();
875 ports_.clear();
876
877 RemoveInterface(kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800878 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
879 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700880 EXPECT_EQ(0U, ports_.size());
881
882 // When the same interfaces are added again, new candidates/ports should not
883 // be generated because the session has stopped.
884 AddInterface(kClientAddr, if_name);
pthatcher1749bc32017-02-08 13:18:00 -0800885 ASSERT_EQ_SIMULATED_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout,
886 fake_clock);
honghaiz8c404fa2015-09-28 07:59:43 -0700887 EXPECT_EQ(0U, ports_.size());
888 EXPECT_TRUE(candidate_allocation_done_);
889}
890
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000891// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700892TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000893 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700894 allocator_->set_step_delay(kDefaultStepDelay);
895 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000896 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800897 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800899 ASSERT_EQ_SIMULATED_WAIT(4U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000900 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700901 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
902 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher1749bc32017-02-08 13:18:00 -0800903 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 1500, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700904 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
905 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000906 EXPECT_EQ(4U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -0800907 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), 2000, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -0700908 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
909 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000910 EXPECT_EQ(4U, ports_.size());
911 EXPECT_TRUE(candidate_allocation_done_);
912 // If we Stop gathering now, we shouldn't get a second "done" callback.
913 session_->StopGettingPorts();
914}
915
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700916TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000917 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700918 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000919 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800920 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
921 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000922 EXPECT_TRUE(candidate_allocation_done_);
923 // If we Stop gathering now, we shouldn't get a second "done" callback.
924 session_->StopGettingPorts();
925
926 // All ports should have unset send-buffer sizes.
927 CheckSendBufferSizesOfAllPorts(-1);
928}
929
930// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700931TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000932 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700933 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000934 session_->StartGettingPorts();
935 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
936 EXPECT_EQ(2U, ports_.size());
937 session_->StopGettingPorts();
938 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
939}
940
941// Test that we restrict client ports appropriately when a port range is set.
942// We check the candidates for udp/stun/tcp ports, and the from address
943// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700944TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000945 AddInterface(kClientAddr);
946 // Check that an invalid port range fails.
947 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
948 // Check that a null port range succeeds.
949 EXPECT_TRUE(SetPortRange(0, 0));
950 // Check that a valid port range succeeds.
951 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700952 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000953 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800954 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
955 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000956 EXPECT_EQ(4U, ports_.size());
honghaiza73df552016-10-27 11:44:17 -0700957
958 int num_nonrelay_candidates = 0;
959 for (const Candidate& candidate : candidates_) {
960 // Check the port number for the UDP/STUN/TCP port objects.
961 if (candidate.type() != RELAY_PORT_TYPE) {
962 EXPECT_PRED3(CheckPort, candidate.address(), kMinPort, kMaxPort);
963 ++num_nonrelay_candidates;
964 }
965 }
966 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000967 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700968 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
969 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000970 EXPECT_TRUE(candidate_allocation_done_);
971}
972
deadbeefe97389c2016-12-23 01:43:45 -0800973// Test that if we have no network adapters, we bind to the ANY address and
974// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700975TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 01:43:45 -0800976 // Default config uses GTURN and no NAT, so replace that with the
977 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
978 ResetWithStunServerAndNat(kStunAddr);
979 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
980 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
981 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
982 // Disable IPv6, because our test infrastructure doesn't support having IPv4
983 // behind a NAT but IPv6 not, or having an IPv6 NAT.
984 // TODO(deadbeef): Fix this.
985 network_manager_.set_ipv6_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700986 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000987 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -0800988 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
989 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 01:43:45 -0800990 EXPECT_EQ(4U, ports_.size());
991 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
992 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
993 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
994 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
995 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
996 // The "any" address port should be in the signaled ready ports, but the host
997 // candidate for it is useless and shouldn't be signaled. So we only have
998 // STUN/TURN candidates.
999 EXPECT_EQ(3U, candidates_.size());
1000 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1001 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1002 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1003 // server.
1004 EXPECT_EQ(2,
1005 CountCandidates(candidates_, "relay", "udp",
1006 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001007}
1008
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001009// Test that when enumeration is disabled, we should not have any ports when
1010// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001011TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001012 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001013 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001014 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -07001015 // Expect to see no ports and no candidates.
1016 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1017 rtc::IPAddress(), rtc::IPAddress());
1018}
1019
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001020// Test that even with multiple interfaces, the result should still be a single
1021// default private, one STUN and one TURN candidate since we bind to any address
1022// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001023TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001024 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001025 AddInterface(kPrivateAddr);
1026 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001027 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001028 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -08001029
1030 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1031 // address set and we have no IPv6 STUN server, there should be no IPv6
1032 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001033 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1034 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -08001035
1036 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1037 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1038 // TURN/UDP candidates.
1039 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001040 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1041 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001042}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001043
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001044// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1045// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001046TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001047 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001048 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001049 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001050 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001051 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1052 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1053 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1054 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001055 kTurnUdpExtAddr.ipaddr());
1056}
1057
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001058// Test that when adapter enumeration is disabled, for endpoints without
1059// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001060TEST_F(BasicPortAllocatorTest,
1061 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001062 ResetWithNoServersOrNat();
1063 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1064 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1065 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001066}
1067
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001068// Test that when adapter enumeration is disabled, with
1069// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1070// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001071TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001072 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1073 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001074 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1075 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001076 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1077 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001078 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001079 rtc::IPAddress(), rtc::IPAddress());
1080}
1081
1082// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001083// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1084// a NAT, there is no local candidate. However, this specified default route
1085// (kClientAddr) which was discovered when sending STUN requests, will become
1086// the srflx addresses.
1087TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001088 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001089 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001090 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001091 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001092 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1093 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -07001094 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1095 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001096 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1097 rtc::IPAddress(), rtc::IPAddress());
1098}
1099
1100// Test that when adapter enumeration is disabled, with
1101// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1102// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001103TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001104 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1105 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001106 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1107 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001108 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1109 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1110 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +00001111}
1112
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001113// Test that we disable relay over UDP, and only TCP is used when connecting to
1114// the relay server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001115TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001116 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001117 AddInterface(kClientAddr);
1118 ResetWithStunServerAndNat(kStunAddr);
1119 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001120 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1121 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1122 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1123 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001124
1125 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001126 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1127 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001128
1129 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1130 // TURN/TCP candidates.
1131 EXPECT_EQ(2U, ports_.size());
1132 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001133 Candidate turn_candidate;
1134 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp", kTurnUdpExtAddr,
1135 &turn_candidate);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001136 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001137 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1138 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -07001139}
1140
Erik Språngefdce692015-06-05 09:41:26 +02001141// Disable for asan, see
1142// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
1143#if !defined(ADDRESS_SANITIZER)
1144
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145// Test that we can get OnCandidatesAllocationDone callback when all the ports
1146// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001147TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001148 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001149 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1150 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1151 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001153 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001155}
1156
1157// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001158TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 AddInterface(kClientAddr);
1160 fss_->set_udp_sockets_enabled(false);
1161 EXPECT_TRUE(CreateSession(1));
1162 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001163 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1164 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001165 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001166 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1167 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1168 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1169 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1170 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1171 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 EXPECT_TRUE(candidate_allocation_done_);
1173}
1174
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001175#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +02001176
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001177// Test that we don't crash or malfunction if we can't create UDP sockets or
1178// listen on TCP sockets. We still give out a local TCP address, since
1179// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001180TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001181 AddInterface(kClientAddr);
1182 fss_->set_udp_sockets_enabled(false);
1183 fss_->set_tcp_listen_enabled(false);
1184 EXPECT_TRUE(CreateSession(1));
1185 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001186 ASSERT_EQ_SIMULATED_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout,
1187 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001188 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001189 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1190 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
1191 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1192 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
1193 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001194 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 EXPECT_TRUE(candidate_allocation_done_);
1196}
1197
1198// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001199// TODO(deadbeef): Find a way to exit early here.
1200TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001201 AddInterface(kClientAddr);
1202 fss_->set_tcp_sockets_enabled(false);
1203 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001204 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001205 session_->StartGettingPorts();
1206 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001207 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001208 // In case of Relay, ports creation will succeed but sockets will fail.
1209 // There is no error reporting from RelayEntry to handle this failure.
1210}
1211
1212// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001213TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001214 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1215 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001216 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001217 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001218 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1219 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001220 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001221 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1222 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001223 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
pthatcher1749bc32017-02-08 13:18:00 -08001224 // will be tried after about 3 seconds.
1225 EXPECT_EQ_SIMULATED_WAIT(6U, candidates_.size(), 3500, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001226 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001227 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpIntAddr);
1228 EXPECT_PRED4(HasCandidate, candidates_, "relay", "tcp", kRelayTcpIntAddr);
1229 EXPECT_PRED4(HasCandidate, candidates_, "relay", "ssltcp",
1230 kRelaySslTcpIntAddr);
1231 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp", kRelayUdpExtAddr);
pthatcher94a2f212017-02-08 14:42:22 -08001232 // We wait at least for a full STUN timeout, which
1233 // cricket::STUN_TOTAL_TIMEOUT seconds. But since 3-3.5 seconds
1234 // already passed (see above), we wait 3 seconds less than that.
1235 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1236 cricket::STUN_TOTAL_TIMEOUT - 3000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001237}
1238
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001239TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001240 AddInterface(kClientAddr);
1241 AddInterface(kClientAddr2);
1242 // Allocating only host UDP ports. This is done purely for testing
1243 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001244 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1245 PORTALLOCATOR_DISABLE_RELAY);
1246 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001247 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001248 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1249 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001250 ASSERT_EQ(2U, candidates_.size());
1251 EXPECT_EQ(2U, ports_.size());
1252 // Candidates priorities should be different.
1253 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1254}
1255
1256// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001257TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001258 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001259 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001260 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001261 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1262 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001263 EXPECT_EQ(4U, ports_.size());
1264 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001265 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001266}
1267
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001268// Test that the allocator session uses the candidate filter it's created with,
1269// rather than the filter of its parent allocator.
1270// The filter of the allocator should only affect the next gathering phase,
1271// according to JSEP, which means the *next* allocator session returned.
1272TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1273 AddInterface(kClientAddr);
1274 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1275 // Set candidate filter *after* creating the session. Should have no effect.
1276 allocator().set_candidate_filter(CF_RELAY);
1277 session_->StartGettingPorts();
1278 // 7 candidates and 4 ports is what we would normally get (see the
1279 // TestGetAllPorts* tests).
pthatcher1749bc32017-02-08 13:18:00 -08001280 EXPECT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1281 fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001282 EXPECT_EQ(4U, ports_.size());
1283 EXPECT_TRUE(candidate_allocation_done_);
1284}
1285
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001286// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1287// This test also verifies that when the allocator is only allowed to use
1288// relay (i.e. IceTransportsType is relay), the raddr is an empty
1289// address with the correct family. This is to prevent any local
1290// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001291TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001292 AddInterface(kClientAddr);
1293 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001294 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001295 allocator().set_candidate_filter(CF_RELAY);
1296 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001297 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001298 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1299 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001300 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1301 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001302
1303 EXPECT_EQ(1U, candidates_.size());
1304 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001305 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1306 EXPECT_EQ(
1307 candidates_[0].related_address(),
1308 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001309}
1310
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001311TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001312 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001313 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1314 allocator().set_candidate_filter(CF_HOST);
1315 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001316 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001317 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1318 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001319 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1320 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001321 for (const Candidate& candidate : candidates_) {
1322 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001323 }
1324}
1325
1326// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001327TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001328 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001329 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001330
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001331 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1332 allocator().set_candidate_filter(CF_REFLEXIVE);
1333 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001334 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001335 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1336 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001337 // Host is behind NAT, no private address will be exposed. Hence only UDP
1338 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001339 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1340 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001341 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1342 EXPECT_EQ(
1343 candidates_[0].related_address(),
1344 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001345}
1346
1347// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001348TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001349 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001350 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1351 allocator().set_candidate_filter(CF_REFLEXIVE);
1352 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001353 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001354 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1355 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001356 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001357 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001358 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001359 for (const Candidate& candidate : candidates_) {
1360 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001361 }
1362}
1363
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001364// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001365TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001366 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001367 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001368 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001369 ASSERT_EQ_SIMULATED_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout,
1370 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001371 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1372 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", kClientAddr);
1373 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001374 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001375 for (const Candidate& candidate : candidates_) {
1376 EXPECT_EQ(kIceUfrag0, candidate.username());
1377 EXPECT_EQ(kIcePwd0, candidate.password());
1378 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001379 EXPECT_TRUE(candidate_allocation_done_);
1380}
1381
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001382// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1383// is allocated for udp and stun. Also verify there is only one candidate
1384// (local) if stun candidate is same as local candidate, which will be the case
1385// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001386TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001387 AddInterface(kClientAddr);
1388 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001389 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1390 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001391 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001392 ASSERT_EQ_SIMULATED_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout,
1393 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001394 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001395 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
pthatcher1749bc32017-02-08 13:18:00 -08001396 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1397 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398}
1399
1400// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1401// is allocated for udp and stun. In this test we should expect both stun and
1402// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001403TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001404 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001405 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001406
1407 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001408 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1409 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001411 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1412 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001414 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1415 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1416 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001417 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1418 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419 EXPECT_EQ(3U, candidates_.size());
1420}
1421
deadbeefc5d0d952015-07-16 10:22:21 -07001422// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001423TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001424 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001425 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001426 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001427
1428 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1429
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001430 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001431 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001432 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1433 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001434
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001435 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001436 session_->StartGettingPorts();
1437
pthatcher1749bc32017-02-08 13:18:00 -08001438 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1439 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001440 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001441 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1442 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1443 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1444 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1445 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001446 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1447 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001448 EXPECT_EQ(3U, candidates_.size());
1449}
1450
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001451// Test that if prune_turn_ports is set, TCP TURN port will not be used
1452// if UDP TurnPort is used, given that TCP TURN port becomes ready first.
1453TEST_F(BasicPortAllocatorTest,
1454 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1455 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1456 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1457 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001458
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001459 TestUdpTurnPortPrunesTcpTurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001460}
1461
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001462// Test that if prune_turn_ports is set, TCP TURN port will not be used
1463// if UDP TurnPort is used, given that UDP TURN port becomes ready first.
1464TEST_F(BasicPortAllocatorTest,
1465 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1466 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1467 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1468 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001469
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001470 TestUdpTurnPortPrunesTcpTurnPort();
1471}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001472
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001473// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1474// if IPv6 TurnPort is used, given that IPv4 TURN port becomes ready first.
1475TEST_F(BasicPortAllocatorTest,
1476 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1477 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1478 // first.
1479 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1480 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001481
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001482 TestIPv6TurnPortPrunesIPv4TurnPort();
1483}
1484
1485// Tests that if prune_turn_ports is set, IPv4 TurnPort will not be used
1486// if IPv6 TurnPort is used, given that IPv6 TURN port becomes ready first.
1487TEST_F(BasicPortAllocatorTest,
1488 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1489 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1490 // first.
1491 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1492 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1493
1494 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001495}
1496
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001497// Tests that if prune_turn_ports is set, each network interface
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001498// will has its own set of TurnPorts based on their priorities, in the default
1499// case where no transit delay is set.
1500TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1501 TestEachInterfaceHasItsOwnTurnPorts();
1502}
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001503
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001504// Tests that if prune_turn_ports is set, each network interface
1505// will has its own set of TurnPorts based on their priorities, given that
1506// IPv4/TCP TURN port becomes ready first.
1507TEST_F(BasicPortAllocatorTest,
1508 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1509 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1510 // becomes ready last.
1511 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1512 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1513 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1514 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001515
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001516 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001517}
1518
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001519// Testing DNS resolve for the TURN server, this will test AllocationSequence
1520// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 13:18:00 -08001521// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1522// appears that it doesn't currently because of the DNS look up not
1523// using the fake clock.
1524TEST_F(BasicPortAllocatorTestWithRealClock,
1525 TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001526 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001527 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001528 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001529 allocator_.reset(new BasicPortAllocator(&network_manager_));
1530 RelayServerConfig turn_server(RELAY_TURN);
1531 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001532 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001533 turn_server.ports.push_back(
hnsl277b2502016-12-13 05:17:23 -08001534 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 12:55:10 -08001535 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001536
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001537 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001538 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001539 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1540 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001541
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001542 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001543 session_->StartGettingPorts();
1544
1545 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1546}
1547
1548// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1549// is allocated for udp/stun/turn. In this test we should expect all local,
1550// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001551TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001552 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001553 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001554
1555 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1556
1557 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001558 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1559 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001560
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001561 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001562 session_->StartGettingPorts();
1563
pthatcher1749bc32017-02-08 13:18:00 -08001564 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1565 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001566 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001567 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1568 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp",
1569 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1570 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1571 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001572 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1573 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001574 EXPECT_EQ(3U, candidates_.size());
1575 // Local port will be created first and then TURN port.
1576 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1577 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1578}
1579
1580// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1581// server is also used as the STUN server, we should get 'local', 'stun', and
1582// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001583TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001584 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001585 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001586 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001587 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1588
1589 // Must set the step delay to 0 to make sure the relay allocation phase is
1590 // started before the STUN candidates are obtained, so that the STUN binding
1591 // response is processed when both StunPort and TurnPort exist to reproduce
1592 // webrtc issue 3537.
1593 allocator_->set_step_delay(0);
1594 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001595 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1596 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001597
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001598 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599 session_->StartGettingPorts();
1600
pthatcher1749bc32017-02-08 13:18:00 -08001601 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1602 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001603 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1604 Candidate stun_candidate;
1605 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1606 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1607 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1608 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1609 stun_candidate.address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001610
pthatcher1749bc32017-02-08 13:18:00 -08001611 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1612 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001613 EXPECT_EQ(3U, candidates_.size());
1614 // Local port will be created first and then TURN port.
1615 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1616 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1617}
1618
deadbeefc5d0d952015-07-16 10:22:21 -07001619// Test that when only a TCP TURN server is available, we do NOT use it as
1620// a UDP STUN server, as this could leak our IP address. Thus we should only
1621// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001622TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001623 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001624 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001625 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001626 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1627
1628 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001629 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1630 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001631
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001632 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001633 session_->StartGettingPorts();
1634
pthatcher1749bc32017-02-08 13:18:00 -08001635 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1636 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001637 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001638 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1639 EXPECT_PRED4(HasCandidate, candidates_, "relay", "udp",
1640 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
pthatcher1749bc32017-02-08 13:18:00 -08001641 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1642 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001643 EXPECT_EQ(2U, candidates_.size());
1644 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1645 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1646}
1647
1648// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1649// TURN server is used as the STUN server and we get 'local', 'stun', and
1650// 'relay' candidates.
1651// TODO(deadbeef): Remove this test when support for non-shared socket mode
1652// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001653TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001654 AddInterface(kClientAddr);
1655 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001656 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001657 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1658
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001659 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001660
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001661 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001662 session_->StartGettingPorts();
1663
pthatcher1749bc32017-02-08 13:18:00 -08001664 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1665 fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001666 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001667 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1668 Candidate stun_candidate;
1669 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1670 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1671 Candidate turn_candidate;
1672 EXPECT_PRED5(FindCandidate, candidates_, "relay", "udp",
1673 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1674 &turn_candidate);
deadbeefc5d0d952015-07-16 10:22:21 -07001675 // Not using shared socket, so the STUN request's server reflexive address
1676 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001677 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001678
pthatcher1749bc32017-02-08 13:18:00 -08001679 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1680 kDefaultAllocationTimeout, fake_clock);
deadbeefc5d0d952015-07-16 10:22:21 -07001681 EXPECT_EQ(3U, candidates_.size());
1682 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1683 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1684 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1685}
1686
1687// Test that even when both a STUN and TURN server are configured, the TURN
1688// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001689TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001690 AddInterface(kClientAddr);
1691 // Configure with STUN server but destroy it, so we can ensure that it's
1692 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001693 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001694 stun_server_.reset();
1695 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1696
1697 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001698 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1699 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001700
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001701 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001702 session_->StartGettingPorts();
1703
pthatcher1749bc32017-02-08 13:18:00 -08001704 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1705 fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001706 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1707 Candidate stun_candidate;
1708 EXPECT_PRED5(FindCandidate, candidates_, "stun", "udp",
1709 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), &stun_candidate);
1710 EXPECT_PRED5(HasCandidateWithRelatedAddr, candidates_, "relay", "udp",
1711 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1712 stun_candidate.address());
deadbeefc5d0d952015-07-16 10:22:21 -07001713
1714 // Don't bother waiting for STUN timeout, since we already verified
1715 // that we got a STUN candidate from the TURN server.
1716}
1717
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001718// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1719// and fail to generate STUN candidate, local UDP candidate is generated
1720// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001721TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001722 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1723 PORTALLOCATOR_DISABLE_TCP |
1724 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001725 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1726 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001727 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001728 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001729 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1730 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001731 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001732 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001733 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 13:18:00 -08001734 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1735 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001736 EXPECT_EQ(1U, candidates_.size());
1737}
1738
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001739// Test that when the NetworkManager doesn't have permission to enumerate
1740// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1741// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001742TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001743 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1744 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001745 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001746 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001747 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1748 PORTALLOCATOR_DISABLE_TCP |
1749 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1750 EXPECT_EQ(0U,
1751 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1752 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1753 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001754 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001755 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
1756 fake_clock);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001757 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001758 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kPrivateAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001759 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001760}
1761
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001762// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001763TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001764 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1765 PORTALLOCATOR_ENABLE_IPV6 |
1766 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001767 AddInterface(kClientIPv6Addr);
1768 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001769 allocator_->set_step_delay(kMinimumStepDelay);
1770 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001771 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001772 ASSERT_EQ_SIMULATED_WAIT(4U, ports_.size(), kDefaultAllocationTimeout,
1773 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001774 EXPECT_EQ(4U, candidates_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001775 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1776 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter8c9be5e2016-05-26 16:07:31 -07001777 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientIPv6Addr);
1778 EXPECT_PRED4(HasCandidate, candidates_, "local", "udp", kClientAddr);
1779 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientIPv6Addr);
1780 EXPECT_PRED4(HasCandidate, candidates_, "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001781 EXPECT_EQ(4U, candidates_.size());
1782}
honghaiz98db68f2015-09-29 07:58:17 -07001783
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001784TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001785 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001786 allocator_->set_step_delay(kDefaultStepDelay);
1787 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001788 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001789 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001790 EXPECT_EQ(2U, ports_.size());
1791 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001792 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001793
1794 // After stopping getting ports, adding a new interface will not start
1795 // getting ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001796 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001797 candidates_.clear();
1798 ports_.clear();
1799 candidate_allocation_done_ = false;
1800 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001801 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001802 EXPECT_EQ(0U, candidates_.size());
1803 EXPECT_EQ(0U, ports_.size());
1804}
1805
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001806TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001807 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001808 allocator_->set_step_delay(kDefaultStepDelay);
1809 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001810 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001811 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001812 EXPECT_EQ(2U, ports_.size());
1813 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 13:18:00 -08001814 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001815
1816 // After clearing getting ports, adding a new interface will start getting
1817 // ports again.
deadbeefb60a8192016-08-24 15:15:00 -07001818 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 07:58:17 -07001819 candidates_.clear();
1820 ports_.clear();
1821 candidate_allocation_done_ = false;
1822 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 13:18:00 -08001823 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001824 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 13:18:00 -08001825 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1826 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 07:58:17 -07001827}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001828
1829// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1830// a pooled session is taken out of the pool.
1831TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1832 AddInterface(kClientAddr);
1833 int pool_size = 1;
1834 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001835 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001836 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001837 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001838 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1839 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001840 // Expect that when TakePooledSession is called,
1841 // UpdateTransportInformationInternal will be called and the
1842 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1843 // candidates.
1844 session_ =
1845 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1846 ASSERT_NE(nullptr, session_.get());
1847 auto ready_ports = session_->ReadyPorts();
1848 auto candidates = session_->ReadyCandidates();
1849 EXPECT_FALSE(ready_ports.empty());
1850 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001851 for (const PortInterface* port_interface : ready_ports) {
1852 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001853 EXPECT_EQ(kContentName, port->content_name());
1854 EXPECT_EQ(1, port->component());
1855 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1856 EXPECT_EQ(kIcePwd0, port->password());
1857 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001858 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001859 EXPECT_EQ(1, candidate.component());
1860 EXPECT_EQ(kIceUfrag0, candidate.username());
1861 EXPECT_EQ(kIcePwd0, candidate.password());
1862 }
1863}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001864
1865// Test that a new candidate filter takes effect even on already-gathered
1866// candidates.
1867TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1868 AddInterface(kClientAddr);
1869 int pool_size = 1;
1870 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -07001871 allocator_->turn_servers(), pool_size, false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001872 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1873 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 13:18:00 -08001874 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
1875 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001876 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1877 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1878 allocator_->set_candidate_filter(CF_RELAY);
1879 // Assume that when TakePooledSession is called, the candidate filter will be
1880 // applied to the pooled session. This is tested by PortAllocatorTest.
1881 session_ =
1882 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1883 ASSERT_NE(nullptr, session_.get());
1884 auto candidates = session_->ReadyCandidates();
1885 auto ports = session_->ReadyPorts();
1886 // Sanity check that the number of candidates and ports decreased.
1887 EXPECT_GT(initial_candidates_size, candidates.size());
1888 EXPECT_GT(initial_ports_size, ports.size());
1889 for (const PortInterface* port : ports) {
1890 // Expect only relay ports.
1891 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1892 }
1893 for (const Candidate& candidate : candidates) {
1894 // Expect only relay candidates now that the filter is applied.
1895 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1896 // Expect that the raddr is emptied due to the CF_RELAY filter.
1897 EXPECT_EQ(candidate.related_address(),
1898 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1899 }
1900}
1901
1902} // namespace cricket