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