blob: fba7f9932c8b9ccfa2f4e618295323c2b91854a5 [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
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070078// STUN timeout (with all retries) is 9500ms.
79// Add some margin of error for slow bots.
80// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
81// fix flaky tests.
82static const int kStunTimeoutMs = 15000;
83
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084namespace cricket {
85
86// Helper for dumping candidates
87std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
88 os << c.ToString();
89 return os;
90}
91
92} // namespace cricket
93
Taylor Brandstettera1c30352016-05-13 08:15:11 -070094class BasicPortAllocatorTest : public testing::Test,
95 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -070097 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 : pss_(new rtc::PhysicalSocketServer),
99 vss_(new rtc::VirtualSocketServer(pss_.get())),
100 fss_(new rtc::FirewallSocketServer(vss_.get())),
101 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -0700102 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700103 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700104 stun_server_(
105 cricket::TestStunServer::Create(Thread::Current(), kStunAddr)),
106 relay_server_(Thread::Current(),
107 kRelayUdpIntAddr,
108 kRelayUdpExtAddr,
109 kRelayTcpIntAddr,
110 kRelayTcpExtAddr,
111 kRelaySslTcpIntAddr,
112 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
114 candidate_allocation_done_(false) {
115 cricket::ServerAddresses stun_servers;
116 stun_servers.insert(kStunAddr);
117 // Passing the addresses of GTURN servers will enable GTURN in
118 // Basicportallocator.
119 allocator_.reset(new cricket::BasicPortAllocator(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700120 &network_manager_, stun_servers, kRelayUdpIntAddr, kRelayTcpIntAddr,
121 kRelaySslTcpIntAddr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 allocator_->set_step_delay(cricket::kMinimumStepDelay);
123 }
124
125 void AddInterface(const SocketAddress& addr) {
126 network_manager_.AddInterface(addr);
127 }
honghaiz8c404fa2015-09-28 07:59:43 -0700128 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
129 network_manager_.AddInterface(addr, if_name);
130 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800131 void AddInterface(const SocketAddress& addr,
132 const std::string& if_name,
133 rtc::AdapterType type) {
134 network_manager_.AddInterface(addr, if_name, type);
135 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800136 // The default route is the public address that STUN server will observe when
137 // the endpoint is sitting on the public internet and the local port is bound
138 // to the "any" address. This may be different from the default local address
139 // which the endpoint observes. This can occur if the route to the public
140 // endpoint like 8.8.8.8 (specified as the default local address) is
141 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700142 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
143 AddInterface(addr);
144 // When a binding comes from the any address, the |addr| will be used as the
145 // srflx address.
146 vss_->SetDefaultRoute(addr.ipaddr());
147 }
honghaiz8c404fa2015-09-28 07:59:43 -0700148 void RemoveInterface(const SocketAddress& addr) {
149 network_manager_.RemoveInterface(addr);
150 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 bool SetPortRange(int min_port, int max_port) {
152 return allocator_->SetPortRange(min_port, max_port);
153 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700154 // Endpoint is on the public network. No STUN or TURN.
155 void ResetWithNoServersOrNat() {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700156 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
157 allocator_->set_step_delay(cricket::kMinimumStepDelay);
158 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700159 // Endpoint is behind a NAT, with STUN specified.
160 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
161 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700162 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700163 // Endpoint is on the public network, with STUN specified.
164 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
165 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700167 // Endpoint is on the public network, with TURN specified.
168 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
169 const rtc::SocketAddress& tcp_turn) {
170 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000171 AddTurnServers(udp_turn, tcp_turn);
172 }
173
174 void AddTurnServers(const rtc::SocketAddress& udp_turn,
175 const rtc::SocketAddress& tcp_turn) {
deadbeef653b8e02015-11-11 12:55:10 -0800176 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800178 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000179
180 if (!udp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800181 turn_server.ports.push_back(
182 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 }
184 if (!tcp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800185 turn_server.ports.push_back(
186 cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187 }
deadbeef653b8e02015-11-11 12:55:10 -0800188 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189 }
190
191 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700192 session_ = CreateSession("session", component);
193 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700195 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196 return true;
197 }
198
199 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700200 session_ = CreateSession("session", content_name, component);
201 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000202 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700203 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204 return true;
205 }
206
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700207 std::unique_ptr<cricket::PortAllocatorSession> CreateSession(
208 const std::string& sid,
209 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000210 return CreateSession(sid, kContentName, component);
211 }
212
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700213 std::unique_ptr<cricket::PortAllocatorSession> CreateSession(
214 const std::string& sid,
215 const std::string& content_name,
216 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000217 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
218 }
219
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700220 std::unique_ptr<cricket::PortAllocatorSession> CreateSession(
221 const std::string& sid,
222 const std::string& content_name,
223 int component,
224 const std::string& ice_ufrag,
225 const std::string& ice_pwd) {
226 std::unique_ptr<cricket::PortAllocatorSession> session =
227 allocator_->CreateSession(sid, content_name, component, ice_ufrag,
228 ice_pwd);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 session->SignalPortReady.connect(this,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700230 &BasicPortAllocatorTest::OnPortReady);
231 session->SignalCandidatesReady.connect(
232 this, &BasicPortAllocatorTest::OnCandidatesReady);
233 session->SignalCandidatesAllocationDone.connect(
234 this, &BasicPortAllocatorTest::OnCandidatesAllocationDone);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235 return session;
236 }
237
238 static bool CheckCandidate(const cricket::Candidate& c,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700239 int component,
240 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000241 const std::string& proto,
242 const SocketAddress& addr) {
243 return (c.component() == component && c.type() == type &&
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700244 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
245 ((addr.port() == 0 && (c.address().port() != 0)) ||
246 (c.address().port() == addr.port())));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247 }
248 static bool CheckPort(const rtc::SocketAddress& addr,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700249 int min_port,
250 int max_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000251 return (addr.port() >= min_port && addr.port() <= max_port);
252 }
253
254 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
255 // We should only get this callback once, except in the mux test where
256 // we have multiple port allocation sessions.
257 if (session == session_.get()) {
258 ASSERT_FALSE(candidate_allocation_done_);
259 candidate_allocation_done_ = true;
260 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700261 EXPECT_TRUE(session->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 }
263
264 // Check if all ports allocated have send-buffer size |expected|. If
265 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
266 void CheckSendBufferSizesOfAllPorts(int expected) {
267 std::vector<cricket::PortInterface*>::iterator it;
268 for (it = ports_.begin(); it < ports_.end(); ++it) {
269 int send_buffer_size;
270 if (expected == -1) {
271 EXPECT_EQ(SOCKET_ERROR,
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700272 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000273 } else {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700274 EXPECT_EQ(0,
275 (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000276 ASSERT_EQ(expected, send_buffer_size);
277 }
278 }
279 }
280
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700281 // This function starts the port/address gathering and check the existence of
282 // candidates as specified. When |expect_stun_candidate| is true,
283 // |stun_candidate_addr| carries the expected reflective address, which is
284 // also the related address for TURN candidate if it is expected. Otherwise,
285 // it should be ignore.
286 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200287 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700288 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700289 const rtc::IPAddress& stun_candidate_addr,
290 const rtc::IPAddress& relay_candidate_udp_transport_addr,
291 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800292 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
293 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700294 if (!session_) {
295 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
296 }
297 session_->set_flags(session_->flags() |
298 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700299 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
300 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000301 session_->StartGettingPorts();
302 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
303
Peter Boström0c4e06b2015-10-07 12:23:21 +0200304 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700305 if (!host_candidate_addr.IsNil()) {
306 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
307 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800308 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700309 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700310 }
311 if (!stun_candidate_addr.IsNil()) {
312 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700313 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
314 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800315 rtc::IPAddress related_address = host_candidate_addr;
316 if (host_candidate_addr.IsNil()) {
317 related_address =
318 rtc::GetAnyIP(candidates_[total_candidates].address().family());
319 }
320 EXPECT_EQ(related_address,
321 candidates_[total_candidates].related_address().ipaddr());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700322 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700323 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700324 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700325 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700326 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
327 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700328 EXPECT_EQ(stun_candidate_addr,
329 candidates_[total_candidates].related_address().ipaddr());
330 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700331 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700332 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700333 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700334 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
335 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700336 EXPECT_EQ(stun_candidate_addr,
337 candidates_[total_candidates].related_address().ipaddr());
338 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700339 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000340
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700341 EXPECT_EQ(total_candidates, candidates_.size());
342 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000343 }
344
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000345 protected:
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700346 cricket::BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000347
348 void OnPortReady(cricket::PortAllocatorSession* ses,
349 cricket::PortInterface* port) {
350 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
351 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700352 // Make sure the new port is added to ReadyPorts.
353 auto ready_ports = ses->ReadyPorts();
354 EXPECT_NE(ready_ports.end(),
355 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000356 }
357 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
358 const std::vector<cricket::Candidate>& candidates) {
359 for (size_t i = 0; i < candidates.size(); ++i) {
360 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
361 candidates_.push_back(candidates[i]);
362 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700363 // Make sure the new candidates are added to Candidates.
364 auto ses_candidates = ses->ReadyCandidates();
365 for (const cricket::Candidate& candidate : candidates) {
366 EXPECT_NE(
367 ses_candidates.end(),
368 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
369 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000370 }
371
372 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800373 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
374 cricket::RelayServerConfig server_config = allocator_->turn_servers()[i];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375 cricket::PortList::const_iterator relay_port;
376 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700377 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378 if (proto_addr.address == relay_port->address &&
379 proto_addr.proto == relay_port->proto)
380 return true;
381 }
382 }
383 return false;
384 }
385
Guo-wei Shieh11477022015-08-15 09:28:41 -0700386 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
387 bool with_nat) {
388 if (with_nat) {
389 nat_server_.reset(new rtc::NATServer(
390 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
391 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
392 } else {
393 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
394 }
395
396 ServerAddresses stun_servers;
397 if (!stun_server.IsNil()) {
398 stun_servers.insert(stun_server);
399 }
400 allocator_.reset(new cricket::BasicPortAllocator(
401 &network_manager_, nat_socket_factory_.get(), stun_servers));
402 allocator().set_step_delay(cricket::kMinimumStepDelay);
403 }
404
kwiberg3ec46792016-04-27 07:22:53 -0700405 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
406 std::unique_ptr<rtc::VirtualSocketServer> vss_;
407 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700409 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700411 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
412 std::unique_ptr<cricket::TestStunServer> stun_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413 cricket::TestRelayServer relay_server_;
414 cricket::TestTurnServer turn_server_;
415 rtc::FakeNetworkManager network_manager_;
kwiberg3ec46792016-04-27 07:22:53 -0700416 std::unique_ptr<cricket::BasicPortAllocator> allocator_;
417 std::unique_ptr<cricket::PortAllocatorSession> session_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 std::vector<cricket::PortInterface*> ports_;
419 std::vector<cricket::Candidate> candidates_;
420 bool candidate_allocation_done_;
421};
422
423// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700424TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000425 EXPECT_EQ(&network_manager_, allocator().network_manager());
426 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800427 ASSERT_EQ(1u, allocator().turn_servers().size());
428 EXPECT_EQ(cricket::RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000429 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800430 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
431 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700432 EXPECT_TRUE(HasRelayAddress(
433 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP)));
434 EXPECT_TRUE(HasRelayAddress(
435 cricket::ProtocolAddress(kRelayTcpIntAddr, cricket::PROTO_TCP)));
436 EXPECT_TRUE(HasRelayAddress(
437 cricket::ProtocolAddress(kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000438 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700439 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000440}
441
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800442// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700443TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800444 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
445 rtc::ADAPTER_TYPE_ETHERNET);
446 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
447 rtc::ADAPTER_TYPE_WIFI);
448 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
449 rtc::ADAPTER_TYPE_CELLULAR);
450 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
451 rtc::ADAPTER_TYPE_VPN);
452 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
453 rtc::ADAPTER_TYPE_LOOPBACK);
454 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
455 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
456 cricket::PORTALLOCATOR_DISABLE_RELAY |
457 cricket::PORTALLOCATOR_DISABLE_TCP);
458 session_->StartGettingPorts();
459 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
460 EXPECT_EQ(4U, candidates_.size());
461 for (cricket::Candidate candidate : candidates_) {
462 EXPECT_LT(candidate.address().ip(), 0x12345604U);
463 }
464}
465
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700466TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800467 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
468 rtc::ADAPTER_TYPE_ETHERNET);
469 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
470 rtc::ADAPTER_TYPE_WIFI);
471 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
472 rtc::ADAPTER_TYPE_CELLULAR);
473 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
474 rtc::ADAPTER_TYPE_LOOPBACK |
475 rtc::ADAPTER_TYPE_WIFI);
476 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
477 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
478 cricket::PORTALLOCATOR_DISABLE_RELAY |
479 cricket::PORTALLOCATOR_DISABLE_TCP);
480 session_->StartGettingPorts();
481 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
482 EXPECT_EQ(1U, candidates_.size());
483 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
484}
485
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000486// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700487TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000488 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
489 session_->StartGettingPorts();
490 // Waiting for one second to make sure BasicPortAllocatorSession has not
491 // called OnAllocate multiple times. In old behavior it's called every 250ms.
492 // When there are no network interfaces, each execution of OnAllocate will
493 // result in SignalCandidatesAllocationDone signal.
494 rtc::Thread::Current()->ProcessMessages(1000);
495 EXPECT_TRUE(candidate_allocation_done_);
496 EXPECT_EQ(0U, candidates_.size());
497}
498
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700499// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700500TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800501 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
502 allocator_->SetNetworkIgnoreMask(0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700503 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
504 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
505 cricket::PORTALLOCATOR_DISABLE_RELAY |
506 cricket::PORTALLOCATOR_DISABLE_TCP);
507 session_->StartGettingPorts();
508 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
509 EXPECT_EQ(1U, candidates_.size());
510}
511
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000512// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700513TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000514 AddInterface(kClientAddr);
515 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
516 session_->StartGettingPorts();
517 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
518 EXPECT_EQ(4U, ports_.size());
519 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700520 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
521 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000522 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700523 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
524 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000525 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700526 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
527 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000528 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700529 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
530 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000531 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700532 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
533 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000534 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700535 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
536 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000537 EXPECT_PRED5(CheckCandidate, candidates_[6],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700538 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
539 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000540 EXPECT_TRUE(candidate_allocation_done_);
541}
542
honghaiz8c404fa2015-09-28 07:59:43 -0700543// Test that when the same network interface is brought down and up, the
544// port allocator session will restart a new allocation sequence if
545// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700546TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700547 std::string if_name("test_net0");
548 AddInterface(kClientAddr, if_name);
549 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
550 session_->StartGettingPorts();
551 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
552 EXPECT_EQ(4U, ports_.size());
553 EXPECT_TRUE(candidate_allocation_done_);
554 candidate_allocation_done_ = false;
555 candidates_.clear();
556 ports_.clear();
557
558 RemoveInterface(kClientAddr);
559 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
560 EXPECT_EQ(0U, ports_.size());
561 EXPECT_FALSE(candidate_allocation_done_);
562
563 // When the same interfaces are added again, new candidates/ports should be
564 // generated.
565 AddInterface(kClientAddr, if_name);
566 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
567 EXPECT_EQ(4U, ports_.size());
568 EXPECT_TRUE(candidate_allocation_done_);
569}
570
571// Test that when the same network interface is brought down and up, the
572// port allocator session will not restart a new allocation sequence if
573// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700574TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700575 std::string if_name("test_net0");
576 AddInterface(kClientAddr, if_name);
577 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
578 session_->StartGettingPorts();
579 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
580 EXPECT_EQ(4U, ports_.size());
581 EXPECT_TRUE(candidate_allocation_done_);
582 session_->StopGettingPorts();
583 candidates_.clear();
584 ports_.clear();
585
586 RemoveInterface(kClientAddr);
587 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
588 EXPECT_EQ(0U, ports_.size());
589
590 // When the same interfaces are added again, new candidates/ports should not
591 // be generated because the session has stopped.
592 AddInterface(kClientAddr, if_name);
593 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
594 EXPECT_EQ(0U, ports_.size());
595 EXPECT_TRUE(candidate_allocation_done_);
596}
597
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000598// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700599TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000600 AddInterface(kClientAddr);
601 allocator_->set_step_delay(cricket::kDefaultStepDelay);
602 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
603 session_->StartGettingPorts();
604 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
605 EXPECT_EQ(2U, ports_.size());
606 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
607 EXPECT_EQ(3U, ports_.size());
608 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700609 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
610 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000611 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700612 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
613 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000614 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
615 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700616 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
617 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000618 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700619 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
620 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000621 EXPECT_EQ(4U, ports_.size());
622 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
623 EXPECT_PRED5(CheckCandidate, candidates_[6],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700624 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
625 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000626 EXPECT_EQ(4U, ports_.size());
627 EXPECT_TRUE(candidate_allocation_done_);
628 // If we Stop gathering now, we shouldn't get a second "done" callback.
629 session_->StopGettingPorts();
630}
631
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700632TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000633 AddInterface(kClientAddr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700634 EXPECT_TRUE(
635 CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP, cricket::CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000636 session_->StartGettingPorts();
637 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
638 EXPECT_TRUE(candidate_allocation_done_);
639 // If we Stop gathering now, we shouldn't get a second "done" callback.
640 session_->StopGettingPorts();
641
642 // All ports should have unset send-buffer sizes.
643 CheckSendBufferSizesOfAllPorts(-1);
644}
645
646// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700647TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000648 AddInterface(kClientAddr);
649 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
650 session_->StartGettingPorts();
651 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
652 EXPECT_EQ(2U, ports_.size());
653 session_->StopGettingPorts();
654 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
655}
656
657// Test that we restrict client ports appropriately when a port range is set.
658// We check the candidates for udp/stun/tcp ports, and the from address
659// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700660TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000661 AddInterface(kClientAddr);
662 // Check that an invalid port range fails.
663 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
664 // Check that a null port range succeeds.
665 EXPECT_TRUE(SetPortRange(0, 0));
666 // Check that a valid port range succeeds.
667 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
668 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
669 session_->StartGettingPorts();
670 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
671 EXPECT_EQ(4U, ports_.size());
672 // Check the port number for the UDP port object.
673 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
674 // Check the port number for the STUN port object.
675 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
676 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700677 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
678 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000679 // Check the port number for the TCP port object.
680 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
681 EXPECT_TRUE(candidate_allocation_done_);
682}
683
684// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700685TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000686 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
687 session_->StartGettingPorts();
688 rtc::Thread::Current()->ProcessMessages(100);
689 // Without network adapter, we should not get any candidate.
690 EXPECT_EQ(0U, candidates_.size());
691 EXPECT_TRUE(candidate_allocation_done_);
692}
693
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700694// Test that when enumeration is disabled, we should not have any ports when
695// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700696TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700697 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700698 ResetWithStunServerNoNat(kStunAddr);
699 allocator().set_candidate_filter(cricket::CF_RELAY);
700 // Expect to see no ports and no candidates.
701 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
702 rtc::IPAddress(), rtc::IPAddress());
703}
704
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800705// Test that even with multiple interfaces, the result should still be a single
706// default private, one STUN and one TURN candidate since we bind to any address
707// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700709 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000710 AddInterface(kPrivateAddr);
711 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700712 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000713 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800714
715 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
716 // address set and we have no IPv6 STUN server, there should be no IPv6
717 // candidates.
718 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
719 session_->set_flags(cricket::PORTALLOCATOR_ENABLE_IPV6);
720
721 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
722 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
723 // TURN/UDP candidates.
724 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800725 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
726 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700727}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000728
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800729// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
730// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700732 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800733 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700734 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700735 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800736 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
737 // private, STUN, TURN/UDP, and TURN/TCP candidates.
738 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
739 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700740 kTurnUdpExtAddr.ipaddr());
741}
742
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800743// Test that when adapter enumeration is disabled, for endpoints without
744// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700745TEST_F(BasicPortAllocatorTest,
746 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800747 ResetWithNoServersOrNat();
748 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
749 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
750 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700751}
752
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800753// Test that when adapter enumeration is disabled, with
754// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
755// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700756TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800757 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
758 ResetWithStunServerNoNat(kStunAddr);
759 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
760 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
761 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
762 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700763 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700764 rtc::IPAddress(), rtc::IPAddress());
765}
766
767// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800768// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
769// a NAT, there is no local candidate. However, this specified default route
770// (kClientAddr) which was discovered when sending STUN requests, will become
771// the srflx addresses.
772TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700773 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800774 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700775 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800776 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700777 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800778 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700779 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
780 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800781 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
782 rtc::IPAddress(), rtc::IPAddress());
783}
784
785// Test that when adapter enumeration is disabled, with
786// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
787// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700788TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800789 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
790 ResetWithStunServerAndNat(kStunAddr);
791 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
792 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
793 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
794 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
795 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000796}
797
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700798// Test that we disable relay over UDP, and only TCP is used when connecting to
799// the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700800TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700801 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
802 AddInterface(kClientAddr);
803 ResetWithStunServerAndNat(kStunAddr);
804 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
805 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
806 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY |
807 cricket::PORTALLOCATOR_DISABLE_UDP |
808 cricket::PORTALLOCATOR_DISABLE_STUN |
809 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
810
811 session_->StartGettingPorts();
812 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
813
814 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
815 // TURN/TCP candidates.
816 EXPECT_EQ(2U, ports_.size());
817 EXPECT_EQ(2U, candidates_.size());
818 EXPECT_PRED5(CheckCandidate, candidates_[0],
819 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
820 kTurnUdpExtAddr);
821 // The TURN candidate should use TCP to contact the TURN server.
822 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
823 EXPECT_PRED5(CheckCandidate, candidates_[1],
824 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
825 kClientAddr);
826}
827
Erik Språngefdce692015-06-05 09:41:26 +0200828// Disable for asan, see
829// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
830#if !defined(ADDRESS_SANITIZER)
831
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000832// Test that we can get OnCandidatesAllocationDone callback when all the ports
833// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700834TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000835 AddInterface(kClientAddr);
836 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
837 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
838 cricket::PORTALLOCATOR_DISABLE_STUN |
839 cricket::PORTALLOCATOR_DISABLE_RELAY |
840 cricket::PORTALLOCATOR_DISABLE_TCP);
841 session_->StartGettingPorts();
842 rtc::Thread::Current()->ProcessMessages(100);
843 EXPECT_EQ(0U, candidates_.size());
844 EXPECT_TRUE(candidate_allocation_done_);
845}
846
847// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700848TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000849 AddInterface(kClientAddr);
850 fss_->set_udp_sockets_enabled(false);
851 EXPECT_TRUE(CreateSession(1));
852 session_->StartGettingPorts();
853 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
854 EXPECT_EQ(2U, ports_.size());
855 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700856 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
857 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000858 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700859 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
860 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000861 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700862 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
863 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700865 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
866 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000867 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700868 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
869 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000870 EXPECT_TRUE(candidate_allocation_done_);
871}
872
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700873#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200874
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000875// Test that we don't crash or malfunction if we can't create UDP sockets or
876// listen on TCP sockets. We still give out a local TCP address, since
877// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700878TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000879 AddInterface(kClientAddr);
880 fss_->set_udp_sockets_enabled(false);
881 fss_->set_tcp_listen_enabled(false);
882 EXPECT_TRUE(CreateSession(1));
883 session_->StartGettingPorts();
884 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
885 EXPECT_EQ(2U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700886 EXPECT_PRED5(CheckCandidate, candidates_[0], 1, "relay", "udp",
887 kRelayUdpIntAddr);
888 EXPECT_PRED5(CheckCandidate, candidates_[1], 1, "relay", "udp",
889 kRelayUdpExtAddr);
890 EXPECT_PRED5(CheckCandidate, candidates_[2], 1, "relay", "tcp",
891 kRelayTcpIntAddr);
892 EXPECT_PRED5(CheckCandidate, candidates_[3], 1, "local", "tcp", kClientAddr);
893 EXPECT_PRED5(CheckCandidate, candidates_[4], 1, "relay", "ssltcp",
894 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 EXPECT_TRUE(candidate_allocation_done_);
896}
897
898// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700899// TODO(deadbeef): Find a way to exit early here.
900TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000901 AddInterface(kClientAddr);
902 fss_->set_tcp_sockets_enabled(false);
903 fss_->set_udp_sockets_enabled(false);
904 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
905 session_->StartGettingPorts();
906 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700907 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000908 // In case of Relay, ports creation will succeed but sockets will fail.
909 // There is no error reporting from RelayEntry to handle this failure.
910}
911
912// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700913TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000914 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
915 AddInterface(kClientAddr);
916 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
917 session_->StartGettingPorts();
918 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
919 EXPECT_EQ(2U, ports_.size());
920 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700921 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
922 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000923 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700924 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
925 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000926 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
927 // will be tried after 3 seconds.
928 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
929 EXPECT_EQ(3U, ports_.size());
930 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700931 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
932 kRelayUdpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700934 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp",
935 kRelayTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 EXPECT_PRED5(CheckCandidate, candidates_[4],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700937 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
938 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000939 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700940 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
941 kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000942 // Stun Timeout is 9sec.
943 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
944}
945
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700946TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000947 AddInterface(kClientAddr);
948 AddInterface(kClientAddr2);
949 // Allocating only host UDP ports. This is done purely for testing
950 // convenience.
951 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
952 cricket::PORTALLOCATOR_DISABLE_STUN |
953 cricket::PORTALLOCATOR_DISABLE_RELAY);
954 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
955 session_->StartGettingPorts();
956 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
957 ASSERT_EQ(2U, candidates_.size());
958 EXPECT_EQ(2U, ports_.size());
959 // Candidates priorities should be different.
960 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
961}
962
963// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700964TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000965 AddInterface(kClientAddr);
966 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
967 session_->StartGettingPorts();
968 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
969 EXPECT_EQ(4U, ports_.size());
970 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700971 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000972}
973
974// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
975// This test also verifies that when the allocator is only allowed to use
976// relay (i.e. IceTransportsType is relay), the raddr is an empty
977// address with the correct family. This is to prevent any local
978// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700979TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 AddInterface(kClientAddr);
981 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700982 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000983 allocator().set_candidate_filter(cricket::CF_RELAY);
984 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
985 session_->StartGettingPorts();
986 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700987 EXPECT_PRED5(CheckCandidate, candidates_[0],
988 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
990
991 EXPECT_EQ(1U, candidates_.size());
992 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
993 for (size_t i = 0; i < candidates_.size(); ++i) {
994 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
995 EXPECT_EQ(
996 candidates_[0].related_address(),
997 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
998 }
999}
1000
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001001TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001002 AddInterface(kClientAddr);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001003 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001004 allocator().set_candidate_filter(cricket::CF_HOST);
1005 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1006 session_->StartGettingPorts();
1007 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001008 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
1009 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001010 for (size_t i = 0; i < candidates_.size(); ++i) {
1011 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
1012 }
1013}
1014
1015// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001016TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001017 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001018 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001019
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001020 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001021 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
1022 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1023 session_->StartGettingPorts();
1024 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1025 // Host is behind NAT, no private address will be exposed. Hence only UDP
1026 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001027 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1028 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001029 for (size_t i = 0; i < candidates_.size(); ++i) {
1030 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
1031 EXPECT_EQ(
1032 candidates_[0].related_address(),
1033 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
1034 }
1035}
1036
1037// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001038TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001039 AddInterface(kClientAddr);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001040 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001041 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
1042 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1043 session_->StartGettingPorts();
1044 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1045 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001046 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001047 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
1048 for (size_t i = 0; i < candidates_.size(); ++i) {
1049 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
1050 }
1051}
1052
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001053// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001054TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001055 AddInterface(kClientAddr);
1056 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1057 session_->StartGettingPorts();
1058 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1059 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001060 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1061 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001062 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001063 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1064 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001065 EXPECT_PRED5(CheckCandidate, candidates_[5],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001066 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1067 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001068 EXPECT_EQ(4U, ports_.size());
1069 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
1070 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
1071 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
1072 EXPECT_EQ(kIcePwd0, candidates_[0].password());
1073 EXPECT_EQ(kIcePwd0, candidates_[1].password());
1074 EXPECT_TRUE(candidate_allocation_done_);
1075}
1076
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001077// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1078// is allocated for udp and stun. Also verify there is only one candidate
1079// (local) if stun candidate is same as local candidate, which will be the case
1080// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001081TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001082 AddInterface(kClientAddr);
1083 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001084 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1085 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1086 session_->StartGettingPorts();
1087 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1088 EXPECT_EQ(3U, ports_.size());
1089 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001090 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1091 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001092 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1093}
1094
1095// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1096// is allocated for udp and stun. In this test we should expect both stun and
1097// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001098TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001099 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001100 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001101
1102 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001103 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1104 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1105 session_->StartGettingPorts();
1106 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1107 ASSERT_EQ(2U, ports_.size());
1108 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001109 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1110 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001111 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001112 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1113 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001114 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1115 EXPECT_EQ(3U, candidates_.size());
1116}
1117
deadbeefc5d0d952015-07-16 10:22:21 -07001118// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001119TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001120 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1121 AddInterface(kClientAddr);
1122 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
1123
1124 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1125
1126 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1127 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001128 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1129 cricket::PORTALLOCATOR_DISABLE_TCP);
1130
1131 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1132 session_->StartGettingPorts();
1133
1134 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1135 ASSERT_EQ(3U, ports_.size());
1136 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001137 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1138 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001139 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001140 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1141 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001142 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001143 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1144 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1146 EXPECT_EQ(3U, candidates_.size());
1147}
1148
1149// Testing DNS resolve for the TURN server, this will test AllocationSequence
1150// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001151TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001152 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
1153 cricket::PROTO_UDP);
1154 AddInterface(kClientAddr);
1155 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
deadbeef653b8e02015-11-11 12:55:10 -08001156 cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001158 turn_server.credentials = credentials;
1159 turn_server.ports.push_back(cricket::ProtocolAddress(
1160 rtc::SocketAddress("localhost", 3478), cricket::PROTO_UDP, false));
1161 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001162
1163 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1164 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001165 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1166 cricket::PORTALLOCATOR_DISABLE_TCP);
1167
1168 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1169 session_->StartGettingPorts();
1170
1171 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1172}
1173
1174// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1175// is allocated for udp/stun/turn. In this test we should expect all local,
1176// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001177TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001178 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001179 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001180
1181 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1182
1183 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001184 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1185 cricket::PORTALLOCATOR_DISABLE_TCP);
1186
1187 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1188 session_->StartGettingPorts();
1189
1190 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1191 ASSERT_EQ(2U, ports_.size());
1192 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001193 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1194 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001195 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001196 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1197 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001198 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001199 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1200 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001201 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1202 EXPECT_EQ(3U, candidates_.size());
1203 // Local port will be created first and then TURN port.
1204 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1205 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1206}
1207
1208// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1209// server is also used as the STUN server, we should get 'local', 'stun', and
1210// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001211TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001212 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001213 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001214 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001215 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1216
1217 // Must set the step delay to 0 to make sure the relay allocation phase is
1218 // started before the STUN candidates are obtained, so that the STUN binding
1219 // response is processed when both StunPort and TurnPort exist to reproduce
1220 // webrtc issue 3537.
1221 allocator_->set_step_delay(0);
1222 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001223 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1224 cricket::PORTALLOCATOR_DISABLE_TCP);
1225
1226 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1227 session_->StartGettingPorts();
1228
1229 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1230 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001231 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1232 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001233 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001234 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1235 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001236 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001237 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1238 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001239 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1240
1241 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1242 EXPECT_EQ(3U, candidates_.size());
1243 // Local port will be created first and then TURN port.
1244 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1245 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1246}
1247
deadbeefc5d0d952015-07-16 10:22:21 -07001248// Test that when only a TCP TURN server is available, we do NOT use it as
1249// a UDP STUN server, as this could leak our IP address. Thus we should only
1250// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001251TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
deadbeefc5d0d952015-07-16 10:22:21 -07001252 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1253 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001254 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001255 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1256
1257 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001258 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1259 cricket::PORTALLOCATOR_DISABLE_TCP);
1260
1261 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1262 session_->StartGettingPorts();
1263
1264 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1265 ASSERT_EQ(2U, ports_.size());
1266 EXPECT_PRED5(CheckCandidate, candidates_[0],
1267 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1268 kClientAddr);
1269 EXPECT_PRED5(CheckCandidate, candidates_[1],
1270 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1271 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1272 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1273 EXPECT_EQ(2U, candidates_.size());
1274 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1275 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1276}
1277
1278// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1279// TURN server is used as the STUN server and we get 'local', 'stun', and
1280// 'relay' candidates.
1281// TODO(deadbeef): Remove this test when support for non-shared socket mode
1282// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001283TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001284 AddInterface(kClientAddr);
1285 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001286 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001287 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1288
1289 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001290 cricket::PORTALLOCATOR_DISABLE_TCP);
1291
1292 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1293 session_->StartGettingPorts();
1294
1295 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1296 ASSERT_EQ(3U, ports_.size());
1297 EXPECT_PRED5(CheckCandidate, candidates_[0],
1298 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1299 kClientAddr);
1300 EXPECT_PRED5(CheckCandidate, candidates_[1],
1301 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1302 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1303 EXPECT_PRED5(CheckCandidate, candidates_[2],
1304 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1305 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1306 // Not using shared socket, so the STUN request's server reflexive address
1307 // should be different than the TURN request's server reflexive address.
1308 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1309
1310 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1311 EXPECT_EQ(3U, candidates_.size());
1312 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1313 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1314 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1315}
1316
1317// Test that even when both a STUN and TURN server are configured, the TURN
1318// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001319TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001320 AddInterface(kClientAddr);
1321 // Configure with STUN server but destroy it, so we can ensure that it's
1322 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001323 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001324 stun_server_.reset();
1325 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1326
1327 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001328 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1329 cricket::PORTALLOCATOR_DISABLE_TCP);
1330
1331 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1332 session_->StartGettingPorts();
1333
1334 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1335 EXPECT_PRED5(CheckCandidate, candidates_[0],
1336 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1337 kClientAddr);
1338 EXPECT_PRED5(CheckCandidate, candidates_[1],
1339 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1340 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1341 EXPECT_PRED5(CheckCandidate, candidates_[2],
1342 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1343 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1344 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1345
1346 // Don't bother waiting for STUN timeout, since we already verified
1347 // that we got a STUN candidate from the TURN server.
1348}
1349
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001350// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1351// and fail to generate STUN candidate, local UDP candidate is generated
1352// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001353TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001354 allocator().set_flags(allocator().flags() |
1355 cricket::PORTALLOCATOR_DISABLE_RELAY |
1356 cricket::PORTALLOCATOR_DISABLE_TCP |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001357 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1358 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1359 AddInterface(kClientAddr);
1360 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1361 session_->StartGettingPorts();
1362 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1363 EXPECT_EQ(1U, candidates_.size());
1364 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001365 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1366 kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001367 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1368 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001369 EXPECT_EQ(1U, candidates_.size());
1370}
1371
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001372// Test that when the NetworkManager doesn't have permission to enumerate
1373// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1374// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001375TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001376 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1377 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001378 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001379 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001380 allocator().set_flags(allocator().flags() |
1381 cricket::PORTALLOCATOR_DISABLE_RELAY |
1382 cricket::PORTALLOCATOR_DISABLE_TCP |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001383 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001384 EXPECT_EQ(0U, allocator_->flags() &
1385 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001386 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001387 EXPECT_EQ(0U, session_->flags() &
1388 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001389 session_->StartGettingPorts();
1390 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001391 EXPECT_EQ(1U, candidates_.size());
1392 EXPECT_PRED5(CheckCandidate, candidates_[0],
1393 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1394 kPrivateAddr);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001395 EXPECT_NE(0U, session_->flags() &
1396 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001397}
1398
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001399// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001400TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001401 allocator().set_flags(allocator().flags() |
1402 cricket::PORTALLOCATOR_DISABLE_RELAY |
1403 cricket::PORTALLOCATOR_ENABLE_IPV6 |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001404 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1405 AddInterface(kClientIPv6Addr);
1406 AddInterface(kClientAddr);
1407 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1408 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1409 session_->StartGettingPorts();
1410 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1411 EXPECT_EQ(4U, candidates_.size());
1412 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1413 EXPECT_PRED5(CheckCandidate, candidates_[0],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001414 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1415 kClientIPv6Addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416 EXPECT_PRED5(CheckCandidate, candidates_[1],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001417 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1418 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419 EXPECT_PRED5(CheckCandidate, candidates_[2],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001420 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1421 kClientIPv6Addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001422 EXPECT_PRED5(CheckCandidate, candidates_[3],
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001423 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1424 kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001425 EXPECT_EQ(4U, candidates_.size());
1426}
honghaiz98db68f2015-09-29 07:58:17 -07001427
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001428TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001429 AddInterface(kClientAddr);
1430 allocator_->set_step_delay(cricket::kDefaultStepDelay);
1431 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1432 session_->StartGettingPorts();
1433 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1434 EXPECT_EQ(2U, ports_.size());
1435 session_->StopGettingPorts();
1436 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1437
1438 // After stopping getting ports, adding a new interface will not start
1439 // getting ports again.
1440 candidates_.clear();
1441 ports_.clear();
1442 candidate_allocation_done_ = false;
1443 network_manager_.AddInterface(kClientAddr2);
1444 rtc::Thread::Current()->ProcessMessages(1000);
1445 EXPECT_EQ(0U, candidates_.size());
1446 EXPECT_EQ(0U, ports_.size());
1447}
1448
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001449TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001450 AddInterface(kClientAddr);
1451 allocator_->set_step_delay(cricket::kDefaultStepDelay);
1452 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1453 session_->StartGettingPorts();
1454 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1455 EXPECT_EQ(2U, ports_.size());
1456 session_->ClearGettingPorts();
1457 WAIT(candidate_allocation_done_, 1000);
1458 EXPECT_FALSE(candidate_allocation_done_);
1459
1460 // After clearing getting ports, adding a new interface will start getting
1461 // ports again.
1462 candidates_.clear();
1463 ports_.clear();
1464 candidate_allocation_done_ = false;
1465 network_manager_.AddInterface(kClientAddr2);
1466 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1467 EXPECT_EQ(2U, ports_.size());
1468}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001469
1470// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1471// a pooled session is taken out of the pool.
1472TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1473 AddInterface(kClientAddr);
1474 int pool_size = 1;
1475 allocator_->SetConfiguration(allocator_->stun_servers(),
1476 allocator_->turn_servers(), pool_size);
1477 const cricket::PortAllocatorSession* peeked_session =
1478 allocator_->GetPooledSession();
1479 ASSERT_NE(nullptr, peeked_session);
1480 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1481 kDefaultAllocationTimeout);
1482 // Expect that when TakePooledSession is called,
1483 // UpdateTransportInformationInternal will be called and the
1484 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1485 // candidates.
1486 session_ =
1487 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1488 ASSERT_NE(nullptr, session_.get());
1489 auto ready_ports = session_->ReadyPorts();
1490 auto candidates = session_->ReadyCandidates();
1491 EXPECT_FALSE(ready_ports.empty());
1492 EXPECT_FALSE(candidates.empty());
1493 for (const cricket::PortInterface* port_interface : ready_ports) {
1494 const cricket::Port* port =
1495 static_cast<const cricket::Port*>(port_interface);
1496 EXPECT_EQ(kContentName, port->content_name());
1497 EXPECT_EQ(1, port->component());
1498 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1499 EXPECT_EQ(kIcePwd0, port->password());
1500 }
1501 for (const cricket::Candidate& candidate : candidates) {
1502 EXPECT_EQ(1, candidate.component());
1503 EXPECT_EQ(kIceUfrag0, candidate.username());
1504 EXPECT_EQ(kIcePwd0, candidate.password());
1505 }
1506}