blob: 26b66e68aa1a742b62965eb0047216c5ab6bab54 [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 |
guoweisd12140a2015-09-10 13:32:11 -0700256 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
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700610// Test that we disable relay over UDP, and only TCP is used when connecting to
611// the relay server.
612TEST_F(PortAllocatorTest, TestDisableUdpTurn) {
613 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
614 AddInterface(kClientAddr);
615 ResetWithStunServerAndNat(kStunAddr);
616 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
617 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
618 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY |
619 cricket::PORTALLOCATOR_DISABLE_UDP |
620 cricket::PORTALLOCATOR_DISABLE_STUN |
guoweisd12140a2015-09-10 13:32:11 -0700621 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700622 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
623
624 session_->StartGettingPorts();
625 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
626
627 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
628 // TURN/TCP candidates.
629 EXPECT_EQ(2U, ports_.size());
630 EXPECT_EQ(2U, candidates_.size());
631 EXPECT_PRED5(CheckCandidate, candidates_[0],
632 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
633 kTurnUdpExtAddr);
634 // The TURN candidate should use TCP to contact the TURN server.
635 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
636 EXPECT_PRED5(CheckCandidate, candidates_[1],
637 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
638 kClientAddr);
639}
640
Erik Språngefdce692015-06-05 09:41:26 +0200641// Disable for asan, see
642// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
643#if !defined(ADDRESS_SANITIZER)
644
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000645// Test that we can get OnCandidatesAllocationDone callback when all the ports
646// are disabled.
647TEST_F(PortAllocatorTest, TestDisableAllPorts) {
648 AddInterface(kClientAddr);
649 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
650 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
651 cricket::PORTALLOCATOR_DISABLE_STUN |
652 cricket::PORTALLOCATOR_DISABLE_RELAY |
653 cricket::PORTALLOCATOR_DISABLE_TCP);
654 session_->StartGettingPorts();
655 rtc::Thread::Current()->ProcessMessages(100);
656 EXPECT_EQ(0U, candidates_.size());
657 EXPECT_TRUE(candidate_allocation_done_);
658}
659
660// Test that we don't crash or malfunction if we can't create UDP sockets.
661TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
662 AddInterface(kClientAddr);
663 fss_->set_udp_sockets_enabled(false);
664 EXPECT_TRUE(CreateSession(1));
665 session_->StartGettingPorts();
666 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
667 EXPECT_EQ(2U, ports_.size());
668 EXPECT_PRED5(CheckCandidate, candidates_[0],
669 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
670 EXPECT_PRED5(CheckCandidate, candidates_[1],
671 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
672 EXPECT_PRED5(CheckCandidate, candidates_[2],
673 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
674 EXPECT_PRED5(CheckCandidate, candidates_[3],
675 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
676 EXPECT_PRED5(CheckCandidate, candidates_[4],
677 cricket::ICE_CANDIDATE_COMPONENT_RTP,
678 "relay", "ssltcp", kRelaySslTcpIntAddr);
679 EXPECT_TRUE(candidate_allocation_done_);
680}
681
Erik Språngefdce692015-06-05 09:41:26 +0200682#endif // if !defined(ADDRESS_SANITIZER)
683
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000684// Test that we don't crash or malfunction if we can't create UDP sockets or
685// listen on TCP sockets. We still give out a local TCP address, since
686// apparently this is needed for the remote side to accept our connection.
687TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
688 AddInterface(kClientAddr);
689 fss_->set_udp_sockets_enabled(false);
690 fss_->set_tcp_listen_enabled(false);
691 EXPECT_TRUE(CreateSession(1));
692 session_->StartGettingPorts();
693 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
694 EXPECT_EQ(2U, ports_.size());
695 EXPECT_PRED5(CheckCandidate, candidates_[0],
696 1, "relay", "udp", kRelayUdpIntAddr);
697 EXPECT_PRED5(CheckCandidate, candidates_[1],
698 1, "relay", "udp", kRelayUdpExtAddr);
699 EXPECT_PRED5(CheckCandidate, candidates_[2],
700 1, "relay", "tcp", kRelayTcpIntAddr);
701 EXPECT_PRED5(CheckCandidate, candidates_[3],
702 1, "local", "tcp", kClientAddr);
703 EXPECT_PRED5(CheckCandidate, candidates_[4],
704 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
705 EXPECT_TRUE(candidate_allocation_done_);
706}
707
708// Test that we don't crash or malfunction if we can't create any sockets.
709// TODO: Find a way to exit early here.
710TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
711 AddInterface(kClientAddr);
712 fss_->set_tcp_sockets_enabled(false);
713 fss_->set_udp_sockets_enabled(false);
714 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
715 session_->StartGettingPorts();
716 WAIT(candidates_.size() > 0, 2000);
717 // TODO - Check candidate_allocation_done signal.
718 // In case of Relay, ports creation will succeed but sockets will fail.
719 // There is no error reporting from RelayEntry to handle this failure.
720}
721
722// Testing STUN timeout.
723TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
724 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
725 AddInterface(kClientAddr);
726 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
727 session_->StartGettingPorts();
728 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
729 EXPECT_EQ(2U, ports_.size());
730 EXPECT_PRED5(CheckCandidate, candidates_[0],
731 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
732 EXPECT_PRED5(CheckCandidate, candidates_[1],
733 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
734 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
735 // will be tried after 3 seconds.
736 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
737 EXPECT_EQ(3U, ports_.size());
738 EXPECT_PRED5(CheckCandidate, candidates_[2],
739 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
740 EXPECT_PRED5(CheckCandidate, candidates_[3],
741 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
742 EXPECT_PRED5(CheckCandidate, candidates_[4],
743 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
744 kRelaySslTcpIntAddr);
745 EXPECT_PRED5(CheckCandidate, candidates_[5],
746 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
747 // Stun Timeout is 9sec.
748 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
749}
750
751TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
752 AddInterface(kClientAddr);
753 AddInterface(kClientAddr2);
754 // Allocating only host UDP ports. This is done purely for testing
755 // convenience.
756 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
757 cricket::PORTALLOCATOR_DISABLE_STUN |
758 cricket::PORTALLOCATOR_DISABLE_RELAY);
759 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
760 session_->StartGettingPorts();
761 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
762 ASSERT_EQ(2U, candidates_.size());
763 EXPECT_EQ(2U, ports_.size());
764 // Candidates priorities should be different.
765 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
766}
767
768// Test to verify ICE restart process.
769TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
770 AddInterface(kClientAddr);
771 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
772 session_->StartGettingPorts();
773 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
774 EXPECT_EQ(4U, ports_.size());
775 EXPECT_TRUE(candidate_allocation_done_);
776 // TODO - Extend this to verify ICE restart.
777}
778
779// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
780// This test also verifies that when the allocator is only allowed to use
781// relay (i.e. IceTransportsType is relay), the raddr is an empty
782// address with the correct family. This is to prevent any local
783// reflective address leakage in the sdp line.
784TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
785 AddInterface(kClientAddr);
786 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700787 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000788 allocator().set_candidate_filter(cricket::CF_RELAY);
789 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
790 session_->StartGettingPorts();
791 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
792 EXPECT_PRED5(CheckCandidate,
793 candidates_[0],
794 cricket::ICE_CANDIDATE_COMPONENT_RTP,
795 "relay",
796 "udp",
797 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
798
799 EXPECT_EQ(1U, candidates_.size());
800 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
801 for (size_t i = 0; i < candidates_.size(); ++i) {
802 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
803 EXPECT_EQ(
804 candidates_[0].related_address(),
805 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
806 }
807}
808
809TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
810 AddInterface(kClientAddr);
guoweisd12140a2015-09-10 13:32:11 -0700811 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
812 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000813 allocator().set_candidate_filter(cricket::CF_HOST);
814 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
815 session_->StartGettingPorts();
816 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
817 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
818 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
819 for (size_t i = 0; i < candidates_.size(); ++i) {
820 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
821 }
822}
823
824// Host is behind the NAT.
825TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
826 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700827 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000828
guoweisd12140a2015-09-10 13:32:11 -0700829 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
830 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000831 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
832 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
833 session_->StartGettingPorts();
834 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
835 // Host is behind NAT, no private address will be exposed. Hence only UDP
836 // port with STUN candidate will be sent outside.
837 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
838 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
839 for (size_t i = 0; i < candidates_.size(); ++i) {
840 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
841 EXPECT_EQ(
842 candidates_[0].related_address(),
843 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
844 }
845}
846
847// Host is not behind the NAT.
848TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
849 AddInterface(kClientAddr);
guoweisd12140a2015-09-10 13:32:11 -0700850 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
851 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000852 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
853 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
854 session_->StartGettingPorts();
855 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
856 // Host has a public address, both UDP and TCP candidates will be exposed.
857 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
858 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
859 for (size_t i = 0; i < candidates_.size(); ++i) {
860 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
861 }
862}
863
guoweisd12140a2015-09-10 13:32:11 -0700864// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
865// ufrag and pwd for the collected candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000866TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
guoweisd12140a2015-09-10 13:32:11 -0700867 allocator().set_flags(allocator().flags() |
868 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000869 AddInterface(kClientAddr);
870 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
871 session_->StartGettingPorts();
872 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
873 EXPECT_PRED5(CheckCandidate, candidates_[0],
874 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
875 EXPECT_PRED5(CheckCandidate, candidates_[1],
876 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
877 EXPECT_PRED5(CheckCandidate, candidates_[5],
878 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
879 EXPECT_EQ(4U, ports_.size());
880 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
881 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
882 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
883 EXPECT_EQ(kIcePwd0, candidates_[0].password());
884 EXPECT_EQ(kIcePwd0, candidates_[1].password());
885 EXPECT_TRUE(candidate_allocation_done_);
886}
887
guoweisd12140a2015-09-10 13:32:11 -0700888// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
889// different ufrag and pwd for the collected candidates.
890TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
891 allocator().set_flags(allocator().flags() &
892 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
893 AddInterface(kClientAddr);
894 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
895 session_->StartGettingPorts();
896 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
897 EXPECT_PRED5(CheckCandidate, candidates_[0],
898 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
899 EXPECT_PRED5(CheckCandidate, candidates_[1],
900 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
901 EXPECT_EQ(4U, ports_.size());
902 // Port should generate random ufrag and pwd.
903 EXPECT_NE(kIceUfrag0, candidates_[0].username());
904 EXPECT_NE(kIceUfrag0, candidates_[1].username());
905 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
906 EXPECT_NE(kIcePwd0, candidates_[0].password());
907 EXPECT_NE(kIcePwd0, candidates_[1].password());
908 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
909 EXPECT_TRUE(candidate_allocation_done_);
910}
911
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000912// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
913// is allocated for udp and stun. Also verify there is only one candidate
914// (local) if stun candidate is same as local candidate, which will be the case
915// in a public network like the below test.
916TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
917 AddInterface(kClientAddr);
918 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -0700919 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
921 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
922 session_->StartGettingPorts();
923 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
924 EXPECT_EQ(3U, ports_.size());
925 EXPECT_PRED5(CheckCandidate, candidates_[0],
926 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
927 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
928}
929
930// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
931// is allocated for udp and stun. In this test we should expect both stun and
932// local candidates as client behind a nat.
933TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
934 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700935 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936
937 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -0700938 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000939 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
940 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
941 session_->StartGettingPorts();
942 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
943 ASSERT_EQ(2U, ports_.size());
944 EXPECT_PRED5(CheckCandidate, candidates_[0],
945 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
946 EXPECT_PRED5(CheckCandidate, candidates_[1],
947 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700948 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000949 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
950 EXPECT_EQ(3U, candidates_.size());
951}
952
deadbeefc5d0d952015-07-16 10:22:21 -0700953// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000954TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
955 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
956 AddInterface(kClientAddr);
957 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
958
959 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
960
961 allocator_->set_step_delay(cricket::kMinimumStepDelay);
962 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -0700963 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000964 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
965 cricket::PORTALLOCATOR_DISABLE_TCP);
966
967 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
968 session_->StartGettingPorts();
969
970 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
971 ASSERT_EQ(3U, ports_.size());
972 EXPECT_PRED5(CheckCandidate, candidates_[0],
973 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
974 EXPECT_PRED5(CheckCandidate, candidates_[1],
975 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
976 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
977 EXPECT_PRED5(CheckCandidate, candidates_[2],
978 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
979 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
980 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
981 EXPECT_EQ(3U, candidates_.size());
982}
983
984// Testing DNS resolve for the TURN server, this will test AllocationSequence
985// handling the unresolved address signal from TurnPort.
986TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
987 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
988 cricket::PROTO_UDP);
989 AddInterface(kClientAddr);
990 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
991 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
992 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
993 relay_server.credentials = credentials;
994 relay_server.ports.push_back(cricket::ProtocolAddress(
995 rtc::SocketAddress("localhost", 3478),
996 cricket::PROTO_UDP, false));
997 allocator_->AddRelay(relay_server);
998
999 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1000 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -07001001 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001002 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1003 cricket::PORTALLOCATOR_DISABLE_TCP);
1004
1005 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1006 session_->StartGettingPorts();
1007
1008 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1009}
1010
1011// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1012// is allocated for udp/stun/turn. In this test we should expect all local,
1013// stun and turn candidates.
1014TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
1015 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001016 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001017
1018 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1019
1020 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -07001021 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001022 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1023 cricket::PORTALLOCATOR_DISABLE_TCP);
1024
1025 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1026 session_->StartGettingPorts();
1027
1028 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1029 ASSERT_EQ(2U, ports_.size());
1030 EXPECT_PRED5(CheckCandidate, candidates_[0],
1031 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1032 EXPECT_PRED5(CheckCandidate, candidates_[1],
1033 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001034 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001035 EXPECT_PRED5(CheckCandidate, candidates_[2],
1036 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1037 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1038 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1039 EXPECT_EQ(3U, candidates_.size());
1040 // Local port will be created first and then TURN port.
1041 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1042 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1043}
1044
1045// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1046// server is also used as the STUN server, we should get 'local', 'stun', and
1047// 'relay' candidates.
1048TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1049 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001050 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001051 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001052 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1053
1054 // Must set the step delay to 0 to make sure the relay allocation phase is
1055 // started before the STUN candidates are obtained, so that the STUN binding
1056 // response is processed when both StunPort and TurnPort exist to reproduce
1057 // webrtc issue 3537.
1058 allocator_->set_step_delay(0);
1059 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -07001060 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001061 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1062 cricket::PORTALLOCATOR_DISABLE_TCP);
1063
1064 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1065 session_->StartGettingPorts();
1066
1067 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1068 EXPECT_PRED5(CheckCandidate, candidates_[0],
1069 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1070 EXPECT_PRED5(CheckCandidate, candidates_[1],
1071 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001072 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001073 EXPECT_PRED5(CheckCandidate, candidates_[2],
1074 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1075 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1076 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1077
1078 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1079 EXPECT_EQ(3U, candidates_.size());
1080 // Local port will be created first and then TURN port.
1081 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1082 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1083}
1084
deadbeefc5d0d952015-07-16 10:22:21 -07001085// Test that when only a TCP TURN server is available, we do NOT use it as
1086// a UDP STUN server, as this could leak our IP address. Thus we should only
1087// expect two ports, a UDPPort and TurnPort.
1088TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1089 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1090 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001091 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001092 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1093
1094 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -07001095 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001096 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1097 cricket::PORTALLOCATOR_DISABLE_TCP);
1098
1099 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1100 session_->StartGettingPorts();
1101
1102 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1103 ASSERT_EQ(2U, ports_.size());
1104 EXPECT_PRED5(CheckCandidate, candidates_[0],
1105 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1106 kClientAddr);
1107 EXPECT_PRED5(CheckCandidate, candidates_[1],
1108 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1109 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1110 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1111 EXPECT_EQ(2U, candidates_.size());
1112 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1113 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1114}
1115
1116// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1117// TURN server is used as the STUN server and we get 'local', 'stun', and
1118// 'relay' candidates.
1119// TODO(deadbeef): Remove this test when support for non-shared socket mode
1120// is removed.
1121TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1122 AddInterface(kClientAddr);
1123 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001124 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001125 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1126
1127 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -07001128 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001129 cricket::PORTALLOCATOR_DISABLE_TCP);
1130
1131 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1132 session_->StartGettingPorts();
1133
1134 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1135 ASSERT_EQ(3U, ports_.size());
1136 EXPECT_PRED5(CheckCandidate, candidates_[0],
1137 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1138 kClientAddr);
1139 EXPECT_PRED5(CheckCandidate, candidates_[1],
1140 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1141 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1142 EXPECT_PRED5(CheckCandidate, candidates_[2],
1143 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1144 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1145 // Not using shared socket, so the STUN request's server reflexive address
1146 // should be different than the TURN request's server reflexive address.
1147 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1148
1149 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1150 EXPECT_EQ(3U, candidates_.size());
1151 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1152 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1153 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1154}
1155
1156// Test that even when both a STUN and TURN server are configured, the TURN
1157// server is used as a STUN server and we get a 'stun' candidate.
1158TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1159 AddInterface(kClientAddr);
1160 // Configure with STUN server but destroy it, so we can ensure that it's
1161 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001162 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001163 stun_server_.reset();
1164 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1165
1166 allocator_->set_flags(allocator().flags() |
guoweisd12140a2015-09-10 13:32:11 -07001167 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
deadbeefc5d0d952015-07-16 10:22:21 -07001168 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1169 cricket::PORTALLOCATOR_DISABLE_TCP);
1170
1171 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1172 session_->StartGettingPorts();
1173
1174 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1175 EXPECT_PRED5(CheckCandidate, candidates_[0],
1176 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1177 kClientAddr);
1178 EXPECT_PRED5(CheckCandidate, candidates_[1],
1179 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1180 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1181 EXPECT_PRED5(CheckCandidate, candidates_[2],
1182 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1183 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1184 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1185
1186 // Don't bother waiting for STUN timeout, since we already verified
1187 // that we got a STUN candidate from the TURN server.
1188}
1189
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1191// and fail to generate STUN candidate, local UDP candidate is generated
1192// properly.
1193TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1194 allocator().set_flags(allocator().flags() |
1195 cricket::PORTALLOCATOR_DISABLE_RELAY |
1196 cricket::PORTALLOCATOR_DISABLE_TCP |
guoweisd12140a2015-09-10 13:32:11 -07001197 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001198 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1199 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1200 AddInterface(kClientAddr);
1201 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1202 session_->StartGettingPorts();
1203 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1204 EXPECT_EQ(1U, candidates_.size());
1205 EXPECT_PRED5(CheckCandidate, candidates_[0],
1206 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1207 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1208 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1209 EXPECT_EQ(1U, candidates_.size());
1210}
1211
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001212// Test that when the NetworkManager doesn't have permission to enumerate
1213// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1214// automatically.
1215TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
1216 AddInterface(kClientAddr);
1217 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001218 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001219 allocator().set_flags(allocator().flags() |
1220 cricket::PORTALLOCATOR_DISABLE_RELAY |
1221 cricket::PORTALLOCATOR_DISABLE_TCP |
guoweisd12140a2015-09-10 13:32:11 -07001222 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001223 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001224 EXPECT_EQ(0U, allocator_->flags() &
1225 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001226 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001227 EXPECT_EQ(0U, session_->flags() &
1228 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001229 session_->StartGettingPorts();
1230 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1231 EXPECT_EQ(0U, candidates_.size());
1232 EXPECT_TRUE((session_->flags() &
1233 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1234}
1235
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001236// This test verifies allocator can use IPv6 addresses along with IPv4.
1237TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1238 allocator().set_flags(allocator().flags() |
1239 cricket::PORTALLOCATOR_DISABLE_RELAY |
1240 cricket::PORTALLOCATOR_ENABLE_IPV6 |
guoweisd12140a2015-09-10 13:32:11 -07001241 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001242 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1243 AddInterface(kClientIPv6Addr);
1244 AddInterface(kClientAddr);
1245 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1246 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1247 session_->StartGettingPorts();
1248 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1249 EXPECT_EQ(4U, candidates_.size());
1250 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1251 EXPECT_PRED5(CheckCandidate, candidates_[0],
1252 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1253 kClientIPv6Addr);
1254 EXPECT_PRED5(CheckCandidate, candidates_[1],
1255 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1256 kClientAddr);
1257 EXPECT_PRED5(CheckCandidate, candidates_[2],
1258 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1259 kClientIPv6Addr);
1260 EXPECT_PRED5(CheckCandidate, candidates_[3],
1261 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1262 kClientAddr);
1263 EXPECT_EQ(4U, candidates_.size());
1264}