| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "webrtc/base/physicalsocketserver.h" | 30 #include "webrtc/base/physicalsocketserver.h" |
| 31 #include "webrtc/base/socketaddress.h" | 31 #include "webrtc/base/socketaddress.h" |
| 32 #include "webrtc/base/ssladapter.h" | 32 #include "webrtc/base/ssladapter.h" |
| 33 #include "webrtc/base/thread.h" | 33 #include "webrtc/base/thread.h" |
| 34 #include "webrtc/base/virtualsocketserver.h" | 34 #include "webrtc/base/virtualsocketserver.h" |
| 35 | 35 |
| 36 using rtc::IPAddress; | 36 using rtc::IPAddress; |
| 37 using rtc::SocketAddress; | 37 using rtc::SocketAddress; |
| 38 using rtc::Thread; | 38 using rtc::Thread; |
| 39 | 39 |
| 40 static const SocketAddress kAnyAddr("0.0.0.0", 0); |
| 40 static const SocketAddress kClientAddr("11.11.11.11", 0); | 41 static const SocketAddress kClientAddr("11.11.11.11", 0); |
| 41 static const SocketAddress kClientAddr2("22.22.22.22", 0); | 42 static const SocketAddress kClientAddr2("22.22.22.22", 0); |
| 42 static const SocketAddress kLoopbackAddr("127.0.0.1", 0); | 43 static const SocketAddress kLoopbackAddr("127.0.0.1", 0); |
| 43 static const SocketAddress kPrivateAddr("192.168.1.11", 0); | 44 static const SocketAddress kPrivateAddr("192.168.1.11", 0); |
| 44 static const SocketAddress kPrivateAddr2("192.168.1.12", 0); | 45 static const SocketAddress kPrivateAddr2("192.168.1.12", 0); |
| 45 static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3", | 46 static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3", |
| 46 0); | 47 0); |
| 47 static const SocketAddress kClientIPv6Addr2( | 48 static const SocketAddress kClientIPv6Addr2( |
| 48 "2401:fa00:4:2000:be30:5bff:fee5:c3", | 49 "2401:fa00:4:2000:be30:5bff:fee5:c3", |
| 49 0); | 50 0); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 } | 108 } |
| 108 | 109 |
| 109 class BasicPortAllocatorTest : public testing::Test, | 110 class BasicPortAllocatorTest : public testing::Test, |
| 110 public sigslot::has_slots<> { | 111 public sigslot::has_slots<> { |
| 111 public: | 112 public: |
| 112 BasicPortAllocatorTest() | 113 BasicPortAllocatorTest() |
| 113 : pss_(new rtc::PhysicalSocketServer), | 114 : pss_(new rtc::PhysicalSocketServer), |
| 114 vss_(new rtc::VirtualSocketServer(pss_.get())), | 115 vss_(new rtc::VirtualSocketServer(pss_.get())), |
| 115 fss_(new rtc::FirewallSocketServer(vss_.get())), | 116 fss_(new rtc::FirewallSocketServer(vss_.get())), |
| 116 ss_scope_(fss_.get()), | 117 ss_scope_(fss_.get()), |
| 118 // Note that the NAT is not used by default. ResetWithStunServerAndNat |
| 119 // must be called. |
| 117 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr), | 120 nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr), |
| 118 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)), | 121 nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)), |
| 119 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)), | 122 stun_server_(TestStunServer::Create(Thread::Current(), kStunAddr)), |
| 120 relay_server_(Thread::Current(), | 123 relay_server_(Thread::Current(), |
| 121 kRelayUdpIntAddr, | 124 kRelayUdpIntAddr, |
| 122 kRelayUdpExtAddr, | 125 kRelayUdpExtAddr, |
| 123 kRelayTcpIntAddr, | 126 kRelayTcpIntAddr, |
| 124 kRelayTcpExtAddr, | 127 kRelayTcpExtAddr, |
| 125 kRelaySslTcpIntAddr, | 128 kRelaySslTcpIntAddr, |
| 126 kRelaySslTcpExtAddr), | 129 kRelaySslTcpExtAddr), |
| 127 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr), | 130 turn_server_(Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr), |
| 128 candidate_allocation_done_(false) { | 131 candidate_allocation_done_(false) { |
| 129 ServerAddresses stun_servers; | 132 ServerAddresses stun_servers; |
| 130 stun_servers.insert(kStunAddr); | 133 stun_servers.insert(kStunAddr); |
| 131 // Passing the addresses of GTURN servers will enable GTURN in | 134 // Passing the addresses of GTURN servers will enable GTURN in |
| 132 // Basicportallocator. | 135 // Basicportallocator. |
| 136 // TODO(deadbeef): Stop using GTURN by default in this test... Either the |
| 137 // configuration should be blank by default (preferred), or it should use |
| 138 // TURN instead. |
| 133 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers, | 139 allocator_.reset(new BasicPortAllocator(&network_manager_, stun_servers, |
| 134 kRelayUdpIntAddr, kRelayTcpIntAddr, | 140 kRelayUdpIntAddr, kRelayTcpIntAddr, |
| 135 kRelaySslTcpIntAddr)); | 141 kRelaySslTcpIntAddr)); |
| 136 allocator_->set_step_delay(kMinimumStepDelay); | 142 allocator_->set_step_delay(kMinimumStepDelay); |
| 137 } | 143 } |
| 138 | 144 |
| 139 void AddInterface(const SocketAddress& addr) { | 145 void AddInterface(const SocketAddress& addr) { |
| 140 network_manager_.AddInterface(addr); | 146 network_manager_.AddInterface(addr); |
| 141 } | 147 } |
| 142 void AddInterface(const SocketAddress& addr, const std::string& if_name) { | 148 void AddInterface(const SocketAddress& addr, const std::string& if_name) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 ProtocolType protocol, | 274 ProtocolType protocol, |
| 269 const SocketAddress& client_addr) { | 275 const SocketAddress& client_addr) { |
| 270 return std::count_if( | 276 return std::count_if( |
| 271 ports.begin(), ports.end(), | 277 ports.begin(), ports.end(), |
| 272 [type, protocol, client_addr](PortInterface* port) { | 278 [type, protocol, client_addr](PortInterface* port) { |
| 273 return port->Type() == type && port->GetProtocol() == protocol && | 279 return port->Type() == type && port->GetProtocol() == protocol && |
| 274 port->Network()->GetBestIP() == client_addr.ipaddr(); | 280 port->Network()->GetBestIP() == client_addr.ipaddr(); |
| 275 }); | 281 }); |
| 276 } | 282 } |
| 277 | 283 |
| 284 static int CountCandidates(const std::vector<Candidate>& candidates, |
| 285 const std::string& type, |
| 286 const std::string& proto, |
| 287 const SocketAddress& addr) { |
| 288 return std::count_if(candidates.begin(), candidates.end(), |
| 289 [type, proto, addr](const Candidate& c) { |
| 290 return c.type() == type && c.protocol() == proto && |
| 291 AddressMatch(c.address(), addr); |
| 292 }); |
| 293 } |
| 294 |
| 278 // Find a candidate and return it. | 295 // Find a candidate and return it. |
| 279 static bool FindCandidate(const std::vector<Candidate>& candidates, | 296 static bool FindCandidate(const std::vector<Candidate>& candidates, |
| 280 const std::string& type, | 297 const std::string& type, |
| 281 const std::string& proto, | 298 const std::string& proto, |
| 282 const SocketAddress& addr, | 299 const SocketAddress& addr, |
| 283 Candidate* found) { | 300 Candidate* found) { |
| 284 auto it = std::find_if(candidates.begin(), candidates.end(), | 301 auto it = std::find_if(candidates.begin(), candidates.end(), |
| 285 [type, proto, addr](const Candidate& c) { | 302 [type, proto, addr](const Candidate& c) { |
| 286 return c.type() == type && c.protocol() == proto && | 303 return c.type() == type && c.protocol() == proto && |
| 287 AddressMatch(c.address(), addr); | 304 AddressMatch(c.address(), addr); |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 // Wi-Fi interface. | 768 // Wi-Fi interface. |
| 752 candidates_.clear(); | 769 candidates_.clear(); |
| 753 candidate_allocation_done_ = false; | 770 candidate_allocation_done_ = false; |
| 754 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI); | 771 AddInterface(addr_wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI); |
| 755 session_->StartGettingPorts(); | 772 session_->StartGettingPorts(); |
| 756 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); | 773 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); |
| 757 EXPECT_EQ(1U, candidates_.size()); | 774 EXPECT_EQ(1U, candidates_.size()); |
| 758 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address())); | 775 EXPECT_TRUE(addr_wifi.EqualIPs(candidates_[0].address())); |
| 759 } | 776 } |
| 760 | 777 |
| 761 // Tests that we allocator session not trying to allocate ports for every 250ms. | |
| 762 TEST_F(BasicPortAllocatorTest, TestNoNetworkInterface) { | |
| 763 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); | |
| 764 session_->StartGettingPorts(); | |
| 765 // Waiting for one second to make sure BasicPortAllocatorSession has not | |
| 766 // called OnAllocate multiple times. In old behavior it's called every 250ms. | |
| 767 // When there are no network interfaces, each execution of OnAllocate will | |
| 768 // result in SignalCandidatesAllocationDone signal. | |
| 769 rtc::Thread::Current()->ProcessMessages(1000); | |
| 770 EXPECT_TRUE(candidate_allocation_done_); | |
| 771 EXPECT_EQ(0U, candidates_.size()); | |
| 772 } | |
| 773 | |
| 774 // Test that we could use loopback interface as host candidate. | 778 // Test that we could use loopback interface as host candidate. |
| 775 TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) { | 779 TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) { |
| 776 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK); | 780 AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK); |
| 777 allocator_->SetNetworkIgnoreMask(0); | 781 allocator_->SetNetworkIgnoreMask(0); |
| 778 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); | 782 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 779 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY | | 783 session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY | |
| 780 PORTALLOCATOR_DISABLE_TCP); | 784 PORTALLOCATOR_DISABLE_TCP); |
| 781 session_->StartGettingPorts(); | 785 session_->StartGettingPorts(); |
| 782 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); | 786 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); |
| 783 EXPECT_EQ(1U, candidates_.size()); | 787 EXPECT_EQ(1U, candidates_.size()); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 929 ++num_nonrelay_candidates; | 933 ++num_nonrelay_candidates; |
| 930 } | 934 } |
| 931 } | 935 } |
| 932 EXPECT_EQ(3, num_nonrelay_candidates); | 936 EXPECT_EQ(3, num_nonrelay_candidates); |
| 933 // Check the port number used to connect to the relay server. | 937 // Check the port number used to connect to the relay server. |
| 934 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort, | 938 EXPECT_PRED3(CheckPort, relay_server_.GetConnection(0).source(), kMinPort, |
| 935 kMaxPort); | 939 kMaxPort); |
| 936 EXPECT_TRUE(candidate_allocation_done_); | 940 EXPECT_TRUE(candidate_allocation_done_); |
| 937 } | 941 } |
| 938 | 942 |
| 939 // Test that we don't crash or malfunction if we have no network adapters. | 943 // Test that if we have no network adapters, we bind to the ANY address and |
| 944 // still get non-host candidates. |
| 940 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) { | 945 TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) { |
| 946 // Default config uses GTURN and no NAT, so replace that with the |
| 947 // desired setup (NAT, STUN server, TURN server, UDP/TCP). |
| 948 ResetWithStunServerAndNat(kStunAddr); |
| 949 turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 950 AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 951 AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr); |
| 952 // Disable IPv6, because our test infrastructure doesn't support having IPv4 |
| 953 // behind a NAT but IPv6 not, or having an IPv6 NAT. |
| 954 // TODO(deadbeef): Fix this. |
| 955 network_manager_.set_ipv6_enabled(false); |
| 941 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); | 956 EXPECT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 942 session_->StartGettingPorts(); | 957 session_->StartGettingPorts(); |
| 943 rtc::Thread::Current()->ProcessMessages(100); | 958 EXPECT_TRUE_WAIT(candidate_allocation_done_, kDefaultAllocationTimeout); |
| 944 // Without network adapter, we should not get any candidate. | 959 EXPECT_EQ(4U, ports_.size()); |
| 945 EXPECT_EQ(0U, candidates_.size()); | 960 EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr)); |
| 946 EXPECT_TRUE(candidate_allocation_done_); | 961 EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr)); |
| 962 // Two TURN ports, using UDP/TCP for the first hop to the TURN server. |
| 963 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr)); |
| 964 EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr)); |
| 965 // The "any" address port should be in the signaled ready ports, but the host |
| 966 // candidate for it is useless and shouldn't be signaled. So we only have |
| 967 // STUN/TURN candidates. |
| 968 EXPECT_EQ(3U, candidates_.size()); |
| 969 EXPECT_PRED4(HasCandidate, candidates_, "stun", "udp", |
| 970 rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0)); |
| 971 // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN |
| 972 // server. |
| 973 EXPECT_EQ(2, |
| 974 CountCandidates(candidates_, "relay", "udp", |
| 975 rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
| 947 } | 976 } |
| 948 | 977 |
| 949 // Test that when enumeration is disabled, we should not have any ports when | 978 // Test that when enumeration is disabled, we should not have any ports when |
| 950 // candidate_filter() is set to CF_RELAY and no relay is specified. | 979 // candidate_filter() is set to CF_RELAY and no relay is specified. |
| 951 TEST_F(BasicPortAllocatorTest, | 980 TEST_F(BasicPortAllocatorTest, |
| 952 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) { | 981 TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) { |
| 953 ResetWithStunServerNoNat(kStunAddr); | 982 ResetWithStunServerNoNat(kStunAddr); |
| 954 allocator().set_candidate_filter(CF_RELAY); | 983 allocator().set_candidate_filter(CF_RELAY); |
| 955 // Expect to see no ports and no candidates. | 984 // Expect to see no ports and no candidates. |
| 956 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(), | 985 CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(), |
| (...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1796 for (const Candidate& candidate : candidates) { | 1825 for (const Candidate& candidate : candidates) { |
| 1797 // Expect only relay candidates now that the filter is applied. | 1826 // Expect only relay candidates now that the filter is applied. |
| 1798 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type()); | 1827 EXPECT_EQ(std::string(RELAY_PORT_TYPE), candidate.type()); |
| 1799 // Expect that the raddr is emptied due to the CF_RELAY filter. | 1828 // Expect that the raddr is emptied due to the CF_RELAY filter. |
| 1800 EXPECT_EQ(candidate.related_address(), | 1829 EXPECT_EQ(candidate.related_address(), |
| 1801 rtc::EmptySocketAddressWithFamily(candidate.address().family())); | 1830 rtc::EmptySocketAddressWithFamily(candidate.address().family())); |
| 1802 } | 1831 } |
| 1803 } | 1832 } |
| 1804 | 1833 |
| 1805 } // namespace cricket | 1834 } // namespace cricket |
| OLD | NEW |