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