Upstream version 7.36.149.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/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"
38 #include "talk/base/ssladapter.h"
39 #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"
47 #include "talk/p2p/base/testturnserver.h"
48 #include "talk/p2p/client/basicportallocator.h"
49 #include "talk/p2p/client/httpportallocator.h"
50
51 using talk_base::SocketAddress;
52 using talk_base::Thread;
53
54 static const SocketAddress kClientAddr("11.11.11.11", 0);
55 static const SocketAddress kClientIPv6Addr(
56     "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
57 static const SocketAddress kClientAddr2("22.22.22.22", 0);
58 static const SocketAddress kNatAddr("77.77.77.77", talk_base::NAT_SERVER_PORT);
59 static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
60 static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
61 static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
62 static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
63 static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
64 static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
65 static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
66 static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
67 static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
68 static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
69 static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
70
71 // Minimum and maximum port for port range tests.
72 static const int kMinPort = 10000;
73 static const int kMaxPort = 10099;
74
75 // Based on ICE_UFRAG_LENGTH
76 static const char kIceUfrag0[] = "TESTICEUFRAG0000";
77 // Based on ICE_PWD_LENGTH
78 static const char kIcePwd0[] = "TESTICEPWD00000000000000";
79
80 static const char kContentName[] = "test content";
81
82 static const int kDefaultAllocationTimeout = 1000;
83 static const char kTurnUsername[] = "test";
84 static const char kTurnPassword[] = "test";
85
86 namespace cricket {
87
88 // Helper for dumping candidates
89 std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
90   os << c.ToString();
91   return os;
92 }
93
94 }  // namespace cricket
95
96 class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
97  public:
98   static void SetUpTestCase() {
99     talk_base::InitializeSSL();
100   }
101
102   static void TearDownTestCase() {
103     talk_base::CleanupSSL();
104   }
105
106   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),
117         turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
118         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_;
256   cricket::TestTurnServer turn_server_;
257   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.
266 TEST_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.
284 TEST_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.
309 TEST_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
338 TEST_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
348   // All ports should have unset send-buffer sizes.
349   CheckSendBufferSizesOfAllPorts(-1);
350 }
351
352 // Tests that we can get callback after StopGetAllPorts.
353 TEST_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.
366 TEST_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.
391 TEST_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.
402 TEST_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.
416 TEST_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.
440 TEST_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.
463 TEST_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
475 // Testing STUN timeout.
476 TEST_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
504 TEST_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
521 // Test to verify ICE restart process.
522 TEST_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
532 TEST_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.
559 TEST_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.
615 TEST_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.
639 TEST_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.
665 TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
666   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.
682 TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
683   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
705 // Test TURN port in shared socket mode with UDP and TCP TURN server adderesses.
706 TEST_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
742 // Testing DNS resolve for the TURN server, this will test AllocationSequence
743 // handling the unresolved address signal from TurnPort.
744 TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
745   turn_server_.AddInternalSocket(talk_base::SocketAddress("127.0.0.1", 3478),
746                                  cricket::PROTO_UDP);
747   AddInterface(kClientAddr);
748   allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
749   cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
750   cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
751   relay_server.credentials = credentials;
752   relay_server.ports.push_back(cricket::ProtocolAddress(
753       talk_base::SocketAddress("localhost", 3478),
754       cricket::PROTO_UDP, false));
755   allocator_->AddRelay(relay_server);
756
757   allocator_->set_step_delay(cricket::kMinimumStepDelay);
758   allocator_->set_flags(allocator().flags() |
759                         cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
760                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
761                         cricket::PORTALLOCATOR_DISABLE_TCP);
762
763   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
764   session_->StartGettingPorts();
765
766   EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
767 }
768
769 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
770 // is allocated for udp/stun/turn. In this test we should expect all local,
771 // stun and turn candidates.
772 TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
773   AddInterface(kClientAddr);
774   talk_base::scoped_ptr<talk_base::NATServer> nat_server(
775       CreateNatServer(kNatAddr, talk_base::NAT_OPEN_CONE));
776   allocator_.reset(new cricket::BasicPortAllocator(
777       &network_manager_, &nat_socket_factory_, kStunAddr));
778   cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
779   cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
780   relay_server.credentials = credentials;
781   relay_server.ports.push_back(cricket::ProtocolAddress(
782       kTurnUdpIntAddr, cricket::PROTO_UDP, false));
783   allocator_->AddRelay(relay_server);
784
785   allocator_->set_step_delay(cricket::kMinimumStepDelay);
786   allocator_->set_flags(allocator().flags() |
787                         cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
788                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
789                         cricket::PORTALLOCATOR_DISABLE_TCP);
790
791   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
792   session_->StartGettingPorts();
793
794   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
795   ASSERT_EQ(2U, ports_.size());
796   EXPECT_PRED5(CheckCandidate, candidates_[0],
797       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
798   EXPECT_PRED5(CheckCandidate, candidates_[1],
799       cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
800       talk_base::SocketAddress(kNatAddr.ipaddr(), 0));
801   EXPECT_PRED5(CheckCandidate, candidates_[2],
802       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
803       talk_base::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
804   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
805   EXPECT_EQ(3U, candidates_.size());
806   // Local port will be created first and then TURN port.
807   // Checking TURN port has two candidates, STUN + TURN.
808   EXPECT_EQ(1U, ports_[0]->Candidates().size());
809   EXPECT_EQ(2U, ports_[1]->Candidates().size());
810 }
811
812 // This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
813 // and fail to generate STUN candidate, local UDP candidate is generated
814 // properly.
815 TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
816   allocator().set_flags(allocator().flags() |
817                         cricket::PORTALLOCATOR_DISABLE_RELAY |
818                         cricket::PORTALLOCATOR_DISABLE_TCP |
819                         cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
820                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
821   fss_->AddRule(false, talk_base::FP_UDP, talk_base::FD_ANY, kClientAddr);
822   AddInterface(kClientAddr);
823   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
824   session_->StartGettingPorts();
825   ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
826   EXPECT_EQ(1U, candidates_.size());
827   EXPECT_PRED5(CheckCandidate, candidates_[0],
828       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
829   // STUN timeout is 9sec. We need to wait to get candidate done signal.
830   EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
831   EXPECT_EQ(1U, candidates_.size());
832 }
833
834 // This test verifies allocator can use IPv6 addresses along with IPv4.
835 TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
836   allocator().set_flags(allocator().flags() |
837                         cricket::PORTALLOCATOR_DISABLE_RELAY |
838                         cricket::PORTALLOCATOR_ENABLE_IPV6 |
839                         cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
840                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
841   AddInterface(kClientIPv6Addr);
842   AddInterface(kClientAddr);
843   allocator_->set_step_delay(cricket::kMinimumStepDelay);
844   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
845   session_->StartGettingPorts();
846   ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
847   EXPECT_EQ(4U, candidates_.size());
848   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
849   EXPECT_PRED5(CheckCandidate, candidates_[0],
850       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
851       kClientIPv6Addr);
852   EXPECT_PRED5(CheckCandidate, candidates_[1],
853       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
854       kClientAddr);
855   EXPECT_PRED5(CheckCandidate, candidates_[2],
856       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
857       kClientIPv6Addr);
858   EXPECT_PRED5(CheckCandidate, candidates_[3],
859       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
860       kClientAddr);
861   EXPECT_EQ(4U, candidates_.size());
862 }
863
864 // Test that the httpportallocator correctly maintains its lists of stun and
865 // relay servers, by never allowing an empty list.
866 TEST(HttpPortAllocatorTest, TestHttpPortAllocatorHostLists) {
867   talk_base::FakeNetworkManager network_manager;
868   cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
869   EXPECT_EQ(1U, alloc.relay_hosts().size());
870   EXPECT_EQ(1U, alloc.stun_hosts().size());
871
872   std::vector<std::string> relay_servers;
873   std::vector<talk_base::SocketAddress> stun_servers;
874
875   alloc.SetRelayHosts(relay_servers);
876   alloc.SetStunHosts(stun_servers);
877   EXPECT_EQ(1U, alloc.relay_hosts().size());
878   EXPECT_EQ(1U, alloc.stun_hosts().size());
879
880   relay_servers.push_back("1.unittest.corp.google.com");
881   relay_servers.push_back("2.unittest.corp.google.com");
882   stun_servers.push_back(
883       talk_base::SocketAddress("1.unittest.corp.google.com", 0));
884   stun_servers.push_back(
885       talk_base::SocketAddress("2.unittest.corp.google.com", 0));
886   alloc.SetRelayHosts(relay_servers);
887   alloc.SetStunHosts(stun_servers);
888   EXPECT_EQ(2U, alloc.relay_hosts().size());
889   EXPECT_EQ(2U, alloc.stun_hosts().size());
890 }
891
892 // Test that the HttpPortAllocator uses correct URL to create sessions.
893 TEST(HttpPortAllocatorTest, TestSessionRequestUrl) {
894   talk_base::FakeNetworkManager network_manager;
895   cricket::HttpPortAllocator alloc(&network_manager, "unit test agent");
896
897   // Disable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
898   alloc.set_flags(alloc.flags() & ~cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
899   talk_base::scoped_ptr<cricket::HttpPortAllocatorSessionBase> session(
900       static_cast<cricket::HttpPortAllocatorSession*>(
901           alloc.CreateSessionInternal(
902               "test content", 0, kIceUfrag0, kIcePwd0)));
903   std::string url = session->GetSessionRequestUrl();
904   LOG(LS_INFO) << "url: " << url;
905   EXPECT_EQ(std::string(cricket::HttpPortAllocator::kCreateSessionURL), url);
906
907   // Enable PORTALLOCATOR_ENABLE_SHARED_UFRAG.
908   alloc.set_flags(alloc.flags() | cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG);
909   session.reset(static_cast<cricket::HttpPortAllocatorSession*>(
910       alloc.CreateSessionInternal("test content", 0, kIceUfrag0, kIcePwd0)));
911   url = session->GetSessionRequestUrl();
912   LOG(LS_INFO) << "url: " << url;
913   std::vector<std::string> parts;
914   talk_base::split(url, '?', &parts);
915   ASSERT_EQ(2U, parts.size());
916
917   std::vector<std::string> args_parts;
918   talk_base::split(parts[1], '&', &args_parts);
919
920   std::map<std::string, std::string> args;
921   for (std::vector<std::string>::iterator it = args_parts.begin();
922        it != args_parts.end(); ++it) {
923     std::vector<std::string> parts;
924     talk_base::split(*it, '=', &parts);
925     ASSERT_EQ(2U, parts.size());
926     args[talk_base::s_url_decode(parts[0])] = talk_base::s_url_decode(parts[1]);
927   }
928
929   EXPECT_EQ(kIceUfrag0, args["username"]);
930   EXPECT_EQ(kIcePwd0, args["password"]);
931 }