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