blob: 5393321f79fe4cb0564dcbb48fcf94f53a6965b0 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
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
Jonas Olssona4d87372019-07-05 17:08:3311#include "p2p/client/basic_port_allocator.h"
12
kwiberg3ec46792016-04-27 14:22:5313#include <memory>
Yves Gerey3e707812018-11-28 15:47:4914#include <ostream> // no-presubmit-check TODO(webrtc:8982)
kwiberg3ec46792016-04-27 14:22:5315
Steve Antonae226f62019-01-29 20:47:3816#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 17:11:0017#include "p2p/base/basic_packet_socket_factory.h"
18#include "p2p/base/p2p_constants.h"
19#include "p2p/base/stun_port.h"
20#include "p2p/base/stun_request.h"
21#include "p2p/base/stun_server.h"
Steve Anton10542f22019-01-11 17:11:0022#include "p2p/base/test_stun_server.h"
23#include "p2p/base/test_turn_server.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 21:34:5625#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 17:11:0026#include "rtc_base/fake_network.h"
27#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0029#include "rtc_base/ip_address.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0031#include "rtc_base/nat_server.h"
32#include "rtc_base/nat_socket_factory.h"
33#include "rtc_base/nat_types.h"
34#include "rtc_base/net_helper.h"
35#include "rtc_base/net_helpers.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3136#include "rtc_base/network.h"
Yves Gerey3e707812018-11-28 15:47:4937#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 17:11:0038#include "rtc_base/network_monitor.h"
Yves Gerey3e707812018-11-28 15:47:4939#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 17:11:0040#include "rtc_base/socket_address.h"
41#include "rtc_base/socket_address_pair.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3142#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 17:11:0043#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 06:51:1044#include "system_wrappers/include/metrics.h"
Honghai Zhang6981fb52019-10-29 19:45:3445#include "test/field_trial.h"
Steve Antonae226f62019-01-29 20:47:3846#include "test/gmock.h"
Yves Gerey3e707812018-11-28 15:47:4947#include "test/gtest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1148
Taylor Brandstetter0c7e9f52015-12-29 22:14:5249using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:1150using rtc::SocketAddress;
Steve Antonae226f62019-01-29 20:47:3851using ::testing::Contains;
52using ::testing::Not;
henrike@webrtc.org269fb4b2014-10-28 22:20:1153
Mirko Bonadei675513b2017-11-09 10:09:2554#define MAYBE_SKIP_IPV4 \
55 if (!rtc::HasIPv4Enabled()) { \
56 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
57 return; \
deadbeef9a6f4d42017-05-16 02:43:3358 }
59
deadbeefe97389c2016-12-23 09:43:4560static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:1161static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-07-01 03:52:0262static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:2063static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:1164static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:0165static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 15:15:1166static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
67 0);
Honghai Zhangb9e7b4a2016-07-01 03:52:0268static const SocketAddress kClientIPv6Addr2(
69 "2401:fa00:4:2000:be30:5bff:fee5:c3",
70 0);
deadbeef3427f532017-07-26 23:09:3371static const SocketAddress kClientIPv6Addr3(
72 "2401:fa00:4:3000:be30:5bff:fee5:c3",
73 0);
deadbeefc5d0d952015-07-16 17:22:2174static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
75static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:1176static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
77static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:1178static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-07-01 03:52:0279static const SocketAddress kTurnUdpIntIPv6Addr(
80 "2402:fb00:4:1000:be30:5bff:fee5:c3",
81 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:1182static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-07-01 03:52:0283static const SocketAddress kTurnTcpIntIPv6Addr(
84 "2402:fb00:4:2000:be30:5bff:fee5:c3",
85 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:1186static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
87
88// Minimum and maximum port for port range tests.
89static const int kMinPort = 10000;
90static const int kMaxPort = 10099;
91
92// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 17:13:3293static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:1194// Based on ICE_PWD_LENGTH
95static const char kIcePwd0[] = "TESTICEPWD00000000000000";
96
97static const char kContentName[] = "test content";
98
Honghai Zhangc67e0f52016-09-19 23:57:3799static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11100static const char kTurnUsername[] = "test";
101static const char kTurnPassword[] = "test";
102
pthatcher94a2f212017-02-08 22:42:22103// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 19:49:30104// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 22:42:22105static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 19:49:30106
Qingsi Wang4ff54432018-03-02 02:25:20107namespace {
108
109void CheckStunKeepaliveIntervalOfAllReadyPorts(
110 const cricket::PortAllocatorSession* allocator_session,
111 int expected) {
112 auto ready_ports = allocator_session->ReadyPorts();
113 for (const auto* port : ready_ports) {
114 if (port->Type() == cricket::STUN_PORT_TYPE ||
115 (port->Type() == cricket::LOCAL_PORT_TYPE &&
116 port->GetProtocol() == cricket::PROTO_UDP)) {
117 EXPECT_EQ(
118 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
119 expected);
120 }
121 }
122}
123
124} // namespace
125
henrike@webrtc.org269fb4b2014-10-28 22:20:11126namespace cricket {
127
128// Helper for dumping candidates
Taylor Brandstetter8c9be5e42016-05-26 23:07:31129std::ostream& operator<<(std::ostream& os,
130 const std::vector<Candidate>& candidates) {
131 os << '[';
132 bool first = true;
133 for (const Candidate& c : candidates) {
134 if (!first) {
135 os << ", ";
136 }
137 os << c.ToString();
138 first = false;
Steve Anton6c38cc72017-11-29 18:25:58139 }
Taylor Brandstetter8c9be5e42016-05-26 23:07:31140 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11141 return os;
142}
143
Mirko Bonadei6a489f22019-04-09 13:11:12144class BasicPortAllocatorTestBase : public ::testing::Test,
pthatcher1749bc32017-02-08 21:18:00145 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11146 public:
pthatcher1749bc32017-02-08 21:18:00147 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-17 01:00:06148 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11149 fss_(new rtc::FirewallSocketServer(vss_.get())),
nisse7eaa4ea2017-05-08 12:25:41150 thread_(fss_.get()),
deadbeefe97389c2016-12-23 09:43:45151 // Note that the NAT is not used by default. ResetWithStunServerAndNat
152 // must be called.
deadbeefc5d0d952015-07-16 17:22:21153 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-14 05:24:02154 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Danil Chapovalov6dbf0e42018-11-13 17:07:46155 stun_server_(TestStunServer::Create(rtc::Thread::Current(), kStunAddr)),
Danil Chapovalov6dbf0e42018-11-13 17:07:46156 turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11157 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 23:02:19158 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11159 stun_servers.insert(kStunAddr);
Niels Möllera8e6f342019-09-24 07:05:19160
161 allocator_ =
162 std::make_unique<BasicPortAllocator>(&network_manager_, stun_servers);
Qingsi Wanga2d60672018-04-11 23:57:45163 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 23:02:19164 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wang7fc821d2018-07-12 19:54:53165 webrtc::metrics::Reset();
henrike@webrtc.org269fb4b2014-10-28 22:20:11166 }
167
168 void AddInterface(const SocketAddress& addr) {
169 network_manager_.AddInterface(addr);
170 }
honghaiz8c404fa2015-09-28 14:59:43171 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
172 network_manager_.AddInterface(addr, if_name);
173 }
Taylor Brandstetter0c7e9f52015-12-29 22:14:52174 void AddInterface(const SocketAddress& addr,
175 const std::string& if_name,
176 rtc::AdapterType type) {
177 network_manager_.AddInterface(addr, if_name, type);
178 }
Guo-wei Shieh9af97f82015-11-10 22:47:39179 // The default route is the public address that STUN server will observe when
180 // the endpoint is sitting on the public internet and the local port is bound
181 // to the "any" address. This may be different from the default local address
182 // which the endpoint observes. This can occur if the route to the public
183 // endpoint like 8.8.8.8 (specified as the default local address) is
184 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-14 05:24:02185 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
186 AddInterface(addr);
187 // When a binding comes from the any address, the |addr| will be used as the
188 // srflx address.
189 vss_->SetDefaultRoute(addr.ipaddr());
190 }
honghaiz8c404fa2015-09-28 14:59:43191 void RemoveInterface(const SocketAddress& addr) {
192 network_manager_.RemoveInterface(addr);
193 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11194 bool SetPortRange(int min_port, int max_port) {
195 return allocator_->SetPortRange(min_port, max_port);
196 }
Guo-wei Shieh11477022015-08-15 16:28:41197 // Endpoint is on the public network. No STUN or TURN.
198 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 23:02:19199 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 23:57:45200 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 23:02:19201 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-14 05:24:02202 }
Guo-wei Shieh11477022015-08-15 16:28:41203 // Endpoint is behind a NAT, with STUN specified.
204 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
205 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-14 05:24:02206 }
Guo-wei Shieh11477022015-08-15 16:28:41207 // Endpoint is on the public network, with STUN specified.
208 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
209 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11210 }
Guo-wei Shieh11477022015-08-15 16:28:41211 // Endpoint is on the public network, with TURN specified.
212 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
213 const rtc::SocketAddress& tcp_turn) {
214 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11215 AddTurnServers(udp_turn, tcp_turn);
216 }
217
Honghai Zhang6981fb52019-10-29 19:45:34218 RelayServerConfig CreateTurnServers(const rtc::SocketAddress& udp_turn,
219 const rtc::SocketAddress& tcp_turn) {
Niels Möller191e38f2019-11-04 07:49:12220 RelayServerConfig turn_server;
Taylor Brandstetter417eebe2016-05-23 23:02:19221 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 20:55:10222 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11223
224 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 13:17:23225 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11226 }
227 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 13:17:23228 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11229 }
Honghai Zhang6981fb52019-10-29 19:45:34230 return turn_server;
231 }
232
233 void AddTurnServers(const rtc::SocketAddress& udp_turn,
234 const rtc::SocketAddress& tcp_turn) {
235 RelayServerConfig turn_server = CreateTurnServers(udp_turn, tcp_turn);
deadbeef653b8e02015-11-11 20:55:10236 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11237 }
238
239 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 15:15:11240 session_ = CreateSession("session", component);
241 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11242 return false;
Taylor Brandstettera1c30352016-05-13 15:15:11243 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11244 return true;
245 }
246
247 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 15:15:11248 session_ = CreateSession("session", content_name, component);
249 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11250 return false;
Taylor Brandstettera1c30352016-05-13 15:15:11251 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11252 return true;
253 }
254
Taylor Brandstetter417eebe2016-05-23 23:02:19255 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
256 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11257 return CreateSession(sid, kContentName, component);
258 }
259
Taylor Brandstetter417eebe2016-05-23 23:02:19260 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 15:15:11261 const std::string& sid,
262 const std::string& content_name,
263 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11264 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
265 }
266
Taylor Brandstetter417eebe2016-05-23 23:02:19267 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 15:15:11268 const std::string& sid,
269 const std::string& content_name,
270 int component,
271 const std::string& ice_ufrag,
272 const std::string& ice_pwd) {
johanfe1ffb12016-08-11 19:37:42273 std::unique_ptr<PortAllocatorSession> session =
274 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11275 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 21:18:00276 &BasicPortAllocatorTestBase::OnPortReady);
277 session->SignalPortsPruned.connect(
278 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 15:15:11279 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 21:18:00280 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 23:57:37281 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 21:18:00282 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 15:15:11283 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 21:18:00284 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11285 return session;
286 }
287
Taylor Brandstetter8c9be5e42016-05-26 23:07:31288 // Return true if the addresses are the same, or the port is 0 in |pattern|
289 // (acting as a wildcard) and the IPs are the same.
290 // Even with a wildcard port, the port of the address should be nonzero if
291 // the IP is nonzero.
292 static bool AddressMatch(const SocketAddress& address,
293 const SocketAddress& pattern) {
294 return address.ipaddr() == pattern.ipaddr() &&
295 ((pattern.port() == 0 &&
296 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
297 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11298 }
Taylor Brandstetter8c9be5e42016-05-26 23:07:31299
Honghai Zhangb9e7b4a2016-07-01 03:52:02300 // Returns the number of ports that have matching type, protocol and
301 // address.
302 static int CountPorts(const std::vector<PortInterface*>& ports,
303 const std::string& type,
304 ProtocolType protocol,
305 const SocketAddress& client_addr) {
Steve Antonae226f62019-01-29 20:47:38306 return absl::c_count_if(
307 ports, [type, protocol, client_addr](PortInterface* port) {
Honghai Zhangb9e7b4a2016-07-01 03:52:02308 return port->Type() == type && port->GetProtocol() == protocol &&
309 port->Network()->GetBestIP() == client_addr.ipaddr();
310 });
311 }
312
deadbeefe97389c2016-12-23 09:43:45313 static int CountCandidates(const std::vector<Candidate>& candidates,
314 const std::string& type,
315 const std::string& proto,
316 const SocketAddress& addr) {
Steve Antonae226f62019-01-29 20:47:38317 return absl::c_count_if(
318 candidates, [type, proto, addr](const Candidate& c) {
319 return c.type() == type && c.protocol() == proto &&
320 AddressMatch(c.address(), addr);
321 });
deadbeefe97389c2016-12-23 09:43:45322 }
323
Taylor Brandstetter8c9be5e42016-05-26 23:07:31324 // Find a candidate and return it.
325 static bool FindCandidate(const std::vector<Candidate>& candidates,
326 const std::string& type,
327 const std::string& proto,
328 const SocketAddress& addr,
329 Candidate* found) {
Steve Antonae226f62019-01-29 20:47:38330 auto it =
331 absl::c_find_if(candidates, [type, proto, addr](const Candidate& c) {
332 return c.type() == type && c.protocol() == proto &&
333 AddressMatch(c.address(), addr);
334 });
Taylor Brandstetter8c9be5e42016-05-26 23:07:31335 if (it != candidates.end() && found) {
336 *found = *it;
337 }
338 return it != candidates.end();
339 }
340
341 // Convenience method to call FindCandidate with no return.
342 static bool HasCandidate(const std::vector<Candidate>& candidates,
343 const std::string& type,
344 const std::string& proto,
345 const SocketAddress& addr) {
346 return FindCandidate(candidates, type, proto, addr, nullptr);
347 }
348
349 // Version of HasCandidate that also takes a related address.
350 static bool HasCandidateWithRelatedAddr(
351 const std::vector<Candidate>& candidates,
352 const std::string& type,
353 const std::string& proto,
354 const SocketAddress& addr,
355 const SocketAddress& related_addr) {
Steve Antonae226f62019-01-29 20:47:38356 return absl::c_any_of(
357 candidates, [type, proto, addr, related_addr](const Candidate& c) {
358 return c.type() == type && c.protocol() == proto &&
359 AddressMatch(c.address(), addr) &&
360 AddressMatch(c.related_address(), related_addr);
361 });
Taylor Brandstetter8c9be5e42016-05-26 23:07:31362 }
363
henrike@webrtc.org269fb4b2014-10-28 22:20:11364 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 15:15:11365 int min_port,
366 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11367 return (addr.port() >= min_port && addr.port() <= max_port);
368 }
369
Taylor Brandstetter417eebe2016-05-23 23:02:19370 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11371 // We should only get this callback once, except in the mux test where
372 // we have multiple port allocation sessions.
373 if (session == session_.get()) {
374 ASSERT_FALSE(candidate_allocation_done_);
375 candidate_allocation_done_ = true;
376 }
Taylor Brandstettera1c30352016-05-13 15:15:11377 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11378 }
379
380 // Check if all ports allocated have send-buffer size |expected|. If
381 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
382 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 23:02:19383 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11384 for (it = ports_.begin(); it < ports_.end(); ++it) {
385 int send_buffer_size;
386 if (expected == -1) {
387 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 15:15:11388 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11389 } else {
Taylor Brandstettera1c30352016-05-13 15:15:11390 EXPECT_EQ(0,
391 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11392 ASSERT_EQ(expected, send_buffer_size);
393 }
394 }
395 }
396
Honghai Zhangc67e0f52016-09-19 23:57:37397 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
398
henrike@webrtc.org269fb4b2014-10-28 22:20:11399 protected:
Taylor Brandstetter417eebe2016-05-23 23:02:19400 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11401
Taylor Brandstetter417eebe2016-05-23 23:02:19402 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 10:09:25403 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11404 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 15:15:11405 // Make sure the new port is added to ReadyPorts.
406 auto ready_ports = ses->ReadyPorts();
Steve Antonae226f62019-01-29 20:47:38407 EXPECT_THAT(ready_ports, Contains(port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11408 }
Honghai Zhang8eeecab2016-07-28 20:20:15409 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-29 01:06:15410 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 10:09:25411 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-07-01 03:52:02412 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 20:20:15413 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-29 01:06:15414 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 20:20:15415 new_end = std::remove(ports_.begin(), new_end, port);
416 // Make sure the pruned port is not in ReadyPorts.
Steve Antonae226f62019-01-29 20:47:38417 EXPECT_THAT(ready_ports, Not(Contains(port)));
Honghai Zhang8eeecab2016-07-28 20:20:15418 }
419 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-07-01 03:52:02420 }
421
Taylor Brandstetter417eebe2016-05-23 23:02:19422 void OnCandidatesReady(PortAllocatorSession* ses,
423 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e42016-05-26 23:07:31424 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 10:09:25425 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e42016-05-26 23:07:31426 // Sanity check that the ICE component is set.
427 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
428 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11429 }
Taylor Brandstettera1c30352016-05-13 15:15:11430 // Make sure the new candidates are added to Candidates.
431 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 23:02:19432 for (const Candidate& candidate : candidates) {
Steve Antonae226f62019-01-29 20:47:38433 EXPECT_THAT(ses_candidates, Contains(candidate));
Taylor Brandstettera1c30352016-05-13 15:15:11434 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11435 }
436
Honghai Zhangc67e0f52016-09-19 23:57:37437 void OnCandidatesRemoved(PortAllocatorSession* session,
438 const std::vector<Candidate>& removed_candidates) {
439 auto new_end = std::remove_if(
440 candidates_.begin(), candidates_.end(),
441 [removed_candidates](Candidate& candidate) {
442 for (const Candidate& removed_candidate : removed_candidates) {
443 if (candidate.MatchesForRemoval(removed_candidate)) {
444 return true;
445 }
446 }
447 return false;
448 });
449 candidates_.erase(new_end, candidates_.end());
450 }
451
Taylor Brandstetter417eebe2016-05-23 23:02:19452 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 20:55:10453 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 23:02:19454 RelayServerConfig server_config = allocator_->turn_servers()[i];
455 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11456 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 15:15:11457 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11458 if (proto_addr.address == relay_port->address &&
459 proto_addr.proto == relay_port->proto)
460 return true;
461 }
462 }
463 return false;
464 }
465
Guo-wei Shieh11477022015-08-15 16:28:41466 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
467 bool with_nat) {
468 if (with_nat) {
469 nat_server_.reset(new rtc::NATServer(
470 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
471 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
472 } else {
473 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
474 }
475
476 ServerAddresses stun_servers;
477 if (!stun_server.IsNil()) {
478 stun_servers.insert(stun_server);
479 }
Taylor Brandstetter417eebe2016-05-23 23:02:19480 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 16:28:41481 &network_manager_, nat_socket_factory_.get(), stun_servers));
Qingsi Wanga2d60672018-04-11 23:57:45482 allocator_->Initialize();
483 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 16:28:41484 }
485
pthatcher1749bc32017-02-08 21:18:00486 std::unique_ptr<rtc::VirtualSocketServer> vss_;
487 std::unique_ptr<rtc::FirewallSocketServer> fss_;
nisse7eaa4ea2017-05-08 12:25:41488 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 21:18:00489 std::unique_ptr<rtc::NATServer> nat_server_;
490 rtc::NATSocketFactory nat_factory_;
491 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
492 std::unique_ptr<TestStunServer> stun_server_;
pthatcher1749bc32017-02-08 21:18:00493 TestTurnServer turn_server_;
494 rtc::FakeNetworkManager network_manager_;
495 std::unique_ptr<BasicPortAllocator> allocator_;
496 std::unique_ptr<PortAllocatorSession> session_;
497 std::vector<PortInterface*> ports_;
498 std::vector<Candidate> candidates_;
499 bool candidate_allocation_done_;
500};
Honghai Zhangc67e0f52016-09-19 23:57:37501
pthatcher1749bc32017-02-08 21:18:00502class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
503};
504
505class FakeClockBase {
506 public:
507 rtc::ScopedFakeClock fake_clock;
508};
509
510class BasicPortAllocatorTest : public FakeClockBase,
511 public BasicPortAllocatorTestBase {
512 public:
513 // This function starts the port/address gathering and check the existence of
514 // candidates as specified. When |expect_stun_candidate| is true,
515 // |stun_candidate_addr| carries the expected reflective address, which is
516 // also the related address for TURN candidate if it is expected. Otherwise,
517 // it should be ignore.
518 void CheckDisableAdapterEnumeration(
519 uint32_t total_ports,
520 const rtc::IPAddress& host_candidate_addr,
521 const rtc::IPAddress& stun_candidate_addr,
522 const rtc::IPAddress& relay_candidate_udp_transport_addr,
523 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
524 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
525 rtc::IPAddress());
526 if (!session_) {
Qingsi Wange53ac042018-05-08 18:55:07527 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 21:18:00528 }
529 session_->set_flags(session_->flags() |
530 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
531 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
532 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 23:57:37533 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00534 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
535 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 23:57:37536
pthatcher1749bc32017-02-08 21:18:00537 uint32_t total_candidates = 0;
538 if (!host_candidate_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 11:40:05539 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
540 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:00541 ++total_candidates;
542 }
543 if (!stun_candidate_addr.IsNil()) {
544 rtc::SocketAddress related_address(host_candidate_addr, 0);
545 if (host_candidate_addr.IsNil()) {
546 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
547 }
Jonas Olssonabbe8412018-04-03 11:40:05548 EXPECT_TRUE(HasCandidateWithRelatedAddr(
549 candidates_, "stun", "udp",
550 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
pthatcher1749bc32017-02-08 21:18:00551 ++total_candidates;
552 }
553 if (!relay_candidate_udp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 11:40:05554 EXPECT_TRUE(HasCandidateWithRelatedAddr(
555 candidates_, "relay", "udp",
556 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
557 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 21:18:00558 ++total_candidates;
559 }
560 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 11:40:05561 EXPECT_TRUE(HasCandidateWithRelatedAddr(
562 candidates_, "relay", "udp",
563 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
564 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 21:18:00565 ++total_candidates;
566 }
567
568 EXPECT_EQ(total_candidates, candidates_.size());
569 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 23:57:37570 }
571
572 void TestIPv6TurnPortPrunesIPv4TurnPort() {
573 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
574 // Add two IP addresses on the same interface.
575 AddInterface(kClientAddr, "net1");
576 AddInterface(kClientIPv6Addr, "net1");
577 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 23:57:45578 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 23:57:37579 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:50580 allocator_->turn_servers(), 0,
581 webrtc::PRUNE_BASED_ON_PRIORITY);
Honghai Zhangc67e0f52016-09-19 23:57:37582 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
583 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
584
585 allocator_->set_step_delay(kMinimumStepDelay);
586 allocator_->set_flags(
587 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
588 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
589
Qingsi Wange53ac042018-05-08 18:55:07590 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 23:57:37591 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00592 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
593 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 23:57:37594 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
595 EXPECT_EQ(3U, session_->ReadyPorts().size());
596 EXPECT_EQ(3U, ports_.size());
597 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
598 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
599 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
600 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
601
602 // Now that we remove candidates when a TURN port is pruned, there will be
603 // exactly 3 candidates in both |candidates_| and |ready_candidates|.
604 EXPECT_EQ(3U, candidates_.size());
605 const std::vector<Candidate>& ready_candidates =
606 session_->ReadyCandidates();
607 EXPECT_EQ(3U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 11:40:05608 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
609 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
610 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 23:57:37611 }
612
Honghai Zhangf8998cf2019-10-14 18:27:50613 void TestTurnPortPrunesWithUdpAndTcpPorts(
614 webrtc::PortPrunePolicy prune_policy,
615 bool tcp_pruned) {
pthatcher1749bc32017-02-08 21:18:00616 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
617 AddInterface(kClientAddr);
618 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 23:57:45619 allocator_->Initialize();
pthatcher1749bc32017-02-08 21:18:00620 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:50621 allocator_->turn_servers(), 0, prune_policy);
pthatcher1749bc32017-02-08 21:18:00622 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
623 allocator_->set_step_delay(kMinimumStepDelay);
624 allocator_->set_flags(allocator().flags() |
625 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
626 PORTALLOCATOR_DISABLE_TCP);
627
Qingsi Wange53ac042018-05-08 18:55:07628 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 21:18:00629 session_->StartGettingPorts();
630 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
631 kDefaultAllocationTimeout, fake_clock);
632 // Only 2 ports (one STUN and one TURN) are actually being used.
633 EXPECT_EQ(2U, session_->ReadyPorts().size());
634 // We have verified that each port, when it is added to |ports_|, it is
635 // found in |ready_ports|, and when it is pruned, it is not found in
636 // |ready_ports|, so we only need to verify the content in one of them.
637 EXPECT_EQ(2U, ports_.size());
638 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
Honghai Zhangf8998cf2019-10-14 18:27:50639 int num_udp_ports = tcp_pruned ? 1 : 0;
640 EXPECT_EQ(num_udp_ports,
641 CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
642 EXPECT_EQ(1 - num_udp_ports,
643 CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
pthatcher1749bc32017-02-08 21:18:00644
645 // Now that we remove candidates when a TURN port is pruned, |candidates_|
646 // should only contains two candidates regardless whether the TCP TURN port
647 // is created before or after the UDP turn port.
648 EXPECT_EQ(2U, candidates_.size());
649 // There will only be 2 candidates in |ready_candidates| because it only
650 // includes the candidates in the ready ports.
651 const std::vector<Candidate>& ready_candidates =
652 session_->ReadyCandidates();
653 EXPECT_EQ(2U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 11:40:05654 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
Honghai Zhangf8998cf2019-10-14 18:27:50655
656 // The external candidate is always udp.
Jonas Olssonabbe8412018-04-03 11:40:05657 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
658 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:00659 }
660
Honghai Zhangc67e0f52016-09-19 23:57:37661 void TestEachInterfaceHasItsOwnTurnPorts() {
662 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
663 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
664 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
665 // Add two interfaces both having IPv4 and IPv6 addresses.
666 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
667 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
668 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
669 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
670 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 23:57:45671 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 23:57:37672 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:50673 allocator_->turn_servers(), 0,
674 webrtc::PRUNE_BASED_ON_PRIORITY);
Honghai Zhangc67e0f52016-09-19 23:57:37675 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
676 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
677 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
678
679 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 22:40:51680 allocator_->set_flags(
681 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
682 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Qingsi Wange53ac042018-05-08 18:55:07683 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 23:57:37684 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00685 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
686 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 23:57:37687 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
688 // use.
689 EXPECT_EQ(10U, session_->ReadyPorts().size());
690 EXPECT_EQ(10U, ports_.size());
691 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
692 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
693 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
694 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
695 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
696 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
697 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
698 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
699 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
700 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
701
702 // Now that we remove candidates when TURN ports are pruned, there will be
703 // exactly 10 candidates in |candidates_|.
704 EXPECT_EQ(10U, candidates_.size());
705 const std::vector<Candidate>& ready_candidates =
706 session_->ReadyCandidates();
707 EXPECT_EQ(10U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 11:40:05708 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
709 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
710 EXPECT_TRUE(
711 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
712 EXPECT_TRUE(
713 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
714 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
715 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
716 EXPECT_TRUE(
717 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
718 EXPECT_TRUE(
719 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
720 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
721 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 23:57:37722 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11723};
724
725// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 15:15:11726TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11727 EXPECT_EQ(&network_manager_, allocator().network_manager());
728 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
Niels Möllera8e6f342019-09-24 07:05:19729 ASSERT_EQ(0u, allocator().turn_servers().size());
730
Qingsi Wange53ac042018-05-08 18:55:07731 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 15:15:11732 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11733}
734
Taylor Brandstetter0c7e9f52015-12-29 22:14:52735// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 15:15:11736TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:52737 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
738 rtc::ADAPTER_TYPE_ETHERNET);
739 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
740 rtc::ADAPTER_TYPE_WIFI);
741 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
742 rtc::ADAPTER_TYPE_CELLULAR);
743 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
744 rtc::ADAPTER_TYPE_VPN);
745 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
746 rtc::ADAPTER_TYPE_LOOPBACK);
Qingsi Wange53ac042018-05-08 18:55:07747 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:19748 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
749 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52750 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00751 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
752 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52753 EXPECT_EQ(4U, candidates_.size());
Mirko Bonadei739baf02019-01-27 16:29:42754 for (const Candidate& candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:52755 EXPECT_LT(candidate.address().ip(), 0x12345604U);
756 }
757}
758
Taylor Brandstettera1c30352016-05-13 15:15:11759TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:52760 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
761 rtc::ADAPTER_TYPE_ETHERNET);
762 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
763 rtc::ADAPTER_TYPE_WIFI);
764 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
765 rtc::ADAPTER_TYPE_CELLULAR);
766 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
767 rtc::ADAPTER_TYPE_LOOPBACK |
768 rtc::ADAPTER_TYPE_WIFI);
Qingsi Wange53ac042018-05-08 18:55:07769 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:19770 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
771 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52772 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00773 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
774 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52775 EXPECT_EQ(1U, candidates_.size());
776 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
777}
778
deadbeef1c46a352017-09-27 18:24:05779// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
780// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
781TEST_F(BasicPortAllocatorTest,
782 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
783 SocketAddress wifi(IPAddress(0x12345600U), 0);
784 SocketAddress cell(IPAddress(0x12345601U), 0);
785 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
786 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
787 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-06-01 01:29:12788 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
789 cricket::PORTALLOCATOR_DISABLE_RELAY |
790 cricket::PORTALLOCATOR_DISABLE_TCP |
791 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07792 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-06-01 01:29:12793 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00794 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
795 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 18:24:05796 // Should only get one Wi-Fi candidate.
honghaiz60347052016-06-01 01:29:12797 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05798 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 18:24:05799}
honghaiz60347052016-06-01 01:29:12800
deadbeef1c46a352017-09-27 18:24:05801// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
802// both "unknown" and cell interfaces are available, only the unknown are used.
803// The unknown interface may be something that ultimately uses Wi-Fi, so we do
804// this to be on the safe side.
805TEST_F(BasicPortAllocatorTest,
806 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
807 SocketAddress cell(IPAddress(0x12345601U), 0);
808 SocketAddress unknown1(IPAddress(0x12345602U), 0);
809 SocketAddress unknown2(IPAddress(0x12345603U), 0);
810 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
811 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
812 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
813 // Disable all but UDP candidates to make the test simpler.
814 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
815 cricket::PORTALLOCATOR_DISABLE_RELAY |
816 cricket::PORTALLOCATOR_DISABLE_TCP |
817 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07818 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-06-01 01:29:12819 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00820 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
821 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 18:24:05822 // Should only get two candidates, none of which is cell.
honghaiz60347052016-06-01 01:29:12823 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05824 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
825 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
deadbeef1c46a352017-09-27 18:24:05826}
honghaiz60347052016-06-01 01:29:12827
deadbeef1c46a352017-09-27 18:24:05828// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
829// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
830// interface is used.
831TEST_F(BasicPortAllocatorTest,
832 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
833 SocketAddress wifi(IPAddress(0x12345600U), 0);
834 SocketAddress cellular(IPAddress(0x12345601U), 0);
835 SocketAddress unknown1(IPAddress(0x12345602U), 0);
836 SocketAddress unknown2(IPAddress(0x12345603U), 0);
837 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
838 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
839 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
840 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
841 // Disable all but UDP candidates to make the test simpler.
842 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
843 cricket::PORTALLOCATOR_DISABLE_RELAY |
844 cricket::PORTALLOCATOR_DISABLE_TCP |
845 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07846 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-06-01 01:29:12847 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00848 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
849 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 18:24:05850 // Should only get one Wi-Fi candidate.
honghaiz60347052016-06-01 01:29:12851 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05852 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 18:24:05853}
854
855// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
856// only interface available is cellular, it ends up used anyway. A costly
857// connection is always better than no connection.
858TEST_F(BasicPortAllocatorTest,
859 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
860 SocketAddress cellular(IPAddress(0x12345601U), 0);
861 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
862 // Disable all but UDP candidates to make the test simpler.
863 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
864 cricket::PORTALLOCATOR_DISABLE_RELAY |
865 cricket::PORTALLOCATOR_DISABLE_TCP |
866 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07867 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 18:24:05868 session_->StartGettingPorts();
869 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
870 kDefaultAllocationTimeout, fake_clock);
871 // Make sure we got the cell candidate.
872 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05873 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
honghaiz60347052016-06-01 01:29:12874}
875
Yuwei Huangb181f712018-01-23 01:01:28876// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
877// a WiFi network with link-local IP address and a cellular network, then the
878// cellular candidate will still be gathered.
879TEST_F(BasicPortAllocatorTest,
880 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
881 SocketAddress wifi_link_local("169.254.0.1", 0);
882 SocketAddress cellular(IPAddress(0x12345601U), 0);
883 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
884 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
885
886 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
887 cricket::PORTALLOCATOR_DISABLE_RELAY |
888 cricket::PORTALLOCATOR_DISABLE_TCP |
889 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07890 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-23 01:01:28891 session_->StartGettingPorts();
892 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
893 kDefaultAllocationTimeout, fake_clock);
894 // Make sure we got both wifi and cell candidates.
895 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05896 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
897 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
Yuwei Huangb181f712018-01-23 01:01:28898}
899
900// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
901// a WiFi network with link-local IP address, a WiFi network with a normal IP
902// address and a cellular network, then the cellular candidate will not be
903// gathered.
904TEST_F(BasicPortAllocatorTest,
905 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
906 SocketAddress wifi(IPAddress(0x12345600U), 0);
907 SocketAddress wifi_link_local("169.254.0.1", 0);
908 SocketAddress cellular(IPAddress(0x12345601U), 0);
909 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
910 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
911 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
912
913 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
914 cricket::PORTALLOCATOR_DISABLE_RELAY |
915 cricket::PORTALLOCATOR_DISABLE_TCP |
916 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07917 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-23 01:01:28918 session_->StartGettingPorts();
919 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
920 kDefaultAllocationTimeout, fake_clock);
921 // Make sure we got only wifi candidates.
922 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05923 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
924 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
Yuwei Huangb181f712018-01-23 01:01:28925}
926
Qingsi Wange53ac042018-05-08 18:55:07927// Test that the adapter types of the Ethernet and the VPN can be correctly
928// identified so that the Ethernet has a lower network cost than the VPN, and
929// the Ethernet is not filtered out if PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is
930// set.
931TEST_F(BasicPortAllocatorTest,
932 EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent) {
933 AddInterface(kClientAddr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
934 AddInterface(kClientAddr2, "tap0", rtc::ADAPTER_TYPE_VPN);
935 allocator().set_flags(PORTALLOCATOR_DISABLE_COSTLY_NETWORKS |
936 PORTALLOCATOR_DISABLE_RELAY |
937 PORTALLOCATOR_DISABLE_TCP);
938 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
939 session_->StartGettingPorts();
940 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
941 kDefaultAllocationTimeout, fake_clock);
942 // The VPN tap0 network should be filtered out as a costly network, and we
943 // should have a UDP port and a STUN port from the Ethernet eth0.
944 ASSERT_EQ(2U, ports_.size());
945 EXPECT_EQ(ports_[0]->Network()->name(), "eth0");
946 EXPECT_EQ(ports_[1]->Network()->name(), "eth0");
947}
948
deadbeef3427f532017-07-26 23:09:33949// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
950// to gather candidates.
951TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
952 // Add three IPv6 network interfaces, but tell the allocator to only use two.
953 allocator().set_max_ipv6_networks(2);
954 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
955 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
956 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
957
958 // To simplify the test, only gather UDP host candidates.
959 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
960 PORTALLOCATOR_DISABLE_STUN |
961 PORTALLOCATOR_DISABLE_RELAY);
962
Qingsi Wange53ac042018-05-08 18:55:07963 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 23:09:33964 session_->StartGettingPorts();
965 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
966 kDefaultAllocationTimeout, fake_clock);
967 EXPECT_EQ(2U, candidates_.size());
968 // Ensure the expected two interfaces (eth0 and eth1) were used.
Jonas Olssonabbe8412018-04-03 11:40:05969 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
970 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
deadbeef3427f532017-07-26 23:09:33971}
972
973// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
974// being used.
975TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
976 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
977 allocator().set_max_ipv6_networks(1);
978 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
979 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
980 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
981 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
982
983 // To simplify the test, only gather UDP host candidates.
984 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
985 PORTALLOCATOR_DISABLE_STUN |
986 PORTALLOCATOR_DISABLE_RELAY);
987
Qingsi Wange53ac042018-05-08 18:55:07988 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 23:09:33989 session_->StartGettingPorts();
990 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
991 kDefaultAllocationTimeout, fake_clock);
992 EXPECT_EQ(3U, candidates_.size());
993 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
994 // were used.
Jonas Olssonabbe8412018-04-03 11:40:05995 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
996 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
997 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
deadbeef3427f532017-07-26 23:09:33998}
999
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201000// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111001TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:521002 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
1003 allocator_->SetNetworkIgnoreMask(0);
Qingsi Wange53ac042018-05-08 18:55:071004 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191005 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
1006 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201007 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001008 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1009 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201010 EXPECT_EQ(1U, candidates_.size());
1011}
1012
henrike@webrtc.org269fb4b2014-10-28 22:20:111013// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 15:15:111014TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111015 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071016 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111017 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011018 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1019 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191020 EXPECT_EQ(3U, candidates_.size());
1021 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051022 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1023 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
Jonas Olssonabbe8412018-04-03 11:40:051024 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:111025}
1026
honghaiz8c404fa2015-09-28 14:59:431027// Test that when the same network interface is brought down and up, the
1028// port allocator session will restart a new allocation sequence if
1029// it is not stopped.
Taylor Brandstettera1c30352016-05-13 15:15:111030TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 14:59:431031 std::string if_name("test_net0");
1032 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 18:55:071033 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 14:59:431034 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011035 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1036 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191037 EXPECT_EQ(3U, candidates_.size());
1038 EXPECT_EQ(3U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431039 candidate_allocation_done_ = false;
1040 candidates_.clear();
1041 ports_.clear();
1042
deadbeef7f1563f2017-09-16 00:40:011043 // Disable socket creation to simulate the network interface being down. When
1044 // no network interfaces are available, BasicPortAllocator will fall back to
1045 // binding to the "ANY" address, so we need to make sure that fails too.
1046 fss_->set_tcp_sockets_enabled(false);
1047 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 14:59:431048 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-16 00:40:011049 SIMULATED_WAIT(false, 1000, fake_clock);
1050 EXPECT_EQ(0U, candidates_.size());
1051 ports_.clear();
Niels Möllera8e6f342019-09-24 07:05:191052 candidate_allocation_done_ = false;
honghaiz8c404fa2015-09-28 14:59:431053
1054 // When the same interfaces are added again, new candidates/ports should be
1055 // generated.
deadbeef7f1563f2017-09-16 00:40:011056 fss_->set_tcp_sockets_enabled(true);
1057 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 14:59:431058 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-16 00:40:011059 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1060 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191061 // TODO(nisse): Needs fixing, test fails with sizes == 0.
1062 EXPECT_EQ(3U, candidates_.size());
1063 EXPECT_EQ(3U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431064}
1065
1066// Test that when the same network interface is brought down and up, the
1067// port allocator session will not restart a new allocation sequence if
1068// it is stopped.
Taylor Brandstettera1c30352016-05-13 15:15:111069TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 14:59:431070 std::string if_name("test_net0");
1071 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 18:55:071072 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 14:59:431073 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011074 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1075 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191076 EXPECT_EQ(3U, candidates_.size());
1077 EXPECT_EQ(3U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431078 session_->StopGettingPorts();
1079 candidates_.clear();
1080 ports_.clear();
1081
1082 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-16 00:40:011083 // Wait one (simulated) second and then verify no new candidates have
1084 // appeared.
1085 SIMULATED_WAIT(false, 1000, fake_clock);
1086 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 14:59:431087 EXPECT_EQ(0U, ports_.size());
1088
1089 // When the same interfaces are added again, new candidates/ports should not
1090 // be generated because the session has stopped.
1091 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-16 00:40:011092 SIMULATED_WAIT(false, 1000, fake_clock);
1093 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 14:59:431094 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431095}
1096
deadbeef1c46a352017-09-27 18:24:051097// Similar to the above tests, but tests a situation when sockets can't be
1098// bound to a network interface, then after a network change event can be.
1099// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1100TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1101 // Only test local ports to simplify test.
1102 ResetWithNoServersOrNat();
1103 // Provide a situation where the interface appears to be available, but
1104 // binding the sockets fails. See bug for description of when this can
1105 // happen.
1106 std::string if_name("test_net0");
1107 AddInterface(kClientAddr, if_name);
1108 fss_->set_tcp_sockets_enabled(false);
1109 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071110 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 18:24:051111 session_->StartGettingPorts();
1112 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1113 kDefaultAllocationTimeout, fake_clock);
1114 // Make sure we actually prevented candidates from being gathered (other than
1115 // a single TCP active candidate, since that doesn't require creating a
1116 // socket).
1117 ASSERT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051118 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 18:24:051119 candidate_allocation_done_ = false;
1120
1121 // Now simulate the interface coming up, with the newfound ability to bind
1122 // sockets.
1123 fss_->set_tcp_sockets_enabled(true);
1124 fss_->set_udp_sockets_enabled(true);
1125 AddInterface(kClientAddr, if_name);
1126 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1127 kDefaultAllocationTimeout, fake_clock);
1128 // Should get UDP and TCP candidate.
1129 ASSERT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051130 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
deadbeef1c46a352017-09-27 18:24:051131 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1132 // We should extend this test to also verify that a server candidate is
1133 // gathered.
Jonas Olssonabbe8412018-04-03 11:40:051134 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 18:24:051135}
1136
henrike@webrtc.org269fb4b2014-10-28 22:20:111137// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 15:15:111138TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111139 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191140 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 18:55:071141 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111142 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001143 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111144 EXPECT_EQ(2U, ports_.size());
Niels Möllera8e6f342019-09-24 07:05:191145 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111146 EXPECT_EQ(3U, ports_.size());
Niels Möllera8e6f342019-09-24 07:05:191147
1148 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 11:40:051149 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Niels Möllera8e6f342019-09-24 07:05:191150 EXPECT_EQ(3U, ports_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111151 EXPECT_TRUE(candidate_allocation_done_);
1152 // If we Stop gathering now, we shouldn't get a second "done" callback.
1153 session_->StopGettingPorts();
1154}
1155
Taylor Brandstettera1c30352016-05-13 15:15:111156TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111157 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071158 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:111159 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011160 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1161 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191162 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111163 // If we Stop gathering now, we shouldn't get a second "done" callback.
1164 session_->StopGettingPorts();
1165
1166 // All ports should have unset send-buffer sizes.
1167 CheckSendBufferSizesOfAllPorts(-1);
1168}
1169
deadbeef7f1563f2017-09-16 00:40:011170// Tests that we can get callback after StopGetAllPorts when called in the
1171// middle of gathering.
Taylor Brandstettera1c30352016-05-13 15:15:111172TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111173 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071174 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111175 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011176 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1177 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111178 EXPECT_EQ(2U, ports_.size());
1179 session_->StopGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011180 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1181 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111182}
1183
1184// Test that we restrict client ports appropriately when a port range is set.
1185// We check the candidates for udp/stun/tcp ports, and the from address
1186// for relay ports.
Taylor Brandstettera1c30352016-05-13 15:15:111187TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111188 AddInterface(kClientAddr);
1189 // Check that an invalid port range fails.
1190 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1191 // Check that a null port range succeeds.
1192 EXPECT_TRUE(SetPortRange(0, 0));
1193 // Check that a valid port range succeeds.
1194 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Qingsi Wange53ac042018-05-08 18:55:071195 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111196 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011197 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1198 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191199 EXPECT_EQ(3U, candidates_.size());
1200 EXPECT_EQ(3U, ports_.size());
honghaiza73df552016-10-27 18:44:171201
1202 int num_nonrelay_candidates = 0;
1203 for (const Candidate& candidate : candidates_) {
1204 // Check the port number for the UDP/STUN/TCP port objects.
1205 if (candidate.type() != RELAY_PORT_TYPE) {
Jonas Olssonabbe8412018-04-03 11:40:051206 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 18:44:171207 ++num_nonrelay_candidates;
1208 }
1209 }
1210 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:111211}
1212
deadbeefe97389c2016-12-23 09:43:451213// Test that if we have no network adapters, we bind to the ANY address and
1214// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111215TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 09:43:451216 // Default config uses GTURN and no NAT, so replace that with the
1217 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1218 ResetWithStunServerAndNat(kStunAddr);
1219 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1220 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1221 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
Qingsi Wange53ac042018-05-08 18:55:071222 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111223 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001224 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1225 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 09:43:451226 EXPECT_EQ(4U, ports_.size());
1227 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1228 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1229 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1230 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1231 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1232 // The "any" address port should be in the signaled ready ports, but the host
1233 // candidate for it is useless and shouldn't be signaled. So we only have
1234 // STUN/TURN candidates.
1235 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051236 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1237 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 09:43:451238 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1239 // server.
1240 EXPECT_EQ(2,
1241 CountCandidates(candidates_, "relay", "udp",
1242 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:111243}
1244
Guo-wei Shieh898d21c2015-09-30 17:54:551245// Test that when enumeration is disabled, we should not have any ports when
1246// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 15:15:111247TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 17:54:551248 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 17:54:551249 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wangc129c352019-04-18 17:41:581250 allocator().SetCandidateFilter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 17:54:551251 // Expect to see no ports and no candidates.
1252 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1253 rtc::IPAddress(), rtc::IPAddress());
1254}
1255
Guo-wei Shieh9af97f82015-11-10 22:47:391256// Test that even with multiple interfaces, the result should still be a single
1257// default private, one STUN and one TURN candidate since we bind to any address
1258// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 15:15:111259TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-14 05:24:021260 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:011261 AddInterface(kPrivateAddr);
1262 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 16:28:411263 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:011264 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 22:12:381265
1266 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1267 // address set and we have no IPv6 STUN server, there should be no IPv6
1268 // candidates.
Qingsi Wange53ac042018-05-08 18:55:071269 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191270 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 22:12:381271
1272 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1273 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1274 // TURN/UDP candidates.
1275 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 22:47:391276 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1277 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-14 05:24:021278}
guoweis@webrtc.orgf358aea2015-02-18 18:44:011279
Guo-wei Shieh9af97f82015-11-10 22:47:391280// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1281// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 15:15:111282TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 23:02:191283 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 22:47:391284 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 16:28:411285 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-14 05:24:021286 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 22:47:391287 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1288 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1289 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1290 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-14 05:24:021291 kTurnUdpExtAddr.ipaddr());
1292}
1293
Guo-wei Shieh9af97f82015-11-10 22:47:391294// Test that when adapter enumeration is disabled, for endpoints without
1295// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 15:15:111296TEST_F(BasicPortAllocatorTest,
1297 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 22:47:391298 ResetWithNoServersOrNat();
1299 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1300 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1301 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-14 05:24:021302}
1303
Guo-wei Shieh9af97f82015-11-10 22:47:391304// Test that when adapter enumeration is disabled, with
1305// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1306// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111307TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 22:47:391308 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1309 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wange53ac042018-05-08 18:55:071310 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191311 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 22:47:391312 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1313 // candidate.
Guo-wei Shieh38f88932015-08-14 05:24:021314 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201315 rtc::IPAddress(), rtc::IPAddress());
1316}
1317
1318// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 22:47:391319// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1320// a NAT, there is no local candidate. However, this specified default route
1321// (kClientAddr) which was discovered when sending STUN requests, will become
1322// the srflx addresses.
Yves Gerey665174f2018-06-19 13:03:051323TEST_F(BasicPortAllocatorTest,
1324 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201325 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 22:47:391326 AddInterfaceAsDefaultRoute(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071327 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191328 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201329 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1330 // candidate.
Guo-wei Shieh9af97f82015-11-10 22:47:391331 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1332 rtc::IPAddress(), rtc::IPAddress());
1333}
1334
1335// Test that when adapter enumeration is disabled, with
1336// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1337// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111338TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 22:47:391339 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1340 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 18:55:071341 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191342 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 22:47:391343 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1344 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1345 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:011346}
1347
Guo-wei Shieh13d35f62015-08-26 22:32:561348// Test that we disable relay over UDP, and only TCP is used when connecting to
1349// the relay server.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311350TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 23:02:191351 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 22:32:561352 AddInterface(kClientAddr);
1353 ResetWithStunServerAndNat(kStunAddr);
1354 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 18:55:071355 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191356 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1357 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1358 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 22:32:561359
1360 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001361 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1362 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 22:32:561363
1364 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1365 // TURN/TCP candidates.
1366 EXPECT_EQ(2U, ports_.size());
1367 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e42016-05-26 23:07:311368 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051369 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1370 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 22:32:561371 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311372 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 11:40:051373 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 22:32:561374}
1375
henrike@webrtc.org269fb4b2014-10-28 22:20:111376// Test that we can get OnCandidatesAllocationDone callback when all the ports
1377// are disabled.
Taylor Brandstettera1c30352016-05-13 15:15:111378TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111379 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071380 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191381 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1382 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111383 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001384 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111385 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111386}
1387
1388// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 15:15:111389TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111390 AddInterface(kClientAddr);
1391 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071392 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:111393 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011394 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1395 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191396 EXPECT_EQ(1U, candidates_.size());
1397 EXPECT_EQ(1U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051398 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:111399}
1400
1401// Test that we don't crash or malfunction if we can't create UDP sockets or
1402// listen on TCP sockets. We still give out a local TCP address, since
1403// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 15:15:111404TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111405 AddInterface(kClientAddr);
1406 fss_->set_udp_sockets_enabled(false);
1407 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071408 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:111409 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011410 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1411 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191412 EXPECT_EQ(1U, candidates_.size());
1413 EXPECT_EQ(1U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051414 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:111415}
1416
1417// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 15:15:111418// TODO(deadbeef): Find a way to exit early here.
1419TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111420 AddInterface(kClientAddr);
1421 fss_->set_tcp_sockets_enabled(false);
1422 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071423 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111424 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011425 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 15:15:111426 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:111427 // In case of Relay, ports creation will succeed but sockets will fail.
1428 // There is no error reporting from RelayEntry to handle this failure.
1429}
1430
1431// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 15:15:111432TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111433 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1434 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071435 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111436 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001437 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1438 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111439 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051440 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1441 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
pthatcher94a2f212017-02-08 22:42:221442 // We wait at least for a full STUN timeout, which
Niels Möllera8e6f342019-09-24 07:05:191443 // cricket::STUN_TOTAL_TIMEOUT seconds.
pthatcher94a2f212017-02-08 22:42:221444 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
Niels Möllera8e6f342019-09-24 07:05:191445 cricket::STUN_TOTAL_TIMEOUT, fake_clock);
1446 // No additional (STUN) candidates.
1447 EXPECT_EQ(2U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111448}
1449
Taylor Brandstettera1c30352016-05-13 15:15:111450TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111451 AddInterface(kClientAddr);
1452 AddInterface(kClientAddr2);
1453 // Allocating only host UDP ports. This is done purely for testing
1454 // convenience.
Taylor Brandstetter417eebe2016-05-23 23:02:191455 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1456 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 18:55:071457 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111458 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001459 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1460 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111461 ASSERT_EQ(2U, candidates_.size());
1462 EXPECT_EQ(2U, ports_.size());
1463 // Candidates priorities should be different.
1464 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1465}
1466
1467// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 15:15:111468TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111469 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071470 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111471 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011472 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1473 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191474 EXPECT_EQ(3U, candidates_.size());
1475 EXPECT_EQ(3U, ports_.size());
Taylor Brandstettera1c30352016-05-13 15:15:111476 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:111477}
1478
Taylor Brandstetter417eebe2016-05-23 23:02:191479// Test that the allocator session uses the candidate filter it's created with,
1480// rather than the filter of its parent allocator.
1481// The filter of the allocator should only affect the next gathering phase,
1482// according to JSEP, which means the *next* allocator session returned.
1483TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1484 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071485 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191486 // Set candidate filter *after* creating the session. Should have no effect.
Qingsi Wangc129c352019-04-18 17:41:581487 allocator().SetCandidateFilter(CF_RELAY);
Taylor Brandstetter417eebe2016-05-23 23:02:191488 session_->StartGettingPorts();
1489 // 7 candidates and 4 ports is what we would normally get (see the
1490 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-16 00:40:011491 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1492 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191493 EXPECT_EQ(3U, candidates_.size());
1494 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 23:02:191495}
1496
henrike@webrtc.org269fb4b2014-10-28 22:20:111497// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1498// This test also verifies that when the allocator is only allowed to use
1499// relay (i.e. IceTransportsType is relay), the raddr is an empty
1500// address with the correct family. This is to prevent any local
1501// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 15:15:111502TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111503 AddInterface(kClientAddr);
1504 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 16:28:411505 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Qingsi Wangc129c352019-04-18 17:41:581506 allocator().SetCandidateFilter(CF_RELAY);
Qingsi Wange53ac042018-05-08 18:55:071507 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111508 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001509 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1510 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 11:40:051511 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1512 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:111513
1514 EXPECT_EQ(1U, candidates_.size());
1515 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311516 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1517 EXPECT_EQ(
1518 candidates_[0].related_address(),
1519 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:111520}
1521
Taylor Brandstettera1c30352016-05-13 15:15:111522TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111523 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191524 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wangc129c352019-04-18 17:41:581525 allocator().SetCandidateFilter(CF_HOST);
Qingsi Wange53ac042018-05-08 18:55:071526 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111527 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001528 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1529 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 15:15:111530 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1531 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311532 for (const Candidate& candidate : candidates_) {
1533 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:111534 }
1535}
1536
1537// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 15:15:111538TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111539 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 16:28:411540 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:111541
Taylor Brandstetter417eebe2016-05-23 23:02:191542 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wangc129c352019-04-18 17:41:581543 allocator().SetCandidateFilter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 18:55:071544 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111545 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001546 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1547 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111548 // Host is behind NAT, no private address will be exposed. Hence only UDP
1549 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 15:15:111550 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1551 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311552 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1553 EXPECT_EQ(
1554 candidates_[0].related_address(),
1555 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:111556}
1557
1558// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 15:15:111559TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111560 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191561 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wangc129c352019-04-18 17:41:581562 allocator().SetCandidateFilter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 18:55:071563 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111564 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001565 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1566 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111567 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 15:15:111568 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:111569 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311570 for (const Candidate& candidate : candidates_) {
1571 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
henrike@webrtc.org269fb4b2014-10-28 22:20:111572 }
1573}
1574
Peter Thatcher7cbd1882015-09-18 01:54:521575// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111576TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111577 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071578 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111579 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011580 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1581 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191582 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051583 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1584 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1585 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Niels Möllera8e6f342019-09-24 07:05:191586 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e42016-05-26 23:07:311587 for (const Candidate& candidate : candidates_) {
1588 EXPECT_EQ(kIceUfrag0, candidate.username());
1589 EXPECT_EQ(kIcePwd0, candidate.password());
1590 }
henrike@webrtc.org269fb4b2014-10-28 22:20:111591}
1592
henrike@webrtc.org269fb4b2014-10-28 22:20:111593// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1594// is allocated for udp and stun. Also verify there is only one candidate
1595// (local) if stun candidate is same as local candidate, which will be the case
1596// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 15:15:111597TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111598 AddInterface(kClientAddr);
1599 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191600 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 18:55:071601 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111602 session_->StartGettingPorts();
Niels Möllera8e6f342019-09-24 07:05:191603 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 21:18:001604 fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191605 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051606 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
pthatcher1749bc32017-02-08 21:18:001607 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1608 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111609}
1610
1611// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1612// is allocated for udp and stun. In this test we should expect both stun and
1613// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 15:15:111614TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111615 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 16:28:411616 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:111617
1618 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191619 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 18:55:071620 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111621 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001622 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1623 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111624 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051625 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1626 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1627 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:001628 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1629 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111630 EXPECT_EQ(3U, candidates_.size());
1631}
1632
deadbeefc5d0d952015-07-16 17:22:211633// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 15:15:111634TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 23:02:191635 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111636 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191637 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 23:57:451638 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:111639
1640 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1641
Taylor Brandstetter417eebe2016-05-23 23:02:191642 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:111643 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191644 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1645 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111646
Qingsi Wange53ac042018-05-08 18:55:071647 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111648 session_->StartGettingPorts();
1649
deadbeef7f1563f2017-09-16 00:40:011650 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1651 kDefaultAllocationTimeout, fake_clock);
1652 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111653 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051654 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1655 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1656 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1657 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1658 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:111659}
1660
Honghai Zhangf8998cf2019-10-14 18:27:501661// Test that if the turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN
1662// port will not be used if UDP TurnPort is used, given that TCP TURN port
1663// becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371664TEST_F(BasicPortAllocatorTest,
1665 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1666 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1667 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1668 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-07-01 03:52:021669
Honghai Zhangf8998cf2019-10-14 18:27:501670 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY,
1671 true /* tcp_pruned */);
Honghai Zhangb9e7b4a2016-07-01 03:52:021672}
1673
Honghai Zhangf8998cf2019-10-14 18:27:501674// Test that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN port
1675// will not be used if UDP TurnPort is used, given that UDP TURN port becomes
1676// ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371677TEST_F(BasicPortAllocatorTest,
1678 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1679 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1680 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1681 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-07-01 03:52:021682
Honghai Zhangf8998cf2019-10-14 18:27:501683 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY,
1684 true /* tcp_pruned */);
Honghai Zhangc67e0f52016-09-19 23:57:371685}
Honghai Zhangb9e7b4a2016-07-01 03:52:021686
Honghai Zhangf8998cf2019-10-14 18:27:501687// Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port
1688// will be kept regardless of the priority.
1689TEST_F(BasicPortAllocatorTest,
1690 TestUdpTurnPortPrunesTcpTurnPortIfUdpReadyFirst) {
1691 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1692 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1693 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
1694
1695 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY,
1696 true /* tcp_pruned */);
1697}
1698
1699// Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port
1700// will be kept regardless of the priority.
1701TEST_F(BasicPortAllocatorTest,
1702 TestTcpTurnPortPrunesUdpTurnPortIfTcpReadyFirst) {
1703 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1704 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1705 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
1706
1707 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY,
1708 false /* tcp_pruned */);
1709}
1710
1711// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4
1712// TurnPort will not be used if IPv6 TurnPort is used, given that IPv4 TURN port
1713// becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371714TEST_F(BasicPortAllocatorTest,
1715 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1716 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1717 // first.
1718 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1719 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-07-01 03:52:021720
Honghai Zhangc67e0f52016-09-19 23:57:371721 TestIPv6TurnPortPrunesIPv4TurnPort();
1722}
1723
Honghai Zhangf8998cf2019-10-14 18:27:501724// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4
1725// TurnPort will not be used if IPv6 TurnPort is used, given that IPv6 TURN port
1726// becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371727TEST_F(BasicPortAllocatorTest,
1728 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1729 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1730 // first.
1731 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1732 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1733
1734 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-07-01 03:52:021735}
1736
Honghai Zhangf8998cf2019-10-14 18:27:501737// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network
1738// interface will has its own set of TurnPorts based on their priorities, in the
1739// default case where no transit delay is set.
Honghai Zhangc67e0f52016-09-19 23:57:371740TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1741 TestEachInterfaceHasItsOwnTurnPorts();
1742}
Honghai Zhangb9e7b4a2016-07-01 03:52:021743
Honghai Zhangf8998cf2019-10-14 18:27:501744// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network
1745// interface will has its own set of TurnPorts based on their priorities, given
1746// that IPv4/TCP TURN port becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371747TEST_F(BasicPortAllocatorTest,
1748 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1749 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1750 // becomes ready last.
1751 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1752 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1753 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1754 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-07-01 03:52:021755
Honghai Zhangc67e0f52016-09-19 23:57:371756 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-07-01 03:52:021757}
1758
henrike@webrtc.org269fb4b2014-10-28 22:20:111759// Testing DNS resolve for the TURN server, this will test AllocationSequence
1760// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 21:18:001761// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1762// appears that it doesn't currently because of the DNS look up not
1763// using the fake clock.
1764TEST_F(BasicPortAllocatorTestWithRealClock,
1765 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-16 02:43:331766 // This test relies on a real query for "localhost", so it won't work on an
1767 // IPv6-only machine.
1768 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:111769 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 23:02:191770 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111771 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191772 allocator_.reset(new BasicPortAllocator(&network_manager_));
Qingsi Wanga2d60672018-04-11 23:57:451773 allocator_->Initialize();
Niels Möller191e38f2019-11-04 07:49:121774 RelayServerConfig turn_server;
Taylor Brandstetter417eebe2016-05-23 23:02:191775 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 20:55:101776 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 23:02:191777 turn_server.ports.push_back(
hnsl277b2502016-12-13 13:17:231778 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
deadbeef653b8e02015-11-11 20:55:101779 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:111780
Taylor Brandstetter417eebe2016-05-23 23:02:191781 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:111782 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191783 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1784 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111785
Qingsi Wange53ac042018-05-08 18:55:071786 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111787 session_->StartGettingPorts();
1788
1789 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1790}
1791
1792// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1793// is allocated for udp/stun/turn. In this test we should expect all local,
1794// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111795TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111796 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 16:28:411797 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:111798
1799 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1800
1801 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191802 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1803 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111804
Qingsi Wange53ac042018-05-08 18:55:071805 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111806 session_->StartGettingPorts();
1807
deadbeef7f1563f2017-09-16 00:40:011808 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1809 kDefaultAllocationTimeout, fake_clock);
1810 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111811 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051812 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1813 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1814 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1815 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1816 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:001817 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1818 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111819 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-16 00:40:011820 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1821 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:111822 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1823 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1824}
1825
1826// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1827// server is also used as the STUN server, we should get 'local', 'stun', and
1828// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111829TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111830 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 18:47:211831 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 16:28:411832 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:111833 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1834
1835 // Must set the step delay to 0 to make sure the relay allocation phase is
1836 // started before the STUN candidates are obtained, so that the STUN binding
1837 // response is processed when both StunPort and TurnPort exist to reproduce
1838 // webrtc issue 3537.
1839 allocator_->set_step_delay(0);
1840 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191841 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1842 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111843
Qingsi Wange53ac042018-05-08 18:55:071844 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111845 session_->StartGettingPorts();
1846
deadbeef7f1563f2017-09-16 00:40:011847 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1848 kDefaultAllocationTimeout, fake_clock);
1849 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051850 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311851 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051852 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1853 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1854 &stun_candidate));
1855 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1856 candidates_, "relay", "udp",
1857 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1858 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:111859
henrike@webrtc.org269fb4b2014-10-28 22:20:111860 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-16 00:40:011861 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1862 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:111863 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1864 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1865}
1866
deadbeefc5d0d952015-07-16 17:22:211867// Test that when only a TCP TURN server is available, we do NOT use it as
1868// a UDP STUN server, as this could leak our IP address. Thus we should only
1869// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 15:15:111870TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 23:02:191871 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 17:22:211872 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 16:28:411873 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 17:22:211874 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1875
1876 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191877 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1878 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 17:22:211879
Qingsi Wange53ac042018-05-08 18:55:071880 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 17:22:211881 session_->StartGettingPorts();
1882
deadbeef7f1563f2017-09-16 00:40:011883 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1884 kDefaultAllocationTimeout, fake_clock);
1885 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 17:22:211886 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051887 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1888 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1889 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 17:22:211890 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1891 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1892}
1893
1894// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1895// TURN server is used as the STUN server and we get 'local', 'stun', and
1896// 'relay' candidates.
1897// TODO(deadbeef): Remove this test when support for non-shared socket mode
1898// is removed.
Taylor Brandstettera1c30352016-05-13 15:15:111899TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 17:22:211900 AddInterface(kClientAddr);
1901 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 16:28:411902 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 17:22:211903 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1904
Taylor Brandstetter417eebe2016-05-23 23:02:191905 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 17:22:211906
Qingsi Wange53ac042018-05-08 18:55:071907 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 17:22:211908 session_->StartGettingPorts();
1909
deadbeef7f1563f2017-09-16 00:40:011910 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1911 kDefaultAllocationTimeout, fake_clock);
1912 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 17:22:211913 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051914 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311915 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051916 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1917 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1918 &stun_candidate));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311919 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051920 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1921 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1922 &turn_candidate));
deadbeefc5d0d952015-07-16 17:22:211923 // Not using shared socket, so the STUN request's server reflexive address
1924 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311925 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 17:22:211926
deadbeefc5d0d952015-07-16 17:22:211927 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1928 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1929 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1930}
1931
1932// Test that even when both a STUN and TURN server are configured, the TURN
1933// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111934TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 17:22:211935 AddInterface(kClientAddr);
1936 // Configure with STUN server but destroy it, so we can ensure that it's
1937 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 16:28:411938 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 17:22:211939 stun_server_.reset();
1940 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1941
1942 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191943 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1944 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 17:22:211945
Qingsi Wange53ac042018-05-08 18:55:071946 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 17:22:211947 session_->StartGettingPorts();
1948
pthatcher1749bc32017-02-08 21:18:001949 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1950 fake_clock);
Jonas Olssonabbe8412018-04-03 11:40:051951 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311952 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051953 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1954 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1955 &stun_candidate));
1956 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1957 candidates_, "relay", "udp",
1958 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1959 stun_candidate.address()));
deadbeefc5d0d952015-07-16 17:22:211960
1961 // Don't bother waiting for STUN timeout, since we already verified
1962 // that we got a STUN candidate from the TURN server.
1963}
1964
henrike@webrtc.org269fb4b2014-10-28 22:20:111965// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1966// and fail to generate STUN candidate, local UDP candidate is generated
1967// properly.
Taylor Brandstettera1c30352016-05-13 15:15:111968TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 23:02:191969 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1970 PORTALLOCATOR_DISABLE_TCP |
1971 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:111972 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1973 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071974 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111975 session_->StartGettingPorts();
Mirko Bonadei5f4d47b2018-08-22 17:41:221976 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 21:18:001977 fake_clock);
Mirko Bonadei5f4d47b2018-08-22 17:41:221978 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051979 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 19:49:301980 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 21:18:001981 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
1982 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111983 EXPECT_EQ(1U, candidates_.size());
1984}
1985
Guo-wei Shieh47872ec2015-08-19 17:32:461986// Test that when the NetworkManager doesn't have permission to enumerate
1987// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1988// automatically.
Taylor Brandstettera1c30352016-05-13 15:15:111989TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 22:47:391990 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1991 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 17:32:461992 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 16:06:281993 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 23:02:191994 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1995 PORTALLOCATOR_DISABLE_TCP |
1996 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1997 EXPECT_EQ(0U,
1998 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 18:55:071999 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:192000 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 17:32:462001 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:002002 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2003 fake_clock);
Guo-wei Shieh9af97f82015-11-10 22:47:392004 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:052005 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 23:02:192006 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 17:32:462007}
2008
henrike@webrtc.org269fb4b2014-10-28 22:20:112009// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 15:15:112010TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 23:02:192011 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2012 PORTALLOCATOR_ENABLE_IPV6 |
2013 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:112014 AddInterface(kClientIPv6Addr);
2015 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:192016 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 18:55:072017 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:112018 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:012019 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 21:18:002020 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-16 00:40:012021 EXPECT_EQ(4U, ports_.size());
2022 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:052023 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2024 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2025 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2026 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:112027}
honghaiz98db68f2015-09-29 14:58:172028
Taylor Brandstettera1c30352016-05-13 15:15:112029TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 14:58:172030 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:192031 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 18:55:072032 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 14:58:172033 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:002034 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172035 EXPECT_EQ(2U, ports_.size());
2036 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 21:18:002037 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172038
2039 // After stopping getting ports, adding a new interface will not start
2040 // getting ports again.
deadbeefb60a8192016-08-24 22:15:002041 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 14:58:172042 candidates_.clear();
2043 ports_.clear();
2044 candidate_allocation_done_ = false;
2045 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 21:18:002046 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172047 EXPECT_EQ(0U, candidates_.size());
2048 EXPECT_EQ(0U, ports_.size());
2049}
2050
Taylor Brandstettera1c30352016-05-13 15:15:112051TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 14:58:172052 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:192053 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 18:55:072054 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 14:58:172055 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:002056 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172057 EXPECT_EQ(2U, ports_.size());
2058 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 21:18:002059 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172060
2061 // After clearing getting ports, adding a new interface will start getting
2062 // ports again.
deadbeefb60a8192016-08-24 22:15:002063 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 14:58:172064 candidates_.clear();
2065 ports_.clear();
2066 candidate_allocation_done_ = false;
2067 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 21:18:002068 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172069 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 21:18:002070 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2071 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 14:58:172072}
Taylor Brandstettera1c30352016-05-13 15:15:112073
2074// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2075// a pooled session is taken out of the pool.
2076TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2077 AddInterface(kClientAddr);
2078 int pool_size = 1;
2079 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:502080 allocator_->turn_servers(), pool_size,
2081 webrtc::NO_PRUNE);
Taylor Brandstetter417eebe2016-05-23 23:02:192082 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 15:15:112083 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 21:18:002084 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2085 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 15:15:112086 // Expect that when TakePooledSession is called,
2087 // UpdateTransportInformationInternal will be called and the
2088 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2089 // candidates.
2090 session_ =
2091 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2092 ASSERT_NE(nullptr, session_.get());
2093 auto ready_ports = session_->ReadyPorts();
2094 auto candidates = session_->ReadyCandidates();
2095 EXPECT_FALSE(ready_ports.empty());
2096 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 23:02:192097 for (const PortInterface* port_interface : ready_ports) {
2098 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 15:15:112099 EXPECT_EQ(kContentName, port->content_name());
2100 EXPECT_EQ(1, port->component());
2101 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2102 EXPECT_EQ(kIcePwd0, port->password());
2103 }
Taylor Brandstetter417eebe2016-05-23 23:02:192104 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 15:15:112105 EXPECT_EQ(1, candidate.component());
2106 EXPECT_EQ(kIceUfrag0, candidate.username());
2107 EXPECT_EQ(kIcePwd0, candidate.password());
2108 }
2109}
Taylor Brandstetter417eebe2016-05-23 23:02:192110
2111// Test that a new candidate filter takes effect even on already-gathered
2112// candidates.
2113TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2114 AddInterface(kClientAddr);
2115 int pool_size = 1;
2116 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:502117 allocator_->turn_servers(), pool_size,
2118 webrtc::NO_PRUNE);
Taylor Brandstetter417eebe2016-05-23 23:02:192119 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2120 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 21:18:002121 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2122 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 23:02:192123 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2124 size_t initial_ports_size = peeked_session->ReadyPorts().size();
Qingsi Wangc129c352019-04-18 17:41:582125 allocator_->SetCandidateFilter(CF_RELAY);
Taylor Brandstetter417eebe2016-05-23 23:02:192126 // Assume that when TakePooledSession is called, the candidate filter will be
2127 // applied to the pooled session. This is tested by PortAllocatorTest.
2128 session_ =
2129 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2130 ASSERT_NE(nullptr, session_.get());
2131 auto candidates = session_->ReadyCandidates();
2132 auto ports = session_->ReadyPorts();
2133 // Sanity check that the number of candidates and ports decreased.
2134 EXPECT_GT(initial_candidates_size, candidates.size());
2135 EXPECT_GT(initial_ports_size, ports.size());
2136 for (const PortInterface* port : ports) {
2137 // Expect only relay ports.
2138 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2139 }
2140 for (const Candidate& candidate : candidates) {
2141 // Expect only relay candidates now that the filter is applied.
2142 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2143 // Expect that the raddr is emptied due to the CF_RELAY filter.
2144 EXPECT_EQ(candidate.related_address(),
2145 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2146 }
2147}
2148
Qingsi Wangc129c352019-04-18 17:41:582149// Test that candidates that do not match a previous candidate filter can be
2150// surfaced if they match the new one after setting the filter value.
2151TEST_F(BasicPortAllocatorTest,
2152 SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes) {
2153 // We would still surface a host candidate if the IP is public, even though it
2154 // is disabled by the candidate filter. See
2155 // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so
2156 // that the srflx candidate is not equivalent to the host candidate.
2157 AddInterface(kPrivateAddr);
2158 ResetWithStunServerAndNat(kStunAddr);
2159
2160 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2161
2162 allocator_->set_flags(allocator().flags() |
2163 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2164 PORTALLOCATOR_DISABLE_TCP);
2165
2166 allocator_->SetCandidateFilter(CF_NONE);
2167 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2168 session_->StartGettingPorts();
2169 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2170 kDefaultAllocationTimeout, fake_clock);
2171 EXPECT_TRUE(candidates_.empty());
2172 EXPECT_TRUE(ports_.empty());
2173
2174 // Surface the relay candidate previously gathered but not signaled.
2175 session_->SetCandidateFilter(CF_RELAY);
2176 ASSERT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout,
2177 fake_clock);
2178 EXPECT_EQ(RELAY_PORT_TYPE, candidates_.back().type());
2179 EXPECT_EQ(1u, ports_.size());
2180
2181 // Surface the srflx candidate previously gathered but not signaled.
2182 session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE);
2183 ASSERT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout,
2184 fake_clock);
2185 EXPECT_EQ(STUN_PORT_TYPE, candidates_.back().type());
2186 EXPECT_EQ(2u, ports_.size());
2187
2188 // Surface the srflx candidate previously gathered but not signaled.
2189 session_->SetCandidateFilter(CF_ALL);
2190 ASSERT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout,
2191 fake_clock);
2192 EXPECT_EQ(LOCAL_PORT_TYPE, candidates_.back().type());
2193 EXPECT_EQ(2u, ports_.size());
2194}
2195
2196// This is a similar test as
2197// SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes, and we
2198// test the transitions for which the new filter value is not a super set of the
2199// previous value.
2200TEST_F(
2201 BasicPortAllocatorTest,
2202 SurfaceNewCandidatesAfterSetCandidateFilterToAllowDifferentCandidateTypes) {
2203 // We would still surface a host candidate if the IP is public, even though it
2204 // is disabled by the candidate filter. See
2205 // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so
2206 // that the srflx candidate is not equivalent to the host candidate.
2207 AddInterface(kPrivateAddr);
2208 ResetWithStunServerAndNat(kStunAddr);
2209
2210 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2211
2212 allocator_->set_flags(allocator().flags() |
2213 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2214 PORTALLOCATOR_DISABLE_TCP);
2215
2216 allocator_->SetCandidateFilter(CF_NONE);
2217 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2218 session_->StartGettingPorts();
2219 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2220 kDefaultAllocationTimeout, fake_clock);
2221 EXPECT_TRUE(candidates_.empty());
2222 EXPECT_TRUE(ports_.empty());
2223
2224 // Surface the relay candidate previously gathered but not signaled.
2225 session_->SetCandidateFilter(CF_RELAY);
2226 EXPECT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout,
2227 fake_clock);
2228 EXPECT_EQ(RELAY_PORT_TYPE, candidates_.back().type());
2229 EXPECT_EQ(1u, ports_.size());
2230
2231 // Surface the srflx candidate previously gathered but not signaled.
2232 session_->SetCandidateFilter(CF_REFLEXIVE);
2233 EXPECT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout,
2234 fake_clock);
2235 EXPECT_EQ(STUN_PORT_TYPE, candidates_.back().type());
2236 EXPECT_EQ(2u, ports_.size());
2237
2238 // Surface the host candidate previously gathered but not signaled.
2239 session_->SetCandidateFilter(CF_HOST);
2240 EXPECT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout,
2241 fake_clock);
2242 EXPECT_EQ(LOCAL_PORT_TYPE, candidates_.back().type());
2243 // We use a shared socket and cricket::UDPPort handles the srflx candidate.
2244 EXPECT_EQ(2u, ports_.size());
2245}
2246
2247// Test that after an allocation session has stopped getting ports, changing the
2248// candidate filter to allow new types of gathered candidates does not surface
2249// any candidate.
2250TEST_F(BasicPortAllocatorTest,
2251 NoCandidateSurfacedWhenUpdatingCandidateFilterIfSessionStopped) {
2252 AddInterface(kPrivateAddr);
2253 ResetWithStunServerAndNat(kStunAddr);
2254
2255 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2256
2257 allocator_->set_flags(allocator().flags() |
2258 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2259 PORTALLOCATOR_DISABLE_TCP);
2260
2261 allocator_->SetCandidateFilter(CF_NONE);
2262 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2263 session_->StartGettingPorts();
2264 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2265 kDefaultAllocationTimeout, fake_clock);
2266 auto test_invariants = [this]() {
2267 EXPECT_TRUE(candidates_.empty());
2268 EXPECT_TRUE(ports_.empty());
2269 };
2270
2271 test_invariants();
2272
2273 session_->StopGettingPorts();
2274
2275 session_->SetCandidateFilter(CF_RELAY);
2276 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2277 test_invariants();
2278
2279 session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE);
2280 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2281 test_invariants();
2282
2283 session_->SetCandidateFilter(CF_ALL);
2284 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2285 test_invariants();
2286}
2287
Qingsi Wang4ff54432018-03-02 02:25:202288TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2289 const int pool_size = 1;
2290 const int expected_stun_keepalive_interval = 123;
2291 AddInterface(kClientAddr);
Honghai Zhangf8998cf2019-10-14 18:27:502292 allocator_->SetConfiguration(
2293 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2294 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wang4ff54432018-03-02 02:25:202295 auto* pooled_session = allocator_->GetPooledSession();
2296 ASSERT_NE(nullptr, pooled_session);
2297 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2298 kDefaultAllocationTimeout, fake_clock);
2299 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2300 expected_stun_keepalive_interval);
2301}
2302
2303TEST_F(BasicPortAllocatorTest,
2304 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2305 const int pool_size = 1;
2306 AddInterface(kClientAddr);
Honghai Zhangf8998cf2019-10-14 18:27:502307 allocator_->SetConfiguration(
2308 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2309 webrtc::NO_PRUNE, nullptr, 123 /* stun keepalive interval */);
Qingsi Wang4ff54432018-03-02 02:25:202310 auto* pooled_session = allocator_->GetPooledSession();
2311 ASSERT_NE(nullptr, pooled_session);
2312 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2313 kDefaultAllocationTimeout, fake_clock);
2314 const int expected_stun_keepalive_interval = 321;
Honghai Zhangf8998cf2019-10-14 18:27:502315 allocator_->SetConfiguration(
2316 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2317 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wang4ff54432018-03-02 02:25:202318 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2319 expected_stun_keepalive_interval);
2320}
2321
2322TEST_F(BasicPortAllocatorTest,
2323 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2324 const int pool_size = 1;
2325 const int expected_stun_keepalive_interval = 123;
2326 AddInterface(kClientAddr);
2327 allocator_->set_flags(allocator().flags() |
2328 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Honghai Zhangf8998cf2019-10-14 18:27:502329 allocator_->SetConfiguration(
2330 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2331 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 18:55:072332 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-02 02:25:202333 session_->StartGettingPorts();
2334 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2335 kDefaultAllocationTimeout, fake_clock);
2336 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2337 expected_stun_keepalive_interval);
2338}
2339
2340TEST_F(BasicPortAllocatorTest,
2341 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2342 const int pool_size = 1;
2343 const int expected_stun_keepalive_interval = 123;
2344 AddInterface(kClientAddr);
2345 allocator_->set_flags(allocator().flags() &
2346 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
Honghai Zhangf8998cf2019-10-14 18:27:502347 allocator_->SetConfiguration(
2348 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2349 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 18:55:072350 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-02 02:25:202351 session_->StartGettingPorts();
2352 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2353 kDefaultAllocationTimeout, fake_clock);
2354 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2355 expected_stun_keepalive_interval);
2356}
2357
Qingsi Wang7fc821d2018-07-12 19:54:532358TEST_F(BasicPortAllocatorTest, IceRegatheringMetricsLoggedWhenNetworkChanges) {
2359 // Only test local ports to simplify test.
2360 ResetWithNoServersOrNat();
2361 AddInterface(kClientAddr, "test_net0");
2362 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2363 session_->StartGettingPorts();
2364 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2365 kDefaultAllocationTimeout, fake_clock);
2366 candidate_allocation_done_ = false;
2367 AddInterface(kClientAddr2, "test_net1");
2368 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2369 kDefaultAllocationTimeout, fake_clock);
Ying Wangef3998f2019-12-09 12:06:532370 EXPECT_METRIC_EQ(1,
2371 webrtc::metrics::NumEvents(
2372 "WebRTC.PeerConnection.IceRegatheringReason",
2373 static_cast<int>(IceRegatheringReason::NETWORK_CHANGE)));
Qingsi Wang7fc821d2018-07-12 19:54:532374}
2375
Qingsi Wang09619332018-09-13 05:51:552376// Test that when an mDNS responder is present, the local address of a host
Jeroen de Borst72d2ddd2018-11-27 21:20:392377// candidate is concealed by an mDNS hostname and the related address of a srflx
2378// candidate is set to 0.0.0.0 or ::0.
Qingsi Wang09619332018-09-13 05:51:552379TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) {
2380 // Default config uses GTURN and no NAT, so replace that with the
2381 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
2382 ResetWithStunServerAndNat(kStunAddr);
2383 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
2384 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2385 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
2386
2387 ASSERT_EQ(&network_manager_, allocator().network_manager());
Qingsi Wangecd30542019-05-22 21:34:562388 network_manager_.set_mdns_responder(
Mirko Bonadei317a1f02019-09-17 15:06:182389 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
Qingsi Wang09619332018-09-13 05:51:552390 AddInterface(kClientAddr);
2391 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2392 session_->StartGettingPorts();
2393 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2394 kDefaultAllocationTimeout, fake_clock);
2395 EXPECT_EQ(5u, candidates_.size());
2396 int num_host_udp_candidates = 0;
2397 int num_host_tcp_candidates = 0;
2398 int num_srflx_candidates = 0;
2399 int num_relay_candidates = 0;
2400 for (const auto& candidate : candidates_) {
Jeroen de Borst72d2ddd2018-11-27 21:20:392401 const auto& raddr = candidate.related_address();
2402
Qingsi Wang09619332018-09-13 05:51:552403 if (candidate.type() == LOCAL_PORT_TYPE) {
Jeroen de Borst72d2ddd2018-11-27 21:20:392404 EXPECT_FALSE(candidate.address().hostname().empty());
2405 EXPECT_TRUE(raddr.IsNil());
Qingsi Wang09619332018-09-13 05:51:552406 if (candidate.protocol() == UDP_PROTOCOL_NAME) {
2407 ++num_host_udp_candidates;
2408 } else {
2409 ++num_host_tcp_candidates;
2410 }
Jeroen de Borst72d2ddd2018-11-27 21:20:392411 } else if (candidate.type() == STUN_PORT_TYPE) {
2412 // For a srflx candidate, the related address should be set to 0.0.0.0 or
2413 // ::0
2414 EXPECT_TRUE(IPIsAny(raddr.ipaddr()));
2415 EXPECT_EQ(raddr.port(), 0);
2416 ++num_srflx_candidates;
2417 } else if (candidate.type() == RELAY_PORT_TYPE) {
2418 EXPECT_EQ(kNatUdpAddr.ipaddr(), raddr.ipaddr());
2419 EXPECT_EQ(kNatUdpAddr.family(), raddr.family());
2420 ++num_relay_candidates;
Qingsi Wang09619332018-09-13 05:51:552421 } else {
Jeroen de Borst72d2ddd2018-11-27 21:20:392422 // prflx candidates are not expected
2423 FAIL();
Qingsi Wang09619332018-09-13 05:51:552424 }
2425 }
2426 EXPECT_EQ(1, num_host_udp_candidates);
2427 EXPECT_EQ(1, num_host_tcp_candidates);
2428 EXPECT_EQ(1, num_srflx_candidates);
2429 EXPECT_EQ(2, num_relay_candidates);
2430}
2431
Honghai Zhang6981fb52019-10-29 19:45:342432TEST_F(BasicPortAllocatorTest, TestUseTurnServerAsStunSever) {
2433 ServerAddresses stun_servers;
2434 stun_servers.insert(kStunAddr);
2435 PortConfiguration port_config(stun_servers, "", "");
2436 RelayServerConfig turn_servers =
2437 CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2438 port_config.AddRelay(turn_servers);
2439
2440 EXPECT_EQ(2U, port_config.StunServers().size());
2441}
2442
2443TEST_F(BasicPortAllocatorTest, TestDoNotUseTurnServerAsStunSever) {
2444 webrtc::test::ScopedFieldTrials field_trials(
2445 "WebRTC-UseTurnServerAsStunServer/Disabled/");
2446 ServerAddresses stun_servers;
2447 stun_servers.insert(kStunAddr);
2448 PortConfiguration port_config(stun_servers, "" /* user_name */,
2449 "" /* password */);
2450 RelayServerConfig turn_servers =
2451 CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2452 port_config.AddRelay(turn_servers);
2453
2454 EXPECT_EQ(1U, port_config.StunServers().size());
2455}
2456
Taylor Brandstetter417eebe2016-05-23 23:02:192457} // namespace cricket