blob: b5f635bd0f9bfbb5d137921cc571eca9bb3eda84 [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"
honghaizf421bdc2015-07-17 16:21:55 -070023#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#include "webrtc/base/logging.h"
25#include "webrtc/base/natserver.h"
26#include "webrtc/base/natsocketfactory.h"
27#include "webrtc/base/network.h"
28#include "webrtc/base/physicalsocketserver.h"
29#include "webrtc/base/socketaddress.h"
30#include "webrtc/base/ssladapter.h"
31#include "webrtc/base/thread.h"
32#include "webrtc/base/virtualsocketserver.h"
33
34using cricket::ServerAddresses;
35using rtc::SocketAddress;
36using rtc::Thread;
37
38static const SocketAddress kClientAddr("11.11.11.11", 0);
39static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000040static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041static const SocketAddress kClientIPv6Addr(
42 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
43static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070044static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
45static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
47static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
48static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
49static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
50static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
51static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
52static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
53static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
54static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
55static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
56static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
57
58// Minimum and maximum port for port range tests.
59static const int kMinPort = 10000;
60static const int kMaxPort = 10099;
61
62// Based on ICE_UFRAG_LENGTH
63static const char kIceUfrag0[] = "TESTICEUFRAG0000";
64// Based on ICE_PWD_LENGTH
65static const char kIcePwd0[] = "TESTICEPWD00000000000000";
66
67static const char kContentName[] = "test content";
68
69static const int kDefaultAllocationTimeout = 1000;
70static const char kTurnUsername[] = "test";
71static const char kTurnPassword[] = "test";
72
73namespace cricket {
74
75// Helper for dumping candidates
76std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
77 os << c.ToString();
78 return os;
79}
80
81} // namespace cricket
82
83class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
84 public:
85 PortAllocatorTest()
86 : pss_(new rtc::PhysicalSocketServer),
87 vss_(new rtc::VirtualSocketServer(pss_.get())),
88 fss_(new rtc::FirewallSocketServer(vss_.get())),
89 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070090 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -070091 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000092 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
93 kStunAddr)),
94 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
95 kRelayTcpIntAddr, kRelayTcpExtAddr,
96 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
97 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
98 candidate_allocation_done_(false) {
99 cricket::ServerAddresses stun_servers;
100 stun_servers.insert(kStunAddr);
101 // Passing the addresses of GTURN servers will enable GTURN in
102 // Basicportallocator.
103 allocator_.reset(new cricket::BasicPortAllocator(
104 &network_manager_,
105 stun_servers,
106 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
107 allocator_->set_step_delay(cricket::kMinimumStepDelay);
108 }
109
110 void AddInterface(const SocketAddress& addr) {
111 network_manager_.AddInterface(addr);
112 }
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700113 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
114 AddInterface(addr);
115 // When a binding comes from the any address, the |addr| will be used as the
116 // srflx address.
117 vss_->SetDefaultRoute(addr.ipaddr());
118 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 bool SetPortRange(int min_port, int max_port) {
120 return allocator_->SetPortRange(min_port, max_port);
121 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700122 // Endpoint is on the public network. No STUN or TURN.
123 void ResetWithNoServersOrNat() {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700124 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
125 allocator_->set_step_delay(cricket::kMinimumStepDelay);
126 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700127 // Endpoint is behind a NAT, with STUN specified.
128 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
129 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700130 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700131 // Endpoint is on the public network, with STUN specified.
132 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
133 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700135 // Endpoint is on the public network, with TURN specified.
136 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
137 const rtc::SocketAddress& tcp_turn) {
138 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 AddTurnServers(udp_turn, tcp_turn);
140 }
141
142 void AddTurnServers(const rtc::SocketAddress& udp_turn,
143 const rtc::SocketAddress& tcp_turn) {
144 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
145 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
146 relay_server.credentials = credentials;
147
148 if (!udp_turn.IsNil()) {
149 relay_server.ports.push_back(cricket::ProtocolAddress(
150 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
151 }
152 if (!tcp_turn.IsNil()) {
153 relay_server.ports.push_back(cricket::ProtocolAddress(
154 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
155 }
156 allocator_->AddRelay(relay_server);
157 }
158
159 bool CreateSession(int component) {
160 session_.reset(CreateSession("session", component));
161 if (!session_)
162 return false;
163 return true;
164 }
165
166 bool CreateSession(int component, const std::string& content_name) {
167 session_.reset(CreateSession("session", content_name, component));
168 if (!session_)
169 return false;
170 return true;
171 }
172
173 cricket::PortAllocatorSession* CreateSession(
174 const std::string& sid, int component) {
175 return CreateSession(sid, kContentName, component);
176 }
177
178 cricket::PortAllocatorSession* CreateSession(
179 const std::string& sid, const std::string& content_name, int component) {
180 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
181 }
182
183 cricket::PortAllocatorSession* CreateSession(
184 const std::string& sid, const std::string& content_name, int component,
185 const std::string& ice_ufrag, const std::string& ice_pwd) {
186 cricket::PortAllocatorSession* session =
187 allocator_->CreateSession(
188 sid, content_name, component, ice_ufrag, ice_pwd);
189 session->SignalPortReady.connect(this,
190 &PortAllocatorTest::OnPortReady);
191 session->SignalCandidatesReady.connect(this,
192 &PortAllocatorTest::OnCandidatesReady);
193 session->SignalCandidatesAllocationDone.connect(this,
194 &PortAllocatorTest::OnCandidatesAllocationDone);
195 return session;
196 }
197
198 static bool CheckCandidate(const cricket::Candidate& c,
199 int component, const std::string& type,
200 const std::string& proto,
201 const SocketAddress& addr) {
202 return (c.component() == component && c.type() == type &&
203 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
204 ((addr.port() == 0 && (c.address().port() != 0)) ||
205 (c.address().port() == addr.port())));
206 }
207 static bool CheckPort(const rtc::SocketAddress& addr,
208 int min_port, int max_port) {
209 return (addr.port() >= min_port && addr.port() <= max_port);
210 }
211
212 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
213 // We should only get this callback once, except in the mux test where
214 // we have multiple port allocation sessions.
215 if (session == session_.get()) {
216 ASSERT_FALSE(candidate_allocation_done_);
217 candidate_allocation_done_ = true;
218 }
219 }
220
221 // Check if all ports allocated have send-buffer size |expected|. If
222 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
223 void CheckSendBufferSizesOfAllPorts(int expected) {
224 std::vector<cricket::PortInterface*>::iterator it;
225 for (it = ports_.begin(); it < ports_.end(); ++it) {
226 int send_buffer_size;
227 if (expected == -1) {
228 EXPECT_EQ(SOCKET_ERROR,
229 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
230 &send_buffer_size));
231 } else {
232 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
233 &send_buffer_size));
234 ASSERT_EQ(expected, send_buffer_size);
235 }
236 }
237 }
238
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700239 // This function starts the port/address gathering and check the existence of
240 // candidates as specified. When |expect_stun_candidate| is true,
241 // |stun_candidate_addr| carries the expected reflective address, which is
242 // also the related address for TURN candidate if it is expected. Otherwise,
243 // it should be ignore.
244 void CheckDisableAdapterEnumeration(
245 uint32 total_ports,
246 const rtc::IPAddress& stun_candidate_addr,
247 const rtc::IPAddress& relay_candidate_udp_transport_addr,
248 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700249 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
250 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700251 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
252 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000253 session_->StartGettingPorts();
254 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
255
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700256 uint32 total_candidates = 0;
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700257 if (!stun_candidate_addr.IsNil()) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700258 ++total_candidates;
259 EXPECT_PRED5(CheckCandidate, candidates_[0],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700260 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
261 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700262 EXPECT_EQ(
263 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()),
264 candidates_[0].related_address());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700265 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700266 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700267 ++total_candidates;
268 EXPECT_PRED5(CheckCandidate, candidates_[1],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700269 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
270 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700271 EXPECT_EQ(stun_candidate_addr, candidates_[1].related_address().ipaddr());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700272 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700273 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700274 ++total_candidates;
275 EXPECT_PRED5(CheckCandidate, candidates_[2],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700276 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
277 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700278 EXPECT_EQ(stun_candidate_addr, candidates_[2].related_address().ipaddr());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700279 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000280
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700281 EXPECT_EQ(total_candidates, candidates_.size());
282 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000283 }
284
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000285 protected:
286 cricket::BasicPortAllocator& allocator() {
287 return *allocator_;
288 }
289
290 void OnPortReady(cricket::PortAllocatorSession* ses,
291 cricket::PortInterface* port) {
292 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
293 ports_.push_back(port);
294 }
295 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
296 const std::vector<cricket::Candidate>& candidates) {
297 for (size_t i = 0; i < candidates.size(); ++i) {
298 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
299 candidates_.push_back(candidates[i]);
300 }
301 }
302
303 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
304 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
305 cricket::RelayServerConfig server_config = allocator_->relays()[i];
306 cricket::PortList::const_iterator relay_port;
307 for (relay_port = server_config.ports.begin();
308 relay_port != server_config.ports.end(); ++relay_port) {
309 if (proto_addr.address == relay_port->address &&
310 proto_addr.proto == relay_port->proto)
311 return true;
312 }
313 }
314 return false;
315 }
316
Guo-wei Shieh11477022015-08-15 09:28:41 -0700317 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
318 bool with_nat) {
319 if (with_nat) {
320 nat_server_.reset(new rtc::NATServer(
321 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
322 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
323 } else {
324 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
325 }
326
327 ServerAddresses stun_servers;
328 if (!stun_server.IsNil()) {
329 stun_servers.insert(stun_server);
330 }
331 allocator_.reset(new cricket::BasicPortAllocator(
332 &network_manager_, nat_socket_factory_.get(), stun_servers));
333 allocator().set_step_delay(cricket::kMinimumStepDelay);
334 }
335
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
337 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
338 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
339 rtc::SocketServerScope ss_scope_;
340 rtc::scoped_ptr<rtc::NATServer> nat_server_;
341 rtc::NATSocketFactory nat_factory_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700342 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
344 cricket::TestRelayServer relay_server_;
345 cricket::TestTurnServer turn_server_;
346 rtc::FakeNetworkManager network_manager_;
347 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
348 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
349 std::vector<cricket::PortInterface*> ports_;
350 std::vector<cricket::Candidate> candidates_;
351 bool candidate_allocation_done_;
352};
353
354// Tests that we can init the port allocator and create a session.
355TEST_F(PortAllocatorTest, TestBasic) {
356 EXPECT_EQ(&network_manager_, allocator().network_manager());
357 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
358 ASSERT_EQ(1u, allocator().relays().size());
359 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
360 // Empty relay credentials are used for GTURN.
361 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
362 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
363 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
364 kRelayUdpIntAddr, cricket::PROTO_UDP)));
365 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
366 kRelayTcpIntAddr, cricket::PROTO_TCP)));
367 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
368 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
369 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
370}
371
372// Tests that we allocator session not trying to allocate ports for every 250ms.
373TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
374 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
375 session_->StartGettingPorts();
376 // Waiting for one second to make sure BasicPortAllocatorSession has not
377 // called OnAllocate multiple times. In old behavior it's called every 250ms.
378 // When there are no network interfaces, each execution of OnAllocate will
379 // result in SignalCandidatesAllocationDone signal.
380 rtc::Thread::Current()->ProcessMessages(1000);
381 EXPECT_TRUE(candidate_allocation_done_);
382 EXPECT_EQ(0U, candidates_.size());
383}
384
385// Tests that we can get all the desired addresses successfully.
386TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
387 AddInterface(kClientAddr);
388 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
389 session_->StartGettingPorts();
390 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
391 EXPECT_EQ(4U, ports_.size());
392 EXPECT_PRED5(CheckCandidate, candidates_[0],
393 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
394 EXPECT_PRED5(CheckCandidate, candidates_[1],
395 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
396 EXPECT_PRED5(CheckCandidate, candidates_[2],
397 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
398 EXPECT_PRED5(CheckCandidate, candidates_[3],
399 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
400 EXPECT_PRED5(CheckCandidate, candidates_[4],
401 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
402 EXPECT_PRED5(CheckCandidate, candidates_[5],
403 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
404 EXPECT_PRED5(CheckCandidate, candidates_[6],
405 cricket::ICE_CANDIDATE_COMPONENT_RTP,
406 "relay", "ssltcp", kRelaySslTcpIntAddr);
407 EXPECT_TRUE(candidate_allocation_done_);
408}
409
410// Verify candidates with default step delay of 1sec.
411TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
412 AddInterface(kClientAddr);
413 allocator_->set_step_delay(cricket::kDefaultStepDelay);
414 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
415 session_->StartGettingPorts();
416 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
417 EXPECT_EQ(2U, ports_.size());
418 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
419 EXPECT_EQ(3U, ports_.size());
420 EXPECT_PRED5(CheckCandidate, candidates_[2],
421 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
422 EXPECT_PRED5(CheckCandidate, candidates_[3],
423 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
424 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
425 EXPECT_PRED5(CheckCandidate, candidates_[4],
426 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
427 EXPECT_PRED5(CheckCandidate, candidates_[5],
428 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
429 EXPECT_EQ(4U, ports_.size());
430 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
431 EXPECT_PRED5(CheckCandidate, candidates_[6],
432 cricket::ICE_CANDIDATE_COMPONENT_RTP,
433 "relay", "ssltcp", kRelaySslTcpIntAddr);
434 EXPECT_EQ(4U, ports_.size());
435 EXPECT_TRUE(candidate_allocation_done_);
436 // If we Stop gathering now, we shouldn't get a second "done" callback.
437 session_->StopGettingPorts();
438}
439
440TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
441 AddInterface(kClientAddr);
442 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
443 cricket::CN_VIDEO));
444 session_->StartGettingPorts();
445 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
446 EXPECT_TRUE(candidate_allocation_done_);
447 // If we Stop gathering now, we shouldn't get a second "done" callback.
448 session_->StopGettingPorts();
449
450 // All ports should have unset send-buffer sizes.
451 CheckSendBufferSizesOfAllPorts(-1);
452}
453
454// Tests that we can get callback after StopGetAllPorts.
455TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
456 AddInterface(kClientAddr);
457 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
458 session_->StartGettingPorts();
459 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
460 EXPECT_EQ(2U, ports_.size());
461 session_->StopGettingPorts();
462 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
463}
464
465// Test that we restrict client ports appropriately when a port range is set.
466// We check the candidates for udp/stun/tcp ports, and the from address
467// for relay ports.
468TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
469 AddInterface(kClientAddr);
470 // Check that an invalid port range fails.
471 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
472 // Check that a null port range succeeds.
473 EXPECT_TRUE(SetPortRange(0, 0));
474 // Check that a valid port range succeeds.
475 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
476 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
477 session_->StartGettingPorts();
478 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
479 EXPECT_EQ(4U, ports_.size());
480 // Check the port number for the UDP port object.
481 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
482 // Check the port number for the STUN port object.
483 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
484 // Check the port number used to connect to the relay server.
485 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
486 kMinPort, kMaxPort);
487 // Check the port number for the TCP port object.
488 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
489 EXPECT_TRUE(candidate_allocation_done_);
490}
491
492// Test that we don't crash or malfunction if we have no network adapters.
493TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
494 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
495 session_->StartGettingPorts();
496 rtc::Thread::Current()->ProcessMessages(100);
497 // Without network adapter, we should not get any candidate.
498 EXPECT_EQ(0U, candidates_.size());
499 EXPECT_TRUE(candidate_allocation_done_);
500}
501
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000502// Test that we should only get STUN and TURN candidates when adapter
503// enumeration is disabled.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700504TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000505 AddInterface(kClientAddr);
506 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700507 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000508 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700509 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
510 // TURN/UDP candidates.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700511 CheckDisableAdapterEnumeration(3U, kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700512 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000513}
514
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700515// Test that even with multiple interfaces, the result should still be one STUN
516// and one TURN candidate since we bind to any address (i.e. all 0s).
517TEST_F(PortAllocatorTest,
518 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000519 AddInterface(kPrivateAddr);
520 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700521 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000522 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700523 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
524 // TURN/UDP candidates.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700525 CheckDisableAdapterEnumeration(3U, kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700526 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
527}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000528
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700529// Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a
530// TURN/TCP server is specified.
531TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
532 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
533 AddInterface(kClientAddr);
534 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700535 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700536 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
537 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN,
538 // TURN/UDP, and TURN/TCP candidates.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700539 CheckDisableAdapterEnumeration(4U, kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700540 kTurnUdpExtAddr.ipaddr(),
541 kTurnUdpExtAddr.ipaddr());
542}
543
544// Test that we should only get STUN and TURN candidates when adapter
545// enumeration is disabled. Since the endpoint is not behind NAT, the srflx
546// address should be the public client interface.
547TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) {
548 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700549 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700550 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
551 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and
552 // TURN candidates. The STUN candidate should have kClientAddr as srflx
553 // address, and TURN candidate with kClientAddr as the related address.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700554 CheckDisableAdapterEnumeration(3U, kClientAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700555 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
556}
557
558// Test that when adapter enumeration is disabled, for endpoints without
559// STUN/TURN specified, no candidate is generated.
560TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
561 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700562 ResetWithNoServersOrNat();
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700563 // Expect to see 2 ports: STUN and TCP ports, but no candidate.
564 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
565 rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000566}
567
Erik Språngefdce692015-06-05 09:41:26 +0200568// Disable for asan, see
569// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
570#if !defined(ADDRESS_SANITIZER)
571
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000572// Test that we can get OnCandidatesAllocationDone callback when all the ports
573// are disabled.
574TEST_F(PortAllocatorTest, TestDisableAllPorts) {
575 AddInterface(kClientAddr);
576 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
577 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
578 cricket::PORTALLOCATOR_DISABLE_STUN |
579 cricket::PORTALLOCATOR_DISABLE_RELAY |
580 cricket::PORTALLOCATOR_DISABLE_TCP);
581 session_->StartGettingPorts();
582 rtc::Thread::Current()->ProcessMessages(100);
583 EXPECT_EQ(0U, candidates_.size());
584 EXPECT_TRUE(candidate_allocation_done_);
585}
586
587// Test that we don't crash or malfunction if we can't create UDP sockets.
588TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
589 AddInterface(kClientAddr);
590 fss_->set_udp_sockets_enabled(false);
591 EXPECT_TRUE(CreateSession(1));
592 session_->StartGettingPorts();
593 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
594 EXPECT_EQ(2U, ports_.size());
595 EXPECT_PRED5(CheckCandidate, candidates_[0],
596 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
597 EXPECT_PRED5(CheckCandidate, candidates_[1],
598 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
599 EXPECT_PRED5(CheckCandidate, candidates_[2],
600 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
601 EXPECT_PRED5(CheckCandidate, candidates_[3],
602 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
603 EXPECT_PRED5(CheckCandidate, candidates_[4],
604 cricket::ICE_CANDIDATE_COMPONENT_RTP,
605 "relay", "ssltcp", kRelaySslTcpIntAddr);
606 EXPECT_TRUE(candidate_allocation_done_);
607}
608
Erik Språngefdce692015-06-05 09:41:26 +0200609#endif // if !defined(ADDRESS_SANITIZER)
610
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000611// Test that we don't crash or malfunction if we can't create UDP sockets or
612// listen on TCP sockets. We still give out a local TCP address, since
613// apparently this is needed for the remote side to accept our connection.
614TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
615 AddInterface(kClientAddr);
616 fss_->set_udp_sockets_enabled(false);
617 fss_->set_tcp_listen_enabled(false);
618 EXPECT_TRUE(CreateSession(1));
619 session_->StartGettingPorts();
620 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
621 EXPECT_EQ(2U, ports_.size());
622 EXPECT_PRED5(CheckCandidate, candidates_[0],
623 1, "relay", "udp", kRelayUdpIntAddr);
624 EXPECT_PRED5(CheckCandidate, candidates_[1],
625 1, "relay", "udp", kRelayUdpExtAddr);
626 EXPECT_PRED5(CheckCandidate, candidates_[2],
627 1, "relay", "tcp", kRelayTcpIntAddr);
628 EXPECT_PRED5(CheckCandidate, candidates_[3],
629 1, "local", "tcp", kClientAddr);
630 EXPECT_PRED5(CheckCandidate, candidates_[4],
631 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
632 EXPECT_TRUE(candidate_allocation_done_);
633}
634
635// Test that we don't crash or malfunction if we can't create any sockets.
636// TODO: Find a way to exit early here.
637TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
638 AddInterface(kClientAddr);
639 fss_->set_tcp_sockets_enabled(false);
640 fss_->set_udp_sockets_enabled(false);
641 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
642 session_->StartGettingPorts();
643 WAIT(candidates_.size() > 0, 2000);
644 // TODO - Check candidate_allocation_done signal.
645 // In case of Relay, ports creation will succeed but sockets will fail.
646 // There is no error reporting from RelayEntry to handle this failure.
647}
648
649// Testing STUN timeout.
650TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
651 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
652 AddInterface(kClientAddr);
653 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
654 session_->StartGettingPorts();
655 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
656 EXPECT_EQ(2U, ports_.size());
657 EXPECT_PRED5(CheckCandidate, candidates_[0],
658 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
659 EXPECT_PRED5(CheckCandidate, candidates_[1],
660 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
661 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
662 // will be tried after 3 seconds.
663 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
664 EXPECT_EQ(3U, ports_.size());
665 EXPECT_PRED5(CheckCandidate, candidates_[2],
666 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
667 EXPECT_PRED5(CheckCandidate, candidates_[3],
668 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
669 EXPECT_PRED5(CheckCandidate, candidates_[4],
670 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
671 kRelaySslTcpIntAddr);
672 EXPECT_PRED5(CheckCandidate, candidates_[5],
673 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
674 // Stun Timeout is 9sec.
675 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
676}
677
678TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
679 AddInterface(kClientAddr);
680 AddInterface(kClientAddr2);
681 // Allocating only host UDP ports. This is done purely for testing
682 // convenience.
683 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
684 cricket::PORTALLOCATOR_DISABLE_STUN |
685 cricket::PORTALLOCATOR_DISABLE_RELAY);
686 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
687 session_->StartGettingPorts();
688 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
689 ASSERT_EQ(2U, candidates_.size());
690 EXPECT_EQ(2U, ports_.size());
691 // Candidates priorities should be different.
692 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
693}
694
695// Test to verify ICE restart process.
696TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
697 AddInterface(kClientAddr);
698 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
699 session_->StartGettingPorts();
700 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
701 EXPECT_EQ(4U, ports_.size());
702 EXPECT_TRUE(candidate_allocation_done_);
703 // TODO - Extend this to verify ICE restart.
704}
705
706// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
707// This test also verifies that when the allocator is only allowed to use
708// relay (i.e. IceTransportsType is relay), the raddr is an empty
709// address with the correct family. This is to prevent any local
710// reflective address leakage in the sdp line.
711TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
712 AddInterface(kClientAddr);
713 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700714 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000715 allocator().set_candidate_filter(cricket::CF_RELAY);
716 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
717 session_->StartGettingPorts();
718 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
719 EXPECT_PRED5(CheckCandidate,
720 candidates_[0],
721 cricket::ICE_CANDIDATE_COMPONENT_RTP,
722 "relay",
723 "udp",
724 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
725
726 EXPECT_EQ(1U, candidates_.size());
727 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
728 for (size_t i = 0; i < candidates_.size(); ++i) {
729 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
730 EXPECT_EQ(
731 candidates_[0].related_address(),
732 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
733 }
734}
735
736TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
737 AddInterface(kClientAddr);
Peter Thatcher081f34b2015-08-19 20:37:54 -0700738 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000739 allocator().set_candidate_filter(cricket::CF_HOST);
740 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
741 session_->StartGettingPorts();
742 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
743 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
744 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
745 for (size_t i = 0; i < candidates_.size(); ++i) {
746 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
747 }
748}
749
750// Host is behind the NAT.
751TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
752 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700753 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000754
Peter Thatcher081f34b2015-08-19 20:37:54 -0700755 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000756 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
757 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
758 session_->StartGettingPorts();
759 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
760 // Host is behind NAT, no private address will be exposed. Hence only UDP
761 // port with STUN candidate will be sent outside.
762 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
763 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
764 for (size_t i = 0; i < candidates_.size(); ++i) {
765 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
766 EXPECT_EQ(
767 candidates_[0].related_address(),
768 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
769 }
770}
771
772// Host is not behind the NAT.
773TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
774 AddInterface(kClientAddr);
Peter Thatcher081f34b2015-08-19 20:37:54 -0700775 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000776 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
777 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
778 session_->StartGettingPorts();
779 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
780 // Host has a public address, both UDP and TCP candidates will be exposed.
781 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
782 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
783 for (size_t i = 0; i < candidates_.size(); ++i) {
784 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
785 }
786}
787
Peter Thatcher081f34b2015-08-19 20:37:54 -0700788// Test that we get the same ufrag and pwd for all candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000789TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000790 AddInterface(kClientAddr);
791 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
792 session_->StartGettingPorts();
793 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
794 EXPECT_PRED5(CheckCandidate, candidates_[0],
795 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
796 EXPECT_PRED5(CheckCandidate, candidates_[1],
797 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
798 EXPECT_PRED5(CheckCandidate, candidates_[5],
799 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
800 EXPECT_EQ(4U, ports_.size());
801 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
802 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
803 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
804 EXPECT_EQ(kIcePwd0, candidates_[0].password());
805 EXPECT_EQ(kIcePwd0, candidates_[1].password());
806 EXPECT_TRUE(candidate_allocation_done_);
807}
808
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000809// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
810// is allocated for udp and stun. Also verify there is only one candidate
811// (local) if stun candidate is same as local candidate, which will be the case
812// in a public network like the below test.
813TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
814 AddInterface(kClientAddr);
815 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000816 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
817 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
818 session_->StartGettingPorts();
819 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
820 EXPECT_EQ(3U, ports_.size());
821 EXPECT_PRED5(CheckCandidate, candidates_[0],
822 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
823 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
824}
825
826// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
827// is allocated for udp and stun. In this test we should expect both stun and
828// local candidates as client behind a nat.
829TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
830 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700831 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000832
833 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000834 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
835 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
836 session_->StartGettingPorts();
837 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
838 ASSERT_EQ(2U, ports_.size());
839 EXPECT_PRED5(CheckCandidate, candidates_[0],
840 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
841 EXPECT_PRED5(CheckCandidate, candidates_[1],
842 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700843 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
845 EXPECT_EQ(3U, candidates_.size());
846}
847
deadbeefc5d0d952015-07-16 10:22:21 -0700848// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000849TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
850 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
851 AddInterface(kClientAddr);
852 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
853
854 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
855
856 allocator_->set_step_delay(cricket::kMinimumStepDelay);
857 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000858 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
859 cricket::PORTALLOCATOR_DISABLE_TCP);
860
861 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
862 session_->StartGettingPorts();
863
864 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
865 ASSERT_EQ(3U, ports_.size());
866 EXPECT_PRED5(CheckCandidate, candidates_[0],
867 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
868 EXPECT_PRED5(CheckCandidate, candidates_[1],
869 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
870 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
871 EXPECT_PRED5(CheckCandidate, candidates_[2],
872 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
873 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
874 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
875 EXPECT_EQ(3U, candidates_.size());
876}
877
878// Testing DNS resolve for the TURN server, this will test AllocationSequence
879// handling the unresolved address signal from TurnPort.
880TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
881 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
882 cricket::PROTO_UDP);
883 AddInterface(kClientAddr);
884 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
885 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
886 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
887 relay_server.credentials = credentials;
888 relay_server.ports.push_back(cricket::ProtocolAddress(
889 rtc::SocketAddress("localhost", 3478),
890 cricket::PROTO_UDP, false));
891 allocator_->AddRelay(relay_server);
892
893 allocator_->set_step_delay(cricket::kMinimumStepDelay);
894 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
896 cricket::PORTALLOCATOR_DISABLE_TCP);
897
898 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
899 session_->StartGettingPorts();
900
901 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
902}
903
904// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
905// is allocated for udp/stun/turn. In this test we should expect all local,
906// stun and turn candidates.
907TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
908 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700909 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000910
911 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
912
913 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000914 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
915 cricket::PORTALLOCATOR_DISABLE_TCP);
916
917 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
918 session_->StartGettingPorts();
919
920 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
921 ASSERT_EQ(2U, ports_.size());
922 EXPECT_PRED5(CheckCandidate, candidates_[0],
923 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
924 EXPECT_PRED5(CheckCandidate, candidates_[1],
925 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700926 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000927 EXPECT_PRED5(CheckCandidate, candidates_[2],
928 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
929 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
930 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
931 EXPECT_EQ(3U, candidates_.size());
932 // Local port will be created first and then TURN port.
933 EXPECT_EQ(2U, ports_[0]->Candidates().size());
934 EXPECT_EQ(1U, ports_[1]->Candidates().size());
935}
936
937// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
938// server is also used as the STUN server, we should get 'local', 'stun', and
939// 'relay' candidates.
940TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
941 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -0700942 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700943 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000944 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
945
946 // Must set the step delay to 0 to make sure the relay allocation phase is
947 // started before the STUN candidates are obtained, so that the STUN binding
948 // response is processed when both StunPort and TurnPort exist to reproduce
949 // webrtc issue 3537.
950 allocator_->set_step_delay(0);
951 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000952 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
953 cricket::PORTALLOCATOR_DISABLE_TCP);
954
955 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
956 session_->StartGettingPorts();
957
958 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
959 EXPECT_PRED5(CheckCandidate, candidates_[0],
960 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
961 EXPECT_PRED5(CheckCandidate, candidates_[1],
962 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700963 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000964 EXPECT_PRED5(CheckCandidate, candidates_[2],
965 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
966 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
967 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
968
969 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
970 EXPECT_EQ(3U, candidates_.size());
971 // Local port will be created first and then TURN port.
972 EXPECT_EQ(2U, ports_[0]->Candidates().size());
973 EXPECT_EQ(1U, ports_[1]->Candidates().size());
974}
975
deadbeefc5d0d952015-07-16 10:22:21 -0700976// Test that when only a TCP TURN server is available, we do NOT use it as
977// a UDP STUN server, as this could leak our IP address. Thus we should only
978// expect two ports, a UDPPort and TurnPort.
979TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
980 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
981 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700982 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -0700983 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
984
985 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -0700986 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
987 cricket::PORTALLOCATOR_DISABLE_TCP);
988
989 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
990 session_->StartGettingPorts();
991
992 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
993 ASSERT_EQ(2U, ports_.size());
994 EXPECT_PRED5(CheckCandidate, candidates_[0],
995 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
996 kClientAddr);
997 EXPECT_PRED5(CheckCandidate, candidates_[1],
998 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
999 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1000 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1001 EXPECT_EQ(2U, candidates_.size());
1002 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1003 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1004}
1005
1006// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1007// TURN server is used as the STUN server and we get 'local', 'stun', and
1008// 'relay' candidates.
1009// TODO(deadbeef): Remove this test when support for non-shared socket mode
1010// is removed.
1011TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1012 AddInterface(kClientAddr);
1013 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001014 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001015 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1016
1017 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001018 cricket::PORTALLOCATOR_DISABLE_TCP);
1019
1020 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1021 session_->StartGettingPorts();
1022
1023 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1024 ASSERT_EQ(3U, ports_.size());
1025 EXPECT_PRED5(CheckCandidate, candidates_[0],
1026 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1027 kClientAddr);
1028 EXPECT_PRED5(CheckCandidate, candidates_[1],
1029 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1030 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1031 EXPECT_PRED5(CheckCandidate, candidates_[2],
1032 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1033 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1034 // Not using shared socket, so the STUN request's server reflexive address
1035 // should be different than the TURN request's server reflexive address.
1036 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1037
1038 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1039 EXPECT_EQ(3U, candidates_.size());
1040 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1041 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1042 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1043}
1044
1045// Test that even when both a STUN and TURN server are configured, the TURN
1046// server is used as a STUN server and we get a 'stun' candidate.
1047TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1048 AddInterface(kClientAddr);
1049 // Configure with STUN server but destroy it, so we can ensure that it's
1050 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001051 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001052 stun_server_.reset();
1053 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1054
1055 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001056 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1057 cricket::PORTALLOCATOR_DISABLE_TCP);
1058
1059 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1060 session_->StartGettingPorts();
1061
1062 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1063 EXPECT_PRED5(CheckCandidate, candidates_[0],
1064 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1065 kClientAddr);
1066 EXPECT_PRED5(CheckCandidate, candidates_[1],
1067 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1068 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1069 EXPECT_PRED5(CheckCandidate, candidates_[2],
1070 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1071 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1072 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1073
1074 // Don't bother waiting for STUN timeout, since we already verified
1075 // that we got a STUN candidate from the TURN server.
1076}
1077
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001078// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1079// and fail to generate STUN candidate, local UDP candidate is generated
1080// properly.
1081TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1082 allocator().set_flags(allocator().flags() |
1083 cricket::PORTALLOCATOR_DISABLE_RELAY |
1084 cricket::PORTALLOCATOR_DISABLE_TCP |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001085 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1086 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1087 AddInterface(kClientAddr);
1088 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1089 session_->StartGettingPorts();
1090 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1091 EXPECT_EQ(1U, candidates_.size());
1092 EXPECT_PRED5(CheckCandidate, candidates_[0],
1093 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1094 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1095 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1096 EXPECT_EQ(1U, candidates_.size());
1097}
1098
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001099// Test that when the NetworkManager doesn't have permission to enumerate
1100// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1101// automatically.
1102TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
1103 AddInterface(kClientAddr);
1104 network_manager_.set_enumeration_permission(
1105 rtc::NetworkManager::kEnumerationDisallowed);
1106 allocator().set_flags(allocator().flags() |
1107 cricket::PORTALLOCATOR_DISABLE_RELAY |
1108 cricket::PORTALLOCATOR_DISABLE_TCP |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001109 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1110 EXPECT_EQ(
1111 allocator_->flags() & cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION,
1112 0U);
1113 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1114 EXPECT_EQ(
1115 session_->flags() & cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION,
1116 0U);
1117 session_->StartGettingPorts();
1118 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1119 EXPECT_EQ(0U, candidates_.size());
1120 EXPECT_TRUE((session_->flags() &
1121 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1122}
1123
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001124// This test verifies allocator can use IPv6 addresses along with IPv4.
1125TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1126 allocator().set_flags(allocator().flags() |
1127 cricket::PORTALLOCATOR_DISABLE_RELAY |
1128 cricket::PORTALLOCATOR_ENABLE_IPV6 |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001129 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1130 AddInterface(kClientIPv6Addr);
1131 AddInterface(kClientAddr);
1132 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1133 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1134 session_->StartGettingPorts();
1135 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1136 EXPECT_EQ(4U, candidates_.size());
1137 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1138 EXPECT_PRED5(CheckCandidate, candidates_[0],
1139 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1140 kClientIPv6Addr);
1141 EXPECT_PRED5(CheckCandidate, candidates_[1],
1142 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1143 kClientAddr);
1144 EXPECT_PRED5(CheckCandidate, candidates_[2],
1145 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1146 kClientIPv6Addr);
1147 EXPECT_PRED5(CheckCandidate, candidates_[3],
1148 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1149 kClientAddr);
1150 EXPECT_EQ(4U, candidates_.size());
1151}
1152
1153// Test that the httpportallocator correctly maintains its lists of stun and
1154// relay servers, by never allowing an empty list.
1155TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1156 rtc::FakeNetworkManager network_manager;
1157 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1158 EXPECT_EQ(1U, alloc.relay_hosts().size());
1159 EXPECT_EQ(1U, alloc.stun_hosts().size());
1160
1161 std::vector<std::string> relay_servers;
1162 std::vector<rtc::SocketAddress> stun_servers;
1163
1164 alloc.SetRelayHosts(relay_servers);
1165 alloc.SetStunHosts(stun_servers);
1166 EXPECT_EQ(1U, alloc.relay_hosts().size());
1167 EXPECT_EQ(1U, alloc.stun_hosts().size());
1168
1169 relay_servers.push_back("1.unittest.corp.google.com");
1170 relay_servers.push_back("2.unittest.corp.google.com");
1171 stun_servers.push_back(
1172 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1173 stun_servers.push_back(
1174 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1175
1176 alloc.SetRelayHosts(relay_servers);
1177 alloc.SetStunHosts(stun_servers);
1178 EXPECT_EQ(2U, alloc.relay_hosts().size());
1179 EXPECT_EQ(2U, alloc.stun_hosts().size());
1180}
1181
1182// Test that the HttpPortAllocator uses correct URL to create sessions.
1183TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1184 rtc::FakeNetworkManager network_manager;
1185 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1186
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001187 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1188 static_cast<cricket::HttpPortAllocatorSession*>(
1189 alloc.CreateSessionInternal(
1190 "test content", 0, kIceUfrag0, kIcePwd0)));
1191 std::string url = session->GetSessionRequestUrl();
1192 LOG(LS_INFO) << "url: " << url;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001193 std::vector<std::string> parts;
1194 rtc::split(url, '?', &parts);
1195 ASSERT_EQ(2U, parts.size());
1196
1197 std::vector<std::string> args_parts;
1198 rtc::split(parts[1], '&', &args_parts);
1199
1200 std::map<std::string, std::string> args;
1201 for (std::vector<std::string>::iterator it = args_parts.begin();
1202 it != args_parts.end(); ++it) {
1203 std::vector<std::string> parts;
1204 rtc::split(*it, '=', &parts);
1205 ASSERT_EQ(2U, parts.size());
1206 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1207 }
1208
1209 EXPECT_EQ(kIceUfrag0, args["username"]);
1210 EXPECT_EQ(kIcePwd0, args["password"]);
1211}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001212
1213// Tests that destroying ports with non-shared sockets does not crash.
1214// b/19074679.
1215TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1216 AddInterface(kClientAddr);
1217 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1218 session_->StartGettingPorts();
1219 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1220 EXPECT_EQ(4U, ports_.size());
1221
1222 auto it = ports_.begin();
1223 for (; it != ports_.end(); ++it) {
1224 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1225 }
1226}
honghaizf421bdc2015-07-17 16:21:55 -07001227
1228class AllocationSequenceForTest : public cricket::AllocationSequence {
1229 public:
1230 AllocationSequenceForTest(cricket::BasicPortAllocatorSession* session,
1231 rtc::Network* network,
1232 cricket::PortConfiguration* config,
1233 uint32 flags)
1234 : cricket::AllocationSequence(session, network, config, flags) {}
1235 using cricket::AllocationSequence::CreateTurnPort;
1236};
1237
1238TEST_F(PortAllocatorTest, TestCreateTurnPortWithNullSocket) {
1239 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1240 session_->StartGettingPorts();
1241
1242 cricket::ServerAddresses stun_servers;
1243 stun_servers.insert(kStunAddr);
1244 cricket::PortConfiguration config(stun_servers, kIceUfrag0, kIcePwd0);
1245 rtc::Network network1("test_eth0", "Test Network Adapter 1",
1246 rtc::IPAddress(0x12345600U), 24);
1247 uint32 flag = cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
1248 AllocationSequenceForTest alloc_sequence(
1249 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1250 &network1, &config, flag);
1251 // This simply tests it will not crash if udp_socket_ in the
1252 // AllocationSequence is null, which is chosen in the constructor.
1253 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1254 relay_server.ports.push_back(
1255 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1256 alloc_sequence.CreateTurnPort(relay_server);
1257}