blob: 561442557312c717996910c187b5ca831cd0e7f1 [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 |
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700256 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
257 allocator().set_allow_tcp_listen(false);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000258 session_->StartGettingPorts();
259 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
260
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700261 uint32 total_candidates = 0;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700262 if (!host_candidate_addr.IsNil()) {
263 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
264 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
265 rtc::SocketAddress(host_candidate_addr, 0));
Guo-wei Shieh370c8842015-08-18 17:00:13 -0700266 ++total_candidates;
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700267 }
268 if (!stun_candidate_addr.IsNil()) {
269 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700270 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
271 rtc::SocketAddress(stun_candidate_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700272 EXPECT_EQ(rtc::EmptySocketAddressWithFamily(
273 candidates_[total_candidates].address().family()),
274 candidates_[total_candidates].related_address());
275 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700276 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700277 if (!relay_candidate_udp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700278 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700279 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
280 rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700281 EXPECT_EQ(stun_candidate_addr,
282 candidates_[total_candidates].related_address().ipaddr());
283 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700284 }
Guo-wei Shieh11477022015-08-15 09:28:41 -0700285 if (!relay_candidate_tcp_transport_addr.IsNil()) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700286 EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700287 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
288 rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700289 EXPECT_EQ(stun_candidate_addr,
290 candidates_[total_candidates].related_address().ipaddr());
291 ++total_candidates;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700292 }
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000293
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700294 EXPECT_EQ(total_candidates, candidates_.size());
295 EXPECT_EQ(total_ports, ports_.size());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000296 }
297
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000298 protected:
299 cricket::BasicPortAllocator& allocator() {
300 return *allocator_;
301 }
302
303 void OnPortReady(cricket::PortAllocatorSession* ses,
304 cricket::PortInterface* port) {
305 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
306 ports_.push_back(port);
307 }
308 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
309 const std::vector<cricket::Candidate>& candidates) {
310 for (size_t i = 0; i < candidates.size(); ++i) {
311 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
312 candidates_.push_back(candidates[i]);
313 }
314 }
315
316 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
317 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
318 cricket::RelayServerConfig server_config = allocator_->relays()[i];
319 cricket::PortList::const_iterator relay_port;
320 for (relay_port = server_config.ports.begin();
321 relay_port != server_config.ports.end(); ++relay_port) {
322 if (proto_addr.address == relay_port->address &&
323 proto_addr.proto == relay_port->proto)
324 return true;
325 }
326 }
327 return false;
328 }
329
Guo-wei Shieh11477022015-08-15 09:28:41 -0700330 void ResetWithStunServer(const rtc::SocketAddress& stun_server,
331 bool with_nat) {
332 if (with_nat) {
333 nat_server_.reset(new rtc::NATServer(
334 rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
335 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
336 } else {
337 nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
338 }
339
340 ServerAddresses stun_servers;
341 if (!stun_server.IsNil()) {
342 stun_servers.insert(stun_server);
343 }
344 allocator_.reset(new cricket::BasicPortAllocator(
345 &network_manager_, nat_socket_factory_.get(), stun_servers));
346 allocator().set_step_delay(cricket::kMinimumStepDelay);
347 }
348
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000349 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
350 rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
351 rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
352 rtc::SocketServerScope ss_scope_;
353 rtc::scoped_ptr<rtc::NATServer> nat_server_;
354 rtc::NATSocketFactory nat_factory_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700355 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000356 rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
357 cricket::TestRelayServer relay_server_;
358 cricket::TestTurnServer turn_server_;
359 rtc::FakeNetworkManager network_manager_;
360 rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
361 rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
362 std::vector<cricket::PortInterface*> ports_;
363 std::vector<cricket::Candidate> candidates_;
364 bool candidate_allocation_done_;
365};
366
367// Tests that we can init the port allocator and create a session.
368TEST_F(PortAllocatorTest, TestBasic) {
369 EXPECT_EQ(&network_manager_, allocator().network_manager());
370 EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
371 ASSERT_EQ(1u, allocator().relays().size());
372 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
373 // Empty relay credentials are used for GTURN.
374 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
375 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
376 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
377 kRelayUdpIntAddr, cricket::PROTO_UDP)));
378 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
379 kRelayTcpIntAddr, cricket::PROTO_TCP)));
380 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
381 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
382 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
383}
384
385// Tests that we allocator session not trying to allocate ports for every 250ms.
386TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
387 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
388 session_->StartGettingPorts();
389 // Waiting for one second to make sure BasicPortAllocatorSession has not
390 // called OnAllocate multiple times. In old behavior it's called every 250ms.
391 // When there are no network interfaces, each execution of OnAllocate will
392 // result in SignalCandidatesAllocationDone signal.
393 rtc::Thread::Current()->ProcessMessages(1000);
394 EXPECT_TRUE(candidate_allocation_done_);
395 EXPECT_EQ(0U, candidates_.size());
396}
397
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700398// Test that we could use loopback interface as host candidate.
399TEST_F(PortAllocatorTest, TestLoopbackNetworkInterface) {
400 AddInterface(kLoopbackAddr);
401 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
402 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
403 cricket::PORTALLOCATOR_DISABLE_RELAY |
404 cricket::PORTALLOCATOR_DISABLE_TCP);
405 session_->StartGettingPorts();
406 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
407 EXPECT_EQ(1U, candidates_.size());
408}
409
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000410// Tests that we can get all the desired addresses successfully.
411TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
412 AddInterface(kClientAddr);
413 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
414 session_->StartGettingPorts();
415 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
416 EXPECT_EQ(4U, ports_.size());
417 EXPECT_PRED5(CheckCandidate, candidates_[0],
418 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
419 EXPECT_PRED5(CheckCandidate, candidates_[1],
420 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
421 EXPECT_PRED5(CheckCandidate, candidates_[2],
422 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
423 EXPECT_PRED5(CheckCandidate, candidates_[3],
424 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
425 EXPECT_PRED5(CheckCandidate, candidates_[4],
426 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
427 EXPECT_PRED5(CheckCandidate, candidates_[5],
428 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
429 EXPECT_PRED5(CheckCandidate, candidates_[6],
430 cricket::ICE_CANDIDATE_COMPONENT_RTP,
431 "relay", "ssltcp", kRelaySslTcpIntAddr);
432 EXPECT_TRUE(candidate_allocation_done_);
433}
434
435// Verify candidates with default step delay of 1sec.
436TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
437 AddInterface(kClientAddr);
438 allocator_->set_step_delay(cricket::kDefaultStepDelay);
439 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
440 session_->StartGettingPorts();
441 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
442 EXPECT_EQ(2U, ports_.size());
443 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
444 EXPECT_EQ(3U, ports_.size());
445 EXPECT_PRED5(CheckCandidate, candidates_[2],
446 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
447 EXPECT_PRED5(CheckCandidate, candidates_[3],
448 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
449 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
450 EXPECT_PRED5(CheckCandidate, candidates_[4],
451 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
452 EXPECT_PRED5(CheckCandidate, candidates_[5],
453 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
454 EXPECT_EQ(4U, ports_.size());
455 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
456 EXPECT_PRED5(CheckCandidate, candidates_[6],
457 cricket::ICE_CANDIDATE_COMPONENT_RTP,
458 "relay", "ssltcp", kRelaySslTcpIntAddr);
459 EXPECT_EQ(4U, ports_.size());
460 EXPECT_TRUE(candidate_allocation_done_);
461 // If we Stop gathering now, we shouldn't get a second "done" callback.
462 session_->StopGettingPorts();
463}
464
465TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
466 AddInterface(kClientAddr);
467 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
468 cricket::CN_VIDEO));
469 session_->StartGettingPorts();
470 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
471 EXPECT_TRUE(candidate_allocation_done_);
472 // If we Stop gathering now, we shouldn't get a second "done" callback.
473 session_->StopGettingPorts();
474
475 // All ports should have unset send-buffer sizes.
476 CheckSendBufferSizesOfAllPorts(-1);
477}
478
479// Tests that we can get callback after StopGetAllPorts.
480TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
481 AddInterface(kClientAddr);
482 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
483 session_->StartGettingPorts();
484 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
485 EXPECT_EQ(2U, ports_.size());
486 session_->StopGettingPorts();
487 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
488}
489
490// Test that we restrict client ports appropriately when a port range is set.
491// We check the candidates for udp/stun/tcp ports, and the from address
492// for relay ports.
493TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
494 AddInterface(kClientAddr);
495 // Check that an invalid port range fails.
496 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
497 // Check that a null port range succeeds.
498 EXPECT_TRUE(SetPortRange(0, 0));
499 // Check that a valid port range succeeds.
500 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
501 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
502 session_->StartGettingPorts();
503 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
504 EXPECT_EQ(4U, ports_.size());
505 // Check the port number for the UDP port object.
506 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
507 // Check the port number for the STUN port object.
508 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
509 // Check the port number used to connect to the relay server.
510 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
511 kMinPort, kMaxPort);
512 // Check the port number for the TCP port object.
513 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
514 EXPECT_TRUE(candidate_allocation_done_);
515}
516
517// Test that we don't crash or malfunction if we have no network adapters.
518TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
519 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
520 session_->StartGettingPorts();
521 rtc::Thread::Current()->ProcessMessages(100);
522 // Without network adapter, we should not get any candidate.
523 EXPECT_EQ(0U, candidates_.size());
524 EXPECT_TRUE(candidate_allocation_done_);
525}
526
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000527// Test that we should only get STUN and TURN candidates when adapter
528// enumeration is disabled.
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700529TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNat) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000530 AddInterface(kClientAddr);
531 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700532 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000533 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700534 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
535 // TURN/UDP candidates.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700536 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700537 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000538}
539
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700540// Test that even with multiple interfaces, the result should still be one STUN
541// and one TURN candidate since we bind to any address (i.e. all 0s).
542TEST_F(PortAllocatorTest,
543 TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000544 AddInterface(kPrivateAddr);
545 AddInterface(kPrivateAddr2);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700546 ResetWithStunServerAndNat(kStunAddr);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000547 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700548 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, and both STUN and
549 // TURN/UDP candidates.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700550 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700551 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
552}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000553
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700554// Test that we should get STUN, TURN/UDP and TURN/TCP candidates when a
555// TURN/TCP server is specified.
556TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
557 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
558 AddInterface(kClientAddr);
559 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700560 ResetWithStunServerAndNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700561 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
562 // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. STUN,
563 // TURN/UDP, and TURN/TCP candidates.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700564 CheckDisableAdapterEnumeration(4U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700565 kTurnUdpExtAddr.ipaddr(),
566 kTurnUdpExtAddr.ipaddr());
567}
568
569// Test that we should only get STUN and TURN candidates when adapter
570// enumeration is disabled. Since the endpoint is not behind NAT, the srflx
571// address should be the public client interface.
572TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNat) {
573 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700574 ResetWithStunServerNoNat(kStunAddr);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700575 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
576 // Expect to see 3 ports: STUN, TURN/UDP and TCP ports, but only both STUN and
577 // TURN candidates. The STUN candidate should have kClientAddr as srflx
578 // address, and TURN candidate with kClientAddr as the related address.
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700579 CheckDisableAdapterEnumeration(3U, rtc::IPAddress(), kClientAddr.ipaddr(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700580 kTurnUdpExtAddr.ipaddr(), rtc::IPAddress());
581}
582
583// Test that when adapter enumeration is disabled, for endpoints without
584// STUN/TURN specified, no candidate is generated.
585TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
586 AddInterfaceAsDefaultRoute(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700587 ResetWithNoServersOrNat();
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700588 // Expect to see 2 ports: STUN and TCP ports, but no candidate.
589 CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700590 rtc::IPAddress(), rtc::IPAddress());
591}
592
593// Test that when adapter enumeration is disabled, with
594// PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
595// a NAT, there are a localhost candidate in addition to a STUN candidate.
596TEST_F(PortAllocatorTest,
597 TestDisableAdapterEnumerationWithoutNatLocalhostCandidateRequested) {
598 AddInterfaceAsDefaultRoute(kClientAddr);
599 ResetWithStunServerNoNat(kStunAddr);
600 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
601 session_->set_flags(cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
602 // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
603 // candidate.
604 CheckDisableAdapterEnumeration(2U, rtc::GetLoopbackIP(AF_INET),
605 kClientAddr.ipaddr(), rtc::IPAddress(),
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700606 rtc::IPAddress());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000607}
608
Erik Språngefdce692015-06-05 09:41:26 +0200609// Disable for asan, see
610// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
611#if !defined(ADDRESS_SANITIZER)
612
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000613// Test that we can get OnCandidatesAllocationDone callback when all the ports
614// are disabled.
615TEST_F(PortAllocatorTest, TestDisableAllPorts) {
616 AddInterface(kClientAddr);
617 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
618 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
619 cricket::PORTALLOCATOR_DISABLE_STUN |
620 cricket::PORTALLOCATOR_DISABLE_RELAY |
621 cricket::PORTALLOCATOR_DISABLE_TCP);
622 session_->StartGettingPorts();
623 rtc::Thread::Current()->ProcessMessages(100);
624 EXPECT_EQ(0U, candidates_.size());
625 EXPECT_TRUE(candidate_allocation_done_);
626}
627
628// Test that we don't crash or malfunction if we can't create UDP sockets.
629TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
630 AddInterface(kClientAddr);
631 fss_->set_udp_sockets_enabled(false);
632 EXPECT_TRUE(CreateSession(1));
633 session_->StartGettingPorts();
634 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
635 EXPECT_EQ(2U, ports_.size());
636 EXPECT_PRED5(CheckCandidate, candidates_[0],
637 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
638 EXPECT_PRED5(CheckCandidate, candidates_[1],
639 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
640 EXPECT_PRED5(CheckCandidate, candidates_[2],
641 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
642 EXPECT_PRED5(CheckCandidate, candidates_[3],
643 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
644 EXPECT_PRED5(CheckCandidate, candidates_[4],
645 cricket::ICE_CANDIDATE_COMPONENT_RTP,
646 "relay", "ssltcp", kRelaySslTcpIntAddr);
647 EXPECT_TRUE(candidate_allocation_done_);
648}
649
Erik Språngefdce692015-06-05 09:41:26 +0200650#endif // if !defined(ADDRESS_SANITIZER)
651
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000652// Test that we don't crash or malfunction if we can't create UDP sockets or
653// listen on TCP sockets. We still give out a local TCP address, since
654// apparently this is needed for the remote side to accept our connection.
655TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
656 AddInterface(kClientAddr);
657 fss_->set_udp_sockets_enabled(false);
658 fss_->set_tcp_listen_enabled(false);
659 EXPECT_TRUE(CreateSession(1));
660 session_->StartGettingPorts();
661 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
662 EXPECT_EQ(2U, ports_.size());
663 EXPECT_PRED5(CheckCandidate, candidates_[0],
664 1, "relay", "udp", kRelayUdpIntAddr);
665 EXPECT_PRED5(CheckCandidate, candidates_[1],
666 1, "relay", "udp", kRelayUdpExtAddr);
667 EXPECT_PRED5(CheckCandidate, candidates_[2],
668 1, "relay", "tcp", kRelayTcpIntAddr);
669 EXPECT_PRED5(CheckCandidate, candidates_[3],
670 1, "local", "tcp", kClientAddr);
671 EXPECT_PRED5(CheckCandidate, candidates_[4],
672 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
673 EXPECT_TRUE(candidate_allocation_done_);
674}
675
676// Test that we don't crash or malfunction if we can't create any sockets.
677// TODO: Find a way to exit early here.
678TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
679 AddInterface(kClientAddr);
680 fss_->set_tcp_sockets_enabled(false);
681 fss_->set_udp_sockets_enabled(false);
682 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
683 session_->StartGettingPorts();
684 WAIT(candidates_.size() > 0, 2000);
685 // TODO - Check candidate_allocation_done signal.
686 // In case of Relay, ports creation will succeed but sockets will fail.
687 // There is no error reporting from RelayEntry to handle this failure.
688}
689
690// Testing STUN timeout.
691TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
692 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
693 AddInterface(kClientAddr);
694 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
695 session_->StartGettingPorts();
696 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
697 EXPECT_EQ(2U, ports_.size());
698 EXPECT_PRED5(CheckCandidate, candidates_[0],
699 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
700 EXPECT_PRED5(CheckCandidate, candidates_[1],
701 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
702 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
703 // will be tried after 3 seconds.
704 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
705 EXPECT_EQ(3U, ports_.size());
706 EXPECT_PRED5(CheckCandidate, candidates_[2],
707 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
708 EXPECT_PRED5(CheckCandidate, candidates_[3],
709 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
710 EXPECT_PRED5(CheckCandidate, candidates_[4],
711 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
712 kRelaySslTcpIntAddr);
713 EXPECT_PRED5(CheckCandidate, candidates_[5],
714 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
715 // Stun Timeout is 9sec.
716 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
717}
718
719TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
720 AddInterface(kClientAddr);
721 AddInterface(kClientAddr2);
722 // Allocating only host UDP ports. This is done purely for testing
723 // convenience.
724 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
725 cricket::PORTALLOCATOR_DISABLE_STUN |
726 cricket::PORTALLOCATOR_DISABLE_RELAY);
727 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
728 session_->StartGettingPorts();
729 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
730 ASSERT_EQ(2U, candidates_.size());
731 EXPECT_EQ(2U, ports_.size());
732 // Candidates priorities should be different.
733 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
734}
735
736// Test to verify ICE restart process.
737TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
738 AddInterface(kClientAddr);
739 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
740 session_->StartGettingPorts();
741 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
742 EXPECT_EQ(4U, ports_.size());
743 EXPECT_TRUE(candidate_allocation_done_);
744 // TODO - Extend this to verify ICE restart.
745}
746
747// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
748// This test also verifies that when the allocator is only allowed to use
749// relay (i.e. IceTransportsType is relay), the raddr is an empty
750// address with the correct family. This is to prevent any local
751// reflective address leakage in the sdp line.
752TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
753 AddInterface(kClientAddr);
754 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700755 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000756 allocator().set_candidate_filter(cricket::CF_RELAY);
757 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
758 session_->StartGettingPorts();
759 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
760 EXPECT_PRED5(CheckCandidate,
761 candidates_[0],
762 cricket::ICE_CANDIDATE_COMPONENT_RTP,
763 "relay",
764 "udp",
765 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
766
767 EXPECT_EQ(1U, candidates_.size());
768 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
769 for (size_t i = 0; i < candidates_.size(); ++i) {
770 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
771 EXPECT_EQ(
772 candidates_[0].related_address(),
773 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
774 }
775}
776
777TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
778 AddInterface(kClientAddr);
Peter Thatcher2159b892015-08-21 20:46:05 -0700779 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000780 allocator().set_candidate_filter(cricket::CF_HOST);
781 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
782 session_->StartGettingPorts();
783 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
784 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
785 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
786 for (size_t i = 0; i < candidates_.size(); ++i) {
787 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
788 }
789}
790
791// Host is behind the NAT.
792TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
793 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700794 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000795
Peter Thatcher2159b892015-08-21 20:46:05 -0700796 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000797 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
798 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
799 session_->StartGettingPorts();
800 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
801 // Host is behind NAT, no private address will be exposed. Hence only UDP
802 // port with STUN candidate will be sent outside.
803 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
804 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
805 for (size_t i = 0; i < candidates_.size(); ++i) {
806 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
807 EXPECT_EQ(
808 candidates_[0].related_address(),
809 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
810 }
811}
812
813// Host is not behind the NAT.
814TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
815 AddInterface(kClientAddr);
Peter Thatcher2159b892015-08-21 20:46:05 -0700816 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000817 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
818 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
819 session_->StartGettingPorts();
820 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
821 // Host has a public address, both UDP and TCP candidates will be exposed.
822 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
823 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
824 for (size_t i = 0; i < candidates_.size(); ++i) {
825 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
826 }
827}
828
Peter Thatcher2159b892015-08-21 20:46:05 -0700829// Test that we get the same ufrag and pwd for all candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000830TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000831 AddInterface(kClientAddr);
832 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
833 session_->StartGettingPorts();
834 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
835 EXPECT_PRED5(CheckCandidate, candidates_[0],
836 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
837 EXPECT_PRED5(CheckCandidate, candidates_[1],
838 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
839 EXPECT_PRED5(CheckCandidate, candidates_[5],
840 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
841 EXPECT_EQ(4U, ports_.size());
842 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
843 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
844 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
845 EXPECT_EQ(kIcePwd0, candidates_[0].password());
846 EXPECT_EQ(kIcePwd0, candidates_[1].password());
847 EXPECT_TRUE(candidate_allocation_done_);
848}
849
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000850// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
851// is allocated for udp and stun. Also verify there is only one candidate
852// (local) if stun candidate is same as local candidate, which will be the case
853// in a public network like the below test.
854TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
855 AddInterface(kClientAddr);
856 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000857 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
858 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
859 session_->StartGettingPorts();
860 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
861 EXPECT_EQ(3U, ports_.size());
862 EXPECT_PRED5(CheckCandidate, candidates_[0],
863 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
864 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
865}
866
867// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
868// is allocated for udp and stun. In this test we should expect both stun and
869// local candidates as client behind a nat.
870TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
871 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700872 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000873
874 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000875 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
876 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
877 session_->StartGettingPorts();
878 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
879 ASSERT_EQ(2U, ports_.size());
880 EXPECT_PRED5(CheckCandidate, candidates_[0],
881 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
882 EXPECT_PRED5(CheckCandidate, candidates_[1],
883 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700884 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000885 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
886 EXPECT_EQ(3U, candidates_.size());
887}
888
deadbeefc5d0d952015-07-16 10:22:21 -0700889// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000890TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
891 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
892 AddInterface(kClientAddr);
893 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
894
895 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
896
897 allocator_->set_step_delay(cricket::kMinimumStepDelay);
898 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000899 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
900 cricket::PORTALLOCATOR_DISABLE_TCP);
901
902 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
903 session_->StartGettingPorts();
904
905 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
906 ASSERT_EQ(3U, ports_.size());
907 EXPECT_PRED5(CheckCandidate, candidates_[0],
908 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
909 EXPECT_PRED5(CheckCandidate, candidates_[1],
910 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
911 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
912 EXPECT_PRED5(CheckCandidate, candidates_[2],
913 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
914 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
915 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
916 EXPECT_EQ(3U, candidates_.size());
917}
918
919// Testing DNS resolve for the TURN server, this will test AllocationSequence
920// handling the unresolved address signal from TurnPort.
921TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
922 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
923 cricket::PROTO_UDP);
924 AddInterface(kClientAddr);
925 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
926 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
927 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
928 relay_server.credentials = credentials;
929 relay_server.ports.push_back(cricket::ProtocolAddress(
930 rtc::SocketAddress("localhost", 3478),
931 cricket::PROTO_UDP, false));
932 allocator_->AddRelay(relay_server);
933
934 allocator_->set_step_delay(cricket::kMinimumStepDelay);
935 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000936 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
937 cricket::PORTALLOCATOR_DISABLE_TCP);
938
939 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
940 session_->StartGettingPorts();
941
942 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
943}
944
945// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
946// is allocated for udp/stun/turn. In this test we should expect all local,
947// stun and turn candidates.
948TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
949 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700950 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000951
952 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
953
954 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
956 cricket::PORTALLOCATOR_DISABLE_TCP);
957
958 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
959 session_->StartGettingPorts();
960
961 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
962 ASSERT_EQ(2U, ports_.size());
963 EXPECT_PRED5(CheckCandidate, candidates_[0],
964 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
965 EXPECT_PRED5(CheckCandidate, candidates_[1],
966 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700967 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000968 EXPECT_PRED5(CheckCandidate, candidates_[2],
969 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
970 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
971 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
972 EXPECT_EQ(3U, candidates_.size());
973 // Local port will be created first and then TURN port.
974 EXPECT_EQ(2U, ports_[0]->Candidates().size());
975 EXPECT_EQ(1U, ports_[1]->Candidates().size());
976}
977
978// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
979// server is also used as the STUN server, we should get 'local', 'stun', and
980// 'relay' candidates.
981TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
982 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -0700983 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700984 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000985 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
986
987 // Must set the step delay to 0 to make sure the relay allocation phase is
988 // started before the STUN candidates are obtained, so that the STUN binding
989 // response is processed when both StunPort and TurnPort exist to reproduce
990 // webrtc issue 3537.
991 allocator_->set_step_delay(0);
992 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000993 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
994 cricket::PORTALLOCATOR_DISABLE_TCP);
995
996 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
997 session_->StartGettingPorts();
998
999 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1000 EXPECT_PRED5(CheckCandidate, candidates_[0],
1001 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1002 EXPECT_PRED5(CheckCandidate, candidates_[1],
1003 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -07001004 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001005 EXPECT_PRED5(CheckCandidate, candidates_[2],
1006 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1007 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1008 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1009
1010 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1011 EXPECT_EQ(3U, candidates_.size());
1012 // Local port will be created first and then TURN port.
1013 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1014 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1015}
1016
deadbeefc5d0d952015-07-16 10:22:21 -07001017// Test that when only a TCP TURN server is available, we do NOT use it as
1018// a UDP STUN server, as this could leak our IP address. Thus we should only
1019// expect two ports, a UDPPort and TurnPort.
1020TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1021 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1022 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001023 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001024 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1025
1026 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001027 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1028 cricket::PORTALLOCATOR_DISABLE_TCP);
1029
1030 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1031 session_->StartGettingPorts();
1032
1033 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1034 ASSERT_EQ(2U, ports_.size());
1035 EXPECT_PRED5(CheckCandidate, candidates_[0],
1036 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1037 kClientAddr);
1038 EXPECT_PRED5(CheckCandidate, candidates_[1],
1039 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1040 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1041 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1042 EXPECT_EQ(2U, candidates_.size());
1043 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1044 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1045}
1046
1047// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1048// TURN server is used as the STUN server and we get 'local', 'stun', and
1049// 'relay' candidates.
1050// TODO(deadbeef): Remove this test when support for non-shared socket mode
1051// is removed.
1052TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1053 AddInterface(kClientAddr);
1054 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001055 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001056 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1057
1058 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001059 cricket::PORTALLOCATOR_DISABLE_TCP);
1060
1061 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1062 session_->StartGettingPorts();
1063
1064 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1065 ASSERT_EQ(3U, ports_.size());
1066 EXPECT_PRED5(CheckCandidate, candidates_[0],
1067 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1068 kClientAddr);
1069 EXPECT_PRED5(CheckCandidate, candidates_[1],
1070 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1071 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1072 EXPECT_PRED5(CheckCandidate, candidates_[2],
1073 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1074 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1075 // Not using shared socket, so the STUN request's server reflexive address
1076 // should be different than the TURN request's server reflexive address.
1077 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1078
1079 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1080 EXPECT_EQ(3U, candidates_.size());
1081 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1082 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1083 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1084}
1085
1086// Test that even when both a STUN and TURN server are configured, the TURN
1087// server is used as a STUN server and we get a 'stun' candidate.
1088TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1089 AddInterface(kClientAddr);
1090 // Configure with STUN server but destroy it, so we can ensure that it's
1091 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001092 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001093 stun_server_.reset();
1094 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1095
1096 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001097 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1098 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 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, "stun", "udp",
1109 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1110 EXPECT_PRED5(CheckCandidate, candidates_[2],
1111 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1112 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1113 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1114
1115 // Don't bother waiting for STUN timeout, since we already verified
1116 // that we got a STUN candidate from the TURN server.
1117}
1118
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001119// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1120// and fail to generate STUN candidate, local UDP candidate is generated
1121// properly.
1122TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1123 allocator().set_flags(allocator().flags() |
1124 cricket::PORTALLOCATOR_DISABLE_RELAY |
1125 cricket::PORTALLOCATOR_DISABLE_TCP |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001126 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1127 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1128 AddInterface(kClientAddr);
1129 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1130 session_->StartGettingPorts();
1131 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1132 EXPECT_EQ(1U, candidates_.size());
1133 EXPECT_PRED5(CheckCandidate, candidates_[0],
1134 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1135 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1136 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1137 EXPECT_EQ(1U, candidates_.size());
1138}
1139
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001140// Test that when the NetworkManager doesn't have permission to enumerate
1141// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1142// automatically.
1143TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
1144 AddInterface(kClientAddr);
1145 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001146 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001147 allocator().set_flags(allocator().flags() |
1148 cricket::PORTALLOCATOR_DISABLE_RELAY |
1149 cricket::PORTALLOCATOR_DISABLE_TCP |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001150 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001151 EXPECT_EQ(0U, allocator_->flags() &
1152 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001153 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001154 EXPECT_EQ(0U, session_->flags() &
1155 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001156 session_->StartGettingPorts();
1157 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1158 EXPECT_EQ(0U, candidates_.size());
1159 EXPECT_TRUE((session_->flags() &
1160 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1161}
1162
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001163// This test verifies allocator can use IPv6 addresses along with IPv4.
1164TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1165 allocator().set_flags(allocator().flags() |
1166 cricket::PORTALLOCATOR_DISABLE_RELAY |
1167 cricket::PORTALLOCATOR_ENABLE_IPV6 |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001168 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1169 AddInterface(kClientIPv6Addr);
1170 AddInterface(kClientAddr);
1171 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1172 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1173 session_->StartGettingPorts();
1174 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1175 EXPECT_EQ(4U, candidates_.size());
1176 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1177 EXPECT_PRED5(CheckCandidate, candidates_[0],
1178 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1179 kClientIPv6Addr);
1180 EXPECT_PRED5(CheckCandidate, candidates_[1],
1181 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1182 kClientAddr);
1183 EXPECT_PRED5(CheckCandidate, candidates_[2],
1184 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1185 kClientIPv6Addr);
1186 EXPECT_PRED5(CheckCandidate, candidates_[3],
1187 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1188 kClientAddr);
1189 EXPECT_EQ(4U, candidates_.size());
1190}
1191
1192// Test that the httpportallocator correctly maintains its lists of stun and
1193// relay servers, by never allowing an empty list.
1194TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
1195 rtc::FakeNetworkManager network_manager;
1196 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1197 EXPECT_EQ(1U, alloc.relay_hosts().size());
1198 EXPECT_EQ(1U, alloc.stun_hosts().size());
1199
1200 std::vector<std::string> relay_servers;
1201 std::vector<rtc::SocketAddress> stun_servers;
1202
1203 alloc.SetRelayHosts(relay_servers);
1204 alloc.SetStunHosts(stun_servers);
1205 EXPECT_EQ(1U, alloc.relay_hosts().size());
1206 EXPECT_EQ(1U, alloc.stun_hosts().size());
1207
1208 relay_servers.push_back("1.unittest.corp.google.com");
1209 relay_servers.push_back("2.unittest.corp.google.com");
1210 stun_servers.push_back(
1211 rtc::SocketAddress("1.unittest.corp.google.com", 0));
1212 stun_servers.push_back(
1213 rtc::SocketAddress("2.unittest.corp.google.com", 0));
1214
1215 alloc.SetRelayHosts(relay_servers);
1216 alloc.SetStunHosts(stun_servers);
1217 EXPECT_EQ(2U, alloc.relay_hosts().size());
1218 EXPECT_EQ(2U, alloc.stun_hosts().size());
1219}
1220
1221// Test that the HttpPortAllocator uses correct URL to create sessions.
1222TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
1223 rtc::FakeNetworkManager network_manager;
1224 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
1225
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001226 rtc::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
1227 static_cast<cricket::HttpPortAllocatorSession*>(
1228 alloc.CreateSessionInternal(
1229 "test content", 0, kIceUfrag0, kIcePwd0)));
1230 std::string url = session->GetSessionRequestUrl();
1231 LOG(LS_INFO) << "url: " << url;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001232 std::vector<std::string> parts;
1233 rtc::split(url, '?', &parts);
1234 ASSERT_EQ(2U, parts.size());
1235
1236 std::vector<std::string> args_parts;
1237 rtc::split(parts[1], '&', &args_parts);
1238
1239 std::map<std::string, std::string> args;
1240 for (std::vector<std::string>::iterator it = args_parts.begin();
1241 it != args_parts.end(); ++it) {
1242 std::vector<std::string> parts;
1243 rtc::split(*it, '=', &parts);
1244 ASSERT_EQ(2U, parts.size());
1245 args[rtc::s_url_decode(parts[0])] = rtc::s_url_decode(parts[1]);
1246 }
1247
1248 EXPECT_EQ(kIceUfrag0, args["username"]);
1249 EXPECT_EQ(kIcePwd0, args["password"]);
1250}
jiayl@webrtc.org7e5b3802015-01-22 21:28:39 +00001251
1252// Tests that destroying ports with non-shared sockets does not crash.
1253// b/19074679.
1254TEST_F(PortAllocatorTest, TestDestroyPortsNonSharedSockets) {
1255 AddInterface(kClientAddr);
1256 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1257 session_->StartGettingPorts();
1258 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1259 EXPECT_EQ(4U, ports_.size());
1260
1261 auto it = ports_.begin();
1262 for (; it != ports_.end(); ++it) {
1263 (reinterpret_cast<cricket::Port*>(*it))->Destroy();
1264 }
1265}
honghaizf421bdc2015-07-17 16:21:55 -07001266
1267class AllocationSequenceForTest : public cricket::AllocationSequence {
1268 public:
1269 AllocationSequenceForTest(cricket::BasicPortAllocatorSession* session,
1270 rtc::Network* network,
1271 cricket::PortConfiguration* config,
1272 uint32 flags)
1273 : cricket::AllocationSequence(session, network, config, flags) {}
1274 using cricket::AllocationSequence::CreateTurnPort;
1275};
1276
1277TEST_F(PortAllocatorTest, TestCreateTurnPortWithNullSocket) {
1278 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1279 session_->StartGettingPorts();
1280
1281 cricket::ServerAddresses stun_servers;
1282 stun_servers.insert(kStunAddr);
1283 cricket::PortConfiguration config(stun_servers, kIceUfrag0, kIcePwd0);
1284 rtc::Network network1("test_eth0", "Test Network Adapter 1",
1285 rtc::IPAddress(0x12345600U), 24);
1286 uint32 flag = cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET;
1287 AllocationSequenceForTest alloc_sequence(
1288 static_cast<cricket::BasicPortAllocatorSession*>(session_.get()),
1289 &network1, &config, flag);
1290 // This simply tests it will not crash if udp_socket_ in the
1291 // AllocationSequence is null, which is chosen in the constructor.
1292 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
1293 relay_server.ports.push_back(
1294 cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
1295 alloc_sequence.CreateTurnPort(relay_server);
1296}