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