blob: 928b45235356e3e48fa43269844517ff05e9b563 [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,
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700246 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700247 const rtc::IPAddress& stun_candidate_addr,
248 const rtc::IPAddress& relay_candidate_udp_transport_addr,
249 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700250 if (!session_) {
251 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
252 }
253 session_->set_flags(session_->flags() |
254 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700255 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
256 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
257 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000258 session_->StartGettingPorts();
259 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
260
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700261 uint32 total_candidates = 0;
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700262 if (!host_candidate_addr.IsNil()) {
263 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
264 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
265 rtc::SocketAddress(host_candidate_addr, 0));
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700266 ++total_candidates;
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700267 }
268 if (!stun_candidate_addr.IsNil()) {
269 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700270 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
271 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700272 EXPECT_EQ(rtc::EmptySocketAddressWithFamily(
273 candidates_[total_candidates].address().family()),
274 candidates_[total_candidates].related_address());
275 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700276 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700277 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700278 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700279 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
280 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700281 EXPECT_EQ(stun_candidate_addr,
282 candidates_[total_candidates].related_address().ipaddr());
283 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700284 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700285 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700286 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700287 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
288 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700289 EXPECT_EQ(stun_candidate_addr,
290 candidates_[total_candidates].related_address().ipaddr());
291 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700292 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000293
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700294 EXPECT_EQ(total_candidates, candidates_.size());
295 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000296 }
297
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000298 protected:
299 cricket::BasicPortAllocator& allocator() {
300 return *allocator_;
301 }
302
303 void OnPortReady(cricket::PortAllocatorSession* ses,
304 cricket::PortInterface* port) {
305 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
306 ports_.push_back(port);
307 }
308 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
309 const std::vector<cricket::Candidate>& candidates) {
310 for (size_t i = 0; i < candidates.size(); ++i) {
311 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
312 candidates_.push_back(candidates[i]);
313 }
314 }
315
316 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
317 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
318 cricket::RelayServerConfig server_config = allocator_->relays()[i];
319 cricket::PortList::const_iterator relay_port;
320 for (relay_port = server_config.ports.begin();
321 relay_port != server_config.ports.end(); ++relay_port) {
322 if (proto_addr.address == relay_port->address &&
323 proto_addr.proto == relay_port->proto)
324 return true;
325 }
326 }
327 return false;
328 }
329
Guo-wei Shieh11477022015-08-15 09:28:41 -0700330 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
331 bool with_nat) {
332 if (with_nat) {
333 nat_server_.reset(new rtc::NATServer(
334 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
335 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
336 } else {
337 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
338 }
339
340 ServerAddresses stun_servers;
341 if (!stun_server.IsNil()) {
342 stun_servers.insert(stun_server);
343 }
344 allocator_.reset(new cricket::BasicPortAllocator(
345 &network_manager_, nat_socket_factory_.get(), stun_servers));
346 allocator().set_step_delay(cricket::kMinimumStepDelay);
347 }
348
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000349 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
350 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
351 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
352 rtc::SocketServerScope ss_scope_;
353 rtc::scoped_ptr<rtc::NATServer> nat_server_;
354 rtc::NATSocketFactory nat_factory_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700355 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000356 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
357 cricket::TestRelayServer relay_server_;
358 cricket::TestTurnServer turn_server_;
359 rtc::FakeNetworkManager network_manager_;
360 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
361 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
362 std::vector<cricket::PortInterface*> ports_;
363 std::vector<cricket::Candidate> candidates_;
364 bool candidate_allocation_done_;
365};
366
367// Tests that we can init the port allocator and create a session.
368TEST_F(PortAllocatorTest, TestBasic) {
369 EXPECT_EQ(&network_manager_, allocator().network_manager());
370 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
371 ASSERT_EQ(1u, allocator().relays().size());
372 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
373 // Empty relay credentials are used for GTURN.
374 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
375 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
376 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
377 kRelayUdpIntAddr, cricket::PROTO_UDP)));
378 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
379 kRelayTcpIntAddr, cricket::PROTO_TCP)));
380 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
381 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
382 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
383}
384
385// Tests that we allocator session not trying to allocate ports for every 250ms.
386TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
387 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
388 session_->StartGettingPorts();
389 // Waiting for one second to make sure BasicPortAllocatorSession has not
390 // called OnAllocate multiple times. In old behavior it's called every 250ms.
391 // When there are no network interfaces, each execution of OnAllocate will
392 // result in SignalCandidatesAllocationDone signal.
393 rtc::Thread::Current()->ProcessMessages(1000);
394 EXPECT_TRUE(candidate_allocation_done_);
395 EXPECT_EQ(0U, candidates_.size());
396}
397
398// Tests that we can get all the desired addresses successfully.
399TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
400 AddInterface(kClientAddr);
401 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
402 session_->StartGettingPorts();
403 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
404 EXPECT_EQ(4U, ports_.size());
405 EXPECT_PRED5(CheckCandidate, candidates_[0],
406 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
407 EXPECT_PRED5(CheckCandidate, candidates_[1],
408 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
409 EXPECT_PRED5(CheckCandidate, candidates_[2],
410 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
411 EXPECT_PRED5(CheckCandidate, candidates_[3],
412 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
413 EXPECT_PRED5(CheckCandidate, candidates_[4],
414 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
415 EXPECT_PRED5(CheckCandidate, candidates_[5],
416 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
417 EXPECT_PRED5(CheckCandidate, candidates_[6],
418 cricket::ICE_CANDIDATE_COMPONENT_RTP,
419 "relay", "ssltcp", kRelaySslTcpIntAddr);
420 EXPECT_TRUE(candidate_allocation_done_);
421}
422
423// Verify candidates with default step delay of 1sec.
424TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
425 AddInterface(kClientAddr);
426 allocator_->set_step_delay(cricket::kDefaultStepDelay);
427 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
428 session_->StartGettingPorts();
429 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
430 EXPECT_EQ(2U, ports_.size());
431 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
432 EXPECT_EQ(3U, ports_.size());
433 EXPECT_PRED5(CheckCandidate, candidates_[2],
434 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
435 EXPECT_PRED5(CheckCandidate, candidates_[3],
436 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
437 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
438 EXPECT_PRED5(CheckCandidate, candidates_[4],
439 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
440 EXPECT_PRED5(CheckCandidate, candidates_[5],
441 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
442 EXPECT_EQ(4U, ports_.size());
443 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
444 EXPECT_PRED5(CheckCandidate, candidates_[6],
445 cricket::ICE_CANDIDATE_COMPONENT_RTP,
446 "relay", "ssltcp", kRelaySslTcpIntAddr);
447 EXPECT_EQ(4U, ports_.size());
448 EXPECT_TRUE(candidate_allocation_done_);
449 // If we Stop gathering now, we shouldn't get a second "done" callback.
450 session_->StopGettingPorts();
451}
452
453TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
454 AddInterface(kClientAddr);
455 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
456 cricket::CN_VIDEO));
457 session_->StartGettingPorts();
458 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
459 EXPECT_TRUE(candidate_allocation_done_);
460 // If we Stop gathering now, we shouldn't get a second "done" callback.
461 session_->StopGettingPorts();
462
463 // All ports should have unset send-buffer sizes.
464 CheckSendBufferSizesOfAllPorts(-1);
465}
466
467// Tests that we can get callback after StopGetAllPorts.
468TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
469 AddInterface(kClientAddr);
470 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
471 session_->StartGettingPorts();
472 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
473 EXPECT_EQ(2U, ports_.size());
474 session_->StopGettingPorts();
475 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
476}
477
478// Test that we restrict client ports appropriately when a port range is set.
479// We check the candidates for udp/stun/tcp ports, and the from address
480// for relay ports.
481TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
482 AddInterface(kClientAddr);
483 // Check that an invalid port range fails.
484 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
485 // Check that a null port range succeeds.
486 EXPECT_TRUE(SetPortRange(0, 0));
487 // Check that a valid port range succeeds.
488 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
489 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
490 session_->StartGettingPorts();
491 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
492 EXPECT_EQ(4U, ports_.size());
493 // Check the port number for the UDP port object.
494 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
495 // Check the port number for the STUN port object.
496 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
497 // Check the port number used to connect to the relay server.
498 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
499 kMinPort, kMaxPort);
500 // Check the port number for the TCP port object.
501 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
502 EXPECT_TRUE(candidate_allocation_done_);
503}
504
505// Test that we don't crash or malfunction if we have no network adapters.
506TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
507 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
508 session_->StartGettingPorts();
509 rtc::Thread::Current()->ProcessMessages(100);
510 // Without network adapter, we should not get any candidate.
511 EXPECT_EQ(0U, candidates_.size());
512 EXPECT_TRUE(candidate_allocation_done_);
513}
514
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000515// Test that we should only get STUN and TURN candidates when adapter
516// enumeration is disabled.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700517TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000518 AddInterface(kClientAddr);
519 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700520 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000521 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700522 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
523 // TURN/UDP candidates.
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700524 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700525 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000526}
527
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700528// Test that even with multiple interfaces, the result should still be one STUN
529// and one TURN candidate since we bind to any address (i.e. all 0s).
530TEST_F(PortAllocatorTest,
531 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000532 AddInterface(kPrivateAddr);
533 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700534 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000535 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700536 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
537 // TURN/UDP candidates.
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700538 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700539 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
540}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000541
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700542// Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a
543// TURN/TCP server is specified.
544TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
545 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
546 AddInterface(kClientAddr);
547 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700548 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700549 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
550 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN,
551 // TURN/UDP, and TURN/TCP candidates.
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700552 CheckDisableAdapterEnumeration(4U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700553 kTurnUdpExtAddr.ipaddr(),
554 kTurnUdpExtAddr.ipaddr());
555}
556
557// Test that we should only get STUN and TURN candidates when adapter
558// enumeration is disabled. Since the endpoint is not behind NAT, the srflx
559// address should be the public client interface.
560TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) {
561 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700562 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700563 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
564 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and
565 // TURN candidates. The STUN candidate should have kClientAddr as srflx
566 // address, and TURN candidate with kClientAddr as the related address.
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700567 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kClientAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700568 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
569}
570
571// Test that when adapter enumeration is disabled, for endpoints without
572// STUN/TURN specified, no candidate is generated.
573TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
574 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700575 ResetWithNoServersOrNat();
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700576 // Expect to see 2 ports: STUN and TCP ports, but no candidate.
577 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shieh0a2955f2015-08-18 13:05:20 -0700578 rtc::IPAddress(), rtc::IPAddress());
579}
580
581// Test that when adapter enumeration is disabled, with
582// PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
583// a NAT, there are a localhost candidate in addition to a STUN candidate.
584TEST_F(PortAllocatorTest,
585 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateRequested) {
586 AddInterfaceAsDefaultRoute(kClientAddr);
587 ResetWithStunServerNoNat(kStunAddr);
588 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
589 session_->set_flags(cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
590 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
591 // candidate.
592 CheckDisableAdapterEnumeration(2U, rtc::GetLoopbackIP(AF_INET),
593 kClientAddr.ipaddr(), rtc::IPAddress(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700594 rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000595}
596
Erik Språngefdce692015-06-05 09:41:26 +0200597// Disable for asan, see
598// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
599#if !defined(ADDRESS_SANITIZER)
600
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000601// Test that we can get OnCandidatesAllocationDone callback when all the ports
602// are disabled.
603TEST_F(PortAllocatorTest, TestDisableAllPorts) {
604 AddInterface(kClientAddr);
605 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
606 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
607 cricket::PORTALLOCATOR_DISABLE_STUN |
608 cricket::PORTALLOCATOR_DISABLE_RELAY |
609 cricket::PORTALLOCATOR_DISABLE_TCP);
610 session_->StartGettingPorts();
611 rtc::Thread::Current()->ProcessMessages(100);
612 EXPECT_EQ(0U, candidates_.size());
613 EXPECT_TRUE(candidate_allocation_done_);
614}
615
616// Test that we don't crash or malfunction if we can't create UDP sockets.
617TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
618 AddInterface(kClientAddr);
619 fss_->set_udp_sockets_enabled(false);
620 EXPECT_TRUE(CreateSession(1));
621 session_->StartGettingPorts();
622 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
623 EXPECT_EQ(2U, ports_.size());
624 EXPECT_PRED5(CheckCandidate, candidates_[0],
625 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
626 EXPECT_PRED5(CheckCandidate, candidates_[1],
627 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
628 EXPECT_PRED5(CheckCandidate, candidates_[2],
629 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
630 EXPECT_PRED5(CheckCandidate, candidates_[3],
631 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
632 EXPECT_PRED5(CheckCandidate, candidates_[4],
633 cricket::ICE_CANDIDATE_COMPONENT_RTP,
634 "relay", "ssltcp", kRelaySslTcpIntAddr);
635 EXPECT_TRUE(candidate_allocation_done_);
636}
637
Erik Språngefdce692015-06-05 09:41:26 +0200638#endif // if !defined(ADDRESS_SANITIZER)
639
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000640// Test that we don't crash or malfunction if we can't create UDP sockets or
641// listen on TCP sockets. We still give out a local TCP address, since
642// apparently this is needed for the remote side to accept our connection.
643TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
644 AddInterface(kClientAddr);
645 fss_->set_udp_sockets_enabled(false);
646 fss_->set_tcp_listen_enabled(false);
647 EXPECT_TRUE(CreateSession(1));
648 session_->StartGettingPorts();
649 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
650 EXPECT_EQ(2U, ports_.size());
651 EXPECT_PRED5(CheckCandidate, candidates_[0],
652 1, "relay", "udp", kRelayUdpIntAddr);
653 EXPECT_PRED5(CheckCandidate, candidates_[1],
654 1, "relay", "udp", kRelayUdpExtAddr);
655 EXPECT_PRED5(CheckCandidate, candidates_[2],
656 1, "relay", "tcp", kRelayTcpIntAddr);
657 EXPECT_PRED5(CheckCandidate, candidates_[3],
658 1, "local", "tcp", kClientAddr);
659 EXPECT_PRED5(CheckCandidate, candidates_[4],
660 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
661 EXPECT_TRUE(candidate_allocation_done_);
662}
663
664// Test that we don't crash or malfunction if we can't create any sockets.
665// TODO: Find a way to exit early here.
666TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
667 AddInterface(kClientAddr);
668 fss_->set_tcp_sockets_enabled(false);
669 fss_->set_udp_sockets_enabled(false);
670 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
671 session_->StartGettingPorts();
672 WAIT(candidates_.size() > 0, 2000);
673 // TODO - Check candidate_allocation_done signal.
674 // In case of Relay, ports creation will succeed but sockets will fail.
675 // There is no error reporting from RelayEntry to handle this failure.
676}
677
678// Testing STUN timeout.
679TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
680 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
681 AddInterface(kClientAddr);
682 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
683 session_->StartGettingPorts();
684 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
685 EXPECT_EQ(2U, ports_.size());
686 EXPECT_PRED5(CheckCandidate, candidates_[0],
687 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
688 EXPECT_PRED5(CheckCandidate, candidates_[1],
689 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
690 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
691 // will be tried after 3 seconds.
692 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
693 EXPECT_EQ(3U, ports_.size());
694 EXPECT_PRED5(CheckCandidate, candidates_[2],
695 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
696 EXPECT_PRED5(CheckCandidate, candidates_[3],
697 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
698 EXPECT_PRED5(CheckCandidate, candidates_[4],
699 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
700 kRelaySslTcpIntAddr);
701 EXPECT_PRED5(CheckCandidate, candidates_[5],
702 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
703 // Stun Timeout is 9sec.
704 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
705}
706
707TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
708 AddInterface(kClientAddr);
709 AddInterface(kClientAddr2);
710 // Allocating only host UDP ports. This is done purely for testing
711 // convenience.
712 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
713 cricket::PORTALLOCATOR_DISABLE_STUN |
714 cricket::PORTALLOCATOR_DISABLE_RELAY);
715 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
716 session_->StartGettingPorts();
717 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
718 ASSERT_EQ(2U, candidates_.size());
719 EXPECT_EQ(2U, ports_.size());
720 // Candidates priorities should be different.
721 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
722}
723
724// Test to verify ICE restart process.
725TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
726 AddInterface(kClientAddr);
727 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
728 session_->StartGettingPorts();
729 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
730 EXPECT_EQ(4U, ports_.size());
731 EXPECT_TRUE(candidate_allocation_done_);
732 // TODO - Extend this to verify ICE restart.
733}
734
735// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
736// This test also verifies that when the allocator is only allowed to use
737// relay (i.e. IceTransportsType is relay), the raddr is an empty
738// address with the correct family. This is to prevent any local
739// reflective address leakage in the sdp line.
740TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
741 AddInterface(kClientAddr);
742 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700743 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000744 allocator().set_candidate_filter(cricket::CF_RELAY);
745 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
746 session_->StartGettingPorts();
747 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
748 EXPECT_PRED5(CheckCandidate,
749 candidates_[0],
750 cricket::ICE_CANDIDATE_COMPONENT_RTP,
751 "relay",
752 "udp",
753 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
754
755 EXPECT_EQ(1U, candidates_.size());
756 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
757 for (size_t i = 0; i < candidates_.size(); ++i) {
758 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
759 EXPECT_EQ(
760 candidates_[0].related_address(),
761 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
762 }
763}
764
765TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
766 AddInterface(kClientAddr);
pthatcherfa301802015-08-11 04:12:56 -0700767 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
768 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000769 allocator().set_candidate_filter(cricket::CF_HOST);
770 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
771 session_->StartGettingPorts();
772 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
773 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
774 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
775 for (size_t i = 0; i < candidates_.size(); ++i) {
776 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
777 }
778}
779
780// Host is behind the NAT.
781TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
782 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700783 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000784
pthatcherfa301802015-08-11 04:12:56 -0700785 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
786 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000787 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
788 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
789 session_->StartGettingPorts();
790 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
791 // Host is behind NAT, no private address will be exposed. Hence only UDP
792 // port with STUN candidate will be sent outside.
793 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
794 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
795 for (size_t i = 0; i < candidates_.size(); ++i) {
796 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
797 EXPECT_EQ(
798 candidates_[0].related_address(),
799 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
800 }
801}
802
803// Host is not behind the NAT.
804TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
805 AddInterface(kClientAddr);
pthatcherfa301802015-08-11 04:12:56 -0700806 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
807 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000808 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
809 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
810 session_->StartGettingPorts();
811 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
812 // Host has a public address, both UDP and TCP candidates will be exposed.
813 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
814 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
815 for (size_t i = 0; i < candidates_.size(); ++i) {
816 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
817 }
818}
819
pthatcherfa301802015-08-11 04:12:56 -0700820// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
821// ufrag and pwd for the collected candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000822TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
pthatcherfa301802015-08-11 04:12:56 -0700823 allocator().set_flags(allocator().flags() |
824 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000825 AddInterface(kClientAddr);
826 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
827 session_->StartGettingPorts();
828 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
829 EXPECT_PRED5(CheckCandidate, candidates_[0],
830 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
831 EXPECT_PRED5(CheckCandidate, candidates_[1],
832 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
833 EXPECT_PRED5(CheckCandidate, candidates_[5],
834 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
835 EXPECT_EQ(4U, ports_.size());
836 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
837 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
838 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
839 EXPECT_EQ(kIcePwd0, candidates_[0].password());
840 EXPECT_EQ(kIcePwd0, candidates_[1].password());
841 EXPECT_TRUE(candidate_allocation_done_);
842}
843
pthatcherfa301802015-08-11 04:12:56 -0700844// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
845// different ufrag and pwd for the collected candidates.
846TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
847 allocator().set_flags(allocator().flags() &
848 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
849 AddInterface(kClientAddr);
850 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
851 session_->StartGettingPorts();
852 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
853 EXPECT_PRED5(CheckCandidate, candidates_[0],
854 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
855 EXPECT_PRED5(CheckCandidate, candidates_[1],
856 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
857 EXPECT_EQ(4U, ports_.size());
858 // Port should generate random ufrag and pwd.
859 EXPECT_NE(kIceUfrag0, candidates_[0].username());
860 EXPECT_NE(kIceUfrag0, candidates_[1].username());
861 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
862 EXPECT_NE(kIcePwd0, candidates_[0].password());
863 EXPECT_NE(kIcePwd0, candidates_[1].password());
864 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
865 EXPECT_TRUE(candidate_allocation_done_);
866}
867
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000868// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
869// is allocated for udp and stun. Also verify there is only one candidate
870// (local) if stun candidate is same as local candidate, which will be the case
871// in a public network like the below test.
872TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
873 AddInterface(kClientAddr);
874 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700875 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000876 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
877 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
878 session_->StartGettingPorts();
879 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
880 EXPECT_EQ(3U, ports_.size());
881 EXPECT_PRED5(CheckCandidate, candidates_[0],
882 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
883 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
884}
885
886// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
887// is allocated for udp and stun. In this test we should expect both stun and
888// local candidates as client behind a nat.
889TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
890 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700891 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000892
893 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700894 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000895 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
896 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
897 session_->StartGettingPorts();
898 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
899 ASSERT_EQ(2U, 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, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700904 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000905 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
906 EXPECT_EQ(3U, candidates_.size());
907}
908
deadbeefc5d0d952015-07-16 10:22:21 -0700909// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000910TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
911 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
912 AddInterface(kClientAddr);
913 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
914
915 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
916
917 allocator_->set_step_delay(cricket::kMinimumStepDelay);
918 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700919 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
921 cricket::PORTALLOCATOR_DISABLE_TCP);
922
923 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
924 session_->StartGettingPorts();
925
926 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
927 ASSERT_EQ(3U, ports_.size());
928 EXPECT_PRED5(CheckCandidate, candidates_[0],
929 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
930 EXPECT_PRED5(CheckCandidate, candidates_[1],
931 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
932 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
933 EXPECT_PRED5(CheckCandidate, candidates_[2],
934 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
935 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
936 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
937 EXPECT_EQ(3U, candidates_.size());
938}
939
940// Testing DNS resolve for the TURN server, this will test AllocationSequence
941// handling the unresolved address signal from TurnPort.
942TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
943 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
944 cricket::PROTO_UDP);
945 AddInterface(kClientAddr);
946 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
947 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
948 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
949 relay_server.credentials = credentials;
950 relay_server.ports.push_back(cricket::ProtocolAddress(
951 rtc::SocketAddress("localhost", 3478),
952 cricket::PROTO_UDP, false));
953 allocator_->AddRelay(relay_server);
954
955 allocator_->set_step_delay(cricket::kMinimumStepDelay);
956 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700957 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000958 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
959 cricket::PORTALLOCATOR_DISABLE_TCP);
960
961 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
962 session_->StartGettingPorts();
963
964 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
965}
966
967// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
968// is allocated for udp/stun/turn. In this test we should expect all local,
969// stun and turn candidates.
970TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
971 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700972 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000973
974 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
975
976 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -0700977 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000978 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
979 cricket::PORTALLOCATOR_DISABLE_TCP);
980
981 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
982 session_->StartGettingPorts();
983
984 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
985 ASSERT_EQ(2U, ports_.size());
986 EXPECT_PRED5(CheckCandidate, candidates_[0],
987 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
988 EXPECT_PRED5(CheckCandidate, candidates_[1],
989 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700990 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 EXPECT_PRED5(CheckCandidate, candidates_[2],
992 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
993 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
994 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
995 EXPECT_EQ(3U, candidates_.size());
996 // Local port will be created first and then TURN port.
997 EXPECT_EQ(2U, ports_[0]->Candidates().size());
998 EXPECT_EQ(1U, ports_[1]->Candidates().size());
999}
1000
1001// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1002// server is also used as the STUN server, we should get 'local', 'stun', and
1003// 'relay' candidates.
1004TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1005 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001006 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001007 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001008 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1009
1010 // Must set the step delay to 0 to make sure the relay allocation phase is
1011 // started before the STUN candidates are obtained, so that the STUN binding
1012 // response is processed when both StunPort and TurnPort exist to reproduce
1013 // webrtc issue 3537.
1014 allocator_->set_step_delay(0);
1015 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001016 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001017 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1018 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 EXPECT_PRED5(CheckCandidate, candidates_[0],
1025 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1026 EXPECT_PRED5(CheckCandidate, candidates_[1],
1027 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001028 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001029 EXPECT_PRED5(CheckCandidate, candidates_[2],
1030 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1031 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1032 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1033
1034 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1035 EXPECT_EQ(3U, candidates_.size());
1036 // Local port will be created first and then TURN port.
1037 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1038 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1039}
1040
deadbeefc5d0d952015-07-16 10:22:21 -07001041// Test that when only a TCP TURN server is available, we do NOT use it as
1042// a UDP STUN server, as this could leak our IP address. Thus we should only
1043// expect two ports, a UDPPort and TurnPort.
1044TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1045 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1046 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001047 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001048 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1049
1050 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001051 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001052 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1053 cricket::PORTALLOCATOR_DISABLE_TCP);
1054
1055 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1056 session_->StartGettingPorts();
1057
1058 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1059 ASSERT_EQ(2U, ports_.size());
1060 EXPECT_PRED5(CheckCandidate, candidates_[0],
1061 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1062 kClientAddr);
1063 EXPECT_PRED5(CheckCandidate, candidates_[1],
1064 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1065 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1066 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1067 EXPECT_EQ(2U, candidates_.size());
1068 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1069 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1070}
1071
1072// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1073// TURN server is used as the STUN server and we get 'local', 'stun', and
1074// 'relay' candidates.
1075// TODO(deadbeef): Remove this test when support for non-shared socket mode
1076// is removed.
1077TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1078 AddInterface(kClientAddr);
1079 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001080 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001081 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1082
1083 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001084 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001085 cricket::PORTALLOCATOR_DISABLE_TCP);
1086
1087 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1088 session_->StartGettingPorts();
1089
1090 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1091 ASSERT_EQ(3U, ports_.size());
1092 EXPECT_PRED5(CheckCandidate, candidates_[0],
1093 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1094 kClientAddr);
1095 EXPECT_PRED5(CheckCandidate, candidates_[1],
1096 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1097 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1098 EXPECT_PRED5(CheckCandidate, candidates_[2],
1099 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1100 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1101 // Not using shared socket, so the STUN request's server reflexive address
1102 // should be different than the TURN request's server reflexive address.
1103 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1104
1105 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1106 EXPECT_EQ(3U, candidates_.size());
1107 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1108 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1109 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1110}
1111
1112// Test that even when both a STUN and TURN server are configured, the TURN
1113// server is used as a STUN server and we get a 'stun' candidate.
1114TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1115 AddInterface(kClientAddr);
1116 // Configure with STUN server but destroy it, so we can ensure that it's
1117 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001118 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001119 stun_server_.reset();
1120 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1121
1122 allocator_->set_flags(allocator().flags() |
pthatcherfa301802015-08-11 04:12:56 -07001123 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001124 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1125 cricket::PORTALLOCATOR_DISABLE_TCP);
1126
1127 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1128 session_->StartGettingPorts();
1129
1130 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1131 EXPECT_PRED5(CheckCandidate, candidates_[0],
1132 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1133 kClientAddr);
1134 EXPECT_PRED5(CheckCandidate, candidates_[1],
1135 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1136 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1137 EXPECT_PRED5(CheckCandidate, candidates_[2],
1138 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1139 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1140 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1141
1142 // Don't bother waiting for STUN timeout, since we already verified
1143 // that we got a STUN candidate from the TURN server.
1144}
1145
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001146// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1147// and fail to generate STUN candidate, local UDP candidate is generated
1148// properly.
1149TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1150 allocator().set_flags(allocator().flags() |
1151 cricket::PORTALLOCATOR_DISABLE_RELAY |
1152 cricket::PORTALLOCATOR_DISABLE_TCP |
pthatcherfa301802015-08-11 04:12:56 -07001153 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001154 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1155 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1156 AddInterface(kClientAddr);
1157 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1158 session_->StartGettingPorts();
1159 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1160 EXPECT_EQ(1U, candidates_.size());
1161 EXPECT_PRED5(CheckCandidate, candidates_[0],
1162 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1163 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1164 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1165 EXPECT_EQ(1U, candidates_.size());
1166}
1167
1168// This test verifies allocator can use IPv6 addresses along with IPv4.
1169TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1170 allocator().set_flags(allocator().flags() |
1171 cricket::PORTALLOCATOR_DISABLE_RELAY |
1172 cricket::PORTALLOCATOR_ENABLE_IPV6 |
pthatcherfa301802015-08-11 04:12:56 -07001173 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001174 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1175 AddInterface(kClientIPv6Addr);
1176 AddInterface(kClientAddr);
1177 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1178 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1179 session_->StartGettingPorts();
1180 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1181 EXPECT_EQ(4U, candidates_.size());
1182 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1183 EXPECT_PRED5(CheckCandidate, candidates_[0],
1184 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1185 kClientIPv6Addr);
1186 EXPECT_PRED5(CheckCandidate, candidates_[1],
1187 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1188 kClientAddr);
1189 EXPECT_PRED5(CheckCandidate, candidates_[2],
1190 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1191 kClientIPv6Addr);
1192 EXPECT_PRED5(CheckCandidate, candidates_[3],
1193 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1194 kClientAddr);
1195 EXPECT_EQ(4U, candidates_.size());
1196}
1197
1198// Test that the httpportallocator correctly maintains its lists of stun and
1199// relay servers, by never allowing an empty list.
1200TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1201 rtc::FakeNetworkManager network_manager;
1202 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1203 EXPECT_EQ(1U, alloc.relay_hosts().size());
1204 EXPECT_EQ(1U, alloc.stun_hosts().size());
1205
1206 std::vector<std::string> relay_servers;
1207 std::vector<rtc::SocketAddress> stun_servers;
1208
1209 alloc.SetRelayHosts(relay_servers);
1210 alloc.SetStunHosts(stun_servers);
1211 EXPECT_EQ(1U, alloc.relay_hosts().size());
1212 EXPECT_EQ(1U, alloc.stun_hosts().size());
1213
1214 relay_servers.push_back("1.unittest.corp.google.com");
1215 relay_servers.push_back("2.unittest.corp.google.com");
1216 stun_servers.push_back(
1217 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1218 stun_servers.push_back(
1219 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1220
1221 alloc.SetRelayHosts(relay_servers);
1222 alloc.SetStunHosts(stun_servers);
1223 EXPECT_EQ(2U, alloc.relay_hosts().size());
1224 EXPECT_EQ(2U, alloc.stun_hosts().size());
1225}
1226
1227// Test that the HttpPortAllocator uses correct URL to create sessions.
1228TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1229 rtc::FakeNetworkManager network_manager;
1230 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1231
pthatcherfa301802015-08-11 04:12:56 -07001232 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1233 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001234 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1235 static_cast<cricket::HttpPortAllocatorSession*>(
1236 alloc.CreateSessionInternal(
1237 "test content", 0, kIceUfrag0, kIcePwd0)));
1238 std::string url = session->GetSessionRequestUrl();
1239 LOG(LS_INFO) << "url: " << url;
pthatcherfa301802015-08-11 04:12:56 -07001240 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
1241
1242 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1243 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1244 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
1245 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
1246 url = session->GetSessionRequestUrl();
1247 LOG(LS_INFO) << "url: " << url;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001248 std::vector<std::string> parts;
1249 rtc::split(url, '?', &parts);
1250 ASSERT_EQ(2U, parts.size());
1251
1252 std::vector<std::string> args_parts;
1253 rtc::split(parts[1], '&', &args_parts);
1254
1255 std::map<std::string, std::string> args;
1256 for (std::vector<std::string>::iterator it = args_parts.begin();
1257 it != args_parts.end(); ++it) {
1258 std::vector<std::string> parts;
1259 rtc::split(*it, '=', &parts);
1260 ASSERT_EQ(2U, parts.size());
1261 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1262 }
1263
1264 EXPECT_EQ(kIceUfrag0, args["username"]);
1265 EXPECT_EQ(kIcePwd0, args["password"]);
1266}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001267
1268// Tests that destroying ports with non-shared sockets does not crash.
1269// b/19074679.
1270TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1271 AddInterface(kClientAddr);
1272 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1273 session_->StartGettingPorts();
1274 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1275 EXPECT_EQ(4U, ports_.size());
1276
1277 auto it = ports_.begin();
1278 for (; it != ports_.end(); ++it) {
1279 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1280 }
1281}
honghaizf421bdc2015-07-17 16:21:55 -07001282
1283class AllocationSequenceForTest : public cricket::AllocationSequence {
1284 public:
1285 AllocationSequenceForTest(cricket::BasicPortAllocatorSession* session,
1286 rtc::Network* network,
1287 cricket::PortConfiguration* config,
1288 uint32 flags)
1289 : cricket::AllocationSequence(session, network, config, flags) {}
1290 using cricket::AllocationSequence::CreateTurnPort;
1291};
1292
1293TEST_F(PortAllocatorTest, TestCreateTurnPortWithNullSocket) {
1294 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1295 session_->StartGettingPorts();
1296
1297 cricket::ServerAddresses stun_servers;
1298 stun_servers.insert(kStunAddr);
1299 cricket::PortConfiguration config(stun_servers, kIceUfrag0, kIcePwd0);
1300 rtc::Network network1("test_eth0", "Test Network Adapter 1",
1301 rtc::IPAddress(0x12345600U), 24);
1302 uint32 flag = cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
1303 AllocationSequenceForTest alloc_sequence(
1304 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1305 &network1, &config, flag);
1306 // This simply tests it will not crash if udp_socket_ in the
1307 // AllocationSequence is null, which is chosen in the constructor.
1308 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1309 relay_server.ports.push_back(
1310 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1311 alloc_sequence.CreateTurnPort(relay_server);
1312}