blob: 024f46fffc1695851769dd884377737a3241655e [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
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080037using rtc::IPAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038using rtc::SocketAddress;
39using rtc::Thread;
40
41static const SocketAddress kClientAddr("11.11.11.11", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070042static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000044static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
Taylor Brandstettera1c30352016-05-13 08:15:11 -070045static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3",
46 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070048static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
49static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
51static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
52static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
53static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
54static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
55static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
56static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
57static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
58static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
59static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
60static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
61
62// Minimum and maximum port for port range tests.
63static const int kMinPort = 10000;
64static const int kMaxPort = 10099;
65
66// Based on ICE_UFRAG_LENGTH
67static const char kIceUfrag0[] = "TESTICEUFRAG0000";
68// Based on ICE_PWD_LENGTH
69static const char kIcePwd0[] = "TESTICEPWD00000000000000";
70
71static const char kContentName[] = "test content";
72
73static const int kDefaultAllocationTimeout = 1000;
74static const char kTurnUsername[] = "test";
75static const char kTurnPassword[] = "test";
76
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -070077// STUN timeout (with all retries) is 9500ms.
78// Add some margin of error for slow bots.
79// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
80// fix flaky tests.
81static const int kStunTimeoutMs = 15000;
82
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083namespace cricket {
84
85// Helper for dumping candidates
Taylor Brandstetter417eebe2016-05-23 16:02:19 -070086std::ostream& operator<<(std::ostream& os, const Candidate& c) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087 os << c.ToString();
88 return os;
89}
90
Taylor Brandstettera1c30352016-05-13 08:15:11 -070091class BasicPortAllocatorTest : public testing::Test,
92 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 public:
Taylor Brandstettera1c30352016-05-13 08:15:11 -070094 BasicPortAllocatorTest()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000095 : pss_(new rtc::PhysicalSocketServer),
96 vss_(new rtc::VirtualSocketServer(pss_.get())),
97 fss_(new rtc::FirewallSocketServer(vss_.get())),
98 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070099 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700100 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700101 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)),
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700102 relay_server_(Thread::Current(),
103 kRelayUdpIntAddr,
104 kRelayUdpExtAddr,
105 kRelayTcpIntAddr,
106 kRelayTcpExtAddr,
107 kRelaySslTcpIntAddr,
108 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000109 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
110 candidate_allocation_done_(false) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700111 ServerAddresses stun_servers;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 stun_servers.insert(kStunAddr);
113 // Passing the addresses of GTURN servers will enable GTURN in
114 // Basicportallocator.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700115 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers,
116 kRelayUdpIntAddr, kRelayTcpIntAddr,
117 kRelaySslTcpIntAddr));
118 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 }
120
121 void AddInterface(const SocketAddress& addr) {
122 network_manager_.AddInterface(addr);
123 }
honghaiz8c404fa2015-09-28 07:59:43 -0700124 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
125 network_manager_.AddInterface(addr, if_name);
126 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800127 void AddInterface(const SocketAddress& addr,
128 const std::string& if_name,
129 rtc::AdapterType type) {
130 network_manager_.AddInterface(addr, if_name, type);
131 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800132 // The default route is the public address that STUN server will observe when
133 // the endpoint is sitting on the public internet and the local port is bound
134 // to the "any" address. This may be different from the default local address
135 // which the endpoint observes. This can occur if the route to the public
136 // endpoint like 8.8.8.8 (specified as the default local address) is
137 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700138 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
139 AddInterface(addr);
140 // When a binding comes from the any address, the |addr| will be used as the
141 // srflx address.
142 vss_->SetDefaultRoute(addr.ipaddr());
143 }
honghaiz8c404fa2015-09-28 07:59:43 -0700144 void RemoveInterface(const SocketAddress& addr) {
145 network_manager_.RemoveInterface(addr);
146 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 bool SetPortRange(int min_port, int max_port) {
148 return allocator_->SetPortRange(min_port, max_port);
149 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700150 // Endpoint is on the public network. No STUN or TURN.
151 void ResetWithNoServersOrNat() {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700152 allocator_.reset(new BasicPortAllocator(&network_manager_));
153 allocator_->set_step_delay(kMinimumStepDelay);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700154 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700155 // Endpoint is behind a NAT, with STUN specified.
156 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
157 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700158 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700159 // Endpoint is on the public network, with STUN specified.
160 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
161 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700163 // Endpoint is on the public network, with TURN specified.
164 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
165 const rtc::SocketAddress& tcp_turn) {
166 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167 AddTurnServers(udp_turn, tcp_turn);
168 }
169
170 void AddTurnServers(const rtc::SocketAddress& udp_turn,
171 const rtc::SocketAddress& tcp_turn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700172 RelayServerConfig turn_server(RELAY_TURN);
173 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -0800174 turn_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175
176 if (!udp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800177 turn_server.ports.push_back(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700178 ProtocolAddress(kTurnUdpIntAddr, PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000179 }
180 if (!tcp_turn.IsNil()) {
deadbeef653b8e02015-11-11 12:55:10 -0800181 turn_server.ports.push_back(
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700182 ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183 }
deadbeef653b8e02015-11-11 12:55:10 -0800184 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 }
186
187 bool CreateSession(int component) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700188 session_ = CreateSession("session", component);
189 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700191 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192 return true;
193 }
194
195 bool CreateSession(int component, const std::string& content_name) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700196 session_ = CreateSession("session", content_name, component);
197 if (!session_) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 return false;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700199 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000200 return true;
201 }
202
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700203 std::unique_ptr<PortAllocatorSession> CreateSession(const std::string& sid,
204 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 return CreateSession(sid, kContentName, component);
206 }
207
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700208 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700209 const std::string& sid,
210 const std::string& content_name,
211 int component) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000212 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
213 }
214
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700215 std::unique_ptr<PortAllocatorSession> CreateSession(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700216 const std::string& sid,
217 const std::string& content_name,
218 int component,
219 const std::string& ice_ufrag,
220 const std::string& ice_pwd) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700221 std::unique_ptr<PortAllocatorSession> session = allocator_->CreateSession(
222 sid, content_name, component, ice_ufrag, 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
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700232 static bool CheckCandidate(const 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
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700248 void OnCandidatesAllocationDone(PortAllocatorSession* session) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000249 // 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) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700261 std::vector<PortInterface*>::iterator it;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 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_) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700289 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700290 }
291 session_->set_flags(session_->flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700292 PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
293 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700294 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],
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700301 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],
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700307 ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700308 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],
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700320 ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700321 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],
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700328 ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700329 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 Brandstetter417eebe2016-05-23 16:02:19 -0700340 BasicPortAllocator& allocator() { return *allocator_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700342 void OnPortReady(PortAllocatorSession* ses, PortInterface* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
344 ports_.push_back(port);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700345 // Make sure the new port is added to ReadyPorts.
346 auto ready_ports = ses->ReadyPorts();
347 EXPECT_NE(ready_ports.end(),
348 std::find(ready_ports.begin(), ready_ports.end(), port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000349 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700350 void OnCandidatesReady(PortAllocatorSession* ses,
351 const std::vector<Candidate>& candidates) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000352 for (size_t i = 0; i < candidates.size(); ++i) {
353 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
354 candidates_.push_back(candidates[i]);
355 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700356 // Make sure the new candidates are added to Candidates.
357 auto ses_candidates = ses->ReadyCandidates();
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700358 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700359 EXPECT_NE(
360 ses_candidates.end(),
361 std::find(ses_candidates.begin(), ses_candidates.end(), candidate));
362 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000363 }
364
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700365 bool HasRelayAddress(const ProtocolAddress& proto_addr) {
deadbeef653b8e02015-11-11 12:55:10 -0800366 for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700367 RelayServerConfig server_config = allocator_->turn_servers()[i];
368 PortList::const_iterator relay_port;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 for (relay_port = server_config.ports.begin();
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700370 relay_port != server_config.ports.end(); ++relay_port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 if (proto_addr.address == relay_port->address &&
372 proto_addr.proto == relay_port->proto)
373 return true;
374 }
375 }
376 return false;
377 }
378
Guo-wei Shieh11477022015-08-15 09:28:41 -0700379 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
380 bool with_nat) {
381 if (with_nat) {
382 nat_server_.reset(new rtc::NATServer(
383 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
384 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
385 } else {
386 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
387 }
388
389 ServerAddresses stun_servers;
390 if (!stun_server.IsNil()) {
391 stun_servers.insert(stun_server);
392 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700393 allocator_.reset(new BasicPortAllocator(
Guo-wei Shieh11477022015-08-15 09:28:41 -0700394 &network_manager_, nat_socket_factory_.get(), stun_servers));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700395 allocator().set_step_delay(kMinimumStepDelay);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700396 }
397
kwiberg3ec46792016-04-27 07:22:53 -0700398 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
399 std::unique_ptr<rtc::VirtualSocketServer> vss_;
400 std::unique_ptr<rtc::FirewallSocketServer> fss_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401 rtc::SocketServerScope ss_scope_;
kwiberg3ec46792016-04-27 07:22:53 -0700402 std::unique_ptr<rtc::NATServer> nat_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000403 rtc::NATSocketFactory nat_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700404 std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700405 std::unique_ptr<TestStunServer> stun_server_;
406 TestRelayServer relay_server_;
407 TestTurnServer turn_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000408 rtc::FakeNetworkManager network_manager_;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700409 std::unique_ptr<BasicPortAllocator> allocator_;
410 std::unique_ptr<PortAllocatorSession> session_;
411 std::vector<PortInterface*> ports_;
412 std::vector<Candidate> candidates_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000413 bool candidate_allocation_done_;
414};
415
416// Tests that we can init the port allocator and create a session.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700417TEST_F(BasicPortAllocatorTest, TestBasic) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 EXPECT_EQ(&network_manager_, allocator().network_manager());
419 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef653b8e02015-11-11 12:55:10 -0800420 ASSERT_EQ(1u, allocator().turn_servers().size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700421 EXPECT_EQ(RELAY_GTURN, allocator().turn_servers()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000422 // Empty relay credentials are used for GTURN.
deadbeef653b8e02015-11-11 12:55:10 -0800423 EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
424 EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700425 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayUdpIntAddr, PROTO_UDP)));
426 EXPECT_TRUE(HasRelayAddress(ProtocolAddress(kRelayTcpIntAddr, PROTO_TCP)));
427 EXPECT_TRUE(
428 HasRelayAddress(ProtocolAddress(kRelaySslTcpIntAddr, PROTO_SSLTCP)));
429 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700430 EXPECT_FALSE(session_->CandidatesAllocationDone());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000431}
432
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800433// Tests that our network filtering works properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700434TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800435 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
436 rtc::ADAPTER_TYPE_ETHERNET);
437 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
438 rtc::ADAPTER_TYPE_WIFI);
439 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
440 rtc::ADAPTER_TYPE_CELLULAR);
441 AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
442 rtc::ADAPTER_TYPE_VPN);
443 AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
444 rtc::ADAPTER_TYPE_LOOPBACK);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700445 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
446 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
447 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800448 session_->StartGettingPorts();
449 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
450 EXPECT_EQ(4U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700451 for (Candidate candidate : candidates_) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800452 EXPECT_LT(candidate.address().ip(), 0x12345604U);
453 }
454}
455
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700456TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800457 AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
458 rtc::ADAPTER_TYPE_ETHERNET);
459 AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
460 rtc::ADAPTER_TYPE_WIFI);
461 AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
462 rtc::ADAPTER_TYPE_CELLULAR);
463 allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
464 rtc::ADAPTER_TYPE_LOOPBACK |
465 rtc::ADAPTER_TYPE_WIFI);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700466 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
467 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
468 PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800469 session_->StartGettingPorts();
470 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
471 EXPECT_EQ(1U, candidates_.size());
472 EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
473}
474
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000475// Tests that we allocator session not trying to allocate ports for every 250ms.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700476TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700477 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000478 session_->StartGettingPorts();
479 // Waiting for one second to make sure BasicPortAllocatorSession has not
480 // called OnAllocate multiple times. In old behavior it's called every 250ms.
481 // When there are no network interfaces, each execution of OnAllocate will
482 // result in SignalCandidatesAllocationDone signal.
483 rtc::Thread::Current()->ProcessMessages(1000);
484 EXPECT_TRUE(candidate_allocation_done_);
485 EXPECT_EQ(0U, candidates_.size());
486}
487
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700488// Test that we could use loopback interface as host candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700489TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800490 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
491 allocator_->SetNetworkIgnoreMask(0);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700492 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
493 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY |
494 PORTALLOCATOR_DISABLE_TCP);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700495 session_->StartGettingPorts();
496 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
497 EXPECT_EQ(1U, candidates_.size());
498}
499
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000500// Tests that we can get all the desired addresses successfully.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700501TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000502 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700503 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000504 session_->StartGettingPorts();
505 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
506 EXPECT_EQ(4U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700507 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
508 "local", "udp", kClientAddr);
509 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
510 "stun", "udp", kClientAddr);
511 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
512 "relay", "udp", kRelayUdpIntAddr);
513 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
514 "relay", "udp", kRelayUdpExtAddr);
515 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
516 "relay", "tcp", kRelayTcpIntAddr);
517 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
518 "local", "tcp", kClientAddr);
519 EXPECT_PRED5(CheckCandidate, candidates_[6], ICE_CANDIDATE_COMPONENT_RTP,
520 "relay", "ssltcp", kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000521 EXPECT_TRUE(candidate_allocation_done_);
522}
523
honghaiz8c404fa2015-09-28 07:59:43 -0700524// Test that when the same network interface is brought down and up, the
525// port allocator session will restart a new allocation sequence if
526// it is not stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700527TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700528 std::string if_name("test_net0");
529 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700530 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700531 session_->StartGettingPorts();
532 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
533 EXPECT_EQ(4U, ports_.size());
534 EXPECT_TRUE(candidate_allocation_done_);
535 candidate_allocation_done_ = false;
536 candidates_.clear();
537 ports_.clear();
538
539 RemoveInterface(kClientAddr);
540 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
541 EXPECT_EQ(0U, ports_.size());
542 EXPECT_FALSE(candidate_allocation_done_);
543
544 // When the same interfaces are added again, new candidates/ports should be
545 // generated.
546 AddInterface(kClientAddr, if_name);
547 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
548 EXPECT_EQ(4U, ports_.size());
549 EXPECT_TRUE(candidate_allocation_done_);
550}
551
552// Test that when the same network interface is brought down and up, the
553// port allocator session will not restart a new allocation sequence if
554// it is stopped.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700555TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
honghaiz8c404fa2015-09-28 07:59:43 -0700556 std::string if_name("test_net0");
557 AddInterface(kClientAddr, if_name);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700558 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz8c404fa2015-09-28 07:59:43 -0700559 session_->StartGettingPorts();
560 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
561 EXPECT_EQ(4U, ports_.size());
562 EXPECT_TRUE(candidate_allocation_done_);
563 session_->StopGettingPorts();
564 candidates_.clear();
565 ports_.clear();
566
567 RemoveInterface(kClientAddr);
568 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
569 EXPECT_EQ(0U, ports_.size());
570
571 // When the same interfaces are added again, new candidates/ports should not
572 // be generated because the session has stopped.
573 AddInterface(kClientAddr, if_name);
574 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
575 EXPECT_EQ(0U, ports_.size());
576 EXPECT_TRUE(candidate_allocation_done_);
577}
578
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000579// Verify candidates with default step delay of 1sec.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700580TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000581 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700582 allocator_->set_step_delay(kDefaultStepDelay);
583 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000584 session_->StartGettingPorts();
585 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
586 EXPECT_EQ(2U, ports_.size());
587 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
588 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700589 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
590 "relay", "udp", kRelayUdpIntAddr);
591 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
592 "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000593 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700594 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
595 "relay", "tcp", kRelayTcpIntAddr);
596 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
597 "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000598 EXPECT_EQ(4U, ports_.size());
599 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700600 EXPECT_PRED5(CheckCandidate, candidates_[6], ICE_CANDIDATE_COMPONENT_RTP,
601 "relay", "ssltcp", kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000602 EXPECT_EQ(4U, ports_.size());
603 EXPECT_TRUE(candidate_allocation_done_);
604 // If we Stop gathering now, we shouldn't get a second "done" callback.
605 session_->StopGettingPorts();
606}
607
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700608TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000609 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700610 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000611 session_->StartGettingPorts();
612 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
613 EXPECT_TRUE(candidate_allocation_done_);
614 // If we Stop gathering now, we shouldn't get a second "done" callback.
615 session_->StopGettingPorts();
616
617 // All ports should have unset send-buffer sizes.
618 CheckSendBufferSizesOfAllPorts(-1);
619}
620
621// Tests that we can get callback after StopGetAllPorts.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700622TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000623 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700624 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000625 session_->StartGettingPorts();
626 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
627 EXPECT_EQ(2U, ports_.size());
628 session_->StopGettingPorts();
629 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
630}
631
632// Test that we restrict client ports appropriately when a port range is set.
633// We check the candidates for udp/stun/tcp ports, and the from address
634// for relay ports.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700635TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000636 AddInterface(kClientAddr);
637 // Check that an invalid port range fails.
638 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
639 // Check that a null port range succeeds.
640 EXPECT_TRUE(SetPortRange(0, 0));
641 // Check that a valid port range succeeds.
642 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700643 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000644 session_->StartGettingPorts();
645 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
646 EXPECT_EQ(4U, ports_.size());
647 // Check the port number for the UDP port object.
648 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
649 // Check the port number for the STUN port object.
650 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
651 // Check the port number used to connect to the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700652 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort,
653 kMaxPort);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000654 // Check the port number for the TCP port object.
655 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
656 EXPECT_TRUE(candidate_allocation_done_);
657}
658
659// Test that we don't crash or malfunction if we have no network adapters.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700660TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700661 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000662 session_->StartGettingPorts();
663 rtc::Thread::Current()->ProcessMessages(100);
664 // Without network adapter, we should not get any candidate.
665 EXPECT_EQ(0U, candidates_.size());
666 EXPECT_TRUE(candidate_allocation_done_);
667}
668
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700669// Test that when enumeration is disabled, we should not have any ports when
670// candidate_filter() is set to CF_RELAY and no relay is specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700671TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700672 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700673 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700674 allocator().set_candidate_filter(CF_RELAY);
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700675 // Expect to see no ports and no candidates.
676 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
677 rtc::IPAddress(), rtc::IPAddress());
678}
679
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800680// Test that even with multiple interfaces, the result should still be a single
681// default private, one STUN and one TURN candidate since we bind to any address
682// (i.e. all 0s).
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700683TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700684 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000685 AddInterface(kPrivateAddr);
686 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700687 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000688 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
guoweis255d6f62015-11-23 14:12:38 -0800689
690 // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
691 // address set and we have no IPv6 STUN server, there should be no IPv6
692 // candidates.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700693 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
694 session_->set_flags(PORTALLOCATOR_ENABLE_IPV6);
guoweis255d6f62015-11-23 14:12:38 -0800695
696 // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
697 // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
698 // TURN/UDP candidates.
699 CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800700 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
701 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700702}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000703
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800704// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
705// candidates when both TURN/UDP and TURN/TCP servers are specified.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700706TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700707 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800708 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700709 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700710 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800711 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
712 // private, STUN, TURN/UDP, and TURN/TCP candidates.
713 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
714 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700715 kTurnUdpExtAddr.ipaddr());
716}
717
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800718// Test that when adapter enumeration is disabled, for endpoints without
719// STUN/TURN specified, a default private candidate is still generated.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720TEST_F(BasicPortAllocatorTest,
721 TestDisableAdapterEnumerationWithoutNatOrServers) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800722 ResetWithNoServersOrNat();
723 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
724 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
725 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700726}
727
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800728// Test that when adapter enumeration is disabled, with
729// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
730// a NAT, there is no local candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700731TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800732 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
733 ResetWithStunServerNoNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700734 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
735 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800736 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
737 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700738 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700739 rtc::IPAddress(), rtc::IPAddress());
740}
741
742// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800743// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
744// a NAT, there is no local candidate. However, this specified default route
745// (kClientAddr) which was discovered when sending STUN requests, will become
746// the srflx addresses.
747TEST_F(
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700748 BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800749 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700750 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800751 AddInterfaceAsDefaultRoute(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700752 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
753 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700754 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
755 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800756 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
757 rtc::IPAddress(), rtc::IPAddress());
758}
759
760// Test that when adapter enumeration is disabled, with
761// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
762// NAT, there is only one STUN candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700763TEST_F(BasicPortAllocatorTest,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800764 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
765 ResetWithStunServerAndNat(kStunAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700766 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
767 session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800768 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
769 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
770 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000771}
772
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700773// Test that we disable relay over UDP, and only TCP is used when connecting to
774// the relay server.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700775TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700776 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700777 AddInterface(kClientAddr);
778 ResetWithStunServerAndNat(kStunAddr);
779 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700780 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
781 session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY |
782 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
783 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700784
785 session_->StartGettingPorts();
786 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
787
788 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
789 // TURN/TCP candidates.
790 EXPECT_EQ(2U, ports_.size());
791 EXPECT_EQ(2U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700792 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
793 "relay", "udp", kTurnUdpExtAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700794 // The TURN candidate should use TCP to contact the TURN server.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700795 EXPECT_EQ(TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
796 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
797 "local", "tcp", kClientAddr);
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700798}
799
Erik Språngefdce692015-06-05 09:41:26 +0200800// Disable for asan, see
801// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
802#if !defined(ADDRESS_SANITIZER)
803
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000804// Test that we can get OnCandidatesAllocationDone callback when all the ports
805// are disabled.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700806TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000807 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700808 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
809 session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN |
810 PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000811 session_->StartGettingPorts();
812 rtc::Thread::Current()->ProcessMessages(100);
813 EXPECT_EQ(0U, candidates_.size());
814 EXPECT_TRUE(candidate_allocation_done_);
815}
816
817// Test that we don't crash or malfunction if we can't create UDP sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700818TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000819 AddInterface(kClientAddr);
820 fss_->set_udp_sockets_enabled(false);
821 EXPECT_TRUE(CreateSession(1));
822 session_->StartGettingPorts();
823 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
824 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700825 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
826 "relay", "udp", kRelayUdpIntAddr);
827 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
828 "relay", "udp", kRelayUdpExtAddr);
829 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
830 "relay", "tcp", kRelayTcpIntAddr);
831 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
832 "local", "tcp", kClientAddr);
833 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
834 "relay", "ssltcp", kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000835 EXPECT_TRUE(candidate_allocation_done_);
836}
837
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700838#endif // if !defined(ADDRESS_SANITIZER)
Erik Språngefdce692015-06-05 09:41:26 +0200839
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000840// Test that we don't crash or malfunction if we can't create UDP sockets or
841// listen on TCP sockets. We still give out a local TCP address, since
842// apparently this is needed for the remote side to accept our connection.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700843TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 AddInterface(kClientAddr);
845 fss_->set_udp_sockets_enabled(false);
846 fss_->set_tcp_listen_enabled(false);
847 EXPECT_TRUE(CreateSession(1));
848 session_->StartGettingPorts();
849 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
850 EXPECT_EQ(2U, ports_.size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700851 EXPECT_PRED5(CheckCandidate, candidates_[0], 1, "relay", "udp",
852 kRelayUdpIntAddr);
853 EXPECT_PRED5(CheckCandidate, candidates_[1], 1, "relay", "udp",
854 kRelayUdpExtAddr);
855 EXPECT_PRED5(CheckCandidate, candidates_[2], 1, "relay", "tcp",
856 kRelayTcpIntAddr);
857 EXPECT_PRED5(CheckCandidate, candidates_[3], 1, "local", "tcp", kClientAddr);
858 EXPECT_PRED5(CheckCandidate, candidates_[4], 1, "relay", "ssltcp",
859 kRelaySslTcpIntAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000860 EXPECT_TRUE(candidate_allocation_done_);
861}
862
863// Test that we don't crash or malfunction if we can't create any sockets.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700864// TODO(deadbeef): Find a way to exit early here.
865TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000866 AddInterface(kClientAddr);
867 fss_->set_tcp_sockets_enabled(false);
868 fss_->set_udp_sockets_enabled(false);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700869 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000870 session_->StartGettingPorts();
871 WAIT(candidates_.size() > 0, 2000);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700872 // TODO(deadbeef): Check candidate_allocation_done signal.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873 // In case of Relay, ports creation will succeed but sockets will fail.
874 // There is no error reporting from RelayEntry to handle this failure.
875}
876
877// Testing STUN timeout.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700878TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000879 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
880 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700881 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000882 session_->StartGettingPorts();
883 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
884 EXPECT_EQ(2U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700885 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
886 "local", "udp", kClientAddr);
887 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
888 "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000889 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
890 // will be tried after 3 seconds.
891 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
892 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700893 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
894 "relay", "udp", kRelayUdpIntAddr);
895 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
896 "relay", "tcp", kRelayTcpIntAddr);
897 EXPECT_PRED5(CheckCandidate, candidates_[4], ICE_CANDIDATE_COMPONENT_RTP,
898 "relay", "ssltcp", kRelaySslTcpIntAddr);
899 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
900 "relay", "udp", kRelayUdpExtAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000901 // Stun Timeout is 9sec.
902 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
903}
904
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700905TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000906 AddInterface(kClientAddr);
907 AddInterface(kClientAddr2);
908 // Allocating only host UDP ports. This is done purely for testing
909 // convenience.
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700910 allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN |
911 PORTALLOCATOR_DISABLE_RELAY);
912 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000913 session_->StartGettingPorts();
914 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
915 ASSERT_EQ(2U, candidates_.size());
916 EXPECT_EQ(2U, ports_.size());
917 // Candidates priorities should be different.
918 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
919}
920
921// Test to verify ICE restart process.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700922TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000923 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700924 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000925 session_->StartGettingPorts();
926 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
927 EXPECT_EQ(4U, ports_.size());
928 EXPECT_TRUE(candidate_allocation_done_);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700929 // TODO(deadbeef): Extend this to verify ICE restart.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930}
931
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700932// Test that the allocator session uses the candidate filter it's created with,
933// rather than the filter of its parent allocator.
934// The filter of the allocator should only affect the next gathering phase,
935// according to JSEP, which means the *next* allocator session returned.
936TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) {
937 AddInterface(kClientAddr);
938 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
939 // Set candidate filter *after* creating the session. Should have no effect.
940 allocator().set_candidate_filter(CF_RELAY);
941 session_->StartGettingPorts();
942 // 7 candidates and 4 ports is what we would normally get (see the
943 // TestGetAllPorts* tests).
944 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
945 EXPECT_EQ(4U, ports_.size());
946 EXPECT_TRUE(candidate_allocation_done_);
947}
948
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000949// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
950// This test also verifies that when the allocator is only allowed to use
951// relay (i.e. IceTransportsType is relay), the raddr is an empty
952// address with the correct family. This is to prevent any local
953// reflective address leakage in the sdp line.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700954TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955 AddInterface(kClientAddr);
956 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700957 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700958 allocator().set_candidate_filter(CF_RELAY);
959 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000960 session_->StartGettingPorts();
961 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700962 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
963 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000964
965 EXPECT_EQ(1U, candidates_.size());
966 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
967 for (size_t i = 0; i < candidates_.size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700968 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidates_[i].type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000969 EXPECT_EQ(
970 candidates_[0].related_address(),
971 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
972 }
973}
974
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700975TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000976 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700977 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
978 allocator().set_candidate_filter(CF_HOST);
979 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 session_->StartGettingPorts();
981 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700982 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
983 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000984 for (size_t i = 0; i < candidates_.size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700985 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidates_[i].type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000986 }
987}
988
989// Host is behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700990TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700992 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000993
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700994 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
995 allocator().set_candidate_filter(CF_REFLEXIVE);
996 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000997 session_->StartGettingPorts();
998 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
999 // Host is behind NAT, no private address will be exposed. Hence only UDP
1000 // port with STUN candidate will be sent outside.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001001 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
1002 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001003 for (size_t i = 0; i < candidates_.size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001004 EXPECT_EQ(std::string(STUN_PORT_TYPE), candidates_[i].type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001005 EXPECT_EQ(
1006 candidates_[0].related_address(),
1007 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
1008 }
1009}
1010
1011// Host is not behind the NAT.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001012TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001013 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001014 allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1015 allocator().set_candidate_filter(CF_REFLEXIVE);
1016 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001017 session_->StartGettingPorts();
1018 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1019 // Host has a public address, both UDP and TCP candidates will be exposed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001020 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001021 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
1022 for (size_t i = 0; i < candidates_.size(); ++i) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001023 EXPECT_EQ(std::string(LOCAL_PORT_TYPE), candidates_[i].type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001024 }
1025}
1026
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001027// Test that we get the same ufrag and pwd for all candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001028TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001029 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001030 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001031 session_->StartGettingPorts();
1032 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001033 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1034 "local", "udp", kClientAddr);
1035 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1036 "stun", "udp", kClientAddr);
1037 EXPECT_PRED5(CheckCandidate, candidates_[5], ICE_CANDIDATE_COMPONENT_RTP,
1038 "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001039 EXPECT_EQ(4U, ports_.size());
1040 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
1041 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
1042 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
1043 EXPECT_EQ(kIcePwd0, candidates_[0].password());
1044 EXPECT_EQ(kIcePwd0, candidates_[1].password());
1045 EXPECT_TRUE(candidate_allocation_done_);
1046}
1047
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001048// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1049// is allocated for udp and stun. Also verify there is only one candidate
1050// (local) if stun candidate is same as local candidate, which will be the case
1051// in a public network like the below test.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001052TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001053 AddInterface(kClientAddr);
1054 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001055 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1056 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001057 session_->StartGettingPorts();
1058 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1059 EXPECT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001060 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1061 "local", "udp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001062 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1063}
1064
1065// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1066// is allocated for udp and stun. In this test we should expect both stun and
1067// local candidates as client behind a nat.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001068TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001069 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001070 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001071
1072 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001073 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1074 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001075 session_->StartGettingPorts();
1076 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1077 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001078 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1079 "local", "udp", kClientAddr);
1080 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1081 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001082 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1083 EXPECT_EQ(3U, candidates_.size());
1084}
1085
deadbeefc5d0d952015-07-16 10:22:21 -07001086// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001087TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001088 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001089 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001090 allocator_.reset(new BasicPortAllocator(&network_manager_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001091
1092 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1093
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001094 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001095 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001096 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1097 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001098
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001099 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001100 session_->StartGettingPorts();
1101
1102 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1103 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001104 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1105 "local", "udp", kClientAddr);
1106 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1107 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1108 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1109 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001110 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1111 EXPECT_EQ(3U, candidates_.size());
1112}
1113
1114// Testing DNS resolve for the TURN server, this will test AllocationSequence
1115// handling the unresolved address signal from TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001116TEST_F(BasicPortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001117 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001118 PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001119 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001120 allocator_.reset(new BasicPortAllocator(&network_manager_));
1121 RelayServerConfig turn_server(RELAY_TURN);
1122 RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef653b8e02015-11-11 12:55:10 -08001123 turn_server.credentials = credentials;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001124 turn_server.ports.push_back(
1125 ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP, false));
deadbeef653b8e02015-11-11 12:55:10 -08001126 allocator_->AddTurnServer(turn_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001127
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001128 allocator_->set_step_delay(kMinimumStepDelay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001129 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001130 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1131 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001132
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001133 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001134 session_->StartGettingPorts();
1135
1136 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1137}
1138
1139// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1140// is allocated for udp/stun/turn. In this test we should expect all local,
1141// stun and turn candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001142TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001143 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001144 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145
1146 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1147
1148 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001149 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1150 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001151
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001152 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001153 session_->StartGettingPorts();
1154
1155 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1156 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001157 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1158 "local", "udp", kClientAddr);
1159 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1160 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1161 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1162 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001163 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1164 EXPECT_EQ(3U, candidates_.size());
1165 // Local port will be created first and then TURN port.
1166 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1167 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1168}
1169
1170// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1171// server is also used as the STUN server, we should get 'local', 'stun', and
1172// 'relay' candidates.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001173TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001174 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001175 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001176 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001177 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1178
1179 // Must set the step delay to 0 to make sure the relay allocation phase is
1180 // started before the STUN candidates are obtained, so that the STUN binding
1181 // response is processed when both StunPort and TurnPort exist to reproduce
1182 // webrtc issue 3537.
1183 allocator_->set_step_delay(0);
1184 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001185 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1186 PORTALLOCATOR_DISABLE_TCP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001187
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001188 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001189 session_->StartGettingPorts();
1190
1191 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001192 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1193 "local", "udp", kClientAddr);
1194 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1195 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1196 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1197 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001198 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1199
1200 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1201 EXPECT_EQ(3U, candidates_.size());
1202 // Local port will be created first and then TURN port.
1203 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1204 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1205}
1206
deadbeefc5d0d952015-07-16 10:22:21 -07001207// Test that when only a TCP TURN server is available, we do NOT use it as
1208// a UDP STUN server, as this could leak our IP address. Thus we should only
1209// expect two ports, a UDPPort and TurnPort.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001210TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001211 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001212 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001213 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001214 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1215
1216 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001217 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1218 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001219
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001220 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001221 session_->StartGettingPorts();
1222
1223 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1224 ASSERT_EQ(2U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001225 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1226 "local", "udp", kClientAddr);
1227 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1228 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001229 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1230 EXPECT_EQ(2U, candidates_.size());
1231 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1232 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1233}
1234
1235// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1236// TURN server is used as the STUN server and we get 'local', 'stun', and
1237// 'relay' candidates.
1238// TODO(deadbeef): Remove this test when support for non-shared socket mode
1239// is removed.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001240TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001241 AddInterface(kClientAddr);
1242 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001243 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001244 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1245
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001246 allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001247
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001248 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001249 session_->StartGettingPorts();
1250
1251 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1252 ASSERT_EQ(3U, ports_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001253 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1254 "local", "udp", kClientAddr);
1255 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1256 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1257 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1258 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001259 // Not using shared socket, so the STUN request's server reflexive address
1260 // should be different than the TURN request's server reflexive address.
1261 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1262
1263 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1264 EXPECT_EQ(3U, candidates_.size());
1265 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1266 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1267 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1268}
1269
1270// Test that even when both a STUN and TURN server are configured, the TURN
1271// server is used as a STUN server and we get a 'stun' candidate.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001272TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
deadbeefc5d0d952015-07-16 10:22:21 -07001273 AddInterface(kClientAddr);
1274 // Configure with STUN server but destroy it, so we can ensure that it's
1275 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001276 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001277 stun_server_.reset();
1278 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1279
1280 allocator_->set_flags(allocator().flags() |
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001281 PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1282 PORTALLOCATOR_DISABLE_TCP);
deadbeefc5d0d952015-07-16 10:22:21 -07001283
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001284 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
deadbeefc5d0d952015-07-16 10:22:21 -07001285 session_->StartGettingPorts();
1286
1287 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001288 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1289 "local", "udp", kClientAddr);
1290 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1291 "stun", "udp", rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1292 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1293 "relay", "udp", rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -07001294 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1295
1296 // Don't bother waiting for STUN timeout, since we already verified
1297 // that we got a STUN candidate from the TURN server.
1298}
1299
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001300// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1301// and fail to generate STUN candidate, local UDP candidate is generated
1302// properly.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001303TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001304 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1305 PORTALLOCATOR_DISABLE_TCP |
1306 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001307 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1308 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001309 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001310 session_->StartGettingPorts();
1311 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1312 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001313 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1314 "local", "udp", kClientAddr);
Taylor Brandstetter8fcf4142016-05-23 12:49:30 -07001315 // STUN timeout is 9.5sec. We need to wait to get candidate done signal.
1316 EXPECT_TRUE_WAIT(candidate_allocation_done_, kStunTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001317 EXPECT_EQ(1U, candidates_.size());
1318}
1319
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001320// Test that when the NetworkManager doesn't have permission to enumerate
1321// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1322// automatically.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001323TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001324 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1325 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001326 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001327 rtc::NetworkManager::ENUMERATION_BLOCKED);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001328 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1329 PORTALLOCATOR_DISABLE_TCP |
1330 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1331 EXPECT_EQ(0U,
1332 allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1333 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
1334 EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001335 session_->StartGettingPorts();
1336 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001337 EXPECT_EQ(1U, candidates_.size());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001338 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1339 "local", "udp", kPrivateAddr);
1340 EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001341}
1342
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001343// This test verifies allocator can use IPv6 addresses along with IPv4.
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001344TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) {
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001345 allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY |
1346 PORTALLOCATOR_ENABLE_IPV6 |
1347 PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001348 AddInterface(kClientIPv6Addr);
1349 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001350 allocator_->set_step_delay(kMinimumStepDelay);
1351 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352 session_->StartGettingPorts();
1353 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1354 EXPECT_EQ(4U, candidates_.size());
1355 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001356 EXPECT_PRED5(CheckCandidate, candidates_[0], ICE_CANDIDATE_COMPONENT_RTP,
1357 "local", "udp", kClientIPv6Addr);
1358 EXPECT_PRED5(CheckCandidate, candidates_[1], ICE_CANDIDATE_COMPONENT_RTP,
1359 "local", "udp", kClientAddr);
1360 EXPECT_PRED5(CheckCandidate, candidates_[2], ICE_CANDIDATE_COMPONENT_RTP,
1361 "local", "tcp", kClientIPv6Addr);
1362 EXPECT_PRED5(CheckCandidate, candidates_[3], ICE_CANDIDATE_COMPONENT_RTP,
1363 "local", "tcp", kClientAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001364 EXPECT_EQ(4U, candidates_.size());
1365}
honghaiz98db68f2015-09-29 07:58:17 -07001366
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001367TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001368 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001369 allocator_->set_step_delay(kDefaultStepDelay);
1370 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001371 session_->StartGettingPorts();
1372 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1373 EXPECT_EQ(2U, ports_.size());
1374 session_->StopGettingPorts();
1375 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1376
1377 // After stopping getting ports, adding a new interface will not start
1378 // getting ports again.
1379 candidates_.clear();
1380 ports_.clear();
1381 candidate_allocation_done_ = false;
1382 network_manager_.AddInterface(kClientAddr2);
1383 rtc::Thread::Current()->ProcessMessages(1000);
1384 EXPECT_EQ(0U, candidates_.size());
1385 EXPECT_EQ(0U, ports_.size());
1386}
1387
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001388TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) {
honghaiz98db68f2015-09-29 07:58:17 -07001389 AddInterface(kClientAddr);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001390 allocator_->set_step_delay(kDefaultStepDelay);
1391 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP));
honghaiz98db68f2015-09-29 07:58:17 -07001392 session_->StartGettingPorts();
1393 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1394 EXPECT_EQ(2U, ports_.size());
1395 session_->ClearGettingPorts();
1396 WAIT(candidate_allocation_done_, 1000);
1397 EXPECT_FALSE(candidate_allocation_done_);
1398
1399 // After clearing getting ports, adding a new interface will start getting
1400 // ports again.
1401 candidates_.clear();
1402 ports_.clear();
1403 candidate_allocation_done_ = false;
1404 network_manager_.AddInterface(kClientAddr2);
1405 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1406 EXPECT_EQ(2U, ports_.size());
1407}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001408
1409// Test that the ports and candidates are updated with new ufrag/pwd/etc. when
1410// a pooled session is taken out of the pool.
1411TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) {
1412 AddInterface(kClientAddr);
1413 int pool_size = 1;
1414 allocator_->SetConfiguration(allocator_->stun_servers(),
1415 allocator_->turn_servers(), pool_size);
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001416 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001417 ASSERT_NE(nullptr, peeked_session);
1418 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1419 kDefaultAllocationTimeout);
1420 // Expect that when TakePooledSession is called,
1421 // UpdateTransportInformationInternal will be called and the
1422 // BasicPortAllocatorSession will update the ufrag/pwd of ports and
1423 // candidates.
1424 session_ =
1425 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1426 ASSERT_NE(nullptr, session_.get());
1427 auto ready_ports = session_->ReadyPorts();
1428 auto candidates = session_->ReadyCandidates();
1429 EXPECT_FALSE(ready_ports.empty());
1430 EXPECT_FALSE(candidates.empty());
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001431 for (const PortInterface* port_interface : ready_ports) {
1432 const Port* port = static_cast<const Port*>(port_interface);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001433 EXPECT_EQ(kContentName, port->content_name());
1434 EXPECT_EQ(1, port->component());
1435 EXPECT_EQ(kIceUfrag0, port->username_fragment());
1436 EXPECT_EQ(kIcePwd0, port->password());
1437 }
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001438 for (const Candidate& candidate : candidates) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001439 EXPECT_EQ(1, candidate.component());
1440 EXPECT_EQ(kIceUfrag0, candidate.username());
1441 EXPECT_EQ(kIcePwd0, candidate.password());
1442 }
1443}
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001444
1445// Test that a new candidate filter takes effect even on already-gathered
1446// candidates.
1447TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) {
1448 AddInterface(kClientAddr);
1449 int pool_size = 1;
1450 allocator_->SetConfiguration(allocator_->stun_servers(),
1451 allocator_->turn_servers(), pool_size);
1452 const PortAllocatorSession* peeked_session = allocator_->GetPooledSession();
1453 ASSERT_NE(nullptr, peeked_session);
1454 EXPECT_EQ_WAIT(true, peeked_session->CandidatesAllocationDone(),
1455 kDefaultAllocationTimeout);
1456 size_t initial_candidates_size = peeked_session->ReadyCandidates().size();
1457 size_t initial_ports_size = peeked_session->ReadyPorts().size();
1458 allocator_->set_candidate_filter(CF_RELAY);
1459 // Assume that when TakePooledSession is called, the candidate filter will be
1460 // applied to the pooled session. This is tested by PortAllocatorTest.
1461 session_ =
1462 allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0);
1463 ASSERT_NE(nullptr, session_.get());
1464 auto candidates = session_->ReadyCandidates();
1465 auto ports = session_->ReadyPorts();
1466 // Sanity check that the number of candidates and ports decreased.
1467 EXPECT_GT(initial_candidates_size, candidates.size());
1468 EXPECT_GT(initial_ports_size, ports.size());
1469 for (const PortInterface* port : ports) {
1470 // Expect only relay ports.
1471 EXPECT_EQ(RELAY_PORT_TYPE, port->Type());
1472 }
1473 for (const Candidate& candidate : candidates) {
1474 // Expect only relay candidates now that the filter is applied.
1475 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type());
1476 // Expect that the raddr is emptied due to the CF_RELAY filter.
1477 EXPECT_EQ(candidate.related_address(),
1478 rtc::EmptySocketAddressWithFamily(candidate.address().family()));
1479 }
1480}
1481
1482} // namespace cricket