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