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