1 /*
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
11 #include "p2p/client/basic_port_allocator.h"
12
13 #include <memory>
14 #include <ostream> // no-presubmit-check TODO(webrtc:8982)
15
16 #include "absl/algorithm/container.h"
17 #include "absl/strings/string_view.h"
18 #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"
23 #include "p2p/base/test_stun_server.h"
24 #include "p2p/base/test_turn_server.h"
25 #include "rtc_base/fake_clock.h"
26 #include "rtc_base/fake_mdns_responder.h"
27 #include "rtc_base/fake_network.h"
28 #include "rtc_base/firewall_socket_server.h"
29 #include "rtc_base/gunit.h"
30 #include "rtc_base/ip_address.h"
31 #include "rtc_base/logging.h"
32 #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"
37 #include "rtc_base/network.h"
38 #include "rtc_base/network_constants.h"
39 #include "rtc_base/network_monitor.h"
40 #include "rtc_base/socket.h"
41 #include "rtc_base/socket_address.h"
42 #include "rtc_base/socket_address_pair.h"
43 #include "rtc_base/thread.h"
44 #include "rtc_base/virtual_socket_server.h"
45 #include "system_wrappers/include/metrics.h"
46 #include "test/gmock.h"
47 #include "test/gtest.h"
48 #include "test/scoped_key_value_config.h"
49
50 using rtc::IPAddress;
51 using rtc::SocketAddress;
52 using ::testing::Contains;
53 using ::testing::Not;
54
55 #define MAYBE_SKIP_IPV4 \
56 if (!rtc::HasIPv4Enabled()) { \
57 RTC_LOG(LS_INFO) << "No IPv4... skipping"; \
58 return; \
59 }
60
61 static const SocketAddress kAnyAddr("0.0.0.0", 0);
62 static const SocketAddress kClientAddr("11.11.11.11", 0);
63 static const SocketAddress kClientAddr2("22.22.22.22", 0);
64 static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
65 static const SocketAddress kPrivateAddr("192.168.1.11", 0);
66 static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
67 static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
68 0);
69 static const SocketAddress kClientIPv6Addr2(
70 "2401:fa00:4:2000:be30:5bff:fee5:c3",
71 0);
72 static const SocketAddress kClientIPv6Addr3(
73 "2401:fa00:4:3000:be30:5bff:fee5:c3",
74 0);
75 static const SocketAddress kClientIPv6Addr4(
76 "2401:fa00:4:4000:be30:5bff:fee5:c3",
77 0);
78 static const SocketAddress kClientIPv6Addr5(
79 "2401:fa00:4:5000:be30:5bff:fee5:c3",
80 0);
81 static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
82 static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
83 static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
84 static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
85 static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
86 static const SocketAddress kTurnUdpIntIPv6Addr(
87 "2402:fb00:4:1000:be30:5bff:fee5:c3",
88 3479);
89 static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
90 static const SocketAddress kTurnTcpIntIPv6Addr(
91 "2402:fb00:4:2000:be30:5bff:fee5:c3",
92 3479);
93 static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
94
95 // Minimum and maximum port for port range tests.
96 static const int kMinPort = 10000;
97 static const int kMaxPort = 10099;
98
99 // Based on ICE_UFRAG_LENGTH
100 static const char kIceUfrag0[] = "UF00";
101 // Based on ICE_PWD_LENGTH
102 static const char kIcePwd0[] = "TESTICEPWD00000000000000";
103
104 static const char kContentName[] = "test content";
105
106 static const int kDefaultAllocationTimeout = 3000;
107 static const char kTurnUsername[] = "test";
108 static const char kTurnPassword[] = "test";
109
110 // STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT.
111 // Add some margin of error for slow bots.
112 static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT;
113
114 constexpr uint64_t kTiebreakerDefault = 44444;
115
116 namespace {
117
CheckStunKeepaliveIntervalOfAllReadyPorts(const cricket::PortAllocatorSession * allocator_session,int expected)118 void CheckStunKeepaliveIntervalOfAllReadyPorts(
119 const cricket::PortAllocatorSession* allocator_session,
120 int expected) {
121 auto ready_ports = allocator_session->ReadyPorts();
122 for (const auto* port : ready_ports) {
123 if (port->Type() == cricket::STUN_PORT_TYPE ||
124 (port->Type() == cricket::LOCAL_PORT_TYPE &&
125 port->GetProtocol() == cricket::PROTO_UDP)) {
126 EXPECT_EQ(
127 static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(),
128 expected);
129 }
130 }
131 }
132
133 } // namespace
134
135 namespace cricket {
136
137 // Helper for dumping candidates
operator <<(std::ostream & os,const std::vector<Candidate> & candidates)138 std::ostream& operator<<(std::ostream& os,
139 const std::vector<Candidate>& candidates) {
140 os << '[';
141 bool first = true;
142 for (const Candidate& c : candidates) {
143 if (!first) {
144 os << ", ";
145 }
146 os << c.ToString();
147 first = false;
148 }
149 os << ']';
150 return os;
151 }
152
153 class BasicPortAllocatorTestBase : public ::testing::Test,
154 public sigslot::has_slots<> {
155 public:
BasicPortAllocatorTestBase()156 BasicPortAllocatorTestBase()
157 : vss_(new rtc::VirtualSocketServer()),
158 fss_(new rtc::FirewallSocketServer(vss_.get())),
159 thread_(fss_.get()),
160 // Note that the NAT is not used by default. ResetWithStunServerAndNat
161 // must be called.
162 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
163 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
164 stun_server_(TestStunServer::Create(fss_.get(), kStunAddr)),
165 turn_server_(rtc::Thread::Current(),
166 fss_.get(),
167 kTurnUdpIntAddr,
168 kTurnUdpExtAddr),
169 candidate_allocation_done_(false) {
170 ServerAddresses stun_servers;
171 stun_servers.insert(kStunAddr);
172
173 allocator_ = std::make_unique<BasicPortAllocator>(
174 &network_manager_,
175 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get()),
176 stun_servers, &field_trials_);
177 allocator_->Initialize();
178 allocator_->set_step_delay(kMinimumStepDelay);
179 allocator_->SetIceTiebreaker(kTiebreakerDefault);
180 webrtc::metrics::Reset();
181 }
182
AddInterface(const SocketAddress & addr)183 void AddInterface(const SocketAddress& addr) {
184 network_manager_.AddInterface(addr);
185 }
AddInterface(const SocketAddress & addr,absl::string_view if_name)186 void AddInterface(const SocketAddress& addr, absl::string_view if_name) {
187 network_manager_.AddInterface(addr, if_name);
188 }
AddInterface(const SocketAddress & addr,absl::string_view if_name,rtc::AdapterType type)189 void AddInterface(const SocketAddress& addr,
190 absl::string_view if_name,
191 rtc::AdapterType type) {
192 network_manager_.AddInterface(addr, if_name, type);
193 }
194 // The default source address is the public address that STUN server will
195 // observe when the endpoint is sitting on the public internet and the local
196 // port is bound to the "any" address. Intended for simulating the situation
197 // that client binds the "any" address, and that's also the address returned
198 // by getsockname/GetLocalAddress, so that the client can learn the actual
199 // local address only from the STUN response.
AddInterfaceAsDefaultSourceAddresss(const SocketAddress & addr)200 void AddInterfaceAsDefaultSourceAddresss(const SocketAddress& addr) {
201 AddInterface(addr);
202 // When a binding comes from the any address, the `addr` will be used as the
203 // srflx address.
204 vss_->SetDefaultSourceAddress(addr.ipaddr());
205 }
RemoveInterface(const SocketAddress & addr)206 void RemoveInterface(const SocketAddress& addr) {
207 network_manager_.RemoveInterface(addr);
208 }
SetPortRange(int min_port,int max_port)209 bool SetPortRange(int min_port, int max_port) {
210 return allocator_->SetPortRange(min_port, max_port);
211 }
212 // Endpoint is on the public network. No STUN or TURN.
ResetWithNoServersOrNat()213 void ResetWithNoServersOrNat() {
214 allocator_.reset(new BasicPortAllocator(
215 &network_manager_,
216 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get())));
217 allocator_->Initialize();
218 allocator_->SetIceTiebreaker(kTiebreakerDefault);
219 allocator_->set_step_delay(kMinimumStepDelay);
220 }
221 // Endpoint is behind a NAT, with STUN specified.
ResetWithStunServerAndNat(const rtc::SocketAddress & stun_server)222 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
223 ResetWithStunServer(stun_server, true);
224 }
225 // Endpoint is on the public network, with STUN specified.
ResetWithStunServerNoNat(const rtc::SocketAddress & stun_server)226 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
227 ResetWithStunServer(stun_server, false);
228 }
229 // Endpoint is on the public network, with TURN specified.
ResetWithTurnServersNoNat(const rtc::SocketAddress & udp_turn,const rtc::SocketAddress & tcp_turn)230 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
231 const rtc::SocketAddress& tcp_turn) {
232 ResetWithNoServersOrNat();
233 AddTurnServers(udp_turn, tcp_turn);
234 }
235
CreateTurnServers(const rtc::SocketAddress & udp_turn,const rtc::SocketAddress & tcp_turn)236 RelayServerConfig CreateTurnServers(const rtc::SocketAddress& udp_turn,
237 const rtc::SocketAddress& tcp_turn) {
238 RelayServerConfig turn_server;
239 RelayCredentials credentials(kTurnUsername, kTurnPassword);
240 turn_server.credentials = credentials;
241
242 if (!udp_turn.IsNil()) {
243 turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP));
244 }
245 if (!tcp_turn.IsNil()) {
246 turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP));
247 }
248 return turn_server;
249 }
250
AddTurnServers(const rtc::SocketAddress & udp_turn,const rtc::SocketAddress & tcp_turn)251 void AddTurnServers(const rtc::SocketAddress& udp_turn,
252 const rtc::SocketAddress& tcp_turn) {
253 RelayServerConfig turn_server = CreateTurnServers(udp_turn, tcp_turn);
254 allocator_->AddTurnServerForTesting(turn_server);
255 }
256
CreateSession(int component)257 bool CreateSession(int component) {
258 session_ = CreateSession("session", component);
259 if (!session_) {
260 return false;
261 }
262 return true;
263 }
264
CreateSession(int component,absl::string_view content_name)265 bool CreateSession(int component, absl::string_view content_name) {
266 session_ = CreateSession("session", content_name, component);
267 if (!session_) {
268 return false;
269 }
270 return true;
271 }
272
CreateSession(absl::string_view sid,int component)273 std::unique_ptr<PortAllocatorSession> CreateSession(absl::string_view sid,
274 int component) {
275 return CreateSession(sid, kContentName, component);
276 }
277
CreateSession(absl::string_view sid,absl::string_view content_name,int component)278 std::unique_ptr<PortAllocatorSession> CreateSession(
279 absl::string_view sid,
280 absl::string_view content_name,
281 int component) {
282 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
283 }
284
CreateSession(absl::string_view sid,absl::string_view content_name,int component,absl::string_view ice_ufrag,absl::string_view ice_pwd)285 std::unique_ptr<PortAllocatorSession> CreateSession(
286 absl::string_view sid,
287 absl::string_view content_name,
288 int component,
289 absl::string_view ice_ufrag,
290 absl::string_view ice_pwd) {
291 std::unique_ptr<PortAllocatorSession> session =
292 allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
293 session->SignalPortReady.connect(this,
294 &BasicPortAllocatorTestBase::OnPortReady);
295 session->SignalPortsPruned.connect(
296 this, &BasicPortAllocatorTestBase::OnPortsPruned);
297 session->SignalCandidatesReady.connect(
298 this, &BasicPortAllocatorTestBase::OnCandidatesReady);
299 session->SignalCandidatesRemoved.connect(
300 this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
301 session->SignalCandidatesAllocationDone.connect(
302 this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
303 session->set_ice_tiebreaker(kTiebreakerDefault);
304 return session;
305 }
306
307 // Return true if the addresses are the same, or the port is 0 in `pattern`
308 // (acting as a wildcard) and the IPs are the same.
309 // Even with a wildcard port, the port of the address should be nonzero if
310 // the IP is nonzero.
AddressMatch(const SocketAddress & address,const SocketAddress & pattern)311 static bool AddressMatch(const SocketAddress& address,
312 const SocketAddress& pattern) {
313 return address.ipaddr() == pattern.ipaddr() &&
314 ((pattern.port() == 0 &&
315 (address.port() != 0 || IPIsAny(address.ipaddr()))) ||
316 (pattern.port() != 0 && address.port() == pattern.port()));
317 }
318
319 // Returns the number of ports that have matching type, protocol and
320 // address.
CountPorts(const std::vector<PortInterface * > & ports,absl::string_view type,ProtocolType protocol,const SocketAddress & client_addr)321 static int CountPorts(const std::vector<PortInterface*>& ports,
322 absl::string_view type,
323 ProtocolType protocol,
324 const SocketAddress& client_addr) {
325 return absl::c_count_if(
326 ports, [type, protocol, client_addr](PortInterface* port) {
327 return port->Type() == type && port->GetProtocol() == protocol &&
328 port->Network()->GetBestIP() == client_addr.ipaddr();
329 });
330 }
331
CountCandidates(const std::vector<Candidate> & candidates,absl::string_view type,absl::string_view proto,const SocketAddress & addr)332 static int CountCandidates(const std::vector<Candidate>& candidates,
333 absl::string_view type,
334 absl::string_view proto,
335 const SocketAddress& addr) {
336 return absl::c_count_if(
337 candidates, [type, proto, addr](const Candidate& c) {
338 return c.type() == type && c.protocol() == proto &&
339 AddressMatch(c.address(), addr);
340 });
341 }
342
343 // Find a candidate and return it.
FindCandidate(const std::vector<Candidate> & candidates,absl::string_view type,absl::string_view proto,const SocketAddress & addr,Candidate * found)344 static bool FindCandidate(const std::vector<Candidate>& candidates,
345 absl::string_view type,
346 absl::string_view proto,
347 const SocketAddress& addr,
348 Candidate* found) {
349 auto it =
350 absl::c_find_if(candidates, [type, proto, addr](const Candidate& c) {
351 return c.type() == type && c.protocol() == proto &&
352 AddressMatch(c.address(), addr);
353 });
354 if (it != candidates.end() && found) {
355 *found = *it;
356 }
357 return it != candidates.end();
358 }
359
360 // Convenience method to call FindCandidate with no return.
HasCandidate(const std::vector<Candidate> & candidates,absl::string_view type,absl::string_view proto,const SocketAddress & addr)361 static bool HasCandidate(const std::vector<Candidate>& candidates,
362 absl::string_view type,
363 absl::string_view proto,
364 const SocketAddress& addr) {
365 return FindCandidate(candidates, type, proto, addr, nullptr);
366 }
367
368 // Version of HasCandidate that also takes a related address.
HasCandidateWithRelatedAddr(const std::vector<Candidate> & candidates,absl::string_view type,absl::string_view proto,const SocketAddress & addr,const SocketAddress & related_addr)369 static bool HasCandidateWithRelatedAddr(
370 const std::vector<Candidate>& candidates,
371 absl::string_view type,
372 absl::string_view proto,
373 const SocketAddress& addr,
374 const SocketAddress& related_addr) {
375 return absl::c_any_of(
376 candidates, [type, proto, addr, related_addr](const Candidate& c) {
377 return c.type() == type && c.protocol() == proto &&
378 AddressMatch(c.address(), addr) &&
379 AddressMatch(c.related_address(), related_addr);
380 });
381 }
382
CheckPort(const rtc::SocketAddress & addr,int min_port,int max_port)383 static bool CheckPort(const rtc::SocketAddress& addr,
384 int min_port,
385 int max_port) {
386 return (addr.port() >= min_port && addr.port() <= max_port);
387 }
388
HasNetwork(const std::vector<const rtc::Network * > & networks,const rtc::Network & to_be_found)389 static bool HasNetwork(const std::vector<const rtc::Network*>& networks,
390 const rtc::Network& to_be_found) {
391 auto it =
392 absl::c_find_if(networks, [to_be_found](const rtc::Network* network) {
393 return network->description() == to_be_found.description() &&
394 network->name() == to_be_found.name() &&
395 network->prefix() == to_be_found.prefix();
396 });
397 return it != networks.end();
398 }
399
OnCandidatesAllocationDone(PortAllocatorSession * session)400 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
401 // We should only get this callback once, except in the mux test where
402 // we have multiple port allocation sessions.
403 if (session == session_.get()) {
404 ASSERT_FALSE(candidate_allocation_done_);
405 candidate_allocation_done_ = true;
406 }
407 EXPECT_TRUE(session->CandidatesAllocationDone());
408 }
409
410 // Check if all ports allocated have send-buffer size `expected`. If
411 // `expected` == -1, check if GetOptions returns SOCKET_ERROR.
CheckSendBufferSizesOfAllPorts(int expected)412 void CheckSendBufferSizesOfAllPorts(int expected) {
413 std::vector<PortInterface*>::iterator it;
414 for (it = ports_.begin(); it < ports_.end(); ++it) {
415 int send_buffer_size;
416 if (expected == -1) {
417 EXPECT_EQ(SOCKET_ERROR,
418 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
419 } else {
420 EXPECT_EQ(0,
421 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
422 ASSERT_EQ(expected, send_buffer_size);
423 }
424 }
425 }
426
virtual_socket_server()427 rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); }
428
429 protected:
allocator()430 BasicPortAllocator& allocator() { return *allocator_; }
431
OnPortReady(PortAllocatorSession * ses,PortInterface * port)432 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
433 RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString();
434 ports_.push_back(port);
435 // Make sure the new port is added to ReadyPorts.
436 auto ready_ports = ses->ReadyPorts();
437 EXPECT_THAT(ready_ports, Contains(port));
438 }
OnPortsPruned(PortAllocatorSession * ses,const std::vector<PortInterface * > & pruned_ports)439 void OnPortsPruned(PortAllocatorSession* ses,
440 const std::vector<PortInterface*>& pruned_ports) {
441 RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size();
442 auto ready_ports = ses->ReadyPorts();
443 auto new_end = ports_.end();
444 for (PortInterface* port : pruned_ports) {
445 new_end = std::remove(ports_.begin(), new_end, port);
446 // Make sure the pruned port is not in ReadyPorts.
447 EXPECT_THAT(ready_ports, Not(Contains(port)));
448 }
449 ports_.erase(new_end, ports_.end());
450 }
451
OnCandidatesReady(PortAllocatorSession * ses,const std::vector<Candidate> & candidates)452 void OnCandidatesReady(PortAllocatorSession* ses,
453 const std::vector<Candidate>& candidates) {
454 for (const Candidate& candidate : candidates) {
455 RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString();
456 // Sanity check that the ICE component is set.
457 EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component());
458 candidates_.push_back(candidate);
459 }
460 // Make sure the new candidates are added to Candidates.
461 auto ses_candidates = ses->ReadyCandidates();
462 for (const Candidate& candidate : candidates) {
463 EXPECT_THAT(ses_candidates, Contains(candidate));
464 }
465 }
466
OnCandidatesRemoved(PortAllocatorSession * session,const std::vector<Candidate> & removed_candidates)467 void OnCandidatesRemoved(PortAllocatorSession* session,
468 const std::vector<Candidate>& removed_candidates) {
469 auto new_end = std::remove_if(
470 candidates_.begin(), candidates_.end(),
471 [removed_candidates](Candidate& candidate) {
472 for (const Candidate& removed_candidate : removed_candidates) {
473 if (candidate.MatchesForRemoval(removed_candidate)) {
474 return true;
475 }
476 }
477 return false;
478 });
479 candidates_.erase(new_end, candidates_.end());
480 }
481
HasRelayAddress(const ProtocolAddress & proto_addr)482 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
483 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
484 RelayServerConfig server_config = allocator_->turn_servers()[i];
485 PortList::const_iterator relay_port;
486 for (relay_port = server_config.ports.begin();
487 relay_port != server_config.ports.end(); ++relay_port) {
488 if (proto_addr.address == relay_port->address &&
489 proto_addr.proto == relay_port->proto)
490 return true;
491 }
492 }
493 return false;
494 }
495
ResetWithStunServer(const rtc::SocketAddress & stun_server,bool with_nat)496 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
497 bool with_nat) {
498 if (with_nat) {
499 nat_server_.reset(new rtc::NATServer(
500 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
501 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
502 } else {
503 nat_socket_factory_ =
504 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get());
505 }
506
507 ServerAddresses stun_servers;
508 if (!stun_server.IsNil()) {
509 stun_servers.insert(stun_server);
510 }
511 allocator_.reset(new BasicPortAllocator(&network_manager_,
512 nat_socket_factory_.get(),
513 stun_servers, &field_trials_));
514 allocator_->Initialize();
515 allocator_->set_step_delay(kMinimumStepDelay);
516 }
517
518 std::unique_ptr<rtc::VirtualSocketServer> vss_;
519 std::unique_ptr<rtc::FirewallSocketServer> fss_;
520 rtc::AutoSocketServerThread thread_;
521 std::unique_ptr<rtc::NATServer> nat_server_;
522 rtc::NATSocketFactory nat_factory_;
523 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
524 std::unique_ptr<TestStunServer> stun_server_;
525 TestTurnServer turn_server_;
526 rtc::FakeNetworkManager network_manager_;
527 std::unique_ptr<BasicPortAllocator> allocator_;
528 std::unique_ptr<PortAllocatorSession> session_;
529 std::vector<PortInterface*> ports_;
530 std::vector<Candidate> candidates_;
531 bool candidate_allocation_done_;
532 webrtc::test::ScopedKeyValueConfig field_trials_;
533 };
534
535 class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase {
536 };
537
538 class FakeClockBase {
539 public:
540 rtc::ScopedFakeClock fake_clock;
541 };
542
543 class BasicPortAllocatorTest : public FakeClockBase,
544 public BasicPortAllocatorTestBase {
545 public:
546 // This function starts the port/address gathering and check the existence of
547 // candidates as specified. When `expect_stun_candidate` is true,
548 // `stun_candidate_addr` carries the expected reflective address, which is
549 // also the related address for TURN candidate if it is expected. Otherwise,
550 // it should be ignore.
CheckDisableAdapterEnumeration(uint32_t total_ports,const rtc::IPAddress & host_candidate_addr,const rtc::IPAddress & stun_candidate_addr,const rtc::IPAddress & relay_candidate_udp_transport_addr,const rtc::IPAddress & relay_candidate_tcp_transport_addr)551 void CheckDisableAdapterEnumeration(
552 uint32_t total_ports,
553 const rtc::IPAddress& host_candidate_addr,
554 const rtc::IPAddress& stun_candidate_addr,
555 const rtc::IPAddress& relay_candidate_udp_transport_addr,
556 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
557 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
558 rtc::IPAddress());
559 if (!session_) {
560 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
561 }
562 session_->set_flags(session_->flags() |
563 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
564 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
565 allocator().set_allow_tcp_listen(false);
566 session_->StartGettingPorts();
567 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
568 kDefaultAllocationTimeout, fake_clock);
569
570 uint32_t total_candidates = 0;
571 if (!host_candidate_addr.IsNil()) {
572 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp",
573 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0)));
574 ++total_candidates;
575 }
576 if (!stun_candidate_addr.IsNil()) {
577 rtc::SocketAddress related_address(host_candidate_addr, 0);
578 if (host_candidate_addr.IsNil()) {
579 related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family()));
580 }
581 EXPECT_TRUE(HasCandidateWithRelatedAddr(
582 candidates_, "stun", "udp",
583 rtc::SocketAddress(stun_candidate_addr, 0), related_address));
584 ++total_candidates;
585 }
586 if (!relay_candidate_udp_transport_addr.IsNil()) {
587 EXPECT_TRUE(HasCandidateWithRelatedAddr(
588 candidates_, "relay", "udp",
589 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0),
590 rtc::SocketAddress(stun_candidate_addr, 0)));
591 ++total_candidates;
592 }
593 if (!relay_candidate_tcp_transport_addr.IsNil()) {
594 EXPECT_TRUE(HasCandidateWithRelatedAddr(
595 candidates_, "relay", "udp",
596 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0),
597 rtc::SocketAddress(stun_candidate_addr, 0)));
598 ++total_candidates;
599 }
600
601 EXPECT_EQ(total_candidates, candidates_.size());
602 EXPECT_EQ(total_ports, ports_.size());
603 }
604
TestIPv6TurnPortPrunesIPv4TurnPort()605 void TestIPv6TurnPortPrunesIPv4TurnPort() {
606 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
607 // Add two IP addresses on the same interface.
608 AddInterface(kClientAddr, "net1");
609 AddInterface(kClientIPv6Addr, "net1");
610 allocator_.reset(new BasicPortAllocator(
611 &network_manager_,
612 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get())));
613 allocator_->Initialize();
614 allocator_->SetConfiguration(allocator_->stun_servers(),
615 allocator_->turn_servers(), 0,
616 webrtc::PRUNE_BASED_ON_PRIORITY);
617 AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress());
618 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
619
620 allocator_->set_step_delay(kMinimumStepDelay);
621 allocator_->set_flags(
622 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
623 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP);
624
625 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
626 session_->StartGettingPorts();
627 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
628 kDefaultAllocationTimeout, fake_clock);
629 // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready.
630 EXPECT_EQ(3U, session_->ReadyPorts().size());
631 EXPECT_EQ(3U, ports_.size());
632 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
633 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
634 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
635 EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
636
637 // Now that we remove candidates when a TURN port is pruned, there will be
638 // exactly 3 candidates in both `candidates_` and `ready_candidates`.
639 EXPECT_EQ(3U, candidates_.size());
640 const std::vector<Candidate>& ready_candidates =
641 session_->ReadyCandidates();
642 EXPECT_EQ(3U, ready_candidates.size());
643 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
644 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
645 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
646 }
647
TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PortPrunePolicy prune_policy,bool tcp_pruned)648 void TestTurnPortPrunesWithUdpAndTcpPorts(
649 webrtc::PortPrunePolicy prune_policy,
650 bool tcp_pruned) {
651 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
652 AddInterface(kClientAddr);
653 allocator_.reset(new BasicPortAllocator(
654 &network_manager_,
655 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get())));
656 allocator_->Initialize();
657 allocator_->SetConfiguration(allocator_->stun_servers(),
658 allocator_->turn_servers(), 0, prune_policy);
659 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
660 allocator_->set_step_delay(kMinimumStepDelay);
661 allocator_->set_flags(allocator().flags() |
662 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
663 PORTALLOCATOR_DISABLE_TCP);
664
665 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
666 session_->StartGettingPorts();
667 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
668 kDefaultAllocationTimeout, fake_clock);
669 // Only 2 ports (one STUN and one TURN) are actually being used.
670 EXPECT_EQ(2U, session_->ReadyPorts().size());
671 // We have verified that each port, when it is added to `ports_`, it is
672 // found in `ready_ports`, and when it is pruned, it is not found in
673 // `ready_ports`, so we only need to verify the content in one of them.
674 EXPECT_EQ(2U, ports_.size());
675 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
676 int num_udp_ports = tcp_pruned ? 1 : 0;
677 EXPECT_EQ(num_udp_ports,
678 CountPorts(ports_, "relay", PROTO_UDP, kClientAddr));
679 EXPECT_EQ(1 - num_udp_ports,
680 CountPorts(ports_, "relay", PROTO_TCP, kClientAddr));
681
682 // Now that we remove candidates when a TURN port is pruned, `candidates_`
683 // should only contains two candidates regardless whether the TCP TURN port
684 // is created before or after the UDP turn port.
685 EXPECT_EQ(2U, candidates_.size());
686 // There will only be 2 candidates in `ready_candidates` because it only
687 // includes the candidates in the ready ports.
688 const std::vector<Candidate>& ready_candidates =
689 session_->ReadyCandidates();
690 EXPECT_EQ(2U, ready_candidates.size());
691 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
692
693 // The external candidate is always udp.
694 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
695 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
696 }
697
TestEachInterfaceHasItsOwnTurnPorts()698 void TestEachInterfaceHasItsOwnTurnPorts() {
699 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
700 turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP);
701 turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP);
702 // Add two interfaces both having IPv4 and IPv6 addresses.
703 AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI);
704 AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI);
705 AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
706 AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR);
707 allocator_.reset(new BasicPortAllocator(
708 &network_manager_,
709 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get())));
710 allocator_->Initialize();
711 allocator_->SetConfiguration(allocator_->stun_servers(),
712 allocator_->turn_servers(), 0,
713 webrtc::PRUNE_BASED_ON_PRIORITY);
714 // Have both UDP/TCP and IPv4/IPv6 TURN ports.
715 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
716 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
717
718 allocator_->set_step_delay(kMinimumStepDelay);
719 allocator_->set_flags(
720 allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET |
721 PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
722 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
723 session_->StartGettingPorts();
724 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
725 kDefaultAllocationTimeout, fake_clock);
726 // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to
727 // use.
728 EXPECT_EQ(10U, session_->ReadyPorts().size());
729 EXPECT_EQ(10U, ports_.size());
730 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr));
731 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2));
732 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr));
733 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2));
734 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr));
735 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2));
736 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr));
737 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2));
738 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr));
739 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2));
740
741 // Now that we remove candidates when TURN ports are pruned, there will be
742 // exactly 10 candidates in `candidates_`.
743 EXPECT_EQ(10U, candidates_.size());
744 const std::vector<Candidate>& ready_candidates =
745 session_->ReadyCandidates();
746 EXPECT_EQ(10U, ready_candidates.size());
747 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr));
748 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2));
749 EXPECT_TRUE(
750 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr));
751 EXPECT_TRUE(
752 HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2));
753 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr));
754 EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2));
755 EXPECT_TRUE(
756 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr));
757 EXPECT_TRUE(
758 HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2));
759 EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp",
760 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
761 }
762 };
763
764 // Tests that we can init the port allocator and create a session.
TEST_F(BasicPortAllocatorTest,TestBasic)765 TEST_F(BasicPortAllocatorTest, TestBasic) {
766 EXPECT_EQ(&network_manager_, allocator().network_manager());
767 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
768 ASSERT_EQ(0u, allocator().turn_servers().size());
769
770 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
771 EXPECT_FALSE(session_->CandidatesAllocationDone());
772 }
773
774 // Tests that our network filtering works properly.
TEST_F(BasicPortAllocatorTest,TestIgnoreOnlyLoopbackNetworkByDefault)775 TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
776 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
777 rtc::ADAPTER_TYPE_ETHERNET);
778 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
779 rtc::ADAPTER_TYPE_WIFI);
780 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
781 rtc::ADAPTER_TYPE_CELLULAR);
782 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
783 rtc::ADAPTER_TYPE_VPN);
784 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
785 rtc::ADAPTER_TYPE_LOOPBACK);
786 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
787 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
788 PORTALLOCATOR_DISABLE_TCP);
789 session_->StartGettingPorts();
790 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
791 kDefaultAllocationTimeout, fake_clock);
792 EXPECT_EQ(4U, candidates_.size());
793 for (const Candidate& candidate : candidates_) {
794 EXPECT_LT(candidate.address().ip(), 0x12345604U);
795 }
796 }
797
TEST_F(BasicPortAllocatorTest,TestIgnoreNetworksAccordingToIgnoreMask)798 TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
799 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
800 rtc::ADAPTER_TYPE_ETHERNET);
801 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
802 rtc::ADAPTER_TYPE_WIFI);
803 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
804 rtc::ADAPTER_TYPE_CELLULAR);
805 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
806 rtc::ADAPTER_TYPE_LOOPBACK |
807 rtc::ADAPTER_TYPE_WIFI);
808 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
809 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
810 PORTALLOCATOR_DISABLE_TCP);
811 session_->StartGettingPorts();
812 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
813 kDefaultAllocationTimeout, fake_clock);
814 EXPECT_EQ(1U, candidates_.size());
815 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
816 }
817
818 // Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
819 // both Wi-Fi and cell interfaces are available, only Wi-Fi is used.
TEST_F(BasicPortAllocatorTest,WifiUsedInsteadOfCellWhenCostlyNetworksDisabled)820 TEST_F(BasicPortAllocatorTest,
821 WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) {
822 SocketAddress wifi(IPAddress(0x12345600U), 0);
823 SocketAddress cell(IPAddress(0x12345601U), 0);
824 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
825 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
826 // Disable all but UDP candidates to make the test simpler.
827 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
828 cricket::PORTALLOCATOR_DISABLE_RELAY |
829 cricket::PORTALLOCATOR_DISABLE_TCP |
830 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
831 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
832 session_->StartGettingPorts();
833 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
834 kDefaultAllocationTimeout, fake_clock);
835 // Should only get one Wi-Fi candidate.
836 EXPECT_EQ(1U, candidates_.size());
837 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
838 }
839
840 // Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
841 // both "unknown" and cell interfaces are available, only the unknown are used.
842 // The unknown interface may be something that ultimately uses Wi-Fi, so we do
843 // this to be on the safe side.
TEST_F(BasicPortAllocatorTest,UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled)844 TEST_F(BasicPortAllocatorTest,
845 UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) {
846 SocketAddress cell(IPAddress(0x12345601U), 0);
847 SocketAddress unknown1(IPAddress(0x12345602U), 0);
848 SocketAddress unknown2(IPAddress(0x12345603U), 0);
849 AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
850 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
851 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
852 // Disable all but UDP candidates to make the test simpler.
853 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
854 cricket::PORTALLOCATOR_DISABLE_RELAY |
855 cricket::PORTALLOCATOR_DISABLE_TCP |
856 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
857 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
858 session_->StartGettingPorts();
859 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
860 kDefaultAllocationTimeout, fake_clock);
861 // Should only get two candidates, none of which is cell.
862 EXPECT_EQ(2U, candidates_.size());
863 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1));
864 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2));
865 }
866
867 // Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and
868 // there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi
869 // interface is used.
TEST_F(BasicPortAllocatorTest,WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled)870 TEST_F(BasicPortAllocatorTest,
871 WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) {
872 SocketAddress wifi(IPAddress(0x12345600U), 0);
873 SocketAddress cellular(IPAddress(0x12345601U), 0);
874 SocketAddress unknown1(IPAddress(0x12345602U), 0);
875 SocketAddress unknown2(IPAddress(0x12345603U), 0);
876 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
877 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
878 AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN);
879 AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN);
880 // Disable all but UDP candidates to make the test simpler.
881 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
882 cricket::PORTALLOCATOR_DISABLE_RELAY |
883 cricket::PORTALLOCATOR_DISABLE_TCP |
884 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
885 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
886 session_->StartGettingPorts();
887 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
888 kDefaultAllocationTimeout, fake_clock);
889 // Should only get one Wi-Fi candidate.
890 EXPECT_EQ(1U, candidates_.size());
891 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
892 }
893
894 // Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the
895 // only interface available is cellular, it ends up used anyway. A costly
896 // connection is always better than no connection.
TEST_F(BasicPortAllocatorTest,CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces)897 TEST_F(BasicPortAllocatorTest,
898 CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) {
899 SocketAddress cellular(IPAddress(0x12345601U), 0);
900 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
901 // Disable all but UDP candidates to make the test simpler.
902 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
903 cricket::PORTALLOCATOR_DISABLE_RELAY |
904 cricket::PORTALLOCATOR_DISABLE_TCP |
905 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
906 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
907 session_->StartGettingPorts();
908 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
909 kDefaultAllocationTimeout, fake_clock);
910 // Make sure we got the cell candidate.
911 EXPECT_EQ(1U, candidates_.size());
912 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
913 }
914
915 // Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
916 // a WiFi network with link-local IP address and a cellular network, then the
917 // cellular candidate will still be gathered.
TEST_F(BasicPortAllocatorTest,CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal)918 TEST_F(BasicPortAllocatorTest,
919 CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) {
920 SocketAddress wifi_link_local("169.254.0.1", 0);
921 SocketAddress cellular(IPAddress(0x12345601U), 0);
922 AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
923 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
924
925 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
926 cricket::PORTALLOCATOR_DISABLE_RELAY |
927 cricket::PORTALLOCATOR_DISABLE_TCP |
928 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
929 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
930 session_->StartGettingPorts();
931 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
932 kDefaultAllocationTimeout, fake_clock);
933 // Make sure we got both wifi and cell candidates.
934 EXPECT_EQ(2U, candidates_.size());
935 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
936 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular));
937 }
938
939 // Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is
940 // a WiFi network with link-local IP address, a WiFi network with a normal IP
941 // address and a cellular network, then the cellular candidate will not be
942 // gathered.
TEST_F(BasicPortAllocatorTest,CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent)943 TEST_F(BasicPortAllocatorTest,
944 CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) {
945 SocketAddress wifi(IPAddress(0x12345600U), 0);
946 SocketAddress wifi_link_local("169.254.0.1", 0);
947 SocketAddress cellular(IPAddress(0x12345601U), 0);
948 AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI);
949 AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI);
950 AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR);
951
952 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
953 cricket::PORTALLOCATOR_DISABLE_RELAY |
954 cricket::PORTALLOCATOR_DISABLE_TCP |
955 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
956 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
957 session_->StartGettingPorts();
958 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
959 kDefaultAllocationTimeout, fake_clock);
960 // Make sure we got only wifi candidates.
961 EXPECT_EQ(2U, candidates_.size());
962 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi));
963 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local));
964 }
965
966 // Test that the adapter types of the Ethernet and the VPN can be correctly
967 // identified so that the Ethernet has a lower network cost than the VPN, and
968 // the Ethernet is not filtered out if PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is
969 // set.
TEST_F(BasicPortAllocatorTest,EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent)970 TEST_F(BasicPortAllocatorTest,
971 EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent) {
972 AddInterface(kClientAddr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
973 AddInterface(kClientAddr2, "tap0", rtc::ADAPTER_TYPE_VPN);
974 allocator().set_flags(PORTALLOCATOR_DISABLE_COSTLY_NETWORKS |
975 PORTALLOCATOR_DISABLE_RELAY |
976 PORTALLOCATOR_DISABLE_TCP);
977 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
978 session_->StartGettingPorts();
979 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
980 kDefaultAllocationTimeout, fake_clock);
981 // The VPN tap0 network should be filtered out as a costly network, and we
982 // should have a UDP port and a STUN port from the Ethernet eth0.
983 ASSERT_EQ(2U, ports_.size());
984 EXPECT_EQ(ports_[0]->Network()->name(), "eth0");
985 EXPECT_EQ(ports_[1]->Network()->name(), "eth0");
986 }
987
988 // Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
989 // to gather candidates.
TEST_F(BasicPortAllocatorTest,MaxIpv6NetworksLimitEnforced)990 TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) {
991 // Add three IPv6 network interfaces, but tell the allocator to only use two.
992 allocator().set_max_ipv6_networks(2);
993 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
994 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
995 AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
996
997 // To simplify the test, only gather UDP host candidates.
998 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
999 PORTALLOCATOR_DISABLE_STUN |
1000 PORTALLOCATOR_DISABLE_RELAY);
1001
1002 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1003 session_->StartGettingPorts();
1004 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1005 kDefaultAllocationTimeout, fake_clock);
1006 EXPECT_EQ(2U, candidates_.size());
1007 // Ensure the expected two interfaces (eth0 and eth1) were used.
1008 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
1009 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
1010 }
1011
1012 // Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from
1013 // being used.
TEST_F(BasicPortAllocatorTest,MaxIpv6NetworksLimitDoesNotImpactIpv4Networks)1014 TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) {
1015 // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks.
1016 allocator().set_max_ipv6_networks(1);
1017 AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET);
1018 AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET);
1019 AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET);
1020 AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET);
1021
1022 // To simplify the test, only gather UDP host candidates.
1023 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
1024 PORTALLOCATOR_DISABLE_STUN |
1025 PORTALLOCATOR_DISABLE_RELAY);
1026
1027 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1028 session_->StartGettingPorts();
1029 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1030 kDefaultAllocationTimeout, fake_clock);
1031 EXPECT_EQ(3U, candidates_.size());
1032 // Ensure that only one IPv6 interface was used, but both IPv4 interfaces
1033 // were used.
1034 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
1035 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1036 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2));
1037 }
1038
1039 // Test that we could use loopback interface as host candidate.
TEST_F(BasicPortAllocatorTest,TestLoopbackNetworkInterface)1040 TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
1041 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
1042 allocator_->SetNetworkIgnoreMask(0);
1043 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1044 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
1045 PORTALLOCATOR_DISABLE_TCP);
1046 session_->StartGettingPorts();
1047 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1048 kDefaultAllocationTimeout, fake_clock);
1049 EXPECT_EQ(1U, candidates_.size());
1050 }
1051
1052 // Tests that we can get all the desired addresses successfully.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsWithMinimumStepDelay)1053 TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
1054 AddInterface(kClientAddr);
1055 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1056 session_->StartGettingPorts();
1057 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1058 kDefaultAllocationTimeout, fake_clock);
1059 EXPECT_EQ(3U, candidates_.size());
1060 EXPECT_EQ(3U, ports_.size());
1061 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1062 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1063 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1064 }
1065
1066 // Test that when the same network interface is brought down and up, the
1067 // port allocator session will restart a new allocation sequence if
1068 // it is not stopped.
TEST_F(BasicPortAllocatorTest,TestSameNetworkDownAndUpWhenSessionNotStopped)1069 TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
1070 std::string if_name("test_net0");
1071 AddInterface(kClientAddr, if_name);
1072 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1073 session_->StartGettingPorts();
1074 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1075 kDefaultAllocationTimeout, fake_clock);
1076 EXPECT_EQ(3U, candidates_.size());
1077 EXPECT_EQ(3U, ports_.size());
1078 candidate_allocation_done_ = false;
1079 candidates_.clear();
1080 ports_.clear();
1081
1082 // Disable socket creation to simulate the network interface being down. When
1083 // no network interfaces are available, BasicPortAllocator will fall back to
1084 // binding to the "ANY" address, so we need to make sure that fails too.
1085 fss_->set_tcp_sockets_enabled(false);
1086 fss_->set_udp_sockets_enabled(false);
1087 RemoveInterface(kClientAddr);
1088 SIMULATED_WAIT(false, 1000, fake_clock);
1089 EXPECT_EQ(0U, candidates_.size());
1090 ports_.clear();
1091 candidate_allocation_done_ = false;
1092
1093 // When the same interfaces are added again, new candidates/ports should be
1094 // generated.
1095 fss_->set_tcp_sockets_enabled(true);
1096 fss_->set_udp_sockets_enabled(true);
1097 AddInterface(kClientAddr, if_name);
1098 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1099 kDefaultAllocationTimeout, fake_clock);
1100 EXPECT_EQ(3U, candidates_.size());
1101 EXPECT_EQ(3U, ports_.size());
1102 }
1103
1104 // Test that when the same network interface is brought down and up, the
1105 // port allocator session will not restart a new allocation sequence if
1106 // it is stopped.
TEST_F(BasicPortAllocatorTest,TestSameNetworkDownAndUpWhenSessionStopped)1107 TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
1108 std::string if_name("test_net0");
1109 AddInterface(kClientAddr, if_name);
1110 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1111 session_->StartGettingPorts();
1112 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1113 kDefaultAllocationTimeout, fake_clock);
1114 EXPECT_EQ(3U, candidates_.size());
1115 EXPECT_EQ(3U, ports_.size());
1116 session_->StopGettingPorts();
1117 candidates_.clear();
1118 ports_.clear();
1119
1120 RemoveInterface(kClientAddr);
1121 // Wait one (simulated) second and then verify no new candidates have
1122 // appeared.
1123 SIMULATED_WAIT(false, 1000, fake_clock);
1124 EXPECT_EQ(0U, candidates_.size());
1125 EXPECT_EQ(0U, ports_.size());
1126
1127 // When the same interfaces are added again, new candidates/ports should not
1128 // be generated because the session has stopped.
1129 AddInterface(kClientAddr, if_name);
1130 SIMULATED_WAIT(false, 1000, fake_clock);
1131 EXPECT_EQ(0U, candidates_.size());
1132 EXPECT_EQ(0U, ports_.size());
1133 }
1134
1135 // Similar to the above tests, but tests a situation when sockets can't be
1136 // bound to a network interface, then after a network change event can be.
1137 // Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256
TEST_F(BasicPortAllocatorTest,CandidatesRegatheredAfterBindingFails)1138 TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) {
1139 // Only test local ports to simplify test.
1140 ResetWithNoServersOrNat();
1141 // Provide a situation where the interface appears to be available, but
1142 // binding the sockets fails. See bug for description of when this can
1143 // happen.
1144 std::string if_name("test_net0");
1145 AddInterface(kClientAddr, if_name);
1146 fss_->set_tcp_sockets_enabled(false);
1147 fss_->set_udp_sockets_enabled(false);
1148 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1149 session_->StartGettingPorts();
1150 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1151 kDefaultAllocationTimeout, fake_clock);
1152 // Make sure we actually prevented candidates from being gathered (other than
1153 // a single TCP active candidate, since that doesn't require creating a
1154 // socket).
1155 ASSERT_EQ(1U, candidates_.size());
1156 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1157 candidate_allocation_done_ = false;
1158
1159 // Now simulate the interface coming up, with the newfound ability to bind
1160 // sockets.
1161 fss_->set_tcp_sockets_enabled(true);
1162 fss_->set_udp_sockets_enabled(true);
1163 AddInterface(kClientAddr, if_name);
1164 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1165 kDefaultAllocationTimeout, fake_clock);
1166 // Should get UDP and TCP candidate.
1167 ASSERT_EQ(2U, candidates_.size());
1168 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1169 // TODO(deadbeef): This is actually the same active TCP candidate as before.
1170 // We should extend this test to also verify that a server candidate is
1171 // gathered.
1172 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1173 }
1174
1175 // Verify candidates with default step delay of 1sec.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsWithOneSecondStepDelay)1176 TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
1177 AddInterface(kClientAddr);
1178 allocator_->set_step_delay(kDefaultStepDelay);
1179 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1180 session_->StartGettingPorts();
1181 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
1182 EXPECT_EQ(2U, ports_.size());
1183 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 2000, fake_clock);
1184 EXPECT_EQ(3U, ports_.size());
1185
1186 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 1500, fake_clock);
1187 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1188 EXPECT_EQ(3U, ports_.size());
1189 EXPECT_TRUE(candidate_allocation_done_);
1190 // If we Stop gathering now, we shouldn't get a second "done" callback.
1191 session_->StopGettingPorts();
1192 }
1193
TEST_F(BasicPortAllocatorTest,TestSetupVideoRtpPortsWithNormalSendBuffers)1194 TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
1195 AddInterface(kClientAddr);
1196 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
1197 session_->StartGettingPorts();
1198 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1199 kDefaultAllocationTimeout, fake_clock);
1200 EXPECT_EQ(3U, candidates_.size());
1201 // If we Stop gathering now, we shouldn't get a second "done" callback.
1202 session_->StopGettingPorts();
1203
1204 // All ports should have unset send-buffer sizes.
1205 CheckSendBufferSizesOfAllPorts(-1);
1206 }
1207
1208 // Tests that we can get callback after StopGetAllPorts when called in the
1209 // middle of gathering.
TEST_F(BasicPortAllocatorTest,TestStopGetAllPorts)1210 TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
1211 AddInterface(kClientAddr);
1212 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1213 session_->StartGettingPorts();
1214 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1215 fake_clock);
1216 EXPECT_EQ(2U, ports_.size());
1217 session_->StopGettingPorts();
1218 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1219 kDefaultAllocationTimeout, fake_clock);
1220 }
1221
1222 // Test that we restrict client ports appropriately when a port range is set.
1223 // We check the candidates for udp/stun/tcp ports, and the from address
1224 // for relay ports.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsPortRange)1225 TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
1226 AddInterface(kClientAddr);
1227 // Check that an invalid port range fails.
1228 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
1229 // Check that a null port range succeeds.
1230 EXPECT_TRUE(SetPortRange(0, 0));
1231 // Check that a valid port range succeeds.
1232 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
1233 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1234 session_->StartGettingPorts();
1235 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1236 kDefaultAllocationTimeout, fake_clock);
1237 EXPECT_EQ(3U, candidates_.size());
1238 EXPECT_EQ(3U, ports_.size());
1239
1240 int num_nonrelay_candidates = 0;
1241 for (const Candidate& candidate : candidates_) {
1242 // Check the port number for the UDP/STUN/TCP port objects.
1243 if (candidate.type() != RELAY_PORT_TYPE) {
1244 EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort));
1245 ++num_nonrelay_candidates;
1246 }
1247 }
1248 EXPECT_EQ(3, num_nonrelay_candidates);
1249 }
1250
1251 // Test that if we have no network adapters, we bind to the ANY address and
1252 // still get non-host candidates.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsNoAdapters)1253 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
1254 // Default config uses GTURN and no NAT, so replace that with the
1255 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
1256 ResetWithStunServerAndNat(kStunAddr);
1257 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1258 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1259 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
1260 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1261 session_->StartGettingPorts();
1262 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1263 kDefaultAllocationTimeout, fake_clock);
1264 EXPECT_EQ(4U, ports_.size());
1265 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr));
1266 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr));
1267 // Two TURN ports, using UDP/TCP for the first hop to the TURN server.
1268 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr));
1269 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr));
1270 // The "any" address port should be in the signaled ready ports, but the host
1271 // candidate for it is useless and shouldn't be signaled. So we only have
1272 // STUN/TURN candidates.
1273 EXPECT_EQ(3U, candidates_.size());
1274 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1275 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1276 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN
1277 // server.
1278 EXPECT_EQ(2,
1279 CountCandidates(candidates_, "relay", "udp",
1280 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1281 }
1282
1283 // Test that when enumeration is disabled, we should not have any ports when
1284 // candidate_filter() is set to CF_RELAY and no relay is specified.
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationWithoutNatRelayTransportOnly)1285 TEST_F(BasicPortAllocatorTest,
1286 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
1287 ResetWithStunServerNoNat(kStunAddr);
1288 allocator().SetCandidateFilter(CF_RELAY);
1289 // Expect to see no ports and no candidates.
1290 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
1291 rtc::IPAddress(), rtc::IPAddress());
1292 }
1293
1294 // Test that even with multiple interfaces, the result should still be a single
1295 // default private, one STUN and one TURN candidate since we bind to any address
1296 // (i.e. all 0s).
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationBehindNatMultipleInterfaces)1297 TEST_F(BasicPortAllocatorTest,
1298 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
1299 AddInterface(kPrivateAddr);
1300 AddInterface(kPrivateAddr2);
1301 ResetWithStunServerAndNat(kStunAddr);
1302 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1303
1304 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
1305 // address set and we have no IPv6 STUN server, there should be no IPv6
1306 // candidates.
1307 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1308 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
1309
1310 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
1311 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
1312 // TURN/UDP candidates.
1313 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
1314 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1315 rtc::IPAddress());
1316 }
1317
1318 // Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
1319 // candidates when both TURN/UDP and TURN/TCP servers are specified.
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationBehindNatWithTcp)1320 TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
1321 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1322 AddInterface(kPrivateAddr);
1323 ResetWithStunServerAndNat(kStunAddr);
1324 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1325 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
1326 // private, STUN, TURN/UDP, and TURN/TCP candidates.
1327 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
1328 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
1329 kTurnUdpExtAddr.ipaddr());
1330 }
1331
1332 // Test that when adapter enumeration is disabled, for endpoints without
1333 // STUN/TURN specified, a default private candidate is still generated.
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationWithoutNatOrServers)1334 TEST_F(BasicPortAllocatorTest,
1335 TestDisableAdapterEnumerationWithoutNatOrServers) {
1336 ResetWithNoServersOrNat();
1337 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
1338 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
1339 rtc::IPAddress(), rtc::IPAddress());
1340 }
1341
1342 // Test that when adapter enumeration is disabled, with
1343 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1344 // a NAT, there is no local candidate.
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled)1345 TEST_F(BasicPortAllocatorTest,
1346 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
1347 ResetWithStunServerNoNat(kStunAddr);
1348 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1349 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
1350 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1351 // candidate.
1352 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
1353 rtc::IPAddress(), rtc::IPAddress());
1354 }
1355
1356 // Test that when adapter enumeration is disabled, with
1357 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
1358 // a NAT, there is no local candidate. However, this specified default route
1359 // (kClientAddr) which was discovered when sending STUN requests, will become
1360 // the srflx addresses.
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute)1361 TEST_F(BasicPortAllocatorTest,
1362 TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) {
1363 ResetWithStunServerNoNat(kStunAddr);
1364 AddInterfaceAsDefaultSourceAddresss(kClientAddr);
1365 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1366 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
1367 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
1368 // candidate.
1369 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
1370 rtc::IPAddress(), rtc::IPAddress());
1371 }
1372
1373 // Test that when adapter enumeration is disabled, with
1374 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
1375 // NAT, there is only one STUN candidate.
TEST_F(BasicPortAllocatorTest,TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled)1376 TEST_F(BasicPortAllocatorTest,
1377 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
1378 ResetWithStunServerAndNat(kStunAddr);
1379 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1380 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
1381 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
1382 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
1383 rtc::IPAddress(), rtc::IPAddress());
1384 }
1385
1386 // Test that we disable relay over UDP, and only TCP is used when connecting to
1387 // the relay server.
TEST_F(BasicPortAllocatorTest,TestDisableUdpTurn)1388 TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
1389 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1390 AddInterface(kClientAddr);
1391 ResetWithStunServerAndNat(kStunAddr);
1392 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1393 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1394 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
1395 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1396 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1397
1398 session_->StartGettingPorts();
1399 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1400 kDefaultAllocationTimeout, fake_clock);
1401
1402 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
1403 // TURN/TCP candidates.
1404 EXPECT_EQ(2U, ports_.size());
1405 EXPECT_EQ(2U, candidates_.size());
1406 Candidate turn_candidate;
1407 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr,
1408 &turn_candidate));
1409 // The TURN candidate should use TCP to contact the TURN server.
1410 EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol());
1411 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1412 }
1413
1414 // Test that we can get OnCandidatesAllocationDone callback when all the ports
1415 // are disabled.
TEST_F(BasicPortAllocatorTest,TestDisableAllPorts)1416 TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
1417 AddInterface(kClientAddr);
1418 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1419 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
1420 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
1421 session_->StartGettingPorts();
1422 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
1423 EXPECT_EQ(0U, candidates_.size());
1424 }
1425
1426 // Test that we don't crash or malfunction if we can't create UDP sockets.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsNoUdpSockets)1427 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
1428 AddInterface(kClientAddr);
1429 fss_->set_udp_sockets_enabled(false);
1430 ASSERT_TRUE(CreateSession(1));
1431 session_->StartGettingPorts();
1432 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1433 kDefaultAllocationTimeout, fake_clock);
1434 EXPECT_EQ(1U, candidates_.size());
1435 EXPECT_EQ(1U, ports_.size());
1436 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1437 }
1438
1439 // Test that we don't crash or malfunction if we can't create UDP sockets or
1440 // listen on TCP sockets. We still give out a local TCP address, since
1441 // apparently this is needed for the remote side to accept our connection.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsNoUdpSocketsNoTcpListen)1442 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
1443 AddInterface(kClientAddr);
1444 fss_->set_udp_sockets_enabled(false);
1445 fss_->set_tcp_listen_enabled(false);
1446 ASSERT_TRUE(CreateSession(1));
1447 session_->StartGettingPorts();
1448 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1449 kDefaultAllocationTimeout, fake_clock);
1450 EXPECT_EQ(1U, candidates_.size());
1451 EXPECT_EQ(1U, ports_.size());
1452 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1453 }
1454
1455 // Test that we don't crash or malfunction if we can't create any sockets.
1456 // TODO(deadbeef): Find a way to exit early here.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsNoSockets)1457 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
1458 AddInterface(kClientAddr);
1459 fss_->set_tcp_sockets_enabled(false);
1460 fss_->set_udp_sockets_enabled(false);
1461 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1462 session_->StartGettingPorts();
1463 SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock);
1464 // TODO(deadbeef): Check candidate_allocation_done signal.
1465 // In case of Relay, ports creation will succeed but sockets will fail.
1466 // There is no error reporting from RelayEntry to handle this failure.
1467 }
1468
1469 // Testing STUN timeout.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsNoUdpAllowed)1470 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
1471 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1472 AddInterface(kClientAddr);
1473 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1474 session_->StartGettingPorts();
1475 EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1476 fake_clock);
1477 EXPECT_EQ(2U, ports_.size());
1478 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1479 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1480 // We wait at least for a full STUN timeout, which
1481 // cricket::STUN_TOTAL_TIMEOUT seconds.
1482 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1483 cricket::STUN_TOTAL_TIMEOUT, fake_clock);
1484 // No additional (STUN) candidates.
1485 EXPECT_EQ(2U, candidates_.size());
1486 }
1487
TEST_F(BasicPortAllocatorTest,TestCandidatePriorityOfMultipleInterfaces)1488 TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
1489 AddInterface(kClientAddr);
1490 AddInterface(kClientAddr2);
1491 // Allocating only host UDP ports. This is done purely for testing
1492 // convenience.
1493 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
1494 PORTALLOCATOR_DISABLE_RELAY);
1495 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1496 session_->StartGettingPorts();
1497 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1498 kDefaultAllocationTimeout, fake_clock);
1499 ASSERT_EQ(2U, candidates_.size());
1500 EXPECT_EQ(2U, ports_.size());
1501 // Candidates priorities should be different.
1502 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
1503 }
1504
1505 // Test to verify ICE restart process.
TEST_F(BasicPortAllocatorTest,TestGetAllPortsRestarts)1506 TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
1507 AddInterface(kClientAddr);
1508 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1509 session_->StartGettingPorts();
1510 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1511 kDefaultAllocationTimeout, fake_clock);
1512 EXPECT_EQ(3U, candidates_.size());
1513 EXPECT_EQ(3U, ports_.size());
1514 // TODO(deadbeef): Extend this to verify ICE restart.
1515 }
1516
1517 // Test that the allocator session uses the candidate filter it's created with,
1518 // rather than the filter of its parent allocator.
1519 // The filter of the allocator should only affect the next gathering phase,
1520 // according to JSEP, which means the *next* allocator session returned.
TEST_F(BasicPortAllocatorTest,TestSessionUsesOwnCandidateFilter)1521 TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
1522 AddInterface(kClientAddr);
1523 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1524 // Set candidate filter *after* creating the session. Should have no effect.
1525 allocator().SetCandidateFilter(CF_RELAY);
1526 session_->StartGettingPorts();
1527 // 7 candidates and 4 ports is what we would normally get (see the
1528 // TestGetAllPorts* tests).
1529 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1530 kDefaultAllocationTimeout, fake_clock);
1531 EXPECT_EQ(3U, candidates_.size());
1532 EXPECT_EQ(3U, ports_.size());
1533 }
1534
1535 // Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
1536 // This test also verifies that when the allocator is only allowed to use
1537 // relay (i.e. IceTransportsType is relay), the raddr is an empty
1538 // address with the correct family. This is to prevent any local
1539 // reflective address leakage in the sdp line.
TEST_F(BasicPortAllocatorTest,TestCandidateFilterWithRelayOnly)1540 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
1541 AddInterface(kClientAddr);
1542 // GTURN is not configured here.
1543 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
1544 allocator().SetCandidateFilter(CF_RELAY);
1545 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1546 session_->StartGettingPorts();
1547 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1548 kDefaultAllocationTimeout, fake_clock);
1549 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1550 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1551
1552 EXPECT_EQ(1U, candidates_.size());
1553 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
1554 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[0].type());
1555 EXPECT_EQ(
1556 candidates_[0].related_address(),
1557 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
1558 }
1559
TEST_F(BasicPortAllocatorTest,TestCandidateFilterWithHostOnly)1560 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
1561 AddInterface(kClientAddr);
1562 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1563 allocator().SetCandidateFilter(CF_HOST);
1564 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1565 session_->StartGettingPorts();
1566 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1567 kDefaultAllocationTimeout, fake_clock);
1568 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1569 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
1570 for (const Candidate& candidate : candidates_) {
1571 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
1572 }
1573 }
1574
1575 // Host is behind the NAT.
TEST_F(BasicPortAllocatorTest,TestCandidateFilterWithReflexiveOnly)1576 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
1577 AddInterface(kPrivateAddr);
1578 ResetWithStunServerAndNat(kStunAddr);
1579
1580 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1581 allocator().SetCandidateFilter(CF_REFLEXIVE);
1582 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1583 session_->StartGettingPorts();
1584 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1585 kDefaultAllocationTimeout, fake_clock);
1586 // Host is behind NAT, no private address will be exposed. Hence only UDP
1587 // port with STUN candidate will be sent outside.
1588 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1589 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
1590 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[0].type());
1591 EXPECT_EQ(
1592 candidates_[0].related_address(),
1593 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
1594 }
1595
1596 // Host is not behind the NAT.
TEST_F(BasicPortAllocatorTest,TestCandidateFilterWithReflexiveOnlyAndNoNAT)1597 TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
1598 AddInterface(kClientAddr);
1599 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1600 allocator().SetCandidateFilter(CF_REFLEXIVE);
1601 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1602 session_->StartGettingPorts();
1603 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1604 kDefaultAllocationTimeout, fake_clock);
1605 // Host has a public address, both UDP and TCP candidates will be exposed.
1606 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
1607 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
1608 for (const Candidate& candidate : candidates_) {
1609 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidate.type());
1610 }
1611 }
1612
1613 // Test that we get the same ufrag and pwd for all candidates.
TEST_F(BasicPortAllocatorTest,TestEnableSharedUfrag)1614 TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
1615 AddInterface(kClientAddr);
1616 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1617 session_->StartGettingPorts();
1618 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1619 kDefaultAllocationTimeout, fake_clock);
1620 EXPECT_EQ(3U, candidates_.size());
1621 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1622 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr));
1623 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
1624 EXPECT_EQ(3U, ports_.size());
1625 for (const Candidate& candidate : candidates_) {
1626 EXPECT_EQ(kIceUfrag0, candidate.username());
1627 EXPECT_EQ(kIcePwd0, candidate.password());
1628 }
1629 }
1630
1631 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1632 // is allocated for udp and stun. Also verify there is only one candidate
1633 // (local) if stun candidate is same as local candidate, which will be the case
1634 // in a public network like the below test.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithoutNat)1635 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
1636 AddInterface(kClientAddr);
1637 allocator_->set_flags(allocator().flags() |
1638 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1639 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1640 session_->StartGettingPorts();
1641 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout,
1642 fake_clock);
1643 EXPECT_EQ(2U, ports_.size());
1644 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1645 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1646 kDefaultAllocationTimeout, fake_clock);
1647 }
1648
1649 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1650 // is allocated for udp and stun. In this test we should expect both stun and
1651 // local candidates as client behind a nat.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithNat)1652 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
1653 AddInterface(kClientAddr);
1654 ResetWithStunServerAndNat(kStunAddr);
1655
1656 allocator_->set_flags(allocator().flags() |
1657 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1658 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1659 session_->StartGettingPorts();
1660 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1661 fake_clock);
1662 ASSERT_EQ(2U, ports_.size());
1663 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1664 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1665 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1666 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1667 kDefaultAllocationTimeout, fake_clock);
1668 EXPECT_EQ(3U, candidates_.size());
1669 }
1670
1671 // Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithoutNatUsingTurn)1672 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
1673 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1674 AddInterface(kClientAddr);
1675 allocator_.reset(new BasicPortAllocator(
1676 &network_manager_,
1677 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get())));
1678 allocator_->Initialize();
1679
1680 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1681
1682 allocator_->set_step_delay(kMinimumStepDelay);
1683 allocator_->set_flags(allocator().flags() |
1684 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1685 PORTALLOCATOR_DISABLE_TCP);
1686
1687 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1688 session_->StartGettingPorts();
1689
1690 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1691 kDefaultAllocationTimeout, fake_clock);
1692 ASSERT_EQ(3U, candidates_.size());
1693 ASSERT_EQ(3U, ports_.size());
1694 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1695 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1696 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1697 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1698 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1699 }
1700
1701 // Test that if the turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN
1702 // port will not be used if UDP TurnPort is used, given that TCP TURN port
1703 // becomes ready first.
TEST_F(BasicPortAllocatorTest,TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst)1704 TEST_F(BasicPortAllocatorTest,
1705 TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) {
1706 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1707 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1708 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
1709
1710 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY,
1711 true /* tcp_pruned */);
1712 }
1713
1714 // Test that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN port
1715 // will not be used if UDP TurnPort is used, given that UDP TURN port becomes
1716 // ready first.
TEST_F(BasicPortAllocatorTest,TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst)1717 TEST_F(BasicPortAllocatorTest,
1718 TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) {
1719 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1720 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1721 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
1722
1723 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY,
1724 true /* tcp_pruned */);
1725 }
1726
1727 // Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port
1728 // will be kept regardless of the priority.
TEST_F(BasicPortAllocatorTest,TestUdpTurnPortPrunesTcpTurnPortIfUdpReadyFirst)1729 TEST_F(BasicPortAllocatorTest,
1730 TestUdpTurnPortPrunesTcpTurnPortIfUdpReadyFirst) {
1731 // UDP has shorter delay than TCP so that UDP TURN port becomes ready first.
1732 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1733 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200);
1734
1735 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY,
1736 true /* tcp_pruned */);
1737 }
1738
1739 // Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port
1740 // will be kept regardless of the priority.
TEST_F(BasicPortAllocatorTest,TestTcpTurnPortPrunesUdpTurnPortIfTcpReadyFirst)1741 TEST_F(BasicPortAllocatorTest,
1742 TestTcpTurnPortPrunesUdpTurnPortIfTcpReadyFirst) {
1743 // UDP has longer delay than TCP so that TCP TURN port becomes ready first.
1744 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1745 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100);
1746
1747 TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY,
1748 false /* tcp_pruned */);
1749 }
1750
1751 // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4
1752 // TurnPort will not be used if IPv6 TurnPort is used, given that IPv4 TURN port
1753 // becomes ready first.
TEST_F(BasicPortAllocatorTest,TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst)1754 TEST_F(BasicPortAllocatorTest,
1755 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) {
1756 // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready
1757 // first.
1758 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1759 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200);
1760
1761 TestIPv6TurnPortPrunesIPv4TurnPort();
1762 }
1763
1764 // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4
1765 // TurnPort will not be used if IPv6 TurnPort is used, given that IPv6 TURN port
1766 // becomes ready first.
TEST_F(BasicPortAllocatorTest,TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst)1767 TEST_F(BasicPortAllocatorTest,
1768 TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) {
1769 // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready
1770 // first.
1771 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200);
1772 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100);
1773
1774 TestIPv6TurnPortPrunesIPv4TurnPort();
1775 }
1776
1777 // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network
1778 // interface will has its own set of TurnPorts based on their priorities, in the
1779 // default case where no transit delay is set.
TEST_F(BasicPortAllocatorTest,TestEachInterfaceHasItsOwnTurnPortsNoDelay)1780 TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) {
1781 TestEachInterfaceHasItsOwnTurnPorts();
1782 }
1783
1784 // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network
1785 // interface will has its own set of TurnPorts based on their priorities, given
1786 // that IPv4/TCP TURN port becomes ready first.
TEST_F(BasicPortAllocatorTest,TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst)1787 TEST_F(BasicPortAllocatorTest,
1788 TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) {
1789 // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port
1790 // becomes ready last.
1791 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10);
1792 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100);
1793 virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20);
1794 virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300);
1795
1796 TestEachInterfaceHasItsOwnTurnPorts();
1797 }
1798
1799 // Testing DNS resolve for the TURN server, this will test AllocationSequence
1800 // handling the unresolved address signal from TurnPort.
1801 // TODO(pthatcher): Make this test work with SIMULATED_WAIT. It
1802 // appears that it doesn't currently because of the DNS look up not
1803 // using the fake clock.
TEST_F(BasicPortAllocatorTestWithRealClock,TestSharedSocketWithServerAddressResolve)1804 TEST_F(BasicPortAllocatorTestWithRealClock,
1805 TestSharedSocketWithServerAddressResolve) {
1806 // This test relies on a real query for "localhost", so it won't work on an
1807 // IPv6-only machine.
1808 MAYBE_SKIP_IPV4;
1809 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
1810 PROTO_UDP);
1811 AddInterface(kClientAddr);
1812 allocator_.reset(new BasicPortAllocator(
1813 &network_manager_,
1814 std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get())));
1815 allocator_->Initialize();
1816 RelayServerConfig turn_server;
1817 RelayCredentials credentials(kTurnUsername, kTurnPassword);
1818 turn_server.credentials = credentials;
1819 turn_server.ports.push_back(
1820 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP));
1821 allocator_->AddTurnServerForTesting(turn_server);
1822
1823 allocator_->set_step_delay(kMinimumStepDelay);
1824 allocator_->set_flags(allocator().flags() |
1825 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1826 PORTALLOCATOR_DISABLE_TCP);
1827
1828 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1829 session_->StartGettingPorts();
1830
1831 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1832 }
1833
1834 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1835 // is allocated for udp/stun/turn. In this test we should expect all local,
1836 // stun and turn candidates.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithNatUsingTurn)1837 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
1838 AddInterface(kClientAddr);
1839 ResetWithStunServerAndNat(kStunAddr);
1840
1841 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1842
1843 allocator_->set_flags(allocator().flags() |
1844 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1845 PORTALLOCATOR_DISABLE_TCP);
1846
1847 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1848 session_->StartGettingPorts();
1849
1850 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1851 kDefaultAllocationTimeout, fake_clock);
1852 EXPECT_EQ(3U, candidates_.size());
1853 ASSERT_EQ(2U, ports_.size());
1854 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1855 EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp",
1856 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
1857 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1858 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1859 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1860 kDefaultAllocationTimeout, fake_clock);
1861 // Local port will be created first and then TURN port.
1862 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1863 // guarantees...
1864 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1865 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1866 }
1867
1868 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1869 // server is also used as the STUN server, we should get 'local', 'stun', and
1870 // 'relay' candidates.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithNatUsingTurnAsStun)1871 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1872 AddInterface(kClientAddr);
1873 // Use an empty SocketAddress to add a NAT without STUN server.
1874 ResetWithStunServerAndNat(SocketAddress());
1875 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1876
1877 // Must set the step delay to 0 to make sure the relay allocation phase is
1878 // started before the STUN candidates are obtained, so that the STUN binding
1879 // response is processed when both StunPort and TurnPort exist to reproduce
1880 // webrtc issue 3537.
1881 allocator_->set_step_delay(0);
1882 allocator_->set_flags(allocator().flags() |
1883 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1884 PORTALLOCATOR_DISABLE_TCP);
1885
1886 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1887 session_->StartGettingPorts();
1888
1889 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1890 kDefaultAllocationTimeout, fake_clock);
1891 EXPECT_EQ(3U, candidates_.size());
1892 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1893 Candidate stun_candidate;
1894 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1895 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1896 &stun_candidate));
1897 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1898 candidates_, "relay", "udp",
1899 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1900 stun_candidate.address()));
1901
1902 // Local port will be created first and then TURN port.
1903 // TODO(deadbeef): This isn't something the BasicPortAllocator API contract
1904 // guarantees...
1905 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1906 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1907 }
1908
1909 // Test that when only a TCP TURN server is available, we do NOT use it as
1910 // a UDP STUN server, as this could leak our IP address. Thus we should only
1911 // expect two ports, a UDPPort and TurnPort.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithNatUsingTurnTcpOnly)1912 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1913 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
1914 AddInterface(kClientAddr);
1915 ResetWithStunServerAndNat(rtc::SocketAddress());
1916 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1917
1918 allocator_->set_flags(allocator().flags() |
1919 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1920 PORTALLOCATOR_DISABLE_TCP);
1921
1922 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1923 session_->StartGettingPorts();
1924
1925 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1926 kDefaultAllocationTimeout, fake_clock);
1927 EXPECT_EQ(2U, candidates_.size());
1928 ASSERT_EQ(2U, ports_.size());
1929 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1930 EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp",
1931 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0)));
1932 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1933 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1934 }
1935
1936 // Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1937 // TURN server is used as the STUN server and we get 'local', 'stun', and
1938 // 'relay' candidates.
1939 // TODO(deadbeef): Remove this test when support for non-shared socket mode
1940 // is removed.
TEST_F(BasicPortAllocatorTest,TestNonSharedSocketWithNatUsingTurnAsStun)1941 TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1942 AddInterface(kClientAddr);
1943 // Use an empty SocketAddress to add a NAT without STUN server.
1944 ResetWithStunServerAndNat(SocketAddress());
1945 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1946
1947 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
1948
1949 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1950 session_->StartGettingPorts();
1951
1952 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
1953 kDefaultAllocationTimeout, fake_clock);
1954 EXPECT_EQ(3U, candidates_.size());
1955 ASSERT_EQ(3U, ports_.size());
1956 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1957 Candidate stun_candidate;
1958 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1959 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1960 &stun_candidate));
1961 Candidate turn_candidate;
1962 EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp",
1963 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
1964 &turn_candidate));
1965 // Not using shared socket, so the STUN request's server reflexive address
1966 // should be different than the TURN request's server reflexive address.
1967 EXPECT_NE(turn_candidate.related_address(), stun_candidate.address());
1968
1969 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1970 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1971 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1972 }
1973
1974 // Test that even when both a STUN and TURN server are configured, the TURN
1975 // server is used as a STUN server and we get a 'stun' candidate.
TEST_F(BasicPortAllocatorTest,TestSharedSocketWithNatUsingTurnAndStun)1976 TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1977 AddInterface(kClientAddr);
1978 // Configure with STUN server but destroy it, so we can ensure that it's
1979 // the TURN server actually being used as a STUN server.
1980 ResetWithStunServerAndNat(kStunAddr);
1981 stun_server_.reset();
1982 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1983
1984 allocator_->set_flags(allocator().flags() |
1985 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1986 PORTALLOCATOR_DISABLE_TCP);
1987
1988 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1989 session_->StartGettingPorts();
1990
1991 ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout,
1992 fake_clock);
1993 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
1994 Candidate stun_candidate;
1995 EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp",
1996 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0),
1997 &stun_candidate));
1998 EXPECT_TRUE(HasCandidateWithRelatedAddr(
1999 candidates_, "relay", "udp",
2000 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0),
2001 stun_candidate.address()));
2002
2003 // Don't bother waiting for STUN timeout, since we already verified
2004 // that we got a STUN candidate from the TURN server.
2005 }
2006
2007 // This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
2008 // and fail to generate STUN candidate, local UDP candidate is generated
2009 // properly.
TEST_F(BasicPortAllocatorTest,TestSharedSocketNoUdpAllowed)2010 TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
2011 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2012 PORTALLOCATOR_DISABLE_TCP |
2013 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2014 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
2015 AddInterface(kClientAddr);
2016 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2017 session_->StartGettingPorts();
2018 ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2019 fake_clock);
2020 EXPECT_EQ(1U, candidates_.size());
2021 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2022 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
2023 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs,
2024 fake_clock);
2025 EXPECT_EQ(1U, candidates_.size());
2026 }
2027
2028 // Test that when the NetworkManager doesn't have permission to enumerate
2029 // adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
2030 // automatically.
TEST_F(BasicPortAllocatorTest,TestNetworkPermissionBlocked)2031 TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
2032 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
2033 rtc::IPAddress());
2034 network_manager_.set_enumeration_permission(
2035 rtc::NetworkManager::ENUMERATION_BLOCKED);
2036 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2037 PORTALLOCATOR_DISABLE_TCP |
2038 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2039 EXPECT_EQ(0U,
2040 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
2041 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2042 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
2043 session_->StartGettingPorts();
2044 EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout,
2045 fake_clock);
2046 EXPECT_EQ(1U, candidates_.size());
2047 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr));
2048 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
2049 }
2050
2051 // This test verifies allocator can use IPv6 addresses along with IPv4.
TEST_F(BasicPortAllocatorTest,TestEnableIPv6Addresses)2052 TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
2053 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
2054 PORTALLOCATOR_ENABLE_IPV6 |
2055 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2056 AddInterface(kClientIPv6Addr);
2057 AddInterface(kClientAddr);
2058 allocator_->set_step_delay(kMinimumStepDelay);
2059 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2060 session_->StartGettingPorts();
2061 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2062 kDefaultAllocationTimeout, fake_clock);
2063 EXPECT_EQ(4U, ports_.size());
2064 EXPECT_EQ(4U, candidates_.size());
2065 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2066 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr));
2067 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr));
2068 EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr));
2069 }
2070
TEST_F(BasicPortAllocatorTest,TestStopGettingPorts)2071 TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
2072 AddInterface(kClientAddr);
2073 allocator_->set_step_delay(kDefaultStepDelay);
2074 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2075 session_->StartGettingPorts();
2076 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
2077 EXPECT_EQ(2U, ports_.size());
2078 session_->StopGettingPorts();
2079 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
2080
2081 // After stopping getting ports, adding a new interface will not start
2082 // getting ports again.
2083 allocator_->set_step_delay(kMinimumStepDelay);
2084 candidates_.clear();
2085 ports_.clear();
2086 candidate_allocation_done_ = false;
2087 network_manager_.AddInterface(kClientAddr2);
2088 SIMULATED_WAIT(false, 1000, fake_clock);
2089 EXPECT_EQ(0U, candidates_.size());
2090 EXPECT_EQ(0U, ports_.size());
2091 }
2092
TEST_F(BasicPortAllocatorTest,TestClearGettingPorts)2093 TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
2094 AddInterface(kClientAddr);
2095 allocator_->set_step_delay(kDefaultStepDelay);
2096 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2097 session_->StartGettingPorts();
2098 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
2099 EXPECT_EQ(2U, ports_.size());
2100 session_->ClearGettingPorts();
2101 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock);
2102
2103 // After clearing getting ports, adding a new interface will start getting
2104 // ports again.
2105 allocator_->set_step_delay(kMinimumStepDelay);
2106 candidates_.clear();
2107 ports_.clear();
2108 candidate_allocation_done_ = false;
2109 network_manager_.AddInterface(kClientAddr2);
2110 ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock);
2111 EXPECT_EQ(2U, ports_.size());
2112 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2113 kDefaultAllocationTimeout, fake_clock);
2114 }
2115
2116 // Test that the ports and candidates are updated with new ufrag/pwd/etc. when
2117 // a pooled session is taken out of the pool.
TEST_F(BasicPortAllocatorTest,TestTransportInformationUpdated)2118 TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
2119 AddInterface(kClientAddr);
2120 int pool_size = 1;
2121 allocator_->SetConfiguration(allocator_->stun_servers(),
2122 allocator_->turn_servers(), pool_size,
2123 webrtc::NO_PRUNE);
2124 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2125 ASSERT_NE(nullptr, peeked_session);
2126 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2127 kDefaultAllocationTimeout, fake_clock);
2128 // Expect that when TakePooledSession is called,
2129 // UpdateTransportInformationInternal will be called and the
2130 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
2131 // candidates.
2132 session_ =
2133 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2134 ASSERT_NE(nullptr, session_.get());
2135 auto ready_ports = session_->ReadyPorts();
2136 auto candidates = session_->ReadyCandidates();
2137 EXPECT_FALSE(ready_ports.empty());
2138 EXPECT_FALSE(candidates.empty());
2139 for (const PortInterface* port_interface : ready_ports) {
2140 const Port* port = static_cast<const Port*>(port_interface);
2141 EXPECT_EQ(kContentName, port->content_name());
2142 EXPECT_EQ(1, port->component());
2143 EXPECT_EQ(kIceUfrag0, port->username_fragment());
2144 EXPECT_EQ(kIcePwd0, port->password());
2145 }
2146 for (const Candidate& candidate : candidates) {
2147 EXPECT_EQ(1, candidate.component());
2148 EXPECT_EQ(kIceUfrag0, candidate.username());
2149 EXPECT_EQ(kIcePwd0, candidate.password());
2150 }
2151 }
2152
2153 // Test that a new candidate filter takes effect even on already-gathered
2154 // candidates.
TEST_F(BasicPortAllocatorTest,TestSetCandidateFilterAfterCandidatesGathered)2155 TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
2156 AddInterface(kClientAddr);
2157 int pool_size = 1;
2158 allocator_->SetConfiguration(allocator_->stun_servers(),
2159 allocator_->turn_servers(), pool_size,
2160 webrtc::NO_PRUNE);
2161 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
2162 ASSERT_NE(nullptr, peeked_session);
2163 EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(),
2164 kDefaultAllocationTimeout, fake_clock);
2165 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
2166 size_t initial_ports_size = peeked_session->ReadyPorts().size();
2167 allocator_->SetCandidateFilter(CF_RELAY);
2168 // Assume that when TakePooledSession is called, the candidate filter will be
2169 // applied to the pooled session. This is tested by PortAllocatorTest.
2170 session_ =
2171 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
2172 ASSERT_NE(nullptr, session_.get());
2173 auto candidates = session_->ReadyCandidates();
2174 auto ports = session_->ReadyPorts();
2175 // Sanity check that the number of candidates and ports decreased.
2176 EXPECT_GT(initial_candidates_size, candidates.size());
2177 EXPECT_GT(initial_ports_size, ports.size());
2178 for (const PortInterface* port : ports) {
2179 // Expect only relay ports.
2180 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
2181 }
2182 for (const Candidate& candidate : candidates) {
2183 // Expect only relay candidates now that the filter is applied.
2184 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
2185 // Expect that the raddr is emptied due to the CF_RELAY filter.
2186 EXPECT_EQ(candidate.related_address(),
2187 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
2188 }
2189 }
2190
2191 // Test that candidates that do not match a previous candidate filter can be
2192 // surfaced if they match the new one after setting the filter value.
TEST_F(BasicPortAllocatorTest,SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes)2193 TEST_F(BasicPortAllocatorTest,
2194 SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes) {
2195 // We would still surface a host candidate if the IP is public, even though it
2196 // is disabled by the candidate filter. See
2197 // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so
2198 // that the srflx candidate is not equivalent to the host candidate.
2199 AddInterface(kPrivateAddr);
2200 ResetWithStunServerAndNat(kStunAddr);
2201
2202 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2203
2204 allocator_->set_flags(allocator().flags() |
2205 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2206 PORTALLOCATOR_DISABLE_TCP);
2207
2208 allocator_->SetCandidateFilter(CF_NONE);
2209 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2210 session_->StartGettingPorts();
2211 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2212 kDefaultAllocationTimeout, fake_clock);
2213 EXPECT_TRUE(candidates_.empty());
2214 EXPECT_TRUE(ports_.empty());
2215
2216 // Surface the relay candidate previously gathered but not signaled.
2217 session_->SetCandidateFilter(CF_RELAY);
2218 ASSERT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout,
2219 fake_clock);
2220 EXPECT_EQ(RELAY_PORT_TYPE, candidates_.back().type());
2221 EXPECT_EQ(1u, ports_.size());
2222
2223 // Surface the srflx candidate previously gathered but not signaled.
2224 session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE);
2225 ASSERT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout,
2226 fake_clock);
2227 EXPECT_EQ(STUN_PORT_TYPE, candidates_.back().type());
2228 EXPECT_EQ(2u, ports_.size());
2229
2230 // Surface the srflx candidate previously gathered but not signaled.
2231 session_->SetCandidateFilter(CF_ALL);
2232 ASSERT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout,
2233 fake_clock);
2234 EXPECT_EQ(LOCAL_PORT_TYPE, candidates_.back().type());
2235 EXPECT_EQ(2u, ports_.size());
2236 }
2237
2238 // This is a similar test as
2239 // SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes, and we
2240 // test the transitions for which the new filter value is not a super set of the
2241 // previous value.
TEST_F(BasicPortAllocatorTest,SurfaceNewCandidatesAfterSetCandidateFilterToAllowDifferentCandidateTypes)2242 TEST_F(
2243 BasicPortAllocatorTest,
2244 SurfaceNewCandidatesAfterSetCandidateFilterToAllowDifferentCandidateTypes) {
2245 // We would still surface a host candidate if the IP is public, even though it
2246 // is disabled by the candidate filter. See
2247 // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so
2248 // that the srflx candidate is not equivalent to the host candidate.
2249 AddInterface(kPrivateAddr);
2250 ResetWithStunServerAndNat(kStunAddr);
2251
2252 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2253
2254 allocator_->set_flags(allocator().flags() |
2255 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2256 PORTALLOCATOR_DISABLE_TCP);
2257
2258 allocator_->SetCandidateFilter(CF_NONE);
2259 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2260 session_->StartGettingPorts();
2261 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2262 kDefaultAllocationTimeout, fake_clock);
2263 EXPECT_TRUE(candidates_.empty());
2264 EXPECT_TRUE(ports_.empty());
2265
2266 // Surface the relay candidate previously gathered but not signaled.
2267 session_->SetCandidateFilter(CF_RELAY);
2268 EXPECT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout,
2269 fake_clock);
2270 EXPECT_EQ(RELAY_PORT_TYPE, candidates_.back().type());
2271 EXPECT_EQ(1u, ports_.size());
2272
2273 // Surface the srflx candidate previously gathered but not signaled.
2274 session_->SetCandidateFilter(CF_REFLEXIVE);
2275 EXPECT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout,
2276 fake_clock);
2277 EXPECT_EQ(STUN_PORT_TYPE, candidates_.back().type());
2278 EXPECT_EQ(2u, ports_.size());
2279
2280 // Surface the host candidate previously gathered but not signaled.
2281 session_->SetCandidateFilter(CF_HOST);
2282 EXPECT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout,
2283 fake_clock);
2284 EXPECT_EQ(LOCAL_PORT_TYPE, candidates_.back().type());
2285 // We use a shared socket and cricket::UDPPort handles the srflx candidate.
2286 EXPECT_EQ(2u, ports_.size());
2287 }
2288
2289 // Test that after an allocation session has stopped getting ports, changing the
2290 // candidate filter to allow new types of gathered candidates does not surface
2291 // any candidate.
TEST_F(BasicPortAllocatorTest,NoCandidateSurfacedWhenUpdatingCandidateFilterIfSessionStopped)2292 TEST_F(BasicPortAllocatorTest,
2293 NoCandidateSurfacedWhenUpdatingCandidateFilterIfSessionStopped) {
2294 AddInterface(kPrivateAddr);
2295 ResetWithStunServerAndNat(kStunAddr);
2296
2297 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
2298
2299 allocator_->set_flags(allocator().flags() |
2300 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
2301 PORTALLOCATOR_DISABLE_TCP);
2302
2303 allocator_->SetCandidateFilter(CF_NONE);
2304 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2305 session_->StartGettingPorts();
2306 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2307 kDefaultAllocationTimeout, fake_clock);
2308 auto test_invariants = [this]() {
2309 EXPECT_TRUE(candidates_.empty());
2310 EXPECT_TRUE(ports_.empty());
2311 };
2312
2313 test_invariants();
2314
2315 session_->StopGettingPorts();
2316
2317 session_->SetCandidateFilter(CF_RELAY);
2318 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2319 test_invariants();
2320
2321 session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE);
2322 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2323 test_invariants();
2324
2325 session_->SetCandidateFilter(CF_ALL);
2326 SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock);
2327 test_invariants();
2328 }
2329
TEST_F(BasicPortAllocatorTest,SetStunKeepaliveIntervalForPorts)2330 TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) {
2331 const int pool_size = 1;
2332 const int expected_stun_keepalive_interval = 123;
2333 AddInterface(kClientAddr);
2334 allocator_->SetConfiguration(
2335 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2336 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
2337 auto* pooled_session = allocator_->GetPooledSession();
2338 ASSERT_NE(nullptr, pooled_session);
2339 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2340 kDefaultAllocationTimeout, fake_clock);
2341 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2342 expected_stun_keepalive_interval);
2343 }
2344
TEST_F(BasicPortAllocatorTest,ChangeStunKeepaliveIntervalForPortsAfterInitialConfig)2345 TEST_F(BasicPortAllocatorTest,
2346 ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) {
2347 const int pool_size = 1;
2348 AddInterface(kClientAddr);
2349 allocator_->SetConfiguration(
2350 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2351 webrtc::NO_PRUNE, nullptr, 123 /* stun keepalive interval */);
2352 auto* pooled_session = allocator_->GetPooledSession();
2353 ASSERT_NE(nullptr, pooled_session);
2354 EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(),
2355 kDefaultAllocationTimeout, fake_clock);
2356 const int expected_stun_keepalive_interval = 321;
2357 allocator_->SetConfiguration(
2358 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2359 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
2360 CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session,
2361 expected_stun_keepalive_interval);
2362 }
2363
TEST_F(BasicPortAllocatorTest,SetStunKeepaliveIntervalForPortsWithSharedSocket)2364 TEST_F(BasicPortAllocatorTest,
2365 SetStunKeepaliveIntervalForPortsWithSharedSocket) {
2366 const int pool_size = 1;
2367 const int expected_stun_keepalive_interval = 123;
2368 AddInterface(kClientAddr);
2369 allocator_->set_flags(allocator().flags() |
2370 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
2371 allocator_->SetConfiguration(
2372 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2373 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
2374 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2375 session_->StartGettingPorts();
2376 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2377 kDefaultAllocationTimeout, fake_clock);
2378 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2379 expected_stun_keepalive_interval);
2380 }
2381
TEST_F(BasicPortAllocatorTest,SetStunKeepaliveIntervalForPortsWithoutSharedSocket)2382 TEST_F(BasicPortAllocatorTest,
2383 SetStunKeepaliveIntervalForPortsWithoutSharedSocket) {
2384 const int pool_size = 1;
2385 const int expected_stun_keepalive_interval = 123;
2386 AddInterface(kClientAddr);
2387 allocator_->set_flags(allocator().flags() &
2388 ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET));
2389 allocator_->SetConfiguration(
2390 allocator_->stun_servers(), allocator_->turn_servers(), pool_size,
2391 webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval);
2392 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2393 session_->StartGettingPorts();
2394 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2395 kDefaultAllocationTimeout, fake_clock);
2396 CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(),
2397 expected_stun_keepalive_interval);
2398 }
2399
TEST_F(BasicPortAllocatorTest,IceRegatheringMetricsLoggedWhenNetworkChanges)2400 TEST_F(BasicPortAllocatorTest, IceRegatheringMetricsLoggedWhenNetworkChanges) {
2401 // Only test local ports to simplify test.
2402 ResetWithNoServersOrNat();
2403 AddInterface(kClientAddr, "test_net0");
2404 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2405 session_->StartGettingPorts();
2406 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2407 kDefaultAllocationTimeout, fake_clock);
2408 candidate_allocation_done_ = false;
2409 AddInterface(kClientAddr2, "test_net1");
2410 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2411 kDefaultAllocationTimeout, fake_clock);
2412 EXPECT_METRIC_EQ(1,
2413 webrtc::metrics::NumEvents(
2414 "WebRTC.PeerConnection.IceRegatheringReason",
2415 static_cast<int>(IceRegatheringReason::NETWORK_CHANGE)));
2416 }
2417
2418 // Test that when an mDNS responder is present, the local address of a host
2419 // candidate is concealed by an mDNS hostname and the related address of a srflx
2420 // candidate is set to 0.0.0.0 or ::0.
TEST_F(BasicPortAllocatorTest,HostCandidateAddressIsReplacedByHostname)2421 TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) {
2422 // Default config uses GTURN and no NAT, so replace that with the
2423 // desired setup (NAT, STUN server, TURN server, UDP/TCP).
2424 ResetWithStunServerAndNat(kStunAddr);
2425 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
2426 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2427 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr);
2428
2429 ASSERT_EQ(&network_manager_, allocator().network_manager());
2430 network_manager_.set_mdns_responder(
2431 std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current()));
2432 AddInterface(kClientAddr);
2433 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2434 session_->StartGettingPorts();
2435 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2436 kDefaultAllocationTimeout, fake_clock);
2437 EXPECT_EQ(5u, candidates_.size());
2438 int num_host_udp_candidates = 0;
2439 int num_host_tcp_candidates = 0;
2440 int num_srflx_candidates = 0;
2441 int num_relay_candidates = 0;
2442 for (const auto& candidate : candidates_) {
2443 const auto& raddr = candidate.related_address();
2444
2445 if (candidate.type() == LOCAL_PORT_TYPE) {
2446 EXPECT_FALSE(candidate.address().hostname().empty());
2447 EXPECT_TRUE(raddr.IsNil());
2448 if (candidate.protocol() == UDP_PROTOCOL_NAME) {
2449 ++num_host_udp_candidates;
2450 } else {
2451 ++num_host_tcp_candidates;
2452 }
2453 } else if (candidate.type() == STUN_PORT_TYPE) {
2454 // For a srflx candidate, the related address should be set to 0.0.0.0 or
2455 // ::0
2456 EXPECT_TRUE(IPIsAny(raddr.ipaddr()));
2457 EXPECT_EQ(raddr.port(), 0);
2458 ++num_srflx_candidates;
2459 } else if (candidate.type() == RELAY_PORT_TYPE) {
2460 EXPECT_EQ(kNatUdpAddr.ipaddr(), raddr.ipaddr());
2461 EXPECT_EQ(kNatUdpAddr.family(), raddr.family());
2462 ++num_relay_candidates;
2463 } else {
2464 // prflx candidates are not expected
2465 FAIL();
2466 }
2467 }
2468 EXPECT_EQ(1, num_host_udp_candidates);
2469 EXPECT_EQ(1, num_host_tcp_candidates);
2470 EXPECT_EQ(1, num_srflx_candidates);
2471 EXPECT_EQ(2, num_relay_candidates);
2472 }
2473
TEST_F(BasicPortAllocatorTest,TestUseTurnServerAsStunSever)2474 TEST_F(BasicPortAllocatorTest, TestUseTurnServerAsStunSever) {
2475 ServerAddresses stun_servers;
2476 stun_servers.insert(kStunAddr);
2477 PortConfiguration port_config(stun_servers, "", "");
2478 RelayServerConfig turn_servers =
2479 CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2480 port_config.AddRelay(turn_servers);
2481
2482 EXPECT_EQ(2U, port_config.StunServers().size());
2483 }
2484
TEST_F(BasicPortAllocatorTest,TestDoNotUseTurnServerAsStunSever)2485 TEST_F(BasicPortAllocatorTest, TestDoNotUseTurnServerAsStunSever) {
2486 webrtc::test::ScopedKeyValueConfig field_trials(
2487 "WebRTC-UseTurnServerAsStunServer/Disabled/");
2488 ServerAddresses stun_servers;
2489 stun_servers.insert(kStunAddr);
2490 PortConfiguration port_config(stun_servers, "" /* user_name */,
2491 "" /* password */, &field_trials);
2492 RelayServerConfig turn_servers =
2493 CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
2494 port_config.AddRelay(turn_servers);
2495
2496 EXPECT_EQ(1U, port_config.StunServers().size());
2497 }
2498
2499 // Test that candidates from different servers get assigned a unique local
2500 // preference (the middle 16 bits of the priority)
TEST_F(BasicPortAllocatorTest,AssignsUniqueLocalPreferencetoRelayCandidates)2501 TEST_F(BasicPortAllocatorTest, AssignsUniqueLocalPreferencetoRelayCandidates) {
2502 allocator_->SetCandidateFilter(CF_RELAY);
2503 allocator_->AddTurnServerForTesting(
2504 CreateTurnServers(kTurnUdpIntAddr, SocketAddress()));
2505 allocator_->AddTurnServerForTesting(
2506 CreateTurnServers(kTurnUdpIntAddr, SocketAddress()));
2507 allocator_->AddTurnServerForTesting(
2508 CreateTurnServers(kTurnUdpIntAddr, SocketAddress()));
2509
2510 AddInterface(kClientAddr);
2511 ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
2512 session_->StartGettingPorts();
2513 ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2514 kDefaultAllocationTimeout, fake_clock);
2515 EXPECT_EQ(3u, candidates_.size());
2516 EXPECT_GT((candidates_[0].priority() >> 8) & 0xFFFF,
2517 (candidates_[1].priority() >> 8) & 0xFFFF);
2518 EXPECT_GT((candidates_[1].priority() >> 8) & 0xFFFF,
2519 (candidates_[2].priority() >> 8) & 0xFFFF);
2520 }
2521
2522 // Test that no more than allocator.max_ipv6_networks() IPv6 networks are used
2523 // to gather candidates.
TEST_F(BasicPortAllocatorTest,TwoIPv6AreSelectedBecauseOfMaxIpv6Limit)2524 TEST_F(BasicPortAllocatorTest, TwoIPv6AreSelectedBecauseOfMaxIpv6Limit) {
2525 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2526 64, rtc::ADAPTER_TYPE_WIFI);
2527 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2528 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2529 rtc::Network wifi2("wifi2", "Test NetworkAdapter 3",
2530 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI);
2531 std::vector<const rtc::Network*> networks = {&wifi1, ðe1, &wifi2};
2532
2533 // Ensure that only 2 interfaces were selected.
2534 EXPECT_EQ(2U, BasicPortAllocatorSession::SelectIPv6Networks(
2535 networks, /*max_ipv6_networks=*/2)
2536 .size());
2537 }
2538
2539 // Test that if the number of available IPv6 networks is less than
2540 // allocator.max_ipv6_networks(), all IPv6 networks will be selected.
TEST_F(BasicPortAllocatorTest,AllIPv6AreSelected)2541 TEST_F(BasicPortAllocatorTest, AllIPv6AreSelected) {
2542 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2543 64, rtc::ADAPTER_TYPE_WIFI);
2544 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2545 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2546 std::vector<const rtc::Network*> networks = {&wifi1, ðe1};
2547
2548 // Ensure that all 2 interfaces were selected.
2549 EXPECT_EQ(2U, BasicPortAllocatorSession::SelectIPv6Networks(
2550 networks, /*max_ipv6_networks=*/3)
2551 .size());
2552 }
2553
2554 // If there are some IPv6 networks with different types, diversify IPv6
2555 // networks.
TEST_F(BasicPortAllocatorTest,TwoIPv6WifiAreSelectedIfThereAreTwo)2556 TEST_F(BasicPortAllocatorTest, TwoIPv6WifiAreSelectedIfThereAreTwo) {
2557 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2558 64, rtc::ADAPTER_TYPE_WIFI);
2559 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2560 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2561 rtc::Network ethe2("ethe2", "Test NetworkAdapter 3",
2562 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2563 rtc::Network unknown1("unknown1", "Test NetworkAdapter 4",
2564 kClientIPv6Addr2.ipaddr(), 64,
2565 rtc::ADAPTER_TYPE_UNKNOWN);
2566 rtc::Network cell1("cell1", "Test NetworkAdapter 5",
2567 kClientIPv6Addr3.ipaddr(), 64,
2568 rtc::ADAPTER_TYPE_CELLULAR_4G);
2569 std::vector<const rtc::Network*> networks = {&wifi1, ðe1, ðe2,
2570 &unknown1, &cell1};
2571
2572 networks = BasicPortAllocatorSession::SelectIPv6Networks(
2573 networks, /*max_ipv6_networks=*/4);
2574
2575 EXPECT_EQ(4U, networks.size());
2576 // Ensure the expected 4 interfaces (wifi1, ethe1, cell1, unknown1) were
2577 // selected.
2578 EXPECT_TRUE(HasNetwork(networks, wifi1));
2579 EXPECT_TRUE(HasNetwork(networks, ethe1));
2580 EXPECT_TRUE(HasNetwork(networks, cell1));
2581 EXPECT_TRUE(HasNetwork(networks, unknown1));
2582 }
2583
2584 // If there are some IPv6 networks with the same type, select them because there
2585 // is no other option.
TEST_F(BasicPortAllocatorTest,IPv6WithSameTypeAreSelectedIfNoOtherOption)2586 TEST_F(BasicPortAllocatorTest, IPv6WithSameTypeAreSelectedIfNoOtherOption) {
2587 // Add 5 cellular interfaces
2588 rtc::Network cell1("cell1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2589 64, rtc::ADAPTER_TYPE_CELLULAR_2G);
2590 rtc::Network cell2("cell2", "Test NetworkAdapter 2",
2591 kClientIPv6Addr2.ipaddr(), 64,
2592 rtc::ADAPTER_TYPE_CELLULAR_3G);
2593 rtc::Network cell3("cell3", "Test NetworkAdapter 3",
2594 kClientIPv6Addr3.ipaddr(), 64,
2595 rtc::ADAPTER_TYPE_CELLULAR_4G);
2596 rtc::Network cell4("cell4", "Test NetworkAdapter 4",
2597 kClientIPv6Addr2.ipaddr(), 64,
2598 rtc::ADAPTER_TYPE_CELLULAR_5G);
2599 rtc::Network cell5("cell5", "Test NetworkAdapter 5",
2600 kClientIPv6Addr3.ipaddr(), 64,
2601 rtc::ADAPTER_TYPE_CELLULAR_3G);
2602 std::vector<const rtc::Network*> networks = {&cell1, &cell2, &cell3, &cell4,
2603 &cell5};
2604
2605 // Ensure that 4 interfaces were selected.
2606 EXPECT_EQ(4U, BasicPortAllocatorSession::SelectIPv6Networks(
2607 networks, /*max_ipv6_networks=*/4)
2608 .size());
2609 }
2610
TEST_F(BasicPortAllocatorTest,IPv6EthernetHasHigherPriorityThanWifi)2611 TEST_F(BasicPortAllocatorTest, IPv6EthernetHasHigherPriorityThanWifi) {
2612 rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2613 64, rtc::ADAPTER_TYPE_WIFI);
2614 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2615 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2616 rtc::Network wifi2("wifi2", "Test NetworkAdapter 3",
2617 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI);
2618 std::vector<const rtc::Network*> networks = {&wifi1, ðe1, &wifi2};
2619
2620 networks = BasicPortAllocatorSession::SelectIPv6Networks(
2621 networks, /*max_ipv6_networks=*/1);
2622
2623 EXPECT_EQ(1U, networks.size());
2624 // Ensure ethe1 was selected.
2625 EXPECT_TRUE(HasNetwork(networks, ethe1));
2626 }
2627
TEST_F(BasicPortAllocatorTest,IPv6EtherAndWifiHaveHigherPriorityThanOthers)2628 TEST_F(BasicPortAllocatorTest, IPv6EtherAndWifiHaveHigherPriorityThanOthers) {
2629 rtc::Network cell1("cell1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(),
2630 64, rtc::ADAPTER_TYPE_CELLULAR_3G);
2631 rtc::Network ethe1("ethe1", "Test NetworkAdapter 2",
2632 kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET);
2633 rtc::Network wifi1("wifi1", "Test NetworkAdapter 3",
2634 kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI);
2635 rtc::Network unknown("unknown", "Test NetworkAdapter 4",
2636 kClientIPv6Addr2.ipaddr(), 64,
2637 rtc::ADAPTER_TYPE_UNKNOWN);
2638 rtc::Network vpn1("vpn1", "Test NetworkAdapter 5", kClientIPv6Addr3.ipaddr(),
2639 64, rtc::ADAPTER_TYPE_VPN);
2640 std::vector<const rtc::Network*> networks = {&cell1, ðe1, &wifi1, &unknown,
2641 &vpn1};
2642
2643 networks = BasicPortAllocatorSession::SelectIPv6Networks(
2644 networks, /*max_ipv6_networks=*/2);
2645
2646 EXPECT_EQ(2U, networks.size());
2647 // Ensure ethe1 and wifi1 were selected.
2648 EXPECT_TRUE(HasNetwork(networks, wifi1));
2649 EXPECT_TRUE(HasNetwork(networks, ethe1));
2650 }
2651
2652 // Do not change the default IPv6 selection behavior if
2653 // IPv6NetworkResolutionFixes is disabled.
TEST_F(BasicPortAllocatorTest,NotDiversifyIPv6NetworkTypesIfIPv6NetworkResolutionFixesDisabled)2654 TEST_F(BasicPortAllocatorTest,
2655 NotDiversifyIPv6NetworkTypesIfIPv6NetworkResolutionFixesDisabled) {
2656 webrtc::test::ScopedKeyValueConfig field_trials(
2657 field_trials_, "WebRTC-IPv6NetworkResolutionFixes/Disabled/");
2658 // Add three IPv6 network interfaces, but tell the allocator to only use two.
2659 allocator().set_max_ipv6_networks(2);
2660 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2661 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2662 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2663 // To simplify the test, only gather UDP host candidates.
2664 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2665 PORTALLOCATOR_DISABLE_STUN |
2666 PORTALLOCATOR_DISABLE_RELAY |
2667 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2668
2669 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2670 session_->StartGettingPorts();
2671 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2672 kDefaultAllocationTimeout, fake_clock);
2673
2674 EXPECT_EQ(2U, candidates_.size());
2675 // Wifi1 was not selected because it comes after ethe1 and ethe2.
2676 EXPECT_FALSE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2677 }
2678
2679 // Do not change the default IPv6 selection behavior if
2680 // IPv6NetworkResolutionFixes is enabled but DiversifyIpv6Interfaces is not
2681 // enabled.
TEST_F(BasicPortAllocatorTest,NotDiversifyIPv6NetworkTypesIfDiversifyIpv6InterfacesDisabled)2682 TEST_F(BasicPortAllocatorTest,
2683 NotDiversifyIPv6NetworkTypesIfDiversifyIpv6InterfacesDisabled) {
2684 webrtc::test::ScopedKeyValueConfig field_trials(
2685 field_trials_,
2686 "WebRTC-IPv6NetworkResolutionFixes/"
2687 "Enabled,DiversifyIpv6Interfaces:false/");
2688 // Add three IPv6 network interfaces, but tell the allocator to only use two.
2689 allocator().set_max_ipv6_networks(2);
2690 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2691 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2692 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2693 // To simplify the test, only gather UDP host candidates.
2694 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2695 PORTALLOCATOR_DISABLE_STUN |
2696 PORTALLOCATOR_DISABLE_RELAY |
2697 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2698
2699 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2700 session_->StartGettingPorts();
2701 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2702 kDefaultAllocationTimeout, fake_clock);
2703
2704 EXPECT_EQ(2U, candidates_.size());
2705 // Wifi1 was not selected because it comes after ethe1 and ethe2.
2706 EXPECT_FALSE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2707 }
2708
TEST_F(BasicPortAllocatorTest,Select2DifferentIntefacesIfDiversifyIpv6InterfacesEnabled)2709 TEST_F(BasicPortAllocatorTest,
2710 Select2DifferentIntefacesIfDiversifyIpv6InterfacesEnabled) {
2711 webrtc::test::ScopedKeyValueConfig field_trials(
2712 field_trials_,
2713 "WebRTC-IPv6NetworkResolutionFixes/"
2714 "Enabled,DiversifyIpv6Interfaces:true/");
2715 allocator().set_max_ipv6_networks(2);
2716 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2717 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2718 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2719 AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI);
2720 AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G);
2721
2722 // To simplify the test, only gather UDP host candidates.
2723 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2724 PORTALLOCATOR_DISABLE_STUN |
2725 PORTALLOCATOR_DISABLE_RELAY |
2726 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2727
2728 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2729 session_->StartGettingPorts();
2730 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2731 kDefaultAllocationTimeout, fake_clock);
2732
2733 EXPECT_EQ(2U, candidates_.size());
2734 // ethe1 and wifi1 were selected.
2735 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2736 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2737 }
2738
TEST_F(BasicPortAllocatorTest,Select3DifferentIntefacesIfDiversifyIpv6InterfacesEnabled)2739 TEST_F(BasicPortAllocatorTest,
2740 Select3DifferentIntefacesIfDiversifyIpv6InterfacesEnabled) {
2741 webrtc::test::ScopedKeyValueConfig field_trials(
2742 field_trials_,
2743 "WebRTC-IPv6NetworkResolutionFixes/"
2744 "Enabled,DiversifyIpv6Interfaces:true/");
2745 allocator().set_max_ipv6_networks(3);
2746 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2747 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2748 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2749 AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI);
2750 AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G);
2751
2752 // To simplify the test, only gather UDP host candidates.
2753 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2754 PORTALLOCATOR_DISABLE_STUN |
2755 PORTALLOCATOR_DISABLE_RELAY |
2756 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2757
2758 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2759 session_->StartGettingPorts();
2760 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2761 kDefaultAllocationTimeout, fake_clock);
2762
2763 EXPECT_EQ(3U, candidates_.size());
2764 // ethe1, wifi1, and cell1 were selected.
2765 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2766 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2767 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr5));
2768 }
2769
TEST_F(BasicPortAllocatorTest,Select4DifferentIntefacesIfDiversifyIpv6InterfacesEnabled)2770 TEST_F(BasicPortAllocatorTest,
2771 Select4DifferentIntefacesIfDiversifyIpv6InterfacesEnabled) {
2772 webrtc::test::ScopedKeyValueConfig field_trials(
2773 field_trials_,
2774 "WebRTC-IPv6NetworkResolutionFixes/"
2775 "Enabled,DiversifyIpv6Interfaces:true/");
2776 allocator().set_max_ipv6_networks(4);
2777 AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET);
2778 AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET);
2779 AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI);
2780 AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI);
2781 AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G);
2782
2783 // To simplify the test, only gather UDP host candidates.
2784 allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP |
2785 PORTALLOCATOR_DISABLE_STUN |
2786 PORTALLOCATOR_DISABLE_RELAY |
2787 PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
2788
2789 ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
2790 session_->StartGettingPorts();
2791 EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_,
2792 kDefaultAllocationTimeout, fake_clock);
2793
2794 EXPECT_EQ(4U, candidates_.size());
2795 // ethe1, ethe2, wifi1, and cell1 were selected.
2796 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr));
2797 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2));
2798 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3));
2799 EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr5));
2800 }
2801
2802 } // namespace cricket
2803