blob: 9ea22dd189487192cb56152c5117f102939454b0 [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
Guo-wei Shieh13d35f62015-08-26 15:32:56 -0700609// Test that we disable relay over UDP, and only TCP is used when connecting to
610// the relay server.
611TEST_F(PortAllocatorTest, TestDisableUdpTurn) {
612 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
613 AddInterface(kClientAddr);
614 ResetWithStunServerAndNat(kStunAddr);
615 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
616 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
617 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY |
618 cricket::PORTALLOCATOR_DISABLE_UDP |
619 cricket::PORTALLOCATOR_DISABLE_STUN |
620 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
621
622 session_->StartGettingPorts();
623 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
624
625 // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
626 // TURN/TCP candidates.
627 EXPECT_EQ(2U, ports_.size());
628 EXPECT_EQ(2U, candidates_.size());
629 EXPECT_PRED5(CheckCandidate, candidates_[0],
630 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
631 kTurnUdpExtAddr);
632 // The TURN candidate should use TCP to contact the TURN server.
633 EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
634 EXPECT_PRED5(CheckCandidate, candidates_[1],
635 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
636 kClientAddr);
637}
638
Erik Språngefdce692015-06-05 09:41:26 +0200639// Disable for asan, see
640// https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
641#if !defined(ADDRESS_SANITIZER)
642
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000643// Test that we can get OnCandidatesAllocationDone callback when all the ports
644// are disabled.
645TEST_F(PortAllocatorTest, TestDisableAllPorts) {
646 AddInterface(kClientAddr);
647 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
648 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
649 cricket::PORTALLOCATOR_DISABLE_STUN |
650 cricket::PORTALLOCATOR_DISABLE_RELAY |
651 cricket::PORTALLOCATOR_DISABLE_TCP);
652 session_->StartGettingPorts();
653 rtc::Thread::Current()->ProcessMessages(100);
654 EXPECT_EQ(0U, candidates_.size());
655 EXPECT_TRUE(candidate_allocation_done_);
656}
657
658// Test that we don't crash or malfunction if we can't create UDP sockets.
659TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
660 AddInterface(kClientAddr);
661 fss_->set_udp_sockets_enabled(false);
662 EXPECT_TRUE(CreateSession(1));
663 session_->StartGettingPorts();
664 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
665 EXPECT_EQ(2U, ports_.size());
666 EXPECT_PRED5(CheckCandidate, candidates_[0],
667 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
668 EXPECT_PRED5(CheckCandidate, candidates_[1],
669 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
670 EXPECT_PRED5(CheckCandidate, candidates_[2],
671 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
672 EXPECT_PRED5(CheckCandidate, candidates_[3],
673 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
674 EXPECT_PRED5(CheckCandidate, candidates_[4],
675 cricket::ICE_CANDIDATE_COMPONENT_RTP,
676 "relay", "ssltcp", kRelaySslTcpIntAddr);
677 EXPECT_TRUE(candidate_allocation_done_);
678}
679
Erik Språngefdce692015-06-05 09:41:26 +0200680#endif // if !defined(ADDRESS_SANITIZER)
681
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000682// Test that we don't crash or malfunction if we can't create UDP sockets or
683// listen on TCP sockets. We still give out a local TCP address, since
684// apparently this is needed for the remote side to accept our connection.
685TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
686 AddInterface(kClientAddr);
687 fss_->set_udp_sockets_enabled(false);
688 fss_->set_tcp_listen_enabled(false);
689 EXPECT_TRUE(CreateSession(1));
690 session_->StartGettingPorts();
691 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
692 EXPECT_EQ(2U, ports_.size());
693 EXPECT_PRED5(CheckCandidate, candidates_[0],
694 1, "relay", "udp", kRelayUdpIntAddr);
695 EXPECT_PRED5(CheckCandidate, candidates_[1],
696 1, "relay", "udp", kRelayUdpExtAddr);
697 EXPECT_PRED5(CheckCandidate, candidates_[2],
698 1, "relay", "tcp", kRelayTcpIntAddr);
699 EXPECT_PRED5(CheckCandidate, candidates_[3],
700 1, "local", "tcp", kClientAddr);
701 EXPECT_PRED5(CheckCandidate, candidates_[4],
702 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
703 EXPECT_TRUE(candidate_allocation_done_);
704}
705
706// Test that we don't crash or malfunction if we can't create any sockets.
707// TODO: Find a way to exit early here.
708TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
709 AddInterface(kClientAddr);
710 fss_->set_tcp_sockets_enabled(false);
711 fss_->set_udp_sockets_enabled(false);
712 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
713 session_->StartGettingPorts();
714 WAIT(candidates_.size() > 0, 2000);
715 // TODO - Check candidate_allocation_done signal.
716 // In case of Relay, ports creation will succeed but sockets will fail.
717 // There is no error reporting from RelayEntry to handle this failure.
718}
719
720// Testing STUN timeout.
721TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
722 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
723 AddInterface(kClientAddr);
724 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
725 session_->StartGettingPorts();
726 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
727 EXPECT_EQ(2U, ports_.size());
728 EXPECT_PRED5(CheckCandidate, candidates_[0],
729 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
730 EXPECT_PRED5(CheckCandidate, candidates_[1],
731 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
732 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
733 // will be tried after 3 seconds.
734 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
735 EXPECT_EQ(3U, ports_.size());
736 EXPECT_PRED5(CheckCandidate, candidates_[2],
737 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
738 EXPECT_PRED5(CheckCandidate, candidates_[3],
739 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
740 EXPECT_PRED5(CheckCandidate, candidates_[4],
741 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
742 kRelaySslTcpIntAddr);
743 EXPECT_PRED5(CheckCandidate, candidates_[5],
744 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
745 // Stun Timeout is 9sec.
746 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
747}
748
749TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
750 AddInterface(kClientAddr);
751 AddInterface(kClientAddr2);
752 // Allocating only host UDP ports. This is done purely for testing
753 // convenience.
754 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
755 cricket::PORTALLOCATOR_DISABLE_STUN |
756 cricket::PORTALLOCATOR_DISABLE_RELAY);
757 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
758 session_->StartGettingPorts();
759 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
760 ASSERT_EQ(2U, candidates_.size());
761 EXPECT_EQ(2U, ports_.size());
762 // Candidates priorities should be different.
763 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
764}
765
766// Test to verify ICE restart process.
767TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
768 AddInterface(kClientAddr);
769 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
770 session_->StartGettingPorts();
771 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
772 EXPECT_EQ(4U, ports_.size());
773 EXPECT_TRUE(candidate_allocation_done_);
774 // TODO - Extend this to verify ICE restart.
775}
776
777// Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
778// This test also verifies that when the allocator is only allowed to use
779// relay (i.e. IceTransportsType is relay), the raddr is an empty
780// address with the correct family. This is to prevent any local
781// reflective address leakage in the sdp line.
782TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
783 AddInterface(kClientAddr);
784 // GTURN is not configured here.
Guo-wei Shieh11477022015-08-15 09:28:41 -0700785 ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786 allocator().set_candidate_filter(cricket::CF_RELAY);
787 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
788 session_->StartGettingPorts();
789 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
790 EXPECT_PRED5(CheckCandidate,
791 candidates_[0],
792 cricket::ICE_CANDIDATE_COMPONENT_RTP,
793 "relay",
794 "udp",
795 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
796
797 EXPECT_EQ(1U, candidates_.size());
798 EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state.
799 for (size_t i = 0; i < candidates_.size(); ++i) {
800 EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
801 EXPECT_EQ(
802 candidates_[0].related_address(),
803 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
804 }
805}
806
807TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
808 AddInterface(kClientAddr);
Peter Thatcher2159b892015-08-21 20:46:05 -0700809 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000810 allocator().set_candidate_filter(cricket::CF_HOST);
811 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
812 session_->StartGettingPorts();
813 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
814 EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
815 EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
816 for (size_t i = 0; i < candidates_.size(); ++i) {
817 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
818 }
819}
820
821// Host is behind the NAT.
822TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
823 AddInterface(kPrivateAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700824 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000825
Peter Thatcher2159b892015-08-21 20:46:05 -0700826 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000827 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
828 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
829 session_->StartGettingPorts();
830 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
831 // Host is behind NAT, no private address will be exposed. Hence only UDP
832 // port with STUN candidate will be sent outside.
833 EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
834 EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state.
835 for (size_t i = 0; i < candidates_.size(); ++i) {
836 EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
837 EXPECT_EQ(
838 candidates_[0].related_address(),
839 rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
840 }
841}
842
843// Host is not behind the NAT.
844TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
845 AddInterface(kClientAddr);
Peter Thatcher2159b892015-08-21 20:46:05 -0700846 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000847 allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
848 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
849 session_->StartGettingPorts();
850 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
851 // Host has a public address, both UDP and TCP candidates will be exposed.
852 EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
853 EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state.
854 for (size_t i = 0; i < candidates_.size(); ++i) {
855 EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
856 }
857}
858
Peter Thatcher2159b892015-08-21 20:46:05 -0700859// Test that we get the same ufrag and pwd for all candidates.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000860TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000861 AddInterface(kClientAddr);
862 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
863 session_->StartGettingPorts();
864 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
865 EXPECT_PRED5(CheckCandidate, candidates_[0],
866 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
867 EXPECT_PRED5(CheckCandidate, candidates_[1],
868 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
869 EXPECT_PRED5(CheckCandidate, candidates_[5],
870 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
871 EXPECT_EQ(4U, ports_.size());
872 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
873 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
874 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
875 EXPECT_EQ(kIcePwd0, candidates_[0].password());
876 EXPECT_EQ(kIcePwd0, candidates_[1].password());
877 EXPECT_TRUE(candidate_allocation_done_);
878}
879
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000880// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
881// is allocated for udp and stun. Also verify there is only one candidate
882// (local) if stun candidate is same as local candidate, which will be the case
883// in a public network like the below test.
884TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
885 AddInterface(kClientAddr);
886 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000887 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
888 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
889 session_->StartGettingPorts();
890 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
891 EXPECT_EQ(3U, ports_.size());
892 EXPECT_PRED5(CheckCandidate, candidates_[0],
893 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
894 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
895}
896
897// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
898// is allocated for udp and stun. In this test we should expect both stun and
899// local candidates as client behind a nat.
900TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
901 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700902 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000903
904 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000905 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
906 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
907 session_->StartGettingPorts();
908 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
909 ASSERT_EQ(2U, ports_.size());
910 EXPECT_PRED5(CheckCandidate, candidates_[0],
911 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
912 EXPECT_PRED5(CheckCandidate, candidates_[1],
913 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700914 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000915 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
916 EXPECT_EQ(3U, candidates_.size());
917}
918
deadbeefc5d0d952015-07-16 10:22:21 -0700919// Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000920TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
921 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
922 AddInterface(kClientAddr);
923 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
924
925 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
926
927 allocator_->set_step_delay(cricket::kMinimumStepDelay);
928 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000929 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
930 cricket::PORTALLOCATOR_DISABLE_TCP);
931
932 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
933 session_->StartGettingPorts();
934
935 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
936 ASSERT_EQ(3U, ports_.size());
937 EXPECT_PRED5(CheckCandidate, candidates_[0],
938 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
939 EXPECT_PRED5(CheckCandidate, candidates_[1],
940 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
941 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
942 EXPECT_PRED5(CheckCandidate, candidates_[2],
943 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
944 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
945 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
946 EXPECT_EQ(3U, candidates_.size());
947}
948
949// Testing DNS resolve for the TURN server, this will test AllocationSequence
950// handling the unresolved address signal from TurnPort.
951TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
952 turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
953 cricket::PROTO_UDP);
954 AddInterface(kClientAddr);
955 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
956 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
957 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
958 relay_server.credentials = credentials;
959 relay_server.ports.push_back(cricket::ProtocolAddress(
960 rtc::SocketAddress("localhost", 3478),
961 cricket::PROTO_UDP, false));
962 allocator_->AddRelay(relay_server);
963
964 allocator_->set_step_delay(cricket::kMinimumStepDelay);
965 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000966 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
967 cricket::PORTALLOCATOR_DISABLE_TCP);
968
969 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
970 session_->StartGettingPorts();
971
972 EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
973}
974
975// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
976// is allocated for udp/stun/turn. In this test we should expect all local,
977// stun and turn candidates.
978TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
979 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -0700980 ResetWithStunServerAndNat(kStunAddr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000981
982 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
983
984 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000985 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
986 cricket::PORTALLOCATOR_DISABLE_TCP);
987
988 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
989 session_->StartGettingPorts();
990
991 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
992 ASSERT_EQ(2U, ports_.size());
993 EXPECT_PRED5(CheckCandidate, candidates_[0],
994 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
995 EXPECT_PRED5(CheckCandidate, candidates_[1],
996 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
deadbeefc5d0d952015-07-16 10:22:21 -0700997 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000998 EXPECT_PRED5(CheckCandidate, candidates_[2],
999 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1000 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1001 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1002 EXPECT_EQ(3U, candidates_.size());
1003 // Local port will be created first and then TURN port.
1004 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1005 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1006}
1007
1008// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1009// server is also used as the STUN server, we should get 'local', 'stun', and
1010// 'relay' candidates.
1011TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1012 AddInterface(kClientAddr);
Jiayang Liud7e5c442015-04-27 11:47:21 -07001013 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001014 ResetWithStunServerAndNat(SocketAddress());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001015 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1016
1017 // Must set the step delay to 0 to make sure the relay allocation phase is
1018 // started before the STUN candidates are obtained, so that the STUN binding
1019 // response is processed when both StunPort and TurnPort exist to reproduce
1020 // webrtc issue 3537.
1021 allocator_->set_step_delay(0);
1022 allocator_->set_flags(allocator().flags() |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001023 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1024 cricket::PORTALLOCATOR_DISABLE_TCP);
1025
1026 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1027 session_->StartGettingPorts();
1028
1029 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
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_EQ(candidates_[2].related_address(), candidates_[1].address());
1039
1040 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1041 EXPECT_EQ(3U, candidates_.size());
1042 // Local port will be created first and then TURN port.
1043 EXPECT_EQ(2U, ports_[0]->Candidates().size());
1044 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1045}
1046
deadbeefc5d0d952015-07-16 10:22:21 -07001047// Test that when only a TCP TURN server is available, we do NOT use it as
1048// a UDP STUN server, as this could leak our IP address. Thus we should only
1049// expect two ports, a UDPPort and TurnPort.
1050TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1051 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1052 AddInterface(kClientAddr);
Guo-wei Shieh11477022015-08-15 09:28:41 -07001053 ResetWithStunServerAndNat(rtc::SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001054 AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1055
1056 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001057 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1058 cricket::PORTALLOCATOR_DISABLE_TCP);
1059
1060 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1061 session_->StartGettingPorts();
1062
1063 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1064 ASSERT_EQ(2U, ports_.size());
1065 EXPECT_PRED5(CheckCandidate, candidates_[0],
1066 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1067 kClientAddr);
1068 EXPECT_PRED5(CheckCandidate, candidates_[1],
1069 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1070 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1071 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1072 EXPECT_EQ(2U, candidates_.size());
1073 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1074 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1075}
1076
1077// Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1078// TURN server is used as the STUN server and we get 'local', 'stun', and
1079// 'relay' candidates.
1080// TODO(deadbeef): Remove this test when support for non-shared socket mode
1081// is removed.
1082TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1083 AddInterface(kClientAddr);
1084 // Use an empty SocketAddress to add a NAT without STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001085 ResetWithStunServerAndNat(SocketAddress());
deadbeefc5d0d952015-07-16 10:22:21 -07001086 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1087
1088 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001089 cricket::PORTALLOCATOR_DISABLE_TCP);
1090
1091 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1092 session_->StartGettingPorts();
1093
1094 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1095 ASSERT_EQ(3U, ports_.size());
1096 EXPECT_PRED5(CheckCandidate, candidates_[0],
1097 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1098 kClientAddr);
1099 EXPECT_PRED5(CheckCandidate, candidates_[1],
1100 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1101 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1102 EXPECT_PRED5(CheckCandidate, candidates_[2],
1103 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1104 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1105 // Not using shared socket, so the STUN request's server reflexive address
1106 // should be different than the TURN request's server reflexive address.
1107 EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1108
1109 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1110 EXPECT_EQ(3U, candidates_.size());
1111 EXPECT_EQ(1U, ports_[0]->Candidates().size());
1112 EXPECT_EQ(1U, ports_[1]->Candidates().size());
1113 EXPECT_EQ(1U, ports_[2]->Candidates().size());
1114}
1115
1116// Test that even when both a STUN and TURN server are configured, the TURN
1117// server is used as a STUN server and we get a 'stun' candidate.
1118TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1119 AddInterface(kClientAddr);
1120 // Configure with STUN server but destroy it, so we can ensure that it's
1121 // the TURN server actually being used as a STUN server.
Guo-wei Shieh11477022015-08-15 09:28:41 -07001122 ResetWithStunServerAndNat(kStunAddr);
deadbeefc5d0d952015-07-16 10:22:21 -07001123 stun_server_.reset();
1124 AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1125
1126 allocator_->set_flags(allocator().flags() |
deadbeefc5d0d952015-07-16 10:22:21 -07001127 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1128 cricket::PORTALLOCATOR_DISABLE_TCP);
1129
1130 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1131 session_->StartGettingPorts();
1132
1133 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1134 EXPECT_PRED5(CheckCandidate, candidates_[0],
1135 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1136 kClientAddr);
1137 EXPECT_PRED5(CheckCandidate, candidates_[1],
1138 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1139 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1140 EXPECT_PRED5(CheckCandidate, candidates_[2],
1141 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1142 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1143 EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1144
1145 // Don't bother waiting for STUN timeout, since we already verified
1146 // that we got a STUN candidate from the TURN server.
1147}
1148
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1150// and fail to generate STUN candidate, local UDP candidate is generated
1151// properly.
1152TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1153 allocator().set_flags(allocator().flags() |
1154 cricket::PORTALLOCATOR_DISABLE_RELAY |
1155 cricket::PORTALLOCATOR_DISABLE_TCP |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001156 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1157 fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1158 AddInterface(kClientAddr);
1159 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1160 session_->StartGettingPorts();
1161 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1162 EXPECT_EQ(1U, candidates_.size());
1163 EXPECT_PRED5(CheckCandidate, candidates_[0],
1164 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1165 // STUN timeout is 9sec. We need to wait to get candidate done signal.
1166 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1167 EXPECT_EQ(1U, candidates_.size());
1168}
1169
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001170// Test that when the NetworkManager doesn't have permission to enumerate
1171// adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1172// automatically.
1173TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
1174 AddInterface(kClientAddr);
1175 network_manager_.set_enumeration_permission(
guoweisea1012b2015-08-21 09:06:28 -07001176 rtc::NetworkManager::ENUMERATION_BLOCKED);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001177 allocator().set_flags(allocator().flags() |
1178 cricket::PORTALLOCATOR_DISABLE_RELAY |
1179 cricket::PORTALLOCATOR_DISABLE_TCP |
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001180 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
guoweisea1012b2015-08-21 09:06:28 -07001181 EXPECT_EQ(0U, allocator_->flags() &
1182 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001183 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
guoweisea1012b2015-08-21 09:06:28 -07001184 EXPECT_EQ(0U, session_->flags() &
1185 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
Guo-wei Shieh47872ec2015-08-19 10:32:46 -07001186 session_->StartGettingPorts();
1187 EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1188 EXPECT_EQ(0U, candidates_.size());
1189 EXPECT_TRUE((session_->flags() &
1190 cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1191}
1192
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001193// This test verifies allocator can use IPv6 addresses along with IPv4.
1194TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1195 allocator().set_flags(allocator().flags() |
1196 cricket::PORTALLOCATOR_DISABLE_RELAY |
1197 cricket::PORTALLOCATOR_ENABLE_IPV6 |
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001198 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1199 AddInterface(kClientIPv6Addr);
1200 AddInterface(kClientAddr);
1201 allocator_->set_step_delay(cricket::kMinimumStepDelay);
1202 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1203 session_->StartGettingPorts();
1204 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1205 EXPECT_EQ(4U, candidates_.size());
1206 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1207 EXPECT_PRED5(CheckCandidate, candidates_[0],
1208 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1209 kClientIPv6Addr);
1210 EXPECT_PRED5(CheckCandidate, candidates_[1],
1211 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1212 kClientAddr);
1213 EXPECT_PRED5(CheckCandidate, candidates_[2],
1214 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1215 kClientIPv6Addr);
1216 EXPECT_PRED5(CheckCandidate, candidates_[3],
1217 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1218 kClientAddr);
1219 EXPECT_EQ(4U, candidates_.size());
1220}