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