henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "p2p/client/basic_port_allocator.h" |
| 12 | |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 13 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 15 | |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "p2p/base/basic_packet_socket_factory.h" |
| 19 | #include "p2p/base/p2p_constants.h" |
| 20 | #include "p2p/base/stun_port.h" |
| 21 | #include "p2p/base/stun_request.h" |
| 22 | #include "p2p/base/stun_server.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "p2p/base/test_stun_server.h" |
| 24 | #include "p2p/base/test_turn_server.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 25 | #include "rtc_base/fake_clock.h" |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [diff] [blame] | 26 | #include "rtc_base/fake_mdns_responder.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 27 | #include "rtc_base/fake_network.h" |
| 28 | #include "rtc_base/firewall_socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 29 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 30 | #include "rtc_base/ip_address.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 31 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 32 | #include "rtc_base/nat_server.h" |
| 33 | #include "rtc_base/nat_socket_factory.h" |
| 34 | #include "rtc_base/nat_types.h" |
| 35 | #include "rtc_base/net_helper.h" |
| 36 | #include "rtc_base/net_helpers.h" |
Mirko Bonadei | 0d8b79e | 2023-07-27 06:55:49 | [diff] [blame] | 37 | #include "rtc_base/net_test_helpers.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 38 | #include "rtc_base/network.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 39 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 40 | #include "rtc_base/network_monitor.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 41 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 42 | #include "rtc_base/socket_address.h" |
| 43 | #include "rtc_base/socket_address_pair.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 44 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 45 | #include "rtc_base/virtual_socket_server.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 06:51:10 | [diff] [blame] | 46 | #include "system_wrappers/include/metrics.h" |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 47 | #include "test/gmock.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 48 | #include "test/gtest.h" |
Jonas Oreland | 340cb5e | 2022-03-17 08:14:16 | [diff] [blame] | 49 | #include "test/scoped_key_value_config.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 50 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 51 | using rtc::IPAddress; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 52 | using rtc::SocketAddress; |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 53 | using ::testing::Contains; |
| 54 | using ::testing::Not; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 55 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 56 | #define MAYBE_SKIP_IPV4 \ |
| 57 | if (!rtc::HasIPv4Enabled()) { \ |
| 58 | RTC_LOG(LS_INFO) << "No IPv4... skipping"; \ |
| 59 | return; \ |
deadbeef | 9a6f4d4 | 2017-05-16 02:43:33 | [diff] [blame] | 60 | } |
| 61 | |
deadbeef | e97389c | 2016-12-23 09:43:45 | [diff] [blame] | 62 | static const SocketAddress kAnyAddr("0.0.0.0", 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 63 | static const SocketAddress kClientAddr("11.11.11.11", 0); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 64 | static const SocketAddress kClientAddr2("22.22.22.22", 0); |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 65 | static const SocketAddress kLoopbackAddr("127.0.0.1", 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 66 | static const SocketAddress kPrivateAddr("192.168.1.11", 0); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 | [diff] [blame] | 67 | static const SocketAddress kPrivateAddr2("192.168.1.12", 0); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 68 | static const SocketAddress kClientIPv6Addr("2401:fa00:4:1000:be30:5bff:fee5:c3", |
| 69 | 0); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 70 | static const SocketAddress kClientIPv6Addr2( |
| 71 | "2401:fa00:4:2000:be30:5bff:fee5:c3", |
| 72 | 0); |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 73 | static const SocketAddress kClientIPv6Addr3( |
| 74 | "2401:fa00:4:3000:be30:5bff:fee5:c3", |
| 75 | 0); |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 76 | static const SocketAddress kClientIPv6Addr4( |
| 77 | "2401:fa00:4:4000:be30:5bff:fee5:c3", |
| 78 | 0); |
| 79 | static const SocketAddress kClientIPv6Addr5( |
| 80 | "2401:fa00:4:5000:be30:5bff:fee5:c3", |
| 81 | 0); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 82 | static const SocketAddress kNatUdpAddr("77.77.77.77", rtc::NAT_SERVER_UDP_PORT); |
| 83 | static const SocketAddress kNatTcpAddr("77.77.77.77", rtc::NAT_SERVER_TCP_PORT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 84 | static const SocketAddress kRemoteClientAddr("22.22.22.22", 0); |
| 85 | static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 86 | static const SocketAddress kTurnUdpIntAddr("99.99.99.4", 3478); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 87 | static const SocketAddress kTurnUdpIntIPv6Addr( |
| 88 | "2402:fb00:4:1000:be30:5bff:fee5:c3", |
| 89 | 3479); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 90 | static const SocketAddress kTurnTcpIntAddr("99.99.99.5", 3478); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 91 | static const SocketAddress kTurnTcpIntIPv6Addr( |
| 92 | "2402:fb00:4:2000:be30:5bff:fee5:c3", |
| 93 | 3479); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 94 | static const SocketAddress kTurnUdpExtAddr("99.99.99.6", 0); |
| 95 | |
| 96 | // Minimum and maximum port for port range tests. |
| 97 | static const int kMinPort = 10000; |
| 98 | static const int kMaxPort = 10099; |
| 99 | |
| 100 | // Based on ICE_UFRAG_LENGTH |
zhihuang | 6d0d4bf | 2016-05-24 17:13:32 | [diff] [blame] | 101 | static const char kIceUfrag0[] = "UF00"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 102 | // Based on ICE_PWD_LENGTH |
| 103 | static const char kIcePwd0[] = "TESTICEPWD00000000000000"; |
| 104 | |
| 105 | static const char kContentName[] = "test content"; |
| 106 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 107 | static const int kDefaultAllocationTimeout = 3000; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 108 | static const char kTurnUsername[] = "test"; |
| 109 | static const char kTurnPassword[] = "test"; |
| 110 | |
pthatcher | 94a2f21 | 2017-02-08 22:42:22 | [diff] [blame] | 111 | // STUN timeout (with all retries) is cricket::STUN_TOTAL_TIMEOUT. |
Taylor Brandstetter | 8fcf414 | 2016-05-23 19:49:30 | [diff] [blame] | 112 | // Add some margin of error for slow bots. |
pthatcher | 94a2f21 | 2017-02-08 22:42:22 | [diff] [blame] | 113 | static const int kStunTimeoutMs = cricket::STUN_TOTAL_TIMEOUT; |
Taylor Brandstetter | 8fcf414 | 2016-05-23 19:49:30 | [diff] [blame] | 114 | |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 115 | namespace { |
| 116 | |
| 117 | void CheckStunKeepaliveIntervalOfAllReadyPorts( |
| 118 | const cricket::PortAllocatorSession* allocator_session, |
| 119 | int expected) { |
| 120 | auto ready_ports = allocator_session->ReadyPorts(); |
| 121 | for (const auto* port : ready_ports) { |
| 122 | if (port->Type() == cricket::STUN_PORT_TYPE || |
| 123 | (port->Type() == cricket::LOCAL_PORT_TYPE && |
| 124 | port->GetProtocol() == cricket::PROTO_UDP)) { |
| 125 | EXPECT_EQ( |
| 126 | static_cast<const cricket::UDPPort*>(port)->stun_keepalive_delay(), |
| 127 | expected); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace |
| 133 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 134 | namespace cricket { |
| 135 | |
| 136 | // Helper for dumping candidates |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 137 | std::ostream& operator<<(std::ostream& os, |
| 138 | const std::vector<Candidate>& candidates) { |
| 139 | os << '['; |
| 140 | bool first = true; |
| 141 | for (const Candidate& c : candidates) { |
| 142 | if (!first) { |
| 143 | os << ", "; |
| 144 | } |
| 145 | os << c.ToString(); |
| 146 | first = false; |
Steve Anton | 6c38cc7 | 2017-11-29 18:25:58 | [diff] [blame] | 147 | } |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 148 | os << ']'; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 149 | return os; |
| 150 | } |
| 151 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 152 | class BasicPortAllocatorTestBase : public ::testing::Test, |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 153 | public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 154 | public: |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 155 | BasicPortAllocatorTestBase() |
deadbeef | 98e186c | 2017-05-17 01:00:06 | [diff] [blame] | 156 | : vss_(new rtc::VirtualSocketServer()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 157 | fss_(new rtc::FirewallSocketServer(vss_.get())), |
Tommi | fe57cec | 2023-08-30 08:07:06 | [diff] [blame] | 158 | socket_factory_(fss_.get()), |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 159 | thread_(fss_.get()), |
deadbeef | e97389c | 2016-12-23 09:43:45 | [diff] [blame] | 160 | // Note that the NAT is not used by default. ResetWithStunServerAndNat |
| 161 | // must be called. |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 162 | nat_factory_(vss_.get(), kNatUdpAddr, kNatTcpAddr), |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 163 | nat_socket_factory_(new rtc::BasicPacketSocketFactory(&nat_factory_)), |
Per K | 14630a7 | 2023-11-22 13:08:58 | [diff] [blame] | 164 | stun_server_(TestStunServer::Create(fss_.get(), kStunAddr, thread_)), |
Niels Möller | 6dd4997 | 2021-11-24 13:05:55 | [diff] [blame] | 165 | turn_server_(rtc::Thread::Current(), |
| 166 | fss_.get(), |
| 167 | kTurnUdpIntAddr, |
| 168 | kTurnUdpExtAddr), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 169 | candidate_allocation_done_(false) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 170 | ServerAddresses stun_servers; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 171 | stun_servers.insert(kStunAddr); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 172 | |
Byoungchan Lee | d197e0b | 2022-05-30 14:59:55 | [diff] [blame] | 173 | allocator_ = std::make_unique<BasicPortAllocator>( |
Tommi | fe57cec | 2023-08-30 08:07:06 | [diff] [blame] | 174 | &network_manager_, &socket_factory_, stun_servers, &field_trials_); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 175 | allocator_->Initialize(); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 176 | allocator_->set_step_delay(kMinimumStepDelay); |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 177 | webrtc::metrics::Reset(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void AddInterface(const SocketAddress& addr) { |
| 181 | network_manager_.AddInterface(addr); |
| 182 | } |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 183 | void AddInterface(const SocketAddress& addr, absl::string_view if_name) { |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 184 | network_manager_.AddInterface(addr, if_name); |
| 185 | } |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 186 | void AddInterface(const SocketAddress& addr, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 187 | absl::string_view if_name, |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 188 | rtc::AdapterType type) { |
| 189 | network_manager_.AddInterface(addr, if_name, type); |
| 190 | } |
Niels Möller | 84d1595 | 2021-09-01 08:50:34 | [diff] [blame] | 191 | // The default source address is the public address that STUN server will |
| 192 | // observe when the endpoint is sitting on the public internet and the local |
| 193 | // port is bound to the "any" address. Intended for simulating the situation |
| 194 | // that client binds the "any" address, and that's also the address returned |
| 195 | // by getsockname/GetLocalAddress, so that the client can learn the actual |
| 196 | // local address only from the STUN response. |
| 197 | void AddInterfaceAsDefaultSourceAddresss(const SocketAddress& addr) { |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 198 | AddInterface(addr); |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 199 | // When a binding comes from the any address, the `addr` will be used as the |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 200 | // srflx address. |
Niels Möller | 84d1595 | 2021-09-01 08:50:34 | [diff] [blame] | 201 | vss_->SetDefaultSourceAddress(addr.ipaddr()); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 202 | } |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 203 | void RemoveInterface(const SocketAddress& addr) { |
| 204 | network_manager_.RemoveInterface(addr); |
| 205 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 206 | bool SetPortRange(int min_port, int max_port) { |
| 207 | return allocator_->SetPortRange(min_port, max_port); |
| 208 | } |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 209 | // Endpoint is on the public network. No STUN or TURN. |
| 210 | void ResetWithNoServersOrNat() { |
Tommi | 3756e29 | 2023-09-01 10:45:06 | [diff] [blame] | 211 | allocator_.reset( |
| 212 | new BasicPortAllocator(&network_manager_, &socket_factory_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 213 | allocator_->Initialize(); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 214 | allocator_->set_step_delay(kMinimumStepDelay); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 215 | } |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 216 | // Endpoint is behind a NAT, with STUN specified. |
| 217 | void ResetWithStunServerAndNat(const rtc::SocketAddress& stun_server) { |
| 218 | ResetWithStunServer(stun_server, true); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 219 | } |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 220 | // Endpoint is on the public network, with STUN specified. |
| 221 | void ResetWithStunServerNoNat(const rtc::SocketAddress& stun_server) { |
| 222 | ResetWithStunServer(stun_server, false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 223 | } |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 224 | // Endpoint is on the public network, with TURN specified. |
| 225 | void ResetWithTurnServersNoNat(const rtc::SocketAddress& udp_turn, |
| 226 | const rtc::SocketAddress& tcp_turn) { |
| 227 | ResetWithNoServersOrNat(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 228 | AddTurnServers(udp_turn, tcp_turn); |
| 229 | } |
| 230 | |
Honghai Zhang | 6981fb5 | 2019-10-29 19:45:34 | [diff] [blame] | 231 | RelayServerConfig CreateTurnServers(const rtc::SocketAddress& udp_turn, |
| 232 | const rtc::SocketAddress& tcp_turn) { |
Niels Möller | 191e38f | 2019-11-04 07:49:12 | [diff] [blame] | 233 | RelayServerConfig turn_server; |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 234 | RelayCredentials credentials(kTurnUsername, kTurnPassword); |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 235 | turn_server.credentials = credentials; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 236 | |
| 237 | if (!udp_turn.IsNil()) { |
hnsl | 277b250 | 2016-12-13 13:17:23 | [diff] [blame] | 238 | turn_server.ports.push_back(ProtocolAddress(udp_turn, PROTO_UDP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 239 | } |
| 240 | if (!tcp_turn.IsNil()) { |
hnsl | 277b250 | 2016-12-13 13:17:23 | [diff] [blame] | 241 | turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 242 | } |
Honghai Zhang | 6981fb5 | 2019-10-29 19:45:34 | [diff] [blame] | 243 | return turn_server; |
| 244 | } |
| 245 | |
| 246 | void AddTurnServers(const rtc::SocketAddress& udp_turn, |
| 247 | const rtc::SocketAddress& tcp_turn) { |
| 248 | RelayServerConfig turn_server = CreateTurnServers(udp_turn, tcp_turn); |
Jonas Oreland | 3e7e15d | 2022-11-03 09:02:55 | [diff] [blame] | 249 | allocator_->AddTurnServerForTesting(turn_server); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool CreateSession(int component) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 253 | session_ = CreateSession("session", component); |
| 254 | if (!session_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 255 | return false; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 256 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 257 | return true; |
| 258 | } |
| 259 | |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 260 | bool CreateSession(int component, absl::string_view content_name) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 261 | session_ = CreateSession("session", content_name, component); |
| 262 | if (!session_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 263 | return false; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 264 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 268 | std::unique_ptr<PortAllocatorSession> CreateSession(absl::string_view sid, |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 269 | int component) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 270 | return CreateSession(sid, kContentName, component); |
| 271 | } |
| 272 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 273 | std::unique_ptr<PortAllocatorSession> CreateSession( |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 274 | absl::string_view sid, |
| 275 | absl::string_view content_name, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 276 | int component) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 277 | return CreateSession(sid, content_name, component, kIceUfrag0, kIcePwd0); |
| 278 | } |
| 279 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 280 | std::unique_ptr<PortAllocatorSession> CreateSession( |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 281 | absl::string_view sid, |
| 282 | absl::string_view content_name, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 283 | int component, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 284 | absl::string_view ice_ufrag, |
| 285 | absl::string_view ice_pwd) { |
johan | fe1ffb1 | 2016-08-11 19:37:42 | [diff] [blame] | 286 | std::unique_ptr<PortAllocatorSession> session = |
| 287 | allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 288 | session->SignalPortReady.connect(this, |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 289 | &BasicPortAllocatorTestBase::OnPortReady); |
| 290 | session->SignalPortsPruned.connect( |
| 291 | this, &BasicPortAllocatorTestBase::OnPortsPruned); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 292 | session->SignalCandidatesReady.connect( |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 293 | this, &BasicPortAllocatorTestBase::OnCandidatesReady); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 294 | session->SignalCandidatesRemoved.connect( |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 295 | this, &BasicPortAllocatorTestBase::OnCandidatesRemoved); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 296 | session->SignalCandidatesAllocationDone.connect( |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 297 | this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 298 | return session; |
| 299 | } |
| 300 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 301 | // Return true if the addresses are the same, or the port is 0 in `pattern` |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 302 | // (acting as a wildcard) and the IPs are the same. |
| 303 | // Even with a wildcard port, the port of the address should be nonzero if |
| 304 | // the IP is nonzero. |
| 305 | static bool AddressMatch(const SocketAddress& address, |
| 306 | const SocketAddress& pattern) { |
| 307 | return address.ipaddr() == pattern.ipaddr() && |
| 308 | ((pattern.port() == 0 && |
| 309 | (address.port() != 0 || IPIsAny(address.ipaddr()))) || |
| 310 | (pattern.port() != 0 && address.port() == pattern.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 311 | } |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 312 | |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 313 | // Returns the number of ports that have matching type, protocol and |
| 314 | // address. |
| 315 | static int CountPorts(const std::vector<PortInterface*>& ports, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 316 | absl::string_view type, |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 317 | ProtocolType protocol, |
| 318 | const SocketAddress& client_addr) { |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 319 | return absl::c_count_if( |
| 320 | ports, [type, protocol, client_addr](PortInterface* port) { |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 321 | return port->Type() == type && port->GetProtocol() == protocol && |
| 322 | port->Network()->GetBestIP() == client_addr.ipaddr(); |
| 323 | }); |
| 324 | } |
| 325 | |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 326 | // Find a candidate and return it. |
| 327 | static bool FindCandidate(const std::vector<Candidate>& candidates, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 328 | absl::string_view type, |
| 329 | absl::string_view proto, |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 330 | const SocketAddress& addr, |
| 331 | Candidate* found) { |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 332 | auto it = |
| 333 | absl::c_find_if(candidates, [type, proto, addr](const Candidate& c) { |
| 334 | return c.type() == type && c.protocol() == proto && |
| 335 | AddressMatch(c.address(), addr); |
| 336 | }); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 337 | if (it != candidates.end() && found) { |
| 338 | *found = *it; |
| 339 | } |
| 340 | return it != candidates.end(); |
| 341 | } |
| 342 | |
| 343 | // Convenience method to call FindCandidate with no return. |
| 344 | static bool HasCandidate(const std::vector<Candidate>& candidates, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 345 | absl::string_view type, |
| 346 | absl::string_view proto, |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 347 | const SocketAddress& addr) { |
| 348 | return FindCandidate(candidates, type, proto, addr, nullptr); |
| 349 | } |
| 350 | |
| 351 | // Version of HasCandidate that also takes a related address. |
| 352 | static bool HasCandidateWithRelatedAddr( |
| 353 | const std::vector<Candidate>& candidates, |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 354 | absl::string_view type, |
| 355 | absl::string_view proto, |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 356 | const SocketAddress& addr, |
| 357 | const SocketAddress& related_addr) { |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 358 | return absl::c_any_of( |
| 359 | candidates, [type, proto, addr, related_addr](const Candidate& c) { |
| 360 | return c.type() == type && c.protocol() == proto && |
| 361 | AddressMatch(c.address(), addr) && |
| 362 | AddressMatch(c.related_address(), related_addr); |
| 363 | }); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 364 | } |
| 365 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 366 | static bool CheckPort(const rtc::SocketAddress& addr, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 367 | int min_port, |
| 368 | int max_port) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 369 | return (addr.port() >= min_port && addr.port() <= max_port); |
| 370 | } |
| 371 | |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 372 | static bool HasNetwork(const std::vector<const rtc::Network*>& networks, |
| 373 | const rtc::Network& to_be_found) { |
| 374 | auto it = |
| 375 | absl::c_find_if(networks, [to_be_found](const rtc::Network* network) { |
| 376 | return network->description() == to_be_found.description() && |
| 377 | network->name() == to_be_found.name() && |
| 378 | network->prefix() == to_be_found.prefix(); |
| 379 | }); |
| 380 | return it != networks.end(); |
| 381 | } |
| 382 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 383 | void OnCandidatesAllocationDone(PortAllocatorSession* session) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 384 | // We should only get this callback once, except in the mux test where |
| 385 | // we have multiple port allocation sessions. |
| 386 | if (session == session_.get()) { |
| 387 | ASSERT_FALSE(candidate_allocation_done_); |
| 388 | candidate_allocation_done_ = true; |
| 389 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 390 | EXPECT_TRUE(session->CandidatesAllocationDone()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 391 | } |
| 392 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 393 | // Check if all ports allocated have send-buffer size `expected`. If |
| 394 | // `expected` == -1, check if GetOptions returns SOCKET_ERROR. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 395 | void CheckSendBufferSizesOfAllPorts(int expected) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 396 | std::vector<PortInterface*>::iterator it; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 397 | for (it = ports_.begin(); it < ports_.end(); ++it) { |
| 398 | int send_buffer_size; |
| 399 | if (expected == -1) { |
| 400 | EXPECT_EQ(SOCKET_ERROR, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 401 | (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 402 | } else { |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 403 | EXPECT_EQ(0, |
| 404 | (*it)->GetOption(rtc::Socket::OPT_SNDBUF, &send_buffer_size)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 405 | ASSERT_EQ(expected, send_buffer_size); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 410 | rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); } |
| 411 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 412 | protected: |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 413 | BasicPortAllocator& allocator() { return *allocator_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 414 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 415 | void OnPortReady(PortAllocatorSession* ses, PortInterface* port) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 416 | RTC_LOG(LS_INFO) << "OnPortReady: " << port->ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 417 | ports_.push_back(port); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 418 | // Make sure the new port is added to ReadyPorts. |
| 419 | auto ready_ports = ses->ReadyPorts(); |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 420 | EXPECT_THAT(ready_ports, Contains(port)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 421 | } |
Honghai Zhang | 8eeecab | 2016-07-28 20:20:15 | [diff] [blame] | 422 | void OnPortsPruned(PortAllocatorSession* ses, |
Honghai Zhang | a74363c | 2016-07-29 01:06:15 | [diff] [blame] | 423 | const std::vector<PortInterface*>& pruned_ports) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 424 | RTC_LOG(LS_INFO) << "Number of ports pruned: " << pruned_ports.size(); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 425 | auto ready_ports = ses->ReadyPorts(); |
Honghai Zhang | 8eeecab | 2016-07-28 20:20:15 | [diff] [blame] | 426 | auto new_end = ports_.end(); |
Honghai Zhang | a74363c | 2016-07-29 01:06:15 | [diff] [blame] | 427 | for (PortInterface* port : pruned_ports) { |
Honghai Zhang | 8eeecab | 2016-07-28 20:20:15 | [diff] [blame] | 428 | new_end = std::remove(ports_.begin(), new_end, port); |
| 429 | // Make sure the pruned port is not in ReadyPorts. |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 430 | EXPECT_THAT(ready_ports, Not(Contains(port))); |
Honghai Zhang | 8eeecab | 2016-07-28 20:20:15 | [diff] [blame] | 431 | } |
| 432 | ports_.erase(new_end, ports_.end()); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 433 | } |
| 434 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 435 | void OnCandidatesReady(PortAllocatorSession* ses, |
| 436 | const std::vector<Candidate>& candidates) { |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 437 | for (const Candidate& candidate : candidates) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 438 | RTC_LOG(LS_INFO) << "OnCandidatesReady: " << candidate.ToString(); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 439 | // Sanity check that the ICE component is set. |
| 440 | EXPECT_EQ(ICE_CANDIDATE_COMPONENT_RTP, candidate.component()); |
| 441 | candidates_.push_back(candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 442 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 443 | // Make sure the new candidates are added to Candidates. |
| 444 | auto ses_candidates = ses->ReadyCandidates(); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 445 | for (const Candidate& candidate : candidates) { |
Steve Anton | ae226f6 | 2019-01-29 20:47:38 | [diff] [blame] | 446 | EXPECT_THAT(ses_candidates, Contains(candidate)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 447 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 448 | } |
| 449 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 450 | void OnCandidatesRemoved(PortAllocatorSession* session, |
| 451 | const std::vector<Candidate>& removed_candidates) { |
| 452 | auto new_end = std::remove_if( |
| 453 | candidates_.begin(), candidates_.end(), |
| 454 | [removed_candidates](Candidate& candidate) { |
| 455 | for (const Candidate& removed_candidate : removed_candidates) { |
| 456 | if (candidate.MatchesForRemoval(removed_candidate)) { |
| 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | return false; |
| 461 | }); |
| 462 | candidates_.erase(new_end, candidates_.end()); |
| 463 | } |
| 464 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 465 | bool HasRelayAddress(const ProtocolAddress& proto_addr) { |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 466 | for (size_t i = 0; i < allocator_->turn_servers().size(); ++i) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 467 | RelayServerConfig server_config = allocator_->turn_servers()[i]; |
| 468 | PortList::const_iterator relay_port; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 469 | for (relay_port = server_config.ports.begin(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 470 | relay_port != server_config.ports.end(); ++relay_port) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 471 | if (proto_addr.address == relay_port->address && |
| 472 | proto_addr.proto == relay_port->proto) |
| 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 479 | void ResetWithStunServer(const rtc::SocketAddress& stun_server, |
| 480 | bool with_nat) { |
| 481 | if (with_nat) { |
| 482 | nat_server_.reset(new rtc::NATServer( |
Per K | a8cd2ba | 2023-12-13 07:10:06 | [diff] [blame] | 483 | rtc::NAT_OPEN_CONE, thread_, vss_.get(), kNatUdpAddr, kNatTcpAddr, |
| 484 | thread_, vss_.get(), rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 485 | } else { |
Niels Möller | 66be895 | 2021-09-03 08:55:12 | [diff] [blame] | 486 | nat_socket_factory_ = |
| 487 | std::make_unique<rtc::BasicPacketSocketFactory>(fss_.get()); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | ServerAddresses stun_servers; |
| 491 | if (!stun_server.IsNil()) { |
| 492 | stun_servers.insert(stun_server); |
| 493 | } |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 494 | allocator_.reset(new BasicPortAllocator(&network_manager_, |
| 495 | nat_socket_factory_.get(), |
| 496 | stun_servers, &field_trials_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 497 | allocator_->Initialize(); |
| 498 | allocator_->set_step_delay(kMinimumStepDelay); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 499 | } |
| 500 | |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 501 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 502 | std::unique_ptr<rtc::FirewallSocketServer> fss_; |
Tommi | fe57cec | 2023-08-30 08:07:06 | [diff] [blame] | 503 | rtc::BasicPacketSocketFactory socket_factory_; |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 504 | rtc::AutoSocketServerThread thread_; |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 505 | std::unique_ptr<rtc::NATServer> nat_server_; |
| 506 | rtc::NATSocketFactory nat_factory_; |
| 507 | std::unique_ptr<rtc::BasicPacketSocketFactory> nat_socket_factory_; |
Per K | 14630a7 | 2023-11-22 13:08:58 | [diff] [blame] | 508 | TestStunServer::StunServerPtr stun_server_; |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 509 | TestTurnServer turn_server_; |
| 510 | rtc::FakeNetworkManager network_manager_; |
| 511 | std::unique_ptr<BasicPortAllocator> allocator_; |
| 512 | std::unique_ptr<PortAllocatorSession> session_; |
| 513 | std::vector<PortInterface*> ports_; |
| 514 | std::vector<Candidate> candidates_; |
| 515 | bool candidate_allocation_done_; |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 516 | webrtc::test::ScopedKeyValueConfig field_trials_; |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 517 | }; |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 518 | |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 519 | class BasicPortAllocatorTestWithRealClock : public BasicPortAllocatorTestBase { |
| 520 | }; |
| 521 | |
| 522 | class FakeClockBase { |
| 523 | public: |
| 524 | rtc::ScopedFakeClock fake_clock; |
| 525 | }; |
| 526 | |
| 527 | class BasicPortAllocatorTest : public FakeClockBase, |
| 528 | public BasicPortAllocatorTestBase { |
| 529 | public: |
| 530 | // This function starts the port/address gathering and check the existence of |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 531 | // candidates as specified. When `expect_stun_candidate` is true, |
| 532 | // `stun_candidate_addr` carries the expected reflective address, which is |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 533 | // also the related address for TURN candidate if it is expected. Otherwise, |
| 534 | // it should be ignore. |
| 535 | void CheckDisableAdapterEnumeration( |
| 536 | uint32_t total_ports, |
| 537 | const rtc::IPAddress& host_candidate_addr, |
| 538 | const rtc::IPAddress& stun_candidate_addr, |
| 539 | const rtc::IPAddress& relay_candidate_udp_transport_addr, |
| 540 | const rtc::IPAddress& relay_candidate_tcp_transport_addr) { |
| 541 | network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(), |
| 542 | rtc::IPAddress()); |
| 543 | if (!session_) { |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 544 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 545 | } |
| 546 | session_->set_flags(session_->flags() | |
| 547 | PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION | |
| 548 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 549 | allocator().set_allow_tcp_listen(false); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 550 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 551 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 552 | kDefaultAllocationTimeout, fake_clock); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 553 | |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 554 | uint32_t total_candidates = 0; |
| 555 | if (!host_candidate_addr.IsNil()) { |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 556 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", |
| 557 | rtc::SocketAddress(kPrivateAddr.ipaddr(), 0))); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 558 | ++total_candidates; |
| 559 | } |
| 560 | if (!stun_candidate_addr.IsNil()) { |
| 561 | rtc::SocketAddress related_address(host_candidate_addr, 0); |
| 562 | if (host_candidate_addr.IsNil()) { |
| 563 | related_address.SetIP(rtc::GetAnyIP(stun_candidate_addr.family())); |
| 564 | } |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 565 | EXPECT_TRUE(HasCandidateWithRelatedAddr( |
| 566 | candidates_, "stun", "udp", |
| 567 | rtc::SocketAddress(stun_candidate_addr, 0), related_address)); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 568 | ++total_candidates; |
| 569 | } |
| 570 | if (!relay_candidate_udp_transport_addr.IsNil()) { |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 571 | EXPECT_TRUE(HasCandidateWithRelatedAddr( |
| 572 | candidates_, "relay", "udp", |
| 573 | rtc::SocketAddress(relay_candidate_udp_transport_addr, 0), |
| 574 | rtc::SocketAddress(stun_candidate_addr, 0))); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 575 | ++total_candidates; |
| 576 | } |
| 577 | if (!relay_candidate_tcp_transport_addr.IsNil()) { |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 578 | EXPECT_TRUE(HasCandidateWithRelatedAddr( |
| 579 | candidates_, "relay", "udp", |
| 580 | rtc::SocketAddress(relay_candidate_tcp_transport_addr, 0), |
| 581 | rtc::SocketAddress(stun_candidate_addr, 0))); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 582 | ++total_candidates; |
| 583 | } |
| 584 | |
| 585 | EXPECT_EQ(total_candidates, candidates_.size()); |
| 586 | EXPECT_EQ(total_ports, ports_.size()); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void TestIPv6TurnPortPrunesIPv4TurnPort() { |
| 590 | turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP); |
| 591 | // Add two IP addresses on the same interface. |
| 592 | AddInterface(kClientAddr, "net1"); |
| 593 | AddInterface(kClientIPv6Addr, "net1"); |
Tommi | 3756e29 | 2023-09-01 10:45:06 | [diff] [blame] | 594 | allocator_.reset( |
| 595 | new BasicPortAllocator(&network_manager_, &socket_factory_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 596 | allocator_->Initialize(); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 597 | allocator_->SetConfiguration(allocator_->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 598 | allocator_->turn_servers(), 0, |
| 599 | webrtc::PRUNE_BASED_ON_PRIORITY); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 600 | AddTurnServers(kTurnUdpIntIPv6Addr, rtc::SocketAddress()); |
| 601 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 602 | |
| 603 | allocator_->set_step_delay(kMinimumStepDelay); |
| 604 | allocator_->set_flags( |
| 605 | allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 606 | PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP); |
| 607 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 608 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 609 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 610 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 611 | kDefaultAllocationTimeout, fake_clock); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 612 | // Three ports (one IPv4 STUN, one IPv6 STUN and one TURN) will be ready. |
| 613 | EXPECT_EQ(3U, session_->ReadyPorts().size()); |
| 614 | EXPECT_EQ(3U, ports_.size()); |
| 615 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr)); |
| 616 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr)); |
| 617 | EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr)); |
| 618 | EXPECT_EQ(0, CountPorts(ports_, "relay", PROTO_UDP, kClientAddr)); |
| 619 | |
| 620 | // Now that we remove candidates when a TURN port is pruned, there will be |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 621 | // exactly 3 candidates in both `candidates_` and `ready_candidates`. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 622 | EXPECT_EQ(3U, candidates_.size()); |
| 623 | const std::vector<Candidate>& ready_candidates = |
| 624 | session_->ReadyCandidates(); |
| 625 | EXPECT_EQ(3U, ready_candidates.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 626 | EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr)); |
| 627 | EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp", |
| 628 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 629 | } |
| 630 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 631 | void TestTurnPortPrunesWithUdpAndTcpPorts( |
| 632 | webrtc::PortPrunePolicy prune_policy, |
| 633 | bool tcp_pruned) { |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 634 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 635 | AddInterface(kClientAddr); |
Tommi | 3756e29 | 2023-09-01 10:45:06 | [diff] [blame] | 636 | allocator_.reset( |
| 637 | new BasicPortAllocator(&network_manager_, &socket_factory_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 638 | allocator_->Initialize(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 639 | allocator_->SetConfiguration(allocator_->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 640 | allocator_->turn_servers(), 0, prune_policy); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 641 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 642 | allocator_->set_step_delay(kMinimumStepDelay); |
| 643 | allocator_->set_flags(allocator().flags() | |
| 644 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 645 | PORTALLOCATOR_DISABLE_TCP); |
| 646 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 647 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 648 | session_->StartGettingPorts(); |
| 649 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 650 | kDefaultAllocationTimeout, fake_clock); |
| 651 | // Only 2 ports (one STUN and one TURN) are actually being used. |
| 652 | EXPECT_EQ(2U, session_->ReadyPorts().size()); |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 653 | // We have verified that each port, when it is added to `ports_`, it is |
| 654 | // found in `ready_ports`, and when it is pruned, it is not found in |
| 655 | // `ready_ports`, so we only need to verify the content in one of them. |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 656 | EXPECT_EQ(2U, ports_.size()); |
| 657 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr)); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 658 | int num_udp_ports = tcp_pruned ? 1 : 0; |
| 659 | EXPECT_EQ(num_udp_ports, |
| 660 | CountPorts(ports_, "relay", PROTO_UDP, kClientAddr)); |
| 661 | EXPECT_EQ(1 - num_udp_ports, |
| 662 | CountPorts(ports_, "relay", PROTO_TCP, kClientAddr)); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 663 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 664 | // Now that we remove candidates when a TURN port is pruned, `candidates_` |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 665 | // should only contains two candidates regardless whether the TCP TURN port |
| 666 | // is created before or after the UDP turn port. |
| 667 | EXPECT_EQ(2U, candidates_.size()); |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 668 | // There will only be 2 candidates in `ready_candidates` because it only |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 669 | // includes the candidates in the ready ports. |
| 670 | const std::vector<Candidate>& ready_candidates = |
| 671 | session_->ReadyCandidates(); |
| 672 | EXPECT_EQ(2U, ready_candidates.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 673 | EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr)); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 674 | |
| 675 | // The external candidate is always udp. |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 676 | EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp", |
| 677 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 678 | } |
| 679 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 680 | void TestEachInterfaceHasItsOwnTurnPorts() { |
| 681 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 682 | turn_server_.AddInternalSocket(kTurnUdpIntIPv6Addr, PROTO_UDP); |
| 683 | turn_server_.AddInternalSocket(kTurnTcpIntIPv6Addr, PROTO_TCP); |
| 684 | // Add two interfaces both having IPv4 and IPv6 addresses. |
| 685 | AddInterface(kClientAddr, "net1", rtc::ADAPTER_TYPE_WIFI); |
| 686 | AddInterface(kClientIPv6Addr, "net1", rtc::ADAPTER_TYPE_WIFI); |
| 687 | AddInterface(kClientAddr2, "net2", rtc::ADAPTER_TYPE_CELLULAR); |
| 688 | AddInterface(kClientIPv6Addr2, "net2", rtc::ADAPTER_TYPE_CELLULAR); |
Tommi | 3756e29 | 2023-09-01 10:45:06 | [diff] [blame] | 689 | allocator_.reset( |
| 690 | new BasicPortAllocator(&network_manager_, &socket_factory_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 691 | allocator_->Initialize(); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 692 | allocator_->SetConfiguration(allocator_->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 693 | allocator_->turn_servers(), 0, |
| 694 | webrtc::PRUNE_BASED_ON_PRIORITY); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 695 | // Have both UDP/TCP and IPv4/IPv6 TURN ports. |
| 696 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 697 | AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr); |
| 698 | |
| 699 | allocator_->set_step_delay(kMinimumStepDelay); |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 700 | allocator_->set_flags( |
| 701 | allocator().flags() | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 702 | PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 703 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 704 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 705 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 706 | kDefaultAllocationTimeout, fake_clock); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 707 | // 10 ports (4 STUN and 1 TURN ports on each interface) will be ready to |
| 708 | // use. |
| 709 | EXPECT_EQ(10U, session_->ReadyPorts().size()); |
| 710 | EXPECT_EQ(10U, ports_.size()); |
| 711 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr)); |
| 712 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientAddr2)); |
| 713 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr)); |
| 714 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_UDP, kClientIPv6Addr2)); |
| 715 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr)); |
| 716 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientAddr2)); |
| 717 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr)); |
| 718 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kClientIPv6Addr2)); |
| 719 | EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr)); |
| 720 | EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kClientIPv6Addr2)); |
| 721 | |
| 722 | // Now that we remove candidates when TURN ports are pruned, there will be |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 723 | // exactly 10 candidates in `candidates_`. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 724 | EXPECT_EQ(10U, candidates_.size()); |
| 725 | const std::vector<Candidate>& ready_candidates = |
| 726 | session_->ReadyCandidates(); |
| 727 | EXPECT_EQ(10U, ready_candidates.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 728 | EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr)); |
| 729 | EXPECT_TRUE(HasCandidate(ready_candidates, "local", "udp", kClientAddr2)); |
| 730 | EXPECT_TRUE( |
| 731 | HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr)); |
| 732 | EXPECT_TRUE( |
| 733 | HasCandidate(ready_candidates, "local", "udp", kClientIPv6Addr2)); |
| 734 | EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr)); |
| 735 | EXPECT_TRUE(HasCandidate(ready_candidates, "local", "tcp", kClientAddr2)); |
| 736 | EXPECT_TRUE( |
| 737 | HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr)); |
| 738 | EXPECT_TRUE( |
| 739 | HasCandidate(ready_candidates, "local", "tcp", kClientIPv6Addr2)); |
| 740 | EXPECT_TRUE(HasCandidate(ready_candidates, "relay", "udp", |
| 741 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 742 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | // Tests that we can init the port allocator and create a session. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 746 | TEST_F(BasicPortAllocatorTest, TestBasic) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 747 | EXPECT_EQ(&network_manager_, allocator().network_manager()); |
| 748 | EXPECT_EQ(kStunAddr, *allocator().stun_servers().begin()); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 749 | ASSERT_EQ(0u, allocator().turn_servers().size()); |
| 750 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 751 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 752 | EXPECT_FALSE(session_->CandidatesAllocationDone()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 753 | } |
| 754 | |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 755 | // Tests that our network filtering works properly. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 756 | TEST_F(BasicPortAllocatorTest, TestIgnoreOnlyLoopbackNetworkByDefault) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 757 | AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0", |
| 758 | rtc::ADAPTER_TYPE_ETHERNET); |
| 759 | AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0", |
| 760 | rtc::ADAPTER_TYPE_WIFI); |
| 761 | AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0", |
| 762 | rtc::ADAPTER_TYPE_CELLULAR); |
| 763 | AddInterface(SocketAddress(IPAddress(0x12345603U), 0), "test_vpn0", |
| 764 | rtc::ADAPTER_TYPE_VPN); |
| 765 | AddInterface(SocketAddress(IPAddress(0x12345604U), 0), "test_lo", |
| 766 | rtc::ADAPTER_TYPE_LOOPBACK); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 767 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 768 | session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY | |
| 769 | PORTALLOCATOR_DISABLE_TCP); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 770 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 771 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 772 | kDefaultAllocationTimeout, fake_clock); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 773 | EXPECT_EQ(4U, candidates_.size()); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 774 | for (const Candidate& candidate : candidates_) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 775 | EXPECT_LT(candidate.address().ip(), 0x12345604U); |
| 776 | } |
| 777 | } |
| 778 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 779 | TEST_F(BasicPortAllocatorTest, TestIgnoreNetworksAccordingToIgnoreMask) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 780 | AddInterface(SocketAddress(IPAddress(0x12345600U), 0), "test_eth0", |
| 781 | rtc::ADAPTER_TYPE_ETHERNET); |
| 782 | AddInterface(SocketAddress(IPAddress(0x12345601U), 0), "test_wlan0", |
| 783 | rtc::ADAPTER_TYPE_WIFI); |
| 784 | AddInterface(SocketAddress(IPAddress(0x12345602U), 0), "test_cell0", |
| 785 | rtc::ADAPTER_TYPE_CELLULAR); |
| 786 | allocator_->SetNetworkIgnoreMask(rtc::ADAPTER_TYPE_ETHERNET | |
| 787 | rtc::ADAPTER_TYPE_LOOPBACK | |
| 788 | rtc::ADAPTER_TYPE_WIFI); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 789 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 790 | session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY | |
| 791 | PORTALLOCATOR_DISABLE_TCP); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 792 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 793 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 794 | kDefaultAllocationTimeout, fake_clock); |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 795 | EXPECT_EQ(1U, candidates_.size()); |
| 796 | EXPECT_EQ(0x12345602U, candidates_[0].address().ip()); |
| 797 | } |
| 798 | |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 799 | // Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and |
| 800 | // both Wi-Fi and cell interfaces are available, only Wi-Fi is used. |
| 801 | TEST_F(BasicPortAllocatorTest, |
| 802 | WifiUsedInsteadOfCellWhenCostlyNetworksDisabled) { |
| 803 | SocketAddress wifi(IPAddress(0x12345600U), 0); |
| 804 | SocketAddress cell(IPAddress(0x12345601U), 0); |
| 805 | AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI); |
| 806 | AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 807 | // Disable all but UDP candidates to make the test simpler. |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 808 | allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | |
| 809 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 810 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 811 | cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 812 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 813 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 814 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 815 | kDefaultAllocationTimeout, fake_clock); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 816 | // Should only get one Wi-Fi candidate. |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 817 | EXPECT_EQ(1U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 818 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 819 | } |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 820 | |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 821 | // Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and |
| 822 | // both "unknown" and cell interfaces are available, only the unknown are used. |
| 823 | // The unknown interface may be something that ultimately uses Wi-Fi, so we do |
| 824 | // this to be on the safe side. |
| 825 | TEST_F(BasicPortAllocatorTest, |
| 826 | UnknownInterfaceUsedInsteadOfCellWhenCostlyNetworksDisabled) { |
| 827 | SocketAddress cell(IPAddress(0x12345601U), 0); |
| 828 | SocketAddress unknown1(IPAddress(0x12345602U), 0); |
| 829 | SocketAddress unknown2(IPAddress(0x12345603U), 0); |
| 830 | AddInterface(cell, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 831 | AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN); |
| 832 | AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN); |
| 833 | // Disable all but UDP candidates to make the test simpler. |
| 834 | allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | |
| 835 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 836 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 837 | cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 838 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 839 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 840 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 841 | kDefaultAllocationTimeout, fake_clock); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 842 | // Should only get two candidates, none of which is cell. |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 843 | EXPECT_EQ(2U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 844 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown1)); |
| 845 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", unknown2)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 846 | } |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 847 | |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 848 | // Test that when the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set and |
| 849 | // there are a mix of Wi-Fi, "unknown" and cell interfaces, only the Wi-Fi |
| 850 | // interface is used. |
| 851 | TEST_F(BasicPortAllocatorTest, |
| 852 | WifiUsedInsteadOfUnknownOrCellWhenCostlyNetworksDisabled) { |
| 853 | SocketAddress wifi(IPAddress(0x12345600U), 0); |
| 854 | SocketAddress cellular(IPAddress(0x12345601U), 0); |
| 855 | SocketAddress unknown1(IPAddress(0x12345602U), 0); |
| 856 | SocketAddress unknown2(IPAddress(0x12345603U), 0); |
| 857 | AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI); |
| 858 | AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 859 | AddInterface(unknown1, "test_unknown0", rtc::ADAPTER_TYPE_UNKNOWN); |
| 860 | AddInterface(unknown2, "test_unknown1", rtc::ADAPTER_TYPE_UNKNOWN); |
| 861 | // Disable all but UDP candidates to make the test simpler. |
| 862 | allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | |
| 863 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 864 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 865 | cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 866 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 867 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 868 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 869 | kDefaultAllocationTimeout, fake_clock); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 870 | // Should only get one Wi-Fi candidate. |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 871 | EXPECT_EQ(1U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 872 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | // Test that if the PORTALLOCATOR_DISABLE_COSTLY_NETWORKS flag is set, but the |
| 876 | // only interface available is cellular, it ends up used anyway. A costly |
| 877 | // connection is always better than no connection. |
| 878 | TEST_F(BasicPortAllocatorTest, |
| 879 | CellUsedWhenCostlyNetworksDisabledButThereAreNoOtherInterfaces) { |
| 880 | SocketAddress cellular(IPAddress(0x12345601U), 0); |
| 881 | AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 882 | // Disable all but UDP candidates to make the test simpler. |
| 883 | allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | |
| 884 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 885 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 886 | cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 887 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 888 | session_->StartGettingPorts(); |
| 889 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 890 | kDefaultAllocationTimeout, fake_clock); |
| 891 | // Make sure we got the cell candidate. |
| 892 | EXPECT_EQ(1U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 893 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular)); |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 894 | } |
| 895 | |
Yuwei Huang | b181f71 | 2018-01-23 01:01:28 | [diff] [blame] | 896 | // Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is |
| 897 | // a WiFi network with link-local IP address and a cellular network, then the |
| 898 | // cellular candidate will still be gathered. |
| 899 | TEST_F(BasicPortAllocatorTest, |
| 900 | CellNotRemovedWhenCostlyNetworksDisabledAndWifiIsLinkLocal) { |
| 901 | SocketAddress wifi_link_local("169.254.0.1", 0); |
| 902 | SocketAddress cellular(IPAddress(0x12345601U), 0); |
| 903 | AddInterface(wifi_link_local, "test_wlan0", rtc::ADAPTER_TYPE_WIFI); |
| 904 | AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 905 | |
| 906 | allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | |
| 907 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 908 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 909 | cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 910 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
Yuwei Huang | b181f71 | 2018-01-23 01:01:28 | [diff] [blame] | 911 | session_->StartGettingPorts(); |
| 912 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 913 | kDefaultAllocationTimeout, fake_clock); |
| 914 | // Make sure we got both wifi and cell candidates. |
| 915 | EXPECT_EQ(2U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 916 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local)); |
| 917 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", cellular)); |
Yuwei Huang | b181f71 | 2018-01-23 01:01:28 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | // Test that if both PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is set, and there is |
| 921 | // a WiFi network with link-local IP address, a WiFi network with a normal IP |
| 922 | // address and a cellular network, then the cellular candidate will not be |
| 923 | // gathered. |
| 924 | TEST_F(BasicPortAllocatorTest, |
| 925 | CellRemovedWhenCostlyNetworksDisabledAndBothWifisPresent) { |
| 926 | SocketAddress wifi(IPAddress(0x12345600U), 0); |
| 927 | SocketAddress wifi_link_local("169.254.0.1", 0); |
| 928 | SocketAddress cellular(IPAddress(0x12345601U), 0); |
| 929 | AddInterface(wifi, "test_wlan0", rtc::ADAPTER_TYPE_WIFI); |
| 930 | AddInterface(wifi_link_local, "test_wlan1", rtc::ADAPTER_TYPE_WIFI); |
| 931 | AddInterface(cellular, "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 932 | |
| 933 | allocator().set_flags(cricket::PORTALLOCATOR_DISABLE_STUN | |
| 934 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 935 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 936 | cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 937 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
Yuwei Huang | b181f71 | 2018-01-23 01:01:28 | [diff] [blame] | 938 | session_->StartGettingPorts(); |
| 939 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 940 | kDefaultAllocationTimeout, fake_clock); |
| 941 | // Make sure we got only wifi candidates. |
| 942 | EXPECT_EQ(2U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 943 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi)); |
| 944 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", wifi_link_local)); |
Yuwei Huang | b181f71 | 2018-01-23 01:01:28 | [diff] [blame] | 945 | } |
| 946 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 947 | // Test that the adapter types of the Ethernet and the VPN can be correctly |
| 948 | // identified so that the Ethernet has a lower network cost than the VPN, and |
| 949 | // the Ethernet is not filtered out if PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is |
| 950 | // set. |
| 951 | TEST_F(BasicPortAllocatorTest, |
| 952 | EthernetIsNotFilteredOutWhenCostlyNetworksDisabledAndVpnPresent) { |
| 953 | AddInterface(kClientAddr, "eth0", rtc::ADAPTER_TYPE_ETHERNET); |
| 954 | AddInterface(kClientAddr2, "tap0", rtc::ADAPTER_TYPE_VPN); |
| 955 | allocator().set_flags(PORTALLOCATOR_DISABLE_COSTLY_NETWORKS | |
| 956 | PORTALLOCATOR_DISABLE_RELAY | |
| 957 | PORTALLOCATOR_DISABLE_TCP); |
| 958 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 959 | session_->StartGettingPorts(); |
| 960 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 961 | kDefaultAllocationTimeout, fake_clock); |
| 962 | // The VPN tap0 network should be filtered out as a costly network, and we |
| 963 | // should have a UDP port and a STUN port from the Ethernet eth0. |
| 964 | ASSERT_EQ(2U, ports_.size()); |
| 965 | EXPECT_EQ(ports_[0]->Network()->name(), "eth0"); |
| 966 | EXPECT_EQ(ports_[1]->Network()->name(), "eth0"); |
| 967 | } |
| 968 | |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 969 | // Test that no more than allocator.max_ipv6_networks() IPv6 networks are used |
| 970 | // to gather candidates. |
| 971 | TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitEnforced) { |
| 972 | // Add three IPv6 network interfaces, but tell the allocator to only use two. |
| 973 | allocator().set_max_ipv6_networks(2); |
| 974 | AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET); |
| 975 | AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET); |
| 976 | AddInterface(kClientIPv6Addr3, "eth2", rtc::ADAPTER_TYPE_ETHERNET); |
| 977 | |
| 978 | // To simplify the test, only gather UDP host candidates. |
| 979 | allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP | |
| 980 | PORTALLOCATOR_DISABLE_STUN | |
| 981 | PORTALLOCATOR_DISABLE_RELAY); |
| 982 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 983 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 984 | session_->StartGettingPorts(); |
| 985 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 986 | kDefaultAllocationTimeout, fake_clock); |
| 987 | EXPECT_EQ(2U, candidates_.size()); |
| 988 | // Ensure the expected two interfaces (eth0 and eth1) were used. |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 989 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr)); |
| 990 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2)); |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // Ensure that allocator.max_ipv6_networks() doesn't prevent IPv4 networks from |
| 994 | // being used. |
| 995 | TEST_F(BasicPortAllocatorTest, MaxIpv6NetworksLimitDoesNotImpactIpv4Networks) { |
| 996 | // Set the "max IPv6" limit to 1, adding two IPv6 and two IPv4 networks. |
| 997 | allocator().set_max_ipv6_networks(1); |
| 998 | AddInterface(kClientIPv6Addr, "eth0", rtc::ADAPTER_TYPE_ETHERNET); |
| 999 | AddInterface(kClientIPv6Addr2, "eth1", rtc::ADAPTER_TYPE_ETHERNET); |
| 1000 | AddInterface(kClientAddr, "eth2", rtc::ADAPTER_TYPE_ETHERNET); |
| 1001 | AddInterface(kClientAddr2, "eth3", rtc::ADAPTER_TYPE_ETHERNET); |
| 1002 | |
| 1003 | // To simplify the test, only gather UDP host candidates. |
| 1004 | allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP | |
| 1005 | PORTALLOCATOR_DISABLE_STUN | |
| 1006 | PORTALLOCATOR_DISABLE_RELAY); |
| 1007 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1008 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 1009 | session_->StartGettingPorts(); |
| 1010 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1011 | kDefaultAllocationTimeout, fake_clock); |
| 1012 | EXPECT_EQ(3U, candidates_.size()); |
| 1013 | // Ensure that only one IPv6 interface was used, but both IPv4 interfaces |
| 1014 | // were used. |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1015 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr)); |
| 1016 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1017 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr2)); |
deadbeef | 3427f53 | 2017-07-26 23:09:33 | [diff] [blame] | 1018 | } |
| 1019 | |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 1020 | // Test that we could use loopback interface as host candidate. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1021 | TEST_F(BasicPortAllocatorTest, TestLoopbackNetworkInterface) { |
Taylor Brandstetter | 0c7e9f5 | 2015-12-29 22:14:52 | [diff] [blame] | 1022 | AddInterface(kLoopbackAddr, "test_loopback", rtc::ADAPTER_TYPE_LOOPBACK); |
| 1023 | allocator_->SetNetworkIgnoreMask(0); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1024 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1025 | session_->set_flags(PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY | |
| 1026 | PORTALLOCATOR_DISABLE_TCP); |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 1027 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1028 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1029 | kDefaultAllocationTimeout, fake_clock); |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 1030 | EXPECT_EQ(1U, candidates_.size()); |
| 1031 | } |
| 1032 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1033 | // Tests that we can get all the desired addresses successfully. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1034 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithMinimumStepDelay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1035 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1036 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1037 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1038 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1039 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1040 | EXPECT_EQ(3U, candidates_.size()); |
| 1041 | EXPECT_EQ(3U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1042 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1043 | EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr)); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1044 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1045 | } |
| 1046 | |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1047 | // Test that when the same network interface is brought down and up, the |
| 1048 | // port allocator session will restart a new allocation sequence if |
| 1049 | // it is not stopped. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1050 | TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionNotStopped) { |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1051 | std::string if_name("test_net0"); |
| 1052 | AddInterface(kClientAddr, if_name); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1053 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1054 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1055 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1056 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1057 | EXPECT_EQ(3U, candidates_.size()); |
| 1058 | EXPECT_EQ(3U, ports_.size()); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1059 | candidate_allocation_done_ = false; |
| 1060 | candidates_.clear(); |
| 1061 | ports_.clear(); |
| 1062 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1063 | // Disable socket creation to simulate the network interface being down. When |
| 1064 | // no network interfaces are available, BasicPortAllocator will fall back to |
| 1065 | // binding to the "ANY" address, so we need to make sure that fails too. |
| 1066 | fss_->set_tcp_sockets_enabled(false); |
| 1067 | fss_->set_udp_sockets_enabled(false); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1068 | RemoveInterface(kClientAddr); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1069 | SIMULATED_WAIT(false, 1000, fake_clock); |
| 1070 | EXPECT_EQ(0U, candidates_.size()); |
| 1071 | ports_.clear(); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1072 | candidate_allocation_done_ = false; |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1073 | |
| 1074 | // When the same interfaces are added again, new candidates/ports should be |
| 1075 | // generated. |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1076 | fss_->set_tcp_sockets_enabled(true); |
| 1077 | fss_->set_udp_sockets_enabled(true); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1078 | AddInterface(kClientAddr, if_name); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1079 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1080 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1081 | EXPECT_EQ(3U, candidates_.size()); |
| 1082 | EXPECT_EQ(3U, ports_.size()); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Test that when the same network interface is brought down and up, the |
| 1086 | // port allocator session will not restart a new allocation sequence if |
| 1087 | // it is stopped. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1088 | TEST_F(BasicPortAllocatorTest, TestSameNetworkDownAndUpWhenSessionStopped) { |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1089 | std::string if_name("test_net0"); |
| 1090 | AddInterface(kClientAddr, if_name); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1091 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1092 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1093 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1094 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1095 | EXPECT_EQ(3U, candidates_.size()); |
| 1096 | EXPECT_EQ(3U, ports_.size()); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1097 | session_->StopGettingPorts(); |
| 1098 | candidates_.clear(); |
| 1099 | ports_.clear(); |
| 1100 | |
| 1101 | RemoveInterface(kClientAddr); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1102 | // Wait one (simulated) second and then verify no new candidates have |
| 1103 | // appeared. |
| 1104 | SIMULATED_WAIT(false, 1000, fake_clock); |
| 1105 | EXPECT_EQ(0U, candidates_.size()); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1106 | EXPECT_EQ(0U, ports_.size()); |
| 1107 | |
| 1108 | // When the same interfaces are added again, new candidates/ports should not |
| 1109 | // be generated because the session has stopped. |
| 1110 | AddInterface(kClientAddr, if_name); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1111 | SIMULATED_WAIT(false, 1000, fake_clock); |
| 1112 | EXPECT_EQ(0U, candidates_.size()); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1113 | EXPECT_EQ(0U, ports_.size()); |
honghaiz | 8c404fa | 2015-09-28 14:59:43 | [diff] [blame] | 1114 | } |
| 1115 | |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 1116 | // Similar to the above tests, but tests a situation when sockets can't be |
| 1117 | // bound to a network interface, then after a network change event can be. |
| 1118 | // Related bug: https://bugs.chromium.org/p/webrtc/issues/detail?id=8256 |
| 1119 | TEST_F(BasicPortAllocatorTest, CandidatesRegatheredAfterBindingFails) { |
| 1120 | // Only test local ports to simplify test. |
| 1121 | ResetWithNoServersOrNat(); |
| 1122 | // Provide a situation where the interface appears to be available, but |
| 1123 | // binding the sockets fails. See bug for description of when this can |
| 1124 | // happen. |
| 1125 | std::string if_name("test_net0"); |
| 1126 | AddInterface(kClientAddr, if_name); |
| 1127 | fss_->set_tcp_sockets_enabled(false); |
| 1128 | fss_->set_udp_sockets_enabled(false); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1129 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 1130 | session_->StartGettingPorts(); |
| 1131 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1132 | kDefaultAllocationTimeout, fake_clock); |
| 1133 | // Make sure we actually prevented candidates from being gathered (other than |
| 1134 | // a single TCP active candidate, since that doesn't require creating a |
| 1135 | // socket). |
| 1136 | ASSERT_EQ(1U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1137 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 1138 | candidate_allocation_done_ = false; |
| 1139 | |
| 1140 | // Now simulate the interface coming up, with the newfound ability to bind |
| 1141 | // sockets. |
| 1142 | fss_->set_tcp_sockets_enabled(true); |
| 1143 | fss_->set_udp_sockets_enabled(true); |
| 1144 | AddInterface(kClientAddr, if_name); |
| 1145 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1146 | kDefaultAllocationTimeout, fake_clock); |
| 1147 | // Should get UDP and TCP candidate. |
| 1148 | ASSERT_EQ(2U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1149 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 1150 | // TODO(deadbeef): This is actually the same active TCP candidate as before. |
| 1151 | // We should extend this test to also verify that a server candidate is |
| 1152 | // gathered. |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1153 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
deadbeef | 1c46a35 | 2017-09-27 18:24:05 | [diff] [blame] | 1154 | } |
| 1155 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1156 | // Verify candidates with default step delay of 1sec. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1157 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsWithOneSecondStepDelay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1158 | AddInterface(kClientAddr); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1159 | allocator_->set_step_delay(kDefaultStepDelay); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1160 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1161 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1162 | ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1163 | EXPECT_EQ(2U, ports_.size()); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1164 | ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 2000, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1165 | EXPECT_EQ(3U, ports_.size()); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1166 | |
| 1167 | ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), 1500, fake_clock); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1168 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1169 | EXPECT_EQ(3U, ports_.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1170 | EXPECT_TRUE(candidate_allocation_done_); |
| 1171 | // If we Stop gathering now, we shouldn't get a second "done" callback. |
| 1172 | session_->StopGettingPorts(); |
| 1173 | } |
| 1174 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1175 | TEST_F(BasicPortAllocatorTest, TestSetupVideoRtpPortsWithNormalSendBuffers) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1176 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1177 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP, CN_VIDEO)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1178 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1179 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1180 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1181 | EXPECT_EQ(3U, candidates_.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1182 | // If we Stop gathering now, we shouldn't get a second "done" callback. |
| 1183 | session_->StopGettingPorts(); |
| 1184 | |
| 1185 | // All ports should have unset send-buffer sizes. |
| 1186 | CheckSendBufferSizesOfAllPorts(-1); |
| 1187 | } |
| 1188 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1189 | // Tests that we can get callback after StopGetAllPorts when called in the |
| 1190 | // middle of gathering. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1191 | TEST_F(BasicPortAllocatorTest, TestStopGetAllPorts) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1192 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1193 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1194 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1195 | ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout, |
| 1196 | fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1197 | EXPECT_EQ(2U, ports_.size()); |
| 1198 | session_->StopGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1199 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1200 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | // Test that we restrict client ports appropriately when a port range is set. |
| 1204 | // We check the candidates for udp/stun/tcp ports, and the from address |
| 1205 | // for relay ports. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1206 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsPortRange) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1207 | AddInterface(kClientAddr); |
| 1208 | // Check that an invalid port range fails. |
| 1209 | EXPECT_FALSE(SetPortRange(kMaxPort, kMinPort)); |
| 1210 | // Check that a null port range succeeds. |
| 1211 | EXPECT_TRUE(SetPortRange(0, 0)); |
| 1212 | // Check that a valid port range succeeds. |
| 1213 | EXPECT_TRUE(SetPortRange(kMinPort, kMaxPort)); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1214 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1215 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1216 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1217 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1218 | EXPECT_EQ(3U, candidates_.size()); |
| 1219 | EXPECT_EQ(3U, ports_.size()); |
honghaiz | a73df55 | 2016-10-27 18:44:17 | [diff] [blame] | 1220 | |
| 1221 | int num_nonrelay_candidates = 0; |
| 1222 | for (const Candidate& candidate : candidates_) { |
| 1223 | // Check the port number for the UDP/STUN/TCP port objects. |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 1224 | if (!candidate.is_relay()) { |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1225 | EXPECT_TRUE(CheckPort(candidate.address(), kMinPort, kMaxPort)); |
honghaiz | a73df55 | 2016-10-27 18:44:17 | [diff] [blame] | 1226 | ++num_nonrelay_candidates; |
| 1227 | } |
| 1228 | } |
| 1229 | EXPECT_EQ(3, num_nonrelay_candidates); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1230 | } |
| 1231 | |
deadbeef | e97389c | 2016-12-23 09:43:45 | [diff] [blame] | 1232 | // Test that if we have no network adapters, we bind to the ANY address and |
| 1233 | // still get non-host candidates. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1234 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoAdapters) { |
deadbeef | e97389c | 2016-12-23 09:43:45 | [diff] [blame] | 1235 | // Default config uses GTURN and no NAT, so replace that with the |
| 1236 | // desired setup (NAT, STUN server, TURN server, UDP/TCP). |
| 1237 | ResetWithStunServerAndNat(kStunAddr); |
| 1238 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 1239 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 1240 | AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1241 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1242 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1243 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1244 | kDefaultAllocationTimeout, fake_clock); |
deadbeef | e97389c | 2016-12-23 09:43:45 | [diff] [blame] | 1245 | EXPECT_EQ(4U, ports_.size()); |
| 1246 | EXPECT_EQ(1, CountPorts(ports_, "stun", PROTO_UDP, kAnyAddr)); |
| 1247 | EXPECT_EQ(1, CountPorts(ports_, "local", PROTO_TCP, kAnyAddr)); |
| 1248 | // Two TURN ports, using UDP/TCP for the first hop to the TURN server. |
| 1249 | EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_UDP, kAnyAddr)); |
| 1250 | EXPECT_EQ(1, CountPorts(ports_, "relay", PROTO_TCP, kAnyAddr)); |
| 1251 | // The "any" address port should be in the signaled ready ports, but the host |
| 1252 | // candidate for it is useless and shouldn't be signaled. So we only have |
| 1253 | // STUN/TURN candidates. |
| 1254 | EXPECT_EQ(3U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1255 | EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", |
| 1256 | rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); |
deadbeef | e97389c | 2016-12-23 09:43:45 | [diff] [blame] | 1257 | // Again, two TURN candidates, using UDP/TCP for the first hop to the TURN |
| 1258 | // server. |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 1259 | rtc::SocketAddress addr(kTurnUdpExtAddr.ipaddr(), 0); |
| 1260 | EXPECT_EQ(2, absl::c_count_if(candidates_, [&](const Candidate& c) { |
| 1261 | return c.is_relay() && c.protocol() == "udp" && |
| 1262 | AddressMatch(c.address(), addr); |
| 1263 | })); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1264 | } |
| 1265 | |
Guo-wei Shieh | 898d21c | 2015-09-30 17:54:55 | [diff] [blame] | 1266 | // Test that when enumeration is disabled, we should not have any ports when |
| 1267 | // candidate_filter() is set to CF_RELAY and no relay is specified. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1268 | TEST_F(BasicPortAllocatorTest, |
Guo-wei Shieh | 898d21c | 2015-09-30 17:54:55 | [diff] [blame] | 1269 | TestDisableAdapterEnumerationWithoutNatRelayTransportOnly) { |
Guo-wei Shieh | 898d21c | 2015-09-30 17:54:55 | [diff] [blame] | 1270 | ResetWithStunServerNoNat(kStunAddr); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 1271 | allocator().SetCandidateFilter(CF_RELAY); |
Guo-wei Shieh | 898d21c | 2015-09-30 17:54:55 | [diff] [blame] | 1272 | // Expect to see no ports and no candidates. |
| 1273 | CheckDisableAdapterEnumeration(0U, rtc::IPAddress(), rtc::IPAddress(), |
| 1274 | rtc::IPAddress(), rtc::IPAddress()); |
| 1275 | } |
| 1276 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1277 | // Test that even with multiple interfaces, the result should still be a single |
| 1278 | // default private, one STUN and one TURN candidate since we bind to any address |
| 1279 | // (i.e. all 0s). |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1280 | TEST_F(BasicPortAllocatorTest, |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1281 | TestDisableAdapterEnumerationBehindNatMultipleInterfaces) { |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 | [diff] [blame] | 1282 | AddInterface(kPrivateAddr); |
| 1283 | AddInterface(kPrivateAddr2); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1284 | ResetWithStunServerAndNat(kStunAddr); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 | [diff] [blame] | 1285 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
guoweis | 255d6f6 | 2015-11-23 22:12:38 | [diff] [blame] | 1286 | |
| 1287 | // Enable IPv6 here. Since the network_manager doesn't have IPv6 default |
| 1288 | // address set and we have no IPv6 STUN server, there should be no IPv6 |
| 1289 | // candidates. |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1290 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1291 | session_->set_flags(PORTALLOCATOR_ENABLE_IPV6); |
guoweis | 255d6f6 | 2015-11-23 22:12:38 | [diff] [blame] | 1292 | |
| 1293 | // Expect to see 3 ports for IPv4: HOST/STUN, TURN/UDP and TCP ports, 2 ports |
| 1294 | // for IPv6: HOST, and TCP. Only IPv4 candidates: a default private, STUN and |
| 1295 | // TURN/UDP candidates. |
| 1296 | CheckDisableAdapterEnumeration(5U, kPrivateAddr.ipaddr(), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1297 | kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(), |
| 1298 | rtc::IPAddress()); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1299 | } |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 | [diff] [blame] | 1300 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1301 | // Test that we should get a default private, STUN, TURN/UDP and TURN/TCP |
| 1302 | // candidates when both TURN/UDP and TURN/TCP servers are specified. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1303 | TEST_F(BasicPortAllocatorTest, TestDisableAdapterEnumerationBehindNatWithTcp) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1304 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1305 | AddInterface(kPrivateAddr); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1306 | ResetWithStunServerAndNat(kStunAddr); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1307 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1308 | // Expect to see 4 ports - STUN, TURN/UDP, TURN/TCP and TCP port. A default |
| 1309 | // private, STUN, TURN/UDP, and TURN/TCP candidates. |
| 1310 | CheckDisableAdapterEnumeration(4U, kPrivateAddr.ipaddr(), |
| 1311 | kNatUdpAddr.ipaddr(), kTurnUdpExtAddr.ipaddr(), |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1312 | kTurnUdpExtAddr.ipaddr()); |
| 1313 | } |
| 1314 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1315 | // Test that when adapter enumeration is disabled, for endpoints without |
| 1316 | // STUN/TURN specified, a default private candidate is still generated. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1317 | TEST_F(BasicPortAllocatorTest, |
| 1318 | TestDisableAdapterEnumerationWithoutNatOrServers) { |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1319 | ResetWithNoServersOrNat(); |
| 1320 | // Expect to see 2 ports: STUN and TCP ports, one default private candidate. |
| 1321 | CheckDisableAdapterEnumeration(2U, kPrivateAddr.ipaddr(), rtc::IPAddress(), |
| 1322 | rtc::IPAddress(), rtc::IPAddress()); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1323 | } |
| 1324 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1325 | // Test that when adapter enumeration is disabled, with |
| 1326 | // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind |
| 1327 | // a NAT, there is no local candidate. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1328 | TEST_F(BasicPortAllocatorTest, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1329 | TestDisableAdapterEnumerationWithoutNatLocalhostCandidateDisabled) { |
| 1330 | ResetWithStunServerNoNat(kStunAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1331 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1332 | session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1333 | // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN |
| 1334 | // candidate. |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1335 | CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), rtc::IPAddress(), |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 1336 | rtc::IPAddress(), rtc::IPAddress()); |
| 1337 | } |
| 1338 | |
| 1339 | // Test that when adapter enumeration is disabled, with |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1340 | // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints not behind |
| 1341 | // a NAT, there is no local candidate. However, this specified default route |
| 1342 | // (kClientAddr) which was discovered when sending STUN requests, will become |
| 1343 | // the srflx addresses. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1344 | TEST_F(BasicPortAllocatorTest, |
| 1345 | TestDisableAdapterEnumerationWithoutNatLocalhostCandDisabledDiffRoute) { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 1346 | ResetWithStunServerNoNat(kStunAddr); |
Niels Möller | 84d1595 | 2021-09-01 08:50:34 | [diff] [blame] | 1347 | AddInterfaceAsDefaultSourceAddresss(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1348 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1349 | session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); |
Guo-wei Shieh | fe3bc9d | 2015-08-20 15:48:20 | [diff] [blame] | 1350 | // Expect to see 2 ports: STUN and TCP ports, localhost candidate and STUN |
| 1351 | // candidate. |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1352 | CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kClientAddr.ipaddr(), |
| 1353 | rtc::IPAddress(), rtc::IPAddress()); |
| 1354 | } |
| 1355 | |
| 1356 | // Test that when adapter enumeration is disabled, with |
| 1357 | // PORTALLOCATOR_DISABLE_LOCALHOST_CANDIDATE specified, for endpoints behind a |
| 1358 | // NAT, there is only one STUN candidate. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1359 | TEST_F(BasicPortAllocatorTest, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1360 | TestDisableAdapterEnumerationWithNatLocalhostCandidateDisabled) { |
| 1361 | ResetWithStunServerAndNat(kStunAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1362 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1363 | session_->set_flags(PORTALLOCATOR_DISABLE_DEFAULT_LOCAL_CANDIDATE); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 1364 | // Expect to see 2 ports: STUN and TCP ports, and single STUN candidate. |
| 1365 | CheckDisableAdapterEnumeration(2U, rtc::IPAddress(), kNatUdpAddr.ipaddr(), |
| 1366 | rtc::IPAddress(), rtc::IPAddress()); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 | [diff] [blame] | 1367 | } |
| 1368 | |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 1369 | // Test that we disable relay over UDP, and only TCP is used when connecting to |
| 1370 | // the relay server. |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1371 | TEST_F(BasicPortAllocatorTest, TestDisableUdpTurn) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1372 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 1373 | AddInterface(kClientAddr); |
| 1374 | ResetWithStunServerAndNat(kStunAddr); |
| 1375 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1376 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1377 | session_->set_flags(PORTALLOCATOR_DISABLE_UDP_RELAY | |
| 1378 | PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN | |
| 1379 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 1380 | |
| 1381 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1382 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1383 | kDefaultAllocationTimeout, fake_clock); |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 1384 | |
| 1385 | // Expect to see 2 ports and 2 candidates - TURN/TCP and TCP ports, TCP and |
| 1386 | // TURN/TCP candidates. |
| 1387 | EXPECT_EQ(2U, ports_.size()); |
| 1388 | EXPECT_EQ(2U, candidates_.size()); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1389 | Candidate turn_candidate; |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1390 | EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", kTurnUdpExtAddr, |
| 1391 | &turn_candidate)); |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 1392 | // The TURN candidate should use TCP to contact the TURN server. |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1393 | EXPECT_EQ(TCP_PROTOCOL_NAME, turn_candidate.relay_protocol()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1394 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
Guo-wei Shieh | 13d35f6 | 2015-08-26 22:32:56 | [diff] [blame] | 1395 | } |
| 1396 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1397 | // Test that we can get OnCandidatesAllocationDone callback when all the ports |
| 1398 | // are disabled. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1399 | TEST_F(BasicPortAllocatorTest, TestDisableAllPorts) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1400 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1401 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1402 | session_->set_flags(PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_STUN | |
| 1403 | PORTALLOCATOR_DISABLE_RELAY | PORTALLOCATOR_DISABLE_TCP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1404 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1405 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1406 | EXPECT_EQ(0U, candidates_.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | // Test that we don't crash or malfunction if we can't create UDP sockets. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1410 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSockets) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1411 | AddInterface(kClientAddr); |
| 1412 | fss_->set_udp_sockets_enabled(false); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1413 | ASSERT_TRUE(CreateSession(1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1414 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1415 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1416 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1417 | EXPECT_EQ(1U, candidates_.size()); |
| 1418 | EXPECT_EQ(1U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1419 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | // Test that we don't crash or malfunction if we can't create UDP sockets or |
| 1423 | // listen on TCP sockets. We still give out a local TCP address, since |
| 1424 | // apparently this is needed for the remote side to accept our connection. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1425 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpSocketsNoTcpListen) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1426 | AddInterface(kClientAddr); |
| 1427 | fss_->set_udp_sockets_enabled(false); |
| 1428 | fss_->set_tcp_listen_enabled(false); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1429 | ASSERT_TRUE(CreateSession(1)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1430 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1431 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1432 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1433 | EXPECT_EQ(1U, candidates_.size()); |
| 1434 | EXPECT_EQ(1U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1435 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | // Test that we don't crash or malfunction if we can't create any sockets. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1439 | // TODO(deadbeef): Find a way to exit early here. |
| 1440 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoSockets) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1441 | AddInterface(kClientAddr); |
| 1442 | fss_->set_tcp_sockets_enabled(false); |
| 1443 | fss_->set_udp_sockets_enabled(false); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1444 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1445 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1446 | SIMULATED_WAIT(candidates_.size() > 0, 2000, fake_clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1447 | // TODO(deadbeef): Check candidate_allocation_done signal. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1448 | // In case of Relay, ports creation will succeed but sockets will fail. |
| 1449 | // There is no error reporting from RelayEntry to handle this failure. |
| 1450 | } |
| 1451 | |
| 1452 | // Testing STUN timeout. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1453 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsNoUdpAllowed) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1454 | fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr); |
| 1455 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1456 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1457 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1458 | EXPECT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout, |
| 1459 | fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1460 | EXPECT_EQ(2U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1461 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1462 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
pthatcher | 94a2f21 | 2017-02-08 22:42:22 | [diff] [blame] | 1463 | // We wait at least for a full STUN timeout, which |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1464 | // cricket::STUN_TOTAL_TIMEOUT seconds. |
pthatcher | 94a2f21 | 2017-02-08 22:42:22 | [diff] [blame] | 1465 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1466 | cricket::STUN_TOTAL_TIMEOUT, fake_clock); |
| 1467 | // No additional (STUN) candidates. |
| 1468 | EXPECT_EQ(2U, candidates_.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1469 | } |
| 1470 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1471 | TEST_F(BasicPortAllocatorTest, TestCandidatePriorityOfMultipleInterfaces) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1472 | AddInterface(kClientAddr); |
| 1473 | AddInterface(kClientAddr2); |
| 1474 | // Allocating only host UDP ports. This is done purely for testing |
| 1475 | // convenience. |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1476 | allocator().set_flags(PORTALLOCATOR_DISABLE_TCP | PORTALLOCATOR_DISABLE_STUN | |
| 1477 | PORTALLOCATOR_DISABLE_RELAY); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1478 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1479 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1480 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1481 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1482 | ASSERT_EQ(2U, candidates_.size()); |
| 1483 | EXPECT_EQ(2U, ports_.size()); |
| 1484 | // Candidates priorities should be different. |
| 1485 | EXPECT_NE(candidates_[0].priority(), candidates_[1].priority()); |
| 1486 | } |
| 1487 | |
| 1488 | // Test to verify ICE restart process. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1489 | TEST_F(BasicPortAllocatorTest, TestGetAllPortsRestarts) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1490 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1491 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1492 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1493 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1494 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1495 | EXPECT_EQ(3U, candidates_.size()); |
| 1496 | EXPECT_EQ(3U, ports_.size()); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1497 | // TODO(deadbeef): Extend this to verify ICE restart. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1498 | } |
| 1499 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1500 | // Test that the allocator session uses the candidate filter it's created with, |
| 1501 | // rather than the filter of its parent allocator. |
| 1502 | // The filter of the allocator should only affect the next gathering phase, |
| 1503 | // according to JSEP, which means the *next* allocator session returned. |
| 1504 | TEST_F(BasicPortAllocatorTest, TestSessionUsesOwnCandidateFilter) { |
| 1505 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1506 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1507 | // Set candidate filter *after* creating the session. Should have no effect. |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 1508 | allocator().SetCandidateFilter(CF_RELAY); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1509 | session_->StartGettingPorts(); |
| 1510 | // 7 candidates and 4 ports is what we would normally get (see the |
| 1511 | // TestGetAllPorts* tests). |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1512 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1513 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1514 | EXPECT_EQ(3U, candidates_.size()); |
| 1515 | EXPECT_EQ(3U, ports_.size()); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1516 | } |
| 1517 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1518 | // Test ICE candidate filter mechanism with options Relay/Host/Reflexive. |
| 1519 | // This test also verifies that when the allocator is only allowed to use |
| 1520 | // relay (i.e. IceTransportsType is relay), the raddr is an empty |
| 1521 | // address with the correct family. This is to prevent any local |
| 1522 | // reflective address leakage in the sdp line. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1523 | TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithRelayOnly) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1524 | AddInterface(kClientAddr); |
| 1525 | // GTURN is not configured here. |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1526 | ResetWithTurnServersNoNat(kTurnUdpIntAddr, rtc::SocketAddress()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 1527 | allocator().SetCandidateFilter(CF_RELAY); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1528 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1529 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1530 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1531 | kDefaultAllocationTimeout, fake_clock); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1532 | EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", |
| 1533 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1534 | |
| 1535 | EXPECT_EQ(1U, candidates_.size()); |
| 1536 | EXPECT_EQ(1U, ports_.size()); // Only Relay port will be in ready state. |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 1537 | EXPECT_TRUE(candidates_[0].is_relay()); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1538 | EXPECT_EQ( |
| 1539 | candidates_[0].related_address(), |
| 1540 | rtc::EmptySocketAddressWithFamily(candidates_[0].address().family())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1541 | } |
| 1542 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1543 | TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithHostOnly) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1544 | AddInterface(kClientAddr); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1545 | allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 1546 | allocator().SetCandidateFilter(CF_HOST); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1547 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1548 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1549 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1550 | kDefaultAllocationTimeout, fake_clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1551 | EXPECT_EQ(2U, candidates_.size()); // Host UDP/TCP candidates only. |
| 1552 | EXPECT_EQ(2U, ports_.size()); // UDP/TCP ports only. |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1553 | for (const Candidate& candidate : candidates_) { |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 1554 | EXPECT_TRUE(candidate.is_local()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | // Host is behind the NAT. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1559 | TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnly) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1560 | AddInterface(kPrivateAddr); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1561 | ResetWithStunServerAndNat(kStunAddr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1562 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1563 | allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 1564 | allocator().SetCandidateFilter(CF_REFLEXIVE); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1565 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1566 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1567 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1568 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1569 | // Host is behind NAT, no private address will be exposed. Hence only UDP |
| 1570 | // port with STUN candidate will be sent outside. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1571 | EXPECT_EQ(1U, candidates_.size()); // Only STUN candidate. |
| 1572 | EXPECT_EQ(1U, ports_.size()); // Only UDP port will be in ready state. |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 1573 | EXPECT_TRUE(candidates_[0].is_stun()); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1574 | EXPECT_EQ( |
| 1575 | candidates_[0].related_address(), |
| 1576 | rtc::EmptySocketAddressWithFamily(candidates_[0].address().family())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | // Host is not behind the NAT. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1580 | TEST_F(BasicPortAllocatorTest, TestCandidateFilterWithReflexiveOnlyAndNoNAT) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1581 | AddInterface(kClientAddr); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1582 | allocator().set_flags(PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 1583 | allocator().SetCandidateFilter(CF_REFLEXIVE); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1584 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1585 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1586 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1587 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1588 | // Host has a public address, both UDP and TCP candidates will be exposed. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1589 | EXPECT_EQ(2U, candidates_.size()); // Local UDP + TCP candidate. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1590 | EXPECT_EQ(2U, ports_.size()); // UDP and TCP ports will be in ready state. |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1591 | for (const Candidate& candidate : candidates_) { |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 1592 | EXPECT_TRUE(candidate.is_local()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
Peter Thatcher | 7cbd188 | 2015-09-18 01:54:52 | [diff] [blame] | 1596 | // Test that we get the same ufrag and pwd for all candidates. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1597 | TEST_F(BasicPortAllocatorTest, TestEnableSharedUfrag) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1598 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1599 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1600 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1601 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1602 | kDefaultAllocationTimeout, fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1603 | EXPECT_EQ(3U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1604 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1605 | EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", kClientAddr)); |
| 1606 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1607 | EXPECT_EQ(3U, ports_.size()); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1608 | for (const Candidate& candidate : candidates_) { |
| 1609 | EXPECT_EQ(kIceUfrag0, candidate.username()); |
| 1610 | EXPECT_EQ(kIcePwd0, candidate.password()); |
| 1611 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1612 | } |
| 1613 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1614 | // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port |
| 1615 | // is allocated for udp and stun. Also verify there is only one candidate |
| 1616 | // (local) if stun candidate is same as local candidate, which will be the case |
| 1617 | // in a public network like the below test. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1618 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNat) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1619 | AddInterface(kClientAddr); |
| 1620 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1621 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1622 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1623 | session_->StartGettingPorts(); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1624 | ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), kDefaultAllocationTimeout, |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1625 | fake_clock); |
Niels Möller | a8e6f34 | 2019-09-24 07:05:19 | [diff] [blame] | 1626 | EXPECT_EQ(2U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1627 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1628 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1629 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port |
| 1633 | // is allocated for udp and stun. In this test we should expect both stun and |
| 1634 | // local candidates as client behind a nat. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1635 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNat) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1636 | AddInterface(kClientAddr); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1637 | ResetWithStunServerAndNat(kStunAddr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1638 | |
| 1639 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1640 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1641 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1642 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1643 | ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout, |
| 1644 | fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1645 | ASSERT_EQ(2U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1646 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1647 | EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", |
| 1648 | rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1649 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1650 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1651 | EXPECT_EQ(3U, candidates_.size()); |
| 1652 | } |
| 1653 | |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1654 | // Test TURN port in shared socket mode with UDP and TCP TURN server addresses. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1655 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithoutNatUsingTurn) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1656 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1657 | AddInterface(kClientAddr); |
Tommi | 3756e29 | 2023-09-01 10:45:06 | [diff] [blame] | 1658 | allocator_.reset(new BasicPortAllocator(&network_manager_, &socket_factory_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 1659 | allocator_->Initialize(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1660 | |
| 1661 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 1662 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1663 | allocator_->set_step_delay(kMinimumStepDelay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1664 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1665 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 1666 | PORTALLOCATOR_DISABLE_TCP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1667 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1668 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1669 | session_->StartGettingPorts(); |
| 1670 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1671 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1672 | kDefaultAllocationTimeout, fake_clock); |
| 1673 | ASSERT_EQ(3U, candidates_.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1674 | ASSERT_EQ(3U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1675 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1676 | EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", |
| 1677 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
| 1678 | EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", |
| 1679 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1680 | } |
| 1681 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1682 | // Test that if the turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN |
| 1683 | // port will not be used if UDP TurnPort is used, given that TCP TURN port |
| 1684 | // becomes ready first. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1685 | TEST_F(BasicPortAllocatorTest, |
| 1686 | TestUdpTurnPortPrunesTcpTurnPortWithTcpPortReadyFirst) { |
| 1687 | // UDP has longer delay than TCP so that TCP TURN port becomes ready first. |
| 1688 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200); |
| 1689 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1690 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1691 | TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY, |
| 1692 | true /* tcp_pruned */); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1693 | } |
| 1694 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1695 | // Test that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, TCP TURN port |
| 1696 | // will not be used if UDP TurnPort is used, given that UDP TURN port becomes |
| 1697 | // ready first. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1698 | TEST_F(BasicPortAllocatorTest, |
| 1699 | TestUdpTurnPortPrunesTcpTurnPortsWithUdpPortReadyFirst) { |
| 1700 | // UDP has shorter delay than TCP so that UDP TURN port becomes ready first. |
| 1701 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100); |
| 1702 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1703 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1704 | TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::PRUNE_BASED_ON_PRIORITY, |
| 1705 | true /* tcp_pruned */); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1706 | } |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1707 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1708 | // Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port |
| 1709 | // will be kept regardless of the priority. |
| 1710 | TEST_F(BasicPortAllocatorTest, |
| 1711 | TestUdpTurnPortPrunesTcpTurnPortIfUdpReadyFirst) { |
| 1712 | // UDP has shorter delay than TCP so that UDP TURN port becomes ready first. |
| 1713 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100); |
| 1714 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 200); |
| 1715 | |
| 1716 | TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY, |
| 1717 | true /* tcp_pruned */); |
| 1718 | } |
| 1719 | |
| 1720 | // Test that if turn_port_prune policy is KEEP_FIRST_READY, the first ready port |
| 1721 | // will be kept regardless of the priority. |
| 1722 | TEST_F(BasicPortAllocatorTest, |
| 1723 | TestTcpTurnPortPrunesUdpTurnPortIfTcpReadyFirst) { |
| 1724 | // UDP has longer delay than TCP so that TCP TURN port becomes ready first. |
| 1725 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200); |
| 1726 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 100); |
| 1727 | |
| 1728 | TestTurnPortPrunesWithUdpAndTcpPorts(webrtc::KEEP_FIRST_READY, |
| 1729 | false /* tcp_pruned */); |
| 1730 | } |
| 1731 | |
| 1732 | // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4 |
| 1733 | // TurnPort will not be used if IPv6 TurnPort is used, given that IPv4 TURN port |
| 1734 | // becomes ready first. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1735 | TEST_F(BasicPortAllocatorTest, |
| 1736 | TestIPv6TurnPortPrunesIPv4TurnPortWithIPv4PortReadyFirst) { |
| 1737 | // IPv6 has longer delay than IPv4, so that IPv4 TURN port becomes ready |
| 1738 | // first. |
| 1739 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100); |
| 1740 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 200); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1741 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1742 | TestIPv6TurnPortPrunesIPv4TurnPort(); |
| 1743 | } |
| 1744 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1745 | // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, IPv4 |
| 1746 | // TurnPort will not be used if IPv6 TurnPort is used, given that IPv6 TURN port |
| 1747 | // becomes ready first. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1748 | TEST_F(BasicPortAllocatorTest, |
| 1749 | TestIPv6TurnPortPrunesIPv4TurnPortWithIPv6PortReadyFirst) { |
| 1750 | // IPv6 has longer delay than IPv4, so that IPv6 TURN port becomes ready |
| 1751 | // first. |
| 1752 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 200); |
| 1753 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 100); |
| 1754 | |
| 1755 | TestIPv6TurnPortPrunesIPv4TurnPort(); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1756 | } |
| 1757 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1758 | // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network |
| 1759 | // interface will has its own set of TurnPorts based on their priorities, in the |
| 1760 | // default case where no transit delay is set. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1761 | TEST_F(BasicPortAllocatorTest, TestEachInterfaceHasItsOwnTurnPortsNoDelay) { |
| 1762 | TestEachInterfaceHasItsOwnTurnPorts(); |
| 1763 | } |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1764 | |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 1765 | // Tests that if turn port prune policy is PRUNE_BASED_ON_PRIORITY, each network |
| 1766 | // interface will has its own set of TurnPorts based on their priorities, given |
| 1767 | // that IPv4/TCP TURN port becomes ready first. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1768 | TEST_F(BasicPortAllocatorTest, |
| 1769 | TestEachInterfaceHasItsOwnTurnPortsWithTcpIPv4ReadyFirst) { |
| 1770 | // IPv6/UDP have longer delay than IPv4/TCP, so that IPv4/TCP TURN port |
| 1771 | // becomes ready last. |
| 1772 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 10); |
| 1773 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntAddr, 100); |
| 1774 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntIPv6Addr, 20); |
| 1775 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpIntIPv6Addr, 300); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1776 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1777 | TestEachInterfaceHasItsOwnTurnPorts(); |
Honghai Zhang | b9e7b4a | 2016-07-01 03:52:02 | [diff] [blame] | 1778 | } |
| 1779 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1780 | // Testing DNS resolve for the TURN server, this will test AllocationSequence |
| 1781 | // handling the unresolved address signal from TurnPort. |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1782 | // TODO(pthatcher): Make this test work with SIMULATED_WAIT. It |
| 1783 | // appears that it doesn't currently because of the DNS look up not |
| 1784 | // using the fake clock. |
| 1785 | TEST_F(BasicPortAllocatorTestWithRealClock, |
| 1786 | TestSharedSocketWithServerAddressResolve) { |
deadbeef | 9a6f4d4 | 2017-05-16 02:43:33 | [diff] [blame] | 1787 | // This test relies on a real query for "localhost", so it won't work on an |
| 1788 | // IPv6-only machine. |
| 1789 | MAYBE_SKIP_IPV4; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1790 | turn_server_.AddInternalSocket(rtc::SocketAddress("127.0.0.1", 3478), |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1791 | PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1792 | AddInterface(kClientAddr); |
Tommi | 3756e29 | 2023-09-01 10:45:06 | [diff] [blame] | 1793 | allocator_.reset(new BasicPortAllocator(&network_manager_, &socket_factory_)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 1794 | allocator_->Initialize(); |
Niels Möller | 191e38f | 2019-11-04 07:49:12 | [diff] [blame] | 1795 | RelayServerConfig turn_server; |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1796 | RelayCredentials credentials(kTurnUsername, kTurnPassword); |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 1797 | turn_server.credentials = credentials; |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1798 | turn_server.ports.push_back( |
hnsl | 277b250 | 2016-12-13 13:17:23 | [diff] [blame] | 1799 | ProtocolAddress(rtc::SocketAddress("localhost", 3478), PROTO_UDP)); |
Jonas Oreland | 3e7e15d | 2022-11-03 09:02:55 | [diff] [blame] | 1800 | allocator_->AddTurnServerForTesting(turn_server); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1801 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1802 | allocator_->set_step_delay(kMinimumStepDelay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1803 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1804 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 1805 | PORTALLOCATOR_DISABLE_TCP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1806 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1807 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1808 | session_->StartGettingPorts(); |
| 1809 | |
| 1810 | EXPECT_EQ_WAIT(2U, ports_.size(), kDefaultAllocationTimeout); |
| 1811 | } |
| 1812 | |
| 1813 | // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled only one port |
| 1814 | // is allocated for udp/stun/turn. In this test we should expect all local, |
| 1815 | // stun and turn candidates. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1816 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurn) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1817 | AddInterface(kClientAddr); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1818 | ResetWithStunServerAndNat(kStunAddr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1819 | |
| 1820 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 1821 | |
| 1822 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1823 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 1824 | PORTALLOCATOR_DISABLE_TCP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1825 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1826 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1827 | session_->StartGettingPorts(); |
| 1828 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1829 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1830 | kDefaultAllocationTimeout, fake_clock); |
| 1831 | EXPECT_EQ(3U, candidates_.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1832 | ASSERT_EQ(2U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1833 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1834 | EXPECT_TRUE(HasCandidate(candidates_, "stun", "udp", |
| 1835 | rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0))); |
| 1836 | EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", |
| 1837 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1838 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1839 | kDefaultAllocationTimeout, fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1840 | // Local port will be created first and then TURN port. |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1841 | // TODO(deadbeef): This isn't something the BasicPortAllocator API contract |
| 1842 | // guarantees... |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1843 | EXPECT_EQ(2U, ports_[0]->Candidates().size()); |
| 1844 | EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
| 1845 | } |
| 1846 | |
| 1847 | // Test that when PORTALLOCATOR_ENABLE_SHARED_SOCKET is enabled and the TURN |
| 1848 | // server is also used as the STUN server, we should get 'local', 'stun', and |
| 1849 | // 'relay' candidates. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1850 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAsStun) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1851 | AddInterface(kClientAddr); |
Jiayang Liu | d7e5c44 | 2015-04-27 18:47:21 | [diff] [blame] | 1852 | // Use an empty SocketAddress to add a NAT without STUN server. |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1853 | ResetWithStunServerAndNat(SocketAddress()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1854 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 1855 | |
| 1856 | // Must set the step delay to 0 to make sure the relay allocation phase is |
| 1857 | // started before the STUN candidates are obtained, so that the STUN binding |
| 1858 | // response is processed when both StunPort and TurnPort exist to reproduce |
| 1859 | // webrtc issue 3537. |
| 1860 | allocator_->set_step_delay(0); |
| 1861 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1862 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 1863 | PORTALLOCATOR_DISABLE_TCP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1864 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1865 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1866 | session_->StartGettingPorts(); |
| 1867 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1868 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1869 | kDefaultAllocationTimeout, fake_clock); |
| 1870 | EXPECT_EQ(3U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1871 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1872 | Candidate stun_candidate; |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1873 | EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp", |
| 1874 | rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), |
| 1875 | &stun_candidate)); |
| 1876 | EXPECT_TRUE(HasCandidateWithRelatedAddr( |
| 1877 | candidates_, "relay", "udp", |
| 1878 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0), |
| 1879 | stun_candidate.address())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1880 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1881 | // Local port will be created first and then TURN port. |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1882 | // TODO(deadbeef): This isn't something the BasicPortAllocator API contract |
| 1883 | // guarantees... |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1884 | EXPECT_EQ(2U, ports_[0]->Candidates().size()); |
| 1885 | EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
| 1886 | } |
| 1887 | |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1888 | // Test that when only a TCP TURN server is available, we do NOT use it as |
| 1889 | // a UDP STUN server, as this could leak our IP address. Thus we should only |
| 1890 | // expect two ports, a UDPPort and TurnPort. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1891 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnTcpOnly) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1892 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1893 | AddInterface(kClientAddr); |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1894 | ResetWithStunServerAndNat(rtc::SocketAddress()); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1895 | AddTurnServers(rtc::SocketAddress(), kTurnTcpIntAddr); |
| 1896 | |
| 1897 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1898 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 1899 | PORTALLOCATOR_DISABLE_TCP); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1900 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1901 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1902 | session_->StartGettingPorts(); |
| 1903 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1904 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1905 | kDefaultAllocationTimeout, fake_clock); |
| 1906 | EXPECT_EQ(2U, candidates_.size()); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1907 | ASSERT_EQ(2U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1908 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 1909 | EXPECT_TRUE(HasCandidate(candidates_, "relay", "udp", |
| 1910 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0))); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1911 | EXPECT_EQ(1U, ports_[0]->Candidates().size()); |
| 1912 | EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
| 1913 | } |
| 1914 | |
| 1915 | // Test that even when PORTALLOCATOR_ENABLE_SHARED_SOCKET is NOT enabled, the |
| 1916 | // TURN server is used as the STUN server and we get 'local', 'stun', and |
| 1917 | // 'relay' candidates. |
| 1918 | // TODO(deadbeef): Remove this test when support for non-shared socket mode |
| 1919 | // is removed. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1920 | TEST_F(BasicPortAllocatorTest, TestNonSharedSocketWithNatUsingTurnAsStun) { |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1921 | AddInterface(kClientAddr); |
| 1922 | // Use an empty SocketAddress to add a NAT without STUN server. |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1923 | ResetWithStunServerAndNat(SocketAddress()); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1924 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 1925 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1926 | allocator_->set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_TCP); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1927 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1928 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1929 | session_->StartGettingPorts(); |
| 1930 | |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 1931 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 1932 | kDefaultAllocationTimeout, fake_clock); |
| 1933 | EXPECT_EQ(3U, candidates_.size()); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1934 | ASSERT_EQ(3U, ports_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1935 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1936 | Candidate stun_candidate; |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1937 | EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp", |
| 1938 | rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), |
| 1939 | &stun_candidate)); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1940 | Candidate turn_candidate; |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1941 | EXPECT_TRUE(FindCandidate(candidates_, "relay", "udp", |
| 1942 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0), |
| 1943 | &turn_candidate)); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1944 | // Not using shared socket, so the STUN request's server reflexive address |
| 1945 | // should be different than the TURN request's server reflexive address. |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1946 | EXPECT_NE(turn_candidate.related_address(), stun_candidate.address()); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1947 | |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1948 | EXPECT_EQ(1U, ports_[0]->Candidates().size()); |
| 1949 | EXPECT_EQ(1U, ports_[1]->Candidates().size()); |
| 1950 | EXPECT_EQ(1U, ports_[2]->Candidates().size()); |
| 1951 | } |
| 1952 | |
| 1953 | // Test that even when both a STUN and TURN server are configured, the TURN |
| 1954 | // server is used as a STUN server and we get a 'stun' candidate. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1955 | TEST_F(BasicPortAllocatorTest, TestSharedSocketWithNatUsingTurnAndStun) { |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1956 | AddInterface(kClientAddr); |
| 1957 | // Configure with STUN server but destroy it, so we can ensure that it's |
| 1958 | // the TURN server actually being used as a STUN server. |
Guo-wei Shieh | 1147702 | 2015-08-15 16:28:41 | [diff] [blame] | 1959 | ResetWithStunServerAndNat(kStunAddr); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1960 | stun_server_.reset(); |
| 1961 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 1962 | |
| 1963 | allocator_->set_flags(allocator().flags() | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1964 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 1965 | PORTALLOCATOR_DISABLE_TCP); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1966 | |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1967 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1968 | session_->StartGettingPorts(); |
| 1969 | |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1970 | ASSERT_EQ_SIMULATED_WAIT(3U, candidates_.size(), kDefaultAllocationTimeout, |
| 1971 | fake_clock); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1972 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
Taylor Brandstetter | 8c9be5e4 | 2016-05-26 23:07:31 | [diff] [blame] | 1973 | Candidate stun_candidate; |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 1974 | EXPECT_TRUE(FindCandidate(candidates_, "stun", "udp", |
| 1975 | rtc::SocketAddress(kNatUdpAddr.ipaddr(), 0), |
| 1976 | &stun_candidate)); |
| 1977 | EXPECT_TRUE(HasCandidateWithRelatedAddr( |
| 1978 | candidates_, "relay", "udp", |
| 1979 | rtc::SocketAddress(kTurnUdpExtAddr.ipaddr(), 0), |
| 1980 | stun_candidate.address())); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 1981 | |
| 1982 | // Don't bother waiting for STUN timeout, since we already verified |
| 1983 | // that we got a STUN candidate from the TURN server. |
| 1984 | } |
| 1985 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1986 | // This test verifies when PORTALLOCATOR_ENABLE_SHARED_SOCKET flag is enabled |
| 1987 | // and fail to generate STUN candidate, local UDP candidate is generated |
| 1988 | // properly. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1989 | TEST_F(BasicPortAllocatorTest, TestSharedSocketNoUdpAllowed) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 1990 | allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY | |
| 1991 | PORTALLOCATOR_DISABLE_TCP | |
| 1992 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1993 | fss_->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kClientAddr); |
| 1994 | AddInterface(kClientAddr); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 1995 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 1996 | session_->StartGettingPorts(); |
Mirko Bonadei | 5f4d47b | 2018-08-22 17:41:22 | [diff] [blame] | 1997 | ASSERT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout, |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 1998 | fake_clock); |
Mirko Bonadei | 5f4d47b | 2018-08-22 17:41:22 | [diff] [blame] | 1999 | EXPECT_EQ(1U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 2000 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
Taylor Brandstetter | 8fcf414 | 2016-05-23 19:49:30 | [diff] [blame] | 2001 | // STUN timeout is 9.5sec. We need to wait to get candidate done signal. |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2002 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, kStunTimeoutMs, |
| 2003 | fake_clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 2004 | EXPECT_EQ(1U, candidates_.size()); |
| 2005 | } |
| 2006 | |
Guo-wei Shieh | 47872ec | 2015-08-19 17:32:46 | [diff] [blame] | 2007 | // Test that when the NetworkManager doesn't have permission to enumerate |
| 2008 | // adapters, the PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION is specified |
| 2009 | // automatically. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2010 | TEST_F(BasicPortAllocatorTest, TestNetworkPermissionBlocked) { |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 2011 | network_manager_.set_default_local_addresses(kPrivateAddr.ipaddr(), |
| 2012 | rtc::IPAddress()); |
Guo-wei Shieh | 47872ec | 2015-08-19 17:32:46 | [diff] [blame] | 2013 | network_manager_.set_enumeration_permission( |
guoweis | ea1012b | 2015-08-21 16:06:28 | [diff] [blame] | 2014 | rtc::NetworkManager::ENUMERATION_BLOCKED); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2015 | allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY | |
| 2016 | PORTALLOCATOR_DISABLE_TCP | |
| 2017 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 2018 | EXPECT_EQ(0U, |
| 2019 | allocator_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 2020 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2021 | EXPECT_EQ(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); |
Guo-wei Shieh | 47872ec | 2015-08-19 17:32:46 | [diff] [blame] | 2022 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2023 | EXPECT_EQ_SIMULATED_WAIT(1U, ports_.size(), kDefaultAllocationTimeout, |
| 2024 | fake_clock); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 22:47:39 | [diff] [blame] | 2025 | EXPECT_EQ(1U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 2026 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kPrivateAddr)); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2027 | EXPECT_NE(0U, session_->flags() & PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION); |
Guo-wei Shieh | 47872ec | 2015-08-19 17:32:46 | [diff] [blame] | 2028 | } |
| 2029 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 2030 | // This test verifies allocator can use IPv6 addresses along with IPv4. |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2031 | TEST_F(BasicPortAllocatorTest, TestEnableIPv6Addresses) { |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2032 | allocator().set_flags(allocator().flags() | PORTALLOCATOR_DISABLE_RELAY | |
| 2033 | PORTALLOCATOR_ENABLE_IPV6 | |
| 2034 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 2035 | AddInterface(kClientIPv6Addr); |
| 2036 | AddInterface(kClientAddr); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2037 | allocator_->set_step_delay(kMinimumStepDelay); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 2038 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 2039 | session_->StartGettingPorts(); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 2040 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2041 | kDefaultAllocationTimeout, fake_clock); |
deadbeef | 7f1563f | 2017-09-16 00:40:01 | [diff] [blame] | 2042 | EXPECT_EQ(4U, ports_.size()); |
| 2043 | EXPECT_EQ(4U, candidates_.size()); |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 2044 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr)); |
| 2045 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientAddr)); |
| 2046 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientIPv6Addr)); |
| 2047 | EXPECT_TRUE(HasCandidate(candidates_, "local", "tcp", kClientAddr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 2048 | } |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2049 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2050 | TEST_F(BasicPortAllocatorTest, TestStopGettingPorts) { |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2051 | AddInterface(kClientAddr); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2052 | allocator_->set_step_delay(kDefaultStepDelay); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 2053 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2054 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2055 | ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2056 | EXPECT_EQ(2U, ports_.size()); |
| 2057 | session_->StopGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2058 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2059 | |
| 2060 | // After stopping getting ports, adding a new interface will not start |
| 2061 | // getting ports again. |
deadbeef | b60a819 | 2016-08-24 22:15:00 | [diff] [blame] | 2062 | allocator_->set_step_delay(kMinimumStepDelay); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2063 | candidates_.clear(); |
| 2064 | ports_.clear(); |
| 2065 | candidate_allocation_done_ = false; |
| 2066 | network_manager_.AddInterface(kClientAddr2); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2067 | SIMULATED_WAIT(false, 1000, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2068 | EXPECT_EQ(0U, candidates_.size()); |
| 2069 | EXPECT_EQ(0U, ports_.size()); |
| 2070 | } |
| 2071 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2072 | TEST_F(BasicPortAllocatorTest, TestClearGettingPorts) { |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2073 | AddInterface(kClientAddr); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2074 | allocator_->set_step_delay(kDefaultStepDelay); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 2075 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2076 | session_->StartGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2077 | ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2078 | EXPECT_EQ(2U, ports_.size()); |
| 2079 | session_->ClearGettingPorts(); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2080 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, 1000, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2081 | |
| 2082 | // After clearing getting ports, adding a new interface will start getting |
| 2083 | // ports again. |
deadbeef | b60a819 | 2016-08-24 22:15:00 | [diff] [blame] | 2084 | allocator_->set_step_delay(kMinimumStepDelay); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2085 | candidates_.clear(); |
| 2086 | ports_.clear(); |
| 2087 | candidate_allocation_done_ = false; |
| 2088 | network_manager_.AddInterface(kClientAddr2); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2089 | ASSERT_EQ_SIMULATED_WAIT(2U, candidates_.size(), 1000, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2090 | EXPECT_EQ(2U, ports_.size()); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2091 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2092 | kDefaultAllocationTimeout, fake_clock); |
honghaiz | 98db68f | 2015-09-29 14:58:17 | [diff] [blame] | 2093 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2094 | |
| 2095 | // Test that the ports and candidates are updated with new ufrag/pwd/etc. when |
| 2096 | // a pooled session is taken out of the pool. |
| 2097 | TEST_F(BasicPortAllocatorTest, TestTransportInformationUpdated) { |
| 2098 | AddInterface(kClientAddr); |
| 2099 | int pool_size = 1; |
| 2100 | allocator_->SetConfiguration(allocator_->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2101 | allocator_->turn_servers(), pool_size, |
| 2102 | webrtc::NO_PRUNE); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2103 | const PortAllocatorSession* peeked_session = allocator_->GetPooledSession(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2104 | ASSERT_NE(nullptr, peeked_session); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2105 | EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(), |
| 2106 | kDefaultAllocationTimeout, fake_clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2107 | // Expect that when TakePooledSession is called, |
| 2108 | // UpdateTransportInformationInternal will be called and the |
| 2109 | // BasicPortAllocatorSession will update the ufrag/pwd of ports and |
| 2110 | // candidates. |
| 2111 | session_ = |
| 2112 | allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0); |
| 2113 | ASSERT_NE(nullptr, session_.get()); |
| 2114 | auto ready_ports = session_->ReadyPorts(); |
| 2115 | auto candidates = session_->ReadyCandidates(); |
| 2116 | EXPECT_FALSE(ready_ports.empty()); |
| 2117 | EXPECT_FALSE(candidates.empty()); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2118 | for (const PortInterface* port_interface : ready_ports) { |
| 2119 | const Port* port = static_cast<const Port*>(port_interface); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2120 | EXPECT_EQ(kContentName, port->content_name()); |
| 2121 | EXPECT_EQ(1, port->component()); |
| 2122 | EXPECT_EQ(kIceUfrag0, port->username_fragment()); |
| 2123 | EXPECT_EQ(kIcePwd0, port->password()); |
| 2124 | } |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2125 | for (const Candidate& candidate : candidates) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 2126 | EXPECT_EQ(1, candidate.component()); |
| 2127 | EXPECT_EQ(kIceUfrag0, candidate.username()); |
| 2128 | EXPECT_EQ(kIcePwd0, candidate.password()); |
| 2129 | } |
| 2130 | } |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2131 | |
| 2132 | // Test that a new candidate filter takes effect even on already-gathered |
| 2133 | // candidates. |
| 2134 | TEST_F(BasicPortAllocatorTest, TestSetCandidateFilterAfterCandidatesGathered) { |
| 2135 | AddInterface(kClientAddr); |
| 2136 | int pool_size = 1; |
| 2137 | allocator_->SetConfiguration(allocator_->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2138 | allocator_->turn_servers(), pool_size, |
| 2139 | webrtc::NO_PRUNE); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2140 | const PortAllocatorSession* peeked_session = allocator_->GetPooledSession(); |
| 2141 | ASSERT_NE(nullptr, peeked_session); |
pthatcher | 1749bc3 | 2017-02-08 21:18:00 | [diff] [blame] | 2142 | EXPECT_EQ_SIMULATED_WAIT(true, peeked_session->CandidatesAllocationDone(), |
| 2143 | kDefaultAllocationTimeout, fake_clock); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2144 | size_t initial_candidates_size = peeked_session->ReadyCandidates().size(); |
| 2145 | size_t initial_ports_size = peeked_session->ReadyPorts().size(); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2146 | allocator_->SetCandidateFilter(CF_RELAY); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2147 | // Assume that when TakePooledSession is called, the candidate filter will be |
| 2148 | // applied to the pooled session. This is tested by PortAllocatorTest. |
| 2149 | session_ = |
| 2150 | allocator_->TakePooledSession(kContentName, 1, kIceUfrag0, kIcePwd0); |
| 2151 | ASSERT_NE(nullptr, session_.get()); |
| 2152 | auto candidates = session_->ReadyCandidates(); |
| 2153 | auto ports = session_->ReadyPorts(); |
| 2154 | // Sanity check that the number of candidates and ports decreased. |
| 2155 | EXPECT_GT(initial_candidates_size, candidates.size()); |
| 2156 | EXPECT_GT(initial_ports_size, ports.size()); |
| 2157 | for (const PortInterface* port : ports) { |
| 2158 | // Expect only relay ports. |
| 2159 | EXPECT_EQ(RELAY_PORT_TYPE, port->Type()); |
| 2160 | } |
| 2161 | for (const Candidate& candidate : candidates) { |
| 2162 | // Expect only relay candidates now that the filter is applied. |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2163 | EXPECT_TRUE(candidate.is_relay()); |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2164 | // Expect that the raddr is emptied due to the CF_RELAY filter. |
| 2165 | EXPECT_EQ(candidate.related_address(), |
| 2166 | rtc::EmptySocketAddressWithFamily(candidate.address().family())); |
| 2167 | } |
| 2168 | } |
| 2169 | |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2170 | // Test that candidates that do not match a previous candidate filter can be |
| 2171 | // surfaced if they match the new one after setting the filter value. |
| 2172 | TEST_F(BasicPortAllocatorTest, |
| 2173 | SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes) { |
| 2174 | // We would still surface a host candidate if the IP is public, even though it |
| 2175 | // is disabled by the candidate filter. See |
| 2176 | // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so |
| 2177 | // that the srflx candidate is not equivalent to the host candidate. |
| 2178 | AddInterface(kPrivateAddr); |
| 2179 | ResetWithStunServerAndNat(kStunAddr); |
| 2180 | |
| 2181 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 2182 | |
| 2183 | allocator_->set_flags(allocator().flags() | |
| 2184 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 2185 | PORTALLOCATOR_DISABLE_TCP); |
| 2186 | |
| 2187 | allocator_->SetCandidateFilter(CF_NONE); |
| 2188 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 2189 | session_->StartGettingPorts(); |
| 2190 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2191 | kDefaultAllocationTimeout, fake_clock); |
| 2192 | EXPECT_TRUE(candidates_.empty()); |
| 2193 | EXPECT_TRUE(ports_.empty()); |
| 2194 | |
| 2195 | // Surface the relay candidate previously gathered but not signaled. |
| 2196 | session_->SetCandidateFilter(CF_RELAY); |
| 2197 | ASSERT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout, |
| 2198 | fake_clock); |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2199 | EXPECT_TRUE(candidates_.back().is_relay()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2200 | EXPECT_EQ(1u, ports_.size()); |
| 2201 | |
| 2202 | // Surface the srflx candidate previously gathered but not signaled. |
| 2203 | session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE); |
| 2204 | ASSERT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout, |
| 2205 | fake_clock); |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2206 | EXPECT_TRUE(candidates_.back().is_stun()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2207 | EXPECT_EQ(2u, ports_.size()); |
| 2208 | |
| 2209 | // Surface the srflx candidate previously gathered but not signaled. |
| 2210 | session_->SetCandidateFilter(CF_ALL); |
| 2211 | ASSERT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout, |
| 2212 | fake_clock); |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2213 | EXPECT_TRUE(candidates_.back().is_local()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2214 | EXPECT_EQ(2u, ports_.size()); |
| 2215 | } |
| 2216 | |
| 2217 | // This is a similar test as |
| 2218 | // SurfaceNewCandidatesAfterSetCandidateFilterToAddCandidateTypes, and we |
| 2219 | // test the transitions for which the new filter value is not a super set of the |
| 2220 | // previous value. |
| 2221 | TEST_F( |
| 2222 | BasicPortAllocatorTest, |
| 2223 | SurfaceNewCandidatesAfterSetCandidateFilterToAllowDifferentCandidateTypes) { |
| 2224 | // We would still surface a host candidate if the IP is public, even though it |
| 2225 | // is disabled by the candidate filter. See |
| 2226 | // BasicPortAllocatorSession::CheckCandidateFilter. Use the private address so |
| 2227 | // that the srflx candidate is not equivalent to the host candidate. |
| 2228 | AddInterface(kPrivateAddr); |
| 2229 | ResetWithStunServerAndNat(kStunAddr); |
| 2230 | |
| 2231 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 2232 | |
| 2233 | allocator_->set_flags(allocator().flags() | |
| 2234 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 2235 | PORTALLOCATOR_DISABLE_TCP); |
| 2236 | |
| 2237 | allocator_->SetCandidateFilter(CF_NONE); |
| 2238 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 2239 | session_->StartGettingPorts(); |
| 2240 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2241 | kDefaultAllocationTimeout, fake_clock); |
| 2242 | EXPECT_TRUE(candidates_.empty()); |
| 2243 | EXPECT_TRUE(ports_.empty()); |
| 2244 | |
| 2245 | // Surface the relay candidate previously gathered but not signaled. |
| 2246 | session_->SetCandidateFilter(CF_RELAY); |
| 2247 | EXPECT_EQ_SIMULATED_WAIT(1u, candidates_.size(), kDefaultAllocationTimeout, |
| 2248 | fake_clock); |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2249 | EXPECT_TRUE(candidates_.back().is_relay()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2250 | EXPECT_EQ(1u, ports_.size()); |
| 2251 | |
| 2252 | // Surface the srflx candidate previously gathered but not signaled. |
| 2253 | session_->SetCandidateFilter(CF_REFLEXIVE); |
| 2254 | EXPECT_EQ_SIMULATED_WAIT(2u, candidates_.size(), kDefaultAllocationTimeout, |
| 2255 | fake_clock); |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2256 | EXPECT_TRUE(candidates_.back().is_stun()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2257 | EXPECT_EQ(2u, ports_.size()); |
| 2258 | |
| 2259 | // Surface the host candidate previously gathered but not signaled. |
| 2260 | session_->SetCandidateFilter(CF_HOST); |
| 2261 | EXPECT_EQ_SIMULATED_WAIT(3u, candidates_.size(), kDefaultAllocationTimeout, |
| 2262 | fake_clock); |
Tommi | 698b4e7 | 2024-01-24 07:36:45 | [diff] [blame] | 2263 | EXPECT_TRUE(candidates_.back().is_local()); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 2264 | // We use a shared socket and cricket::UDPPort handles the srflx candidate. |
| 2265 | EXPECT_EQ(2u, ports_.size()); |
| 2266 | } |
| 2267 | |
| 2268 | // Test that after an allocation session has stopped getting ports, changing the |
| 2269 | // candidate filter to allow new types of gathered candidates does not surface |
| 2270 | // any candidate. |
| 2271 | TEST_F(BasicPortAllocatorTest, |
| 2272 | NoCandidateSurfacedWhenUpdatingCandidateFilterIfSessionStopped) { |
| 2273 | AddInterface(kPrivateAddr); |
| 2274 | ResetWithStunServerAndNat(kStunAddr); |
| 2275 | |
| 2276 | AddTurnServers(kTurnUdpIntAddr, rtc::SocketAddress()); |
| 2277 | |
| 2278 | allocator_->set_flags(allocator().flags() | |
| 2279 | PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 2280 | PORTALLOCATOR_DISABLE_TCP); |
| 2281 | |
| 2282 | allocator_->SetCandidateFilter(CF_NONE); |
| 2283 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 2284 | session_->StartGettingPorts(); |
| 2285 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2286 | kDefaultAllocationTimeout, fake_clock); |
| 2287 | auto test_invariants = [this]() { |
| 2288 | EXPECT_TRUE(candidates_.empty()); |
| 2289 | EXPECT_TRUE(ports_.empty()); |
| 2290 | }; |
| 2291 | |
| 2292 | test_invariants(); |
| 2293 | |
| 2294 | session_->StopGettingPorts(); |
| 2295 | |
| 2296 | session_->SetCandidateFilter(CF_RELAY); |
| 2297 | SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock); |
| 2298 | test_invariants(); |
| 2299 | |
| 2300 | session_->SetCandidateFilter(CF_RELAY | CF_REFLEXIVE); |
| 2301 | SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock); |
| 2302 | test_invariants(); |
| 2303 | |
| 2304 | session_->SetCandidateFilter(CF_ALL); |
| 2305 | SIMULATED_WAIT(false, kDefaultAllocationTimeout, fake_clock); |
| 2306 | test_invariants(); |
| 2307 | } |
| 2308 | |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 2309 | TEST_F(BasicPortAllocatorTest, SetStunKeepaliveIntervalForPorts) { |
| 2310 | const int pool_size = 1; |
| 2311 | const int expected_stun_keepalive_interval = 123; |
| 2312 | AddInterface(kClientAddr); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2313 | allocator_->SetConfiguration( |
| 2314 | allocator_->stun_servers(), allocator_->turn_servers(), pool_size, |
| 2315 | webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval); |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 2316 | auto* pooled_session = allocator_->GetPooledSession(); |
| 2317 | ASSERT_NE(nullptr, pooled_session); |
| 2318 | EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(), |
| 2319 | kDefaultAllocationTimeout, fake_clock); |
| 2320 | CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session, |
| 2321 | expected_stun_keepalive_interval); |
| 2322 | } |
| 2323 | |
| 2324 | TEST_F(BasicPortAllocatorTest, |
| 2325 | ChangeStunKeepaliveIntervalForPortsAfterInitialConfig) { |
| 2326 | const int pool_size = 1; |
| 2327 | AddInterface(kClientAddr); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2328 | allocator_->SetConfiguration( |
| 2329 | allocator_->stun_servers(), allocator_->turn_servers(), pool_size, |
| 2330 | webrtc::NO_PRUNE, nullptr, 123 /* stun keepalive interval */); |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 2331 | auto* pooled_session = allocator_->GetPooledSession(); |
| 2332 | ASSERT_NE(nullptr, pooled_session); |
| 2333 | EXPECT_EQ_SIMULATED_WAIT(true, pooled_session->CandidatesAllocationDone(), |
| 2334 | kDefaultAllocationTimeout, fake_clock); |
| 2335 | const int expected_stun_keepalive_interval = 321; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2336 | allocator_->SetConfiguration( |
| 2337 | allocator_->stun_servers(), allocator_->turn_servers(), pool_size, |
| 2338 | webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval); |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 2339 | CheckStunKeepaliveIntervalOfAllReadyPorts(pooled_session, |
| 2340 | expected_stun_keepalive_interval); |
| 2341 | } |
| 2342 | |
| 2343 | TEST_F(BasicPortAllocatorTest, |
| 2344 | SetStunKeepaliveIntervalForPortsWithSharedSocket) { |
| 2345 | const int pool_size = 1; |
| 2346 | const int expected_stun_keepalive_interval = 123; |
| 2347 | AddInterface(kClientAddr); |
| 2348 | allocator_->set_flags(allocator().flags() | |
| 2349 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2350 | allocator_->SetConfiguration( |
| 2351 | allocator_->stun_servers(), allocator_->turn_servers(), pool_size, |
| 2352 | webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 2353 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 2354 | session_->StartGettingPorts(); |
| 2355 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2356 | kDefaultAllocationTimeout, fake_clock); |
| 2357 | CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(), |
| 2358 | expected_stun_keepalive_interval); |
| 2359 | } |
| 2360 | |
| 2361 | TEST_F(BasicPortAllocatorTest, |
| 2362 | SetStunKeepaliveIntervalForPortsWithoutSharedSocket) { |
| 2363 | const int pool_size = 1; |
| 2364 | const int expected_stun_keepalive_interval = 123; |
| 2365 | AddInterface(kClientAddr); |
| 2366 | allocator_->set_flags(allocator().flags() & |
| 2367 | ~(PORTALLOCATOR_ENABLE_SHARED_SOCKET)); |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 2368 | allocator_->SetConfiguration( |
| 2369 | allocator_->stun_servers(), allocator_->turn_servers(), pool_size, |
| 2370 | webrtc::NO_PRUNE, nullptr, expected_stun_keepalive_interval); |
Qingsi Wang | e53ac04 | 2018-05-08 18:55:07 | [diff] [blame] | 2371 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
Qingsi Wang | 4ff5443 | 2018-03-02 02:25:20 | [diff] [blame] | 2372 | session_->StartGettingPorts(); |
| 2373 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2374 | kDefaultAllocationTimeout, fake_clock); |
| 2375 | CheckStunKeepaliveIntervalOfAllReadyPorts(session_.get(), |
| 2376 | expected_stun_keepalive_interval); |
| 2377 | } |
| 2378 | |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 2379 | // Test that when an mDNS responder is present, the local address of a host |
Jeroen de Borst | 72d2ddd | 2018-11-27 21:20:39 | [diff] [blame] | 2380 | // candidate is concealed by an mDNS hostname and the related address of a srflx |
| 2381 | // candidate is set to 0.0.0.0 or ::0. |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 2382 | TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) { |
| 2383 | // Default config uses GTURN and no NAT, so replace that with the |
| 2384 | // desired setup (NAT, STUN server, TURN server, UDP/TCP). |
| 2385 | ResetWithStunServerAndNat(kStunAddr); |
| 2386 | turn_server_.AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 2387 | AddTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 2388 | AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr); |
| 2389 | |
| 2390 | ASSERT_EQ(&network_manager_, allocator().network_manager()); |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [diff] [blame] | 2391 | network_manager_.set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 2392 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 2393 | AddInterface(kClientAddr); |
| 2394 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 2395 | session_->StartGettingPorts(); |
| 2396 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2397 | kDefaultAllocationTimeout, fake_clock); |
| 2398 | EXPECT_EQ(5u, candidates_.size()); |
| 2399 | int num_host_udp_candidates = 0; |
| 2400 | int num_host_tcp_candidates = 0; |
| 2401 | int num_srflx_candidates = 0; |
| 2402 | int num_relay_candidates = 0; |
| 2403 | for (const auto& candidate : candidates_) { |
Jeroen de Borst | 72d2ddd | 2018-11-27 21:20:39 | [diff] [blame] | 2404 | const auto& raddr = candidate.related_address(); |
| 2405 | |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 2406 | if (candidate.is_local()) { |
Jeroen de Borst | 72d2ddd | 2018-11-27 21:20:39 | [diff] [blame] | 2407 | EXPECT_FALSE(candidate.address().hostname().empty()); |
| 2408 | EXPECT_TRUE(raddr.IsNil()); |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 2409 | if (candidate.protocol() == UDP_PROTOCOL_NAME) { |
| 2410 | ++num_host_udp_candidates; |
| 2411 | } else { |
| 2412 | ++num_host_tcp_candidates; |
| 2413 | } |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 2414 | } else if (candidate.is_stun()) { |
Jeroen de Borst | 72d2ddd | 2018-11-27 21:20:39 | [diff] [blame] | 2415 | // For a srflx candidate, the related address should be set to 0.0.0.0 or |
| 2416 | // ::0 |
| 2417 | EXPECT_TRUE(IPIsAny(raddr.ipaddr())); |
| 2418 | EXPECT_EQ(raddr.port(), 0); |
| 2419 | ++num_srflx_candidates; |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 2420 | } else if (candidate.is_relay()) { |
Jeroen de Borst | 72d2ddd | 2018-11-27 21:20:39 | [diff] [blame] | 2421 | EXPECT_EQ(kNatUdpAddr.ipaddr(), raddr.ipaddr()); |
| 2422 | EXPECT_EQ(kNatUdpAddr.family(), raddr.family()); |
| 2423 | ++num_relay_candidates; |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 2424 | } else { |
Jeroen de Borst | 72d2ddd | 2018-11-27 21:20:39 | [diff] [blame] | 2425 | // prflx candidates are not expected |
| 2426 | FAIL(); |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 2427 | } |
| 2428 | } |
| 2429 | EXPECT_EQ(1, num_host_udp_candidates); |
| 2430 | EXPECT_EQ(1, num_host_tcp_candidates); |
| 2431 | EXPECT_EQ(1, num_srflx_candidates); |
| 2432 | EXPECT_EQ(2, num_relay_candidates); |
| 2433 | } |
| 2434 | |
Honghai Zhang | 6981fb5 | 2019-10-29 19:45:34 | [diff] [blame] | 2435 | TEST_F(BasicPortAllocatorTest, TestUseTurnServerAsStunSever) { |
| 2436 | ServerAddresses stun_servers; |
| 2437 | stun_servers.insert(kStunAddr); |
| 2438 | PortConfiguration port_config(stun_servers, "", ""); |
| 2439 | RelayServerConfig turn_servers = |
| 2440 | CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 2441 | port_config.AddRelay(turn_servers); |
| 2442 | |
| 2443 | EXPECT_EQ(2U, port_config.StunServers().size()); |
| 2444 | } |
| 2445 | |
| 2446 | TEST_F(BasicPortAllocatorTest, TestDoNotUseTurnServerAsStunSever) { |
Jonas Oreland | 340cb5e | 2022-03-17 08:14:16 | [diff] [blame] | 2447 | webrtc::test::ScopedKeyValueConfig field_trials( |
Honghai Zhang | 6981fb5 | 2019-10-29 19:45:34 | [diff] [blame] | 2448 | "WebRTC-UseTurnServerAsStunServer/Disabled/"); |
| 2449 | ServerAddresses stun_servers; |
| 2450 | stun_servers.insert(kStunAddr); |
| 2451 | PortConfiguration port_config(stun_servers, "" /* user_name */, |
Jonas Oreland | 340cb5e | 2022-03-17 08:14:16 | [diff] [blame] | 2452 | "" /* password */, &field_trials); |
Honghai Zhang | 6981fb5 | 2019-10-29 19:45:34 | [diff] [blame] | 2453 | RelayServerConfig turn_servers = |
| 2454 | CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); |
| 2455 | port_config.AddRelay(turn_servers); |
| 2456 | |
| 2457 | EXPECT_EQ(1U, port_config.StunServers().size()); |
| 2458 | } |
| 2459 | |
Jonas Oreland | 3e7e15d | 2022-11-03 09:02:55 | [diff] [blame] | 2460 | // Test that candidates from different servers get assigned a unique local |
| 2461 | // preference (the middle 16 bits of the priority) |
| 2462 | TEST_F(BasicPortAllocatorTest, AssignsUniqueLocalPreferencetoRelayCandidates) { |
| 2463 | allocator_->SetCandidateFilter(CF_RELAY); |
| 2464 | allocator_->AddTurnServerForTesting( |
| 2465 | CreateTurnServers(kTurnUdpIntAddr, SocketAddress())); |
| 2466 | allocator_->AddTurnServerForTesting( |
| 2467 | CreateTurnServers(kTurnUdpIntAddr, SocketAddress())); |
| 2468 | allocator_->AddTurnServerForTesting( |
| 2469 | CreateTurnServers(kTurnUdpIntAddr, SocketAddress())); |
| 2470 | |
| 2471 | AddInterface(kClientAddr); |
| 2472 | ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); |
| 2473 | session_->StartGettingPorts(); |
| 2474 | ASSERT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2475 | kDefaultAllocationTimeout, fake_clock); |
| 2476 | EXPECT_EQ(3u, candidates_.size()); |
| 2477 | EXPECT_GT((candidates_[0].priority() >> 8) & 0xFFFF, |
| 2478 | (candidates_[1].priority() >> 8) & 0xFFFF); |
| 2479 | EXPECT_GT((candidates_[1].priority() >> 8) & 0xFFFF, |
| 2480 | (candidates_[2].priority() >> 8) & 0xFFFF); |
| 2481 | } |
| 2482 | |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 2483 | // Test that no more than allocator.max_ipv6_networks() IPv6 networks are used |
| 2484 | // to gather candidates. |
| 2485 | TEST_F(BasicPortAllocatorTest, TwoIPv6AreSelectedBecauseOfMaxIpv6Limit) { |
| 2486 | rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(), |
| 2487 | 64, rtc::ADAPTER_TYPE_WIFI); |
| 2488 | rtc::Network ethe1("ethe1", "Test NetworkAdapter 2", |
| 2489 | kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET); |
| 2490 | rtc::Network wifi2("wifi2", "Test NetworkAdapter 3", |
| 2491 | kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI); |
| 2492 | std::vector<const rtc::Network*> networks = {&wifi1, ðe1, &wifi2}; |
| 2493 | |
| 2494 | // Ensure that only 2 interfaces were selected. |
| 2495 | EXPECT_EQ(2U, BasicPortAllocatorSession::SelectIPv6Networks( |
| 2496 | networks, /*max_ipv6_networks=*/2) |
| 2497 | .size()); |
| 2498 | } |
| 2499 | |
| 2500 | // Test that if the number of available IPv6 networks is less than |
| 2501 | // allocator.max_ipv6_networks(), all IPv6 networks will be selected. |
| 2502 | TEST_F(BasicPortAllocatorTest, AllIPv6AreSelected) { |
| 2503 | rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(), |
| 2504 | 64, rtc::ADAPTER_TYPE_WIFI); |
| 2505 | rtc::Network ethe1("ethe1", "Test NetworkAdapter 2", |
| 2506 | kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET); |
| 2507 | std::vector<const rtc::Network*> networks = {&wifi1, ðe1}; |
| 2508 | |
| 2509 | // Ensure that all 2 interfaces were selected. |
| 2510 | EXPECT_EQ(2U, BasicPortAllocatorSession::SelectIPv6Networks( |
| 2511 | networks, /*max_ipv6_networks=*/3) |
| 2512 | .size()); |
| 2513 | } |
| 2514 | |
| 2515 | // If there are some IPv6 networks with different types, diversify IPv6 |
| 2516 | // networks. |
| 2517 | TEST_F(BasicPortAllocatorTest, TwoIPv6WifiAreSelectedIfThereAreTwo) { |
| 2518 | rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(), |
| 2519 | 64, rtc::ADAPTER_TYPE_WIFI); |
| 2520 | rtc::Network ethe1("ethe1", "Test NetworkAdapter 2", |
| 2521 | kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET); |
| 2522 | rtc::Network ethe2("ethe2", "Test NetworkAdapter 3", |
| 2523 | kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET); |
| 2524 | rtc::Network unknown1("unknown1", "Test NetworkAdapter 4", |
| 2525 | kClientIPv6Addr2.ipaddr(), 64, |
| 2526 | rtc::ADAPTER_TYPE_UNKNOWN); |
| 2527 | rtc::Network cell1("cell1", "Test NetworkAdapter 5", |
| 2528 | kClientIPv6Addr3.ipaddr(), 64, |
| 2529 | rtc::ADAPTER_TYPE_CELLULAR_4G); |
| 2530 | std::vector<const rtc::Network*> networks = {&wifi1, ðe1, ðe2, |
| 2531 | &unknown1, &cell1}; |
| 2532 | |
| 2533 | networks = BasicPortAllocatorSession::SelectIPv6Networks( |
| 2534 | networks, /*max_ipv6_networks=*/4); |
| 2535 | |
| 2536 | EXPECT_EQ(4U, networks.size()); |
| 2537 | // Ensure the expected 4 interfaces (wifi1, ethe1, cell1, unknown1) were |
| 2538 | // selected. |
| 2539 | EXPECT_TRUE(HasNetwork(networks, wifi1)); |
| 2540 | EXPECT_TRUE(HasNetwork(networks, ethe1)); |
| 2541 | EXPECT_TRUE(HasNetwork(networks, cell1)); |
| 2542 | EXPECT_TRUE(HasNetwork(networks, unknown1)); |
| 2543 | } |
| 2544 | |
| 2545 | // If there are some IPv6 networks with the same type, select them because there |
| 2546 | // is no other option. |
| 2547 | TEST_F(BasicPortAllocatorTest, IPv6WithSameTypeAreSelectedIfNoOtherOption) { |
| 2548 | // Add 5 cellular interfaces |
| 2549 | rtc::Network cell1("cell1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(), |
| 2550 | 64, rtc::ADAPTER_TYPE_CELLULAR_2G); |
| 2551 | rtc::Network cell2("cell2", "Test NetworkAdapter 2", |
| 2552 | kClientIPv6Addr2.ipaddr(), 64, |
| 2553 | rtc::ADAPTER_TYPE_CELLULAR_3G); |
| 2554 | rtc::Network cell3("cell3", "Test NetworkAdapter 3", |
| 2555 | kClientIPv6Addr3.ipaddr(), 64, |
| 2556 | rtc::ADAPTER_TYPE_CELLULAR_4G); |
| 2557 | rtc::Network cell4("cell4", "Test NetworkAdapter 4", |
| 2558 | kClientIPv6Addr2.ipaddr(), 64, |
| 2559 | rtc::ADAPTER_TYPE_CELLULAR_5G); |
| 2560 | rtc::Network cell5("cell5", "Test NetworkAdapter 5", |
| 2561 | kClientIPv6Addr3.ipaddr(), 64, |
| 2562 | rtc::ADAPTER_TYPE_CELLULAR_3G); |
| 2563 | std::vector<const rtc::Network*> networks = {&cell1, &cell2, &cell3, &cell4, |
| 2564 | &cell5}; |
| 2565 | |
| 2566 | // Ensure that 4 interfaces were selected. |
| 2567 | EXPECT_EQ(4U, BasicPortAllocatorSession::SelectIPv6Networks( |
| 2568 | networks, /*max_ipv6_networks=*/4) |
| 2569 | .size()); |
| 2570 | } |
| 2571 | |
| 2572 | TEST_F(BasicPortAllocatorTest, IPv6EthernetHasHigherPriorityThanWifi) { |
| 2573 | rtc::Network wifi1("wifi1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(), |
| 2574 | 64, rtc::ADAPTER_TYPE_WIFI); |
| 2575 | rtc::Network ethe1("ethe1", "Test NetworkAdapter 2", |
| 2576 | kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET); |
| 2577 | rtc::Network wifi2("wifi2", "Test NetworkAdapter 3", |
| 2578 | kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI); |
| 2579 | std::vector<const rtc::Network*> networks = {&wifi1, ðe1, &wifi2}; |
| 2580 | |
| 2581 | networks = BasicPortAllocatorSession::SelectIPv6Networks( |
| 2582 | networks, /*max_ipv6_networks=*/1); |
| 2583 | |
| 2584 | EXPECT_EQ(1U, networks.size()); |
| 2585 | // Ensure ethe1 was selected. |
| 2586 | EXPECT_TRUE(HasNetwork(networks, ethe1)); |
| 2587 | } |
| 2588 | |
| 2589 | TEST_F(BasicPortAllocatorTest, IPv6EtherAndWifiHaveHigherPriorityThanOthers) { |
| 2590 | rtc::Network cell1("cell1", "Test NetworkAdapter 1", kClientIPv6Addr.ipaddr(), |
| 2591 | 64, rtc::ADAPTER_TYPE_CELLULAR_3G); |
| 2592 | rtc::Network ethe1("ethe1", "Test NetworkAdapter 2", |
| 2593 | kClientIPv6Addr2.ipaddr(), 64, rtc::ADAPTER_TYPE_ETHERNET); |
| 2594 | rtc::Network wifi1("wifi1", "Test NetworkAdapter 3", |
| 2595 | kClientIPv6Addr3.ipaddr(), 64, rtc::ADAPTER_TYPE_WIFI); |
| 2596 | rtc::Network unknown("unknown", "Test NetworkAdapter 4", |
| 2597 | kClientIPv6Addr2.ipaddr(), 64, |
| 2598 | rtc::ADAPTER_TYPE_UNKNOWN); |
| 2599 | rtc::Network vpn1("vpn1", "Test NetworkAdapter 5", kClientIPv6Addr3.ipaddr(), |
| 2600 | 64, rtc::ADAPTER_TYPE_VPN); |
| 2601 | std::vector<const rtc::Network*> networks = {&cell1, ðe1, &wifi1, &unknown, |
| 2602 | &vpn1}; |
| 2603 | |
| 2604 | networks = BasicPortAllocatorSession::SelectIPv6Networks( |
| 2605 | networks, /*max_ipv6_networks=*/2); |
| 2606 | |
| 2607 | EXPECT_EQ(2U, networks.size()); |
| 2608 | // Ensure ethe1 and wifi1 were selected. |
| 2609 | EXPECT_TRUE(HasNetwork(networks, wifi1)); |
| 2610 | EXPECT_TRUE(HasNetwork(networks, ethe1)); |
| 2611 | } |
| 2612 | |
Sameer Vijaykar | df7df19 | 2023-04-24 09:05:44 | [diff] [blame] | 2613 | TEST_F(BasicPortAllocatorTest, Select2DifferentIntefaces) { |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 2614 | allocator().set_max_ipv6_networks(2); |
| 2615 | AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET); |
| 2616 | AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET); |
| 2617 | AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2618 | AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI); |
| 2619 | AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G); |
| 2620 | |
| 2621 | // To simplify the test, only gather UDP host candidates. |
| 2622 | allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP | |
| 2623 | PORTALLOCATOR_DISABLE_STUN | |
| 2624 | PORTALLOCATOR_DISABLE_RELAY | |
| 2625 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 2626 | |
| 2627 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
| 2628 | session_->StartGettingPorts(); |
| 2629 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2630 | kDefaultAllocationTimeout, fake_clock); |
| 2631 | |
| 2632 | EXPECT_EQ(2U, candidates_.size()); |
| 2633 | // ethe1 and wifi1 were selected. |
| 2634 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr)); |
| 2635 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3)); |
| 2636 | } |
| 2637 | |
Sameer Vijaykar | df7df19 | 2023-04-24 09:05:44 | [diff] [blame] | 2638 | TEST_F(BasicPortAllocatorTest, Select3DifferentIntefaces) { |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 2639 | allocator().set_max_ipv6_networks(3); |
| 2640 | AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET); |
| 2641 | AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET); |
| 2642 | AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2643 | AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI); |
| 2644 | AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G); |
| 2645 | |
| 2646 | // To simplify the test, only gather UDP host candidates. |
| 2647 | allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP | |
| 2648 | PORTALLOCATOR_DISABLE_STUN | |
| 2649 | PORTALLOCATOR_DISABLE_RELAY | |
| 2650 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 2651 | |
| 2652 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
| 2653 | session_->StartGettingPorts(); |
| 2654 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2655 | kDefaultAllocationTimeout, fake_clock); |
| 2656 | |
| 2657 | EXPECT_EQ(3U, candidates_.size()); |
| 2658 | // ethe1, wifi1, and cell1 were selected. |
| 2659 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr)); |
| 2660 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3)); |
| 2661 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr5)); |
| 2662 | } |
| 2663 | |
Sameer Vijaykar | df7df19 | 2023-04-24 09:05:44 | [diff] [blame] | 2664 | TEST_F(BasicPortAllocatorTest, Select4DifferentIntefaces) { |
Diep Bui | 9068f456 | 2022-08-26 15:43:44 | [diff] [blame] | 2665 | allocator().set_max_ipv6_networks(4); |
| 2666 | AddInterface(kClientIPv6Addr, "ethe1", rtc::ADAPTER_TYPE_ETHERNET); |
| 2667 | AddInterface(kClientIPv6Addr2, "ethe2", rtc::ADAPTER_TYPE_ETHERNET); |
| 2668 | AddInterface(kClientIPv6Addr3, "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2669 | AddInterface(kClientIPv6Addr4, "wifi2", rtc::ADAPTER_TYPE_WIFI); |
| 2670 | AddInterface(kClientIPv6Addr5, "cell1", rtc::ADAPTER_TYPE_CELLULAR_3G); |
| 2671 | |
| 2672 | // To simplify the test, only gather UDP host candidates. |
| 2673 | allocator().set_flags(PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_DISABLE_TCP | |
| 2674 | PORTALLOCATOR_DISABLE_STUN | |
| 2675 | PORTALLOCATOR_DISABLE_RELAY | |
| 2676 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 2677 | |
| 2678 | ASSERT_TRUE(CreateSession(cricket::ICE_CANDIDATE_COMPONENT_RTP)); |
| 2679 | session_->StartGettingPorts(); |
| 2680 | EXPECT_TRUE_SIMULATED_WAIT(candidate_allocation_done_, |
| 2681 | kDefaultAllocationTimeout, fake_clock); |
| 2682 | |
| 2683 | EXPECT_EQ(4U, candidates_.size()); |
| 2684 | // ethe1, ethe2, wifi1, and cell1 were selected. |
| 2685 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr)); |
| 2686 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr2)); |
| 2687 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr3)); |
| 2688 | EXPECT_TRUE(HasCandidate(candidates_, "local", "udp", kClientIPv6Addr5)); |
| 2689 | } |
| 2690 | |
Taylor Brandstetter | 417eebe | 2016-05-23 23:02:19 | [diff] [blame] | 2691 | } // namespace cricket |