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