blob: f77040d128510c9b421ec3ea6b0a55dd25bae636 [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"
Ali Tofighde2ac5a2022-06-30 09:58:2617#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 17:11:0018#include "p2p/base/basic_packet_socket_factory.h"
19#include "p2p/base/p2p_constants.h"
20#include "p2p/base/stun_port.h"
21#include "p2p/base/stun_request.h"
22#include "p2p/base/stun_server.h"
Steve Anton10542f22019-01-11 17:11:0023#include "p2p/base/test_stun_server.h"
24#include "p2p/base/test_turn_server.h"
Steve Anton10542f22019-01-11 17:11:0025#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 21:34:5626#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 17:11:0027#include "rtc_base/fake_network.h"
28#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3129#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0030#include "rtc_base/ip_address.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3131#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0032#include "rtc_base/nat_server.h"
33#include "rtc_base/nat_socket_factory.h"
34#include "rtc_base/nat_types.h"
35#include "rtc_base/net_helper.h"
36#include "rtc_base/net_helpers.h"
Mirko Bonadei0d8b79e2023-07-27 06:55:4937#include "rtc_base/net_test_helpers.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3138#include "rtc_base/network.h"
Yves Gerey3e707812018-11-28 15:47:4939#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 17:11:0040#include "rtc_base/network_monitor.h"
Yves Gerey3e707812018-11-28 15:47:4941#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 17:11:0042#include "rtc_base/socket_address.h"
43#include "rtc_base/socket_address_pair.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3144#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 17:11:0045#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 06:51:1046#include "system_wrappers/include/metrics.h"
Steve Antonae226f62019-01-29 20:47:3847#include "test/gmock.h"
Yves Gerey3e707812018-11-28 15:47:4948#include "test/gtest.h"
Jonas Oreland340cb5e2022-03-17 08:14:1649#include "test/scoped_key_value_config.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1150
Taylor Brandstetter0c7e9f52015-12-29 22:14:5251using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:1152using rtc::SocketAddress;
Steve Antonae226f62019-01-29 20:47:3853using ::testing::Contains;
54using ::testing::Not;
henrike@webrtc.org269fb4b2014-10-28 22:20:1155
Mirko Bonadei675513b2017-11-09 10:09:2556#define MAYBE_SKIP_IPV4 \
57 if (!rtc::HasIPv4Enabled()) { \
58 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
59 return; \
deadbeef9a6f4d42017-05-16 02:43:3360 }
61
deadbeefe97389c2016-12-23 09:43:4562static const SocketAddress kAnyAddr("0.0.0.0", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:1163static const SocketAddress kClientAddr("11.11.11.11", 0);
Honghai Zhangb9e7b4a2016-07-01 03:52:0264static const SocketAddress kClientAddr2("22.22.22.22", 0);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:2065static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:1166static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:0167static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 15:15:1168static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
69 0);
Honghai Zhangb9e7b4a2016-07-01 03:52:0270static const SocketAddress kClientIPv6Addr2(
71 "2401:fa00:4:2000:be30:5bff:fee5:c3",
72 0);
deadbeef3427f532017-07-26 23:09:3373static const SocketAddress kClientIPv6Addr3(
74 "2401:fa00:4:3000:be30:5bff:fee5:c3",
75 0);
Diep Bui9068f4562022-08-26 15:43:4476static const SocketAddress kClientIPv6Addr4(
77 "2401:fa00:4:4000:be30:5bff:fee5:c3",
78 0);
79static const SocketAddress kClientIPv6Addr5(
80 "2401:fa00:4:5000:be30:5bff:fee5:c3",
81 0);
deadbeefc5d0d952015-07-16 17:22:2182static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
83static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:1184static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
85static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:1186static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
Honghai Zhangb9e7b4a2016-07-01 03:52:0287static const SocketAddress kTurnUdpIntIPv6Addr(
88 "2402:fb00:4:1000:be30:5bff:fee5:c3",
89 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:1190static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
Honghai Zhangb9e7b4a2016-07-01 03:52:0291static const SocketAddress kTurnTcpIntIPv6Addr(
92 "2402:fb00:4:2000:be30:5bff:fee5:c3",
93 3479);
henrike@webrtc.org269fb4b2014-10-28 22:20:1194static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
95
96// Minimum and maximum port for port range tests.
97static const int kMinPort = 10000;
98static const int kMaxPort = 10099;
99
100// Based on ICE_UFRAG_LENGTH
zhihuang6d0d4bf2016-05-24 17:13:32101static const char kIceUfrag0[] = "UF00";
henrike@webrtc.org269fb4b2014-10-28 22:20:11102// Based on ICE_PWD_LENGTH
103static const char kIcePwd0[] = "TESTICEPWD00000000000000";
104
105static const char kContentName[] = "test content";
106
Honghai Zhangc67e0f52016-09-19 23:57:37107static const int kDefaultAllocationTimeout = 3000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11108static const char kTurnUsername[] = "test";
109static const char kTurnPassword[] = "test";
110
pthatcher94a2f212017-02-08 22:42:22111// STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
Taylor Brandstetter8fcf4142016-05-23 19:49:30112// Add some margin of error for slow bots.
pthatcher94a2f212017-02-08 22:42:22113static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
Taylor Brandstetter8fcf4142016-05-23 19:49:30114
Qingsi Wang4ff54432018-03-02 02:25:20115namespace {
116
117void CheckStunKeepaliveIntervalOfAllReadyPorts(
118 const cricket::PortAllocatorSession* allocator_session,
119 int expected) {
120 auto ready_ports = allocator_session->ReadyPorts();
121 for (const auto* port : ready_ports) {
122 if (port->Type() == cricket::STUN_PORT_TYPE ||
123 (port->Type() == cricket::LOCAL_PORT_TYPE &&
124 port->GetProtocol() == cricket::PROTO_UDP)) {
125 EXPECT_EQ(
126 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
127 expected);
128 }
129 }
130}
131
132} // namespace
133
henrike@webrtc.org269fb4b2014-10-28 22:20:11134namespace cricket {
135
136// Helper for dumping candidates
Taylor Brandstetter8c9be5e42016-05-26 23:07:31137std::ostream& operator<<(std::ostream& os,
138 const std::vector<Candidate>& candidates) {
139 os << '[';
140 bool first = true;
141 for (const Candidate& c : candidates) {
142 if (!first) {
143 os << ", ";
144 }
145 os << c.ToString();
146 first = false;
Steve Anton6c38cc72017-11-29 18:25:58147 }
Taylor Brandstetter8c9be5e42016-05-26 23:07:31148 os << ']';
henrike@webrtc.org269fb4b2014-10-28 22:20:11149 return os;
150}
151
Mirko Bonadei6a489f22019-04-09 13:11:12152class BasicPortAllocatorTestBase : public ::testing::Test,
pthatcher1749bc32017-02-08 21:18:00153 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11154 public:
pthatcher1749bc32017-02-08 21:18:00155 BasicPortAllocatorTestBase()
deadbeef98e186c2017-05-17 01:00:06156 : vss_(new rtc::VirtualSocketServer()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11157 fss_(new rtc::FirewallSocketServer(vss_.get())),
Tommife57cec2023-08-30 08:07:06158 socket_factory_(fss_.get()),
nisse7eaa4ea2017-05-08 12:25:41159 thread_(fss_.get()),
deadbeefe97389c2016-12-23 09:43:45160 // Note that the NAT is not used by default. ResetWithStunServerAndNat
161 // must be called.
deadbeefc5d0d952015-07-16 17:22:21162 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-14 05:24:02163 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Per K14630a72023-11-22 13:08:58164 stun_server_(TestStunServer::Create(fss_.get(), kStunAddr, thread_)),
Niels Möller6dd49972021-11-24 13:05:55165 turn_server_(rtc::Thread::Current(),
166 fss_.get(),
167 kTurnUdpIntAddr,
168 kTurnUdpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11169 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 23:02:19170 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11171 stun_servers.insert(kStunAddr);
Niels Möllera8e6f342019-09-24 07:05:19172
Byoungchan Leed197e0b2022-05-30 14:59:55173 allocator_ = std::make_unique<BasicPortAllocator>(
Tommife57cec2023-08-30 08:07:06174 &network_manager_, &socket_factory_, stun_servers, &field_trials_);
Qingsi Wanga2d60672018-04-11 23:57:45175 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 23:02:19176 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wang7fc821d2018-07-12 19:54:53177 webrtc::metrics::Reset();
henrike@webrtc.org269fb4b2014-10-28 22:20:11178 }
179
180 void AddInterface(const SocketAddress& addr) {
181 network_manager_.AddInterface(addr);
182 }
Ali Tofighde2ac5a2022-06-30 09:58:26183 void AddInterface(const SocketAddress& addr, absl::string_view if_name) {
honghaiz8c404fa2015-09-28 14:59:43184 network_manager_.AddInterface(addr, if_name);
185 }
Taylor Brandstetter0c7e9f52015-12-29 22:14:52186 void AddInterface(const SocketAddress& addr,
Ali Tofighde2ac5a2022-06-30 09:58:26187 absl::string_view if_name,
Taylor Brandstetter0c7e9f52015-12-29 22:14:52188 rtc::AdapterType type) {
189 network_manager_.AddInterface(addr, if_name, type);
190 }
Niels Möller84d15952021-09-01 08:50:34191 // The default source address is the public address that STUN server will
192 // observe when the endpoint is sitting on the public internet and the local
193 // port is bound to the "any" address. Intended for simulating the situation
194 // that client binds the "any" address, and that's also the address returned
195 // by getsockname/GetLocalAddress, so that the client can learn the actual
196 // local address only from the STUN response.
197 void AddInterfaceAsDefaultSourceAddresss(const SocketAddress& addr) {
Guo-wei Shieh38f88932015-08-14 05:24:02198 AddInterface(addr);
Artem Titov2dbb4c92021-07-26 13:12:41199 // When a binding comes from the any address, the `addr` will be used as the
Guo-wei Shieh38f88932015-08-14 05:24:02200 // srflx address.
Niels Möller84d15952021-09-01 08:50:34201 vss_->SetDefaultSourceAddress(addr.ipaddr());
Guo-wei Shieh38f88932015-08-14 05:24:02202 }
honghaiz8c404fa2015-09-28 14:59:43203 void RemoveInterface(const SocketAddress& addr) {
204 network_manager_.RemoveInterface(addr);
205 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11206 bool SetPortRange(int min_port, int max_port) {
207 return allocator_->SetPortRange(min_port, max_port);
208 }
Guo-wei Shieh11477022015-08-15 16:28:41209 // Endpoint is on the public network. No STUN or TURN.
210 void ResetWithNoServersOrNat() {
Tommi3756e292023-09-01 10:45:06211 allocator_.reset(
212 new BasicPortAllocator(&network_manager_, &socket_factory_));
Qingsi Wanga2d60672018-04-11 23:57:45213 allocator_->Initialize();
Taylor Brandstetter417eebe2016-05-23 23:02:19214 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-14 05:24:02215 }
Guo-wei Shieh11477022015-08-15 16:28:41216 // Endpoint is behind a NAT, with STUN specified.
217 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
218 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-14 05:24:02219 }
Guo-wei Shieh11477022015-08-15 16:28:41220 // Endpoint is on the public network, with STUN specified.
221 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
222 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11223 }
Guo-wei Shieh11477022015-08-15 16:28:41224 // Endpoint is on the public network, with TURN specified.
225 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
226 const rtc::SocketAddress& tcp_turn) {
227 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11228 AddTurnServers(udp_turn, tcp_turn);
229 }
230
Honghai Zhang6981fb52019-10-29 19:45:34231 RelayServerConfig CreateTurnServers(const rtc::SocketAddress& udp_turn,
232 const rtc::SocketAddress& tcp_turn) {
Niels Möller191e38f2019-11-04 07:49:12233 RelayServerConfig turn_server;
Taylor Brandstetter417eebe2016-05-23 23:02:19234 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 20:55:10235 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11236
237 if (!udp_turn.IsNil()) {
hnsl277b2502016-12-13 13:17:23238 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11239 }
240 if (!tcp_turn.IsNil()) {
hnsl277b2502016-12-13 13:17:23241 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11242 }
Honghai Zhang6981fb52019-10-29 19:45:34243 return turn_server;
244 }
245
246 void AddTurnServers(const rtc::SocketAddress& udp_turn,
247 const rtc::SocketAddress& tcp_turn) {
248 RelayServerConfig turn_server = CreateTurnServers(udp_turn, tcp_turn);
Jonas Oreland3e7e15d2022-11-03 09:02:55249 allocator_->AddTurnServerForTesting(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11250 }
251
252 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 15:15:11253 session_ = CreateSession("session", component);
254 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11255 return false;
Taylor Brandstettera1c30352016-05-13 15:15:11256 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11257 return true;
258 }
259
Ali Tofighde2ac5a2022-06-30 09:58:26260 bool CreateSession(int component, absl::string_view content_name) {
Taylor Brandstettera1c30352016-05-13 15:15:11261 session_ = CreateSession("session", content_name, component);
262 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11263 return false;
Taylor Brandstettera1c30352016-05-13 15:15:11264 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11265 return true;
266 }
267
Ali Tofighde2ac5a2022-06-30 09:58:26268 std::unique_ptr<PortAllocatorSession> CreateSession(absl::string_view sid,
Taylor Brandstetter417eebe2016-05-23 23:02:19269 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11270 return CreateSession(sid, kContentName, component);
271 }
272
Taylor Brandstetter417eebe2016-05-23 23:02:19273 std::unique_ptr<PortAllocatorSession> CreateSession(
Ali Tofighde2ac5a2022-06-30 09:58:26274 absl::string_view sid,
275 absl::string_view content_name,
Taylor Brandstettera1c30352016-05-13 15:15:11276 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11277 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
278 }
279
Taylor Brandstetter417eebe2016-05-23 23:02:19280 std::unique_ptr<PortAllocatorSession> CreateSession(
Ali Tofighde2ac5a2022-06-30 09:58:26281 absl::string_view sid,
282 absl::string_view content_name,
Taylor Brandstettera1c30352016-05-13 15:15:11283 int component,
Ali Tofighde2ac5a2022-06-30 09:58:26284 absl::string_view ice_ufrag,
285 absl::string_view ice_pwd) {
johanfe1ffb12016-08-11 19:37:42286 std::unique_ptr<PortAllocatorSession> session =
287 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11288 session->SignalPortReady.connect(this,
pthatcher1749bc32017-02-08 21:18:00289 &BasicPortAllocatorTestBase::OnPortReady);
290 session->SignalPortsPruned.connect(
291 this, &BasicPortAllocatorTestBase::OnPortsPruned);
Taylor Brandstettera1c30352016-05-13 15:15:11292 session->SignalCandidatesReady.connect(
pthatcher1749bc32017-02-08 21:18:00293 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
Honghai Zhangc67e0f52016-09-19 23:57:37294 session->SignalCandidatesRemoved.connect(
pthatcher1749bc32017-02-08 21:18:00295 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
Taylor Brandstettera1c30352016-05-13 15:15:11296 session->SignalCandidatesAllocationDone.connect(
pthatcher1749bc32017-02-08 21:18:00297 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11298 return session;
299 }
300
Artem Titov2dbb4c92021-07-26 13:12:41301 // Return true if the addresses are the same, or the port is 0 in `pattern`
Taylor Brandstetter8c9be5e42016-05-26 23:07:31302 // (acting as a wildcard) and the IPs are the same.
303 // Even with a wildcard port, the port of the address should be nonzero if
304 // the IP is nonzero.
305 static bool AddressMatch(const SocketAddress& address,
306 const SocketAddress& pattern) {
307 return address.ipaddr() == pattern.ipaddr() &&
308 ((pattern.port() == 0 &&
309 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
310 (pattern.port() != 0 && address.port() == pattern.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11311 }
Taylor Brandstetter8c9be5e42016-05-26 23:07:31312
Honghai Zhangb9e7b4a2016-07-01 03:52:02313 // Returns the number of ports that have matching type, protocol and
314 // address.
315 static int CountPorts(const std::vector<PortInterface*>& ports,
Ali Tofighde2ac5a2022-06-30 09:58:26316 absl::string_view type,
Honghai Zhangb9e7b4a2016-07-01 03:52:02317 ProtocolType protocol,
318 const SocketAddress& client_addr) {
Steve Antonae226f62019-01-29 20:47:38319 return absl::c_count_if(
320 ports, [type, protocol, client_addr](PortInterface* port) {
Honghai Zhangb9e7b4a2016-07-01 03:52:02321 return port->Type() == type && port->GetProtocol() == protocol &&
322 port->Network()->GetBestIP() == client_addr.ipaddr();
323 });
324 }
325
Taylor Brandstetter8c9be5e42016-05-26 23:07:31326 // Find a candidate and return it.
327 static bool FindCandidate(const std::vector<Candidate>& candidates,
Ali Tofighde2ac5a2022-06-30 09:58:26328 absl::string_view type,
329 absl::string_view proto,
Taylor Brandstetter8c9be5e42016-05-26 23:07:31330 const SocketAddress& addr,
331 Candidate* found) {
Steve Antonae226f62019-01-29 20:47:38332 auto it =
333 absl::c_find_if(candidates, [type, proto, addr](const Candidate& c) {
334 return c.type() == type && c.protocol() == proto &&
335 AddressMatch(c.address(), addr);
336 });
Taylor Brandstetter8c9be5e42016-05-26 23:07:31337 if (it != candidates.end() && found) {
338 *found = *it;
339 }
340 return it != candidates.end();
341 }
342
343 // Convenience method to call FindCandidate with no return.
344 static bool HasCandidate(const std::vector<Candidate>& candidates,
Ali Tofighde2ac5a2022-06-30 09:58:26345 absl::string_view type,
346 absl::string_view proto,
Taylor Brandstetter8c9be5e42016-05-26 23:07:31347 const SocketAddress& addr) {
348 return FindCandidate(candidates, type, proto, addr, nullptr);
349 }
350
351 // Version of HasCandidate that also takes a related address.
352 static bool HasCandidateWithRelatedAddr(
353 const std::vector<Candidate>& candidates,
Ali Tofighde2ac5a2022-06-30 09:58:26354 absl::string_view type,
355 absl::string_view proto,
Taylor Brandstetter8c9be5e42016-05-26 23:07:31356 const SocketAddress& addr,
357 const SocketAddress& related_addr) {
Steve Antonae226f62019-01-29 20:47:38358 return absl::c_any_of(
359 candidates, [type, proto, addr, related_addr](const Candidate& c) {
360 return c.type() == type && c.protocol() == proto &&
361 AddressMatch(c.address(), addr) &&
362 AddressMatch(c.related_address(), related_addr);
363 });
Taylor Brandstetter8c9be5e42016-05-26 23:07:31364 }
365
henrike@webrtc.org269fb4b2014-10-28 22:20:11366 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 15:15:11367 int min_port,
368 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11369 return (addr.port() >= min_port && addr.port() <= max_port);
370 }
371
Diep Bui9068f4562022-08-26 15:43:44372 static bool HasNetwork(const std::vector<const rtc::Network*>& networks,
373 const rtc::Network& to_be_found) {
374 auto it =
375 absl::c_find_if(networks, [to_be_found](const rtc::Network* network) {
376 return network->description() == to_be_found.description() &&
377 network->name() == to_be_found.name() &&
378 network->prefix() == to_be_found.prefix();
379 });
380 return it != networks.end();
381 }
382
Taylor Brandstetter417eebe2016-05-23 23:02:19383 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11384 // We should only get this callback once, except in the mux test where
385 // we have multiple port allocation sessions.
386 if (session == session_.get()) {
387 ASSERT_FALSE(candidate_allocation_done_);
388 candidate_allocation_done_ = true;
389 }
Taylor Brandstettera1c30352016-05-13 15:15:11390 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11391 }
392
Artem Titov2dbb4c92021-07-26 13:12:41393 // Check if all ports allocated have send-buffer size `expected`. If
394 // `expected` == -1, check if GetOptions returns SOCKET_ERROR.
henrike@webrtc.org269fb4b2014-10-28 22:20:11395 void CheckSendBufferSizesOfAllPorts(int expected) {
Taylor Brandstetter417eebe2016-05-23 23:02:19396 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11397 for (it = ports_.begin(); it < ports_.end(); ++it) {
398 int send_buffer_size;
399 if (expected == -1) {
400 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 15:15:11401 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11402 } else {
Taylor Brandstettera1c30352016-05-13 15:15:11403 EXPECT_EQ(0,
404 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11405 ASSERT_EQ(expected, send_buffer_size);
406 }
407 }
408 }
409
Honghai Zhangc67e0f52016-09-19 23:57:37410 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
411
henrike@webrtc.org269fb4b2014-10-28 22:20:11412 protected:
Taylor Brandstetter417eebe2016-05-23 23:02:19413 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11414
Taylor Brandstetter417eebe2016-05-23 23:02:19415 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
Mirko Bonadei675513b2017-11-09 10:09:25416 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11417 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 15:15:11418 // Make sure the new port is added to ReadyPorts.
419 auto ready_ports = ses->ReadyPorts();
Steve Antonae226f62019-01-29 20:47:38420 EXPECT_THAT(ready_ports, Contains(port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11421 }
Honghai Zhang8eeecab2016-07-28 20:20:15422 void OnPortsPruned(PortAllocatorSession* ses,
Honghai Zhanga74363c2016-07-29 01:06:15423 const std::vector<PortInterface*>& pruned_ports) {
Mirko Bonadei675513b2017-11-09 10:09:25424 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
Honghai Zhangb9e7b4a2016-07-01 03:52:02425 auto ready_ports = ses->ReadyPorts();
Honghai Zhang8eeecab2016-07-28 20:20:15426 auto new_end = ports_.end();
Honghai Zhanga74363c2016-07-29 01:06:15427 for (PortInterface* port : pruned_ports) {
Honghai Zhang8eeecab2016-07-28 20:20:15428 new_end = std::remove(ports_.begin(), new_end, port);
429 // Make sure the pruned port is not in ReadyPorts.
Steve Antonae226f62019-01-29 20:47:38430 EXPECT_THAT(ready_ports, Not(Contains(port)));
Honghai Zhang8eeecab2016-07-28 20:20:15431 }
432 ports_.erase(new_end, ports_.end());
Honghai Zhangb9e7b4a2016-07-01 03:52:02433 }
434
Taylor Brandstetter417eebe2016-05-23 23:02:19435 void OnCandidatesReady(PortAllocatorSession* ses,
436 const std::vector<Candidate>& candidates) {
Taylor Brandstetter8c9be5e42016-05-26 23:07:31437 for (const Candidate& candidate : candidates) {
Mirko Bonadei675513b2017-11-09 10:09:25438 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
Taylor Brandstetter8c9be5e42016-05-26 23:07:31439 // Sanity check that the ICE component is set.
440 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
441 candidates_.push_back(candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11442 }
Taylor Brandstettera1c30352016-05-13 15:15:11443 // Make sure the new candidates are added to Candidates.
444 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 23:02:19445 for (const Candidate& candidate : candidates) {
Steve Antonae226f62019-01-29 20:47:38446 EXPECT_THAT(ses_candidates, Contains(candidate));
Taylor Brandstettera1c30352016-05-13 15:15:11447 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11448 }
449
Honghai Zhangc67e0f52016-09-19 23:57:37450 void OnCandidatesRemoved(PortAllocatorSession* session,
451 const std::vector<Candidate>& removed_candidates) {
452 auto new_end = std::remove_if(
453 candidates_.begin(), candidates_.end(),
454 [removed_candidates](Candidate& candidate) {
455 for (const Candidate& removed_candidate : removed_candidates) {
456 if (candidate.MatchesForRemoval(removed_candidate)) {
457 return true;
458 }
459 }
460 return false;
461 });
462 candidates_.erase(new_end, candidates_.end());
463 }
464
Taylor Brandstetter417eebe2016-05-23 23:02:19465 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 20:55:10466 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 23:02:19467 RelayServerConfig server_config = allocator_->turn_servers()[i];
468 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11469 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 15:15:11470 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11471 if (proto_addr.address == relay_port->address &&
472 proto_addr.proto == relay_port->proto)
473 return true;
474 }
475 }
476 return false;
477 }
478
Guo-wei Shieh11477022015-08-15 16:28:41479 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
480 bool with_nat) {
481 if (with_nat) {
482 nat_server_.reset(new rtc::NATServer(
Per Ka8cd2ba2023-12-13 07:10:06483 rtc::NAT_OPEN_CONE, thread_, vss_.get(), kNatUdpAddr, kNatTcpAddr,
484 thread_, vss_.get(), rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
Guo-wei Shieh11477022015-08-15 16:28:41485 } else {
Niels Möller66be8952021-09-03 08:55:12486 nat_socket_factory_ =
487 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get());
Guo-wei Shieh11477022015-08-15 16:28:41488 }
489
490 ServerAddresses stun_servers;
491 if (!stun_server.IsNil()) {
492 stun_servers.insert(stun_server);
493 }
Diep Bui9068f4562022-08-26 15:43:44494 allocator_.reset(new BasicPortAllocator(&network_manager_,
495 nat_socket_factory_.get(),
496 stun_servers, &field_trials_));
Qingsi Wanga2d60672018-04-11 23:57:45497 allocator_->Initialize();
498 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 16:28:41499 }
500
pthatcher1749bc32017-02-08 21:18:00501 std::unique_ptr<rtc::VirtualSocketServer> vss_;
502 std::unique_ptr<rtc::FirewallSocketServer> fss_;
Tommife57cec2023-08-30 08:07:06503 rtc::BasicPacketSocketFactory socket_factory_;
nisse7eaa4ea2017-05-08 12:25:41504 rtc::AutoSocketServerThread thread_;
pthatcher1749bc32017-02-08 21:18:00505 std::unique_ptr<rtc::NATServer> nat_server_;
506 rtc::NATSocketFactory nat_factory_;
507 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Per K14630a72023-11-22 13:08:58508 TestStunServer::StunServerPtr stun_server_;
pthatcher1749bc32017-02-08 21:18:00509 TestTurnServer turn_server_;
510 rtc::FakeNetworkManager network_manager_;
511 std::unique_ptr<BasicPortAllocator> allocator_;
512 std::unique_ptr<PortAllocatorSession> session_;
513 std::vector<PortInterface*> ports_;
514 std::vector<Candidate> candidates_;
515 bool candidate_allocation_done_;
Diep Bui9068f4562022-08-26 15:43:44516 webrtc::test::ScopedKeyValueConfig field_trials_;
pthatcher1749bc32017-02-08 21:18:00517};
Honghai Zhangc67e0f52016-09-19 23:57:37518
pthatcher1749bc32017-02-08 21:18:00519class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
520};
521
522class FakeClockBase {
523 public:
524 rtc::ScopedFakeClock fake_clock;
525};
526
527class BasicPortAllocatorTest : public FakeClockBase,
528 public BasicPortAllocatorTestBase {
529 public:
530 // This function starts the port/address gathering and check the existence of
Artem Titov2dbb4c92021-07-26 13:12:41531 // candidates as specified. When `expect_stun_candidate` is true,
532 // `stun_candidate_addr` carries the expected reflective address, which is
pthatcher1749bc32017-02-08 21:18:00533 // also the related address for TURN candidate if it is expected. Otherwise,
534 // it should be ignore.
535 void CheckDisableAdapterEnumeration(
536 uint32_t total_ports,
537 const rtc::IPAddress& host_candidate_addr,
538 const rtc::IPAddress& stun_candidate_addr,
539 const rtc::IPAddress& relay_candidate_udp_transport_addr,
540 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
541 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
542 rtc::IPAddress());
543 if (!session_) {
Qingsi Wange53ac042018-05-08 18:55:07544 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 21:18:00545 }
546 session_->set_flags(session_->flags() |
547 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
548 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
549 allocator().set_allow_tcp_listen(false);
Honghai Zhangc67e0f52016-09-19 23:57:37550 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00551 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
552 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 23:57:37553
pthatcher1749bc32017-02-08 21:18:00554 uint32_t total_candidates = 0;
555 if (!host_candidate_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 11:40:05556 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
557 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:00558 ++total_candidates;
559 }
560 if (!stun_candidate_addr.IsNil()) {
561 rtc::SocketAddress related_address(host_candidate_addr, 0);
562 if (host_candidate_addr.IsNil()) {
563 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
564 }
Jonas Olssonabbe8412018-04-03 11:40:05565 EXPECT_TRUE(HasCandidateWithRelatedAddr(
566 candidates_, "stun", "udp",
567 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
pthatcher1749bc32017-02-08 21:18:00568 ++total_candidates;
569 }
570 if (!relay_candidate_udp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 11:40:05571 EXPECT_TRUE(HasCandidateWithRelatedAddr(
572 candidates_, "relay", "udp",
573 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
574 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 21:18:00575 ++total_candidates;
576 }
577 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Jonas Olssonabbe8412018-04-03 11:40:05578 EXPECT_TRUE(HasCandidateWithRelatedAddr(
579 candidates_, "relay", "udp",
580 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
581 rtc::SocketAddress(stun_candidate_addr, 0)));
pthatcher1749bc32017-02-08 21:18:00582 ++total_candidates;
583 }
584
585 EXPECT_EQ(total_candidates, candidates_.size());
586 EXPECT_EQ(total_ports, ports_.size());
Honghai Zhangc67e0f52016-09-19 23:57:37587 }
588
589 void TestIPv6TurnPortPrunesIPv4TurnPort() {
590 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
591 // Add two IP addresses on the same interface.
592 AddInterface(kClientAddr, "net1");
593 AddInterface(kClientIPv6Addr, "net1");
Tommi3756e292023-09-01 10:45:06594 allocator_.reset(
595 new BasicPortAllocator(&network_manager_, &socket_factory_));
Qingsi Wanga2d60672018-04-11 23:57:45596 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 23:57:37597 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:50598 allocator_->turn_servers(), 0,
599 webrtc::PRUNE_BASED_ON_PRIORITY);
Honghai Zhangc67e0f52016-09-19 23:57:37600 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
601 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
602
603 allocator_->set_step_delay(kMinimumStepDelay);
604 allocator_->set_flags(
605 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
606 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
607
Qingsi Wange53ac042018-05-08 18:55:07608 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 23:57:37609 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00610 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
611 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 23:57:37612 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
613 EXPECT_EQ(3U, session_->ReadyPorts().size());
614 EXPECT_EQ(3U, ports_.size());
615 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
616 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
617 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
618 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
619
620 // Now that we remove candidates when a TURN port is pruned, there will be
Artem Titov2dbb4c92021-07-26 13:12:41621 // exactly 3 candidates in both `candidates_` and `ready_candidates`.
Honghai Zhangc67e0f52016-09-19 23:57:37622 EXPECT_EQ(3U, candidates_.size());
623 const std::vector<Candidate>& ready_candidates =
624 session_->ReadyCandidates();
625 EXPECT_EQ(3U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 11:40:05626 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
627 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
628 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 23:57:37629 }
630
Honghai Zhangf8998cf2019-10-14 18:27:50631 void TestTurnPortPrunesWithUdpAndTcpPorts(
632 webrtc::PortPrunePolicy prune_policy,
633 bool tcp_pruned) {
pthatcher1749bc32017-02-08 21:18:00634 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
635 AddInterface(kClientAddr);
Tommi3756e292023-09-01 10:45:06636 allocator_.reset(
637 new BasicPortAllocator(&network_manager_, &socket_factory_));
Qingsi Wanga2d60672018-04-11 23:57:45638 allocator_->Initialize();
pthatcher1749bc32017-02-08 21:18:00639 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:50640 allocator_->turn_servers(), 0, prune_policy);
pthatcher1749bc32017-02-08 21:18:00641 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
642 allocator_->set_step_delay(kMinimumStepDelay);
643 allocator_->set_flags(allocator().flags() |
644 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
645 PORTALLOCATOR_DISABLE_TCP);
646
Qingsi Wange53ac042018-05-08 18:55:07647 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
pthatcher1749bc32017-02-08 21:18:00648 session_->StartGettingPorts();
649 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
650 kDefaultAllocationTimeout, fake_clock);
651 // Only 2 ports (one STUN and one TURN) are actually being used.
652 EXPECT_EQ(2U, session_->ReadyPorts().size());
Artem Titov2dbb4c92021-07-26 13:12:41653 // We have verified that each port, when it is added to `ports_`, it is
654 // found in `ready_ports`, and when it is pruned, it is not found in
655 // `ready_ports`, so we only need to verify the content in one of them.
pthatcher1749bc32017-02-08 21:18:00656 EXPECT_EQ(2U, ports_.size());
657 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
Honghai Zhangf8998cf2019-10-14 18:27:50658 int num_udp_ports = tcp_pruned ? 1 : 0;
659 EXPECT_EQ(num_udp_ports,
660 CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
661 EXPECT_EQ(1 - num_udp_ports,
662 CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
pthatcher1749bc32017-02-08 21:18:00663
Artem Titov2dbb4c92021-07-26 13:12:41664 // Now that we remove candidates when a TURN port is pruned, `candidates_`
pthatcher1749bc32017-02-08 21:18:00665 // should only contains two candidates regardless whether the TCP TURN port
666 // is created before or after the UDP turn port.
667 EXPECT_EQ(2U, candidates_.size());
Artem Titov2dbb4c92021-07-26 13:12:41668 // There will only be 2 candidates in `ready_candidates` because it only
pthatcher1749bc32017-02-08 21:18:00669 // includes the candidates in the ready ports.
670 const std::vector<Candidate>& ready_candidates =
671 session_->ReadyCandidates();
672 EXPECT_EQ(2U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 11:40:05673 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
Honghai Zhangf8998cf2019-10-14 18:27:50674
675 // The external candidate is always udp.
Jonas Olssonabbe8412018-04-03 11:40:05676 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
677 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:00678 }
679
Honghai Zhangc67e0f52016-09-19 23:57:37680 void TestEachInterfaceHasItsOwnTurnPorts() {
681 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
682 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
683 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
684 // Add two interfaces both having IPv4 and IPv6 addresses.
685 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
686 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
687 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
688 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
Tommi3756e292023-09-01 10:45:06689 allocator_.reset(
690 new BasicPortAllocator(&network_manager_, &socket_factory_));
Qingsi Wanga2d60672018-04-11 23:57:45691 allocator_->Initialize();
Honghai Zhangc67e0f52016-09-19 23:57:37692 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:50693 allocator_->turn_servers(), 0,
694 webrtc::PRUNE_BASED_ON_PRIORITY);
Honghai Zhangc67e0f52016-09-19 23:57:37695 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
696 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
697 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
698
699 allocator_->set_step_delay(kMinimumStepDelay);
zhihuangb09b3f92017-03-07 22:40:51700 allocator_->set_flags(
701 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
702 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
Qingsi Wange53ac042018-05-08 18:55:07703 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Honghai Zhangc67e0f52016-09-19 23:57:37704 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00705 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
706 kDefaultAllocationTimeout, fake_clock);
Honghai Zhangc67e0f52016-09-19 23:57:37707 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
708 // use.
709 EXPECT_EQ(10U, session_->ReadyPorts().size());
710 EXPECT_EQ(10U, ports_.size());
711 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
712 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
713 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
714 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
715 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
716 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
717 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
718 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
719 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
720 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
721
722 // Now that we remove candidates when TURN ports are pruned, there will be
Artem Titov2dbb4c92021-07-26 13:12:41723 // exactly 10 candidates in `candidates_`.
Honghai Zhangc67e0f52016-09-19 23:57:37724 EXPECT_EQ(10U, candidates_.size());
725 const std::vector<Candidate>& ready_candidates =
726 session_->ReadyCandidates();
727 EXPECT_EQ(10U, ready_candidates.size());
Jonas Olssonabbe8412018-04-03 11:40:05728 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
729 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
730 EXPECT_TRUE(
731 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
732 EXPECT_TRUE(
733 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
734 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
735 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
736 EXPECT_TRUE(
737 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
738 EXPECT_TRUE(
739 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
740 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
741 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
Honghai Zhangc67e0f52016-09-19 23:57:37742 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11743};
744
745// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 15:15:11746TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11747 EXPECT_EQ(&network_manager_, allocator().network_manager());
748 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
Niels Möllera8e6f342019-09-24 07:05:19749 ASSERT_EQ(0u, allocator().turn_servers().size());
750
Qingsi Wange53ac042018-05-08 18:55:07751 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 15:15:11752 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11753}
754
Taylor Brandstetter0c7e9f52015-12-29 22:14:52755// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 15:15:11756TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:52757 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
758 rtc::ADAPTER_TYPE_ETHERNET);
759 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
760 rtc::ADAPTER_TYPE_WIFI);
761 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
762 rtc::ADAPTER_TYPE_CELLULAR);
763 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
764 rtc::ADAPTER_TYPE_VPN);
765 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
766 rtc::ADAPTER_TYPE_LOOPBACK);
Qingsi Wange53ac042018-05-08 18:55:07767 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:19768 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
769 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52770 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00771 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
772 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52773 EXPECT_EQ(4U, candidates_.size());
Mirko Bonadei739baf02019-01-27 16:29:42774 for (const Candidate& candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:52775 EXPECT_LT(candidate.address().ip(), 0x12345604U);
776 }
777}
778
Taylor Brandstettera1c30352016-05-13 15:15:11779TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:52780 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
781 rtc::ADAPTER_TYPE_ETHERNET);
782 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
783 rtc::ADAPTER_TYPE_WIFI);
784 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
785 rtc::ADAPTER_TYPE_CELLULAR);
786 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
787 rtc::ADAPTER_TYPE_LOOPBACK |
788 rtc::ADAPTER_TYPE_WIFI);
Qingsi Wange53ac042018-05-08 18:55:07789 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:19790 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
791 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52792 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00793 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
794 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter0c7e9f52015-12-29 22:14:52795 EXPECT_EQ(1U, candidates_.size());
796 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
797}
798
deadbeef1c46a352017-09-27 18:24:05799// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
800// both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
801TEST_F(BasicPortAllocatorTest,
802 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
803 SocketAddress wifi(IPAddress(0x12345600U), 0);
804 SocketAddress cell(IPAddress(0x12345601U), 0);
805 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
806 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
807 // Disable all but UDP candidates to make the test simpler.
honghaiz60347052016-06-01 01:29:12808 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
809 cricket::PORTALLOCATOR_DISABLE_RELAY |
810 cricket::PORTALLOCATOR_DISABLE_TCP |
811 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07812 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-06-01 01:29:12813 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00814 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
815 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 18:24:05816 // Should only get one Wi-Fi candidate.
honghaiz60347052016-06-01 01:29:12817 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05818 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 18:24:05819}
honghaiz60347052016-06-01 01:29:12820
deadbeef1c46a352017-09-27 18:24:05821// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
822// both "unknown" and cell interfaces are available, only the unknown are used.
823// The unknown interface may be something that ultimately uses Wi-Fi, so we do
824// this to be on the safe side.
825TEST_F(BasicPortAllocatorTest,
826 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
827 SocketAddress cell(IPAddress(0x12345601U), 0);
828 SocketAddress unknown1(IPAddress(0x12345602U), 0);
829 SocketAddress unknown2(IPAddress(0x12345603U), 0);
830 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
831 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
832 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
833 // Disable all but UDP candidates to make the test simpler.
834 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
835 cricket::PORTALLOCATOR_DISABLE_RELAY |
836 cricket::PORTALLOCATOR_DISABLE_TCP |
837 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07838 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-06-01 01:29:12839 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00840 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
841 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 18:24:05842 // Should only get two candidates, none of which is cell.
honghaiz60347052016-06-01 01:29:12843 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05844 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
845 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
deadbeef1c46a352017-09-27 18:24:05846}
honghaiz60347052016-06-01 01:29:12847
deadbeef1c46a352017-09-27 18:24:05848// Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
849// there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
850// interface is used.
851TEST_F(BasicPortAllocatorTest,
852 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
853 SocketAddress wifi(IPAddress(0x12345600U), 0);
854 SocketAddress cellular(IPAddress(0x12345601U), 0);
855 SocketAddress unknown1(IPAddress(0x12345602U), 0);
856 SocketAddress unknown2(IPAddress(0x12345603U), 0);
857 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
858 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
859 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
860 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
861 // Disable all but UDP candidates to make the test simpler.
862 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
863 cricket::PORTALLOCATOR_DISABLE_RELAY |
864 cricket::PORTALLOCATOR_DISABLE_TCP |
865 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07866 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
honghaiz60347052016-06-01 01:29:12867 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:00868 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
869 kDefaultAllocationTimeout, fake_clock);
deadbeef1c46a352017-09-27 18:24:05870 // Should only get one Wi-Fi candidate.
honghaiz60347052016-06-01 01:29:12871 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05872 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
deadbeef1c46a352017-09-27 18:24:05873}
874
875// Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
876// only interface available is cellular, it ends up used anyway. A costly
877// connection is always better than no connection.
878TEST_F(BasicPortAllocatorTest,
879 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
880 SocketAddress cellular(IPAddress(0x12345601U), 0);
881 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
882 // Disable all but UDP candidates to make the test simpler.
883 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
884 cricket::PORTALLOCATOR_DISABLE_RELAY |
885 cricket::PORTALLOCATOR_DISABLE_TCP |
886 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07887 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 18:24:05888 session_->StartGettingPorts();
889 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
890 kDefaultAllocationTimeout, fake_clock);
891 // Make sure we got the cell candidate.
892 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05893 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
honghaiz60347052016-06-01 01:29:12894}
895
Yuwei Huangb181f712018-01-23 01:01:28896// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
897// a WiFi network with link-local IP address and a cellular network, then the
898// cellular candidate will still be gathered.
899TEST_F(BasicPortAllocatorTest,
900 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
901 SocketAddress wifi_link_local("169.254.0.1", 0);
902 SocketAddress cellular(IPAddress(0x12345601U), 0);
903 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
904 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
905
906 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
907 cricket::PORTALLOCATOR_DISABLE_RELAY |
908 cricket::PORTALLOCATOR_DISABLE_TCP |
909 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07910 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-23 01:01:28911 session_->StartGettingPorts();
912 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
913 kDefaultAllocationTimeout, fake_clock);
914 // Make sure we got both wifi and cell candidates.
915 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05916 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
917 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
Yuwei Huangb181f712018-01-23 01:01:28918}
919
920// Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
921// a WiFi network with link-local IP address, a WiFi network with a normal IP
922// address and a cellular network, then the cellular candidate will not be
923// gathered.
924TEST_F(BasicPortAllocatorTest,
925 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
926 SocketAddress wifi(IPAddress(0x12345600U), 0);
927 SocketAddress wifi_link_local("169.254.0.1", 0);
928 SocketAddress cellular(IPAddress(0x12345601U), 0);
929 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
930 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
931 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
932
933 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
934 cricket::PORTALLOCATOR_DISABLE_RELAY |
935 cricket::PORTALLOCATOR_DISABLE_TCP |
936 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Qingsi Wange53ac042018-05-08 18:55:07937 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Yuwei Huangb181f712018-01-23 01:01:28938 session_->StartGettingPorts();
939 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
940 kDefaultAllocationTimeout, fake_clock);
941 // Make sure we got only wifi candidates.
942 EXPECT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:05943 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
944 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
Yuwei Huangb181f712018-01-23 01:01:28945}
946
Qingsi Wange53ac042018-05-08 18:55:07947// Test that the adapter types of the Ethernet and the VPN can be correctly
948// identified so that the Ethernet has a lower network cost than the VPN, and
949// the Ethernet is not filtered out if PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is
950// set.
951TEST_F(BasicPortAllocatorTest,
952 EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent) {
953 AddInterface(kClientAddr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
954 AddInterface(kClientAddr2, "tap0", rtc::ADAPTER_TYPE_VPN);
955 allocator().set_flags(PORTALLOCATOR_DISABLE_COSTLY_NETWORKS |
956 PORTALLOCATOR_DISABLE_RELAY |
957 PORTALLOCATOR_DISABLE_TCP);
958 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
959 session_->StartGettingPorts();
960 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
961 kDefaultAllocationTimeout, fake_clock);
962 // The VPN tap0 network should be filtered out as a costly network, and we
963 // should have a UDP port and a STUN port from the Ethernet eth0.
964 ASSERT_EQ(2U, ports_.size());
965 EXPECT_EQ(ports_[0]->Network()->name(), "eth0");
966 EXPECT_EQ(ports_[1]->Network()->name(), "eth0");
967}
968
deadbeef3427f532017-07-26 23:09:33969// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
970// to gather candidates.
971TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
972 // Add three IPv6 network interfaces, but tell the allocator to only use two.
973 allocator().set_max_ipv6_networks(2);
974 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
975 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
976 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
977
978 // To simplify the test, only gather UDP host candidates.
979 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
980 PORTALLOCATOR_DISABLE_STUN |
981 PORTALLOCATOR_DISABLE_RELAY);
982
Qingsi Wange53ac042018-05-08 18:55:07983 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 23:09:33984 session_->StartGettingPorts();
985 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
986 kDefaultAllocationTimeout, fake_clock);
987 EXPECT_EQ(2U, candidates_.size());
988 // Ensure the expected two interfaces (eth0 and eth1) were used.
Jonas Olssonabbe8412018-04-03 11:40:05989 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
990 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
deadbeef3427f532017-07-26 23:09:33991}
992
993// Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
994// being used.
995TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
996 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
997 allocator().set_max_ipv6_networks(1);
998 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
999 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
1000 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
1001 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
1002
1003 // To simplify the test, only gather UDP host candidates.
1004 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
1005 PORTALLOCATOR_DISABLE_STUN |
1006 PORTALLOCATOR_DISABLE_RELAY);
1007
Qingsi Wange53ac042018-05-08 18:55:071008 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
deadbeef3427f532017-07-26 23:09:331009 session_->StartGettingPorts();
1010 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1011 kDefaultAllocationTimeout, fake_clock);
1012 EXPECT_EQ(3U, candidates_.size());
1013 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
1014 // were used.
Jonas Olssonabbe8412018-04-03 11:40:051015 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
1016 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1017 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
deadbeef3427f532017-07-26 23:09:331018}
1019
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201020// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111021TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 22:14:521022 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
1023 allocator_->SetNetworkIgnoreMask(0);
Qingsi Wange53ac042018-05-08 18:55:071024 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191025 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
1026 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201027 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001028 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1029 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201030 EXPECT_EQ(1U, candidates_.size());
1031}
1032
henrike@webrtc.org269fb4b2014-10-28 22:20:111033// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 15:15:111034TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111035 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071036 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111037 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011038 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1039 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191040 EXPECT_EQ(3U, candidates_.size());
1041 EXPECT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051042 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1043 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
Jonas Olssonabbe8412018-04-03 11:40:051044 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:111045}
1046
honghaiz8c404fa2015-09-28 14:59:431047// Test that when the same network interface is brought down and up, the
1048// port allocator session will restart a new allocation sequence if
1049// it is not stopped.
Taylor Brandstettera1c30352016-05-13 15:15:111050TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 14:59:431051 std::string if_name("test_net0");
1052 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 18:55:071053 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 14:59:431054 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011055 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1056 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191057 EXPECT_EQ(3U, candidates_.size());
1058 EXPECT_EQ(3U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431059 candidate_allocation_done_ = false;
1060 candidates_.clear();
1061 ports_.clear();
1062
deadbeef7f1563f2017-09-16 00:40:011063 // Disable socket creation to simulate the network interface being down. When
1064 // no network interfaces are available, BasicPortAllocator will fall back to
1065 // binding to the "ANY" address, so we need to make sure that fails too.
1066 fss_->set_tcp_sockets_enabled(false);
1067 fss_->set_udp_sockets_enabled(false);
honghaiz8c404fa2015-09-28 14:59:431068 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-16 00:40:011069 SIMULATED_WAIT(false, 1000, fake_clock);
1070 EXPECT_EQ(0U, candidates_.size());
1071 ports_.clear();
Niels Möllera8e6f342019-09-24 07:05:191072 candidate_allocation_done_ = false;
honghaiz8c404fa2015-09-28 14:59:431073
1074 // When the same interfaces are added again, new candidates/ports should be
1075 // generated.
deadbeef7f1563f2017-09-16 00:40:011076 fss_->set_tcp_sockets_enabled(true);
1077 fss_->set_udp_sockets_enabled(true);
honghaiz8c404fa2015-09-28 14:59:431078 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-16 00:40:011079 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1080 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191081 EXPECT_EQ(3U, candidates_.size());
1082 EXPECT_EQ(3U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431083}
1084
1085// Test that when the same network interface is brought down and up, the
1086// port allocator session will not restart a new allocation sequence if
1087// it is stopped.
Taylor Brandstettera1c30352016-05-13 15:15:111088TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 14:59:431089 std::string if_name("test_net0");
1090 AddInterface(kClientAddr, if_name);
Qingsi Wange53ac042018-05-08 18:55:071091 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 14:59:431092 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011093 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1094 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191095 EXPECT_EQ(3U, candidates_.size());
1096 EXPECT_EQ(3U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431097 session_->StopGettingPorts();
1098 candidates_.clear();
1099 ports_.clear();
1100
1101 RemoveInterface(kClientAddr);
deadbeef7f1563f2017-09-16 00:40:011102 // Wait one (simulated) second and then verify no new candidates have
1103 // appeared.
1104 SIMULATED_WAIT(false, 1000, fake_clock);
1105 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 14:59:431106 EXPECT_EQ(0U, ports_.size());
1107
1108 // When the same interfaces are added again, new candidates/ports should not
1109 // be generated because the session has stopped.
1110 AddInterface(kClientAddr, if_name);
deadbeef7f1563f2017-09-16 00:40:011111 SIMULATED_WAIT(false, 1000, fake_clock);
1112 EXPECT_EQ(0U, candidates_.size());
honghaiz8c404fa2015-09-28 14:59:431113 EXPECT_EQ(0U, ports_.size());
honghaiz8c404fa2015-09-28 14:59:431114}
1115
deadbeef1c46a352017-09-27 18:24:051116// Similar to the above tests, but tests a situation when sockets can't be
1117// bound to a network interface, then after a network change event can be.
1118// Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
1119TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1120 // Only test local ports to simplify test.
1121 ResetWithNoServersOrNat();
1122 // Provide a situation where the interface appears to be available, but
1123 // binding the sockets fails. See bug for description of when this can
1124 // happen.
1125 std::string if_name("test_net0");
1126 AddInterface(kClientAddr, if_name);
1127 fss_->set_tcp_sockets_enabled(false);
1128 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071129 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeef1c46a352017-09-27 18:24:051130 session_->StartGettingPorts();
1131 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1132 kDefaultAllocationTimeout, fake_clock);
1133 // Make sure we actually prevented candidates from being gathered (other than
1134 // a single TCP active candidate, since that doesn't require creating a
1135 // socket).
1136 ASSERT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051137 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 18:24:051138 candidate_allocation_done_ = false;
1139
1140 // Now simulate the interface coming up, with the newfound ability to bind
1141 // sockets.
1142 fss_->set_tcp_sockets_enabled(true);
1143 fss_->set_udp_sockets_enabled(true);
1144 AddInterface(kClientAddr, if_name);
1145 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1146 kDefaultAllocationTimeout, fake_clock);
1147 // Should get UDP and TCP candidate.
1148 ASSERT_EQ(2U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051149 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
deadbeef1c46a352017-09-27 18:24:051150 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1151 // We should extend this test to also verify that a server candidate is
1152 // gathered.
Jonas Olssonabbe8412018-04-03 11:40:051153 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
deadbeef1c46a352017-09-27 18:24:051154}
1155
henrike@webrtc.org269fb4b2014-10-28 22:20:111156// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 15:15:111157TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111158 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191159 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 18:55:071160 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111161 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001162 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111163 EXPECT_EQ(2U, ports_.size());
Niels Möllera8e6f342019-09-24 07:05:191164 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 2000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111165 EXPECT_EQ(3U, ports_.size());
Niels Möllera8e6f342019-09-24 07:05:191166
1167 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 1500, fake_clock);
Jonas Olssonabbe8412018-04-03 11:40:051168 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Niels Möllera8e6f342019-09-24 07:05:191169 EXPECT_EQ(3U, ports_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111170 EXPECT_TRUE(candidate_allocation_done_);
1171 // If we Stop gathering now, we shouldn't get a second "done" callback.
1172 session_->StopGettingPorts();
1173}
1174
Taylor Brandstettera1c30352016-05-13 15:15:111175TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111176 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071177 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:111178 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011179 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1180 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191181 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111182 // If we Stop gathering now, we shouldn't get a second "done" callback.
1183 session_->StopGettingPorts();
1184
1185 // All ports should have unset send-buffer sizes.
1186 CheckSendBufferSizesOfAllPorts(-1);
1187}
1188
deadbeef7f1563f2017-09-16 00:40:011189// Tests that we can get callback after StopGetAllPorts when called in the
1190// middle of gathering.
Taylor Brandstettera1c30352016-05-13 15:15:111191TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111192 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071193 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111194 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011195 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1196 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111197 EXPECT_EQ(2U, ports_.size());
1198 session_->StopGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011199 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1200 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111201}
1202
1203// Test that we restrict client ports appropriately when a port range is set.
1204// We check the candidates for udp/stun/tcp ports, and the from address
1205// for relay ports.
Taylor Brandstettera1c30352016-05-13 15:15:111206TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111207 AddInterface(kClientAddr);
1208 // Check that an invalid port range fails.
1209 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1210 // Check that a null port range succeeds.
1211 EXPECT_TRUE(SetPortRange(0, 0));
1212 // Check that a valid port range succeeds.
1213 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Qingsi Wange53ac042018-05-08 18:55:071214 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111215 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011216 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1217 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191218 EXPECT_EQ(3U, candidates_.size());
1219 EXPECT_EQ(3U, ports_.size());
honghaiza73df552016-10-27 18:44:171220
1221 int num_nonrelay_candidates = 0;
1222 for (const Candidate& candidate : candidates_) {
1223 // Check the port number for the UDP/STUN/TCP port objects.
Tommi698b4e72024-01-24 07:36:451224 if (!candidate.is_relay()) {
Jonas Olssonabbe8412018-04-03 11:40:051225 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
honghaiza73df552016-10-27 18:44:171226 ++num_nonrelay_candidates;
1227 }
1228 }
1229 EXPECT_EQ(3, num_nonrelay_candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:111230}
1231
deadbeefe97389c2016-12-23 09:43:451232// Test that if we have no network adapters, we bind to the ANY address and
1233// still get non-host candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111234TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
deadbeefe97389c2016-12-23 09:43:451235 // Default config uses GTURN and no NAT, so replace that with the
1236 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1237 ResetWithStunServerAndNat(kStunAddr);
1238 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1239 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1240 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
Qingsi Wange53ac042018-05-08 18:55:071241 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111242 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001243 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1244 kDefaultAllocationTimeout, fake_clock);
deadbeefe97389c2016-12-23 09:43:451245 EXPECT_EQ(4U, ports_.size());
1246 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1247 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1248 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1249 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1250 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1251 // The "any" address port should be in the signaled ready ports, but the host
1252 // candidate for it is useless and shouldn't be signaled. So we only have
1253 // STUN/TURN candidates.
1254 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051255 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1256 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
deadbeefe97389c2016-12-23 09:43:451257 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1258 // server.
Tommi698b4e72024-01-24 07:36:451259 rtc::SocketAddress addr(kTurnUdpExtAddr.ipaddr(), 0);
1260 EXPECT_EQ(2, absl::c_count_if(candidates_, [&](const Candidate& c) {
1261 return c.is_relay() && c.protocol() == "udp" &&
1262 AddressMatch(c.address(), addr);
1263 }));
henrike@webrtc.org269fb4b2014-10-28 22:20:111264}
1265
Guo-wei Shieh898d21c2015-09-30 17:54:551266// Test that when enumeration is disabled, we should not have any ports when
1267// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 15:15:111268TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 17:54:551269 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 17:54:551270 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wangc129c352019-04-18 17:41:581271 allocator().SetCandidateFilter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 17:54:551272 // Expect to see no ports and no candidates.
1273 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1274 rtc::IPAddress(), rtc::IPAddress());
1275}
1276
Guo-wei Shieh9af97f82015-11-10 22:47:391277// Test that even with multiple interfaces, the result should still be a single
1278// default private, one STUN and one TURN candidate since we bind to any address
1279// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 15:15:111280TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-14 05:24:021281 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:011282 AddInterface(kPrivateAddr);
1283 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 16:28:411284 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:011285 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 22:12:381286
1287 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1288 // address set and we have no IPv6 STUN server, there should be no IPv6
1289 // candidates.
Qingsi Wange53ac042018-05-08 18:55:071290 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191291 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 22:12:381292
1293 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1294 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1295 // TURN/UDP candidates.
1296 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 22:47:391297 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1298 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-14 05:24:021299}
guoweis@webrtc.orgf358aea2015-02-18 18:44:011300
Guo-wei Shieh9af97f82015-11-10 22:47:391301// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1302// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 15:15:111303TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 23:02:191304 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 22:47:391305 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 16:28:411306 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-14 05:24:021307 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 22:47:391308 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1309 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1310 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1311 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-14 05:24:021312 kTurnUdpExtAddr.ipaddr());
1313}
1314
Guo-wei Shieh9af97f82015-11-10 22:47:391315// Test that when adapter enumeration is disabled, for endpoints without
1316// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 15:15:111317TEST_F(BasicPortAllocatorTest,
1318 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 22:47:391319 ResetWithNoServersOrNat();
1320 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1321 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1322 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-14 05:24:021323}
1324
Guo-wei Shieh9af97f82015-11-10 22:47:391325// Test that when adapter enumeration is disabled, with
1326// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1327// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111328TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 22:47:391329 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1330 ResetWithStunServerNoNat(kStunAddr);
Qingsi Wange53ac042018-05-08 18:55:071331 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191332 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 22:47:391333 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1334 // candidate.
Guo-wei Shieh38f88932015-08-14 05:24:021335 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201336 rtc::IPAddress(), rtc::IPAddress());
1337}
1338
1339// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 22:47:391340// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1341// a NAT, there is no local candidate. However, this specified default route
1342// (kClientAddr) which was discovered when sending STUN requests, will become
1343// the srflx addresses.
Yves Gerey665174f2018-06-19 13:03:051344TEST_F(BasicPortAllocatorTest,
1345 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201346 ResetWithStunServerNoNat(kStunAddr);
Niels Möller84d15952021-09-01 08:50:341347 AddInterfaceAsDefaultSourceAddresss(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071348 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191349 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 15:48:201350 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1351 // candidate.
Guo-wei Shieh9af97f82015-11-10 22:47:391352 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1353 rtc::IPAddress(), rtc::IPAddress());
1354}
1355
1356// Test that when adapter enumeration is disabled, with
1357// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1358// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111359TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 22:47:391360 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1361 ResetWithStunServerAndNat(kStunAddr);
Qingsi Wange53ac042018-05-08 18:55:071362 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191363 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 22:47:391364 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1365 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1366 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:011367}
1368
Guo-wei Shieh13d35f62015-08-26 22:32:561369// Test that we disable relay over UDP, and only TCP is used when connecting to
1370// the relay server.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311371TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 23:02:191372 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 22:32:561373 AddInterface(kClientAddr);
1374 ResetWithStunServerAndNat(kStunAddr);
1375 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Qingsi Wange53ac042018-05-08 18:55:071376 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191377 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1378 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1379 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 22:32:561380
1381 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001382 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1383 kDefaultAllocationTimeout, fake_clock);
Guo-wei Shieh13d35f62015-08-26 22:32:561384
1385 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1386 // TURN/TCP candidates.
1387 EXPECT_EQ(2U, ports_.size());
1388 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter8c9be5e42016-05-26 23:07:311389 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051390 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1391 &turn_candidate));
Guo-wei Shieh13d35f62015-08-26 22:32:561392 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311393 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
Jonas Olssonabbe8412018-04-03 11:40:051394 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Guo-wei Shieh13d35f62015-08-26 22:32:561395}
1396
henrike@webrtc.org269fb4b2014-10-28 22:20:111397// Test that we can get OnCandidatesAllocationDone callback when all the ports
1398// are disabled.
Taylor Brandstettera1c30352016-05-13 15:15:111399TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111400 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071401 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191402 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1403 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111404 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001405 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111406 EXPECT_EQ(0U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111407}
1408
1409// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 15:15:111410TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111411 AddInterface(kClientAddr);
1412 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071413 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:111414 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011415 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1416 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191417 EXPECT_EQ(1U, candidates_.size());
1418 EXPECT_EQ(1U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051419 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:111420}
1421
1422// Test that we don't crash or malfunction if we can't create UDP sockets or
1423// listen on TCP sockets. We still give out a local TCP address, since
1424// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 15:15:111425TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111426 AddInterface(kClientAddr);
1427 fss_->set_udp_sockets_enabled(false);
1428 fss_->set_tcp_listen_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071429 ASSERT_TRUE(CreateSession(1));
henrike@webrtc.org269fb4b2014-10-28 22:20:111430 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011431 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1432 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191433 EXPECT_EQ(1U, candidates_.size());
1434 EXPECT_EQ(1U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051435 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:111436}
1437
1438// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 15:15:111439// TODO(deadbeef): Find a way to exit early here.
1440TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111441 AddInterface(kClientAddr);
1442 fss_->set_tcp_sockets_enabled(false);
1443 fss_->set_udp_sockets_enabled(false);
Qingsi Wange53ac042018-05-08 18:55:071444 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111445 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011446 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
Taylor Brandstettera1c30352016-05-13 15:15:111447 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:111448 // In case of Relay, ports creation will succeed but sockets will fail.
1449 // There is no error reporting from RelayEntry to handle this failure.
1450}
1451
1452// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 15:15:111453TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111454 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1455 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071456 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111457 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001458 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1459 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111460 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051461 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1462 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
pthatcher94a2f212017-02-08 22:42:221463 // We wait at least for a full STUN timeout, which
Niels Möllera8e6f342019-09-24 07:05:191464 // cricket::STUN_TOTAL_TIMEOUT seconds.
pthatcher94a2f212017-02-08 22:42:221465 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
Niels Möllera8e6f342019-09-24 07:05:191466 cricket::STUN_TOTAL_TIMEOUT, fake_clock);
1467 // No additional (STUN) candidates.
1468 EXPECT_EQ(2U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111469}
1470
Taylor Brandstettera1c30352016-05-13 15:15:111471TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111472 AddInterface(kClientAddr);
1473 AddInterface(kClientAddr2);
1474 // Allocating only host UDP ports. This is done purely for testing
1475 // convenience.
Taylor Brandstetter417eebe2016-05-23 23:02:191476 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1477 PORTALLOCATOR_DISABLE_RELAY);
Qingsi Wange53ac042018-05-08 18:55:071478 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111479 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001480 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1481 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111482 ASSERT_EQ(2U, candidates_.size());
1483 EXPECT_EQ(2U, ports_.size());
1484 // Candidates priorities should be different.
1485 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1486}
1487
1488// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 15:15:111489TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111490 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071491 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111492 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011493 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1494 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191495 EXPECT_EQ(3U, candidates_.size());
1496 EXPECT_EQ(3U, ports_.size());
Taylor Brandstettera1c30352016-05-13 15:15:111497 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:111498}
1499
Taylor Brandstetter417eebe2016-05-23 23:02:191500// Test that the allocator session uses the candidate filter it's created with,
1501// rather than the filter of its parent allocator.
1502// The filter of the allocator should only affect the next gathering phase,
1503// according to JSEP, which means the *next* allocator session returned.
1504TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1505 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071506 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:191507 // Set candidate filter *after* creating the session. Should have no effect.
Qingsi Wangc129c352019-04-18 17:41:581508 allocator().SetCandidateFilter(CF_RELAY);
Taylor Brandstetter417eebe2016-05-23 23:02:191509 session_->StartGettingPorts();
1510 // 7 candidates and 4 ports is what we would normally get (see the
1511 // TestGetAllPorts* tests).
deadbeef7f1563f2017-09-16 00:40:011512 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1513 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191514 EXPECT_EQ(3U, candidates_.size());
1515 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 23:02:191516}
1517
henrike@webrtc.org269fb4b2014-10-28 22:20:111518// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1519// This test also verifies that when the allocator is only allowed to use
1520// relay (i.e. IceTransportsType is relay), the raddr is an empty
1521// address with the correct family. This is to prevent any local
1522// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 15:15:111523TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111524 AddInterface(kClientAddr);
1525 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 16:28:411526 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Qingsi Wangc129c352019-04-18 17:41:581527 allocator().SetCandidateFilter(CF_RELAY);
Qingsi Wange53ac042018-05-08 18:55:071528 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111529 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001530 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1531 kDefaultAllocationTimeout, fake_clock);
Jonas Olssonabbe8412018-04-03 11:40:051532 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1533 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:111534
1535 EXPECT_EQ(1U, candidates_.size());
1536 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
Tommi698b4e72024-01-24 07:36:451537 EXPECT_TRUE(candidates_[0].is_relay());
Taylor Brandstetter8c9be5e42016-05-26 23:07:311538 EXPECT_EQ(
1539 candidates_[0].related_address(),
1540 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:111541}
1542
Taylor Brandstettera1c30352016-05-13 15:15:111543TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111544 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191545 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wangc129c352019-04-18 17:41:581546 allocator().SetCandidateFilter(CF_HOST);
Qingsi Wange53ac042018-05-08 18:55:071547 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111548 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001549 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1550 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 15:15:111551 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1552 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311553 for (const Candidate& candidate : candidates_) {
Tommi698b4e72024-01-24 07:36:451554 EXPECT_TRUE(candidate.is_local());
henrike@webrtc.org269fb4b2014-10-28 22:20:111555 }
1556}
1557
1558// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 15:15:111559TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111560 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 16:28:411561 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:111562
Taylor Brandstetter417eebe2016-05-23 23:02:191563 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wangc129c352019-04-18 17:41:581564 allocator().SetCandidateFilter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 18:55:071565 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111566 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001567 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1568 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111569 // Host is behind NAT, no private address will be exposed. Hence only UDP
1570 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 15:15:111571 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1572 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
Tommi698b4e72024-01-24 07:36:451573 EXPECT_TRUE(candidates_[0].is_stun());
Taylor Brandstetter8c9be5e42016-05-26 23:07:311574 EXPECT_EQ(
1575 candidates_[0].related_address(),
1576 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
henrike@webrtc.org269fb4b2014-10-28 22:20:111577}
1578
1579// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 15:15:111580TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111581 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:191582 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wangc129c352019-04-18 17:41:581583 allocator().SetCandidateFilter(CF_REFLEXIVE);
Qingsi Wange53ac042018-05-08 18:55:071584 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111585 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001586 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1587 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111588 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 15:15:111589 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:111590 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311591 for (const Candidate& candidate : candidates_) {
Tommi698b4e72024-01-24 07:36:451592 EXPECT_TRUE(candidate.is_local());
henrike@webrtc.org269fb4b2014-10-28 22:20:111593 }
1594}
1595
Peter Thatcher7cbd1882015-09-18 01:54:521596// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111597TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111598 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071599 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111600 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:011601 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1602 kDefaultAllocationTimeout, fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191603 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051604 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1605 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1606 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
Niels Möllera8e6f342019-09-24 07:05:191607 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter8c9be5e42016-05-26 23:07:311608 for (const Candidate& candidate : candidates_) {
1609 EXPECT_EQ(kIceUfrag0, candidate.username());
1610 EXPECT_EQ(kIcePwd0, candidate.password());
1611 }
henrike@webrtc.org269fb4b2014-10-28 22:20:111612}
1613
henrike@webrtc.org269fb4b2014-10-28 22:20:111614// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1615// is allocated for udp and stun. Also verify there is only one candidate
1616// (local) if stun candidate is same as local candidate, which will be the case
1617// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 15:15:111618TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111619 AddInterface(kClientAddr);
1620 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191621 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 18:55:071622 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111623 session_->StartGettingPorts();
Niels Möllera8e6f342019-09-24 07:05:191624 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 21:18:001625 fake_clock);
Niels Möllera8e6f342019-09-24 07:05:191626 EXPECT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051627 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
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}
1631
1632// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1633// is allocated for udp and stun. In this test we should expect both stun and
1634// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 15:15:111635TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111636 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 16:28:411637 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:111638
1639 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191640 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Qingsi Wange53ac042018-05-08 18:55:071641 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111642 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:001643 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1644 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111645 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051646 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1647 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1648 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:001649 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1650 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111651 EXPECT_EQ(3U, candidates_.size());
1652}
1653
deadbeefc5d0d952015-07-16 17:22:211654// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 15:15:111655TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 23:02:191656 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111657 AddInterface(kClientAddr);
Tommi3756e292023-09-01 10:45:061658 allocator_.reset(new BasicPortAllocator(&network_manager_, &socket_factory_));
Qingsi Wanga2d60672018-04-11 23:57:451659 allocator_->Initialize();
henrike@webrtc.org269fb4b2014-10-28 22:20:111660
1661 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1662
Taylor Brandstetter417eebe2016-05-23 23:02:191663 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:111664 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191665 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1666 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111667
Qingsi Wange53ac042018-05-08 18:55:071668 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111669 session_->StartGettingPorts();
1670
deadbeef7f1563f2017-09-16 00:40:011671 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1672 kDefaultAllocationTimeout, fake_clock);
1673 ASSERT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111674 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051675 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1676 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1677 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1678 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1679 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:111680}
1681
Honghai Zhangf8998cf2019-10-14 18:27:501682// Test that if the turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN
1683// port will not be used if UDP TurnPort is used, given that TCP TURN port
1684// becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371685TEST_F(BasicPortAllocatorTest,
1686 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1687 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1688 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1689 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
Honghai Zhangb9e7b4a2016-07-01 03:52:021690
Honghai Zhangf8998cf2019-10-14 18:27:501691 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY,
1692 true /* tcp_pruned */);
Honghai Zhangb9e7b4a2016-07-01 03:52:021693}
1694
Honghai Zhangf8998cf2019-10-14 18:27:501695// Test that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN port
1696// will not be used if UDP TurnPort is used, given that UDP TURN port becomes
1697// ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371698TEST_F(BasicPortAllocatorTest,
1699 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1700 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1701 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1702 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
Honghai Zhangb9e7b4a2016-07-01 03:52:021703
Honghai Zhangf8998cf2019-10-14 18:27:501704 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY,
1705 true /* tcp_pruned */);
Honghai Zhangc67e0f52016-09-19 23:57:371706}
Honghai Zhangb9e7b4a2016-07-01 03:52:021707
Honghai Zhangf8998cf2019-10-14 18:27:501708// Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port
1709// will be kept regardless of the priority.
1710TEST_F(BasicPortAllocatorTest,
1711 TestUdpTurnPortPrunesTcpTurnPortIfUdpReadyFirst) {
1712 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1713 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1714 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
1715
1716 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY,
1717 true /* tcp_pruned */);
1718}
1719
1720// Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port
1721// will be kept regardless of the priority.
1722TEST_F(BasicPortAllocatorTest,
1723 TestTcpTurnPortPrunesUdpTurnPortIfTcpReadyFirst) {
1724 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1725 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1726 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
1727
1728 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY,
1729 false /* tcp_pruned */);
1730}
1731
1732// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4
1733// TurnPort will not be used if IPv6 TurnPort is used, given that IPv4 TURN port
1734// becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371735TEST_F(BasicPortAllocatorTest,
1736 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1737 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1738 // first.
1739 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1740 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
Honghai Zhangb9e7b4a2016-07-01 03:52:021741
Honghai Zhangc67e0f52016-09-19 23:57:371742 TestIPv6TurnPortPrunesIPv4TurnPort();
1743}
1744
Honghai Zhangf8998cf2019-10-14 18:27:501745// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4
1746// TurnPort will not be used if IPv6 TurnPort is used, given that IPv6 TURN port
1747// becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371748TEST_F(BasicPortAllocatorTest,
1749 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1750 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1751 // first.
1752 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1753 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1754
1755 TestIPv6TurnPortPrunesIPv4TurnPort();
Honghai Zhangb9e7b4a2016-07-01 03:52:021756}
1757
Honghai Zhangf8998cf2019-10-14 18:27:501758// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network
1759// interface will has its own set of TurnPorts based on their priorities, in the
1760// default case where no transit delay is set.
Honghai Zhangc67e0f52016-09-19 23:57:371761TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1762 TestEachInterfaceHasItsOwnTurnPorts();
1763}
Honghai Zhangb9e7b4a2016-07-01 03:52:021764
Honghai Zhangf8998cf2019-10-14 18:27:501765// Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network
1766// interface will has its own set of TurnPorts based on their priorities, given
1767// that IPv4/TCP TURN port becomes ready first.
Honghai Zhangc67e0f52016-09-19 23:57:371768TEST_F(BasicPortAllocatorTest,
1769 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1770 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1771 // becomes ready last.
1772 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1773 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1774 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1775 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
Honghai Zhangb9e7b4a2016-07-01 03:52:021776
Honghai Zhangc67e0f52016-09-19 23:57:371777 TestEachInterfaceHasItsOwnTurnPorts();
Honghai Zhangb9e7b4a2016-07-01 03:52:021778}
1779
henrike@webrtc.org269fb4b2014-10-28 22:20:111780// Testing DNS resolve for the TURN server, this will test AllocationSequence
1781// handling the unresolved address signal from TurnPort.
pthatcher1749bc32017-02-08 21:18:001782// TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1783// appears that it doesn't currently because of the DNS look up not
1784// using the fake clock.
1785TEST_F(BasicPortAllocatorTestWithRealClock,
1786 TestSharedSocketWithServerAddressResolve) {
deadbeef9a6f4d42017-05-16 02:43:331787 // This test relies on a real query for "localhost", so it won't work on an
1788 // IPv6-only machine.
1789 MAYBE_SKIP_IPV4;
henrike@webrtc.org269fb4b2014-10-28 22:20:111790 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 23:02:191791 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111792 AddInterface(kClientAddr);
Tommi3756e292023-09-01 10:45:061793 allocator_.reset(new BasicPortAllocator(&network_manager_, &socket_factory_));
Qingsi Wanga2d60672018-04-11 23:57:451794 allocator_->Initialize();
Niels Möller191e38f2019-11-04 07:49:121795 RelayServerConfig turn_server;
Taylor Brandstetter417eebe2016-05-23 23:02:191796 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 20:55:101797 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 23:02:191798 turn_server.ports.push_back(
hnsl277b2502016-12-13 13:17:231799 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
Jonas Oreland3e7e15d2022-11-03 09:02:551800 allocator_->AddTurnServerForTesting(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:111801
Taylor Brandstetter417eebe2016-05-23 23:02:191802 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:111803 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191804 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1805 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111806
Qingsi Wange53ac042018-05-08 18:55:071807 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111808 session_->StartGettingPorts();
1809
1810 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1811}
1812
1813// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1814// is allocated for udp/stun/turn. In this test we should expect all local,
1815// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111816TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111817 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 16:28:411818 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:111819
1820 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1821
1822 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191823 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1824 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111825
Qingsi Wange53ac042018-05-08 18:55:071826 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111827 session_->StartGettingPorts();
1828
deadbeef7f1563f2017-09-16 00:40:011829 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1830 kDefaultAllocationTimeout, fake_clock);
1831 EXPECT_EQ(3U, candidates_.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:111832 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051833 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1834 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1835 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1836 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1837 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
pthatcher1749bc32017-02-08 21:18:001838 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1839 kDefaultAllocationTimeout, fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:111840 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-16 00:40:011841 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1842 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:111843 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1844 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1845}
1846
1847// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1848// server is also used as the STUN server, we should get 'local', 'stun', and
1849// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 15:15:111850TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:111851 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 18:47:211852 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 16:28:411853 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:111854 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1855
1856 // Must set the step delay to 0 to make sure the relay allocation phase is
1857 // started before the STUN candidates are obtained, so that the STUN binding
1858 // response is processed when both StunPort and TurnPort exist to reproduce
1859 // webrtc issue 3537.
1860 allocator_->set_step_delay(0);
1861 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191862 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1863 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:111864
Qingsi Wange53ac042018-05-08 18:55:071865 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111866 session_->StartGettingPorts();
1867
deadbeef7f1563f2017-09-16 00:40:011868 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1869 kDefaultAllocationTimeout, fake_clock);
1870 EXPECT_EQ(3U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:051871 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311872 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051873 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1874 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1875 &stun_candidate));
1876 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1877 candidates_, "relay", "udp",
1878 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1879 stun_candidate.address()));
henrike@webrtc.org269fb4b2014-10-28 22:20:111880
henrike@webrtc.org269fb4b2014-10-28 22:20:111881 // Local port will be created first and then TURN port.
deadbeef7f1563f2017-09-16 00:40:011882 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1883 // guarantees...
henrike@webrtc.org269fb4b2014-10-28 22:20:111884 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1885 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1886}
1887
deadbeefc5d0d952015-07-16 17:22:211888// Test that when only a TCP TURN server is available, we do NOT use it as
1889// a UDP STUN server, as this could leak our IP address. Thus we should only
1890// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 15:15:111891TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 23:02:191892 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 17:22:211893 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 16:28:411894 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 17:22:211895 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1896
1897 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191898 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1899 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 17:22:211900
Qingsi Wange53ac042018-05-08 18:55:071901 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 17:22:211902 session_->StartGettingPorts();
1903
deadbeef7f1563f2017-09-16 00:40:011904 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1905 kDefaultAllocationTimeout, fake_clock);
1906 EXPECT_EQ(2U, candidates_.size());
deadbeefc5d0d952015-07-16 17:22:211907 ASSERT_EQ(2U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051908 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1909 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1910 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
deadbeefc5d0d952015-07-16 17:22:211911 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1912 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1913}
1914
1915// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1916// TURN server is used as the STUN server and we get 'local', 'stun', and
1917// 'relay' candidates.
1918// TODO(deadbeef): Remove this test when support for non-shared socket mode
1919// is removed.
Taylor Brandstettera1c30352016-05-13 15:15:111920TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 17:22:211921 AddInterface(kClientAddr);
1922 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 16:28:411923 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 17:22:211924 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1925
Taylor Brandstetter417eebe2016-05-23 23:02:191926 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 17:22:211927
Qingsi Wange53ac042018-05-08 18:55:071928 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 17:22:211929 session_->StartGettingPorts();
1930
deadbeef7f1563f2017-09-16 00:40:011931 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1932 kDefaultAllocationTimeout, fake_clock);
1933 EXPECT_EQ(3U, candidates_.size());
deadbeefc5d0d952015-07-16 17:22:211934 ASSERT_EQ(3U, ports_.size());
Jonas Olssonabbe8412018-04-03 11:40:051935 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311936 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051937 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1938 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1939 &stun_candidate));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311940 Candidate turn_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051941 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1942 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1943 &turn_candidate));
deadbeefc5d0d952015-07-16 17:22:211944 // Not using shared socket, so the STUN request's server reflexive address
1945 // should be different than the TURN request's server reflexive address.
Taylor Brandstetter8c9be5e42016-05-26 23:07:311946 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
deadbeefc5d0d952015-07-16 17:22:211947
deadbeefc5d0d952015-07-16 17:22:211948 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1949 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1950 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1951}
1952
1953// Test that even when both a STUN and TURN server are configured, the TURN
1954// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 15:15:111955TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 17:22:211956 AddInterface(kClientAddr);
1957 // Configure with STUN server but destroy it, so we can ensure that it's
1958 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 16:28:411959 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 17:22:211960 stun_server_.reset();
1961 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1962
1963 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 23:02:191964 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1965 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 17:22:211966
Qingsi Wange53ac042018-05-08 18:55:071967 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 17:22:211968 session_->StartGettingPorts();
1969
pthatcher1749bc32017-02-08 21:18:001970 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1971 fake_clock);
Jonas Olssonabbe8412018-04-03 11:40:051972 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8c9be5e42016-05-26 23:07:311973 Candidate stun_candidate;
Jonas Olssonabbe8412018-04-03 11:40:051974 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1975 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1976 &stun_candidate));
1977 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1978 candidates_, "relay", "udp",
1979 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1980 stun_candidate.address()));
deadbeefc5d0d952015-07-16 17:22:211981
1982 // Don't bother waiting for STUN timeout, since we already verified
1983 // that we got a STUN candidate from the TURN server.
1984}
1985
henrike@webrtc.org269fb4b2014-10-28 22:20:111986// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1987// and fail to generate STUN candidate, local UDP candidate is generated
1988// properly.
Taylor Brandstettera1c30352016-05-13 15:15:111989TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 23:02:191990 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1991 PORTALLOCATOR_DISABLE_TCP |
1992 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:111993 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1994 AddInterface(kClientAddr);
Qingsi Wange53ac042018-05-08 18:55:071995 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:111996 session_->StartGettingPorts();
Mirko Bonadei5f4d47b2018-08-22 17:41:221997 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
pthatcher1749bc32017-02-08 21:18:001998 fake_clock);
Mirko Bonadei5f4d47b2018-08-22 17:41:221999 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:052000 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
Taylor Brandstetter8fcf4142016-05-23 19:49:302001 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
pthatcher1749bc32017-02-08 21:18:002002 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2003 fake_clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:112004 EXPECT_EQ(1U, candidates_.size());
2005}
2006
Guo-wei Shieh47872ec2015-08-19 17:32:462007// Test that when the NetworkManager doesn't have permission to enumerate
2008// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2009// automatically.
Taylor Brandstettera1c30352016-05-13 15:15:112010TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 22:47:392011 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2012 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 17:32:462013 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 16:06:282014 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 23:02:192015 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2016 PORTALLOCATOR_DISABLE_TCP |
2017 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2018 EXPECT_EQ(0U,
2019 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Qingsi Wange53ac042018-05-08 18:55:072020 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstetter417eebe2016-05-23 23:02:192021 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 17:32:462022 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:002023 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2024 fake_clock);
Guo-wei Shieh9af97f82015-11-10 22:47:392025 EXPECT_EQ(1U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:052026 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
Taylor Brandstetter417eebe2016-05-23 23:02:192027 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 17:32:462028}
2029
henrike@webrtc.org269fb4b2014-10-28 22:20:112030// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 15:15:112031TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 23:02:192032 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2033 PORTALLOCATOR_ENABLE_IPV6 |
2034 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:112035 AddInterface(kClientIPv6Addr);
2036 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:192037 allocator_->set_step_delay(kMinimumStepDelay);
Qingsi Wange53ac042018-05-08 18:55:072038 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:112039 session_->StartGettingPorts();
deadbeef7f1563f2017-09-16 00:40:012040 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
pthatcher1749bc32017-02-08 21:18:002041 kDefaultAllocationTimeout, fake_clock);
deadbeef7f1563f2017-09-16 00:40:012042 EXPECT_EQ(4U, ports_.size());
2043 EXPECT_EQ(4U, candidates_.size());
Jonas Olssonabbe8412018-04-03 11:40:052044 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2045 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2046 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2047 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:112048}
honghaiz98db68f2015-09-29 14:58:172049
Taylor Brandstettera1c30352016-05-13 15:15:112050TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 14:58:172051 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:192052 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 18:55:072053 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 14:58:172054 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:002055 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172056 EXPECT_EQ(2U, ports_.size());
2057 session_->StopGettingPorts();
pthatcher1749bc32017-02-08 21:18:002058 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172059
2060 // After stopping getting ports, adding a new interface will not start
2061 // getting ports again.
deadbeefb60a8192016-08-24 22:15:002062 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 14:58:172063 candidates_.clear();
2064 ports_.clear();
2065 candidate_allocation_done_ = false;
2066 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 21:18:002067 SIMULATED_WAIT(false, 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172068 EXPECT_EQ(0U, candidates_.size());
2069 EXPECT_EQ(0U, ports_.size());
2070}
2071
Taylor Brandstettera1c30352016-05-13 15:15:112072TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 14:58:172073 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 23:02:192074 allocator_->set_step_delay(kDefaultStepDelay);
Qingsi Wange53ac042018-05-08 18:55:072075 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 14:58:172076 session_->StartGettingPorts();
pthatcher1749bc32017-02-08 21:18:002077 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172078 EXPECT_EQ(2U, ports_.size());
2079 session_->ClearGettingPorts();
pthatcher1749bc32017-02-08 21:18:002080 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172081
2082 // After clearing getting ports, adding a new interface will start getting
2083 // ports again.
deadbeefb60a8192016-08-24 22:15:002084 allocator_->set_step_delay(kMinimumStepDelay);
honghaiz98db68f2015-09-29 14:58:172085 candidates_.clear();
2086 ports_.clear();
2087 candidate_allocation_done_ = false;
2088 network_manager_.AddInterface(kClientAddr2);
pthatcher1749bc32017-02-08 21:18:002089 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
honghaiz98db68f2015-09-29 14:58:172090 EXPECT_EQ(2U, ports_.size());
pthatcher1749bc32017-02-08 21:18:002091 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2092 kDefaultAllocationTimeout, fake_clock);
honghaiz98db68f2015-09-29 14:58:172093}
Taylor Brandstettera1c30352016-05-13 15:15:112094
2095// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2096// a pooled session is taken out of the pool.
2097TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2098 AddInterface(kClientAddr);
2099 int pool_size = 1;
2100 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:502101 allocator_->turn_servers(), pool_size,
2102 webrtc::NO_PRUNE);
Taylor Brandstetter417eebe2016-05-23 23:02:192103 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 15:15:112104 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 21:18:002105 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2106 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstettera1c30352016-05-13 15:15:112107 // Expect that when TakePooledSession is called,
2108 // UpdateTransportInformationInternal will be called and the
2109 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2110 // candidates.
2111 session_ =
2112 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2113 ASSERT_NE(nullptr, session_.get());
2114 auto ready_ports = session_->ReadyPorts();
2115 auto candidates = session_->ReadyCandidates();
2116 EXPECT_FALSE(ready_ports.empty());
2117 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 23:02:192118 for (const PortInterface* port_interface : ready_ports) {
2119 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 15:15:112120 EXPECT_EQ(kContentName, port->content_name());
2121 EXPECT_EQ(1, port->component());
2122 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2123 EXPECT_EQ(kIcePwd0, port->password());
2124 }
Taylor Brandstetter417eebe2016-05-23 23:02:192125 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 15:15:112126 EXPECT_EQ(1, candidate.component());
2127 EXPECT_EQ(kIceUfrag0, candidate.username());
2128 EXPECT_EQ(kIcePwd0, candidate.password());
2129 }
2130}
Taylor Brandstetter417eebe2016-05-23 23:02:192131
2132// Test that a new candidate filter takes effect even on already-gathered
2133// candidates.
2134TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2135 AddInterface(kClientAddr);
2136 int pool_size = 1;
2137 allocator_->SetConfiguration(allocator_->stun_servers(),
Honghai Zhangf8998cf2019-10-14 18:27:502138 allocator_->turn_servers(), pool_size,
2139 webrtc::NO_PRUNE);
Taylor Brandstetter417eebe2016-05-23 23:02:192140 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2141 ASSERT_NE(nullptr, peeked_session);
pthatcher1749bc32017-02-08 21:18:002142 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2143 kDefaultAllocationTimeout, fake_clock);
Taylor Brandstetter417eebe2016-05-23 23:02:192144 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2145 size_t initial_ports_size = peeked_session->ReadyPorts().size();
Qingsi Wangc129c352019-04-18 17:41:582146 allocator_->SetCandidateFilter(CF_RELAY);
Taylor Brandstetter417eebe2016-05-23 23:02:192147 // Assume that when TakePooledSession is called, the candidate filter will be
2148 // applied to the pooled session. This is tested by PortAllocatorTest.
2149 session_ =
2150 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2151 ASSERT_NE(nullptr, session_.get());
2152 auto candidates = session_->ReadyCandidates();
2153 auto ports = session_->ReadyPorts();
2154 // Sanity check that the number of candidates and ports decreased.
2155 EXPECT_GT(initial_candidates_size, candidates.size());
2156 EXPECT_GT(initial_ports_size, ports.size());
2157 for (const PortInterface* port : ports) {
2158 // Expect only relay ports.
2159 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2160 }
2161 for (const Candidate& candidate : candidates) {
2162 // Expect only relay candidates now that the filter is applied.
Tommi698b4e72024-01-24 07:36:452163 EXPECT_TRUE(candidate.is_relay());
Taylor Brandstetter417eebe2016-05-23 23:02:192164 // Expect that the raddr is emptied due to the CF_RELAY filter.
2165 EXPECT_EQ(candidate.related_address(),
2166 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2167 }
2168}
2169
Qingsi Wangc129c352019-04-18 17:41:582170// Test that candidates that do not match a previous candidate filter can be
2171// surfaced if they match the new one after setting the filter value.
2172TEST_F(BasicPortAllocatorTest,
2173 SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes) {
2174 // We would still surface a host candidate if the IP is public, even though it
2175 // is disabled by the candidate filter. See
2176 // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so
2177 // that the srflx candidate is not equivalent to the host candidate.
2178 AddInterface(kPrivateAddr);
2179 ResetWithStunServerAndNat(kStunAddr);
2180
2181 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2182
2183 allocator_->set_flags(allocator().flags() |
2184 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2185 PORTALLOCATOR_DISABLE_TCP);
2186
2187 allocator_->SetCandidateFilter(CF_NONE);
2188 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2189 session_->StartGettingPorts();
2190 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2191 kDefaultAllocationTimeout, fake_clock);
2192 EXPECT_TRUE(candidates_.empty());
2193 EXPECT_TRUE(ports_.empty());
2194
2195 // Surface the relay candidate previously gathered but not signaled.
2196 session_->SetCandidateFilter(CF_RELAY);
2197 ASSERT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout,
2198 fake_clock);
Tommi698b4e72024-01-24 07:36:452199 EXPECT_TRUE(candidates_.back().is_relay());
Qingsi Wangc129c352019-04-18 17:41:582200 EXPECT_EQ(1u, ports_.size());
2201
2202 // Surface the srflx candidate previously gathered but not signaled.
2203 session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE);
2204 ASSERT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout,
2205 fake_clock);
Tommi698b4e72024-01-24 07:36:452206 EXPECT_TRUE(candidates_.back().is_stun());
Qingsi Wangc129c352019-04-18 17:41:582207 EXPECT_EQ(2u, ports_.size());
2208
2209 // Surface the srflx candidate previously gathered but not signaled.
2210 session_->SetCandidateFilter(CF_ALL);
2211 ASSERT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout,
2212 fake_clock);
Tommi698b4e72024-01-24 07:36:452213 EXPECT_TRUE(candidates_.back().is_local());
Qingsi Wangc129c352019-04-18 17:41:582214 EXPECT_EQ(2u, ports_.size());
2215}
2216
2217// This is a similar test as
2218// SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes, and we
2219// test the transitions for which the new filter value is not a super set of the
2220// previous value.
2221TEST_F(
2222 BasicPortAllocatorTest,
2223 SurfaceNewCandidatesAfterSetCandidateFilterToAllowDifferentCandidateTypes) {
2224 // We would still surface a host candidate if the IP is public, even though it
2225 // is disabled by the candidate filter. See
2226 // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so
2227 // that the srflx candidate is not equivalent to the host candidate.
2228 AddInterface(kPrivateAddr);
2229 ResetWithStunServerAndNat(kStunAddr);
2230
2231 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2232
2233 allocator_->set_flags(allocator().flags() |
2234 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2235 PORTALLOCATOR_DISABLE_TCP);
2236
2237 allocator_->SetCandidateFilter(CF_NONE);
2238 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2239 session_->StartGettingPorts();
2240 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2241 kDefaultAllocationTimeout, fake_clock);
2242 EXPECT_TRUE(candidates_.empty());
2243 EXPECT_TRUE(ports_.empty());
2244
2245 // Surface the relay candidate previously gathered but not signaled.
2246 session_->SetCandidateFilter(CF_RELAY);
2247 EXPECT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout,
2248 fake_clock);
Tommi698b4e72024-01-24 07:36:452249 EXPECT_TRUE(candidates_.back().is_relay());
Qingsi Wangc129c352019-04-18 17:41:582250 EXPECT_EQ(1u, ports_.size());
2251
2252 // Surface the srflx candidate previously gathered but not signaled.
2253 session_->SetCandidateFilter(CF_REFLEXIVE);
2254 EXPECT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout,
2255 fake_clock);
Tommi698b4e72024-01-24 07:36:452256 EXPECT_TRUE(candidates_.back().is_stun());
Qingsi Wangc129c352019-04-18 17:41:582257 EXPECT_EQ(2u, ports_.size());
2258
2259 // Surface the host candidate previously gathered but not signaled.
2260 session_->SetCandidateFilter(CF_HOST);
2261 EXPECT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout,
2262 fake_clock);
Tommi698b4e72024-01-24 07:36:452263 EXPECT_TRUE(candidates_.back().is_local());
Qingsi Wangc129c352019-04-18 17:41:582264 // We use a shared socket and cricket::UDPPort handles the srflx candidate.
2265 EXPECT_EQ(2u, ports_.size());
2266}
2267
2268// Test that after an allocation session has stopped getting ports, changing the
2269// candidate filter to allow new types of gathered candidates does not surface
2270// any candidate.
2271TEST_F(BasicPortAllocatorTest,
2272 NoCandidateSurfacedWhenUpdatingCandidateFilterIfSessionStopped) {
2273 AddInterface(kPrivateAddr);
2274 ResetWithStunServerAndNat(kStunAddr);
2275
2276 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2277
2278 allocator_->set_flags(allocator().flags() |
2279 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2280 PORTALLOCATOR_DISABLE_TCP);
2281
2282 allocator_->SetCandidateFilter(CF_NONE);
2283 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2284 session_->StartGettingPorts();
2285 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2286 kDefaultAllocationTimeout, fake_clock);
2287 auto test_invariants = [this]() {
2288 EXPECT_TRUE(candidates_.empty());
2289 EXPECT_TRUE(ports_.empty());
2290 };
2291
2292 test_invariants();
2293
2294 session_->StopGettingPorts();
2295
2296 session_->SetCandidateFilter(CF_RELAY);
2297 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2298 test_invariants();
2299
2300 session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE);
2301 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2302 test_invariants();
2303
2304 session_->SetCandidateFilter(CF_ALL);
2305 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2306 test_invariants();
2307}
2308
Qingsi Wang4ff54432018-03-02 02:25:202309TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2310 const int pool_size = 1;
2311 const int expected_stun_keepalive_interval = 123;
2312 AddInterface(kClientAddr);
Honghai Zhangf8998cf2019-10-14 18:27:502313 allocator_->SetConfiguration(
2314 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2315 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wang4ff54432018-03-02 02:25:202316 auto* pooled_session = allocator_->GetPooledSession();
2317 ASSERT_NE(nullptr, pooled_session);
2318 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2319 kDefaultAllocationTimeout, fake_clock);
2320 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2321 expected_stun_keepalive_interval);
2322}
2323
2324TEST_F(BasicPortAllocatorTest,
2325 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2326 const int pool_size = 1;
2327 AddInterface(kClientAddr);
Honghai Zhangf8998cf2019-10-14 18:27:502328 allocator_->SetConfiguration(
2329 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2330 webrtc::NO_PRUNE, nullptr, 123 /* stun keepalive interval */);
Qingsi Wang4ff54432018-03-02 02:25:202331 auto* pooled_session = allocator_->GetPooledSession();
2332 ASSERT_NE(nullptr, pooled_session);
2333 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2334 kDefaultAllocationTimeout, fake_clock);
2335 const int expected_stun_keepalive_interval = 321;
Honghai Zhangf8998cf2019-10-14 18:27:502336 allocator_->SetConfiguration(
2337 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2338 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wang4ff54432018-03-02 02:25:202339 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2340 expected_stun_keepalive_interval);
2341}
2342
2343TEST_F(BasicPortAllocatorTest,
2344 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2345 const int pool_size = 1;
2346 const int expected_stun_keepalive_interval = 123;
2347 AddInterface(kClientAddr);
2348 allocator_->set_flags(allocator().flags() |
2349 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Honghai Zhangf8998cf2019-10-14 18:27:502350 allocator_->SetConfiguration(
2351 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2352 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 18:55:072353 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-02 02:25:202354 session_->StartGettingPorts();
2355 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2356 kDefaultAllocationTimeout, fake_clock);
2357 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2358 expected_stun_keepalive_interval);
2359}
2360
2361TEST_F(BasicPortAllocatorTest,
2362 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2363 const int pool_size = 1;
2364 const int expected_stun_keepalive_interval = 123;
2365 AddInterface(kClientAddr);
2366 allocator_->set_flags(allocator().flags() &
2367 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
Honghai Zhangf8998cf2019-10-14 18:27:502368 allocator_->SetConfiguration(
2369 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2370 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
Qingsi Wange53ac042018-05-08 18:55:072371 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Qingsi Wang4ff54432018-03-02 02:25:202372 session_->StartGettingPorts();
2373 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2374 kDefaultAllocationTimeout, fake_clock);
2375 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2376 expected_stun_keepalive_interval);
2377}
2378
Qingsi Wang09619332018-09-13 05:51:552379// Test that when an mDNS responder is present, the local address of a host
Jeroen de Borst72d2ddd2018-11-27 21:20:392380// candidate is concealed by an mDNS hostname and the related address of a srflx
2381// candidate is set to 0.0.0.0 or ::0.
Qingsi Wang09619332018-09-13 05:51:552382TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) {
2383 // Default config uses GTURN and no NAT, so replace that with the
2384 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
2385 ResetWithStunServerAndNat(kStunAddr);
2386 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
2387 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2388 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
2389
2390 ASSERT_EQ(&network_manager_, allocator().network_manager());
Qingsi Wangecd30542019-05-22 21:34:562391 network_manager_.set_mdns_responder(
Mirko Bonadei317a1f02019-09-17 15:06:182392 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
Qingsi Wang09619332018-09-13 05:51:552393 AddInterface(kClientAddr);
2394 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2395 session_->StartGettingPorts();
2396 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2397 kDefaultAllocationTimeout, fake_clock);
2398 EXPECT_EQ(5u, candidates_.size());
2399 int num_host_udp_candidates = 0;
2400 int num_host_tcp_candidates = 0;
2401 int num_srflx_candidates = 0;
2402 int num_relay_candidates = 0;
2403 for (const auto& candidate : candidates_) {
Jeroen de Borst72d2ddd2018-11-27 21:20:392404 const auto& raddr = candidate.related_address();
2405
Tommi0a7fc842024-01-19 12:11:372406 if (candidate.is_local()) {
Jeroen de Borst72d2ddd2018-11-27 21:20:392407 EXPECT_FALSE(candidate.address().hostname().empty());
2408 EXPECT_TRUE(raddr.IsNil());
Qingsi Wang09619332018-09-13 05:51:552409 if (candidate.protocol() == UDP_PROTOCOL_NAME) {
2410 ++num_host_udp_candidates;
2411 } else {
2412 ++num_host_tcp_candidates;
2413 }
Tommi0a7fc842024-01-19 12:11:372414 } else if (candidate.is_stun()) {
Jeroen de Borst72d2ddd2018-11-27 21:20:392415 // For a srflx candidate, the related address should be set to 0.0.0.0 or
2416 // ::0
2417 EXPECT_TRUE(IPIsAny(raddr.ipaddr()));
2418 EXPECT_EQ(raddr.port(), 0);
2419 ++num_srflx_candidates;
Tommi0a7fc842024-01-19 12:11:372420 } else if (candidate.is_relay()) {
Jeroen de Borst72d2ddd2018-11-27 21:20:392421 EXPECT_EQ(kNatUdpAddr.ipaddr(), raddr.ipaddr());
2422 EXPECT_EQ(kNatUdpAddr.family(), raddr.family());
2423 ++num_relay_candidates;
Qingsi Wang09619332018-09-13 05:51:552424 } else {
Jeroen de Borst72d2ddd2018-11-27 21:20:392425 // prflx candidates are not expected
2426 FAIL();
Qingsi Wang09619332018-09-13 05:51:552427 }
2428 }
2429 EXPECT_EQ(1, num_host_udp_candidates);
2430 EXPECT_EQ(1, num_host_tcp_candidates);
2431 EXPECT_EQ(1, num_srflx_candidates);
2432 EXPECT_EQ(2, num_relay_candidates);
2433}
2434
Honghai Zhang6981fb52019-10-29 19:45:342435TEST_F(BasicPortAllocatorTest, TestUseTurnServerAsStunSever) {
2436 ServerAddresses stun_servers;
2437 stun_servers.insert(kStunAddr);
2438 PortConfiguration port_config(stun_servers, "", "");
2439 RelayServerConfig turn_servers =
2440 CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2441 port_config.AddRelay(turn_servers);
2442
2443 EXPECT_EQ(2U, port_config.StunServers().size());
2444}
2445
2446TEST_F(BasicPortAllocatorTest, TestDoNotUseTurnServerAsStunSever) {
Jonas Oreland340cb5e2022-03-17 08:14:162447 webrtc::test::ScopedKeyValueConfig field_trials(
Honghai Zhang6981fb52019-10-29 19:45:342448 "WebRTC-UseTurnServerAsStunServer/Disabled/");
2449 ServerAddresses stun_servers;
2450 stun_servers.insert(kStunAddr);
2451 PortConfiguration port_config(stun_servers, "" /* user_name */,
Jonas Oreland340cb5e2022-03-17 08:14:162452 "" /* password */, &field_trials);
Honghai Zhang6981fb52019-10-29 19:45:342453 RelayServerConfig turn_servers =
2454 CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2455 port_config.AddRelay(turn_servers);
2456
2457 EXPECT_EQ(1U, port_config.StunServers().size());
2458}
2459
Jonas Oreland3e7e15d2022-11-03 09:02:552460// Test that candidates from different servers get assigned a unique local
2461// preference (the middle 16 bits of the priority)
2462TEST_F(BasicPortAllocatorTest, AssignsUniqueLocalPreferencetoRelayCandidates) {
2463 allocator_->SetCandidateFilter(CF_RELAY);
2464 allocator_->AddTurnServerForTesting(
2465 CreateTurnServers(kTurnUdpIntAddr, SocketAddress()));
2466 allocator_->AddTurnServerForTesting(
2467 CreateTurnServers(kTurnUdpIntAddr, SocketAddress()));
2468 allocator_->AddTurnServerForTesting(
2469 CreateTurnServers(kTurnUdpIntAddr, SocketAddress()));
2470
2471 AddInterface(kClientAddr);
2472 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2473 session_->StartGettingPorts();
2474 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2475 kDefaultAllocationTimeout, fake_clock);
2476 EXPECT_EQ(3u, candidates_.size());
2477 EXPECT_GT((candidates_[0].priority() >> 8) & 0xFFFF,
2478 (candidates_[1].priority() >> 8) & 0xFFFF);
2479 EXPECT_GT((candidates_[1].priority() >> 8) & 0xFFFF,
2480 (candidates_[2].priority() >> 8) & 0xFFFF);
2481}
2482
Diep Bui9068f4562022-08-26 15:43:442483// Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
2484// to gather candidates.
2485TEST_F(BasicPortAllocatorTest, TwoIPv6AreSelectedBecauseOfMaxIpv6Limit) {
2486 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2487 64, rtc::ADAPTER_TYPE_WIFI);
2488 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2489 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2490 rtc::Network wifi2("wifi2", "Test NetworkAdapter 3",
2491 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI);
2492 std::vector<const rtc::Network*> networks = {&wifi1, &ethe1, &wifi2};
2493
2494 // Ensure that only 2 interfaces were selected.
2495 EXPECT_EQ(2U, BasicPortAllocatorSession::SelectIPv6Networks(
2496 networks, /*max_ipv6_networks=*/2)
2497 .size());
2498}
2499
2500// Test that if the number of available IPv6 networks is less than
2501// allocator.max_ipv6_networks(), all IPv6 networks will be selected.
2502TEST_F(BasicPortAllocatorTest, AllIPv6AreSelected) {
2503 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2504 64, rtc::ADAPTER_TYPE_WIFI);
2505 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2506 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2507 std::vector<const rtc::Network*> networks = {&wifi1, &ethe1};
2508
2509 // Ensure that all 2 interfaces were selected.
2510 EXPECT_EQ(2U, BasicPortAllocatorSession::SelectIPv6Networks(
2511 networks, /*max_ipv6_networks=*/3)
2512 .size());
2513}
2514
2515// If there are some IPv6 networks with different types, diversify IPv6
2516// networks.
2517TEST_F(BasicPortAllocatorTest, TwoIPv6WifiAreSelectedIfThereAreTwo) {
2518 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2519 64, rtc::ADAPTER_TYPE_WIFI);
2520 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2521 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2522 rtc::Network ethe2("ethe2", "Test NetworkAdapter 3",
2523 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2524 rtc::Network unknown1("unknown1", "Test NetworkAdapter 4",
2525 kClientIPv6Addr2.ipaddr(), 64,
2526 rtc::ADAPTER_TYPE_UNKNOWN);
2527 rtc::Network cell1("cell1", "Test NetworkAdapter 5",
2528 kClientIPv6Addr3.ipaddr(), 64,
2529 rtc::ADAPTER_TYPE_CELLULAR_4G);
2530 std::vector<const rtc::Network*> networks = {&wifi1, &ethe1, &ethe2,
2531 &unknown1, &cell1};
2532
2533 networks = BasicPortAllocatorSession::SelectIPv6Networks(
2534 networks, /*max_ipv6_networks=*/4);
2535
2536 EXPECT_EQ(4U, networks.size());
2537 // Ensure the expected 4 interfaces (wifi1, ethe1, cell1, unknown1) were
2538 // selected.
2539 EXPECT_TRUE(HasNetwork(networks, wifi1));
2540 EXPECT_TRUE(HasNetwork(networks, ethe1));
2541 EXPECT_TRUE(HasNetwork(networks, cell1));
2542 EXPECT_TRUE(HasNetwork(networks, unknown1));
2543}
2544
2545// If there are some IPv6 networks with the same type, select them because there
2546// is no other option.
2547TEST_F(BasicPortAllocatorTest, IPv6WithSameTypeAreSelectedIfNoOtherOption) {
2548 // Add 5 cellular interfaces
2549 rtc::Network cell1("cell1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2550 64, rtc::ADAPTER_TYPE_CELLULAR_2G);
2551 rtc::Network cell2("cell2", "Test NetworkAdapter 2",
2552 kClientIPv6Addr2.ipaddr(), 64,
2553 rtc::ADAPTER_TYPE_CELLULAR_3G);
2554 rtc::Network cell3("cell3", "Test NetworkAdapter 3",
2555 kClientIPv6Addr3.ipaddr(), 64,
2556 rtc::ADAPTER_TYPE_CELLULAR_4G);
2557 rtc::Network cell4("cell4", "Test NetworkAdapter 4",
2558 kClientIPv6Addr2.ipaddr(), 64,
2559 rtc::ADAPTER_TYPE_CELLULAR_5G);
2560 rtc::Network cell5("cell5", "Test NetworkAdapter 5",
2561 kClientIPv6Addr3.ipaddr(), 64,
2562 rtc::ADAPTER_TYPE_CELLULAR_3G);
2563 std::vector<const rtc::Network*> networks = {&cell1, &cell2, &cell3, &cell4,
2564 &cell5};
2565
2566 // Ensure that 4 interfaces were selected.
2567 EXPECT_EQ(4U, BasicPortAllocatorSession::SelectIPv6Networks(
2568 networks, /*max_ipv6_networks=*/4)
2569 .size());
2570}
2571
2572TEST_F(BasicPortAllocatorTest, IPv6EthernetHasHigherPriorityThanWifi) {
2573 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2574 64, rtc::ADAPTER_TYPE_WIFI);
2575 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2576 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2577 rtc::Network wifi2("wifi2", "Test NetworkAdapter 3",
2578 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI);
2579 std::vector<const rtc::Network*> networks = {&wifi1, &ethe1, &wifi2};
2580
2581 networks = BasicPortAllocatorSession::SelectIPv6Networks(
2582 networks, /*max_ipv6_networks=*/1);
2583
2584 EXPECT_EQ(1U, networks.size());
2585 // Ensure ethe1 was selected.
2586 EXPECT_TRUE(HasNetwork(networks, ethe1));
2587}
2588
2589TEST_F(BasicPortAllocatorTest, IPv6EtherAndWifiHaveHigherPriorityThanOthers) {
2590 rtc::Network cell1("cell1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2591 64, rtc::ADAPTER_TYPE_CELLULAR_3G);
2592 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2593 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2594 rtc::Network wifi1("wifi1", "Test NetworkAdapter 3",
2595 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI);
2596 rtc::Network unknown("unknown", "Test NetworkAdapter 4",
2597 kClientIPv6Addr2.ipaddr(), 64,
2598 rtc::ADAPTER_TYPE_UNKNOWN);
2599 rtc::Network vpn1("vpn1", "Test NetworkAdapter 5", kClientIPv6Addr3.ipaddr(),
2600 64, rtc::ADAPTER_TYPE_VPN);
2601 std::vector<const rtc::Network*> networks = {&cell1, &ethe1, &wifi1, &unknown,
2602 &vpn1};
2603
2604 networks = BasicPortAllocatorSession::SelectIPv6Networks(
2605 networks, /*max_ipv6_networks=*/2);
2606
2607 EXPECT_EQ(2U, networks.size());
2608 // Ensure ethe1 and wifi1 were selected.
2609 EXPECT_TRUE(HasNetwork(networks, wifi1));
2610 EXPECT_TRUE(HasNetwork(networks, ethe1));
2611}
2612
Sameer Vijaykardf7df192023-04-24 09:05:442613TEST_F(BasicPortAllocatorTest, Select2DifferentIntefaces) {
Diep Bui9068f4562022-08-26 15:43:442614 allocator().set_max_ipv6_networks(2);
2615 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2616 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2617 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2618 AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI);
2619 AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G);
2620
2621 // To simplify the test, only gather UDP host candidates.
2622 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2623 PORTALLOCATOR_DISABLE_STUN |
2624 PORTALLOCATOR_DISABLE_RELAY |
2625 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2626
2627 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2628 session_->StartGettingPorts();
2629 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2630 kDefaultAllocationTimeout, fake_clock);
2631
2632 EXPECT_EQ(2U, candidates_.size());
2633 // ethe1 and wifi1 were selected.
2634 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2635 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2636}
2637
Sameer Vijaykardf7df192023-04-24 09:05:442638TEST_F(BasicPortAllocatorTest, Select3DifferentIntefaces) {
Diep Bui9068f4562022-08-26 15:43:442639 allocator().set_max_ipv6_networks(3);
2640 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2641 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2642 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2643 AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI);
2644 AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G);
2645
2646 // To simplify the test, only gather UDP host candidates.
2647 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2648 PORTALLOCATOR_DISABLE_STUN |
2649 PORTALLOCATOR_DISABLE_RELAY |
2650 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2651
2652 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2653 session_->StartGettingPorts();
2654 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2655 kDefaultAllocationTimeout, fake_clock);
2656
2657 EXPECT_EQ(3U, candidates_.size());
2658 // ethe1, wifi1, and cell1 were selected.
2659 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2660 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2661 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr5));
2662}
2663
Sameer Vijaykardf7df192023-04-24 09:05:442664TEST_F(BasicPortAllocatorTest, Select4DifferentIntefaces) {
Diep Bui9068f4562022-08-26 15:43:442665 allocator().set_max_ipv6_networks(4);
2666 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2667 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2668 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2669 AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI);
2670 AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G);
2671
2672 // To simplify the test, only gather UDP host candidates.
2673 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2674 PORTALLOCATOR_DISABLE_STUN |
2675 PORTALLOCATOR_DISABLE_RELAY |
2676 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2677
2678 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2679 session_->StartGettingPorts();
2680 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2681 kDefaultAllocationTimeout, fake_clock);
2682
2683 EXPECT_EQ(4U, candidates_.size());
2684 // ethe1, ethe2, wifi1, and cell1 were selected.
2685 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2686 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
2687 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2688 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr5));
2689}
2690
Taylor Brandstetter417eebe2016-05-23 23:02:192691} // namespace cricket