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