• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2009 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/p2p/base/basicpacketsocketfactory.h"
12 #include "webrtc/p2p/base/constants.h"
13 #include "webrtc/p2p/base/p2ptransportchannel.h"
14 #include "webrtc/p2p/base/testrelayserver.h"
15 #include "webrtc/p2p/base/teststunserver.h"
16 #include "webrtc/p2p/base/testturnserver.h"
17 #include "webrtc/p2p/client/basicportallocator.h"
18 #include "webrtc/p2p/client/httpportallocator.h"
19 #include "webrtc/base/fakenetwork.h"
20 #include "webrtc/base/firewallsocketserver.h"
21 #include "webrtc/base/gunit.h"
22 #include "webrtc/base/helpers.h"
23 #include "webrtc/base/ipaddress.h"
24 #include "webrtc/base/logging.h"
25 #include "webrtc/base/natserver.h"
26 #include "webrtc/base/natsocketfactory.h"
27 #include "webrtc/base/network.h"
28 #include "webrtc/base/physicalsocketserver.h"
29 #include "webrtc/base/socketaddress.h"
30 #include "webrtc/base/ssladapter.h"
31 #include "webrtc/base/thread.h"
32 #include "webrtc/base/virtualsocketserver.h"
33 
34 using cricket::ServerAddresses;
35 using rtc::IPAddress;
36 using rtc::SocketAddress;
37 using rtc::Thread;
38 
39 static const SocketAddress kClientAddr("11.11.11.11", 0);
40 static const SocketAddress kLoopbackAddr("127.0.0.1", 0);
41 static const SocketAddress kPrivateAddr("192.168.1.11", 0);
42 static const SocketAddress kPrivateAddr2("192.168.1.12", 0);
43 static const SocketAddress kClientIPv6Addr(
44     "2401:fa00:4:1000:be30:5bff:fee5:c3", 0);
45 static const SocketAddress kClientAddr2("22.22.22.22", 0);
46 static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
47 static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT);
48 static const SocketAddress kRemoteClientAddr("22.22.22.22", 0);
49 static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT);
50 static const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
51 static const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
52 static const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
53 static const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
54 static const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
55 static const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
56 static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478);
57 static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478);
58 static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0);
59 
60 // Minimum and maximum port for port range tests.
61 static const int kMinPort = 10000;
62 static const int kMaxPort = 10099;
63 
64 // Based on ICE_UFRAG_LENGTH
65 static const char kIceUfrag0[] = "TESTICEUFRAG0000";
66 // Based on ICE_PWD_LENGTH
67 static const char kIcePwd0[] = "TESTICEPWD00000000000000";
68 
69 static const char kContentName[] = "test content";
70 
71 static const int kDefaultAllocationTimeout = 1000;
72 static const char kTurnUsername[] = "test";
73 static const char kTurnPassword[] = "test";
74 
75 namespace cricket {
76 
77 // Helper for dumping candidates
operator <<(std::ostream & os,const cricket::Candidate & c)78 std::ostream& operator<<(std::ostream& os, const cricket::Candidate& c) {
79   os << c.ToString();
80   return os;
81 }
82 
83 }  // namespace cricket
84 
85 class PortAllocatorTest : public testing::Test, public sigslot::has_slots<> {
86  public:
PortAllocatorTest()87   PortAllocatorTest()
88       : pss_(new rtc::PhysicalSocketServer),
89         vss_(new rtc::VirtualSocketServer(pss_.get())),
90         fss_(new rtc::FirewallSocketServer(vss_.get())),
91         ss_scope_(fss_.get()),
92         nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr),
93         nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)),
94         stun_server_(cricket::TestStunServer::Create(Thread::Current(),
95                                                      kStunAddr)),
96         relay_server_(Thread::Current(), kRelayUdpIntAddr, kRelayUdpExtAddr,
97                       kRelayTcpIntAddr, kRelayTcpExtAddr,
98                       kRelaySslTcpIntAddr, kRelaySslTcpExtAddr),
99         turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr),
100         candidate_allocation_done_(false) {
101     cricket::ServerAddresses stun_servers;
102     stun_servers.insert(kStunAddr);
103     // Passing the addresses of GTURN servers will enable GTURN in
104     // Basicportallocator.
105     allocator_.reset(new cricket::BasicPortAllocator(
106         &network_manager_,
107         stun_servers,
108         kRelayUdpIntAddr, kRelayTcpIntAddr, kRelaySslTcpIntAddr));
109     allocator_->set_step_delay(cricket::kMinimumStepDelay);
110   }
111 
AddInterface(const SocketAddress & addr)112   void AddInterface(const SocketAddress& addr) {
113     network_manager_.AddInterface(addr);
114   }
AddInterface(const SocketAddress & addr,const std::string & if_name)115   void AddInterface(const SocketAddress& addr, const std::string& if_name) {
116     network_manager_.AddInterface(addr, if_name);
117   }
AddInterface(const SocketAddress & addr,const std::string & if_name,rtc::AdapterType type)118   void AddInterface(const SocketAddress& addr,
119                     const std::string& if_name,
120                     rtc::AdapterType type) {
121     network_manager_.AddInterface(addr, if_name, type);
122   }
123   // The default route is the public address that STUN server will observe when
124   // the endpoint is sitting on the public internet and the local port is bound
125   // to the "any" address. This may be different from the default local address
126   // which the endpoint observes. This can occur if the route to the public
127   // endpoint like 8.8.8.8 (specified as the default local address) is
128   // different from the route to the STUN server (the default route).
AddInterfaceAsDefaultRoute(const SocketAddress & addr)129   void AddInterfaceAsDefaultRoute(const SocketAddress& addr) {
130     AddInterface(addr);
131     // When a binding comes from the any address, the |addr| will be used as the
132     // srflx address.
133     vss_->SetDefaultRoute(addr.ipaddr());
134   }
RemoveInterface(const SocketAddress & addr)135   void RemoveInterface(const SocketAddress& addr) {
136     network_manager_.RemoveInterface(addr);
137   }
SetPortRange(int min_port,int max_port)138   bool SetPortRange(int min_port, int max_port) {
139     return allocator_->SetPortRange(min_port, max_port);
140   }
141   // Endpoint is on the public network. No STUN or TURN.
ResetWithNoServersOrNat()142   void ResetWithNoServersOrNat() {
143     allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
144     allocator_->set_step_delay(cricket::kMinimumStepDelay);
145   }
146   // Endpoint is behind a NAT, with STUN specified.
ResetWithStunServerAndNat(const rtc::SocketAddress & stun_server)147   void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) {
148     ResetWithStunServer(stun_server, true);
149   }
150   // Endpoint is on the public network, with STUN specified.
ResetWithStunServerNoNat(const rtc::SocketAddress & stun_server)151   void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) {
152     ResetWithStunServer(stun_server, false);
153   }
154   // Endpoint is on the public network, with TURN specified.
ResetWithTurnServersNoNat(const rtc::SocketAddress & udp_turn,const rtc::SocketAddress & tcp_turn)155   void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn,
156                                  const rtc::SocketAddress& tcp_turn) {
157     ResetWithNoServersOrNat();
158     AddTurnServers(udp_turn, tcp_turn);
159   }
160 
AddTurnServers(const rtc::SocketAddress & udp_turn,const rtc::SocketAddress & tcp_turn)161   void AddTurnServers(const rtc::SocketAddress& udp_turn,
162                       const rtc::SocketAddress& tcp_turn) {
163     cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
164     cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
165     turn_server.credentials = credentials;
166 
167     if (!udp_turn.IsNil()) {
168       turn_server.ports.push_back(
169           cricket::ProtocolAddress(kTurnUdpIntAddr, cricket::PROTO_UDP, false));
170     }
171     if (!tcp_turn.IsNil()) {
172       turn_server.ports.push_back(
173           cricket::ProtocolAddress(kTurnTcpIntAddr, cricket::PROTO_TCP, false));
174     }
175     allocator_->AddTurnServer(turn_server);
176   }
177 
CreateSession(int component)178   bool CreateSession(int component) {
179     session_.reset(CreateSession("session", component));
180     if (!session_)
181       return false;
182     return true;
183   }
184 
CreateSession(int component,const std::string & content_name)185   bool CreateSession(int component, const std::string& content_name) {
186     session_.reset(CreateSession("session", content_name, component));
187     if (!session_)
188       return false;
189     return true;
190   }
191 
CreateSession(const std::string & sid,int component)192   cricket::PortAllocatorSession* CreateSession(
193       const std::string& sid, int component) {
194     return CreateSession(sid, kContentName, component);
195   }
196 
CreateSession(const std::string & sid,const std::string & content_name,int component)197   cricket::PortAllocatorSession* CreateSession(
198       const std::string& sid, const std::string& content_name, int component) {
199     return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0);
200   }
201 
CreateSession(const std::string & sid,const std::string & content_name,int component,const std::string & ice_ufrag,const std::string & ice_pwd)202   cricket::PortAllocatorSession* CreateSession(
203       const std::string& sid, const std::string& content_name, int component,
204       const std::string& ice_ufrag, const std::string& ice_pwd) {
205     cricket::PortAllocatorSession* session =
206         allocator_->CreateSession(
207             sid, content_name, component, ice_ufrag, ice_pwd);
208     session->SignalPortReady.connect(this,
209             &PortAllocatorTest::OnPortReady);
210     session->SignalCandidatesReady.connect(this,
211         &PortAllocatorTest::OnCandidatesReady);
212     session->SignalCandidatesAllocationDone.connect(this,
213         &PortAllocatorTest::OnCandidatesAllocationDone);
214     return session;
215   }
216 
CheckCandidate(const cricket::Candidate & c,int component,const std::string & type,const std::string & proto,const SocketAddress & addr)217   static bool CheckCandidate(const cricket::Candidate& c,
218                              int component, const std::string& type,
219                              const std::string& proto,
220                              const SocketAddress& addr) {
221     return (c.component() == component && c.type() == type &&
222         c.protocol() == proto && c.address().ipaddr() == addr.ipaddr() &&
223         ((addr.port() == 0 && (c.address().port() != 0)) ||
224         (c.address().port() == addr.port())));
225   }
CheckPort(const rtc::SocketAddress & addr,int min_port,int max_port)226   static bool CheckPort(const rtc::SocketAddress& addr,
227                         int min_port, int max_port) {
228     return (addr.port() >= min_port && addr.port() <= max_port);
229   }
230 
OnCandidatesAllocationDone(cricket::PortAllocatorSession * session)231   void OnCandidatesAllocationDone(cricket::PortAllocatorSession* session) {
232     // We should only get this callback once, except in the mux test where
233     // we have multiple port allocation sessions.
234     if (session == session_.get()) {
235       ASSERT_FALSE(candidate_allocation_done_);
236       candidate_allocation_done_ = true;
237     }
238   }
239 
240   // Check if all ports allocated have send-buffer size |expected|. If
241   // |expected| == -1, check if GetOptions returns SOCKET_ERROR.
CheckSendBufferSizesOfAllPorts(int expected)242   void CheckSendBufferSizesOfAllPorts(int expected) {
243     std::vector<cricket::PortInterface*>::iterator it;
244     for (it = ports_.begin(); it < ports_.end(); ++it) {
245       int send_buffer_size;
246       if (expected == -1) {
247         EXPECT_EQ(SOCKET_ERROR,
248                   (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
249                                    &send_buffer_size));
250       } else {
251         EXPECT_EQ(0, (*it)->GetOption(rtc::Socket::OPT_SNDBUF,
252                                       &send_buffer_size));
253         ASSERT_EQ(expected, send_buffer_size);
254       }
255     }
256   }
257 
258   // This function starts the port/address gathering and check the existence of
259   // candidates as specified. When |expect_stun_candidate| is true,
260   // |stun_candidate_addr| carries the expected reflective address, which is
261   // also the related address for TURN candidate if it is expected. Otherwise,
262   // it should be ignore.
CheckDisableAdapterEnumeration(uint32_t total_ports,const rtc::IPAddress & host_candidate_addr,const rtc::IPAddress & stun_candidate_addr,const rtc::IPAddress & relay_candidate_udp_transport_addr,const rtc::IPAddress & relay_candidate_tcp_transport_addr)263   void CheckDisableAdapterEnumeration(
264       uint32_t total_ports,
265       const rtc::IPAddress& host_candidate_addr,
266       const rtc::IPAddress& stun_candidate_addr,
267       const rtc::IPAddress& relay_candidate_udp_transport_addr,
268       const rtc::IPAddress& relay_candidate_tcp_transport_addr) {
269     network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
270                                                  rtc::IPAddress());
271     if (!session_) {
272       EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
273     }
274     session_->set_flags(session_->flags() |
275                         cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION |
276                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
277     allocator().set_allow_tcp_listen(false);
278     session_->StartGettingPorts();
279     EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
280 
281     uint32_t total_candidates = 0;
282     if (!host_candidate_addr.IsNil()) {
283       EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
284                    cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
285                    rtc::SocketAddress(kPrivateAddr.ipaddr(), 0));
286       ++total_candidates;
287     }
288     if (!stun_candidate_addr.IsNil()) {
289       EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
290                    cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
291                    rtc::SocketAddress(stun_candidate_addr, 0));
292       rtc::IPAddress related_address = host_candidate_addr;
293       if (host_candidate_addr.IsNil()) {
294         related_address =
295             rtc::GetAnyIP(candidates_[total_candidates].address().family());
296       }
297       EXPECT_EQ(related_address,
298                 candidates_[total_candidates].related_address().ipaddr());
299       ++total_candidates;
300     }
301     if (!relay_candidate_udp_transport_addr.IsNil()) {
302       EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
303                    cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
304                    rtc::SocketAddress(relay_candidate_udp_transport_addr, 0));
305       EXPECT_EQ(stun_candidate_addr,
306                 candidates_[total_candidates].related_address().ipaddr());
307       ++total_candidates;
308     }
309     if (!relay_candidate_tcp_transport_addr.IsNil()) {
310       EXPECT_PRED5(CheckCandidate, candidates_[total_candidates],
311                    cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
312                    rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0));
313       EXPECT_EQ(stun_candidate_addr,
314                 candidates_[total_candidates].related_address().ipaddr());
315       ++total_candidates;
316     }
317 
318     EXPECT_EQ(total_candidates, candidates_.size());
319     EXPECT_EQ(total_ports, ports_.size());
320   }
321 
322  protected:
allocator()323   cricket::BasicPortAllocator& allocator() {
324     return *allocator_;
325   }
326 
OnPortReady(cricket::PortAllocatorSession * ses,cricket::PortInterface * port)327   void OnPortReady(cricket::PortAllocatorSession* ses,
328                    cricket::PortInterface* port) {
329     LOG(LS_INFO) << "OnPortReady: " << port->ToString();
330     ports_.push_back(port);
331   }
OnCandidatesReady(cricket::PortAllocatorSession * ses,const std::vector<cricket::Candidate> & candidates)332   void OnCandidatesReady(cricket::PortAllocatorSession* ses,
333                          const std::vector<cricket::Candidate>& candidates) {
334     for (size_t i = 0; i < candidates.size(); ++i) {
335       LOG(LS_INFO) << "OnCandidatesReady: " << candidates[i].ToString();
336       candidates_.push_back(candidates[i]);
337     }
338   }
339 
HasRelayAddress(const cricket::ProtocolAddress & proto_addr)340   bool HasRelayAddress(const cricket::ProtocolAddress& proto_addr) {
341     for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) {
342       cricket::RelayServerConfig server_config = allocator_->turn_servers()[i];
343       cricket::PortList::const_iterator relay_port;
344       for (relay_port = server_config.ports.begin();
345           relay_port != server_config.ports.end(); ++relay_port) {
346         if (proto_addr.address == relay_port->address &&
347             proto_addr.proto == relay_port->proto)
348           return true;
349       }
350     }
351     return false;
352   }
353 
ResetWithStunServer(const rtc::SocketAddress & stun_server,bool with_nat)354   void ResetWithStunServer(const rtc::SocketAddress& stun_server,
355                            bool with_nat) {
356     if (with_nat) {
357       nat_server_.reset(new rtc::NATServer(
358           rtc::NAT_OPEN_CONE, vss_.get(), kNatUdpAddr, kNatTcpAddr, vss_.get(),
359           rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)));
360     } else {
361       nat_socket_factory_.reset(new rtc::BasicPacketSocketFactory());
362     }
363 
364     ServerAddresses stun_servers;
365     if (!stun_server.IsNil()) {
366       stun_servers.insert(stun_server);
367     }
368     allocator_.reset(new cricket::BasicPortAllocator(
369         &network_manager_, nat_socket_factory_.get(), stun_servers));
370     allocator().set_step_delay(cricket::kMinimumStepDelay);
371   }
372 
373   rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
374   rtc::scoped_ptr<rtc::VirtualSocketServer> vss_;
375   rtc::scoped_ptr<rtc::FirewallSocketServer> fss_;
376   rtc::SocketServerScope ss_scope_;
377   rtc::scoped_ptr<rtc::NATServer> nat_server_;
378   rtc::NATSocketFactory nat_factory_;
379   rtc::scoped_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_;
380   rtc::scoped_ptr<cricket::TestStunServer> stun_server_;
381   cricket::TestRelayServer relay_server_;
382   cricket::TestTurnServer turn_server_;
383   rtc::FakeNetworkManager network_manager_;
384   rtc::scoped_ptr<cricket::BasicPortAllocator> allocator_;
385   rtc::scoped_ptr<cricket::PortAllocatorSession> session_;
386   std::vector<cricket::PortInterface*> ports_;
387   std::vector<cricket::Candidate> candidates_;
388   bool candidate_allocation_done_;
389 };
390 
391 // Tests that we can init the port allocator and create a session.
TEST_F(PortAllocatorTest,TestBasic)392 TEST_F(PortAllocatorTest, TestBasic) {
393   EXPECT_EQ(&network_manager_, allocator().network_manager());
394   EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin());
395   ASSERT_EQ(1u, allocator().turn_servers().size());
396   EXPECT_EQ(cricket::RELAY_GTURN, allocator().turn_servers()[0].type);
397   // Empty relay credentials are used for GTURN.
398   EXPECT_TRUE(allocator().turn_servers()[0].credentials.username.empty());
399   EXPECT_TRUE(allocator().turn_servers()[0].credentials.password.empty());
400   EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
401       kRelayUdpIntAddr, cricket::PROTO_UDP)));
402   EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
403       kRelayTcpIntAddr, cricket::PROTO_TCP)));
404   EXPECT_TRUE(HasRelayAddress(cricket::ProtocolAddress(
405       kRelaySslTcpIntAddr, cricket::PROTO_SSLTCP)));
406   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
407 }
408 
409 // Tests that our network filtering works properly.
TEST_F(PortAllocatorTest,TestIgnoreOnlyLoopbackNetworkByDefault)410 TEST_F(PortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) {
411   AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
412                rtc::ADAPTER_TYPE_ETHERNET);
413   AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
414                rtc::ADAPTER_TYPE_WIFI);
415   AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
416                rtc::ADAPTER_TYPE_CELLULAR);
417   AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0",
418                rtc::ADAPTER_TYPE_VPN);
419   AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo",
420                rtc::ADAPTER_TYPE_LOOPBACK);
421   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
422   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
423                       cricket::PORTALLOCATOR_DISABLE_RELAY |
424                       cricket::PORTALLOCATOR_DISABLE_TCP);
425   session_->StartGettingPorts();
426   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
427   EXPECT_EQ(4U, candidates_.size());
428   for (cricket::Candidate candidate : candidates_) {
429     EXPECT_LT(candidate.address().ip(), 0x12345604U);
430   }
431 }
432 
TEST_F(PortAllocatorTest,TestIgnoreNetworksAccordingToIgnoreMask)433 TEST_F(PortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) {
434   AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0",
435                rtc::ADAPTER_TYPE_ETHERNET);
436   AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0",
437                rtc::ADAPTER_TYPE_WIFI);
438   AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0",
439                rtc::ADAPTER_TYPE_CELLULAR);
440   allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET |
441                                    rtc::ADAPTER_TYPE_LOOPBACK |
442                                    rtc::ADAPTER_TYPE_WIFI);
443   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
444   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
445                       cricket::PORTALLOCATOR_DISABLE_RELAY |
446                       cricket::PORTALLOCATOR_DISABLE_TCP);
447   session_->StartGettingPorts();
448   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
449   EXPECT_EQ(1U, candidates_.size());
450   EXPECT_EQ(0x12345602U, candidates_[0].address().ip());
451 }
452 
453 // Tests that we allocator session not trying to allocate ports for every 250ms.
TEST_F(PortAllocatorTest,TestNoNetworkInterface)454 TEST_F(PortAllocatorTest, TestNoNetworkInterface) {
455   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
456   session_->StartGettingPorts();
457   // Waiting for one second to make sure BasicPortAllocatorSession has not
458   // called OnAllocate multiple times. In old behavior it's called every 250ms.
459   // When there are no network interfaces, each execution of OnAllocate will
460   // result in SignalCandidatesAllocationDone signal.
461   rtc::Thread::Current()->ProcessMessages(1000);
462   EXPECT_TRUE(candidate_allocation_done_);
463   EXPECT_EQ(0U, candidates_.size());
464 }
465 
466 // Test that we could use loopback interface as host candidate.
TEST_F(PortAllocatorTest,TestLoopbackNetworkInterface)467 TEST_F(PortAllocatorTest, TestLoopbackNetworkInterface) {
468   AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK);
469   allocator_->SetNetworkIgnoreMask(0);
470   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
471   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_STUN |
472                       cricket::PORTALLOCATOR_DISABLE_RELAY |
473                       cricket::PORTALLOCATOR_DISABLE_TCP);
474   session_->StartGettingPorts();
475   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
476   EXPECT_EQ(1U, candidates_.size());
477 }
478 
479 // Tests that we can get all the desired addresses successfully.
TEST_F(PortAllocatorTest,TestGetAllPortsWithMinimumStepDelay)480 TEST_F(PortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) {
481   AddInterface(kClientAddr);
482   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
483   session_->StartGettingPorts();
484   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
485   EXPECT_EQ(4U, ports_.size());
486   EXPECT_PRED5(CheckCandidate, candidates_[0],
487       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
488   EXPECT_PRED5(CheckCandidate, candidates_[1],
489       cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
490   EXPECT_PRED5(CheckCandidate, candidates_[2],
491       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
492   EXPECT_PRED5(CheckCandidate, candidates_[3],
493       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
494   EXPECT_PRED5(CheckCandidate, candidates_[4],
495       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
496   EXPECT_PRED5(CheckCandidate, candidates_[5],
497       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
498   EXPECT_PRED5(CheckCandidate, candidates_[6],
499       cricket::ICE_CANDIDATE_COMPONENT_RTP,
500       "relay", "ssltcp", kRelaySslTcpIntAddr);
501   EXPECT_TRUE(candidate_allocation_done_);
502 }
503 
504 // Test that when the same network interface is brought down and up, the
505 // port allocator session will restart a new allocation sequence if
506 // it is not stopped.
TEST_F(PortAllocatorTest,TestSameNetworkDownAndUpWhenSessionNotStopped)507 TEST_F(PortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) {
508   std::string if_name("test_net0");
509   AddInterface(kClientAddr, if_name);
510   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
511   session_->StartGettingPorts();
512   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
513   EXPECT_EQ(4U, ports_.size());
514   EXPECT_TRUE(candidate_allocation_done_);
515   candidate_allocation_done_ = false;
516   candidates_.clear();
517   ports_.clear();
518 
519   RemoveInterface(kClientAddr);
520   ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
521   EXPECT_EQ(0U, ports_.size());
522   EXPECT_FALSE(candidate_allocation_done_);
523 
524   // When the same interfaces are added again, new candidates/ports should be
525   // generated.
526   AddInterface(kClientAddr, if_name);
527   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
528   EXPECT_EQ(4U, ports_.size());
529   EXPECT_TRUE(candidate_allocation_done_);
530 }
531 
532 // Test that when the same network interface is brought down and up, the
533 // port allocator session will not restart a new allocation sequence if
534 // it is stopped.
TEST_F(PortAllocatorTest,TestSameNetworkDownAndUpWhenSessionStopped)535 TEST_F(PortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) {
536   std::string if_name("test_net0");
537   AddInterface(kClientAddr, if_name);
538   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
539   session_->StartGettingPorts();
540   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
541   EXPECT_EQ(4U, ports_.size());
542   EXPECT_TRUE(candidate_allocation_done_);
543   session_->StopGettingPorts();
544   candidates_.clear();
545   ports_.clear();
546 
547   RemoveInterface(kClientAddr);
548   ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
549   EXPECT_EQ(0U, ports_.size());
550 
551   // When the same interfaces are added again, new candidates/ports should not
552   // be generated because the session has stopped.
553   AddInterface(kClientAddr, if_name);
554   ASSERT_EQ_WAIT(0U, candidates_.size(), kDefaultAllocationTimeout);
555   EXPECT_EQ(0U, ports_.size());
556   EXPECT_TRUE(candidate_allocation_done_);
557 }
558 
559 // Verify candidates with default step delay of 1sec.
TEST_F(PortAllocatorTest,TestGetAllPortsWithOneSecondStepDelay)560 TEST_F(PortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) {
561   AddInterface(kClientAddr);
562   allocator_->set_step_delay(cricket::kDefaultStepDelay);
563   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
564   session_->StartGettingPorts();
565   ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
566   EXPECT_EQ(2U, ports_.size());
567   ASSERT_EQ_WAIT(4U, candidates_.size(), 2000);
568   EXPECT_EQ(3U, ports_.size());
569   EXPECT_PRED5(CheckCandidate, candidates_[2],
570       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
571   EXPECT_PRED5(CheckCandidate, candidates_[3],
572       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
573   ASSERT_EQ_WAIT(6U, candidates_.size(), 1500);
574   EXPECT_PRED5(CheckCandidate, candidates_[4],
575       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
576   EXPECT_PRED5(CheckCandidate, candidates_[5],
577       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
578   EXPECT_EQ(4U, ports_.size());
579   ASSERT_EQ_WAIT(7U, candidates_.size(), 2000);
580   EXPECT_PRED5(CheckCandidate, candidates_[6],
581       cricket::ICE_CANDIDATE_COMPONENT_RTP,
582                "relay", "ssltcp", kRelaySslTcpIntAddr);
583   EXPECT_EQ(4U, ports_.size());
584   EXPECT_TRUE(candidate_allocation_done_);
585   // If we Stop gathering now, we shouldn't get a second "done" callback.
586   session_->StopGettingPorts();
587 }
588 
TEST_F(PortAllocatorTest,TestSetupVideoRtpPortsWithNormalSendBuffers)589 TEST_F(PortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) {
590   AddInterface(kClientAddr);
591   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP,
592                             cricket::CN_VIDEO));
593   session_->StartGettingPorts();
594   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
595   EXPECT_TRUE(candidate_allocation_done_);
596   // If we Stop gathering now, we shouldn't get a second "done" callback.
597   session_->StopGettingPorts();
598 
599   // All ports should have unset send-buffer sizes.
600   CheckSendBufferSizesOfAllPorts(-1);
601 }
602 
603 // Tests that we can get callback after StopGetAllPorts.
TEST_F(PortAllocatorTest,TestStopGetAllPorts)604 TEST_F(PortAllocatorTest, TestStopGetAllPorts) {
605   AddInterface(kClientAddr);
606   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
607   session_->StartGettingPorts();
608   ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
609   EXPECT_EQ(2U, ports_.size());
610   session_->StopGettingPorts();
611   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
612 }
613 
614 // Test that we restrict client ports appropriately when a port range is set.
615 // We check the candidates for udp/stun/tcp ports, and the from address
616 // for relay ports.
TEST_F(PortAllocatorTest,TestGetAllPortsPortRange)617 TEST_F(PortAllocatorTest, TestGetAllPortsPortRange) {
618   AddInterface(kClientAddr);
619   // Check that an invalid port range fails.
620   EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort));
621   // Check that a null port range succeeds.
622   EXPECT_TRUE(SetPortRange(0, 0));
623   // Check that a valid port range succeeds.
624   EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort));
625   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
626   session_->StartGettingPorts();
627   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
628   EXPECT_EQ(4U, ports_.size());
629   // Check the port number for the UDP port object.
630   EXPECT_PRED3(CheckPort, candidates_[0].address(), kMinPort, kMaxPort);
631   // Check the port number for the STUN port object.
632   EXPECT_PRED3(CheckPort, candidates_[1].address(), kMinPort, kMaxPort);
633   // Check the port number used to connect to the relay server.
634   EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(),
635                kMinPort, kMaxPort);
636   // Check the port number for the TCP port object.
637   EXPECT_PRED3(CheckPort, candidates_[5].address(), kMinPort, kMaxPort);
638   EXPECT_TRUE(candidate_allocation_done_);
639 }
640 
641 // Test that we don't crash or malfunction if we have no network adapters.
TEST_F(PortAllocatorTest,TestGetAllPortsNoAdapters)642 TEST_F(PortAllocatorTest, TestGetAllPortsNoAdapters) {
643   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
644   session_->StartGettingPorts();
645   rtc::Thread::Current()->ProcessMessages(100);
646   // Without network adapter, we should not get any candidate.
647   EXPECT_EQ(0U, candidates_.size());
648   EXPECT_TRUE(candidate_allocation_done_);
649 }
650 
651 // Test that when enumeration is disabled, we should not have any ports when
652 // candidate_filter() is set to CF_RELAY and no relay is specified.
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationWithoutNatRelayTransportOnly)653 TEST_F(PortAllocatorTest,
654        TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) {
655   ResetWithStunServerNoNat(kStunAddr);
656   allocator().set_candidate_filter(cricket::CF_RELAY);
657   // Expect to see no ports and no candidates.
658   CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(),
659                                  rtc::IPAddress(), rtc::IPAddress());
660 }
661 
662 // Test that even with multiple interfaces, the result should still be a single
663 // default private, one STUN and one TURN candidate since we bind to any address
664 // (i.e. all 0s).
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationBehindNatMultipleInterfaces)665 TEST_F(PortAllocatorTest,
666        TestDisableAdapterEnumerationBehindNatMultipleInterfaces) {
667   AddInterface(kPrivateAddr);
668   AddInterface(kPrivateAddr2);
669   ResetWithStunServerAndNat(kStunAddr);
670   AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
671 
672   // Enable IPv6 here. Since the network_manager doesn't have IPv6 default
673   // address set and we have no IPv6 STUN server, there should be no IPv6
674   // candidates.
675   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
676   session_->set_flags(cricket::PORTALLOCATOR_ENABLE_IPV6);
677 
678   // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports
679   // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and
680   // TURN/UDP candidates.
681   CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(),
682                                  kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
683                                  rtc::IPAddress());
684 }
685 
686 // Test that we should get a default private, STUN, TURN/UDP and TURN/TCP
687 // candidates when both TURN/UDP and TURN/TCP servers are specified.
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationBehindNatWithTcp)688 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) {
689   turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
690   AddInterface(kPrivateAddr);
691   ResetWithStunServerAndNat(kStunAddr);
692   AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
693   // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default
694   // private, STUN, TURN/UDP, and TURN/TCP candidates.
695   CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(),
696                                  kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(),
697                                  kTurnUdpExtAddr.ipaddr());
698 }
699 
700 // Test that when adapter enumeration is disabled, for endpoints without
701 // STUN/TURN specified, a default private candidate is still generated.
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationWithoutNatOrServers)702 TEST_F(PortAllocatorTest, TestDisableAdapterEnumerationWithoutNatOrServers) {
703   ResetWithNoServersOrNat();
704   // Expect to see 2 ports: STUN and TCP ports, one default private candidate.
705   CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(),
706                                  rtc::IPAddress(), rtc::IPAddress());
707 }
708 
709 // Test that when adapter enumeration is disabled, with
710 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
711 // a NAT, there is no local candidate.
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled)712 TEST_F(PortAllocatorTest,
713        TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) {
714   ResetWithStunServerNoNat(kStunAddr);
715   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
716   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
717   // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
718   // candidate.
719   CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(),
720                                  rtc::IPAddress(), rtc::IPAddress());
721 }
722 
723 // Test that when adapter enumeration is disabled, with
724 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind
725 // a NAT, there is no local candidate. However, this specified default route
726 // (kClientAddr) which was discovered when sending STUN requests, will become
727 // the srflx addresses.
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute)728 TEST_F(
729     PortAllocatorTest,
730     TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabledWithDifferentDefaultRoute) {
731   ResetWithStunServerNoNat(kStunAddr);
732   AddInterfaceAsDefaultRoute(kClientAddr);
733   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
734   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
735   // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN
736   // candidate.
737   CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(),
738                                  rtc::IPAddress(), rtc::IPAddress());
739 }
740 
741 // Test that when adapter enumeration is disabled, with
742 // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a
743 // NAT, there is only one STUN candidate.
TEST_F(PortAllocatorTest,TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled)744 TEST_F(PortAllocatorTest,
745        TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) {
746   ResetWithStunServerAndNat(kStunAddr);
747   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
748   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE);
749   // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate.
750   CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(),
751                                  rtc::IPAddress(), rtc::IPAddress());
752 }
753 
754 // Test that we disable relay over UDP, and only TCP is used when connecting to
755 // the relay server.
TEST_F(PortAllocatorTest,TestDisableUdpTurn)756 TEST_F(PortAllocatorTest, TestDisableUdpTurn) {
757   turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
758   AddInterface(kClientAddr);
759   ResetWithStunServerAndNat(kStunAddr);
760   AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
761   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
762   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP_RELAY |
763                       cricket::PORTALLOCATOR_DISABLE_UDP |
764                       cricket::PORTALLOCATOR_DISABLE_STUN |
765                       cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
766 
767   session_->StartGettingPorts();
768   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
769 
770   // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and
771   // TURN/TCP candidates.
772   EXPECT_EQ(2U, ports_.size());
773   EXPECT_EQ(2U, candidates_.size());
774   EXPECT_PRED5(CheckCandidate, candidates_[0],
775                cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
776                kTurnUdpExtAddr);
777   // The TURN candidate should use TCP to contact the TURN server.
778   EXPECT_EQ(cricket::TCP_PROTOCOL_NAME, candidates_[0].relay_protocol());
779   EXPECT_PRED5(CheckCandidate, candidates_[1],
780                cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
781                kClientAddr);
782 }
783 
784 // Disable for asan, see
785 // https://code.google.com/p/webrtc/issues/detail?id=4743 for details.
786 #if !defined(ADDRESS_SANITIZER)
787 
788 // Test that we can get OnCandidatesAllocationDone callback when all the ports
789 // are disabled.
TEST_F(PortAllocatorTest,TestDisableAllPorts)790 TEST_F(PortAllocatorTest, TestDisableAllPorts) {
791   AddInterface(kClientAddr);
792   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
793   session_->set_flags(cricket::PORTALLOCATOR_DISABLE_UDP |
794                       cricket::PORTALLOCATOR_DISABLE_STUN |
795                       cricket::PORTALLOCATOR_DISABLE_RELAY |
796                       cricket::PORTALLOCATOR_DISABLE_TCP);
797   session_->StartGettingPorts();
798   rtc::Thread::Current()->ProcessMessages(100);
799   EXPECT_EQ(0U, candidates_.size());
800   EXPECT_TRUE(candidate_allocation_done_);
801 }
802 
803 // Test that we don't crash or malfunction if we can't create UDP sockets.
TEST_F(PortAllocatorTest,TestGetAllPortsNoUdpSockets)804 TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSockets) {
805   AddInterface(kClientAddr);
806   fss_->set_udp_sockets_enabled(false);
807   EXPECT_TRUE(CreateSession(1));
808   session_->StartGettingPorts();
809   ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
810   EXPECT_EQ(2U, ports_.size());
811   EXPECT_PRED5(CheckCandidate, candidates_[0],
812       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
813   EXPECT_PRED5(CheckCandidate, candidates_[1],
814       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
815   EXPECT_PRED5(CheckCandidate, candidates_[2],
816       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
817   EXPECT_PRED5(CheckCandidate, candidates_[3],
818       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
819   EXPECT_PRED5(CheckCandidate, candidates_[4],
820       cricket::ICE_CANDIDATE_COMPONENT_RTP,
821       "relay", "ssltcp", kRelaySslTcpIntAddr);
822   EXPECT_TRUE(candidate_allocation_done_);
823 }
824 
825 #endif // if !defined(ADDRESS_SANITIZER)
826 
827 // Test that we don't crash or malfunction if we can't create UDP sockets or
828 // listen on TCP sockets. We still give out a local TCP address, since
829 // apparently this is needed for the remote side to accept our connection.
TEST_F(PortAllocatorTest,TestGetAllPortsNoUdpSocketsNoTcpListen)830 TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) {
831   AddInterface(kClientAddr);
832   fss_->set_udp_sockets_enabled(false);
833   fss_->set_tcp_listen_enabled(false);
834   EXPECT_TRUE(CreateSession(1));
835   session_->StartGettingPorts();
836   ASSERT_EQ_WAIT(5U, candidates_.size(), kDefaultAllocationTimeout);
837   EXPECT_EQ(2U, ports_.size());
838   EXPECT_PRED5(CheckCandidate, candidates_[0],
839       1, "relay", "udp", kRelayUdpIntAddr);
840   EXPECT_PRED5(CheckCandidate, candidates_[1],
841       1, "relay", "udp", kRelayUdpExtAddr);
842   EXPECT_PRED5(CheckCandidate, candidates_[2],
843       1, "relay", "tcp", kRelayTcpIntAddr);
844   EXPECT_PRED5(CheckCandidate, candidates_[3],
845       1, "local", "tcp", kClientAddr);
846   EXPECT_PRED5(CheckCandidate, candidates_[4],
847       1, "relay", "ssltcp", kRelaySslTcpIntAddr);
848   EXPECT_TRUE(candidate_allocation_done_);
849 }
850 
851 // Test that we don't crash or malfunction if we can't create any sockets.
852 // TODO: Find a way to exit early here.
TEST_F(PortAllocatorTest,TestGetAllPortsNoSockets)853 TEST_F(PortAllocatorTest, TestGetAllPortsNoSockets) {
854   AddInterface(kClientAddr);
855   fss_->set_tcp_sockets_enabled(false);
856   fss_->set_udp_sockets_enabled(false);
857   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
858   session_->StartGettingPorts();
859   WAIT(candidates_.size() > 0, 2000);
860   // TODO - Check candidate_allocation_done signal.
861   // In case of Relay, ports creation will succeed but sockets will fail.
862   // There is no error reporting from RelayEntry to handle this failure.
863 }
864 
865 // Testing STUN timeout.
TEST_F(PortAllocatorTest,TestGetAllPortsNoUdpAllowed)866 TEST_F(PortAllocatorTest, TestGetAllPortsNoUdpAllowed) {
867   fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
868   AddInterface(kClientAddr);
869   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
870   session_->StartGettingPorts();
871   EXPECT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
872   EXPECT_EQ(2U, ports_.size());
873   EXPECT_PRED5(CheckCandidate, candidates_[0],
874       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
875   EXPECT_PRED5(CheckCandidate, candidates_[1],
876       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
877   // RelayPort connection timeout is 3sec. TCP connection with RelayServer
878   // will be tried after 3 seconds.
879   EXPECT_EQ_WAIT(6U, candidates_.size(), 4000);
880   EXPECT_EQ(3U, ports_.size());
881   EXPECT_PRED5(CheckCandidate, candidates_[2],
882       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpIntAddr);
883   EXPECT_PRED5(CheckCandidate, candidates_[3],
884       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "tcp", kRelayTcpIntAddr);
885   EXPECT_PRED5(CheckCandidate, candidates_[4],
886       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "ssltcp",
887       kRelaySslTcpIntAddr);
888   EXPECT_PRED5(CheckCandidate, candidates_[5],
889       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp", kRelayUdpExtAddr);
890   // Stun Timeout is 9sec.
891   EXPECT_TRUE_WAIT(candidate_allocation_done_, 9000);
892 }
893 
TEST_F(PortAllocatorTest,TestCandidatePriorityOfMultipleInterfaces)894 TEST_F(PortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) {
895   AddInterface(kClientAddr);
896   AddInterface(kClientAddr2);
897   // Allocating only host UDP ports. This is done purely for testing
898   // convenience.
899   allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
900                         cricket::PORTALLOCATOR_DISABLE_STUN |
901                         cricket::PORTALLOCATOR_DISABLE_RELAY);
902   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
903   session_->StartGettingPorts();
904   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
905   ASSERT_EQ(2U, candidates_.size());
906   EXPECT_EQ(2U, ports_.size());
907   // Candidates priorities should be different.
908   EXPECT_NE(candidates_[0].priority(), candidates_[1].priority());
909 }
910 
911 // Test to verify ICE restart process.
TEST_F(PortAllocatorTest,TestGetAllPortsRestarts)912 TEST_F(PortAllocatorTest, TestGetAllPortsRestarts) {
913   AddInterface(kClientAddr);
914   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
915   session_->StartGettingPorts();
916   EXPECT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
917   EXPECT_EQ(4U, ports_.size());
918   EXPECT_TRUE(candidate_allocation_done_);
919   // TODO - Extend this to verify ICE restart.
920 }
921 
922 // Test ICE candidate filter mechanism with options Relay/Host/Reflexive.
923 // This test also verifies that when the allocator is only allowed to use
924 // relay (i.e. IceTransportsType is relay), the raddr is an empty
925 // address with the correct family. This is to prevent any local
926 // reflective address leakage in the sdp line.
TEST_F(PortAllocatorTest,TestCandidateFilterWithRelayOnly)927 TEST_F(PortAllocatorTest, TestCandidateFilterWithRelayOnly) {
928   AddInterface(kClientAddr);
929   // GTURN is not configured here.
930   ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress());
931   allocator().set_candidate_filter(cricket::CF_RELAY);
932   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
933   session_->StartGettingPorts();
934   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
935   EXPECT_PRED5(CheckCandidate,
936                candidates_[0],
937                cricket::ICE_CANDIDATE_COMPONENT_RTP,
938                "relay",
939                "udp",
940                rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
941 
942   EXPECT_EQ(1U, candidates_.size());
943   EXPECT_EQ(1U, ports_.size());  // Only Relay port will be in ready state.
944   for (size_t i = 0; i < candidates_.size(); ++i) {
945     EXPECT_EQ(std::string(cricket::RELAY_PORT_TYPE), candidates_[i].type());
946     EXPECT_EQ(
947         candidates_[0].related_address(),
948         rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
949   }
950 }
951 
TEST_F(PortAllocatorTest,TestCandidateFilterWithHostOnly)952 TEST_F(PortAllocatorTest, TestCandidateFilterWithHostOnly) {
953   AddInterface(kClientAddr);
954   allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
955   allocator().set_candidate_filter(cricket::CF_HOST);
956   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
957   session_->StartGettingPorts();
958   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
959   EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only.
960   EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only.
961   for (size_t i = 0; i < candidates_.size(); ++i) {
962     EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
963   }
964 }
965 
966 // Host is behind the NAT.
TEST_F(PortAllocatorTest,TestCandidateFilterWithReflexiveOnly)967 TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnly) {
968   AddInterface(kPrivateAddr);
969   ResetWithStunServerAndNat(kStunAddr);
970 
971   allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
972   allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
973   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
974   session_->StartGettingPorts();
975   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
976   // Host is behind NAT, no private address will be exposed. Hence only UDP
977   // port with STUN candidate will be sent outside.
978   EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate.
979   EXPECT_EQ(1U, ports_.size());  // Only UDP port will be in ready state.
980   for (size_t i = 0; i < candidates_.size(); ++i) {
981     EXPECT_EQ(std::string(cricket::STUN_PORT_TYPE), candidates_[i].type());
982     EXPECT_EQ(
983         candidates_[0].related_address(),
984         rtc::EmptySocketAddressWithFamily(candidates_[0].address().family()));
985   }
986 }
987 
988 // Host is not behind the NAT.
TEST_F(PortAllocatorTest,TestCandidateFilterWithReflexiveOnlyAndNoNAT)989 TEST_F(PortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) {
990   AddInterface(kClientAddr);
991   allocator().set_flags(cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
992   allocator().set_candidate_filter(cricket::CF_REFLEXIVE);
993   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
994   session_->StartGettingPorts();
995   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
996   // Host has a public address, both UDP and TCP candidates will be exposed.
997   EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate.
998   EXPECT_EQ(2U, ports_.size());  //  UDP and TCP ports will be in ready state.
999   for (size_t i = 0; i < candidates_.size(); ++i) {
1000     EXPECT_EQ(std::string(cricket::LOCAL_PORT_TYPE), candidates_[i].type());
1001   }
1002 }
1003 
1004 // Test that we get the same ufrag and pwd for all candidates.
TEST_F(PortAllocatorTest,TestEnableSharedUfrag)1005 TEST_F(PortAllocatorTest, TestEnableSharedUfrag) {
1006   AddInterface(kClientAddr);
1007   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1008   session_->StartGettingPorts();
1009   ASSERT_EQ_WAIT(7U, candidates_.size(), kDefaultAllocationTimeout);
1010   EXPECT_PRED5(CheckCandidate, candidates_[0],
1011       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1012   EXPECT_PRED5(CheckCandidate, candidates_[1],
1013       cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp", kClientAddr);
1014   EXPECT_PRED5(CheckCandidate, candidates_[5],
1015       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp", kClientAddr);
1016   EXPECT_EQ(4U, ports_.size());
1017   EXPECT_EQ(kIceUfrag0, candidates_[0].username());
1018   EXPECT_EQ(kIceUfrag0, candidates_[1].username());
1019   EXPECT_EQ(kIceUfrag0, candidates_[2].username());
1020   EXPECT_EQ(kIcePwd0, candidates_[0].password());
1021   EXPECT_EQ(kIcePwd0, candidates_[1].password());
1022   EXPECT_TRUE(candidate_allocation_done_);
1023 }
1024 
1025 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1026 // is allocated for udp and stun. Also verify there is only one candidate
1027 // (local) if stun candidate is same as local candidate, which will be the case
1028 // in a public network like the below test.
TEST_F(PortAllocatorTest,TestSharedSocketWithoutNat)1029 TEST_F(PortAllocatorTest, TestSharedSocketWithoutNat) {
1030   AddInterface(kClientAddr);
1031   allocator_->set_flags(allocator().flags() |
1032                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1033   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1034   session_->StartGettingPorts();
1035   ASSERT_EQ_WAIT(6U, candidates_.size(), kDefaultAllocationTimeout);
1036   EXPECT_EQ(3U, ports_.size());
1037   EXPECT_PRED5(CheckCandidate, candidates_[0],
1038       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1039   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1040 }
1041 
1042 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1043 // is allocated for udp and stun. In this test we should expect both stun and
1044 // local candidates as client behind a nat.
TEST_F(PortAllocatorTest,TestSharedSocketWithNat)1045 TEST_F(PortAllocatorTest, TestSharedSocketWithNat) {
1046   AddInterface(kClientAddr);
1047   ResetWithStunServerAndNat(kStunAddr);
1048 
1049   allocator_->set_flags(allocator().flags() |
1050                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1051   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1052   session_->StartGettingPorts();
1053   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1054   ASSERT_EQ(2U, ports_.size());
1055   EXPECT_PRED5(CheckCandidate, candidates_[0],
1056       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1057   EXPECT_PRED5(CheckCandidate, candidates_[1],
1058       cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1059       rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1060   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1061   EXPECT_EQ(3U, candidates_.size());
1062 }
1063 
1064 // Test TURN port in shared socket mode with UDP and TCP TURN server addresses.
TEST_F(PortAllocatorTest,TestSharedSocketWithoutNatUsingTurn)1065 TEST_F(PortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) {
1066   turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1067   AddInterface(kClientAddr);
1068   allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
1069 
1070   AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr);
1071 
1072   allocator_->set_step_delay(cricket::kMinimumStepDelay);
1073   allocator_->set_flags(allocator().flags() |
1074                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1075                         cricket::PORTALLOCATOR_DISABLE_TCP);
1076 
1077   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1078   session_->StartGettingPorts();
1079 
1080   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1081   ASSERT_EQ(3U, ports_.size());
1082   EXPECT_PRED5(CheckCandidate, candidates_[0],
1083       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1084   EXPECT_PRED5(CheckCandidate, candidates_[1],
1085       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1086       rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1087   EXPECT_PRED5(CheckCandidate, candidates_[2],
1088       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1089       rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1090   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1091   EXPECT_EQ(3U, candidates_.size());
1092 }
1093 
1094 // Testing DNS resolve for the TURN server, this will test AllocationSequence
1095 // handling the unresolved address signal from TurnPort.
TEST_F(PortAllocatorTest,TestSharedSocketWithServerAddressResolve)1096 TEST_F(PortAllocatorTest, TestSharedSocketWithServerAddressResolve) {
1097   turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478),
1098                                  cricket::PROTO_UDP);
1099   AddInterface(kClientAddr);
1100   allocator_.reset(new cricket::BasicPortAllocator(&network_manager_));
1101   cricket::RelayServerConfig turn_server(cricket::RELAY_TURN);
1102   cricket::RelayCredentials credentials(kTurnUsername, kTurnPassword);
1103   turn_server.credentials = credentials;
1104   turn_server.ports.push_back(cricket::ProtocolAddress(
1105       rtc::SocketAddress("localhost", 3478), cricket::PROTO_UDP, false));
1106   allocator_->AddTurnServer(turn_server);
1107 
1108   allocator_->set_step_delay(cricket::kMinimumStepDelay);
1109   allocator_->set_flags(allocator().flags() |
1110                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1111                         cricket::PORTALLOCATOR_DISABLE_TCP);
1112 
1113   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1114   session_->StartGettingPorts();
1115 
1116   EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout);
1117 }
1118 
1119 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port
1120 // is allocated for udp/stun/turn. In this test we should expect all local,
1121 // stun and turn candidates.
TEST_F(PortAllocatorTest,TestSharedSocketWithNatUsingTurn)1122 TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurn) {
1123   AddInterface(kClientAddr);
1124   ResetWithStunServerAndNat(kStunAddr);
1125 
1126   AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1127 
1128   allocator_->set_flags(allocator().flags() |
1129                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1130                         cricket::PORTALLOCATOR_DISABLE_TCP);
1131 
1132   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1133   session_->StartGettingPorts();
1134 
1135   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1136   ASSERT_EQ(2U, ports_.size());
1137   EXPECT_PRED5(CheckCandidate, candidates_[0],
1138       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1139   EXPECT_PRED5(CheckCandidate, candidates_[1],
1140       cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1141       rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1142   EXPECT_PRED5(CheckCandidate, candidates_[2],
1143       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1144       rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1145   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1146   EXPECT_EQ(3U, candidates_.size());
1147   // Local port will be created first and then TURN port.
1148   EXPECT_EQ(2U, ports_[0]->Candidates().size());
1149   EXPECT_EQ(1U, ports_[1]->Candidates().size());
1150 }
1151 
1152 // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN
1153 // server is also used as the STUN server, we should get 'local', 'stun', and
1154 // 'relay' candidates.
TEST_F(PortAllocatorTest,TestSharedSocketWithNatUsingTurnAsStun)1155 TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) {
1156   AddInterface(kClientAddr);
1157   // Use an empty SocketAddress to add a NAT without STUN server.
1158   ResetWithStunServerAndNat(SocketAddress());
1159   AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1160 
1161   // Must set the step delay to 0 to make sure the relay allocation phase is
1162   // started before the STUN candidates are obtained, so that the STUN binding
1163   // response is processed when both StunPort and TurnPort exist to reproduce
1164   // webrtc issue 3537.
1165   allocator_->set_step_delay(0);
1166   allocator_->set_flags(allocator().flags() |
1167                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1168                         cricket::PORTALLOCATOR_DISABLE_TCP);
1169 
1170   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1171   session_->StartGettingPorts();
1172 
1173   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1174   EXPECT_PRED5(CheckCandidate, candidates_[0],
1175       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1176   EXPECT_PRED5(CheckCandidate, candidates_[1],
1177       cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1178       rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1179   EXPECT_PRED5(CheckCandidate, candidates_[2],
1180       cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1181       rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1182   EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1183 
1184   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1185   EXPECT_EQ(3U, candidates_.size());
1186   // Local port will be created first and then TURN port.
1187   EXPECT_EQ(2U, ports_[0]->Candidates().size());
1188   EXPECT_EQ(1U, ports_[1]->Candidates().size());
1189 }
1190 
1191 // Test that when only a TCP TURN server is available, we do NOT use it as
1192 // a UDP STUN server, as this could leak our IP address. Thus we should only
1193 // expect two ports, a UDPPort and TurnPort.
TEST_F(PortAllocatorTest,TestSharedSocketWithNatUsingTurnTcpOnly)1194 TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) {
1195   turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP);
1196   AddInterface(kClientAddr);
1197   ResetWithStunServerAndNat(rtc::SocketAddress());
1198   AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr);
1199 
1200   allocator_->set_flags(allocator().flags() |
1201                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1202                         cricket::PORTALLOCATOR_DISABLE_TCP);
1203 
1204   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1205   session_->StartGettingPorts();
1206 
1207   ASSERT_EQ_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout);
1208   ASSERT_EQ(2U, ports_.size());
1209   EXPECT_PRED5(CheckCandidate, candidates_[0],
1210                cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1211                kClientAddr);
1212   EXPECT_PRED5(CheckCandidate, candidates_[1],
1213                cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1214                rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1215   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1216   EXPECT_EQ(2U, candidates_.size());
1217   EXPECT_EQ(1U, ports_[0]->Candidates().size());
1218   EXPECT_EQ(1U, ports_[1]->Candidates().size());
1219 }
1220 
1221 // Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the
1222 // TURN server is used as the STUN server and we get 'local', 'stun', and
1223 // 'relay' candidates.
1224 // TODO(deadbeef): Remove this test when support for non-shared socket mode
1225 // is removed.
TEST_F(PortAllocatorTest,TestNonSharedSocketWithNatUsingTurnAsStun)1226 TEST_F(PortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) {
1227   AddInterface(kClientAddr);
1228   // Use an empty SocketAddress to add a NAT without STUN server.
1229   ResetWithStunServerAndNat(SocketAddress());
1230   AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1231 
1232   allocator_->set_flags(allocator().flags() |
1233                         cricket::PORTALLOCATOR_DISABLE_TCP);
1234 
1235   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1236   session_->StartGettingPorts();
1237 
1238   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1239   ASSERT_EQ(3U, ports_.size());
1240   EXPECT_PRED5(CheckCandidate, candidates_[0],
1241                cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1242                kClientAddr);
1243   EXPECT_PRED5(CheckCandidate, candidates_[1],
1244                cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1245                rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1246   EXPECT_PRED5(CheckCandidate, candidates_[2],
1247                cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1248                rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1249   // Not using shared socket, so the STUN request's server reflexive address
1250   // should be different than the TURN request's server reflexive address.
1251   EXPECT_NE(candidates_[2].related_address(), candidates_[1].address());
1252 
1253   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1254   EXPECT_EQ(3U, candidates_.size());
1255   EXPECT_EQ(1U, ports_[0]->Candidates().size());
1256   EXPECT_EQ(1U, ports_[1]->Candidates().size());
1257   EXPECT_EQ(1U, ports_[2]->Candidates().size());
1258 }
1259 
1260 // Test that even when both a STUN and TURN server are configured, the TURN
1261 // server is used as a STUN server and we get a 'stun' candidate.
TEST_F(PortAllocatorTest,TestSharedSocketWithNatUsingTurnAndStun)1262 TEST_F(PortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) {
1263   AddInterface(kClientAddr);
1264   // Configure with STUN server but destroy it, so we can ensure that it's
1265   // the TURN server actually being used as a STUN server.
1266   ResetWithStunServerAndNat(kStunAddr);
1267   stun_server_.reset();
1268   AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress());
1269 
1270   allocator_->set_flags(allocator().flags() |
1271                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
1272                         cricket::PORTALLOCATOR_DISABLE_TCP);
1273 
1274   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1275   session_->StartGettingPorts();
1276 
1277   ASSERT_EQ_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout);
1278   EXPECT_PRED5(CheckCandidate, candidates_[0],
1279                cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1280                kClientAddr);
1281   EXPECT_PRED5(CheckCandidate, candidates_[1],
1282                cricket::ICE_CANDIDATE_COMPONENT_RTP, "stun", "udp",
1283                rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0));
1284   EXPECT_PRED5(CheckCandidate, candidates_[2],
1285                cricket::ICE_CANDIDATE_COMPONENT_RTP, "relay", "udp",
1286                rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0));
1287   EXPECT_EQ(candidates_[2].related_address(), candidates_[1].address());
1288 
1289   // Don't bother waiting for STUN timeout, since we already verified
1290   // that we got a STUN candidate from the TURN server.
1291 }
1292 
1293 // This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled
1294 // and fail to generate STUN candidate, local UDP candidate is generated
1295 // properly.
TEST_F(PortAllocatorTest,TestSharedSocketNoUdpAllowed)1296 TEST_F(PortAllocatorTest, TestSharedSocketNoUdpAllowed) {
1297   allocator().set_flags(allocator().flags() |
1298                         cricket::PORTALLOCATOR_DISABLE_RELAY |
1299                         cricket::PORTALLOCATOR_DISABLE_TCP |
1300                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1301   fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr);
1302   AddInterface(kClientAddr);
1303   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1304   session_->StartGettingPorts();
1305   ASSERT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1306   EXPECT_EQ(1U, candidates_.size());
1307   EXPECT_PRED5(CheckCandidate, candidates_[0],
1308       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp", kClientAddr);
1309   // STUN timeout is 9sec. We need to wait to get candidate done signal.
1310   EXPECT_TRUE_WAIT(candidate_allocation_done_, 10000);
1311   EXPECT_EQ(1U, candidates_.size());
1312 }
1313 
1314 // Test that when the NetworkManager doesn't have permission to enumerate
1315 // adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified
1316 // automatically.
TEST_F(PortAllocatorTest,TestNetworkPermissionBlocked)1317 TEST_F(PortAllocatorTest, TestNetworkPermissionBlocked) {
1318   network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(),
1319                                                rtc::IPAddress());
1320   network_manager_.set_enumeration_permission(
1321       rtc::NetworkManager::ENUMERATION_BLOCKED);
1322   allocator().set_flags(allocator().flags() |
1323                         cricket::PORTALLOCATOR_DISABLE_RELAY |
1324                         cricket::PORTALLOCATOR_DISABLE_TCP |
1325                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1326   EXPECT_EQ(0U, allocator_->flags() &
1327                     cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1328   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1329   EXPECT_EQ(0U, session_->flags() &
1330                     cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
1331   session_->StartGettingPorts();
1332   EXPECT_EQ_WAIT(1U, ports_.size(), kDefaultAllocationTimeout);
1333   EXPECT_EQ(1U, candidates_.size());
1334   EXPECT_PRED5(CheckCandidate, candidates_[0],
1335                cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1336                kPrivateAddr);
1337   EXPECT_TRUE((session_->flags() &
1338                cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION) != 0);
1339 }
1340 
1341 // This test verifies allocator can use IPv6 addresses along with IPv4.
TEST_F(PortAllocatorTest,TestEnableIPv6Addresses)1342 TEST_F(PortAllocatorTest, TestEnableIPv6Addresses) {
1343   allocator().set_flags(allocator().flags() |
1344                         cricket::PORTALLOCATOR_DISABLE_RELAY |
1345                         cricket::PORTALLOCATOR_ENABLE_IPV6 |
1346                         cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET);
1347   AddInterface(kClientIPv6Addr);
1348   AddInterface(kClientAddr);
1349   allocator_->set_step_delay(cricket::kMinimumStepDelay);
1350   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1351   session_->StartGettingPorts();
1352   ASSERT_EQ_WAIT(4U, ports_.size(), kDefaultAllocationTimeout);
1353   EXPECT_EQ(4U, candidates_.size());
1354   EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout);
1355   EXPECT_PRED5(CheckCandidate, candidates_[0],
1356       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1357       kClientIPv6Addr);
1358   EXPECT_PRED5(CheckCandidate, candidates_[1],
1359       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "udp",
1360       kClientAddr);
1361   EXPECT_PRED5(CheckCandidate, candidates_[2],
1362       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1363       kClientIPv6Addr);
1364   EXPECT_PRED5(CheckCandidate, candidates_[3],
1365       cricket::ICE_CANDIDATE_COMPONENT_RTP, "local", "tcp",
1366       kClientAddr);
1367   EXPECT_EQ(4U, candidates_.size());
1368 }
1369 
TEST_F(PortAllocatorTest,TestStopGettingPorts)1370 TEST_F(PortAllocatorTest, TestStopGettingPorts) {
1371   AddInterface(kClientAddr);
1372   allocator_->set_step_delay(cricket::kDefaultStepDelay);
1373   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1374   session_->StartGettingPorts();
1375   ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1376   EXPECT_EQ(2U, ports_.size());
1377   session_->StopGettingPorts();
1378   EXPECT_TRUE_WAIT(candidate_allocation_done_, 1000);
1379 
1380   // After stopping getting ports, adding a new interface will not start
1381   // getting ports again.
1382   candidates_.clear();
1383   ports_.clear();
1384   candidate_allocation_done_ = false;
1385   network_manager_.AddInterface(kClientAddr2);
1386   rtc::Thread::Current()->ProcessMessages(1000);
1387   EXPECT_EQ(0U, candidates_.size());
1388   EXPECT_EQ(0U, ports_.size());
1389 }
1390 
TEST_F(PortAllocatorTest,TestClearGettingPorts)1391 TEST_F(PortAllocatorTest, TestClearGettingPorts) {
1392   AddInterface(kClientAddr);
1393   allocator_->set_step_delay(cricket::kDefaultStepDelay);
1394   EXPECT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP));
1395   session_->StartGettingPorts();
1396   ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1397   EXPECT_EQ(2U, ports_.size());
1398   session_->ClearGettingPorts();
1399   WAIT(candidate_allocation_done_, 1000);
1400   EXPECT_FALSE(candidate_allocation_done_);
1401 
1402   // After clearing getting ports, adding a new interface will start getting
1403   // ports again.
1404   candidates_.clear();
1405   ports_.clear();
1406   candidate_allocation_done_ = false;
1407   network_manager_.AddInterface(kClientAddr2);
1408   ASSERT_EQ_WAIT(2U, candidates_.size(), 1000);
1409   EXPECT_EQ(2U, ports_.size());
1410 }
1411