blob: 45545f5c50db304c419128fb34f378be41f3eb5a [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
11#include "webrtc/p2p/base/basicpacketsocketfactory.h"
12#include "webrtc/p2p/base/constants.h"
13#include "webrtc/p2p/base/p2ptransportchannel.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/p2p/base/testrelayserver.h"
15#include "webrtc/p2p/base/teststunserver.h"
16#include "webrtc/p2p/base/testturnserver.h"
17#include "webrtc/p2p/client/basicportallocator.h"
18#include "webrtc/p2p/client/httpportallocator.h"
19#include "webrtc/base/fakenetwork.h"
20#include "webrtc/base/firewallsocketserver.h"
21#include "webrtc/base/gunit.h"
22#include "webrtc/base/helpers.h"
honghaizf421bdc2015-07-17 16:21:55 -070023#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#include "webrtc/base/logging.h"
25#include "webrtc/base/natserver.h"
26#include "webrtc/base/natsocketfactory.h"
27#include "webrtc/base/network.h"
28#include "webrtc/base/physicalsocketserver.h"
29#include "webrtc/base/socketaddress.h"
30#include "webrtc/base/ssladapter.h"
31#include "webrtc/base/thread.h"
32#include "webrtc/base/virtualsocketserver.h"
33
34using cricket::ServerAddresses;
35using rtc::SocketAddress;
36using rtc::Thread;
37
38static const SocketAddress kClientAddr("11.11.11.11", 0);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070039static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000041static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042static const SocketAddress kClientIPv6Addr(
43 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
44static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070045static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
46static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
48static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
49static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
50static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
51static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
52static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
53static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
54static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
55static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
56static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
57static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
58
59// Minimum and maximum port for port range tests.
60static const int kMinPort = 10000;
61static const int kMaxPort = 10099;
62
63// Based on ICE_UFRAG_LENGTH
64static const char kIceUfrag0[] = "TESTICEUFRAG0000";
65// Based on ICE_PWD_LENGTH
66static const char kIcePwd0[] = "TESTICEPWD00000000000000";
67
68static const char kContentName[] = "test content";
69
70static const int kDefaultAllocationTimeout = 1000;
71static const char kTurnUsername[] = "test";
72static const char kTurnPassword[] = "test";
73
74namespace cricket {
75
76// Helper for dumping candidates
77std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
78 os << c.ToString();
79 return os;
80}
81
82} // namespace cricket
83
84class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
85 public:
86 PortAllocatorTest()
87 : pss_(new rtc::PhysicalSocketServer),
88 vss_(new rtc::VirtualSocketServer(pss_.get())),
89 fss_(new rtc::FirewallSocketServer(vss_.get())),
90 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070091 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -070092 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
94 kStunAddr)),
95 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
96 kRelayTcpIntAddr, kRelayTcpExtAddr,
97 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
98 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
99 candidate_allocation_done_(false) {
100 cricket::ServerAddresses stun_servers;
101 stun_servers.insert(kStunAddr);
102 // Passing the addresses of GTURN servers will enable GTURN in
103 // Basicportallocator.
104 allocator_.reset(new cricket::BasicPortAllocator(
105 &network_manager_,
106 stun_servers,
107 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
108 allocator_->set_step_delay(cricket::kMinimumStepDelay);
109 }
110
111 void AddInterface(const SocketAddress& addr) {
112 network_manager_.AddInterface(addr);
113 }
honghaiz8c404fa2015-09-28 07:59:43 -0700114 void AddInterface(const SocketAddress& addr, const std::string& if_name) {
115 network_manager_.AddInterface(addr, if_name);
116 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800117 // The default route is the public address that STUN server will observe when
118 // the endpoint is sitting on the public internet and the local port is bound
119 // to the "any" address. This may be different from the default local address
120 // which the endpoint observes. This can occur if the route to the public
121 // endpoint like 8.8.8.8 (specified as the default local address) is
122 // different from the route to the STUN server (the default route).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700123 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
124 AddInterface(addr);
125 // When a binding comes from the any address, the |addr| will be used as the
126 // srflx address.
127 vss_->SetDefaultRoute(addr.ipaddr());
128 }
honghaiz8c404fa2015-09-28 07:59:43 -0700129 void RemoveInterface(const SocketAddress& addr) {
130 network_manager_.RemoveInterface(addr);
131 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 bool SetPortRange(int min_port, int max_port) {
133 return allocator_->SetPortRange(min_port, max_port);
134 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700135 // Endpoint is on the public network. No STUN or TURN.
136 void ResetWithNoServersOrNat() {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700137 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
138 allocator_->set_step_delay(cricket::kMinimumStepDelay);
139 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700140 // Endpoint is behind a NAT, with STUN specified.
141 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
142 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700143 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700144 // Endpoint is on the public network, with STUN specified.
145 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
146 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700148 // Endpoint is on the public network, with TURN specified.
149 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
150 const rtc::SocketAddress& tcp_turn) {
151 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 AddTurnServers(udp_turn, tcp_turn);
153 }
154
155 void AddTurnServers(const rtc::SocketAddress& udp_turn,
156 const rtc::SocketAddress& tcp_turn) {
deadbeef18a944b2015-10-26 19:21:40 -0700157 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef18a944b2015-10-26 19:21:40 -0700159 relay_server.credentials = credentials;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160
161 if (!udp_turn.IsNil()) {
deadbeef18a944b2015-10-26 19:21:40 -0700162 relay_server.ports.push_back(cricket::ProtocolAddress(
163 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000164 }
165 if (!tcp_turn.IsNil()) {
deadbeef18a944b2015-10-26 19:21:40 -0700166 relay_server.ports.push_back(cricket::ProtocolAddress(
167 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 }
deadbeef18a944b2015-10-26 19:21:40 -0700169 allocator_->AddRelay(relay_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 }
171
172 bool CreateSession(int component) {
173 session_.reset(CreateSession("session", component));
174 if (!session_)
175 return false;
176 return true;
177 }
178
179 bool CreateSession(int component, const std::string& content_name) {
180 session_.reset(CreateSession("session", content_name, component));
181 if (!session_)
182 return false;
183 return true;
184 }
185
186 cricket::PortAllocatorSession* CreateSession(
187 const std::string& sid, int component) {
188 return CreateSession(sid, kContentName, component);
189 }
190
191 cricket::PortAllocatorSession* CreateSession(
192 const std::string& sid, const std::string& content_name, int component) {
193 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
194 }
195
196 cricket::PortAllocatorSession* CreateSession(
197 const std::string& sid, const std::string& content_name, int component,
198 const std::string& ice_ufrag, const std::string& ice_pwd) {
199 cricket::PortAllocatorSession* session =
200 allocator_->CreateSession(
201 sid, content_name, component, ice_ufrag, ice_pwd);
202 session->SignalPortReady.connect(this,
203 &PortAllocatorTest::OnPortReady);
204 session->SignalCandidatesReady.connect(this,
205 &PortAllocatorTest::OnCandidatesReady);
206 session->SignalCandidatesAllocationDone.connect(this,
207 &PortAllocatorTest::OnCandidatesAllocationDone);
208 return session;
209 }
210
211 static bool CheckCandidate(const cricket::Candidate& c,
212 int component, const std::string& type,
213 const std::string& proto,
214 const SocketAddress& addr) {
215 return (c.component() == component && c.type() == type &&
216 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
217 ((addr.port() == 0 && (c.address().port() != 0)) ||
218 (c.address().port() == addr.port())));
219 }
220 static bool CheckPort(const rtc::SocketAddress& addr,
221 int min_port, int max_port) {
222 return (addr.port() >= min_port && addr.port() <= max_port);
223 }
224
225 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
226 // We should only get this callback once, except in the mux test where
227 // we have multiple port allocation sessions.
228 if (session == session_.get()) {
229 ASSERT_FALSE(candidate_allocation_done_);
230 candidate_allocation_done_ = true;
231 }
232 }
233
234 // Check if all ports allocated have send-buffer size |expected|. If
235 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
236 void CheckSendBufferSizesOfAllPorts(int expected) {
237 std::vector<cricket::PortInterface*>::iterator it;
238 for (it = ports_.begin(); it < ports_.end(); ++it) {
239 int send_buffer_size;
240 if (expected == -1) {
241 EXPECT_EQ(SOCKET_ERROR,
242 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
243 &send_buffer_size));
244 } else {
245 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
246 &send_buffer_size));
247 ASSERT_EQ(expected, send_buffer_size);
248 }
249 }
250 }
251
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700252 // This function starts the port/address gathering and check the existence of
253 // candidates as specified. When |expect_stun_candidate| is true,
254 // |stun_candidate_addr| carries the expected reflective address, which is
255 // also the related address for TURN candidate if it is expected. Otherwise,
256 // it should be ignore.
257 void CheckDisableAdapterEnumeration(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200258 uint32_t total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700259 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700260 const rtc::IPAddress& stun_candidate_addr,
261 const rtc::IPAddress& relay_candidate_udp_transport_addr,
262 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800263 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
264 rtc::IPAddress());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700265 if (!session_) {
266 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
267 }
268 session_->set_flags(session_->flags() |
269 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700270 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
271 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000272 session_->StartGettingPorts();
273 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
274
Peter Boström0c4e06b2015-10-07 12:23:21 +0200275 uint32_t total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700276 if (!host_candidate_addr.IsNil()) {
277 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
278 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800279 rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700280 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700281 }
282 if (!stun_candidate_addr.IsNil()) {
283 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700284 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
285 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800286 rtc::IPAddress related_address = host_candidate_addr;
287 if (host_candidate_addr.IsNil()) {
288 related_address =
289 rtc::GetAnyIP(candidates_[total_candidates].address().family());
290 }
291 EXPECT_EQ(related_address,
292 candidates_[total_candidates].related_address().ipaddr());
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700293 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700294 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700295 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700296 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700297 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
298 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700299 EXPECT_EQ(stun_candidate_addr,
300 candidates_[total_candidates].related_address().ipaddr());
301 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700302 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700303 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700304 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700305 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
306 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700307 EXPECT_EQ(stun_candidate_addr,
308 candidates_[total_candidates].related_address().ipaddr());
309 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700310 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000311
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700312 EXPECT_EQ(total_candidates, candidates_.size());
313 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000314 }
315
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000316 protected:
317 cricket::BasicPortAllocator& allocator() {
318 return *allocator_;
319 }
320
321 void OnPortReady(cricket::PortAllocatorSession* ses,
322 cricket::PortInterface* port) {
323 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
324 ports_.push_back(port);
325 }
326 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
327 const std::vector<cricket::Candidate>& candidates) {
328 for (size_t i = 0; i < candidates.size(); ++i) {
329 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
330 candidates_.push_back(candidates[i]);
331 }
332 }
333
334 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
deadbeef18a944b2015-10-26 19:21:40 -0700335 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
336 cricket::RelayServerConfig server_config = allocator_->relays()[i];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000337 cricket::PortList::const_iterator relay_port;
338 for (relay_port = server_config.ports.begin();
339 relay_port != server_config.ports.end(); ++relay_port) {
340 if (proto_addr.address == relay_port->address &&
341 proto_addr.proto == relay_port->proto)
342 return true;
343 }
344 }
345 return false;
346 }
347
Guo-wei Shieh11477022015-08-15 09:28:41 -0700348 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
349 bool with_nat) {
350 if (with_nat) {
351 nat_server_.reset(new rtc::NATServer(
352 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
353 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
354 } else {
355 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
356 }
357
358 ServerAddresses stun_servers;
359 if (!stun_server.IsNil()) {
360 stun_servers.insert(stun_server);
361 }
362 allocator_.reset(new cricket::BasicPortAllocator(
363 &network_manager_, nat_socket_factory_.get(), stun_servers));
364 allocator().set_step_delay(cricket::kMinimumStepDelay);
365 }
366
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000367 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
368 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
369 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
370 rtc::SocketServerScope ss_scope_;
371 rtc::scoped_ptr<rtc::NATServer> nat_server_;
372 rtc::NATSocketFactory nat_factory_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700373 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000374 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
375 cricket::TestRelayServer relay_server_;
376 cricket::TestTurnServer turn_server_;
377 rtc::FakeNetworkManager network_manager_;
378 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
379 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
380 std::vector<cricket::PortInterface*> ports_;
381 std::vector<cricket::Candidate> candidates_;
382 bool candidate_allocation_done_;
383};
384
385// Tests that we can init the port allocator and create a session.
386TEST_F(PortAllocatorTest, TestBasic) {
387 EXPECT_EQ(&network_manager_, allocator().network_manager());
388 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
deadbeef18a944b2015-10-26 19:21:40 -0700389 ASSERT_EQ(1u, allocator().relays().size());
390 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000391 // Empty relay credentials are used for GTURN.
deadbeef18a944b2015-10-26 19:21:40 -0700392 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
393 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
395 kRelayUdpIntAddr, cricket::PROTO_UDP)));
396 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
397 kRelayTcpIntAddr, cricket::PROTO_TCP)));
398 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
399 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
400 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
401}
402
403// Tests that we allocator session not trying to allocate ports for every 250ms.
404TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
405 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
406 session_->StartGettingPorts();
407 // Waiting for one second to make sure BasicPortAllocatorSession has not
408 // called OnAllocate multiple times. In old behavior it's called every 250ms.
409 // When there are no network interfaces, each execution of OnAllocate will
410 // result in SignalCandidatesAllocationDone signal.
411 rtc::Thread::Current()->ProcessMessages(1000);
412 EXPECT_TRUE(candidate_allocation_done_);
413 EXPECT_EQ(0U, candidates_.size());
414}
415
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700416// Test that we could use loopback interface as host candidate.
417TEST_F(PortAllocatorTest, TestLoopbackNetworkInterface) {
418 AddInterface(kLoopbackAddr);
419 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
420 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
421 cricket::PORTALLOCATOR_DISABLE_RELAY |
422 cricket::PORTALLOCATOR_DISABLE_TCP);
423 session_->StartGettingPorts();
424 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
425 EXPECT_EQ(1U, candidates_.size());
426}
427
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000428// Tests that we can get all the desired addresses successfully.
429TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
430 AddInterface(kClientAddr);
431 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
432 session_->StartGettingPorts();
433 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
434 EXPECT_EQ(4U, ports_.size());
435 EXPECT_PRED5(CheckCandidate, candidates_[0],
436 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
437 EXPECT_PRED5(CheckCandidate, candidates_[1],
438 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
439 EXPECT_PRED5(CheckCandidate, candidates_[2],
440 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
441 EXPECT_PRED5(CheckCandidate, candidates_[3],
442 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
443 EXPECT_PRED5(CheckCandidate, candidates_[4],
444 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
445 EXPECT_PRED5(CheckCandidate, candidates_[5],
446 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
447 EXPECT_PRED5(CheckCandidate, candidates_[6],
448 cricket::ICE_CANDIDATE_COMPONENT_RTP,
449 "relay", "ssltcp", kRelaySslTcpIntAddr);
450 EXPECT_TRUE(candidate_allocation_done_);
451}
452
honghaiz8c404fa2015-09-28 07:59:43 -0700453// Test that when the same network interface is brought down and up, the
454// port allocator session will restart a new allocation sequence if
455// it is not stopped.
456TEST_F(PortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
457 std::string if_name("test_net0");
458 AddInterface(kClientAddr, if_name);
459 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
460 session_->StartGettingPorts();
461 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
462 EXPECT_EQ(4U, ports_.size());
463 EXPECT_TRUE(candidate_allocation_done_);
464 candidate_allocation_done_ = false;
465 candidates_.clear();
466 ports_.clear();
467
468 RemoveInterface(kClientAddr);
469 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
470 EXPECT_EQ(0U, ports_.size());
471 EXPECT_FALSE(candidate_allocation_done_);
472
473 // When the same interfaces are added again, new candidates/ports should be
474 // generated.
475 AddInterface(kClientAddr, if_name);
476 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
477 EXPECT_EQ(4U, ports_.size());
478 EXPECT_TRUE(candidate_allocation_done_);
479}
480
481// Test that when the same network interface is brought down and up, the
482// port allocator session will not restart a new allocation sequence if
483// it is stopped.
484TEST_F(PortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
485 std::string if_name("test_net0");
486 AddInterface(kClientAddr, if_name);
487 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
488 session_->StartGettingPorts();
489 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
490 EXPECT_EQ(4U, ports_.size());
491 EXPECT_TRUE(candidate_allocation_done_);
492 session_->StopGettingPorts();
493 candidates_.clear();
494 ports_.clear();
495
496 RemoveInterface(kClientAddr);
497 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
498 EXPECT_EQ(0U, ports_.size());
499
500 // When the same interfaces are added again, new candidates/ports should not
501 // be generated because the session has stopped.
502 AddInterface(kClientAddr, if_name);
503 ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
504 EXPECT_EQ(0U, ports_.size());
505 EXPECT_TRUE(candidate_allocation_done_);
506}
507
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000508// Verify candidates with default step delay of 1sec.
509TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
510 AddInterface(kClientAddr);
511 allocator_->set_step_delay(cricket::kDefaultStepDelay);
512 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
513 session_->StartGettingPorts();
514 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
515 EXPECT_EQ(2U, ports_.size());
516 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
517 EXPECT_EQ(3U, ports_.size());
518 EXPECT_PRED5(CheckCandidate, candidates_[2],
519 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
520 EXPECT_PRED5(CheckCandidate, candidates_[3],
521 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
522 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
523 EXPECT_PRED5(CheckCandidate, candidates_[4],
524 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
525 EXPECT_PRED5(CheckCandidate, candidates_[5],
526 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
527 EXPECT_EQ(4U, ports_.size());
528 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
529 EXPECT_PRED5(CheckCandidate, candidates_[6],
530 cricket::ICE_CANDIDATE_COMPONENT_RTP,
531 "relay", "ssltcp", kRelaySslTcpIntAddr);
532 EXPECT_EQ(4U, ports_.size());
533 EXPECT_TRUE(candidate_allocation_done_);
534 // If we Stop gathering now, we shouldn't get a second "done" callback.
535 session_->StopGettingPorts();
536}
537
538TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
539 AddInterface(kClientAddr);
540 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
541 cricket::CN_VIDEO));
542 session_->StartGettingPorts();
543 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
544 EXPECT_TRUE(candidate_allocation_done_);
545 // If we Stop gathering now, we shouldn't get a second "done" callback.
546 session_->StopGettingPorts();
547
548 // All ports should have unset send-buffer sizes.
549 CheckSendBufferSizesOfAllPorts(-1);
550}
551
552// Tests that we can get callback after StopGetAllPorts.
553TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
554 AddInterface(kClientAddr);
555 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
556 session_->StartGettingPorts();
557 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
558 EXPECT_EQ(2U, ports_.size());
559 session_->StopGettingPorts();
560 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
561}
562
563// Test that we restrict client ports appropriately when a port range is set.
564// We check the candidates for udp/stun/tcp ports, and the from address
565// for relay ports.
566TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
567 AddInterface(kClientAddr);
568 // Check that an invalid port range fails.
569 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
570 // Check that a null port range succeeds.
571 EXPECT_TRUE(SetPortRange(0, 0));
572 // Check that a valid port range succeeds.
573 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
574 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
575 session_->StartGettingPorts();
576 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
577 EXPECT_EQ(4U, ports_.size());
578 // Check the port number for the UDP port object.
579 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
580 // Check the port number for the STUN port object.
581 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
582 // Check the port number used to connect to the relay server.
583 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
584 kMinPort, kMaxPort);
585 // Check the port number for the TCP port object.
586 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
587 EXPECT_TRUE(candidate_allocation_done_);
588}
589
590// Test that we don't crash or malfunction if we have no network adapters.
591TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
592 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
593 session_->StartGettingPorts();
594 rtc::Thread::Current()->ProcessMessages(100);
595 // Without network adapter, we should not get any candidate.
596 EXPECT_EQ(0U, candidates_.size());
597 EXPECT_TRUE(candidate_allocation_done_);
598}
599
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700600// Test that when enumeration is disabled, we should not have any ports when
601// candidate_filter() is set to CF_RELAY and no relay is specified.
602TEST_F(PortAllocatorTest,
603 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
Guo-wei Shieh898d21c2015-09-30 10:54:55 -0700604 ResetWithStunServerNoNat(kStunAddr);
605 allocator().set_candidate_filter(cricket::CF_RELAY);
606 // Expect to see no ports and no candidates.
607 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
608 rtc::IPAddress(), rtc::IPAddress());
609}
610
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800611// Test that even with multiple interfaces, the result should still be a single
612// default private, one STUN and one TURN candidate since we bind to any address
613// (i.e. all 0s).
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700614TEST_F(PortAllocatorTest,
615 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000616 AddInterface(kPrivateAddr);
617 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700618 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000619 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800620 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and a default private,
621 // STUN and TURN/UDP candidates.
622 CheckDisableAdapterEnumeration(3U, kPrivateAddr.ipaddr(),
623 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
624 rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700625}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000626
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800627// Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
628// candidates when both TURN/UDP and TURN/TCP servers are specified.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700629TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
630 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800631 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700632 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700633 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800634 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
635 // private, STUN, TURN/UDP, and TURN/TCP candidates.
636 CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
637 kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700638 kTurnUdpExtAddr.ipaddr());
639}
640
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800641// Test that when adapter enumeration is disabled, for endpoints without
642// STUN/TURN specified, a default private candidate is still generated.
643TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
644 ResetWithNoServersOrNat();
645 // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
646 CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
647 rtc::IPAddress(), rtc::IPAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700648}
649
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800650// Test that when adapter enumeration is disabled, with
651// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
652// a NAT, there is no local candidate.
653TEST_F(PortAllocatorTest,
654 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
655 ResetWithStunServerNoNat(kStunAddr);
656 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
657 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
658 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
659 // candidate.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700660 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700661 rtc::IPAddress(), rtc::IPAddress());
662}
663
664// Test that when adapter enumeration is disabled, with
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800665// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
666// a NAT, there is no local candidate. However, this specified default route
667// (kClientAddr) which was discovered when sending STUN requests, will become
668// the srflx addresses.
669TEST_F(
670 PortAllocatorTest,
671 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700672 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800673 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700674 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800675 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700676 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
677 // candidate.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800678 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
679 rtc::IPAddress(), rtc::IPAddress());
680}
681
682// Test that when adapter enumeration is disabled, with
683// PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
684// NAT, there is only one STUN candidate.
685TEST_F(PortAllocatorTest,
686 TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
687 ResetWithStunServerAndNat(kStunAddr);
688 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
689 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
690 // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
691 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
692 rtc::IPAddress(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000693}
694
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700695// Test that we disable relay over UDP, and only TCP is used when connecting to
696// the relay server.
697TEST_F(PortAllocatorTest, TestDisableUdpTurn) {
698 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
699 AddInterface(kClientAddr);
700 ResetWithStunServerAndNat(kStunAddr);
701 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
702 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
703 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY |
704 cricket::PORTALLOCATOR_DISABLE_UDP |
705 cricket::PORTALLOCATOR_DISABLE_STUN |
706 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
707
708 session_->StartGettingPorts();
709 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
710
711 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
712 // TURN/TCP candidates.
713 EXPECT_EQ(2U, ports_.size());
714 EXPECT_EQ(2U, candidates_.size());
715 EXPECT_PRED5(CheckCandidate, candidates_[0],
716 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
717 kTurnUdpExtAddr);
718 // The TURN candidate should use TCP to contact the TURN server.
719 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
720 EXPECT_PRED5(CheckCandidate, candidates_[1],
721 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
722 kClientAddr);
723}
724
Erik Språngefdce692015-06-05 09:41:26 +0200725// Disable for asan, see
726// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
727#if !defined(ADDRESS_SANITIZER)
728
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000729// Test that we can get OnCandidatesAllocationDone callback when all the ports
730// are disabled.
731TEST_F(PortAllocatorTest, TestDisableAllPorts) {
732 AddInterface(kClientAddr);
733 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
734 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
735 cricket::PORTALLOCATOR_DISABLE_STUN |
736 cricket::PORTALLOCATOR_DISABLE_RELAY |
737 cricket::PORTALLOCATOR_DISABLE_TCP);
738 session_->StartGettingPorts();
739 rtc::Thread::Current()->ProcessMessages(100);
740 EXPECT_EQ(0U, candidates_.size());
741 EXPECT_TRUE(candidate_allocation_done_);
742}
743
744// Test that we don't crash or malfunction if we can't create UDP sockets.
745TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
746 AddInterface(kClientAddr);
747 fss_->set_udp_sockets_enabled(false);
748 EXPECT_TRUE(CreateSession(1));
749 session_->StartGettingPorts();
750 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
751 EXPECT_EQ(2U, ports_.size());
752 EXPECT_PRED5(CheckCandidate, candidates_[0],
753 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
754 EXPECT_PRED5(CheckCandidate, candidates_[1],
755 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
756 EXPECT_PRED5(CheckCandidate, candidates_[2],
757 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
758 EXPECT_PRED5(CheckCandidate, candidates_[3],
759 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
760 EXPECT_PRED5(CheckCandidate, candidates_[4],
761 cricket::ICE_CANDIDATE_COMPONENT_RTP,
762 "relay", "ssltcp", kRelaySslTcpIntAddr);
763 EXPECT_TRUE(candidate_allocation_done_);
764}
765
Erik Språngefdce692015-06-05 09:41:26 +0200766#endif // if !defined(ADDRESS_SANITIZER)
767
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000768// Test that we don't crash or malfunction if we can't create UDP sockets or
769// listen on TCP sockets. We still give out a local TCP address, since
770// apparently this is needed for the remote side to accept our connection.
771TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
772 AddInterface(kClientAddr);
773 fss_->set_udp_sockets_enabled(false);
774 fss_->set_tcp_listen_enabled(false);
775 EXPECT_TRUE(CreateSession(1));
776 session_->StartGettingPorts();
777 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
778 EXPECT_EQ(2U, ports_.size());
779 EXPECT_PRED5(CheckCandidate, candidates_[0],
780 1, "relay", "udp", kRelayUdpIntAddr);
781 EXPECT_PRED5(CheckCandidate, candidates_[1],
782 1, "relay", "udp", kRelayUdpExtAddr);
783 EXPECT_PRED5(CheckCandidate, candidates_[2],
784 1, "relay", "tcp", kRelayTcpIntAddr);
785 EXPECT_PRED5(CheckCandidate, candidates_[3],
786 1, "local", "tcp", kClientAddr);
787 EXPECT_PRED5(CheckCandidate, candidates_[4],
788 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
789 EXPECT_TRUE(candidate_allocation_done_);
790}
791
792// Test that we don't crash or malfunction if we can't create any sockets.
793// TODO: Find a way to exit early here.
794TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
795 AddInterface(kClientAddr);
796 fss_->set_tcp_sockets_enabled(false);
797 fss_->set_udp_sockets_enabled(false);
798 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
799 session_->StartGettingPorts();
800 WAIT(candidates_.size() > 0, 2000);
801 // TODO - Check candidate_allocation_done signal.
802 // In case of Relay, ports creation will succeed but sockets will fail.
803 // There is no error reporting from RelayEntry to handle this failure.
804}
805
806// Testing STUN timeout.
807TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
808 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
809 AddInterface(kClientAddr);
810 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
811 session_->StartGettingPorts();
812 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
813 EXPECT_EQ(2U, ports_.size());
814 EXPECT_PRED5(CheckCandidate, candidates_[0],
815 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
816 EXPECT_PRED5(CheckCandidate, candidates_[1],
817 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
818 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
819 // will be tried after 3 seconds.
820 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
821 EXPECT_EQ(3U, ports_.size());
822 EXPECT_PRED5(CheckCandidate, candidates_[2],
823 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
824 EXPECT_PRED5(CheckCandidate, candidates_[3],
825 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
826 EXPECT_PRED5(CheckCandidate, candidates_[4],
827 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
828 kRelaySslTcpIntAddr);
829 EXPECT_PRED5(CheckCandidate, candidates_[5],
830 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
831 // Stun Timeout is 9sec.
832 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
833}
834
835TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
836 AddInterface(kClientAddr);
837 AddInterface(kClientAddr2);
838 // Allocating only host UDP ports. This is done purely for testing
839 // convenience.
840 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
841 cricket::PORTALLOCATOR_DISABLE_STUN |
842 cricket::PORTALLOCATOR_DISABLE_RELAY);
843 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
844 session_->StartGettingPorts();
845 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
846 ASSERT_EQ(2U, candidates_.size());
847 EXPECT_EQ(2U, ports_.size());
848 // Candidates priorities should be different.
849 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
850}
851
852// Test to verify ICE restart process.
853TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
854 AddInterface(kClientAddr);
855 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
856 session_->StartGettingPorts();
857 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
858 EXPECT_EQ(4U, ports_.size());
859 EXPECT_TRUE(candidate_allocation_done_);
860 // TODO - Extend this to verify ICE restart.
861}
862
863// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
864// This test also verifies that when the allocator is only allowed to use
865// relay (i.e. IceTransportsType is relay), the raddr is an empty
866// address with the correct family. This is to prevent any local
867// reflective address leakage in the sdp line.
868TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
869 AddInterface(kClientAddr);
870 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700871 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000872 allocator().set_candidate_filter(cricket::CF_RELAY);
873 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
874 session_->StartGettingPorts();
875 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
876 EXPECT_PRED5(CheckCandidate,
877 candidates_[0],
878 cricket::ICE_CANDIDATE_COMPONENT_RTP,
879 "relay",
880 "udp",
881 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
882
883 EXPECT_EQ(1U, candidates_.size());
884 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
885 for (size_t i = 0; i < candidates_.size(); ++i) {
886 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
887 EXPECT_EQ(
888 candidates_[0].related_address(),
889 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
890 }
891}
892
893TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
894 AddInterface(kClientAddr);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700895 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000896 allocator().set_candidate_filter(cricket::CF_HOST);
897 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
898 session_->StartGettingPorts();
899 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
900 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
901 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
902 for (size_t i = 0; i < candidates_.size(); ++i) {
903 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
904 }
905}
906
907// Host is behind the NAT.
908TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
909 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700910 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000911
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700912 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000913 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
914 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
915 session_->StartGettingPorts();
916 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
917 // Host is behind NAT, no private address will be exposed. Hence only UDP
918 // port with STUN candidate will be sent outside.
919 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
920 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
921 for (size_t i = 0; i < candidates_.size(); ++i) {
922 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
923 EXPECT_EQ(
924 candidates_[0].related_address(),
925 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
926 }
927}
928
929// Host is not behind the NAT.
930TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
931 AddInterface(kClientAddr);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700932 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
934 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
935 session_->StartGettingPorts();
936 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
937 // Host has a public address, both UDP and TCP candidates will be exposed.
938 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
939 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
940 for (size_t i = 0; i < candidates_.size(); ++i) {
941 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
942 }
943}
944
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700945// Test that we get the same ufrag and pwd for all candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000946TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000947 AddInterface(kClientAddr);
948 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
949 session_->StartGettingPorts();
950 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
951 EXPECT_PRED5(CheckCandidate, candidates_[0],
952 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
953 EXPECT_PRED5(CheckCandidate, candidates_[1],
954 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
955 EXPECT_PRED5(CheckCandidate, candidates_[5],
956 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
957 EXPECT_EQ(4U, ports_.size());
958 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
959 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
960 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
961 EXPECT_EQ(kIcePwd0, candidates_[0].password());
962 EXPECT_EQ(kIcePwd0, candidates_[1].password());
963 EXPECT_TRUE(candidate_allocation_done_);
964}
965
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000966// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
967// is allocated for udp and stun. Also verify there is only one candidate
968// (local) if stun candidate is same as local candidate, which will be the case
969// in a public network like the below test.
970TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
971 AddInterface(kClientAddr);
972 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000973 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
974 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
975 session_->StartGettingPorts();
976 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
977 EXPECT_EQ(3U, ports_.size());
978 EXPECT_PRED5(CheckCandidate, candidates_[0],
979 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
980 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
981}
982
983// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
984// is allocated for udp and stun. In this test we should expect both stun and
985// local candidates as client behind a nat.
986TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
987 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700988 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989
990 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
992 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
993 session_->StartGettingPorts();
994 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
995 ASSERT_EQ(2U, ports_.size());
996 EXPECT_PRED5(CheckCandidate, candidates_[0],
997 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
998 EXPECT_PRED5(CheckCandidate, candidates_[1],
999 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001000 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001001 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1002 EXPECT_EQ(3U, candidates_.size());
1003}
1004
deadbeefc5d0d952015-07-16 10:22:21 -07001005// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001006TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
1007 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1008 AddInterface(kClientAddr);
1009 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
1010
1011 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1012
1013 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1014 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001015 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1016 cricket::PORTALLOCATOR_DISABLE_TCP);
1017
1018 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1019 session_->StartGettingPorts();
1020
1021 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1022 ASSERT_EQ(3U, ports_.size());
1023 EXPECT_PRED5(CheckCandidate, candidates_[0],
1024 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1025 EXPECT_PRED5(CheckCandidate, candidates_[1],
1026 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1027 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1028 EXPECT_PRED5(CheckCandidate, candidates_[2],
1029 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1030 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1031 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1032 EXPECT_EQ(3U, candidates_.size());
1033}
1034
1035// Testing DNS resolve for the TURN server, this will test AllocationSequence
1036// handling the unresolved address signal from TurnPort.
1037TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
1038 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
1039 cricket::PROTO_UDP);
1040 AddInterface(kClientAddr);
1041 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
deadbeef18a944b2015-10-26 19:21:40 -07001042 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001043 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
deadbeef18a944b2015-10-26 19:21:40 -07001044 relay_server.credentials = credentials;
1045 relay_server.ports.push_back(cricket::ProtocolAddress(
1046 rtc::SocketAddress("localhost", 3478),
1047 cricket::PROTO_UDP, false));
1048 allocator_->AddRelay(relay_server);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001049
1050 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1051 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1053 cricket::PORTALLOCATOR_DISABLE_TCP);
1054
1055 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1056 session_->StartGettingPorts();
1057
1058 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1059}
1060
1061// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1062// is allocated for udp/stun/turn. In this test we should expect all local,
1063// stun and turn candidates.
1064TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
1065 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001066 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001067
1068 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1069
1070 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001071 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1072 cricket::PORTALLOCATOR_DISABLE_TCP);
1073
1074 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1075 session_->StartGettingPorts();
1076
1077 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1078 ASSERT_EQ(2U, ports_.size());
1079 EXPECT_PRED5(CheckCandidate, candidates_[0],
1080 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1081 EXPECT_PRED5(CheckCandidate, candidates_[1],
1082 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001083 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001084 EXPECT_PRED5(CheckCandidate, candidates_[2],
1085 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1086 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1087 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1088 EXPECT_EQ(3U, candidates_.size());
1089 // Local port will be created first and then TURN port.
1090 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1091 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1092}
1093
1094// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1095// server is also used as the STUN server, we should get 'local', 'stun', and
1096// 'relay' candidates.
1097TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1098 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001099 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001100 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001101 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1102
1103 // Must set the step delay to 0 to make sure the relay allocation phase is
1104 // started before the STUN candidates are obtained, so that the STUN binding
1105 // response is processed when both StunPort and TurnPort exist to reproduce
1106 // webrtc issue 3537.
1107 allocator_->set_step_delay(0);
1108 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001109 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1110 cricket::PORTALLOCATOR_DISABLE_TCP);
1111
1112 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1113 session_->StartGettingPorts();
1114
1115 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1116 EXPECT_PRED5(CheckCandidate, candidates_[0],
1117 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1118 EXPECT_PRED5(CheckCandidate, candidates_[1],
1119 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001120 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001121 EXPECT_PRED5(CheckCandidate, candidates_[2],
1122 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1123 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1124 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1125
1126 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1127 EXPECT_EQ(3U, candidates_.size());
1128 // Local port will be created first and then TURN port.
1129 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1130 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1131}
1132
deadbeefc5d0d952015-07-16 10:22:21 -07001133// Test that when only a TCP TURN server is available, we do NOT use it as
1134// a UDP STUN server, as this could leak our IP address. Thus we should only
1135// expect two ports, a UDPPort and TurnPort.
1136TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1137 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1138 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001139 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001140 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1141
1142 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001143 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1144 cricket::PORTALLOCATOR_DISABLE_TCP);
1145
1146 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1147 session_->StartGettingPorts();
1148
1149 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1150 ASSERT_EQ(2U, ports_.size());
1151 EXPECT_PRED5(CheckCandidate, candidates_[0],
1152 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1153 kClientAddr);
1154 EXPECT_PRED5(CheckCandidate, candidates_[1],
1155 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1156 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1157 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1158 EXPECT_EQ(2U, candidates_.size());
1159 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1160 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1161}
1162
1163// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1164// TURN server is used as the STUN server and we get 'local', 'stun', and
1165// 'relay' candidates.
1166// TODO(deadbeef): Remove this test when support for non-shared socket mode
1167// is removed.
1168TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1169 AddInterface(kClientAddr);
1170 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001171 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001172 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1173
1174 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001175 cricket::PORTALLOCATOR_DISABLE_TCP);
1176
1177 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1178 session_->StartGettingPorts();
1179
1180 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1181 ASSERT_EQ(3U, ports_.size());
1182 EXPECT_PRED5(CheckCandidate, candidates_[0],
1183 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1184 kClientAddr);
1185 EXPECT_PRED5(CheckCandidate, candidates_[1],
1186 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1187 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1188 EXPECT_PRED5(CheckCandidate, candidates_[2],
1189 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1190 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1191 // Not using shared socket, so the STUN request's server reflexive address
1192 // should be different than the TURN request's server reflexive address.
1193 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1194
1195 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1196 EXPECT_EQ(3U, candidates_.size());
1197 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1198 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1199 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1200}
1201
1202// Test that even when both a STUN and TURN server are configured, the TURN
1203// server is used as a STUN server and we get a 'stun' candidate.
1204TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1205 AddInterface(kClientAddr);
1206 // Configure with STUN server but destroy it, so we can ensure that it's
1207 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001208 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001209 stun_server_.reset();
1210 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1211
1212 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001213 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1214 cricket::PORTALLOCATOR_DISABLE_TCP);
1215
1216 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1217 session_->StartGettingPorts();
1218
1219 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1220 EXPECT_PRED5(CheckCandidate, candidates_[0],
1221 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1222 kClientAddr);
1223 EXPECT_PRED5(CheckCandidate, candidates_[1],
1224 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1225 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1226 EXPECT_PRED5(CheckCandidate, candidates_[2],
1227 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1228 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1229 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1230
1231 // Don't bother waiting for STUN timeout, since we already verified
1232 // that we got a STUN candidate from the TURN server.
1233}
1234
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001235// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1236// and fail to generate STUN candidate, local UDP candidate is generated
1237// properly.
1238TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1239 allocator().set_flags(allocator().flags() |
1240 cricket::PORTALLOCATOR_DISABLE_RELAY |
1241 cricket::PORTALLOCATOR_DISABLE_TCP |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001242 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1243 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1244 AddInterface(kClientAddr);
1245 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1246 session_->StartGettingPorts();
1247 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1248 EXPECT_EQ(1U, candidates_.size());
1249 EXPECT_PRED5(CheckCandidate, candidates_[0],
1250 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1251 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1252 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1253 EXPECT_EQ(1U, candidates_.size());
1254}
1255
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001256// Test that when the NetworkManager doesn't have permission to enumerate
1257// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1258// automatically.
1259TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001260 network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1261 rtc::IPAddress());
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001262 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001263 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001264 allocator().set_flags(allocator().flags() |
1265 cricket::PORTALLOCATOR_DISABLE_RELAY |
1266 cricket::PORTALLOCATOR_DISABLE_TCP |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001267 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001268 EXPECT_EQ(0U, allocator_->flags() &
1269 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001270 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001271 EXPECT_EQ(0U, session_->flags() &
1272 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001273 session_->StartGettingPorts();
1274 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -08001275 EXPECT_EQ(1U, candidates_.size());
1276 EXPECT_PRED5(CheckCandidate, candidates_[0],
1277 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1278 kPrivateAddr);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001279 EXPECT_TRUE((session_->flags() &
1280 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1281}
1282
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001283// This test verifies allocator can use IPv6 addresses along with IPv4.
1284TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1285 allocator().set_flags(allocator().flags() |
1286 cricket::PORTALLOCATOR_DISABLE_RELAY |
1287 cricket::PORTALLOCATOR_ENABLE_IPV6 |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001288 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1289 AddInterface(kClientIPv6Addr);
1290 AddInterface(kClientAddr);
1291 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1292 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1293 session_->StartGettingPorts();
1294 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1295 EXPECT_EQ(4U, candidates_.size());
1296 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1297 EXPECT_PRED5(CheckCandidate, candidates_[0],
1298 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1299 kClientIPv6Addr);
1300 EXPECT_PRED5(CheckCandidate, candidates_[1],
1301 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1302 kClientAddr);
1303 EXPECT_PRED5(CheckCandidate, candidates_[2],
1304 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1305 kClientIPv6Addr);
1306 EXPECT_PRED5(CheckCandidate, candidates_[3],
1307 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1308 kClientAddr);
1309 EXPECT_EQ(4U, candidates_.size());
1310}
honghaiz98db68f2015-09-29 07:58:17 -07001311
1312TEST_F(PortAllocatorTest, TestStopGettingPorts) {
1313 AddInterface(kClientAddr);
1314 allocator_->set_step_delay(cricket::kDefaultStepDelay);
1315 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1316 session_->StartGettingPorts();
1317 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1318 EXPECT_EQ(2U, ports_.size());
1319 session_->StopGettingPorts();
1320 EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1321
1322 // After stopping getting ports, adding a new interface will not start
1323 // getting ports again.
1324 candidates_.clear();
1325 ports_.clear();
1326 candidate_allocation_done_ = false;
1327 network_manager_.AddInterface(kClientAddr2);
1328 rtc::Thread::Current()->ProcessMessages(1000);
1329 EXPECT_EQ(0U, candidates_.size());
1330 EXPECT_EQ(0U, ports_.size());
1331}
1332
1333TEST_F(PortAllocatorTest, TestClearGettingPorts) {
1334 AddInterface(kClientAddr);
1335 allocator_->set_step_delay(cricket::kDefaultStepDelay);
1336 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1337 session_->StartGettingPorts();
1338 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1339 EXPECT_EQ(2U, ports_.size());
1340 session_->ClearGettingPorts();
1341 WAIT(candidate_allocation_done_, 1000);
1342 EXPECT_FALSE(candidate_allocation_done_);
1343
1344 // After clearing getting ports, adding a new interface will start getting
1345 // ports again.
1346 candidates_.clear();
1347 ports_.clear();
1348 candidate_allocation_done_ = false;
1349 network_manager_.AddInterface(kClientAddr2);
1350 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1351 EXPECT_EQ(2U, ports_.size());
1352}