blob: b6de0c215ad5551f3641ff1c56ae317decd01f00 [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);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070039static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040static const SocketAddress kPrivateAddr("192.168.1.11", 0);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000041static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042static const SocketAddress kClientIPv6Addr(
43 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
44static const SocketAddress kClientAddr2("22.22.22.22", 0);
deadbeefc5d0d952015-07-16 10:22:21 -070045static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
46static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
48static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
49static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
50static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
51static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
52static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
53static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
54static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
55static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
56static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
57static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
58
59// Minimum and maximum port for port range tests.
60static const int kMinPort = 10000;
61static const int kMaxPort = 10099;
62
63// Based on ICE_UFRAG_LENGTH
64static const char kIceUfrag0[] = "TESTICEUFRAG0000";
65// Based on ICE_PWD_LENGTH
66static const char kIcePwd0[] = "TESTICEPWD00000000000000";
67
68static const char kContentName[] = "test content";
69
70static const int kDefaultAllocationTimeout = 1000;
71static const char kTurnUsername[] = "test";
72static const char kTurnPassword[] = "test";
73
74namespace cricket {
75
76// Helper for dumping candidates
77std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
78 os << c.ToString();
79 return os;
80}
81
82} // namespace cricket
83
84class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
85 public:
86 PortAllocatorTest()
87 : pss_(new rtc::PhysicalSocketServer),
88 vss_(new rtc::VirtualSocketServer(pss_.get())),
89 fss_(new rtc::FirewallSocketServer(vss_.get())),
90 ss_scope_(fss_.get()),
deadbeefc5d0d952015-07-16 10:22:21 -070091 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
Guo-wei Shieh38f88932015-08-13 22:24:02 -070092 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 stun_server_(cricket::TestStunServer::Create(Thread::Current(),
94 kStunAddr)),
95 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
96 kRelayTcpIntAddr, kRelayTcpExtAddr,
97 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
98 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
99 candidate_allocation_done_(false) {
100 cricket::ServerAddresses stun_servers;
101 stun_servers.insert(kStunAddr);
102 // Passing the addresses of GTURN servers will enable GTURN in
103 // Basicportallocator.
104 allocator_.reset(new cricket::BasicPortAllocator(
105 &network_manager_,
106 stun_servers,
107 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
108 allocator_->set_step_delay(cricket::kMinimumStepDelay);
109 }
110
111 void AddInterface(const SocketAddress& addr) {
112 network_manager_.AddInterface(addr);
113 }
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700114 void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
115 AddInterface(addr);
116 // When a binding comes from the any address, the |addr| will be used as the
117 // srflx address.
118 vss_->SetDefaultRoute(addr.ipaddr());
119 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120 bool SetPortRange(int min_port, int max_port) {
121 return allocator_->SetPortRange(min_port, max_port);
122 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700123 // Endpoint is on the public network. No STUN or TURN.
124 void ResetWithNoServersOrNat() {
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700125 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
126 allocator_->set_step_delay(cricket::kMinimumStepDelay);
127 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700128 // Endpoint is behind a NAT, with STUN specified.
129 void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
130 ResetWithStunServer(stun_server, true);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700131 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700132 // Endpoint is on the public network, with STUN specified.
133 void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
134 ResetWithStunServer(stun_server, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700136 // Endpoint is on the public network, with TURN specified.
137 void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
138 const rtc::SocketAddress& tcp_turn) {
139 ResetWithNoServersOrNat();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 AddTurnServers(udp_turn, tcp_turn);
141 }
142
143 void AddTurnServers(const rtc::SocketAddress& udp_turn,
144 const rtc::SocketAddress& tcp_turn) {
145 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
146 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
147 relay_server.credentials = credentials;
148
149 if (!udp_turn.IsNil()) {
150 relay_server.ports.push_back(cricket::ProtocolAddress(
151 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
152 }
153 if (!tcp_turn.IsNil()) {
154 relay_server.ports.push_back(cricket::ProtocolAddress(
155 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
156 }
157 allocator_->AddRelay(relay_server);
158 }
159
160 bool CreateSession(int component) {
161 session_.reset(CreateSession("session", component));
162 if (!session_)
163 return false;
164 return true;
165 }
166
167 bool CreateSession(int component, const std::string& content_name) {
168 session_.reset(CreateSession("session", content_name, component));
169 if (!session_)
170 return false;
171 return true;
172 }
173
174 cricket::PortAllocatorSession* CreateSession(
175 const std::string& sid, int component) {
176 return CreateSession(sid, kContentName, component);
177 }
178
179 cricket::PortAllocatorSession* CreateSession(
180 const std::string& sid, const std::string& content_name, int component) {
181 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
182 }
183
184 cricket::PortAllocatorSession* CreateSession(
185 const std::string& sid, const std::string& content_name, int component,
186 const std::string& ice_ufrag, const std::string& ice_pwd) {
187 cricket::PortAllocatorSession* session =
188 allocator_->CreateSession(
189 sid, content_name, component, ice_ufrag, ice_pwd);
190 session->SignalPortReady.connect(this,
191 &PortAllocatorTest::OnPortReady);
192 session->SignalCandidatesReady.connect(this,
193 &PortAllocatorTest::OnCandidatesReady);
194 session->SignalCandidatesAllocationDone.connect(this,
195 &PortAllocatorTest::OnCandidatesAllocationDone);
196 return session;
197 }
198
199 static bool CheckCandidate(const cricket::Candidate& c,
200 int component, const std::string& type,
201 const std::string& proto,
202 const SocketAddress& addr) {
203 return (c.component() == component && c.type() == type &&
204 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
205 ((addr.port() == 0 && (c.address().port() != 0)) ||
206 (c.address().port() == addr.port())));
207 }
208 static bool CheckPort(const rtc::SocketAddress& addr,
209 int min_port, int max_port) {
210 return (addr.port() >= min_port && addr.port() <= max_port);
211 }
212
213 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
214 // We should only get this callback once, except in the mux test where
215 // we have multiple port allocation sessions.
216 if (session == session_.get()) {
217 ASSERT_FALSE(candidate_allocation_done_);
218 candidate_allocation_done_ = true;
219 }
220 }
221
222 // Check if all ports allocated have send-buffer size |expected|. If
223 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
224 void CheckSendBufferSizesOfAllPorts(int expected) {
225 std::vector<cricket::PortInterface*>::iterator it;
226 for (it = ports_.begin(); it < ports_.end(); ++it) {
227 int send_buffer_size;
228 if (expected == -1) {
229 EXPECT_EQ(SOCKET_ERROR,
230 (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
231 &send_buffer_size));
232 } else {
233 EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
234 &send_buffer_size));
235 ASSERT_EQ(expected, send_buffer_size);
236 }
237 }
238 }
239
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700240 // This function starts the port/address gathering and check the existence of
241 // candidates as specified. When |expect_stun_candidate| is true,
242 // |stun_candidate_addr| carries the expected reflective address, which is
243 // also the related address for TURN candidate if it is expected. Otherwise,
244 // it should be ignore.
245 void CheckDisableAdapterEnumeration(
246 uint32 total_ports,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700247 const rtc::IPAddress& host_candidate_addr,
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700248 const rtc::IPAddress& stun_candidate_addr,
249 const rtc::IPAddress& relay_candidate_udp_transport_addr,
250 const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700251 if (!session_) {
252 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
253 }
254 session_->set_flags(session_->flags() |
255 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
minyuel5bdafd42015-08-21 15:52:48 +0200256 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700257 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
258 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000259 session_->StartGettingPorts();
260 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
261
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700262 uint32 total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700263 if (!host_candidate_addr.IsNil()) {
264 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
265 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
266 rtc::SocketAddress(host_candidate_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700267 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700268 }
269 if (!stun_candidate_addr.IsNil()) {
270 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700271 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
272 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700273 EXPECT_EQ(rtc::EmptySocketAddressWithFamily(
274 candidates_[total_candidates].address().family()),
275 candidates_[total_candidates].related_address());
276 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700277 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700278 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700279 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700280 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
281 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700282 EXPECT_EQ(stun_candidate_addr,
283 candidates_[total_candidates].related_address().ipaddr());
284 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700285 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700286 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700287 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700288 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
289 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700290 EXPECT_EQ(stun_candidate_addr,
291 candidates_[total_candidates].related_address().ipaddr());
292 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700293 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000294
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700295 EXPECT_EQ(total_candidates, candidates_.size());
296 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000297 }
298
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000299 protected:
300 cricket::BasicPortAllocator& allocator() {
301 return *allocator_;
302 }
303
304 void OnPortReady(cricket::PortAllocatorSession* ses,
305 cricket::PortInterface* port) {
306 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
307 ports_.push_back(port);
308 }
309 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
310 const std::vector<cricket::Candidate>& candidates) {
311 for (size_t i = 0; i < candidates.size(); ++i) {
312 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
313 candidates_.push_back(candidates[i]);
314 }
315 }
316
317 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
318 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
319 cricket::RelayServerConfig server_config = allocator_->relays()[i];
320 cricket::PortList::const_iterator relay_port;
321 for (relay_port = server_config.ports.begin();
322 relay_port != server_config.ports.end(); ++relay_port) {
323 if (proto_addr.address == relay_port->address &&
324 proto_addr.proto == relay_port->proto)
325 return true;
326 }
327 }
328 return false;
329 }
330
Guo-wei Shieh11477022015-08-15 09:28:41 -0700331 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
332 bool with_nat) {
333 if (with_nat) {
334 nat_server_.reset(new rtc::NATServer(
335 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
336 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
337 } else {
338 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
339 }
340
341 ServerAddresses stun_servers;
342 if (!stun_server.IsNil()) {
343 stun_servers.insert(stun_server);
344 }
345 allocator_.reset(new cricket::BasicPortAllocator(
346 &network_manager_, nat_socket_factory_.get(), stun_servers));
347 allocator().set_step_delay(cricket::kMinimumStepDelay);
348 }
349
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000350 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
351 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
352 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
353 rtc::SocketServerScope ss_scope_;
354 rtc::scoped_ptr<rtc::NATServer> nat_server_;
355 rtc::NATSocketFactory nat_factory_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700356 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000357 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
358 cricket::TestRelayServer relay_server_;
359 cricket::TestTurnServer turn_server_;
360 rtc::FakeNetworkManager network_manager_;
361 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
362 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
363 std::vector<cricket::PortInterface*> ports_;
364 std::vector<cricket::Candidate> candidates_;
365 bool candidate_allocation_done_;
366};
367
368// Tests that we can init the port allocator and create a session.
369TEST_F(PortAllocatorTest, TestBasic) {
370 EXPECT_EQ(&network_manager_, allocator().network_manager());
371 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
372 ASSERT_EQ(1u, allocator().relays().size());
373 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
374 // Empty relay credentials are used for GTURN.
375 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
376 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
377 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
378 kRelayUdpIntAddr, cricket::PROTO_UDP)));
379 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
380 kRelayTcpIntAddr, cricket::PROTO_TCP)));
381 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
382 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
383 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
384}
385
386// Tests that we allocator session not trying to allocate ports for every 250ms.
387TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
388 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
389 session_->StartGettingPorts();
390 // Waiting for one second to make sure BasicPortAllocatorSession has not
391 // called OnAllocate multiple times. In old behavior it's called every 250ms.
392 // When there are no network interfaces, each execution of OnAllocate will
393 // result in SignalCandidatesAllocationDone signal.
394 rtc::Thread::Current()->ProcessMessages(1000);
395 EXPECT_TRUE(candidate_allocation_done_);
396 EXPECT_EQ(0U, candidates_.size());
397}
398
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700399// Test that we could use loopback interface as host candidate.
400TEST_F(PortAllocatorTest, TestLoopbackNetworkInterface) {
401 AddInterface(kLoopbackAddr);
402 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
403 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
404 cricket::PORTALLOCATOR_DISABLE_RELAY |
405 cricket::PORTALLOCATOR_DISABLE_TCP);
406 session_->StartGettingPorts();
407 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
408 EXPECT_EQ(1U, candidates_.size());
409}
410
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411// Tests that we can get all the desired addresses successfully.
412TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
413 AddInterface(kClientAddr);
414 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
415 session_->StartGettingPorts();
416 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
417 EXPECT_EQ(4U, ports_.size());
418 EXPECT_PRED5(CheckCandidate, candidates_[0],
419 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
420 EXPECT_PRED5(CheckCandidate, candidates_[1],
421 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
422 EXPECT_PRED5(CheckCandidate, candidates_[2],
423 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
424 EXPECT_PRED5(CheckCandidate, candidates_[3],
425 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
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_PRED5(CheckCandidate, candidates_[6],
431 cricket::ICE_CANDIDATE_COMPONENT_RTP,
432 "relay", "ssltcp", kRelaySslTcpIntAddr);
433 EXPECT_TRUE(candidate_allocation_done_);
434}
435
436// Verify candidates with default step delay of 1sec.
437TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
438 AddInterface(kClientAddr);
439 allocator_->set_step_delay(cricket::kDefaultStepDelay);
440 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
441 session_->StartGettingPorts();
442 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
443 EXPECT_EQ(2U, ports_.size());
444 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
445 EXPECT_EQ(3U, ports_.size());
446 EXPECT_PRED5(CheckCandidate, candidates_[2],
447 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
448 EXPECT_PRED5(CheckCandidate, candidates_[3],
449 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
450 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
451 EXPECT_PRED5(CheckCandidate, candidates_[4],
452 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
453 EXPECT_PRED5(CheckCandidate, candidates_[5],
454 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
455 EXPECT_EQ(4U, ports_.size());
456 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
457 EXPECT_PRED5(CheckCandidate, candidates_[6],
458 cricket::ICE_CANDIDATE_COMPONENT_RTP,
459 "relay", "ssltcp", kRelaySslTcpIntAddr);
460 EXPECT_EQ(4U, ports_.size());
461 EXPECT_TRUE(candidate_allocation_done_);
462 // If we Stop gathering now, we shouldn't get a second "done" callback.
463 session_->StopGettingPorts();
464}
465
466TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
467 AddInterface(kClientAddr);
468 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
469 cricket::CN_VIDEO));
470 session_->StartGettingPorts();
471 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
472 EXPECT_TRUE(candidate_allocation_done_);
473 // If we Stop gathering now, we shouldn't get a second "done" callback.
474 session_->StopGettingPorts();
475
476 // All ports should have unset send-buffer sizes.
477 CheckSendBufferSizesOfAllPorts(-1);
478}
479
480// Tests that we can get callback after StopGetAllPorts.
481TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
482 AddInterface(kClientAddr);
483 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
484 session_->StartGettingPorts();
485 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
486 EXPECT_EQ(2U, ports_.size());
487 session_->StopGettingPorts();
488 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
489}
490
491// Test that we restrict client ports appropriately when a port range is set.
492// We check the candidates for udp/stun/tcp ports, and the from address
493// for relay ports.
494TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
495 AddInterface(kClientAddr);
496 // Check that an invalid port range fails.
497 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
498 // Check that a null port range succeeds.
499 EXPECT_TRUE(SetPortRange(0, 0));
500 // Check that a valid port range succeeds.
501 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
502 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
503 session_->StartGettingPorts();
504 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
505 EXPECT_EQ(4U, ports_.size());
506 // Check the port number for the UDP port object.
507 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
508 // Check the port number for the STUN port object.
509 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
510 // Check the port number used to connect to the relay server.
511 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
512 kMinPort, kMaxPort);
513 // Check the port number for the TCP port object.
514 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
515 EXPECT_TRUE(candidate_allocation_done_);
516}
517
518// Test that we don't crash or malfunction if we have no network adapters.
519TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
520 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
521 session_->StartGettingPorts();
522 rtc::Thread::Current()->ProcessMessages(100);
523 // Without network adapter, we should not get any candidate.
524 EXPECT_EQ(0U, candidates_.size());
525 EXPECT_TRUE(candidate_allocation_done_);
526}
527
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000528// Test that we should only get STUN and TURN candidates when adapter
529// enumeration is disabled.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700530TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000531 AddInterface(kClientAddr);
532 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700533 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000534 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700535 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
536 // TURN/UDP candidates.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700537 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700538 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000539}
540
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700541// Test that even with multiple interfaces, the result should still be one STUN
542// and one TURN candidate since we bind to any address (i.e. all 0s).
543TEST_F(PortAllocatorTest,
544 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000545 AddInterface(kPrivateAddr);
546 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700547 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000548 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700549 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
550 // TURN/UDP candidates.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700551 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700552 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
553}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000554
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700555// Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a
556// TURN/TCP server is specified.
557TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
558 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
559 AddInterface(kClientAddr);
560 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700561 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700562 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
563 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN,
564 // TURN/UDP, and TURN/TCP candidates.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700565 CheckDisableAdapterEnumeration(4U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700566 kTurnUdpExtAddr.ipaddr(),
567 kTurnUdpExtAddr.ipaddr());
568}
569
570// Test that we should only get STUN and TURN candidates when adapter
571// enumeration is disabled. Since the endpoint is not behind NAT, the srflx
572// address should be the public client interface.
573TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) {
574 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700575 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700576 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
577 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and
578 // TURN candidates. The STUN candidate should have kClientAddr as srflx
579 // address, and TURN candidate with kClientAddr as the related address.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700580 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kClientAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700581 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
582}
583
584// Test that when adapter enumeration is disabled, for endpoints without
585// STUN/TURN specified, no candidate is generated.
586TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
587 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700588 ResetWithNoServersOrNat();
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700589 // Expect to see 2 ports: STUN and TCP ports, but no candidate.
590 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700591 rtc::IPAddress(), rtc::IPAddress());
592}
593
594// Test that when adapter enumeration is disabled, with
595// PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
596// a NAT, there are a localhost candidate in addition to a STUN candidate.
597TEST_F(PortAllocatorTest,
598 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateRequested) {
599 AddInterfaceAsDefaultRoute(kClientAddr);
600 ResetWithStunServerNoNat(kStunAddr);
601 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
602 session_->set_flags(cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
603 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
604 // candidate.
605 CheckDisableAdapterEnumeration(2U, rtc::GetLoopbackIP(AF_INET),
606 kClientAddr.ipaddr(), rtc::IPAddress(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700607 rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000608}
609
Erik Språngefdce692015-06-05 09:41:26 +0200610// Disable for asan, see
611// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
612#if !defined(ADDRESS_SANITIZER)
613
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000614// Test that we can get OnCandidatesAllocationDone callback when all the ports
615// are disabled.
616TEST_F(PortAllocatorTest, TestDisableAllPorts) {
617 AddInterface(kClientAddr);
618 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
619 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
620 cricket::PORTALLOCATOR_DISABLE_STUN |
621 cricket::PORTALLOCATOR_DISABLE_RELAY |
622 cricket::PORTALLOCATOR_DISABLE_TCP);
623 session_->StartGettingPorts();
624 rtc::Thread::Current()->ProcessMessages(100);
625 EXPECT_EQ(0U, candidates_.size());
626 EXPECT_TRUE(candidate_allocation_done_);
627}
628
629// Test that we don't crash or malfunction if we can't create UDP sockets.
630TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
631 AddInterface(kClientAddr);
632 fss_->set_udp_sockets_enabled(false);
633 EXPECT_TRUE(CreateSession(1));
634 session_->StartGettingPorts();
635 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
636 EXPECT_EQ(2U, ports_.size());
637 EXPECT_PRED5(CheckCandidate, candidates_[0],
638 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
639 EXPECT_PRED5(CheckCandidate, candidates_[1],
640 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
641 EXPECT_PRED5(CheckCandidate, candidates_[2],
642 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
643 EXPECT_PRED5(CheckCandidate, candidates_[3],
644 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
645 EXPECT_PRED5(CheckCandidate, candidates_[4],
646 cricket::ICE_CANDIDATE_COMPONENT_RTP,
647 "relay", "ssltcp", kRelaySslTcpIntAddr);
648 EXPECT_TRUE(candidate_allocation_done_);
649}
650
Erik Språngefdce692015-06-05 09:41:26 +0200651#endif // if !defined(ADDRESS_SANITIZER)
652
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000653// Test that we don't crash or malfunction if we can't create UDP sockets or
654// listen on TCP sockets. We still give out a local TCP address, since
655// apparently this is needed for the remote side to accept our connection.
656TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
657 AddInterface(kClientAddr);
658 fss_->set_udp_sockets_enabled(false);
659 fss_->set_tcp_listen_enabled(false);
660 EXPECT_TRUE(CreateSession(1));
661 session_->StartGettingPorts();
662 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
663 EXPECT_EQ(2U, ports_.size());
664 EXPECT_PRED5(CheckCandidate, candidates_[0],
665 1, "relay", "udp", kRelayUdpIntAddr);
666 EXPECT_PRED5(CheckCandidate, candidates_[1],
667 1, "relay", "udp", kRelayUdpExtAddr);
668 EXPECT_PRED5(CheckCandidate, candidates_[2],
669 1, "relay", "tcp", kRelayTcpIntAddr);
670 EXPECT_PRED5(CheckCandidate, candidates_[3],
671 1, "local", "tcp", kClientAddr);
672 EXPECT_PRED5(CheckCandidate, candidates_[4],
673 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
674 EXPECT_TRUE(candidate_allocation_done_);
675}
676
677// Test that we don't crash or malfunction if we can't create any sockets.
678// TODO: Find a way to exit early here.
679TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
680 AddInterface(kClientAddr);
681 fss_->set_tcp_sockets_enabled(false);
682 fss_->set_udp_sockets_enabled(false);
683 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
684 session_->StartGettingPorts();
685 WAIT(candidates_.size() > 0, 2000);
686 // TODO - Check candidate_allocation_done signal.
687 // In case of Relay, ports creation will succeed but sockets will fail.
688 // There is no error reporting from RelayEntry to handle this failure.
689}
690
691// Testing STUN timeout.
692TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
693 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
694 AddInterface(kClientAddr);
695 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
696 session_->StartGettingPorts();
697 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
698 EXPECT_EQ(2U, ports_.size());
699 EXPECT_PRED5(CheckCandidate, candidates_[0],
700 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
701 EXPECT_PRED5(CheckCandidate, candidates_[1],
702 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
703 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
704 // will be tried after 3 seconds.
705 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
706 EXPECT_EQ(3U, ports_.size());
707 EXPECT_PRED5(CheckCandidate, candidates_[2],
708 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
709 EXPECT_PRED5(CheckCandidate, candidates_[3],
710 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
711 EXPECT_PRED5(CheckCandidate, candidates_[4],
712 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
713 kRelaySslTcpIntAddr);
714 EXPECT_PRED5(CheckCandidate, candidates_[5],
715 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
716 // Stun Timeout is 9sec.
717 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
718}
719
720TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
721 AddInterface(kClientAddr);
722 AddInterface(kClientAddr2);
723 // Allocating only host UDP ports. This is done purely for testing
724 // convenience.
725 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
726 cricket::PORTALLOCATOR_DISABLE_STUN |
727 cricket::PORTALLOCATOR_DISABLE_RELAY);
728 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
729 session_->StartGettingPorts();
730 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
731 ASSERT_EQ(2U, candidates_.size());
732 EXPECT_EQ(2U, ports_.size());
733 // Candidates priorities should be different.
734 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
735}
736
737// Test to verify ICE restart process.
738TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
739 AddInterface(kClientAddr);
740 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
741 session_->StartGettingPorts();
742 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
743 EXPECT_EQ(4U, ports_.size());
744 EXPECT_TRUE(candidate_allocation_done_);
745 // TODO - Extend this to verify ICE restart.
746}
747
748// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
749// This test also verifies that when the allocator is only allowed to use
750// relay (i.e. IceTransportsType is relay), the raddr is an empty
751// address with the correct family. This is to prevent any local
752// reflective address leakage in the sdp line.
753TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
754 AddInterface(kClientAddr);
755 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700756 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000757 allocator().set_candidate_filter(cricket::CF_RELAY);
758 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
759 session_->StartGettingPorts();
760 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
761 EXPECT_PRED5(CheckCandidate,
762 candidates_[0],
763 cricket::ICE_CANDIDATE_COMPONENT_RTP,
764 "relay",
765 "udp",
766 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
767
768 EXPECT_EQ(1U, candidates_.size());
769 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
770 for (size_t i = 0; i < candidates_.size(); ++i) {
771 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
772 EXPECT_EQ(
773 candidates_[0].related_address(),
774 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
775 }
776}
777
778TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
779 AddInterface(kClientAddr);
minyuel5bdafd42015-08-21 15:52:48 +0200780 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
781 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000782 allocator().set_candidate_filter(cricket::CF_HOST);
783 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
784 session_->StartGettingPorts();
785 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
786 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
787 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
788 for (size_t i = 0; i < candidates_.size(); ++i) {
789 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
790 }
791}
792
793// Host is behind the NAT.
794TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
795 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700796 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000797
minyuel5bdafd42015-08-21 15:52:48 +0200798 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
799 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000800 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
801 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
802 session_->StartGettingPorts();
803 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
804 // Host is behind NAT, no private address will be exposed. Hence only UDP
805 // port with STUN candidate will be sent outside.
806 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
807 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
808 for (size_t i = 0; i < candidates_.size(); ++i) {
809 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
810 EXPECT_EQ(
811 candidates_[0].related_address(),
812 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
813 }
814}
815
816// Host is not behind the NAT.
817TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
818 AddInterface(kClientAddr);
minyuel5bdafd42015-08-21 15:52:48 +0200819 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
820 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000821 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
822 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
823 session_->StartGettingPorts();
824 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
825 // Host has a public address, both UDP and TCP candidates will be exposed.
826 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
827 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
828 for (size_t i = 0; i < candidates_.size(); ++i) {
829 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
830 }
831}
832
minyuel5bdafd42015-08-21 15:52:48 +0200833// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
834// ufrag and pwd for the collected candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000835TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
minyuel5bdafd42015-08-21 15:52:48 +0200836 allocator().set_flags(allocator().flags() |
837 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000838 AddInterface(kClientAddr);
839 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
840 session_->StartGettingPorts();
841 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
842 EXPECT_PRED5(CheckCandidate, candidates_[0],
843 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
844 EXPECT_PRED5(CheckCandidate, candidates_[1],
845 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
846 EXPECT_PRED5(CheckCandidate, candidates_[5],
847 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
848 EXPECT_EQ(4U, ports_.size());
849 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
850 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
851 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
852 EXPECT_EQ(kIcePwd0, candidates_[0].password());
853 EXPECT_EQ(kIcePwd0, candidates_[1].password());
854 EXPECT_TRUE(candidate_allocation_done_);
855}
856
minyuel5bdafd42015-08-21 15:52:48 +0200857// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
858// different ufrag and pwd for the collected candidates.
859TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
860 allocator().set_flags(allocator().flags() &
861 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
862 AddInterface(kClientAddr);
863 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
864 session_->StartGettingPorts();
865 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
866 EXPECT_PRED5(CheckCandidate, candidates_[0],
867 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
868 EXPECT_PRED5(CheckCandidate, candidates_[1],
869 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
870 EXPECT_EQ(4U, ports_.size());
871 // Port should generate random ufrag and pwd.
872 EXPECT_NE(kIceUfrag0, candidates_[0].username());
873 EXPECT_NE(kIceUfrag0, candidates_[1].username());
874 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
875 EXPECT_NE(kIcePwd0, candidates_[0].password());
876 EXPECT_NE(kIcePwd0, candidates_[1].password());
877 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
878 EXPECT_TRUE(candidate_allocation_done_);
879}
880
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000881// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
882// is allocated for udp and stun. Also verify there is only one candidate
883// (local) if stun candidate is same as local candidate, which will be the case
884// in a public network like the below test.
885TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
886 AddInterface(kClientAddr);
887 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +0200888 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000889 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
890 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
891 session_->StartGettingPorts();
892 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
893 EXPECT_EQ(3U, ports_.size());
894 EXPECT_PRED5(CheckCandidate, candidates_[0],
895 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
896 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
897}
898
899// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
900// is allocated for udp and stun. In this test we should expect both stun and
901// local candidates as client behind a nat.
902TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
903 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700904 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000905
906 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +0200907 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000908 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
909 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
910 session_->StartGettingPorts();
911 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
912 ASSERT_EQ(2U, ports_.size());
913 EXPECT_PRED5(CheckCandidate, candidates_[0],
914 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
915 EXPECT_PRED5(CheckCandidate, candidates_[1],
916 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700917 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000918 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
919 EXPECT_EQ(3U, candidates_.size());
920}
921
deadbeefc5d0d952015-07-16 10:22:21 -0700922// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000923TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
924 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
925 AddInterface(kClientAddr);
926 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
927
928 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
929
930 allocator_->set_step_delay(cricket::kMinimumStepDelay);
931 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +0200932 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
934 cricket::PORTALLOCATOR_DISABLE_TCP);
935
936 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
937 session_->StartGettingPorts();
938
939 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
940 ASSERT_EQ(3U, ports_.size());
941 EXPECT_PRED5(CheckCandidate, candidates_[0],
942 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
943 EXPECT_PRED5(CheckCandidate, candidates_[1],
944 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
945 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
946 EXPECT_PRED5(CheckCandidate, candidates_[2],
947 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
948 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
949 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
950 EXPECT_EQ(3U, candidates_.size());
951}
952
953// Testing DNS resolve for the TURN server, this will test AllocationSequence
954// handling the unresolved address signal from TurnPort.
955TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
956 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
957 cricket::PROTO_UDP);
958 AddInterface(kClientAddr);
959 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
960 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
961 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
962 relay_server.credentials = credentials;
963 relay_server.ports.push_back(cricket::ProtocolAddress(
964 rtc::SocketAddress("localhost", 3478),
965 cricket::PROTO_UDP, false));
966 allocator_->AddRelay(relay_server);
967
968 allocator_->set_step_delay(cricket::kMinimumStepDelay);
969 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +0200970 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000971 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
972 cricket::PORTALLOCATOR_DISABLE_TCP);
973
974 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
975 session_->StartGettingPorts();
976
977 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
978}
979
980// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
981// is allocated for udp/stun/turn. In this test we should expect all local,
982// stun and turn candidates.
983TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
984 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700985 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000986
987 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
988
989 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +0200990 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
992 cricket::PORTALLOCATOR_DISABLE_TCP);
993
994 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
995 session_->StartGettingPorts();
996
997 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
998 ASSERT_EQ(2U, ports_.size());
999 EXPECT_PRED5(CheckCandidate, candidates_[0],
1000 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1001 EXPECT_PRED5(CheckCandidate, candidates_[1],
1002 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001003 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001004 EXPECT_PRED5(CheckCandidate, candidates_[2],
1005 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1006 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1007 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1008 EXPECT_EQ(3U, candidates_.size());
1009 // Local port will be created first and then TURN port.
1010 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1011 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1012}
1013
1014// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1015// server is also used as the STUN server, we should get 'local', 'stun', and
1016// 'relay' candidates.
1017TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1018 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001019 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001020 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001021 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1022
1023 // Must set the step delay to 0 to make sure the relay allocation phase is
1024 // started before the STUN candidates are obtained, so that the STUN binding
1025 // response is processed when both StunPort and TurnPort exist to reproduce
1026 // webrtc issue 3537.
1027 allocator_->set_step_delay(0);
1028 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +02001029 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001030 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1031 cricket::PORTALLOCATOR_DISABLE_TCP);
1032
1033 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1034 session_->StartGettingPorts();
1035
1036 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1037 EXPECT_PRED5(CheckCandidate, candidates_[0],
1038 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1039 EXPECT_PRED5(CheckCandidate, candidates_[1],
1040 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001041 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001042 EXPECT_PRED5(CheckCandidate, candidates_[2],
1043 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1044 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1045 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1046
1047 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1048 EXPECT_EQ(3U, candidates_.size());
1049 // Local port will be created first and then TURN port.
1050 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1051 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1052}
1053
deadbeefc5d0d952015-07-16 10:22:21 -07001054// Test that when only a TCP TURN server is available, we do NOT use it as
1055// a UDP STUN server, as this could leak our IP address. Thus we should only
1056// expect two ports, a UDPPort and TurnPort.
1057TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1058 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1059 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001060 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001061 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1062
1063 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +02001064 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001065 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1066 cricket::PORTALLOCATOR_DISABLE_TCP);
1067
1068 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1069 session_->StartGettingPorts();
1070
1071 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1072 ASSERT_EQ(2U, ports_.size());
1073 EXPECT_PRED5(CheckCandidate, candidates_[0],
1074 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1075 kClientAddr);
1076 EXPECT_PRED5(CheckCandidate, candidates_[1],
1077 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1078 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1079 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1080 EXPECT_EQ(2U, candidates_.size());
1081 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1082 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1083}
1084
1085// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1086// TURN server is used as the STUN server and we get 'local', 'stun', and
1087// 'relay' candidates.
1088// TODO(deadbeef): Remove this test when support for non-shared socket mode
1089// is removed.
1090TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1091 AddInterface(kClientAddr);
1092 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001093 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001094 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1095
1096 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +02001097 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001098 cricket::PORTALLOCATOR_DISABLE_TCP);
1099
1100 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1101 session_->StartGettingPorts();
1102
1103 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1104 ASSERT_EQ(3U, ports_.size());
1105 EXPECT_PRED5(CheckCandidate, candidates_[0],
1106 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1107 kClientAddr);
1108 EXPECT_PRED5(CheckCandidate, candidates_[1],
1109 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1110 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1111 EXPECT_PRED5(CheckCandidate, candidates_[2],
1112 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1113 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1114 // Not using shared socket, so the STUN request's server reflexive address
1115 // should be different than the TURN request's server reflexive address.
1116 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1117
1118 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1119 EXPECT_EQ(3U, candidates_.size());
1120 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1121 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1122 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1123}
1124
1125// Test that even when both a STUN and TURN server are configured, the TURN
1126// server is used as a STUN server and we get a 'stun' candidate.
1127TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1128 AddInterface(kClientAddr);
1129 // Configure with STUN server but destroy it, so we can ensure that it's
1130 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001131 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001132 stun_server_.reset();
1133 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1134
1135 allocator_->set_flags(allocator().flags() |
minyuel5bdafd42015-08-21 15:52:48 +02001136 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001137 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1138 cricket::PORTALLOCATOR_DISABLE_TCP);
1139
1140 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1141 session_->StartGettingPorts();
1142
1143 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1144 EXPECT_PRED5(CheckCandidate, candidates_[0],
1145 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1146 kClientAddr);
1147 EXPECT_PRED5(CheckCandidate, candidates_[1],
1148 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1149 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1150 EXPECT_PRED5(CheckCandidate, candidates_[2],
1151 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1152 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1153 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1154
1155 // Don't bother waiting for STUN timeout, since we already verified
1156 // that we got a STUN candidate from the TURN server.
1157}
1158
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1160// and fail to generate STUN candidate, local UDP candidate is generated
1161// properly.
1162TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1163 allocator().set_flags(allocator().flags() |
1164 cricket::PORTALLOCATOR_DISABLE_RELAY |
1165 cricket::PORTALLOCATOR_DISABLE_TCP |
minyuel5bdafd42015-08-21 15:52:48 +02001166 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001167 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1168 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1169 AddInterface(kClientAddr);
1170 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1171 session_->StartGettingPorts();
1172 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1173 EXPECT_EQ(1U, candidates_.size());
1174 EXPECT_PRED5(CheckCandidate, candidates_[0],
1175 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1176 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1177 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1178 EXPECT_EQ(1U, candidates_.size());
1179}
1180
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001181// Test that when the NetworkManager doesn't have permission to enumerate
1182// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1183// automatically.
1184TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
1185 AddInterface(kClientAddr);
1186 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001187 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001188 allocator().set_flags(allocator().flags() |
1189 cricket::PORTALLOCATOR_DISABLE_RELAY |
1190 cricket::PORTALLOCATOR_DISABLE_TCP |
minyuel5bdafd42015-08-21 15:52:48 +02001191 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001192 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001193 EXPECT_EQ(0U, allocator_->flags() &
1194 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001195 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001196 EXPECT_EQ(0U, session_->flags() &
1197 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001198 session_->StartGettingPorts();
1199 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1200 EXPECT_EQ(0U, candidates_.size());
1201 EXPECT_TRUE((session_->flags() &
1202 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1203}
1204
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001205// This test verifies allocator can use IPv6 addresses along with IPv4.
1206TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1207 allocator().set_flags(allocator().flags() |
1208 cricket::PORTALLOCATOR_DISABLE_RELAY |
1209 cricket::PORTALLOCATOR_ENABLE_IPV6 |
minyuel5bdafd42015-08-21 15:52:48 +02001210 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001211 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1212 AddInterface(kClientIPv6Addr);
1213 AddInterface(kClientAddr);
1214 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1215 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1216 session_->StartGettingPorts();
1217 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1218 EXPECT_EQ(4U, candidates_.size());
1219 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1220 EXPECT_PRED5(CheckCandidate, candidates_[0],
1221 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1222 kClientIPv6Addr);
1223 EXPECT_PRED5(CheckCandidate, candidates_[1],
1224 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1225 kClientAddr);
1226 EXPECT_PRED5(CheckCandidate, candidates_[2],
1227 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1228 kClientIPv6Addr);
1229 EXPECT_PRED5(CheckCandidate, candidates_[3],
1230 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1231 kClientAddr);
1232 EXPECT_EQ(4U, candidates_.size());
1233}
1234
1235// Test that the httpportallocator correctly maintains its lists of stun and
1236// relay servers, by never allowing an empty list.
1237TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1238 rtc::FakeNetworkManager network_manager;
1239 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1240 EXPECT_EQ(1U, alloc.relay_hosts().size());
1241 EXPECT_EQ(1U, alloc.stun_hosts().size());
1242
1243 std::vector<std::string> relay_servers;
1244 std::vector<rtc::SocketAddress> stun_servers;
1245
1246 alloc.SetRelayHosts(relay_servers);
1247 alloc.SetStunHosts(stun_servers);
1248 EXPECT_EQ(1U, alloc.relay_hosts().size());
1249 EXPECT_EQ(1U, alloc.stun_hosts().size());
1250
1251 relay_servers.push_back("1.unittest.corp.google.com");
1252 relay_servers.push_back("2.unittest.corp.google.com");
1253 stun_servers.push_back(
1254 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1255 stun_servers.push_back(
1256 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1257
1258 alloc.SetRelayHosts(relay_servers);
1259 alloc.SetStunHosts(stun_servers);
1260 EXPECT_EQ(2U, alloc.relay_hosts().size());
1261 EXPECT_EQ(2U, alloc.stun_hosts().size());
1262}
1263
1264// Test that the HttpPortAllocator uses correct URL to create sessions.
1265TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1266 rtc::FakeNetworkManager network_manager;
1267 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1268
minyuel5bdafd42015-08-21 15:52:48 +02001269 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1270 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001271 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1272 static_cast<cricket::HttpPortAllocatorSession*>(
1273 alloc.CreateSessionInternal(
1274 "test content", 0, kIceUfrag0, kIcePwd0)));
1275 std::string url = session->GetSessionRequestUrl();
1276 LOG(LS_INFO) << "url: " << url;
minyuel5bdafd42015-08-21 15:52:48 +02001277 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
1278
1279 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
1280 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
1281 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
1282 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
1283 url = session->GetSessionRequestUrl();
1284 LOG(LS_INFO) << "url: " << url;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001285 std::vector<std::string> parts;
1286 rtc::split(url, '?', &parts);
1287 ASSERT_EQ(2U, parts.size());
1288
1289 std::vector<std::string> args_parts;
1290 rtc::split(parts[1], '&', &args_parts);
1291
1292 std::map<std::string, std::string> args;
1293 for (std::vector<std::string>::iterator it = args_parts.begin();
1294 it != args_parts.end(); ++it) {
1295 std::vector<std::string> parts;
1296 rtc::split(*it, '=', &parts);
1297 ASSERT_EQ(2U, parts.size());
1298 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1299 }
1300
1301 EXPECT_EQ(kIceUfrag0, args["username"]);
1302 EXPECT_EQ(kIcePwd0, args["password"]);
1303}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001304
1305// Tests that destroying ports with non-shared sockets does not crash.
1306// b/19074679.
1307TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1308 AddInterface(kClientAddr);
1309 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1310 session_->StartGettingPorts();
1311 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1312 EXPECT_EQ(4U, ports_.size());
1313
1314 auto it = ports_.begin();
1315 for (; it != ports_.end(); ++it) {
1316 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1317 }
1318}
honghaizf421bdc2015-07-17 16:21:55 -07001319
1320class AllocationSequenceForTest : public cricket::AllocationSequence {
1321 public:
1322 AllocationSequenceForTest(cricket::BasicPortAllocatorSession* session,
1323 rtc::Network* network,
1324 cricket::PortConfiguration* config,
1325 uint32 flags)
1326 : cricket::AllocationSequence(session, network, config, flags) {}
1327 using cricket::AllocationSequence::CreateTurnPort;
1328};
1329
1330TEST_F(PortAllocatorTest, TestCreateTurnPortWithNullSocket) {
1331 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1332 session_->StartGettingPorts();
1333
1334 cricket::ServerAddresses stun_servers;
1335 stun_servers.insert(kStunAddr);
1336 cricket::PortConfiguration config(stun_servers, kIceUfrag0, kIcePwd0);
1337 rtc::Network network1("test_eth0", "Test Network Adapter 1",
1338 rtc::IPAddress(0x12345600U), 24);
1339 uint32 flag = cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
1340 AllocationSequenceForTest alloc_sequence(
1341 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1342 &network1, &config, flag);
1343 // This simply tests it will not crash if udp_socket_ in the
1344 // AllocationSequence is null, which is chosen in the constructor.
1345 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1346 relay_server.ports.push_back(
1347 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1348 alloc_sequence.CreateTurnPort(relay_server);
1349}