blob: 693d54702cef4a23cab1bbc6ebffdfabc4ad0897 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2009 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/fakenetwork.h"
29#include "talk/base/firewallsocketserver.h"
30#include "talk/base/gunit.h"
31#include "talk/base/helpers.h"
32#include "talk/base/logging.h"
33#include "talk/base/natserver.h"
34#include "talk/base/natsocketfactory.h"
35#include "talk/base/network.h"
36#include "talk/base/physicalsocketserver.h"
37#include "talk/base/socketaddress.h"
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +000038#include "talk/base/ssladapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/base/thread.h"
40#include "talk/base/virtualsocketserver.h"
41#include "talk/p2p/base/basicpacketsocketfactory.h"
42#include "talk/p2p/base/constants.h"
43#include "talk/p2p/base/p2ptransportchannel.h"
44#include "talk/p2p/base/portallocatorsessionproxy.h"
45#include "talk/p2p/base/testrelayserver.h"
46#include "talk/p2p/base/teststunserver.h"
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000047#include "talk/p2p/base/testturnserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/p2p/client/basicportallocator.h"
49#include "talk/p2p/client/httpportallocator.h"
50
51using talk_base::SocketAddress;
52using talk_base::Thread;
53
54static const SocketAddress kClientAddr("11.11.11.11", 0);
wu@webrtc.org97077a32013-10-25 21:18:33 +000055static const SocketAddress kClientIPv6Addr(
56 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000057static const SocketAddress kClientAddr2("22.22.22.22", 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058static const SocketAddress kNatAddr("77.77.77.77", talk_base::NAT_SERVER_PORT);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000059static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
61static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
62static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
63static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
64static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
65static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
66static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000067static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
buildbot@webrtc.org39b868b2014-04-17 00:04:39 +000068static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
69static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71// Minimum and maximum port for port range tests.
72static const int kMinPort = 10000;
73static const int kMaxPort = 10099;
74
75// Based on ICE_UFRAG_LENGTH
76static const char kIceUfrag0[] = "TESTICEUFRAG0000";
77// Based on ICE_PWD_LENGTH
78static const char kIcePwd0[] = "TESTICEPWD00000000000000";
79
80static const char kContentName[] = "test content";
81
82static const int kDefaultAllocationTimeout = 1000;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +000083static const char kTurnUsername[] = "test";
84static const char kTurnPassword[] = "test";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
86namespace cricket {
87
88// Helper for dumping candidates
89std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
90 os << c.ToString();
91 return os;
92}
93
94} // namespace cricket
95
96class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
97 public:
98 static void SetUpTestCase() {
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +000099 talk_base::InitializeSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 }
mallinath@webrtc.org5a59ccb2014-02-07 23:22:00 +0000101
102 static void TearDownTestCase() {
103 talk_base::CleanupSSL();
104 }
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 PortAllocatorTest()
107 : pss_(new talk_base::PhysicalSocketServer),
108 vss_(new talk_base::VirtualSocketServer(pss_.get())),
109 fss_(new talk_base::FirewallSocketServer(vss_.get())),
110 ss_scope_(fss_.get()),
111 nat_factory_(vss_.get(), kNatAddr),
112 nat_socket_factory_(&nat_factory_),
113 stun_server_(Thread::Current(), kStunAddr),
114 relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
115 kRelayTcpIntAddr, kRelayTcpExtAddr,
116 kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000117 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 allocator_(new cricket::BasicPortAllocator(
119 &network_manager_, kStunAddr,
120 kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr)),
121 candidate_allocation_done_(false) {
122 allocator_->set_step_delay(cricket::kMinimumStepDelay);
123 }
124
125 void AddInterface(const SocketAddress& addr) {
126 network_manager_.AddInterface(addr);
127 }
128 bool SetPortRange(int min_port, int max_port) {
129 return allocator_->SetPortRange(min_port, max_port);
130 }
131 talk_base::NATServer* CreateNatServer(const SocketAddress& addr,
132 talk_base::NATType type) {
133 return new talk_base::NATServer(type, vss_.get(), addr, vss_.get(), addr);
134 }
135
136 bool CreateSession(int component) {
137 session_.reset(CreateSession("session", component));
138 if (!session_)
139 return false;
140 return true;
141 }
142
143 bool CreateSession(int component, const std::string& content_name) {
144 session_.reset(CreateSession("session", content_name, component));
145 if (!session_)
146 return false;
147 return true;
148 }
149
150 cricket::PortAllocatorSession* CreateSession(
151 const std::string& sid, int component) {
152 return CreateSession(sid, kContentName, component);
153 }
154
155 cricket::PortAllocatorSession* CreateSession(
156 const std::string& sid, const std::string& content_name, int component) {
157 return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
158 }
159
160 cricket::PortAllocatorSession* CreateSession(
161 const std::string& sid, const std::string& content_name, int component,
162 const std::string& ice_ufrag, const std::string& ice_pwd) {
163 cricket::PortAllocatorSession* session =
164 allocator_->CreateSession(
165 sid, content_name, component, ice_ufrag, ice_pwd);
166 session->SignalPortReady.connect(this,
167 &PortAllocatorTest::OnPortReady);
168 session->SignalCandidatesReady.connect(this,
169 &PortAllocatorTest::OnCandidatesReady);
170 session->SignalCandidatesAllocationDone.connect(this,
171 &PortAllocatorTest::OnCandidatesAllocationDone);
172 return session;
173 }
174
175 static bool CheckCandidate(const cricket::Candidate& c,
176 int component, const std::string& type,
177 const std::string& proto,
178 const SocketAddress& addr) {
179 return (c.component() == component && c.type() == type &&
180 c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
181 ((addr.port() == 0 && (c.address().port() != 0)) ||
182 (c.address().port() == addr.port())));
183 }
184 static bool CheckPort(const talk_base::SocketAddress& addr,
185 int min_port, int max_port) {
186 return (addr.port() >= min_port && addr.port() <= max_port);
187 }
188
189 void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
190 // We should only get this callback once, except in the mux test where
191 // we have multiple port allocation sessions.
192 if (session == session_.get()) {
193 ASSERT_FALSE(candidate_allocation_done_);
194 candidate_allocation_done_ = true;
195 }
196 }
197
198 // Check if all ports allocated have send-buffer size |expected|. If
199 // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
200 void CheckSendBufferSizesOfAllPorts(int expected) {
201 std::vector<cricket::PortInterface*>::iterator it;
202 for (it = ports_.begin(); it < ports_.end(); ++it) {
203 int send_buffer_size;
204 if (expected == -1) {
205 EXPECT_EQ(SOCKET_ERROR,
206 (*it)->GetOption(talk_base::Socket::OPT_SNDBUF,
207 &send_buffer_size));
208 } else {
209 EXPECT_EQ(0, (*it)->GetOption(talk_base::Socket::OPT_SNDBUF,
210 &send_buffer_size));
211 ASSERT_EQ(expected, send_buffer_size);
212 }
213 }
214 }
215
216 protected:
217 cricket::BasicPortAllocator& allocator() {
218 return *allocator_;
219 }
220
221 void OnPortReady(cricket::PortAllocatorSession* ses,
222 cricket::PortInterface* port) {
223 LOG(LS_INFO) << "OnPortReady: " << port->ToString();
224 ports_.push_back(port);
225 }
226 void OnCandidatesReady(cricket::PortAllocatorSession* ses,
227 const std::vector<cricket::Candidate>& candidates) {
228 for (size_t i = 0; i < candidates.size(); ++i) {
229 LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
230 candidates_.push_back(candidates[i]);
231 }
232 }
233
234 bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
235 for (size_t i = 0; i < allocator_->relays().size(); ++i) {
236 cricket::RelayServerConfig server_config = allocator_->relays()[i];
237 cricket::PortList::const_iterator relay_port;
238 for (relay_port = server_config.ports.begin();
239 relay_port != server_config.ports.end(); ++relay_port) {
240 if (proto_addr.address == relay_port->address &&
241 proto_addr.proto == relay_port->proto)
242 return true;
243 }
244 }
245 return false;
246 }
247
248 talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
249 talk_base::scoped_ptr<talk_base::VirtualSocketServer> vss_;
250 talk_base::scoped_ptr<talk_base::FirewallSocketServer> fss_;
251 talk_base::SocketServerScope ss_scope_;
252 talk_base::NATSocketFactory nat_factory_;
253 talk_base::BasicPacketSocketFactory nat_socket_factory_;
254 cricket::TestStunServer stun_server_;
255 cricket::TestRelayServer relay_server_;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000256 cricket::TestTurnServer turn_server_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 talk_base::FakeNetworkManager network_manager_;
258 talk_base::scoped_ptr<cricket::BasicPortAllocator> allocator_;
259 talk_base::scoped_ptr<cricket::PortAllocatorSession> session_;
260 std::vector<cricket::PortInterface*> ports_;
261 std::vector<cricket::Candidate> candidates_;
262 bool candidate_allocation_done_;
263};
264
265// Tests that we can init the port allocator and create a session.
266TEST_F(PortAllocatorTest, TestBasic) {
267 EXPECT_EQ(&network_manager_, allocator().network_manager());
268 EXPECT_EQ(kStunAddr, allocator().stun_address());
269 ASSERT_EQ(1u, allocator().relays().size());
270 EXPECT_EQ(cricket::RELAY_GTURN, allocator().relays()[0].type);
271 // Empty relay credentials are used for GTURN.
272 EXPECT_TRUE(allocator().relays()[0].credentials.username.empty());
273 EXPECT_TRUE(allocator().relays()[0].credentials.password.empty());
274 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
275 kRelayUdpIntAddr, cricket::PROTO_UDP)));
276 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
277 kRelayTcpIntAddr, cricket::PROTO_TCP)));
278 EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
279 kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
280 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
281}
282
283// Tests that we can get all the desired addresses successfully.
284TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
285 AddInterface(kClientAddr);
286 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
287 session_->StartGettingPorts();
288 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
289 EXPECT_EQ(4U, ports_.size());
290 EXPECT_PRED5(CheckCandidate, candidates_[0],
291 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
292 EXPECT_PRED5(CheckCandidate, candidates_[1],
293 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
294 EXPECT_PRED5(CheckCandidate, candidates_[2],
295 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
296 EXPECT_PRED5(CheckCandidate, candidates_[3],
297 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
298 EXPECT_PRED5(CheckCandidate, candidates_[4],
299 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
300 EXPECT_PRED5(CheckCandidate, candidates_[5],
301 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
302 EXPECT_PRED5(CheckCandidate, candidates_[6],
303 cricket::ICE_CANDIDATE_COMPONENT_RTP,
304 "relay", "ssltcp", kRelaySslTcpIntAddr);
305 EXPECT_TRUE(candidate_allocation_done_);
306}
307
308// Verify candidates with default step delay of 1sec.
309TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
310 AddInterface(kClientAddr);
311 allocator_->set_step_delay(cricket::kDefaultStepDelay);
312 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
313 session_->StartGettingPorts();
314 ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
315 EXPECT_EQ(2U, ports_.size());
316 ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
317 EXPECT_EQ(3U, ports_.size());
318 EXPECT_PRED5(CheckCandidate, candidates_[2],
319 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
320 EXPECT_PRED5(CheckCandidate, candidates_[3],
321 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
322 ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
323 EXPECT_PRED5(CheckCandidate, candidates_[4],
324 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
325 EXPECT_PRED5(CheckCandidate, candidates_[5],
326 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
327 EXPECT_EQ(4U, ports_.size());
328 ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
329 EXPECT_PRED5(CheckCandidate, candidates_[6],
330 cricket::ICE_CANDIDATE_COMPONENT_RTP,
331 "relay", "ssltcp", kRelaySslTcpIntAddr);
332 EXPECT_EQ(4U, ports_.size());
333 EXPECT_TRUE(candidate_allocation_done_);
334 // If we Stop gathering now, we shouldn't get a second "done" callback.
335 session_->StopGettingPorts();
336}
337
338TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
339 AddInterface(kClientAddr);
340 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
341 cricket::CN_VIDEO));
342 session_->StartGettingPorts();
343 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
344 EXPECT_TRUE(candidate_allocation_done_);
345 // If we Stop gathering now, we shouldn't get a second "done" callback.
346 session_->StopGettingPorts();
347
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000348 // All ports should have unset send-buffer sizes.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 CheckSendBufferSizesOfAllPorts(-1);
350}
351
352// Tests that we can get callback after StopGetAllPorts.
353TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
354 AddInterface(kClientAddr);
355 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
356 session_->StartGettingPorts();
357 ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
358 EXPECT_EQ(2U, ports_.size());
359 session_->StopGettingPorts();
360 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
361}
362
363// Test that we restrict client ports appropriately when a port range is set.
364// We check the candidates for udp/stun/tcp ports, and the from address
365// for relay ports.
366TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
367 AddInterface(kClientAddr);
368 // Check that an invalid port range fails.
369 EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
370 // Check that a null port range succeeds.
371 EXPECT_TRUE(SetPortRange(0, 0));
372 // Check that a valid port range succeeds.
373 EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
374 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
375 session_->StartGettingPorts();
376 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
377 EXPECT_EQ(4U, ports_.size());
378 // Check the port number for the UDP port object.
379 EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
380 // Check the port number for the STUN port object.
381 EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
382 // Check the port number used to connect to the relay server.
383 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
384 kMinPort, kMaxPort);
385 // Check the port number for the TCP port object.
386 EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
387 EXPECT_TRUE(candidate_allocation_done_);
388}
389
390// Test that we don't crash or malfunction if we have no network adapters.
391TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
392 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
393 session_->StartGettingPorts();
394 talk_base::Thread::Current()->ProcessMessages(100);
395 // Without network adapter, we should not get any candidate.
396 EXPECT_EQ(0U, candidates_.size());
397 EXPECT_TRUE(candidate_allocation_done_);
398}
399
400// Test that we can get OnCandidatesAllocationDone callback when all the ports
401// are disabled.
402TEST_F(PortAllocatorTest, TestDisableAllPorts) {
403 AddInterface(kClientAddr);
404 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
405 session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
406 cricket::PORTALLOCATOR_DISABLE_STUN |
407 cricket::PORTALLOCATOR_DISABLE_RELAY |
408 cricket::PORTALLOCATOR_DISABLE_TCP);
409 session_->StartGettingPorts();
410 talk_base::Thread::Current()->ProcessMessages(100);
411 EXPECT_EQ(0U, candidates_.size());
412 EXPECT_TRUE(candidate_allocation_done_);
413}
414
415// Test that we don't crash or malfunction if we can't create UDP sockets.
416TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
417 AddInterface(kClientAddr);
418 fss_->set_udp_sockets_enabled(false);
419 EXPECT_TRUE(CreateSession(1));
420 session_->StartGettingPorts();
421 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
422 EXPECT_EQ(2U, ports_.size());
423 EXPECT_PRED5(CheckCandidate, candidates_[0],
424 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
425 EXPECT_PRED5(CheckCandidate, candidates_[1],
426 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
427 EXPECT_PRED5(CheckCandidate, candidates_[2],
428 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
429 EXPECT_PRED5(CheckCandidate, candidates_[3],
430 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
431 EXPECT_PRED5(CheckCandidate, candidates_[4],
432 cricket::ICE_CANDIDATE_COMPONENT_RTP,
433 "relay", "ssltcp", kRelaySslTcpIntAddr);
434 EXPECT_TRUE(candidate_allocation_done_);
435}
436
437// Test that we don't crash or malfunction if we can't create UDP sockets or
438// listen on TCP sockets. We still give out a local TCP address, since
439// apparently this is needed for the remote side to accept our connection.
440TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
441 AddInterface(kClientAddr);
442 fss_->set_udp_sockets_enabled(false);
443 fss_->set_tcp_listen_enabled(false);
444 EXPECT_TRUE(CreateSession(1));
445 session_->StartGettingPorts();
446 ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
447 EXPECT_EQ(2U, ports_.size());
448 EXPECT_PRED5(CheckCandidate, candidates_[0],
449 1, "relay", "udp", kRelayUdpIntAddr);
450 EXPECT_PRED5(CheckCandidate, candidates_[1],
451 1, "relay", "udp", kRelayUdpExtAddr);
452 EXPECT_PRED5(CheckCandidate, candidates_[2],
453 1, "relay", "tcp", kRelayTcpIntAddr);
454 EXPECT_PRED5(CheckCandidate, candidates_[3],
455 1, "local", "tcp", kClientAddr);
456 EXPECT_PRED5(CheckCandidate, candidates_[4],
457 1, "relay", "ssltcp", kRelaySslTcpIntAddr);
458 EXPECT_TRUE(candidate_allocation_done_);
459}
460
461// Test that we don't crash or malfunction if we can't create any sockets.
462// TODO: Find a way to exit early here.
463TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
464 AddInterface(kClientAddr);
465 fss_->set_tcp_sockets_enabled(false);
466 fss_->set_udp_sockets_enabled(false);
467 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
468 session_->StartGettingPorts();
469 WAIT(candidates_.size() > 0, 2000);
470 // TODO - Check candidate_allocation_done signal.
471 // In case of Relay, ports creation will succeed but sockets will fail.
472 // There is no error reporting from RelayEntry to handle this failure.
473}
474
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475// Testing STUN timeout.
476TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
477 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
478 AddInterface(kClientAddr);
479 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
480 session_->StartGettingPorts();
481 EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
482 EXPECT_EQ(2U, ports_.size());
483 EXPECT_PRED5(CheckCandidate, candidates_[0],
484 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
485 EXPECT_PRED5(CheckCandidate, candidates_[1],
486 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
487 // RelayPort connection timeout is 3sec. TCP connection with RelayServer
488 // will be tried after 3 seconds.
489 EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
490 EXPECT_EQ(3U, ports_.size());
491 EXPECT_PRED5(CheckCandidate, candidates_[2],
492 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
493 EXPECT_PRED5(CheckCandidate, candidates_[3],
494 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
495 EXPECT_PRED5(CheckCandidate, candidates_[4],
496 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
497 kRelaySslTcpIntAddr);
498 EXPECT_PRED5(CheckCandidate, candidates_[5],
499 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
500 // Stun Timeout is 9sec.
501 EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
502}
503
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000504TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
505 AddInterface(kClientAddr);
506 AddInterface(kClientAddr2);
507 // Allocating only host UDP ports. This is done purely for testing
508 // convenience.
509 allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
510 cricket::PORTALLOCATOR_DISABLE_STUN |
511 cricket::PORTALLOCATOR_DISABLE_RELAY);
512 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
513 session_->StartGettingPorts();
514 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
515 ASSERT_EQ(2U, candidates_.size());
516 EXPECT_EQ(2U, ports_.size());
517 // Candidates priorities should be different.
518 EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
519}
520
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521// Test to verify ICE restart process.
522TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
523 AddInterface(kClientAddr);
524 EXPECT_TRUE(CreateSession(1));
525 session_->StartGettingPorts();
526 EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
527 EXPECT_EQ(4U, ports_.size());
528 EXPECT_TRUE(candidate_allocation_done_);
529 // TODO - Extend this to verify ICE restart.
530}
531
532TEST_F(PortAllocatorTest, TestBasicMuxFeatures) {
533 AddInterface(kClientAddr);
534 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
535 // Session ID - session1.
536 talk_base::scoped_ptr<cricket::PortAllocatorSession> session1(
537 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
538 talk_base::scoped_ptr<cricket::PortAllocatorSession> session2(
539 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTCP));
540 session1->StartGettingPorts();
541 session2->StartGettingPorts();
542 // Each session should receive two proxy ports of local and stun.
543 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
544 EXPECT_EQ(8U, ports_.size());
545
546 talk_base::scoped_ptr<cricket::PortAllocatorSession> session3(
547 CreateSession("session1", cricket::ICE_CANDIDATE_COMPONENT_RTP));
548 session3->StartGettingPorts();
549 // Already allocated candidates and ports will be sent to the newly
550 // allocated proxy session.
551 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
552 EXPECT_EQ(12U, ports_.size());
553}
554
555// This test verifies by changing ice_ufrag and/or ice_pwd
556// will result in different set of candidates when BUNDLE is enabled.
557// If BUNDLE is disabled, CreateSession will always allocate new
558// set of candidates.
559TEST_F(PortAllocatorTest, TestBundleIceRestart) {
560 AddInterface(kClientAddr);
561 allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_BUNDLE);
562 // Session ID - session1.
563 talk_base::scoped_ptr<cricket::PortAllocatorSession> session1(
564 CreateSession("session1", kContentName,
565 cricket::ICE_CANDIDATE_COMPONENT_RTP,
566 kIceUfrag0, kIcePwd0));
567 session1->StartGettingPorts();
568 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
569 EXPECT_EQ(4U, ports_.size());
570
571 // Allocate a different session with sid |session1| and different ice_ufrag.
572 talk_base::scoped_ptr<cricket::PortAllocatorSession> session2(
573 CreateSession("session1", kContentName,
574 cricket::ICE_CANDIDATE_COMPONENT_RTP,
575 "TestIceUfrag", kIcePwd0));
576 session2->StartGettingPorts();
577 ASSERT_EQ_WAIT(14U, candidates_.size(), kDefaultAllocationTimeout);
578 EXPECT_EQ(8U, ports_.size());
579 // Verifying the candidate address different from previously allocated
580 // address.
581 // Skipping verification of component id and candidate type.
582 EXPECT_NE(candidates_[0].address(), candidates_[7].address());
583 EXPECT_NE(candidates_[1].address(), candidates_[8].address());
584
585 // Allocating a different session with sid |session1| and
586 // different ice_pwd.
587 talk_base::scoped_ptr<cricket::PortAllocatorSession> session3(
588 CreateSession("session1", kContentName,
589 cricket::ICE_CANDIDATE_COMPONENT_RTP,
590 kIceUfrag0, "TestIcePwd"));
591 session3->StartGettingPorts();
592 ASSERT_EQ_WAIT(21U, candidates_.size(), kDefaultAllocationTimeout);
593 EXPECT_EQ(12U, ports_.size());
594 // Verifying the candidate address different from previously
595 // allocated address.
596 EXPECT_NE(candidates_[7].address(), candidates_[14].address());
597 EXPECT_NE(candidates_[8].address(), candidates_[15].address());
598
599 // Allocating a session with by changing both ice_ufrag and ice_pwd.
600 talk_base::scoped_ptr<cricket::PortAllocatorSession> session4(
601 CreateSession("session1", kContentName,
602 cricket::ICE_CANDIDATE_COMPONENT_RTP,
603 "TestIceUfrag", "TestIcePwd"));
604 session4->StartGettingPorts();
605 ASSERT_EQ_WAIT(28U, candidates_.size(), kDefaultAllocationTimeout);
606 EXPECT_EQ(16U, ports_.size());
607 // Verifying the candidate address different from previously
608 // allocated address.
609 EXPECT_NE(candidates_[14].address(), candidates_[21].address());
610 EXPECT_NE(candidates_[15].address(), candidates_[22].address());
611}
612
613// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG is enabled we got same
614// ufrag and pwd for the collected candidates.
615TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
616 allocator().set_flags(allocator().flags() |
617 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
618 AddInterface(kClientAddr);
619 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
620 session_->StartGettingPorts();
621 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
622 EXPECT_PRED5(CheckCandidate, candidates_[0],
623 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
624 EXPECT_PRED5(CheckCandidate, candidates_[1],
625 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
626 EXPECT_PRED5(CheckCandidate, candidates_[5],
627 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
628 EXPECT_EQ(4U, ports_.size());
629 EXPECT_EQ(kIceUfrag0, candidates_[0].username());
630 EXPECT_EQ(kIceUfrag0, candidates_[1].username());
631 EXPECT_EQ(kIceUfrag0, candidates_[2].username());
632 EXPECT_EQ(kIcePwd0, candidates_[0].password());
633 EXPECT_EQ(kIcePwd0, candidates_[1].password());
634 EXPECT_TRUE(candidate_allocation_done_);
635}
636
637// Test that when the PORTALLOCATOR_ENABLE_SHARED_UFRAG isn't enabled we got
638// different ufrag and pwd for the collected candidates.
639TEST_F(PortAllocatorTest, TestDisableSharedUfrag) {
640 allocator().set_flags(allocator().flags() &
641 ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
642 AddInterface(kClientAddr);
643 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
644 session_->StartGettingPorts();
645 ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
646 EXPECT_PRED5(CheckCandidate, candidates_[0],
647 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
648 EXPECT_PRED5(CheckCandidate, candidates_[1],
649 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
650 EXPECT_EQ(4U, ports_.size());
651 // Port should generate random ufrag and pwd.
652 EXPECT_NE(kIceUfrag0, candidates_[0].username());
653 EXPECT_NE(kIceUfrag0, candidates_[1].username());
654 EXPECT_NE(candidates_[0].username(), candidates_[1].username());
655 EXPECT_NE(kIcePwd0, candidates_[0].password());
656 EXPECT_NE(kIcePwd0, candidates_[1].password());
657 EXPECT_NE(candidates_[0].password(), candidates_[1].password());
658 EXPECT_TRUE(candidate_allocation_done_);
659}
660
661// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
662// is allocated for udp and stun. Also verify there is only one candidate
663// (local) if stun candidate is same as local candidate, which will be the case
664// in a public network like the below test.
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000665TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 AddInterface(kClientAddr);
667 allocator_->set_flags(allocator().flags() |
668 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
669 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
670 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
671 session_->StartGettingPorts();
672 ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
673 EXPECT_EQ(3U, ports_.size());
674 EXPECT_PRED5(CheckCandidate, candidates_[0],
675 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
676 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
677}
678
679// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
680// is allocated for udp and stun. In this test we should expect both stun and
681// local candidates as client behind a nat.
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000682TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 AddInterface(kClientAddr);
684 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
685 CreateNatServer(kNatAddr, talk_base::NAT_OPEN_CONE));
686 allocator_.reset(new cricket::BasicPortAllocator(
687 &network_manager_, &nat_socket_factory_, kStunAddr));
688 allocator_->set_step_delay(cricket::kMinimumStepDelay);
689 allocator_->set_flags(allocator().flags() |
690 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
691 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
692 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
693 session_->StartGettingPorts();
694 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
695 ASSERT_EQ(2U, ports_.size());
696 EXPECT_PRED5(CheckCandidate, candidates_[0],
697 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
698 EXPECT_PRED5(CheckCandidate, candidates_[1],
699 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
700 talk_base::SocketAddress(kNatAddr.ipaddr(), 0));
701 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
702 EXPECT_EQ(3U, candidates_.size());
703}
704
buildbot@webrtc.org39b868b2014-04-17 00:04:39 +0000705// Test TURN port in shared socket mode with UDP and TCP TURN server adderesses.
706TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
707 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
708 AddInterface(kClientAddr);
709 allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
710 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
711 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
712 relay_server.credentials = credentials;
713 relay_server.ports.push_back(cricket::ProtocolAddress(
714 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
715 relay_server.ports.push_back(cricket::ProtocolAddress(
716 kTurnTcpIntAddr, cricket::PROTO_TCP, false));
717 allocator_->AddRelay(relay_server);
718
719 allocator_->set_step_delay(cricket::kMinimumStepDelay);
720 allocator_->set_flags(allocator().flags() |
721 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
722 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
723 cricket::PORTALLOCATOR_DISABLE_TCP);
724
725 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
726 session_->StartGettingPorts();
727
728 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
729 ASSERT_EQ(3U, ports_.size());
730 EXPECT_PRED5(CheckCandidate, candidates_[0],
731 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
732 EXPECT_PRED5(CheckCandidate, candidates_[1],
733 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
734 talk_base::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
735 EXPECT_PRED5(CheckCandidate, candidates_[2],
736 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
737 talk_base::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
738 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
739 EXPECT_EQ(3U, candidates_.size());
740}
741
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000742// Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
743// is allocated for udp/stun/turn. In this test we should expect all local,
744// stun and turn candidates.
745TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
746 AddInterface(kClientAddr);
747 talk_base::scoped_ptr<talk_base::NATServer> nat_server(
748 CreateNatServer(kNatAddr, talk_base::NAT_OPEN_CONE));
749 allocator_.reset(new cricket::BasicPortAllocator(
750 &network_manager_, &nat_socket_factory_, kStunAddr));
751 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
752 cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
753 relay_server.credentials = credentials;
754 relay_server.ports.push_back(cricket::ProtocolAddress(
755 kTurnUdpIntAddr, cricket::PROTO_UDP, false));
756 allocator_->AddRelay(relay_server);
757
758 allocator_->set_step_delay(cricket::kMinimumStepDelay);
759 allocator_->set_flags(allocator().flags() |
760 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
761 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
762 cricket::PORTALLOCATOR_DISABLE_TCP);
buildbot@webrtc.org39b868b2014-04-17 00:04:39 +0000763
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000764 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
765 session_->StartGettingPorts();
buildbot@webrtc.org39b868b2014-04-17 00:04:39 +0000766
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000767 ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
768 ASSERT_EQ(2U, ports_.size());
769 EXPECT_PRED5(CheckCandidate, candidates_[0],
770 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
771 EXPECT_PRED5(CheckCandidate, candidates_[1],
772 cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
773 talk_base::SocketAddress(kNatAddr.ipaddr(), 0));
774 EXPECT_PRED5(CheckCandidate, candidates_[2],
775 cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
776 talk_base::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
777 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
778 EXPECT_EQ(3U, candidates_.size());
779 // Local port will be created first and then TURN port.
780 // Checking TURN port has two candidates, STUN + TURN.
781 EXPECT_EQ(1U, ports_[0]->Candidates().size());
782 EXPECT_EQ(2U, ports_[1]->Candidates().size());
783}
784
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785// This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
786// and fail to generate STUN candidate, local UDP candidate is generated
787// properly.
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000788TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 allocator().set_flags(allocator().flags() |
790 cricket::PORTALLOCATOR_DISABLE_RELAY |
791 cricket::PORTALLOCATOR_DISABLE_TCP |
792 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
793 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
794 fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
795 AddInterface(kClientAddr);
796 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
797 session_->StartGettingPorts();
798 ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
799 EXPECT_EQ(1U, candidates_.size());
800 EXPECT_PRED5(CheckCandidate, candidates_[0],
801 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
802 // STUN timeout is 9sec. We need to wait to get candidate done signal.
803 EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
804 EXPECT_EQ(1U, candidates_.size());
805}
806
wu@webrtc.org97077a32013-10-25 21:18:33 +0000807// This test verifies allocator can use IPv6 addresses along with IPv4.
808TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
809 allocator().set_flags(allocator().flags() |
810 cricket::PORTALLOCATOR_DISABLE_RELAY |
811 cricket::PORTALLOCATOR_ENABLE_IPV6 |
812 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
813 cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
814 AddInterface(kClientIPv6Addr);
815 AddInterface(kClientAddr);
816 allocator_->set_step_delay(cricket::kMinimumStepDelay);
817 EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
818 session_->StartGettingPorts();
819 ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
820 EXPECT_EQ(4U, candidates_.size());
821 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
822 EXPECT_PRED5(CheckCandidate, candidates_[0],
823 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
824 kClientIPv6Addr);
825 EXPECT_PRED5(CheckCandidate, candidates_[1],
826 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
827 kClientAddr);
828 EXPECT_PRED5(CheckCandidate, candidates_[2],
829 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
830 kClientIPv6Addr);
831 EXPECT_PRED5(CheckCandidate, candidates_[3],
832 cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
833 kClientAddr);
834 EXPECT_EQ(4U, candidates_.size());
835}
836
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837// Test that the httpportallocator correctly maintains its lists of stun and
838// relay servers, by never allowing an empty list.
839TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
840 talk_base::FakeNetworkManager network_manager;
841 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
842 EXPECT_EQ(1U, alloc.relay_hosts().size());
843 EXPECT_EQ(1U, alloc.stun_hosts().size());
844
845 std::vector<std::string> relay_servers;
846 std::vector<talk_base::SocketAddress> stun_servers;
847
848 alloc.SetRelayHosts(relay_servers);
849 alloc.SetStunHosts(stun_servers);
850 EXPECT_EQ(1U, alloc.relay_hosts().size());
851 EXPECT_EQ(1U, alloc.stun_hosts().size());
852
853 relay_servers.push_back("1.unittest.corp.google.com");
854 relay_servers.push_back("2.unittest.corp.google.com");
855 stun_servers.push_back(
856 talk_base::SocketAddress("1.unittest.corp.google.com", 0));
857 stun_servers.push_back(
858 talk_base::SocketAddress("2.unittest.corp.google.com", 0));
859 alloc.SetRelayHosts(relay_servers);
860 alloc.SetStunHosts(stun_servers);
861 EXPECT_EQ(2U, alloc.relay_hosts().size());
862 EXPECT_EQ(2U, alloc.stun_hosts().size());
863}
864
865// Test that the HttpPortAllocator uses correct URL to create sessions.
866TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
867 talk_base::FakeNetworkManager network_manager;
868 cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
869
870 // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
871 alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
872 talk_base::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
873 static_cast<cricket::HttpPortAllocatorSession*>(
874 alloc.CreateSessionInternal(
875 "test content", 0, kIceUfrag0, kIcePwd0)));
876 std::string url = session->GetSessionRequestUrl();
877 LOG(LS_INFO) << "url: " << url;
878 EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
879
880 // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
881 alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
882 session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
883 alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
884 url = session->GetSessionRequestUrl();
885 LOG(LS_INFO) << "url: " << url;
886 std::vector<std::string> parts;
887 talk_base::split(url, '?', &parts);
888 ASSERT_EQ(2U, parts.size());
889
890 std::vector<std::string> args_parts;
891 talk_base::split(parts[1], '&', &args_parts);
892
893 std::map<std::string, std::string> args;
894 for (std::vector<std::string>::iterator it = args_parts.begin();
895 it != args_parts.end(); ++it) {
896 std::vector<std::string> parts;
897 talk_base::split(*it, '=', &parts);
898 ASSERT_EQ(2U, parts.size());
899 args[talk_base::s_url_decode(parts[0])] = talk_base::s_url_decode(parts[1]);
900 }
901
902 EXPECT_EQ(kIceUfrag0, args["username"]);
903 EXPECT_EQ(kIcePwd0, args["password"]);
904}