blob: 5e2c1f89341a8d99977f917fd0601b5c86266e86 [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_UFRAG |
252 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
253 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000254 session_->StartGettingPorts();
255 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
256
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700257 uint32 total_candidates = 0;
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700258 if (!stun_candidate_addr.IsNil()) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700259 ++total_candidates;
260 EXPECT_PRED5(CheckCandidate, candidates_[0],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700261 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
262 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700263 EXPECT_EQ(
264 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()),
265 candidates_[0].related_address());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700266 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700267 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700268 ++total_candidates;
269 EXPECT_PRED5(CheckCandidate, candidates_[1],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700270 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
271 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700272 EXPECT_EQ(stun_candidate_addr, candidates_[1].related_address().ipaddr());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700273 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700274 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700275 ++total_candidates;
276 EXPECT_PRED5(CheckCandidate, candidates_[2],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700277 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
278 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700279 EXPECT_EQ(stun_candidate_addr, candidates_[2].related_address().ipaddr());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700280 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000281
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700282 EXPECT_EQ(total_candidates, candidates_.size());
283 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000284 }
285
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000286 protected:
287 cricket::BasicPortAllocator& allocator() {
288 return *allocator_;
289 }
290
291 void OnPortReady(cricket::PortAllocatorSession* ses,
292 cricket::PortInterface* port) {
293 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
294 ports_.push_back(port);
295 }
296 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
297 const std::vector<cricket::Candidate>& candidates) {
298 for (size_t i = 0; i < candidates.size(); ++i) {
299 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
300 candidates_.push_back(candidates[i]);
301 }
302 }
303
304 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
305 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
306 cricket::RelayServerConfig server_config = allocator_->relays()[i];
307 cricket::PortList::const_iterator relay_port;
308 for (relay_port = server_config.ports.begin();
309 relay_port != server_config.ports.end(); ++relay_port) {
310 if (proto_addr.address == relay_port->address &&
311 proto_addr.proto == relay_port->proto)
312 return true;
313 }
314 }
315 return false;
316 }
317
Guo-wei Shieh11477022015-08-15 09:28:41 -0700318 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
319 bool with_nat) {
320 if (with_nat) {
321 nat_server_.reset(new rtc::NATServer(
322 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
323 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
324 } else {
325 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
326 }
327
328 ServerAddresses stun_servers;
329 if (!stun_server.IsNil()) {
330 stun_servers.insert(stun_server);
331 }
332 allocator_.reset(new cricket::BasicPortAllocator(
333 &network_manager_, nat_socket_factory_.get(), stun_servers));
334 allocator().set_step_delay(cricket::kMinimumStepDelay);
335 }
336
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000337 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
338 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
339 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
340 rtc::SocketServerScope ss_scope_;
341 rtc::scoped_ptr<rtc::NATServer> nat_server_;
342 rtc::NATSocketFactory nat_factory_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700343 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
345 cricket::TestRelayServer relay_server_;
346 cricket::TestTurnServer turn_server_;
347 rtc::FakeNetworkManager network_manager_;
348 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
349 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
350 std::vector<cricket::PortInterface*> ports_;
351 std::vector<cricket::Candidate> candidates_;
352 bool candidate_allocation_done_;
353};
354
355// Tests that we can init the port allocator and create a session.
356TEST_F(PortAllocatorTest, TestBasic) {
357 EXPECT_EQ(&network_manager_, allocator().network_manager());
358 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
359 ASSERT_EQ(1u, allocator().relays().size());
360 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
361 // Empty relay credentials are used for GTURN.
362 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
363 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
364 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
365 kRelayUdpIntAddr, cricket::PROTO_UDP)));
366 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
367 kRelayTcpIntAddr, cricket::PROTO_TCP)));
368 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
369 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
370 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
371}
372
373// Tests that we allocator session not trying to allocate ports for every 250ms.
374TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
375 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
376 session_->StartGettingPorts();
377 // Waiting for one second to make sure BasicPortAllocatorSession has not
378 // called OnAllocate multiple times. In old behavior it's called every 250ms.
379 // When there are no network interfaces, each execution of OnAllocate will
380 // result in SignalCandidatesAllocationDone signal.
381 rtc::Thread::Current()->ProcessMessages(1000);
382 EXPECT_TRUE(candidate_allocation_done_);
383 EXPECT_EQ(0U, candidates_.size());
384}
385
386// Tests that we can get all the desired addresses successfully.
387TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
388 AddInterface(kClientAddr);
389 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
390 session_->StartGettingPorts();
391 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
392 EXPECT_EQ(4U, ports_.size());
393 EXPECT_PRED5(CheckCandidate, candidates_[0],
394 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
395 EXPECT_PRED5(CheckCandidate, candidates_[1],
396 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
397 EXPECT_PRED5(CheckCandidate, candidates_[2],
398 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
399 EXPECT_PRED5(CheckCandidate, candidates_[3],
400 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
401 EXPECT_PRED5(CheckCandidate, candidates_[4],
402 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
403 EXPECT_PRED5(CheckCandidate, candidates_[5],
404 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
405 EXPECT_PRED5(CheckCandidate, candidates_[6],
406 cricket::ICE_CANDIDATE_COMPONENT_RTP,
407 "relay", "ssltcp", kRelaySslTcpIntAddr);
408 EXPECT_TRUE(candidate_allocation_done_);
409}
410
411// Verify candidates with default step delay of 1sec.
412TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
413 AddInterface(kClientAddr);
414 allocator_->set_step_delay(cricket::kDefaultStepDelay);
415 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
416 session_->StartGettingPorts();
417 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
418 EXPECT_EQ(2U, ports_.size());
419 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
420 EXPECT_EQ(3U, ports_.size());
421 EXPECT_PRED5(CheckCandidate, candidates_[2],
422 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
423 EXPECT_PRED5(CheckCandidate, candidates_[3],
424 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
425 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
426 EXPECT_PRED5(CheckCandidate, candidates_[4],
427 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
428 EXPECT_PRED5(CheckCandidate, candidates_[5],
429 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
430 EXPECT_EQ(4U, ports_.size());
431 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
432 EXPECT_PRED5(CheckCandidate, candidates_[6],
433 cricket::ICE_CANDIDATE_COMPONENT_RTP,
434 "relay", "ssltcp", kRelaySslTcpIntAddr);
435 EXPECT_EQ(4U, ports_.size());
436 EXPECT_TRUE(candidate_allocation_done_);
437 // If we Stop gathering now, we shouldn't get a second "done" callback.
438 session_->StopGettingPorts();
439}
440
441TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
442 AddInterface(kClientAddr);
443 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
444 cricket::CN_VIDEO));
445 session_->StartGettingPorts();
446 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
447 EXPECT_TRUE(candidate_allocation_done_);
448 // If we Stop gathering now, we shouldn't get a second "done" callback.
449 session_->StopGettingPorts();
450
451 // All ports should have unset send-buffer sizes.
452 CheckSendBufferSizesOfAllPorts(-1);
453}
454
455// Tests that we can get callback after StopGetAllPorts.
456TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
457 AddInterface(kClientAddr);
458 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
459 session_->StartGettingPorts();
460 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
461 EXPECT_EQ(2U, ports_.size());
462 session_->StopGettingPorts();
463 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
464}
465
466// Test that we restrict client ports appropriately when a port range is set.
467// We check the candidates for udp/stun/tcp ports, and the from address
468// for relay ports.
469TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
470 AddInterface(kClientAddr);
471 // Check that an invalid port range fails.
472 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
473 // Check that a null port range succeeds.
474 EXPECT_TRUE(SetPortRange(0, 0));
475 // Check that a valid port range succeeds.
476 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
477 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
478 session_->StartGettingPorts();
479 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
480 EXPECT_EQ(4U, ports_.size());
481 // Check the port number for the UDP port object.
482 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
483 // Check the port number for the STUN port object.
484 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
485 // Check the port number used to connect to the relay server.
486 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
487 kMinPort, kMaxPort);
488 // Check the port number for the TCP port object.
489 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
490 EXPECT_TRUE(candidate_allocation_done_);
491}
492
493// Test that we don't crash or malfunction if we have no network adapters.
494TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
495 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
496 session_->StartGettingPorts();
497 rtc::Thread::Current()->ProcessMessages(100);
498 // Without network adapter, we should not get any candidate.
499 EXPECT_EQ(0U, candidates_.size());
500 EXPECT_TRUE(candidate_allocation_done_);
501}
502
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000503// Test that we should only get STUN and TURN candidates when adapter
504// enumeration is disabled.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700505TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000506 AddInterface(kClientAddr);
507 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700508 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000509 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700510 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
511 // TURN/UDP candidates.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700512 CheckDisableAdapterEnumeration(3U, kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700513 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000514}
515
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700516// Test that even with multiple interfaces, the result should still be one STUN
517// and one TURN candidate since we bind to any address (i.e. all 0s).
518TEST_F(PortAllocatorTest,
519 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000520 AddInterface(kPrivateAddr);
521 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700522 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000523 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700524 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
525 // TURN/UDP candidates.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700526 CheckDisableAdapterEnumeration(3U, kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700527 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
528}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000529
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700530// Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a
531// TURN/TCP server is specified.
532TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
533 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
534 AddInterface(kClientAddr);
535 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700536 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700537 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
538 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN,
539 // TURN/UDP, and TURN/TCP candidates.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700540 CheckDisableAdapterEnumeration(4U, kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700541 kTurnUdpExtAddr.ipaddr(),
542 kTurnUdpExtAddr.ipaddr());
543}
544
545// Test that we should only get STUN and TURN candidates when adapter
546// enumeration is disabled. Since the endpoint is not behind NAT, the srflx
547// address should be the public client interface.
548TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) {
549 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700550 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700551 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
552 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and
553 // TURN candidates. The STUN candidate should have kClientAddr as srflx
554 // address, and TURN candidate with kClientAddr as the related address.
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700555 CheckDisableAdapterEnumeration(3U, kClientAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700556 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
557}
558
559// Test that when adapter enumeration is disabled, for endpoints without
560// STUN/TURN specified, no candidate is generated.
561TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
562 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700563 ResetWithNoServersOrNat();
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700564 // Expect to see 2 ports: STUN and TCP ports, but no candidate.
565 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
566 rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000567}
568
Erik Språngefdce692015-06-05 09:41:26 +0200569// Disable for asan, see
570// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
571#if !defined(ADDRESS_SANITIZER)
572
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000573// Test that we can get OnCandidatesAllocationDone callback when all the ports
574// are disabled.
575TEST_F(PortAllocatorTest, TestDisableAllPorts) {
576 AddInterface(kClientAddr);
577 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
578 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
579 cricket::PORTALLOCATOR_DISABLE_STUN |
580 cricket::PORTALLOCATOR_DISABLE_RELAY |
581 cricket::PORTALLOCATOR_DISABLE_TCP);
582 session_->StartGettingPorts();
583 rtc::Thread::Current()->ProcessMessages(100);
584 EXPECT_EQ(0U, candidates_.size());
585 EXPECT_TRUE(candidate_allocation_done_);
586}
587
588// Test that we don't crash or malfunction if we can't create UDP sockets.
589TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
590 AddInterface(kClientAddr);
591 fss_->set_udp_sockets_enabled(false);
592 EXPECT_TRUE(CreateSession(1));
593 session_->StartGettingPorts();
594 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
595 EXPECT_EQ(2U, ports_.size());
596 EXPECT_PRED5(CheckCandidate, candidates_[0],
597 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
598 EXPECT_PRED5(CheckCandidate, candidates_[1],
599 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
600 EXPECT_PRED5(CheckCandidate, candidates_[2],
601 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
602 EXPECT_PRED5(CheckCandidate, candidates_[3],
603 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
604 EXPECT_PRED5(CheckCandidate, candidates_[4],
605 cricket::ICE_CANDIDATE_COMPONENT_RTP,
606 "relay", "ssltcp", kRelaySslTcpIntAddr);
607 EXPECT_TRUE(candidate_allocation_done_);
608}
609
Erik Språngefdce692015-06-05 09:41:26 +0200610#endif // if !defined(ADDRESS_SANITIZER)
611
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000612// Test that we don't crash or malfunction if we can't create UDP sockets or
613// listen on TCP sockets. We still give out a local TCP address, since
614// apparently this is needed for the remote side to accept our connection.
615TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
616 AddInterface(kClientAddr);
617 fss_->set_udp_sockets_enabled(false);
618 fss_->set_tcp_listen_enabled(false);
619 EXPECT_TRUE(CreateSession(1));
620 session_->StartGettingPorts();
621 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
622 EXPECT_EQ(2U, ports_.size());
623 EXPECT_PRED5(CheckCandidate, candidates_[0],
624 1, "relay", "udp", kRelayUdpIntAddr);
625 EXPECT_PRED5(CheckCandidate, candidates_[1],
626 1, "relay", "udp", kRelayUdpExtAddr);
627 EXPECT_PRED5(CheckCandidate, candidates_[2],
628 1, "relay", "tcp", kRelayTcpIntAddr);
629 EXPECT_PRED5(CheckCandidate, candidates_[3],
630 1, "local", "tcp", kClientAddr);
631 EXPECT_PRED5(CheckCandidate, candidates_[4],
632 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
633 EXPECT_TRUE(candidate_allocation_done_);
634}
635
636// Test that we don't crash or malfunction if we can't create any sockets.
637// TODO: Find a way to exit early here.
638TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
639 AddInterface(kClientAddr);
640 fss_->set_tcp_sockets_enabled(false);
641 fss_->set_udp_sockets_enabled(false);
642 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
643 session_->StartGettingPorts();
644 WAIT(candidates_.size() > 0, 2000);
645 // TODO - Check candidate_allocation_done signal.
646 // In case of Relay, ports creation will succeed but sockets will fail.
647 // There is no error reporting from RelayEntry to handle this failure.
648}
649
650// Testing STUN timeout.
651TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
652 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
653 AddInterface(kClientAddr);
654 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
655 session_->StartGettingPorts();
656 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
657 EXPECT_EQ(2U, ports_.size());
658 EXPECT_PRED5(CheckCandidate, candidates_[0],
659 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
660 EXPECT_PRED5(CheckCandidate, candidates_[1],
661 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
662 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
663 // will be tried after 3 seconds.
664 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
665 EXPECT_EQ(3U, ports_.size());
666 EXPECT_PRED5(CheckCandidate, candidates_[2],
667 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
668 EXPECT_PRED5(CheckCandidate, candidates_[3],
669 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
670 EXPECT_PRED5(CheckCandidate, candidates_[4],
671 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
672 kRelaySslTcpIntAddr);
673 EXPECT_PRED5(CheckCandidate, candidates_[5],
674 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
675 // Stun Timeout is 9sec.
676 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
677}
678
679TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
680 AddInterface(kClientAddr);
681 AddInterface(kClientAddr2);
682 // Allocating only host UDP ports. This is done purely for testing
683 // convenience.
684 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
685 cricket::PORTALLOCATOR_DISABLE_STUN |
686 cricket::PORTALLOCATOR_DISABLE_RELAY);
687 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
688 session_->StartGettingPorts();
689 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
690 ASSERT_EQ(2U, candidates_.size());
691 EXPECT_EQ(2U, ports_.size());
692 // Candidates priorities should be different.
693 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
694}
695
696// Test to verify ICE restart process.
697TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
698 AddInterface(kClientAddr);
699 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
700 session_->StartGettingPorts();
701 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
702 EXPECT_EQ(4U, ports_.size());
703 EXPECT_TRUE(candidate_allocation_done_);
704 // TODO - Extend this to verify ICE restart.
705}
706
707// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
708// This test also verifies that when the allocator is only allowed to use
709// relay (i.e. IceTransportsType is relay), the raddr is an empty
710// address with the correct family. This is to prevent any local
711// reflective address leakage in the sdp line.
712TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
713 AddInterface(kClientAddr);
714 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700715 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000716 allocator().set_candidate_filter(cricket::CF_RELAY);
717 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
718 session_->StartGettingPorts();
719 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
720 EXPECT_PRED5(CheckCandidate,
721 candidates_[0],
722 cricket::ICE_CANDIDATE_COMPONENT_RTP,
723 "relay",
724 "udp",
725 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
726
727 EXPECT_EQ(1U, candidates_.size());
728 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
729 for (size_t i = 0; i < candidates_.size(); ++i) {
730 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
731 EXPECT_EQ(
732 candidates_[0].related_address(),
733 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
734 }
735}
736
737TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
738 AddInterface(kClientAddr);
pthatcherfa301802015-08-11 04:12:56 -0700739 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
740 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000741 allocator().set_candidate_filter(cricket::CF_HOST);
742 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
743 session_->StartGettingPorts();
744 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
745 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
746 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
747 for (size_t i = 0; i < candidates_.size(); ++i) {
748 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
749 }
750}
751
752// Host is behind the NAT.
753TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
754 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700755 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000756
pthatcherfa301802015-08-11 04:12:56 -0700757 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
758 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000759 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
760 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
761 session_->StartGettingPorts();
762 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
763 // Host is behind NAT, no private address will be exposed. Hence only UDP
764 // port with STUN candidate will be sent outside.
765 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
766 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
767 for (size_t i = 0; i < candidates_.size(); ++i) {
768 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
769 EXPECT_EQ(
770 candidates_[0].related_address(),
771 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
772 }
773}
774
775// Host is not behind the NAT.
776TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
777 AddInterface(kClientAddr);
pthatcherfa301802015-08-11 04:12:56 -0700778 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
779 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000780 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
781 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
782 session_->StartGettingPorts();
783 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
784 // Host has a public address, both UDP and TCP candidates will be exposed.
785 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
786 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
787 for (size_t i = 0; i < candidates_.size(); ++i) {
788 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
789 }
790}
791
pthatcherfa301802015-08-11 04:12:56 -0700792// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
793// ufrag and pwd for the collected candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000794TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
pthatcherfa301802015-08-11 04:12:56 -0700795 allocator().set_flags(allocator().flags() |
796 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000797 AddInterface(kClientAddr);
798 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
799 session_->StartGettingPorts();
800 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
801 EXPECT_PRED5(CheckCandidate, candidates_[0],
802 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
803 EXPECT_PRED5(CheckCandidate, candidates_[1],
804 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
805 EXPECT_PRED5(CheckCandidate, candidates_[5],
806 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
807 EXPECT_EQ(4U, ports_.size());
808 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
809 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
810 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
811 EXPECT_EQ(kIcePwd0, candidates_[0].password());
812 EXPECT_EQ(kIcePwd0, candidates_[1].password());
813 EXPECT_TRUE(candidate_allocation_done_);
814}
815
pthatcherfa301802015-08-11 04:12:56 -0700816// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
817// different ufrag and pwd for the collected candidates.
818TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
819 allocator().set_flags(allocator().flags() &
820 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
821 AddInterface(kClientAddr);
822 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
823 session_->StartGettingPorts();
824 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
825 EXPECT_PRED5(CheckCandidate, candidates_[0],
826 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
827 EXPECT_PRED5(CheckCandidate, candidates_[1],
828 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
829 EXPECT_EQ(4U, ports_.size());
830 // Port should generate random ufrag and pwd.
831 EXPECT_NE(kIceUfrag0, candidates_[0].username());
832 EXPECT_NE(kIceUfrag0, candidates_[1].username());
833 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
834 EXPECT_NE(kIcePwd0, candidates_[0].password());
835 EXPECT_NE(kIcePwd0, candidates_[1].password());
836 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
837 EXPECT_TRUE(candidate_allocation_done_);
838}
839
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000840// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
841// is allocated for udp and stun. Also verify there is only one candidate
842// (local) if stun candidate is same as local candidate, which will be the case
843// in a public network like the below test.
844TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
845 AddInterface(kClientAddr);
846 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700847 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000848 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
849 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
850 session_->StartGettingPorts();
851 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
852 EXPECT_EQ(3U, ports_.size());
853 EXPECT_PRED5(CheckCandidate, candidates_[0],
854 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
855 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
856}
857
858// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
859// is allocated for udp and stun. In this test we should expect both stun and
860// local candidates as client behind a nat.
861TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
862 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700863 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864
865 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700866 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000867 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
868 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
869 session_->StartGettingPorts();
870 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
871 ASSERT_EQ(2U, ports_.size());
872 EXPECT_PRED5(CheckCandidate, candidates_[0],
873 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
874 EXPECT_PRED5(CheckCandidate, candidates_[1],
875 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700876 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000877 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
878 EXPECT_EQ(3U, candidates_.size());
879}
880
deadbeefc5d0d952015-07-16 10:22:21 -0700881// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000882TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
883 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
884 AddInterface(kClientAddr);
885 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
886
887 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
888
889 allocator_->set_step_delay(cricket::kMinimumStepDelay);
890 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700891 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000892 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
893 cricket::PORTALLOCATOR_DISABLE_TCP);
894
895 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
896 session_->StartGettingPorts();
897
898 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
899 ASSERT_EQ(3U, ports_.size());
900 EXPECT_PRED5(CheckCandidate, candidates_[0],
901 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
902 EXPECT_PRED5(CheckCandidate, candidates_[1],
903 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
904 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
905 EXPECT_PRED5(CheckCandidate, candidates_[2],
906 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
907 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
908 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
909 EXPECT_EQ(3U, candidates_.size());
910}
911
912// Testing DNS resolve for the TURN server, this will test AllocationSequence
913// handling the unresolved address signal from TurnPort.
914TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
915 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
916 cricket::PROTO_UDP);
917 AddInterface(kClientAddr);
918 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
919 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
920 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
921 relay_server.credentials = credentials;
922 relay_server.ports.push_back(cricket::ProtocolAddress(
923 rtc::SocketAddress("localhost", 3478),
924 cricket::PROTO_UDP, false));
925 allocator_->AddRelay(relay_server);
926
927 allocator_->set_step_delay(cricket::kMinimumStepDelay);
928 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700929 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000930 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
931 cricket::PORTALLOCATOR_DISABLE_TCP);
932
933 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
934 session_->StartGettingPorts();
935
936 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
937}
938
939// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
940// is allocated for udp/stun/turn. In this test we should expect all local,
941// stun and turn candidates.
942TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
943 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700944 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000945
946 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
947
948 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700949 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000950 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
951 cricket::PORTALLOCATOR_DISABLE_TCP);
952
953 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
954 session_->StartGettingPorts();
955
956 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
957 ASSERT_EQ(2U, ports_.size());
958 EXPECT_PRED5(CheckCandidate, candidates_[0],
959 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
960 EXPECT_PRED5(CheckCandidate, candidates_[1],
961 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700962 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000963 EXPECT_PRED5(CheckCandidate, candidates_[2],
964 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
965 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
966 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
967 EXPECT_EQ(3U, candidates_.size());
968 // Local port will be created first and then TURN port.
969 EXPECT_EQ(2U, ports_[0]->Candidates().size());
970 EXPECT_EQ(1U, ports_[1]->Candidates().size());
971}
972
973// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
974// server is also used as the STUN server, we should get 'local', 'stun', and
975// 'relay' candidates.
976TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
977 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -0700978 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700979 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000980 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
981
982 // Must set the step delay to 0 to make sure the relay allocation phase is
983 // started before the STUN candidates are obtained, so that the STUN binding
984 // response is processed when both StunPort and TurnPort exist to reproduce
985 // webrtc issue 3537.
986 allocator_->set_step_delay(0);
987 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700988 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000989 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
990 cricket::PORTALLOCATOR_DISABLE_TCP);
991
992 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
993 session_->StartGettingPorts();
994
995 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
996 EXPECT_PRED5(CheckCandidate, candidates_[0],
997 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
998 EXPECT_PRED5(CheckCandidate, candidates_[1],
999 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001000 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001001 EXPECT_PRED5(CheckCandidate, candidates_[2],
1002 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1003 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1004 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1005
1006 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1007 EXPECT_EQ(3U, candidates_.size());
1008 // Local port will be created first and then TURN port.
1009 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1010 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1011}
1012
deadbeefc5d0d952015-07-16 10:22:21 -07001013// Test that when only a TCP TURN server is available, we do NOT use it as
1014// a UDP STUN server, as this could leak our IP address. Thus we should only
1015// expect two ports, a UDPPort and TurnPort.
1016TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1017 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1018 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001019 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001020 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1021
1022 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001023 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001024 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1025 cricket::PORTALLOCATOR_DISABLE_TCP);
1026
1027 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1028 session_->StartGettingPorts();
1029
1030 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1031 ASSERT_EQ(2U, ports_.size());
1032 EXPECT_PRED5(CheckCandidate, candidates_[0],
1033 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1034 kClientAddr);
1035 EXPECT_PRED5(CheckCandidate, candidates_[1],
1036 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1037 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1038 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1039 EXPECT_EQ(2U, candidates_.size());
1040 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1041 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1042}
1043
1044// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1045// TURN server is used as the STUN server and we get 'local', 'stun', and
1046// 'relay' candidates.
1047// TODO(deadbeef): Remove this test when support for non-shared socket mode
1048// is removed.
1049TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1050 AddInterface(kClientAddr);
1051 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001052 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001053 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1054
1055 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001056 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001057 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 ASSERT_EQ(3U, ports_.size());
1064 EXPECT_PRED5(CheckCandidate, candidates_[0],
1065 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1066 kClientAddr);
1067 EXPECT_PRED5(CheckCandidate, candidates_[1],
1068 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1069 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1070 EXPECT_PRED5(CheckCandidate, candidates_[2],
1071 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1072 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1073 // Not using shared socket, so the STUN request's server reflexive address
1074 // should be different than the TURN request's server reflexive address.
1075 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1076
1077 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1078 EXPECT_EQ(3U, candidates_.size());
1079 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1080 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1081 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1082}
1083
1084// Test that even when both a STUN and TURN server are configured, the TURN
1085// server is used as a STUN server and we get a 'stun' candidate.
1086TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1087 AddInterface(kClientAddr);
1088 // Configure with STUN server but destroy it, so we can ensure that it's
1089 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001090 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001091 stun_server_.reset();
1092 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1093
1094 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001095 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001096 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1097 cricket::PORTALLOCATOR_DISABLE_TCP);
1098
1099 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1100 session_->StartGettingPorts();
1101
1102 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1103 EXPECT_PRED5(CheckCandidate, candidates_[0],
1104 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1105 kClientAddr);
1106 EXPECT_PRED5(CheckCandidate, candidates_[1],
1107 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1108 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1109 EXPECT_PRED5(CheckCandidate, candidates_[2],
1110 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1111 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1112 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1113
1114 // Don't bother waiting for STUN timeout, since we already verified
1115 // that we got a STUN candidate from the TURN server.
1116}
1117
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001118// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1119// and fail to generate STUN candidate, local UDP candidate is generated
1120// properly.
1121TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1122 allocator().set_flags(allocator().flags() |
1123 cricket::PORTALLOCATOR_DISABLE_RELAY |
1124 cricket::PORTALLOCATOR_DISABLE_TCP |
pthatcherfa301802015-08-11 04:12:56 -07001125 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001126 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1127 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1128 AddInterface(kClientAddr);
1129 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1130 session_->StartGettingPorts();
1131 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1132 EXPECT_EQ(1U, candidates_.size());
1133 EXPECT_PRED5(CheckCandidate, candidates_[0],
1134 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1135 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1136 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1137 EXPECT_EQ(1U, candidates_.size());
1138}
1139
1140// This test verifies allocator can use IPv6 addresses along with IPv4.
1141TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1142 allocator().set_flags(allocator().flags() |
1143 cricket::PORTALLOCATOR_DISABLE_RELAY |
1144 cricket::PORTALLOCATOR_ENABLE_IPV6 |
pthatcherfa301802015-08-11 04:12:56 -07001145 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1147 AddInterface(kClientIPv6Addr);
1148 AddInterface(kClientAddr);
1149 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1150 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1151 session_->StartGettingPorts();
1152 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1153 EXPECT_EQ(4U, candidates_.size());
1154 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1155 EXPECT_PRED5(CheckCandidate, candidates_[0],
1156 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1157 kClientIPv6Addr);
1158 EXPECT_PRED5(CheckCandidate, candidates_[1],
1159 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1160 kClientAddr);
1161 EXPECT_PRED5(CheckCandidate, candidates_[2],
1162 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1163 kClientIPv6Addr);
1164 EXPECT_PRED5(CheckCandidate, candidates_[3],
1165 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1166 kClientAddr);
1167 EXPECT_EQ(4U, candidates_.size());
1168}
1169
1170// Test that the httpportallocator correctly maintains its lists of stun and
1171// relay servers, by never allowing an empty list.
1172TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1173 rtc::FakeNetworkManager network_manager;
1174 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1175 EXPECT_EQ(1U, alloc.relay_hosts().size());
1176 EXPECT_EQ(1U, alloc.stun_hosts().size());
1177
1178 std::vector<std::string> relay_servers;
1179 std::vector<rtc::SocketAddress> stun_servers;
1180
1181 alloc.SetRelayHosts(relay_servers);
1182 alloc.SetStunHosts(stun_servers);
1183 EXPECT_EQ(1U, alloc.relay_hosts().size());
1184 EXPECT_EQ(1U, alloc.stun_hosts().size());
1185
1186 relay_servers.push_back("1.unittest.corp.google.com");
1187 relay_servers.push_back("2.unittest.corp.google.com");
1188 stun_servers.push_back(
1189 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1190 stun_servers.push_back(
1191 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1192
1193 alloc.SetRelayHosts(relay_servers);
1194 alloc.SetStunHosts(stun_servers);
1195 EXPECT_EQ(2U, alloc.relay_hosts().size());
1196 EXPECT_EQ(2U, alloc.stun_hosts().size());
1197}
1198
1199// Test that the HttpPortAllocator uses correct URL to create sessions.
1200TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1201 rtc::FakeNetworkManager network_manager;
1202 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1203
pthatcherfa301802015-08-11 04:12:56 -07001204 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1205 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001206 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1207 static_cast<cricket::HttpPortAllocatorSession*>(
1208 alloc.CreateSessionInternal(
1209 "test content", 0, kIceUfrag0, kIcePwd0)));
1210 std::string url = session->GetSessionRequestUrl();
1211 LOG(LS_INFO) << "url: " << url;
pthatcherfa301802015-08-11 04:12:56 -07001212 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
1213
1214 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1215 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1216 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
1217 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
1218 url = session->GetSessionRequestUrl();
1219 LOG(LS_INFO) << "url: " << url;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001220 std::vector<std::string> parts;
1221 rtc::split(url, '?', &parts);
1222 ASSERT_EQ(2U, parts.size());
1223
1224 std::vector<std::string> args_parts;
1225 rtc::split(parts[1], '&', &args_parts);
1226
1227 std::map<std::string, std::string> args;
1228 for (std::vector<std::string>::iterator it = args_parts.begin();
1229 it != args_parts.end(); ++it) {
1230 std::vector<std::string> parts;
1231 rtc::split(*it, '=', &parts);
1232 ASSERT_EQ(2U, parts.size());
1233 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1234 }
1235
1236 EXPECT_EQ(kIceUfrag0, args["username"]);
1237 EXPECT_EQ(kIcePwd0, args["password"]);
1238}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001239
1240// Tests that destroying ports with non-shared sockets does not crash.
1241// b/19074679.
1242TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1243 AddInterface(kClientAddr);
1244 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1245 session_->StartGettingPorts();
1246 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1247 EXPECT_EQ(4U, ports_.size());
1248
1249 auto it = ports_.begin();
1250 for (; it != ports_.end(); ++it) {
1251 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1252 }
1253}
honghaizf421bdc2015-07-17 16:21:55 -07001254
1255class AllocationSequenceForTest : public cricket::AllocationSequence {
1256 public:
1257 AllocationSequenceForTest(cricket::BasicPortAllocatorSession* session,
1258 rtc::Network* network,
1259 cricket::PortConfiguration* config,
1260 uint32 flags)
1261 : cricket::AllocationSequence(session, network, config, flags) {}
1262 using cricket::AllocationSequence::CreateTurnPort;
1263};
1264
1265TEST_F(PortAllocatorTest, TestCreateTurnPortWithNullSocket) {
1266 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1267 session_->StartGettingPorts();
1268
1269 cricket::ServerAddresses stun_servers;
1270 stun_servers.insert(kStunAddr);
1271 cricket::PortConfiguration config(stun_servers, kIceUfrag0, kIcePwd0);
1272 rtc::Network network1("test_eth0", "Test Network Adapter 1",
1273 rtc::IPAddress(0x12345600U), 24);
1274 uint32 flag = cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
1275 AllocationSequenceForTest alloc_sequence(
1276 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1277 &network1, &config, flag);
1278 // This simply tests it will not crash if udp_socket_ in the
1279 // AllocationSequence is null, which is chosen in the constructor.
1280 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1281 relay_server.ports.push_back(
1282 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1283 alloc_sequence.CreateTurnPort(relay_server);
1284}