blob: 83d904f181480deca88bc1701af4e7e451e6208c [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Taylor Brandstettera1c30352016-05-13 08:15:11 -070011#include <algorithm>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
13
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/basicpacketsocketfactory.h"
kjellanderf4752772016-03-02 05:42:30 -080015#include "webrtc/p2p/base/p2pconstants.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/p2p/base/testrelayserver.h"
18#include "webrtc/p2p/base/teststunserver.h"
19#include "webrtc/p2p/base/testturnserver.h"
20#include "webrtc/p2p/client/basicportallocator.h"
21#include "webrtc/p2p/client/httpportallocator.h"
22#include "webrtc/base/fakenetwork.h"
23#include "webrtc/base/firewallsocketserver.h"
24#include "webrtc/base/gunit.h"
25#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070026#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include "webrtc/base/logging.h"
28#include "webrtc/base/natserver.h"
29#include "webrtc/base/natsocketfactory.h"
30#include "webrtc/base/network.h"
31#include "webrtc/base/physicalsocketserver.h"
32#include "webrtc/base/socketaddress.h"
33#include "webrtc/base/ssladapter.h"
34#include "webrtc/base/thread.h"
35#include "webrtc/base/virtualsocketserver.h"
36
37using cricket::ServerAddresses;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080038using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039using rtc::SocketAddress;
40using rtc::Thread;
41
42static const SocketAddress kClientAddr("11.11.11.11", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070043static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000045static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070046static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
47 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070049static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
50static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
52static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
53static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
54static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
55static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
56static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
57static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
58static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
59static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
60static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
61static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
62
63// Minimum and maximum port for port range tests.
64static const int kMinPort = 10000;
65static const int kMaxPort = 10099;
66
67// Based on ICE_UFRAG_LENGTH
68static const char kIceUfrag0[] = "TESTICEUFRAG0000";
69// Based on ICE_PWD_LENGTH
70static const char kIcePwd0[] = "TESTICEPWD00000000000000";
71
72static const char kContentName[] = "test content";
73
74static const int kDefaultAllocationTimeout = 1000;
75static const char kTurnUsername[] = "test";
76static const char kTurnPassword[] = "test";
77
78namespace cricket {
79
80// Helper for dumping candidates
81std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
82 os << c.ToString();
83 return os;
84}
85
86} // namespace cricket
87
Taylor Brandstettera1c30352016-05-13 08:15:11 -070088class BasicPortAllocatorTest : public testing::Test,
89 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -070091 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092 : pss_(new rtc::PhysicalSocketServer),
93 vss_(new rtc::VirtualSocketServer(pss_.get())),
94 fss_(new rtc::FirewallSocketServer(vss_.get())),
95 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070096 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -070097 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -070098 stun_server_(
99 cricket::TestStunServer::Create(Thread::Current(), kStunAddr)),
100 relay_server_(Thread::Current(),
101 kRelayUdpIntAddr,
102 kRelayUdpExtAddr,
103 kRelayTcpIntAddr,
104 kRelayTcpExtAddr,
105 kRelaySslTcpIntAddr,
106 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
108 candidate_allocation_done_(false) {
109 cricket::ServerAddresses stun_servers;
110 stun_servers.insert(kStunAddr);
111 // Passing the addresses of GTURN servers will enable GTURN in
112 // Basicportallocator.
113 allocator_.reset(new cricket::BasicPortAllocator(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700114 &network_manager_, stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr,
115 kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 allocator_->set_step_delay(cricket::kMinimumStepDelay);
117 }
118
119 void AddInterface(const SocketAddress& addr) {
120 network_manager_.AddInterface(addr);
121 }
honghaiz8c404fa2015-09-28 07:59:43 -0700122 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
123 network_manager_.AddInterface(addr, if_name);
124 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800125 void AddInterface(const SocketAddress& addr,
126 const std::string& if_name,
127 rtc::AdapterType type) {
128 network_manager_.AddInterface(addr, if_name, type);
129 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800130 // The default route is the public address that STUN server will observe when
131 // the endpoint is sitting on the public internet and the local port is bound
132 // to the "any" address. This may be different from the default local address
133 // which the endpoint observes. This can occur if the route to the public
134 // endpoint like 8.8.8.8 (specified as the default local address) is
135 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700136 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
137 AddInterface(addr);
138 // When a binding comes from the any address, the |addr| will be used as the
139 // srflx address.
140 vss_->SetDefaultRoute(addr.ipaddr());
141 }
honghaiz8c404fa2015-09-28 07:59:43 -0700142 void RemoveInterface(const SocketAddress& addr) {
143 network_manager_.RemoveInterface(addr);
144 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 bool SetPortRange(int min_port, int max_port) {
146 return allocator_->SetPortRange(min_port, max_port);
147 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700148 // Endpoint is on the public network. No STUN or TURN.
149 void ResetWithNoServersOrNat() {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700150 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
151 allocator_->set_step_delay(cricket::kMinimumStepDelay);
152 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700153 // Endpoint is behind a NAT, with STUN specified.
154 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
155 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700156 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700157 // Endpoint is on the public network, with STUN specified.
158 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
159 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700161 // Endpoint is on the public network, with TURN specified.
162 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
163 const rtc::SocketAddress& tcp_turn) {
164 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 AddTurnServers(udp_turn, tcp_turn);
166 }
167
168 void AddTurnServers(const rtc::SocketAddress& udp_turn,
169 const rtc::SocketAddress& tcp_turn) {
deadbeef653b8e02015-11-11 12:55:10 -0800170 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800172 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173
174 if (!udp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800175 turn_server.ports.push_back(
176 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 }
178 if (!tcp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800179 turn_server.ports.push_back(
180 cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181 }
deadbeef653b8e02015-11-11 12:55:10 -0800182 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 }
184
185 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700186 session_ = CreateSession("session", component);
187 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000188 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700189 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 return true;
191 }
192
193 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700194 session_ = CreateSession("session", content_name, component);
195 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700197 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 return true;
199 }
200
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700201 std::unique_ptr<cricket::PortAllocatorSession> CreateSession(
202 const std::string& sid,
203 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 return CreateSession(sid, kContentName, component);
205 }
206
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700207 std::unique_ptr<cricket::PortAllocatorSession> CreateSession(
208 const std::string& sid,
209 const std::string& content_name,
210 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000211 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
212 }
213
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700214 std::unique_ptr<cricket::PortAllocatorSession> CreateSession(
215 const std::string& sid,
216 const std::string& content_name,
217 int component,
218 const std::string& ice_ufrag,
219 const std::string& ice_pwd) {
220 std::unique_ptr<cricket::PortAllocatorSession> session =
221 allocator_->CreateSession(sid, content_name, component, ice_ufrag,
222 ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000223 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700224 &BasicPortAllocatorTest::OnPortReady);
225 session->SignalCandidatesReady.connect(
226 this, &BasicPortAllocatorTest::OnCandidatesReady);
227 session->SignalCandidatesAllocationDone.connect(
228 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 return session;
230 }
231
232 static bool CheckCandidate(const cricket::Candidate& c,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700233 int component,
234 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 const std::string& proto,
236 const SocketAddress& addr) {
237 return (c.component() == component && c.type() == type &&
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700238 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
239 ((addr.port() == 0 && (c.address().port() != 0)) ||
240 (c.address().port() == addr.port())));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 }
242 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700243 int min_port,
244 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 return (addr.port() >= min_port && addr.port() <= max_port);
246 }
247
248 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
249 // We should only get this callback once, except in the mux test where
250 // we have multiple port allocation sessions.
251 if (session == session_.get()) {
252 ASSERT_FALSE(candidate_allocation_done_);
253 candidate_allocation_done_ = true;
254 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700255 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000256 }
257
258 // Check if all ports allocated have send-buffer size |expected|. If
259 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
260 void CheckSendBufferSizesOfAllPorts(int expected) {
261 std::vector<cricket::PortInterface*>::iterator it;
262 for (it = ports_.begin(); it < ports_.end(); ++it) {
263 int send_buffer_size;
264 if (expected == -1) {
265 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700266 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000267 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700268 EXPECT_EQ(0,
269 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000270 ASSERT_EQ(expected, send_buffer_size);
271 }
272 }
273 }
274
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700275 // This function starts the port/address gathering and check the existence of
276 // candidates as specified. When |expect_stun_candidate| is true,
277 // |stun_candidate_addr| carries the expected reflective address, which is
278 // also the related address for TURN candidate if it is expected. Otherwise,
279 // it should be ignore.
280 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200281 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700282 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700283 const rtc::IPAddress& stun_candidate_addr,
284 const rtc::IPAddress& relay_candidate_udp_transport_addr,
285 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800286 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
287 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700288 if (!session_) {
289 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
290 }
291 session_->set_flags(session_->flags() |
292 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700293 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
294 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000295 session_->StartGettingPorts();
296 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
297
Peter Boström0c4e06b2015-10-07 12:23:21 +0200298 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700299 if (!host_candidate_addr.IsNil()) {
300 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
301 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800302 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700303 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700304 }
305 if (!stun_candidate_addr.IsNil()) {
306 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700307 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
308 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800309 rtc::IPAddress related_address = host_candidate_addr;
310 if (host_candidate_addr.IsNil()) {
311 related_address =
312 rtc::GetAnyIP(candidates_[total_candidates].address().family());
313 }
314 EXPECT_EQ(related_address,
315 candidates_[total_candidates].related_address().ipaddr());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700316 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700317 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700318 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700319 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700320 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
321 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700322 EXPECT_EQ(stun_candidate_addr,
323 candidates_[total_candidates].related_address().ipaddr());
324 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700325 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700326 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700327 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700328 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
329 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700330 EXPECT_EQ(stun_candidate_addr,
331 candidates_[total_candidates].related_address().ipaddr());
332 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700333 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000334
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700335 EXPECT_EQ(total_candidates, candidates_.size());
336 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000337 }
338
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000339 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700340 cricket::BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341
342 void OnPortReady(cricket::PortAllocatorSession* ses,
343 cricket::PortInterface* port) {
344 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
345 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700346 // Make sure the new port is added to ReadyPorts.
347 auto ready_ports = ses->ReadyPorts();
348 EXPECT_NE(ready_ports.end(),
349 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350 }
351 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
352 const std::vector<cricket::Candidate>& candidates) {
353 for (size_t i = 0; i < candidates.size(); ++i) {
354 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
355 candidates_.push_back(candidates[i]);
356 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700357 // Make sure the new candidates are added to Candidates.
358 auto ses_candidates = ses->ReadyCandidates();
359 for (const cricket::Candidate& candidate : candidates) {
360 EXPECT_NE(
361 ses_candidates.end(),
362 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
363 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364 }
365
366 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800367 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
368 cricket::RelayServerConfig server_config = allocator_->turn_servers()[i];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 cricket::PortList::const_iterator relay_port;
370 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700371 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000372 if (proto_addr.address == relay_port->address &&
373 proto_addr.proto == relay_port->proto)
374 return true;
375 }
376 }
377 return false;
378 }
379
Guo-wei Shieh11477022015-08-15 09:28:41 -0700380 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
381 bool with_nat) {
382 if (with_nat) {
383 nat_server_.reset(new rtc::NATServer(
384 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
385 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
386 } else {
387 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
388 }
389
390 ServerAddresses stun_servers;
391 if (!stun_server.IsNil()) {
392 stun_servers.insert(stun_server);
393 }
394 allocator_.reset(new cricket::BasicPortAllocator(
395 &network_manager_, nat_socket_factory_.get(), stun_servers));
396 allocator().set_step_delay(cricket::kMinimumStepDelay);
397 }
398
kwiberg3ec46792016-04-27 07:22:53 -0700399 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
400 std::unique_ptr<rtc::VirtualSocketServer> vss_;
401 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000402 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700403 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000404 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700405 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
406 std::unique_ptr<cricket::TestStunServer> stun_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000407 cricket::TestRelayServer relay_server_;
408 cricket::TestTurnServer turn_server_;
409 rtc::FakeNetworkManager network_manager_;
kwiberg3ec46792016-04-27 07:22:53 -0700410 std::unique_ptr<cricket::BasicPortAllocator> allocator_;
411 std::unique_ptr<cricket::PortAllocatorSession> session_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000412 std::vector<cricket::PortInterface*> ports_;
413 std::vector<cricket::Candidate> candidates_;
414 bool candidate_allocation_done_;
415};
416
417// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700418TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000419 EXPECT_EQ(&network_manager_, allocator().network_manager());
420 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800421 ASSERT_EQ(1u, allocator().turn_servers().size());
422 EXPECT_EQ(cricket::RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000423 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800424 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
425 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700426 EXPECT_TRUE(HasRelayAddress(
427 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP)));
428 EXPECT_TRUE(HasRelayAddress(
429 cricket::ProtocolAddress(kRelayTcpIntAddr, cricket::PROTO_TCP)));
430 EXPECT_TRUE(HasRelayAddress(
431 cricket::ProtocolAddress(kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000432 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700433 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000434}
435
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800436// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700437TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800438 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
439 rtc::ADAPTER_TYPE_ETHERNET);
440 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
441 rtc::ADAPTER_TYPE_WIFI);
442 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
443 rtc::ADAPTER_TYPE_CELLULAR);
444 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
445 rtc::ADAPTER_TYPE_VPN);
446 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
447 rtc::ADAPTER_TYPE_LOOPBACK);
448 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
449 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
450 cricket::PORTALLOCATOR_DISABLE_RELAY |
451 cricket::PORTALLOCATOR_DISABLE_TCP);
452 session_->StartGettingPorts();
453 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
454 EXPECT_EQ(4U, candidates_.size());
455 for (cricket::Candidate candidate : candidates_) {
456 EXPECT_LT(candidate.address().ip(), 0x12345604U);
457 }
458}
459
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700460TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800461 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
462 rtc::ADAPTER_TYPE_ETHERNET);
463 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
464 rtc::ADAPTER_TYPE_WIFI);
465 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
466 rtc::ADAPTER_TYPE_CELLULAR);
467 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
468 rtc::ADAPTER_TYPE_LOOPBACK |
469 rtc::ADAPTER_TYPE_WIFI);
470 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
471 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
472 cricket::PORTALLOCATOR_DISABLE_RELAY |
473 cricket::PORTALLOCATOR_DISABLE_TCP);
474 session_->StartGettingPorts();
475 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
476 EXPECT_EQ(1U, candidates_.size());
477 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
478}
479
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000480// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700481TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000482 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
483 session_->StartGettingPorts();
484 // Waiting for one second to make sure BasicPortAllocatorSession has not
485 // called OnAllocate multiple times. In old behavior it's called every 250ms.
486 // When there are no network interfaces, each execution of OnAllocate will
487 // result in SignalCandidatesAllocationDone signal.
488 rtc::Thread::Current()->ProcessMessages(1000);
489 EXPECT_TRUE(candidate_allocation_done_);
490 EXPECT_EQ(0U, candidates_.size());
491}
492
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700493// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700494TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800495 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
496 allocator_->SetNetworkIgnoreMask(0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700497 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
498 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
499 cricket::PORTALLOCATOR_DISABLE_RELAY |
500 cricket::PORTALLOCATOR_DISABLE_TCP);
501 session_->StartGettingPorts();
502 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
503 EXPECT_EQ(1U, candidates_.size());
504}
505
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000506// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700507TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000508 AddInterface(kClientAddr);
509 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
510 session_->StartGettingPorts();
511 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
512 EXPECT_EQ(4U, ports_.size());
513 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700514 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
515 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000516 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700517 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
518 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000519 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700520 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
521 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700523 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
524 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000525 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700526 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
527 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000528 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700529 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
530 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000531 EXPECT_PRED5(CheckCandidate, candidates_[6],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700532 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
533 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000534 EXPECT_TRUE(candidate_allocation_done_);
535}
536
honghaiz8c404fa2015-09-28 07:59:43 -0700537// Test that when the same network interface is brought down and up, the
538// port allocator session will restart a new allocation sequence if
539// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700540TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700541 std::string if_name("test_net0");
542 AddInterface(kClientAddr, if_name);
543 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
544 session_->StartGettingPorts();
545 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
546 EXPECT_EQ(4U, ports_.size());
547 EXPECT_TRUE(candidate_allocation_done_);
548 candidate_allocation_done_ = false;
549 candidates_.clear();
550 ports_.clear();
551
552 RemoveInterface(kClientAddr);
553 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
554 EXPECT_EQ(0U, ports_.size());
555 EXPECT_FALSE(candidate_allocation_done_);
556
557 // When the same interfaces are added again, new candidates/ports should be
558 // generated.
559 AddInterface(kClientAddr, if_name);
560 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
561 EXPECT_EQ(4U, ports_.size());
562 EXPECT_TRUE(candidate_allocation_done_);
563}
564
565// Test that when the same network interface is brought down and up, the
566// port allocator session will not restart a new allocation sequence if
567// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700568TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700569 std::string if_name("test_net0");
570 AddInterface(kClientAddr, if_name);
571 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
572 session_->StartGettingPorts();
573 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
574 EXPECT_EQ(4U, ports_.size());
575 EXPECT_TRUE(candidate_allocation_done_);
576 session_->StopGettingPorts();
577 candidates_.clear();
578 ports_.clear();
579
580 RemoveInterface(kClientAddr);
581 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
582 EXPECT_EQ(0U, ports_.size());
583
584 // When the same interfaces are added again, new candidates/ports should not
585 // be generated because the session has stopped.
586 AddInterface(kClientAddr, if_name);
587 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
588 EXPECT_EQ(0U, ports_.size());
589 EXPECT_TRUE(candidate_allocation_done_);
590}
591
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000592// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700593TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000594 AddInterface(kClientAddr);
595 allocator_->set_step_delay(cricket::kDefaultStepDelay);
596 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
597 session_->StartGettingPorts();
598 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
599 EXPECT_EQ(2U, ports_.size());
600 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
601 EXPECT_EQ(3U, ports_.size());
602 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700603 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
604 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000605 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700606 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
607 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000608 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
609 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700610 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
611 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000612 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700613 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
614 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000615 EXPECT_EQ(4U, ports_.size());
616 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
617 EXPECT_PRED5(CheckCandidate, candidates_[6],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700618 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
619 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000620 EXPECT_EQ(4U, ports_.size());
621 EXPECT_TRUE(candidate_allocation_done_);
622 // If we Stop gathering now, we shouldn't get a second "done" callback.
623 session_->StopGettingPorts();
624}
625
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700626TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000627 AddInterface(kClientAddr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700628 EXPECT_TRUE(
629 CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP, cricket::CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000630 session_->StartGettingPorts();
631 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
632 EXPECT_TRUE(candidate_allocation_done_);
633 // If we Stop gathering now, we shouldn't get a second "done" callback.
634 session_->StopGettingPorts();
635
636 // All ports should have unset send-buffer sizes.
637 CheckSendBufferSizesOfAllPorts(-1);
638}
639
640// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700641TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000642 AddInterface(kClientAddr);
643 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
644 session_->StartGettingPorts();
645 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
646 EXPECT_EQ(2U, ports_.size());
647 session_->StopGettingPorts();
648 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
649}
650
651// Test that we restrict client ports appropriately when a port range is set.
652// We check the candidates for udp/stun/tcp ports, and the from address
653// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700654TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000655 AddInterface(kClientAddr);
656 // Check that an invalid port range fails.
657 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
658 // Check that a null port range succeeds.
659 EXPECT_TRUE(SetPortRange(0, 0));
660 // Check that a valid port range succeeds.
661 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
662 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
663 session_->StartGettingPorts();
664 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
665 EXPECT_EQ(4U, ports_.size());
666 // Check the port number for the UDP port object.
667 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
668 // Check the port number for the STUN port object.
669 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
670 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700671 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
672 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000673 // Check the port number for the TCP port object.
674 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
675 EXPECT_TRUE(candidate_allocation_done_);
676}
677
678// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700679TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000680 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
681 session_->StartGettingPorts();
682 rtc::Thread::Current()->ProcessMessages(100);
683 // Without network adapter, we should not get any candidate.
684 EXPECT_EQ(0U, candidates_.size());
685 EXPECT_TRUE(candidate_allocation_done_);
686}
687
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700688// Test that when enumeration is disabled, we should not have any ports when
689// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700690TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700691 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700692 ResetWithStunServerNoNat(kStunAddr);
693 allocator().set_candidate_filter(cricket::CF_RELAY);
694 // Expect to see no ports and no candidates.
695 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
696 rtc::IPAddress(), rtc::IPAddress());
697}
698
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800699// Test that even with multiple interfaces, the result should still be a single
700// default private, one STUN and one TURN candidate since we bind to any address
701// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700702TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700703 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000704 AddInterface(kPrivateAddr);
705 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700706 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000707 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800708
709 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
710 // address set and we have no IPv6 STUN server, there should be no IPv6
711 // candidates.
712 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
713 session_->set_flags(cricket::PORTALLOCATOR_ENABLE_IPV6);
714
715 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
716 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
717 // TURN/UDP candidates.
718 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800719 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
720 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700721}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000722
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800723// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
724// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700725TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700726 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800727 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700728 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700729 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800730 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
731 // private, STUN, TURN/UDP, and TURN/TCP candidates.
732 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
733 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700734 kTurnUdpExtAddr.ipaddr());
735}
736
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800737// Test that when adapter enumeration is disabled, for endpoints without
738// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739TEST_F(BasicPortAllocatorTest,
740 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800741 ResetWithNoServersOrNat();
742 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
743 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
744 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700745}
746
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800747// Test that when adapter enumeration is disabled, with
748// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
749// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700750TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800751 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
752 ResetWithStunServerNoNat(kStunAddr);
753 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
754 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
755 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
756 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700757 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700758 rtc::IPAddress(), rtc::IPAddress());
759}
760
761// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800762// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
763// a NAT, there is no local candidate. However, this specified default route
764// (kClientAddr) which was discovered when sending STUN requests, will become
765// the srflx addresses.
766TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700767 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800768 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700769 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800770 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700771 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800772 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700773 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
774 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800775 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
776 rtc::IPAddress(), rtc::IPAddress());
777}
778
779// Test that when adapter enumeration is disabled, with
780// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
781// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700782TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800783 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
784 ResetWithStunServerAndNat(kStunAddr);
785 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
786 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
787 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
788 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
789 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000790}
791
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700792// Test that we disable relay over UDP, and only TCP is used when connecting to
793// the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700794TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700795 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
796 AddInterface(kClientAddr);
797 ResetWithStunServerAndNat(kStunAddr);
798 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
799 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
800 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY |
801 cricket::PORTALLOCATOR_DISABLE_UDP |
802 cricket::PORTALLOCATOR_DISABLE_STUN |
803 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
804
805 session_->StartGettingPorts();
806 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
807
808 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
809 // TURN/TCP candidates.
810 EXPECT_EQ(2U, ports_.size());
811 EXPECT_EQ(2U, candidates_.size());
812 EXPECT_PRED5(CheckCandidate, candidates_[0],
813 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
814 kTurnUdpExtAddr);
815 // The TURN candidate should use TCP to contact the TURN server.
816 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
817 EXPECT_PRED5(CheckCandidate, candidates_[1],
818 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
819 kClientAddr);
820}
821
Erik Språngefdce692015-06-05 09:41:26 +0200822// Disable for asan, see
823// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
824#if !defined(ADDRESS_SANITIZER)
825
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000826// Test that we can get OnCandidatesAllocationDone callback when all the ports
827// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700828TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000829 AddInterface(kClientAddr);
830 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
831 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
832 cricket::PORTALLOCATOR_DISABLE_STUN |
833 cricket::PORTALLOCATOR_DISABLE_RELAY |
834 cricket::PORTALLOCATOR_DISABLE_TCP);
835 session_->StartGettingPorts();
836 rtc::Thread::Current()->ProcessMessages(100);
837 EXPECT_EQ(0U, candidates_.size());
838 EXPECT_TRUE(candidate_allocation_done_);
839}
840
841// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700842TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000843 AddInterface(kClientAddr);
844 fss_->set_udp_sockets_enabled(false);
845 EXPECT_TRUE(CreateSession(1));
846 session_->StartGettingPorts();
847 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
848 EXPECT_EQ(2U, ports_.size());
849 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700850 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
851 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000852 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700853 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
854 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000855 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700856 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
857 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000858 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700859 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
860 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000861 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700862 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
863 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864 EXPECT_TRUE(candidate_allocation_done_);
865}
866
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700867#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200868
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000869// Test that we don't crash or malfunction if we can't create UDP sockets or
870// listen on TCP sockets. We still give out a local TCP address, since
871// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700872TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 AddInterface(kClientAddr);
874 fss_->set_udp_sockets_enabled(false);
875 fss_->set_tcp_listen_enabled(false);
876 EXPECT_TRUE(CreateSession(1));
877 session_->StartGettingPorts();
878 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
879 EXPECT_EQ(2U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700880 EXPECT_PRED5(CheckCandidate, candidates_[0], 1, "relay", "udp",
881 kRelayUdpIntAddr);
882 EXPECT_PRED5(CheckCandidate, candidates_[1], 1, "relay", "udp",
883 kRelayUdpExtAddr);
884 EXPECT_PRED5(CheckCandidate, candidates_[2], 1, "relay", "tcp",
885 kRelayTcpIntAddr);
886 EXPECT_PRED5(CheckCandidate, candidates_[3], 1, "local", "tcp", kClientAddr);
887 EXPECT_PRED5(CheckCandidate, candidates_[4], 1, "relay", "ssltcp",
888 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000889 EXPECT_TRUE(candidate_allocation_done_);
890}
891
892// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700893// TODO(deadbeef): Find a way to exit early here.
894TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 AddInterface(kClientAddr);
896 fss_->set_tcp_sockets_enabled(false);
897 fss_->set_udp_sockets_enabled(false);
898 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
899 session_->StartGettingPorts();
900 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700901 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000902 // In case of Relay, ports creation will succeed but sockets will fail.
903 // There is no error reporting from RelayEntry to handle this failure.
904}
905
906// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700907TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000908 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
909 AddInterface(kClientAddr);
910 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
911 session_->StartGettingPorts();
912 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
913 EXPECT_EQ(2U, ports_.size());
914 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700915 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
916 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000917 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700918 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
919 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
921 // will be tried after 3 seconds.
922 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
923 EXPECT_EQ(3U, ports_.size());
924 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700925 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
926 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000927 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700928 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
929 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700931 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
932 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700934 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
935 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 // Stun Timeout is 9sec.
937 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
938}
939
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700940TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000941 AddInterface(kClientAddr);
942 AddInterface(kClientAddr2);
943 // Allocating only host UDP ports. This is done purely for testing
944 // convenience.
945 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
946 cricket::PORTALLOCATOR_DISABLE_STUN |
947 cricket::PORTALLOCATOR_DISABLE_RELAY);
948 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
949 session_->StartGettingPorts();
950 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
951 ASSERT_EQ(2U, candidates_.size());
952 EXPECT_EQ(2U, ports_.size());
953 // Candidates priorities should be different.
954 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
955}
956
957// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700958TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000959 AddInterface(kClientAddr);
960 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
961 session_->StartGettingPorts();
962 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
963 EXPECT_EQ(4U, ports_.size());
964 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700965 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000966}
967
968// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
969// This test also verifies that when the allocator is only allowed to use
970// relay (i.e. IceTransportsType is relay), the raddr is an empty
971// address with the correct family. This is to prevent any local
972// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700973TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000974 AddInterface(kClientAddr);
975 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700976 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000977 allocator().set_candidate_filter(cricket::CF_RELAY);
978 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
979 session_->StartGettingPorts();
980 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700981 EXPECT_PRED5(CheckCandidate, candidates_[0],
982 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000983 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
984
985 EXPECT_EQ(1U, candidates_.size());
986 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
987 for (size_t i = 0; i < candidates_.size(); ++i) {
988 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
989 EXPECT_EQ(
990 candidates_[0].related_address(),
991 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
992 }
993}
994
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700995TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000996 AddInterface(kClientAddr);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700997 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000998 allocator().set_candidate_filter(cricket::CF_HOST);
999 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1000 session_->StartGettingPorts();
1001 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001002 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1003 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001004 for (size_t i = 0; i < candidates_.size(); ++i) {
1005 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
1006 }
1007}
1008
1009// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001010TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001011 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001012 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001013
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001014 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001015 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
1016 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1017 session_->StartGettingPorts();
1018 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1019 // Host is behind NAT, no private address will be exposed. Hence only UDP
1020 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001021 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1022 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001023 for (size_t i = 0; i < candidates_.size(); ++i) {
1024 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
1025 EXPECT_EQ(
1026 candidates_[0].related_address(),
1027 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
1028 }
1029}
1030
1031// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001032TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001033 AddInterface(kClientAddr);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001034 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001035 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
1036 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1037 session_->StartGettingPorts();
1038 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1039 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001040 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001041 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
1042 for (size_t i = 0; i < candidates_.size(); ++i) {
1043 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
1044 }
1045}
1046
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001047// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001048TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001049 AddInterface(kClientAddr);
1050 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1051 session_->StartGettingPorts();
1052 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1053 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001054 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1055 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001056 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001057 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1058 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001059 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001060 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1061 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001062 EXPECT_EQ(4U, ports_.size());
1063 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
1064 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
1065 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
1066 EXPECT_EQ(kIcePwd0, candidates_[0].password());
1067 EXPECT_EQ(kIcePwd0, candidates_[1].password());
1068 EXPECT_TRUE(candidate_allocation_done_);
1069}
1070
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001071// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1072// is allocated for udp and stun. Also verify there is only one candidate
1073// (local) if stun candidate is same as local candidate, which will be the case
1074// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001075TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001076 AddInterface(kClientAddr);
1077 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001078 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1079 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1080 session_->StartGettingPorts();
1081 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1082 EXPECT_EQ(3U, ports_.size());
1083 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001084 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1085 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001086 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1087}
1088
1089// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1090// is allocated for udp and stun. In this test we should expect both stun and
1091// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001092TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001093 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001094 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001095
1096 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001097 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1098 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1099 session_->StartGettingPorts();
1100 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1101 ASSERT_EQ(2U, ports_.size());
1102 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001103 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1104 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001105 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001106 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1107 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001108 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1109 EXPECT_EQ(3U, candidates_.size());
1110}
1111
deadbeefc5d0d952015-07-16 10:22:21 -07001112// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001113TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001114 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1115 AddInterface(kClientAddr);
1116 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
1117
1118 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1119
1120 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1121 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001122 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1123 cricket::PORTALLOCATOR_DISABLE_TCP);
1124
1125 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1126 session_->StartGettingPorts();
1127
1128 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1129 ASSERT_EQ(3U, ports_.size());
1130 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001131 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1132 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001133 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001134 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1135 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001136 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001137 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1138 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001139 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1140 EXPECT_EQ(3U, candidates_.size());
1141}
1142
1143// Testing DNS resolve for the TURN server, this will test AllocationSequence
1144// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001145TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
1147 cricket::PROTO_UDP);
1148 AddInterface(kClientAddr);
1149 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
deadbeef653b8e02015-11-11 12:55:10 -08001150 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001151 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001152 turn_server.credentials = credentials;
1153 turn_server.ports.push_back(cricket::ProtocolAddress(
1154 rtc::SocketAddress("localhost", 3478), cricket::PROTO_UDP, false));
1155 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156
1157 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1158 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1160 cricket::PORTALLOCATOR_DISABLE_TCP);
1161
1162 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1163 session_->StartGettingPorts();
1164
1165 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1166}
1167
1168// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1169// is allocated for udp/stun/turn. In this test we should expect all local,
1170// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001171TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001172 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001173 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001174
1175 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1176
1177 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001178 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1179 cricket::PORTALLOCATOR_DISABLE_TCP);
1180
1181 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1182 session_->StartGettingPorts();
1183
1184 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1185 ASSERT_EQ(2U, ports_.size());
1186 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001187 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1188 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001190 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1191 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001192 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001193 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1194 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1196 EXPECT_EQ(3U, candidates_.size());
1197 // Local port will be created first and then TURN port.
1198 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1199 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1200}
1201
1202// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1203// server is also used as the STUN server, we should get 'local', 'stun', and
1204// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001205TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001206 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001207 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001208 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001209 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1210
1211 // Must set the step delay to 0 to make sure the relay allocation phase is
1212 // started before the STUN candidates are obtained, so that the STUN binding
1213 // response is processed when both StunPort and TurnPort exist to reproduce
1214 // webrtc issue 3537.
1215 allocator_->set_step_delay(0);
1216 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001217 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1218 cricket::PORTALLOCATOR_DISABLE_TCP);
1219
1220 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1221 session_->StartGettingPorts();
1222
1223 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1224 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001225 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1226 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001227 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001228 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1229 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001230 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001231 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1232 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001233 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1234
1235 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1236 EXPECT_EQ(3U, candidates_.size());
1237 // Local port will be created first and then TURN port.
1238 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1239 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1240}
1241
deadbeefc5d0d952015-07-16 10:22:21 -07001242// Test that when only a TCP TURN server is available, we do NOT use it as
1243// a UDP STUN server, as this could leak our IP address. Thus we should only
1244// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001245TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
deadbeefc5d0d952015-07-16 10:22:21 -07001246 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1247 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001248 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001249 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1250
1251 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001252 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1253 cricket::PORTALLOCATOR_DISABLE_TCP);
1254
1255 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1256 session_->StartGettingPorts();
1257
1258 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1259 ASSERT_EQ(2U, ports_.size());
1260 EXPECT_PRED5(CheckCandidate, candidates_[0],
1261 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1262 kClientAddr);
1263 EXPECT_PRED5(CheckCandidate, candidates_[1],
1264 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1265 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1266 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1267 EXPECT_EQ(2U, candidates_.size());
1268 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1269 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1270}
1271
1272// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1273// TURN server is used as the STUN server and we get 'local', 'stun', and
1274// 'relay' candidates.
1275// TODO(deadbeef): Remove this test when support for non-shared socket mode
1276// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001277TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001278 AddInterface(kClientAddr);
1279 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001280 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001281 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1282
1283 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001284 cricket::PORTALLOCATOR_DISABLE_TCP);
1285
1286 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1287 session_->StartGettingPorts();
1288
1289 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1290 ASSERT_EQ(3U, ports_.size());
1291 EXPECT_PRED5(CheckCandidate, candidates_[0],
1292 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1293 kClientAddr);
1294 EXPECT_PRED5(CheckCandidate, candidates_[1],
1295 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1296 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1297 EXPECT_PRED5(CheckCandidate, candidates_[2],
1298 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1299 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1300 // Not using shared socket, so the STUN request's server reflexive address
1301 // should be different than the TURN request's server reflexive address.
1302 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1303
1304 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1305 EXPECT_EQ(3U, candidates_.size());
1306 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1307 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1308 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1309}
1310
1311// Test that even when both a STUN and TURN server are configured, the TURN
1312// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001313TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001314 AddInterface(kClientAddr);
1315 // Configure with STUN server but destroy it, so we can ensure that it's
1316 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001317 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001318 stun_server_.reset();
1319 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1320
1321 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001322 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1323 cricket::PORTALLOCATOR_DISABLE_TCP);
1324
1325 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1326 session_->StartGettingPorts();
1327
1328 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1329 EXPECT_PRED5(CheckCandidate, candidates_[0],
1330 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1331 kClientAddr);
1332 EXPECT_PRED5(CheckCandidate, candidates_[1],
1333 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1334 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1335 EXPECT_PRED5(CheckCandidate, candidates_[2],
1336 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1337 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1338 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1339
1340 // Don't bother waiting for STUN timeout, since we already verified
1341 // that we got a STUN candidate from the TURN server.
1342}
1343
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001344// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1345// and fail to generate STUN candidate, local UDP candidate is generated
1346// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001347TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001348 allocator().set_flags(allocator().flags() |
1349 cricket::PORTALLOCATOR_DISABLE_RELAY |
1350 cricket::PORTALLOCATOR_DISABLE_TCP |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001351 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1352 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1353 AddInterface(kClientAddr);
1354 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1355 session_->StartGettingPorts();
1356 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1357 EXPECT_EQ(1U, candidates_.size());
1358 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001359 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1360 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001361 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1362 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1363 EXPECT_EQ(1U, candidates_.size());
1364}
1365
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001366// Test that when the NetworkManager doesn't have permission to enumerate
1367// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1368// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001369TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001370 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1371 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001372 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001373 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001374 allocator().set_flags(allocator().flags() |
1375 cricket::PORTALLOCATOR_DISABLE_RELAY |
1376 cricket::PORTALLOCATOR_DISABLE_TCP |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001377 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001378 EXPECT_EQ(0U, allocator_->flags() &
1379 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001380 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001381 EXPECT_EQ(0U, session_->flags() &
1382 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001383 session_->StartGettingPorts();
1384 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001385 EXPECT_EQ(1U, candidates_.size());
1386 EXPECT_PRED5(CheckCandidate, candidates_[0],
1387 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1388 kPrivateAddr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001389 EXPECT_NE(0U, session_->flags() &
1390 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001391}
1392
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001393// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001394TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001395 allocator().set_flags(allocator().flags() |
1396 cricket::PORTALLOCATOR_DISABLE_RELAY |
1397 cricket::PORTALLOCATOR_ENABLE_IPV6 |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1399 AddInterface(kClientIPv6Addr);
1400 AddInterface(kClientAddr);
1401 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1402 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1403 session_->StartGettingPorts();
1404 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1405 EXPECT_EQ(4U, candidates_.size());
1406 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1407 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001408 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1409 kClientIPv6Addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001410 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001411 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1412 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001414 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1415 kClientIPv6Addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001417 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1418 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419 EXPECT_EQ(4U, candidates_.size());
1420}
honghaiz98db68f2015-09-29 07:58:17 -07001421
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001422TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001423 AddInterface(kClientAddr);
1424 allocator_->set_step_delay(cricket::kDefaultStepDelay);
1425 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1426 session_->StartGettingPorts();
1427 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1428 EXPECT_EQ(2U, ports_.size());
1429 session_->StopGettingPorts();
1430 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1431
1432 // After stopping getting ports, adding a new interface will not start
1433 // getting ports again.
1434 candidates_.clear();
1435 ports_.clear();
1436 candidate_allocation_done_ = false;
1437 network_manager_.AddInterface(kClientAddr2);
1438 rtc::Thread::Current()->ProcessMessages(1000);
1439 EXPECT_EQ(0U, candidates_.size());
1440 EXPECT_EQ(0U, ports_.size());
1441}
1442
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001443TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001444 AddInterface(kClientAddr);
1445 allocator_->set_step_delay(cricket::kDefaultStepDelay);
1446 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1447 session_->StartGettingPorts();
1448 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1449 EXPECT_EQ(2U, ports_.size());
1450 session_->ClearGettingPorts();
1451 WAIT(candidate_allocation_done_, 1000);
1452 EXPECT_FALSE(candidate_allocation_done_);
1453
1454 // After clearing getting ports, adding a new interface will start getting
1455 // ports again.
1456 candidates_.clear();
1457 ports_.clear();
1458 candidate_allocation_done_ = false;
1459 network_manager_.AddInterface(kClientAddr2);
1460 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1461 EXPECT_EQ(2U, ports_.size());
1462}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001463
1464// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1465// a pooled session is taken out of the pool.
1466TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1467 AddInterface(kClientAddr);
1468 int pool_size = 1;
1469 allocator_->SetConfiguration(allocator_->stun_servers(),
1470 allocator_->turn_servers(), pool_size);
1471 const cricket::PortAllocatorSession* peeked_session =
1472 allocator_->GetPooledSession();
1473 ASSERT_NE(nullptr, peeked_session);
1474 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1475 kDefaultAllocationTimeout);
1476 // Expect that when TakePooledSession is called,
1477 // UpdateTransportInformationInternal will be called and the
1478 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1479 // candidates.
1480 session_ =
1481 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1482 ASSERT_NE(nullptr, session_.get());
1483 auto ready_ports = session_->ReadyPorts();
1484 auto candidates = session_->ReadyCandidates();
1485 EXPECT_FALSE(ready_ports.empty());
1486 EXPECT_FALSE(candidates.empty());
1487 for (const cricket::PortInterface* port_interface : ready_ports) {
1488 const cricket::Port* port =
1489 static_cast<const cricket::Port*>(port_interface);
1490 EXPECT_EQ(kContentName, port->content_name());
1491 EXPECT_EQ(1, port->component());
1492 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1493 EXPECT_EQ(kIcePwd0, port->password());
1494 }
1495 for (const cricket::Candidate& candidate : candidates) {
1496 EXPECT_EQ(1, candidate.component());
1497 EXPECT_EQ(kIceUfrag0, candidate.username());
1498 EXPECT_EQ(kIcePwd0, candidate.password());
1499 }
1500}