blob: 6940ea17ad2272b5265260f2137f480da1028ef3 [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"
23#include "webrtc/base/logging.h"
24#include "webrtc/base/natserver.h"
25#include "webrtc/base/natsocketfactory.h"
26#include "webrtc/base/network.h"
27#include "webrtc/base/physicalsocketserver.h"
28#include "webrtc/base/socketaddress.h"
29#include "webrtc/base/ssladapter.h"
30#include "webrtc/base/thread.h"
31#include "webrtc/base/virtualsocketserver.h"
32
33using cricket::ServerAddresses;
34using rtc::SocketAddress;
35using rtc::Thread;
36
37static const SocketAddress kClientAddr("11.11.11.11", 0);
38static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000039static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040static const SocketAddress kClientIPv6Addr(
41 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
42static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070043static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
44static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
46static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
47static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
48static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
49static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
50static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
51static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
52static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
53static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
54static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
55static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
56
57// Minimum and maximum port for port range tests.
58static const int kMinPort = 10000;
59static const int kMaxPort = 10099;
60
61// Based on ICE_UFRAG_LENGTH
62static const char kIceUfrag0[] = "TESTICEUFRAG0000";
63// Based on ICE_PWD_LENGTH
64static const char kIcePwd0[] = "TESTICEPWD00000000000000";
65
66static const char kContentName[] = "test content";
67
68static const int kDefaultAllocationTimeout = 1000;
69static const char kTurnUsername[] = "test";
70static const char kTurnPassword[] = "test";
71
72namespace cricket {
73
74// Helper for dumping candidates
75std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
76 os << c.ToString();
77 return os;
78}
79
80} // namespace cricket
81
82class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
83 public:
84 PortAllocatorTest()
85 : pss_(new rtc::PhysicalSocketServer),
86 vss_(new rtc::VirtualSocketServer(pss_.get())),
87 fss_(new rtc::FirewallSocketServer(vss_.get())),
88 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070089 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090 nat_socket_factory_(&nat_factory_),
91 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
92 kStunAddr)),
93 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
94 kRelayTcpIntAddr, kRelayTcpExtAddr,
95 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
96 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
97 candidate_allocation_done_(false) {
98 cricket::ServerAddresses stun_servers;
99 stun_servers.insert(kStunAddr);
100 // Passing the addresses of GTURN servers will enable GTURN in
101 // Basicportallocator.
102 allocator_.reset(new cricket::BasicPortAllocator(
103 &network_manager_,
104 stun_servers,
105 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
106 allocator_->set_step_delay(cricket::kMinimumStepDelay);
107 }
108
109 void AddInterface(const SocketAddress& addr) {
110 network_manager_.AddInterface(addr);
111 }
112 bool SetPortRange(int min_port, int max_port) {
113 return allocator_->SetPortRange(min_port, max_port);
114 }
115 void ResetWithNatServer(const rtc::SocketAddress& stun_server) {
116 nat_server_.reset(new rtc::NATServer(
deadbeefc5d0d952015-07-16 10:22:21 -0700117 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
118 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 ServerAddresses stun_servers;
Jiayang Liud7e5c442015-04-27 11:47:21 -0700121 if (!stun_server.IsNil()) {
122 stun_servers.insert(stun_server);
123 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124 allocator_.reset(new cricket::BasicPortAllocator(
125 &network_manager_, &nat_socket_factory_, stun_servers));
126 allocator().set_step_delay(cricket::kMinimumStepDelay);
127 }
128
129 // Create a BasicPortAllocator without GTURN and add the TURN servers.
130 void ResetWithTurnServers(const rtc::SocketAddress& udp_turn,
131 const rtc::SocketAddress& tcp_turn) {
132 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
133 allocator().set_step_delay(cricket::kMinimumStepDelay);
134 AddTurnServers(udp_turn, tcp_turn);
135 }
136
137 void AddTurnServers(const rtc::SocketAddress& udp_turn,
138 const rtc::SocketAddress& tcp_turn) {
139 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
140 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
141 relay_server.credentials = credentials;
142
143 if (!udp_turn.IsNil()) {
144 relay_server.ports.push_back(cricket::ProtocolAddress(
145 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
146 }
147 if (!tcp_turn.IsNil()) {
148 relay_server.ports.push_back(cricket::ProtocolAddress(
149 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
150 }
151 allocator_->AddRelay(relay_server);
152 }
153
154 bool CreateSession(int component) {
155 session_.reset(CreateSession("session", component));
156 if (!session_)
157 return false;
158 return true;
159 }
160
161 bool CreateSession(int component, const std::string& content_name) {
162 session_.reset(CreateSession("session", content_name, component));
163 if (!session_)
164 return false;
165 return true;
166 }
167
168 cricket::PortAllocatorSession* CreateSession(
169 const std::string& sid, int component) {
170 return CreateSession(sid, kContentName, component);
171 }
172
173 cricket::PortAllocatorSession* CreateSession(
174 const std::string& sid, const std::string& content_name, int component) {
175 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
176 }
177
178 cricket::PortAllocatorSession* CreateSession(
179 const std::string& sid, const std::string& content_name, int component,
180 const std::string& ice_ufrag, const std::string& ice_pwd) {
181 cricket::PortAllocatorSession* session =
182 allocator_->CreateSession(
183 sid, content_name, component, ice_ufrag, ice_pwd);
184 session->SignalPortReady.connect(this,
185 &PortAllocatorTest::OnPortReady);
186 session->SignalCandidatesReady.connect(this,
187 &PortAllocatorTest::OnCandidatesReady);
188 session->SignalCandidatesAllocationDone.connect(this,
189 &PortAllocatorTest::OnCandidatesAllocationDone);
190 return session;
191 }
192
193 static bool CheckCandidate(const cricket::Candidate& c,
194 int component, const std::string& type,
195 const std::string& proto,
196 const SocketAddress& addr) {
197 return (c.component() == component && c.type() == type &&
198 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
199 ((addr.port() == 0 && (c.address().port() != 0)) ||
200 (c.address().port() == addr.port())));
201 }
202 static bool CheckPort(const rtc::SocketAddress& addr,
203 int min_port, int max_port) {
204 return (addr.port() >= min_port && addr.port() <= max_port);
205 }
206
207 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
208 // We should only get this callback once, except in the mux test where
209 // we have multiple port allocation sessions.
210 if (session == session_.get()) {
211 ASSERT_FALSE(candidate_allocation_done_);
212 candidate_allocation_done_ = true;
213 }
214 }
215
216 // Check if all ports allocated have send-buffer size |expected|. If
217 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
218 void CheckSendBufferSizesOfAllPorts(int expected) {
219 std::vector<cricket::PortInterface*>::iterator it;
220 for (it = ports_.begin(); it < ports_.end(); ++it) {
221 int send_buffer_size;
222 if (expected == -1) {
223 EXPECT_EQ(SOCKET_ERROR,
224 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
225 &send_buffer_size));
226 } else {
227 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
228 &send_buffer_size));
229 ASSERT_EQ(expected, send_buffer_size);
230 }
231 }
232 }
233
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000234 void CheckDisableAdapterEnumeration() {
235 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
236 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
237 session_->StartGettingPorts();
238 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
239
240 // Only 2 candidates as local UDP/TCP are all 0s and get trimmed out.
241 EXPECT_EQ(2U, candidates_.size());
242 EXPECT_EQ(2U, ports_.size()); // One stunport and one turnport.
243
244 EXPECT_PRED5(CheckCandidate, candidates_[0],
245 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700246 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000247 EXPECT_EQ(
248 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()),
249 candidates_[0].related_address());
250
251 EXPECT_PRED5(CheckCandidate, candidates_[1],
252 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
253 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
deadbeefc5d0d952015-07-16 10:22:21 -0700254 EXPECT_EQ(kNatUdpAddr.ipaddr(), candidates_[1].related_address().ipaddr());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000255 }
256
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000257 protected:
258 cricket::BasicPortAllocator& allocator() {
259 return *allocator_;
260 }
261
262 void OnPortReady(cricket::PortAllocatorSession* ses,
263 cricket::PortInterface* port) {
264 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
265 ports_.push_back(port);
266 }
267 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
268 const std::vector<cricket::Candidate>& candidates) {
269 for (size_t i = 0; i < candidates.size(); ++i) {
270 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
271 candidates_.push_back(candidates[i]);
272 }
273 }
274
275 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
276 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
277 cricket::RelayServerConfig server_config = allocator_->relays()[i];
278 cricket::PortList::const_iterator relay_port;
279 for (relay_port = server_config.ports.begin();
280 relay_port != server_config.ports.end(); ++relay_port) {
281 if (proto_addr.address == relay_port->address &&
282 proto_addr.proto == relay_port->proto)
283 return true;
284 }
285 }
286 return false;
287 }
288
289 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
290 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
291 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
292 rtc::SocketServerScope ss_scope_;
293 rtc::scoped_ptr<rtc::NATServer> nat_server_;
294 rtc::NATSocketFactory nat_factory_;
295 rtc::BasicPacketSocketFactory nat_socket_factory_;
296 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
297 cricket::TestRelayServer relay_server_;
298 cricket::TestTurnServer turn_server_;
299 rtc::FakeNetworkManager network_manager_;
300 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
301 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
302 std::vector<cricket::PortInterface*> ports_;
303 std::vector<cricket::Candidate> candidates_;
304 bool candidate_allocation_done_;
305};
306
307// Tests that we can init the port allocator and create a session.
308TEST_F(PortAllocatorTest, TestBasic) {
309 EXPECT_EQ(&network_manager_, allocator().network_manager());
310 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
311 ASSERT_EQ(1u, allocator().relays().size());
312 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
313 // Empty relay credentials are used for GTURN.
314 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
315 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
316 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
317 kRelayUdpIntAddr, cricket::PROTO_UDP)));
318 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
319 kRelayTcpIntAddr, cricket::PROTO_TCP)));
320 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
321 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
322 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
323}
324
325// Tests that we allocator session not trying to allocate ports for every 250ms.
326TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
327 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
328 session_->StartGettingPorts();
329 // Waiting for one second to make sure BasicPortAllocatorSession has not
330 // called OnAllocate multiple times. In old behavior it's called every 250ms.
331 // When there are no network interfaces, each execution of OnAllocate will
332 // result in SignalCandidatesAllocationDone signal.
333 rtc::Thread::Current()->ProcessMessages(1000);
334 EXPECT_TRUE(candidate_allocation_done_);
335 EXPECT_EQ(0U, candidates_.size());
336}
337
338// Tests that we can get all the desired addresses successfully.
339TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
340 AddInterface(kClientAddr);
341 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
342 session_->StartGettingPorts();
343 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
344 EXPECT_EQ(4U, ports_.size());
345 EXPECT_PRED5(CheckCandidate, candidates_[0],
346 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
347 EXPECT_PRED5(CheckCandidate, candidates_[1],
348 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
349 EXPECT_PRED5(CheckCandidate, candidates_[2],
350 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
351 EXPECT_PRED5(CheckCandidate, candidates_[3],
352 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
353 EXPECT_PRED5(CheckCandidate, candidates_[4],
354 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
355 EXPECT_PRED5(CheckCandidate, candidates_[5],
356 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
357 EXPECT_PRED5(CheckCandidate, candidates_[6],
358 cricket::ICE_CANDIDATE_COMPONENT_RTP,
359 "relay", "ssltcp", kRelaySslTcpIntAddr);
360 EXPECT_TRUE(candidate_allocation_done_);
361}
362
363// Verify candidates with default step delay of 1sec.
364TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
365 AddInterface(kClientAddr);
366 allocator_->set_step_delay(cricket::kDefaultStepDelay);
367 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
368 session_->StartGettingPorts();
369 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
370 EXPECT_EQ(2U, ports_.size());
371 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
372 EXPECT_EQ(3U, ports_.size());
373 EXPECT_PRED5(CheckCandidate, candidates_[2],
374 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
375 EXPECT_PRED5(CheckCandidate, candidates_[3],
376 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
377 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
378 EXPECT_PRED5(CheckCandidate, candidates_[4],
379 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
380 EXPECT_PRED5(CheckCandidate, candidates_[5],
381 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
382 EXPECT_EQ(4U, ports_.size());
383 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
384 EXPECT_PRED5(CheckCandidate, candidates_[6],
385 cricket::ICE_CANDIDATE_COMPONENT_RTP,
386 "relay", "ssltcp", kRelaySslTcpIntAddr);
387 EXPECT_EQ(4U, ports_.size());
388 EXPECT_TRUE(candidate_allocation_done_);
389 // If we Stop gathering now, we shouldn't get a second "done" callback.
390 session_->StopGettingPorts();
391}
392
393TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
394 AddInterface(kClientAddr);
395 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
396 cricket::CN_VIDEO));
397 session_->StartGettingPorts();
398 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
399 EXPECT_TRUE(candidate_allocation_done_);
400 // If we Stop gathering now, we shouldn't get a second "done" callback.
401 session_->StopGettingPorts();
402
403 // All ports should have unset send-buffer sizes.
404 CheckSendBufferSizesOfAllPorts(-1);
405}
406
407// Tests that we can get callback after StopGetAllPorts.
408TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
409 AddInterface(kClientAddr);
410 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
411 session_->StartGettingPorts();
412 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
413 EXPECT_EQ(2U, ports_.size());
414 session_->StopGettingPorts();
415 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
416}
417
418// Test that we restrict client ports appropriately when a port range is set.
419// We check the candidates for udp/stun/tcp ports, and the from address
420// for relay ports.
421TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
422 AddInterface(kClientAddr);
423 // Check that an invalid port range fails.
424 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
425 // Check that a null port range succeeds.
426 EXPECT_TRUE(SetPortRange(0, 0));
427 // Check that a valid port range succeeds.
428 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
429 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
430 session_->StartGettingPorts();
431 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
432 EXPECT_EQ(4U, ports_.size());
433 // Check the port number for the UDP port object.
434 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
435 // Check the port number for the STUN port object.
436 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
437 // Check the port number used to connect to the relay server.
438 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
439 kMinPort, kMaxPort);
440 // Check the port number for the TCP port object.
441 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
442 EXPECT_TRUE(candidate_allocation_done_);
443}
444
445// Test that we don't crash or malfunction if we have no network adapters.
446TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
447 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
448 session_->StartGettingPorts();
449 rtc::Thread::Current()->ProcessMessages(100);
450 // Without network adapter, we should not get any candidate.
451 EXPECT_EQ(0U, candidates_.size());
452 EXPECT_TRUE(candidate_allocation_done_);
453}
454
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000455// Test that we should only get STUN and TURN candidates when adapter
456// enumeration is disabled.
457TEST_F(PortAllocatorTest, TestDisableAdapterEnumeration) {
458 AddInterface(kClientAddr);
459 // GTURN is not configured here.
460 ResetWithNatServer(kStunAddr);
461 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
462
463 CheckDisableAdapterEnumeration();
464}
465
466// Test that even with multiple interfaces, the result should be only 1 Stun
467// candidate since we bind to any address (i.e. all 0s).
468TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationMultipleInterfaces) {
469 AddInterface(kPrivateAddr);
470 AddInterface(kPrivateAddr2);
471 ResetWithNatServer(kStunAddr);
472 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
473
474 CheckDisableAdapterEnumeration();
475}
476
Erik Språngefdce692015-06-05 09:41:26 +0200477// Disable for asan, see
478// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
479#if !defined(ADDRESS_SANITIZER)
480
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000481// Test that we can get OnCandidatesAllocationDone callback when all the ports
482// are disabled.
483TEST_F(PortAllocatorTest, TestDisableAllPorts) {
484 AddInterface(kClientAddr);
485 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
486 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
487 cricket::PORTALLOCATOR_DISABLE_STUN |
488 cricket::PORTALLOCATOR_DISABLE_RELAY |
489 cricket::PORTALLOCATOR_DISABLE_TCP);
490 session_->StartGettingPorts();
491 rtc::Thread::Current()->ProcessMessages(100);
492 EXPECT_EQ(0U, candidates_.size());
493 EXPECT_TRUE(candidate_allocation_done_);
494}
495
496// Test that we don't crash or malfunction if we can't create UDP sockets.
497TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
498 AddInterface(kClientAddr);
499 fss_->set_udp_sockets_enabled(false);
500 EXPECT_TRUE(CreateSession(1));
501 session_->StartGettingPorts();
502 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
503 EXPECT_EQ(2U, ports_.size());
504 EXPECT_PRED5(CheckCandidate, candidates_[0],
505 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
506 EXPECT_PRED5(CheckCandidate, candidates_[1],
507 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
508 EXPECT_PRED5(CheckCandidate, candidates_[2],
509 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
510 EXPECT_PRED5(CheckCandidate, candidates_[3],
511 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
512 EXPECT_PRED5(CheckCandidate, candidates_[4],
513 cricket::ICE_CANDIDATE_COMPONENT_RTP,
514 "relay", "ssltcp", kRelaySslTcpIntAddr);
515 EXPECT_TRUE(candidate_allocation_done_);
516}
517
Erik Språngefdce692015-06-05 09:41:26 +0200518#endif // if !defined(ADDRESS_SANITIZER)
519
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000520// Test that we don't crash or malfunction if we can't create UDP sockets or
521// listen on TCP sockets. We still give out a local TCP address, since
522// apparently this is needed for the remote side to accept our connection.
523TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
524 AddInterface(kClientAddr);
525 fss_->set_udp_sockets_enabled(false);
526 fss_->set_tcp_listen_enabled(false);
527 EXPECT_TRUE(CreateSession(1));
528 session_->StartGettingPorts();
529 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
530 EXPECT_EQ(2U, ports_.size());
531 EXPECT_PRED5(CheckCandidate, candidates_[0],
532 1, "relay", "udp", kRelayUdpIntAddr);
533 EXPECT_PRED5(CheckCandidate, candidates_[1],
534 1, "relay", "udp", kRelayUdpExtAddr);
535 EXPECT_PRED5(CheckCandidate, candidates_[2],
536 1, "relay", "tcp", kRelayTcpIntAddr);
537 EXPECT_PRED5(CheckCandidate, candidates_[3],
538 1, "local", "tcp", kClientAddr);
539 EXPECT_PRED5(CheckCandidate, candidates_[4],
540 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
541 EXPECT_TRUE(candidate_allocation_done_);
542}
543
544// Test that we don't crash or malfunction if we can't create any sockets.
545// TODO: Find a way to exit early here.
546TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
547 AddInterface(kClientAddr);
548 fss_->set_tcp_sockets_enabled(false);
549 fss_->set_udp_sockets_enabled(false);
550 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
551 session_->StartGettingPorts();
552 WAIT(candidates_.size() > 0, 2000);
553 // TODO - Check candidate_allocation_done signal.
554 // In case of Relay, ports creation will succeed but sockets will fail.
555 // There is no error reporting from RelayEntry to handle this failure.
556}
557
558// Testing STUN timeout.
559TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
560 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
561 AddInterface(kClientAddr);
562 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
563 session_->StartGettingPorts();
564 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
565 EXPECT_EQ(2U, ports_.size());
566 EXPECT_PRED5(CheckCandidate, candidates_[0],
567 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
568 EXPECT_PRED5(CheckCandidate, candidates_[1],
569 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
570 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
571 // will be tried after 3 seconds.
572 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
573 EXPECT_EQ(3U, ports_.size());
574 EXPECT_PRED5(CheckCandidate, candidates_[2],
575 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
576 EXPECT_PRED5(CheckCandidate, candidates_[3],
577 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
578 EXPECT_PRED5(CheckCandidate, candidates_[4],
579 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
580 kRelaySslTcpIntAddr);
581 EXPECT_PRED5(CheckCandidate, candidates_[5],
582 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
583 // Stun Timeout is 9sec.
584 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
585}
586
587TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
588 AddInterface(kClientAddr);
589 AddInterface(kClientAddr2);
590 // Allocating only host UDP ports. This is done purely for testing
591 // convenience.
592 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
593 cricket::PORTALLOCATOR_DISABLE_STUN |
594 cricket::PORTALLOCATOR_DISABLE_RELAY);
595 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
596 session_->StartGettingPorts();
597 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
598 ASSERT_EQ(2U, candidates_.size());
599 EXPECT_EQ(2U, ports_.size());
600 // Candidates priorities should be different.
601 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
602}
603
604// Test to verify ICE restart process.
605TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
606 AddInterface(kClientAddr);
607 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
608 session_->StartGettingPorts();
609 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
610 EXPECT_EQ(4U, ports_.size());
611 EXPECT_TRUE(candidate_allocation_done_);
612 // TODO - Extend this to verify ICE restart.
613}
614
615// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
616// This test also verifies that when the allocator is only allowed to use
617// relay (i.e. IceTransportsType is relay), the raddr is an empty
618// address with the correct family. This is to prevent any local
619// reflective address leakage in the sdp line.
620TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
621 AddInterface(kClientAddr);
622 // GTURN is not configured here.
623 ResetWithTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
624 allocator().set_candidate_filter(cricket::CF_RELAY);
625 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
626 session_->StartGettingPorts();
627 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
628 EXPECT_PRED5(CheckCandidate,
629 candidates_[0],
630 cricket::ICE_CANDIDATE_COMPONENT_RTP,
631 "relay",
632 "udp",
633 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
634
635 EXPECT_EQ(1U, candidates_.size());
636 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
637 for (size_t i = 0; i < candidates_.size(); ++i) {
638 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
639 EXPECT_EQ(
640 candidates_[0].related_address(),
641 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
642 }
643}
644
645TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
646 AddInterface(kClientAddr);
647 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
648 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
649 allocator().set_candidate_filter(cricket::CF_HOST);
650 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
651 session_->StartGettingPorts();
652 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
653 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
654 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
655 for (size_t i = 0; i < candidates_.size(); ++i) {
656 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
657 }
658}
659
660// Host is behind the NAT.
661TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
662 AddInterface(kPrivateAddr);
663 ResetWithNatServer(kStunAddr);
664
665 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
666 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
667 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
668 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
669 session_->StartGettingPorts();
670 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
671 // Host is behind NAT, no private address will be exposed. Hence only UDP
672 // port with STUN candidate will be sent outside.
673 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
674 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
675 for (size_t i = 0; i < candidates_.size(); ++i) {
676 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
677 EXPECT_EQ(
678 candidates_[0].related_address(),
679 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
680 }
681}
682
683// Host is not behind the NAT.
684TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
685 AddInterface(kClientAddr);
686 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
687 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
688 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
689 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
690 session_->StartGettingPorts();
691 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
692 // Host has a public address, both UDP and TCP candidates will be exposed.
693 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
694 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
695 for (size_t i = 0; i < candidates_.size(); ++i) {
696 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
697 }
698}
699
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000700// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
701// ufrag and pwd for the collected candidates.
702TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
703 allocator().set_flags(allocator().flags() |
704 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
705 AddInterface(kClientAddr);
706 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
707 session_->StartGettingPorts();
708 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
709 EXPECT_PRED5(CheckCandidate, candidates_[0],
710 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
711 EXPECT_PRED5(CheckCandidate, candidates_[1],
712 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
713 EXPECT_PRED5(CheckCandidate, candidates_[5],
714 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
715 EXPECT_EQ(4U, ports_.size());
716 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
717 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
718 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
719 EXPECT_EQ(kIcePwd0, candidates_[0].password());
720 EXPECT_EQ(kIcePwd0, candidates_[1].password());
721 EXPECT_TRUE(candidate_allocation_done_);
722}
723
724// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
725// different ufrag and pwd for the collected candidates.
726TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
727 allocator().set_flags(allocator().flags() &
728 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
729 AddInterface(kClientAddr);
730 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
731 session_->StartGettingPorts();
732 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
733 EXPECT_PRED5(CheckCandidate, candidates_[0],
734 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
735 EXPECT_PRED5(CheckCandidate, candidates_[1],
736 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
737 EXPECT_EQ(4U, ports_.size());
738 // Port should generate random ufrag and pwd.
739 EXPECT_NE(kIceUfrag0, candidates_[0].username());
740 EXPECT_NE(kIceUfrag0, candidates_[1].username());
741 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
742 EXPECT_NE(kIcePwd0, candidates_[0].password());
743 EXPECT_NE(kIcePwd0, candidates_[1].password());
744 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
745 EXPECT_TRUE(candidate_allocation_done_);
746}
747
748// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
749// is allocated for udp and stun. Also verify there is only one candidate
750// (local) if stun candidate is same as local candidate, which will be the case
751// in a public network like the below test.
752TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
753 AddInterface(kClientAddr);
754 allocator_->set_flags(allocator().flags() |
755 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
756 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
757 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
758 session_->StartGettingPorts();
759 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
760 EXPECT_EQ(3U, ports_.size());
761 EXPECT_PRED5(CheckCandidate, candidates_[0],
762 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
763 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
764}
765
766// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
767// is allocated for udp and stun. In this test we should expect both stun and
768// local candidates as client behind a nat.
769TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
770 AddInterface(kClientAddr);
771 ResetWithNatServer(kStunAddr);
772
773 allocator_->set_flags(allocator().flags() |
774 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
775 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
776 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
777 session_->StartGettingPorts();
778 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
779 ASSERT_EQ(2U, ports_.size());
780 EXPECT_PRED5(CheckCandidate, candidates_[0],
781 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
782 EXPECT_PRED5(CheckCandidate, candidates_[1],
783 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700784 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000785 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
786 EXPECT_EQ(3U, candidates_.size());
787}
788
deadbeefc5d0d952015-07-16 10:22:21 -0700789// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000790TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
791 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
792 AddInterface(kClientAddr);
793 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
794
795 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
796
797 allocator_->set_step_delay(cricket::kMinimumStepDelay);
798 allocator_->set_flags(allocator().flags() |
799 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
800 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
801 cricket::PORTALLOCATOR_DISABLE_TCP);
802
803 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
804 session_->StartGettingPorts();
805
806 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
807 ASSERT_EQ(3U, ports_.size());
808 EXPECT_PRED5(CheckCandidate, candidates_[0],
809 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
810 EXPECT_PRED5(CheckCandidate, candidates_[1],
811 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
812 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
813 EXPECT_PRED5(CheckCandidate, candidates_[2],
814 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
815 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
816 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
817 EXPECT_EQ(3U, candidates_.size());
818}
819
820// Testing DNS resolve for the TURN server, this will test AllocationSequence
821// handling the unresolved address signal from TurnPort.
822TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
823 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
824 cricket::PROTO_UDP);
825 AddInterface(kClientAddr);
826 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
827 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
828 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
829 relay_server.credentials = credentials;
830 relay_server.ports.push_back(cricket::ProtocolAddress(
831 rtc::SocketAddress("localhost", 3478),
832 cricket::PROTO_UDP, false));
833 allocator_->AddRelay(relay_server);
834
835 allocator_->set_step_delay(cricket::kMinimumStepDelay);
836 allocator_->set_flags(allocator().flags() |
837 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
838 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
839 cricket::PORTALLOCATOR_DISABLE_TCP);
840
841 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
842 session_->StartGettingPorts();
843
844 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
845}
846
847// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
848// is allocated for udp/stun/turn. In this test we should expect all local,
849// stun and turn candidates.
850TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
851 AddInterface(kClientAddr);
852 ResetWithNatServer(kStunAddr);
853
854 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
855
856 allocator_->set_flags(allocator().flags() |
857 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
858 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
859 cricket::PORTALLOCATOR_DISABLE_TCP);
860
861 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
862 session_->StartGettingPorts();
863
864 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
865 ASSERT_EQ(2U, ports_.size());
866 EXPECT_PRED5(CheckCandidate, candidates_[0],
867 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
868 EXPECT_PRED5(CheckCandidate, candidates_[1],
869 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700870 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000871 EXPECT_PRED5(CheckCandidate, candidates_[2],
872 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
873 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
874 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
875 EXPECT_EQ(3U, candidates_.size());
876 // Local port will be created first and then TURN port.
877 EXPECT_EQ(2U, ports_[0]->Candidates().size());
878 EXPECT_EQ(1U, ports_[1]->Candidates().size());
879}
880
881// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
882// server is also used as the STUN server, we should get 'local', 'stun', and
883// 'relay' candidates.
884TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
885 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -0700886 // Use an empty SocketAddress to add a NAT without STUN server.
887 ResetWithNatServer(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000888 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
889
890 // Must set the step delay to 0 to make sure the relay allocation phase is
891 // started before the STUN candidates are obtained, so that the STUN binding
892 // response is processed when both StunPort and TurnPort exist to reproduce
893 // webrtc issue 3537.
894 allocator_->set_step_delay(0);
895 allocator_->set_flags(allocator().flags() |
896 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
897 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
898 cricket::PORTALLOCATOR_DISABLE_TCP);
899
900 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
901 session_->StartGettingPorts();
902
903 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
904 EXPECT_PRED5(CheckCandidate, candidates_[0],
905 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
906 EXPECT_PRED5(CheckCandidate, candidates_[1],
907 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700908 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000909 EXPECT_PRED5(CheckCandidate, candidates_[2],
910 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
911 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
912 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
913
914 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
915 EXPECT_EQ(3U, candidates_.size());
916 // Local port will be created first and then TURN port.
917 EXPECT_EQ(2U, ports_[0]->Candidates().size());
918 EXPECT_EQ(1U, ports_[1]->Candidates().size());
919}
920
deadbeefc5d0d952015-07-16 10:22:21 -0700921// Test that when only a TCP TURN server is available, we do NOT use it as
922// a UDP STUN server, as this could leak our IP address. Thus we should only
923// expect two ports, a UDPPort and TurnPort.
924TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
925 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
926 AddInterface(kClientAddr);
927 ResetWithNatServer(rtc::SocketAddress());
928 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
929
930 allocator_->set_flags(allocator().flags() |
931 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
932 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
933 cricket::PORTALLOCATOR_DISABLE_TCP);
934
935 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
936 session_->StartGettingPorts();
937
938 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
939 ASSERT_EQ(2U, ports_.size());
940 EXPECT_PRED5(CheckCandidate, candidates_[0],
941 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
942 kClientAddr);
943 EXPECT_PRED5(CheckCandidate, candidates_[1],
944 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
945 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
946 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
947 EXPECT_EQ(2U, candidates_.size());
948 EXPECT_EQ(1U, ports_[0]->Candidates().size());
949 EXPECT_EQ(1U, ports_[1]->Candidates().size());
950}
951
952// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
953// TURN server is used as the STUN server and we get 'local', 'stun', and
954// 'relay' candidates.
955// TODO(deadbeef): Remove this test when support for non-shared socket mode
956// is removed.
957TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
958 AddInterface(kClientAddr);
959 // Use an empty SocketAddress to add a NAT without STUN server.
960 ResetWithNatServer(SocketAddress());
961 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
962
963 allocator_->set_flags(allocator().flags() |
964 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
965 cricket::PORTALLOCATOR_DISABLE_TCP);
966
967 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
968 session_->StartGettingPorts();
969
970 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
971 ASSERT_EQ(3U, ports_.size());
972 EXPECT_PRED5(CheckCandidate, candidates_[0],
973 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
974 kClientAddr);
975 EXPECT_PRED5(CheckCandidate, candidates_[1],
976 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
977 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
978 EXPECT_PRED5(CheckCandidate, candidates_[2],
979 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
980 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
981 // Not using shared socket, so the STUN request's server reflexive address
982 // should be different than the TURN request's server reflexive address.
983 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
984
985 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
986 EXPECT_EQ(3U, candidates_.size());
987 EXPECT_EQ(1U, ports_[0]->Candidates().size());
988 EXPECT_EQ(1U, ports_[1]->Candidates().size());
989 EXPECT_EQ(1U, ports_[2]->Candidates().size());
990}
991
992// Test that even when both a STUN and TURN server are configured, the TURN
993// server is used as a STUN server and we get a 'stun' candidate.
994TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
995 AddInterface(kClientAddr);
996 // Configure with STUN server but destroy it, so we can ensure that it's
997 // the TURN server actually being used as a STUN server.
998 ResetWithNatServer(kStunAddr);
999 stun_server_.reset();
1000 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1001
1002 allocator_->set_flags(allocator().flags() |
1003 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
1004 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1005 cricket::PORTALLOCATOR_DISABLE_TCP);
1006
1007 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1008 session_->StartGettingPorts();
1009
1010 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1011 EXPECT_PRED5(CheckCandidate, candidates_[0],
1012 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1013 kClientAddr);
1014 EXPECT_PRED5(CheckCandidate, candidates_[1],
1015 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1016 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1017 EXPECT_PRED5(CheckCandidate, candidates_[2],
1018 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1019 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1020 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1021
1022 // Don't bother waiting for STUN timeout, since we already verified
1023 // that we got a STUN candidate from the TURN server.
1024}
1025
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001026// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1027// and fail to generate STUN candidate, local UDP candidate is generated
1028// properly.
1029TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1030 allocator().set_flags(allocator().flags() |
1031 cricket::PORTALLOCATOR_DISABLE_RELAY |
1032 cricket::PORTALLOCATOR_DISABLE_TCP |
1033 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
1034 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1035 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1036 AddInterface(kClientAddr);
1037 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1038 session_->StartGettingPorts();
1039 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1040 EXPECT_EQ(1U, candidates_.size());
1041 EXPECT_PRED5(CheckCandidate, candidates_[0],
1042 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1043 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1044 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1045 EXPECT_EQ(1U, candidates_.size());
1046}
1047
1048// This test verifies allocator can use IPv6 addresses along with IPv4.
1049TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1050 allocator().set_flags(allocator().flags() |
1051 cricket::PORTALLOCATOR_DISABLE_RELAY |
1052 cricket::PORTALLOCATOR_ENABLE_IPV6 |
1053 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
1054 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1055 AddInterface(kClientIPv6Addr);
1056 AddInterface(kClientAddr);
1057 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1058 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1059 session_->StartGettingPorts();
1060 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1061 EXPECT_EQ(4U, candidates_.size());
1062 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1063 EXPECT_PRED5(CheckCandidate, candidates_[0],
1064 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1065 kClientIPv6Addr);
1066 EXPECT_PRED5(CheckCandidate, candidates_[1],
1067 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1068 kClientAddr);
1069 EXPECT_PRED5(CheckCandidate, candidates_[2],
1070 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1071 kClientIPv6Addr);
1072 EXPECT_PRED5(CheckCandidate, candidates_[3],
1073 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1074 kClientAddr);
1075 EXPECT_EQ(4U, candidates_.size());
1076}
1077
1078// Test that the httpportallocator correctly maintains its lists of stun and
1079// relay servers, by never allowing an empty list.
1080TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1081 rtc::FakeNetworkManager network_manager;
1082 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1083 EXPECT_EQ(1U, alloc.relay_hosts().size());
1084 EXPECT_EQ(1U, alloc.stun_hosts().size());
1085
1086 std::vector<std::string> relay_servers;
1087 std::vector<rtc::SocketAddress> stun_servers;
1088
1089 alloc.SetRelayHosts(relay_servers);
1090 alloc.SetStunHosts(stun_servers);
1091 EXPECT_EQ(1U, alloc.relay_hosts().size());
1092 EXPECT_EQ(1U, alloc.stun_hosts().size());
1093
1094 relay_servers.push_back("1.unittest.corp.google.com");
1095 relay_servers.push_back("2.unittest.corp.google.com");
1096 stun_servers.push_back(
1097 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1098 stun_servers.push_back(
1099 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1100
1101 alloc.SetRelayHosts(relay_servers);
1102 alloc.SetStunHosts(stun_servers);
1103 EXPECT_EQ(2U, alloc.relay_hosts().size());
1104 EXPECT_EQ(2U, alloc.stun_hosts().size());
1105}
1106
1107// Test that the HttpPortAllocator uses correct URL to create sessions.
1108TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1109 rtc::FakeNetworkManager network_manager;
1110 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1111
1112 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1113 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1114 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1115 static_cast<cricket::HttpPortAllocatorSession*>(
1116 alloc.CreateSessionInternal(
1117 "test content", 0, kIceUfrag0, kIcePwd0)));
1118 std::string url = session->GetSessionRequestUrl();
1119 LOG(LS_INFO) << "url: " << url;
1120 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
1121
1122 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1123 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1124 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
1125 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
1126 url = session->GetSessionRequestUrl();
1127 LOG(LS_INFO) << "url: " << url;
1128 std::vector<std::string> parts;
1129 rtc::split(url, '?', &parts);
1130 ASSERT_EQ(2U, parts.size());
1131
1132 std::vector<std::string> args_parts;
1133 rtc::split(parts[1], '&', &args_parts);
1134
1135 std::map<std::string, std::string> args;
1136 for (std::vector<std::string>::iterator it = args_parts.begin();
1137 it != args_parts.end(); ++it) {
1138 std::vector<std::string> parts;
1139 rtc::split(*it, '=', &parts);
1140 ASSERT_EQ(2U, parts.size());
1141 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1142 }
1143
1144 EXPECT_EQ(kIceUfrag0, args["username"]);
1145 EXPECT_EQ(kIcePwd0, args["password"]);
1146}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001147
1148// Tests that destroying ports with non-shared sockets does not crash.
1149// b/19074679.
1150TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1151 AddInterface(kClientAddr);
1152 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1153 session_->StartGettingPorts();
1154 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1155 EXPECT_EQ(4U, ports_.size());
1156
1157 auto it = ports_.begin();
1158 for (; it != ports_.end(); ++it) {
1159 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1160 }
1161}