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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "p2p/base/p2p_transport_channel.h" |
| 12 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 13 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 14 | #include <memory> |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 15 | #include <utility> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 16 | |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 17 | #include "p2p/base/connection.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "p2p/base/fake_port_allocator.h" |
| 19 | #include "p2p/base/ice_transport_internal.h" |
| 20 | #include "p2p/base/mock_async_resolver.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "p2p/base/packet_transport_internal.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "p2p/base/test_stun_server.h" |
| 23 | #include "p2p/base/test_turn_server.h" |
| 24 | #include "p2p/client/basic_port_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/dscp.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/fake_clock.h" |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 28 | #include "rtc_base/fake_mdns_responder.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/fake_network.h" |
| 30 | #include "rtc_base/firewall_socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/gunit.h" |
| 32 | #include "rtc_base/helpers.h" |
| 33 | #include "rtc_base/logging.h" |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 34 | #include "rtc_base/mdns_responder_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "rtc_base/nat_server.h" |
| 36 | #include "rtc_base/nat_socket_factory.h" |
| 37 | #include "rtc_base/proxy_server.h" |
| 38 | #include "rtc_base/socket_address.h" |
| 39 | #include "rtc_base/ssl_adapter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 41 | #include "rtc_base/virtual_socket_server.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 08:51:10 +0200 | [diff] [blame] | 42 | #include "system_wrappers/include/metrics.h" |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 43 | #include "test/field_trial.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 44 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | using rtc::SocketAddress; |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 48 | using ::testing::_; |
Zach Stein | 5ec2c94 | 2018-11-27 16:58:08 -0800 | [diff] [blame] | 49 | using ::testing::Assign; |
Steve Anton | ae226f6 | 2019-01-29 12:47:38 -0800 | [diff] [blame] | 50 | using ::testing::Contains; |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 51 | using ::testing::DoAll; |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 52 | using ::testing::InSequence; |
| 53 | using ::testing::InvokeWithoutArgs; |
| 54 | using ::testing::NiceMock; |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 55 | using ::testing::Return; |
| 56 | using ::testing::SetArgPointee; |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 57 | using ::testing::SizeIs; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 59 | // Default timeout for tests in this file. |
| 60 | // Should be large enough for slow buildbots to run the tests reliably. |
| 61 | static const int kDefaultTimeout = 10000; |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 62 | static const int kMediumTimeout = 3000; |
| 63 | static const int kShortTimeout = 1000; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 64 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | static const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN | |
| 66 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 67 | cricket::PORTALLOCATOR_DISABLE_TCP; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 68 | static const int LOW_RTT = 20; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | // Addresses on the public internet. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | static const SocketAddress kPublicAddrs[2] = {SocketAddress("11.11.11.11", 0), |
| 71 | SocketAddress("22.22.22.22", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | // IPv6 Addresses on the public internet. |
| 73 | static const SocketAddress kIPv6PublicAddrs[2] = { |
| 74 | SocketAddress("2400:4030:1:2c00:be30:abcd:efab:cdef", 0), |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 75 | SocketAddress("2600:0:1000:1b03:2e41:38ff:fea6:f2a4", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | // For configuring multihomed clients. |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 77 | static const SocketAddress kAlternateAddrs[2] = { |
| 78 | SocketAddress("101.101.101.101", 0), SocketAddress("202.202.202.202", 0)}; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 79 | static const SocketAddress kIPv6AlternateAddrs[2] = { |
| 80 | SocketAddress("2401:4030:1:2c00:be30:abcd:efab:cdef", 0), |
| 81 | SocketAddress("2601:0:1000:1b03:2e41:38ff:fea6:f2a4", 0)}; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 82 | // Addresses for HTTP proxy servers. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 83 | static const SocketAddress kHttpsProxyAddrs[2] = { |
| 84 | SocketAddress("11.11.11.1", 443), SocketAddress("22.22.22.1", 443)}; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 85 | // Addresses for SOCKS proxy servers. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 86 | static const SocketAddress kSocksProxyAddrs[2] = { |
| 87 | SocketAddress("11.11.11.1", 1080), SocketAddress("22.22.22.1", 1080)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 88 | // Internal addresses for NAT boxes. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | static const SocketAddress kNatAddrs[2] = {SocketAddress("192.168.1.1", 0), |
| 90 | SocketAddress("192.168.2.1", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | // Private addresses inside the NAT private networks. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | static const SocketAddress kPrivateAddrs[2] = { |
| 93 | SocketAddress("192.168.1.11", 0), SocketAddress("192.168.2.22", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 94 | // For cascaded NATs, the internal addresses of the inner NAT boxes. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 95 | static const SocketAddress kCascadedNatAddrs[2] = { |
| 96 | SocketAddress("192.168.10.1", 0), SocketAddress("192.168.20.1", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | // For cascaded NATs, private addresses inside the inner private networks. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | static const SocketAddress kCascadedPrivateAddrs[2] = { |
| 99 | SocketAddress("192.168.10.11", 0), SocketAddress("192.168.20.22", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 100 | // The address of the public STUN server. |
| 101 | static const SocketAddress kStunAddr("99.99.99.1", cricket::STUN_SERVER_PORT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | // The addresses for the public turn server. |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 103 | static const SocketAddress kTurnUdpIntAddr("99.99.99.3", |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 104 | cricket::STUN_SERVER_PORT); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 105 | static const SocketAddress kTurnTcpIntAddr("99.99.99.4", |
| 106 | cricket::STUN_SERVER_PORT + 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); |
| 108 | static const cricket::RelayCredentials kRelayCredentials("test", "test"); |
| 109 | |
| 110 | // Based on ICE_UFRAG_LENGTH |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 111 | const char* kIceUfrag[4] = {"UF00", "UF01", "UF02", "UF03"}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 112 | // Based on ICE_PWD_LENGTH |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 113 | const char* kIcePwd[4] = { |
| 114 | "TESTICEPWD00000000000000", "TESTICEPWD00000000000001", |
| 115 | "TESTICEPWD00000000000002", "TESTICEPWD00000000000003"}; |
| 116 | const cricket::IceParameters kIceParams[4] = { |
| 117 | {kIceUfrag[0], kIcePwd[0], false}, |
| 118 | {kIceUfrag[1], kIcePwd[1], false}, |
| 119 | {kIceUfrag[2], kIcePwd[2], false}, |
| 120 | {kIceUfrag[3], kIcePwd[3], false}}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 121 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 122 | const uint64_t kLowTiebreaker = 11111; |
| 123 | const uint64_t kHighTiebreaker = 22222; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 125 | enum { MSG_ADD_CANDIDATES, MSG_REMOVE_CANDIDATES }; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 126 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 127 | cricket::IceConfig CreateIceConfig( |
| 128 | int receiving_timeout, |
| 129 | cricket::ContinualGatheringPolicy continual_gathering_policy, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 130 | absl::optional<int> backup_ping_interval = absl::nullopt) { |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 131 | cricket::IceConfig config; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 132 | config.receiving_timeout = receiving_timeout; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 133 | config.continual_gathering_policy = continual_gathering_policy; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 134 | config.backup_connection_ping_interval = backup_ping_interval; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 135 | return config; |
| 136 | } |
| 137 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 138 | cricket::Candidate CreateUdpCandidate(const std::string& type, |
| 139 | const std::string& ip, |
| 140 | int port, |
| 141 | int priority, |
| 142 | const std::string& ufrag = "") { |
| 143 | cricket::Candidate c; |
| 144 | c.set_address(rtc::SocketAddress(ip, port)); |
| 145 | c.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 146 | c.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 147 | c.set_priority(priority); |
| 148 | c.set_username(ufrag); |
| 149 | c.set_type(type); |
| 150 | return c; |
| 151 | } |
| 152 | |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 153 | cricket::BasicPortAllocator* CreateBasicPortAllocator( |
| 154 | rtc::NetworkManager* network_manager, |
| 155 | const cricket::ServerAddresses& stun_servers, |
| 156 | const rtc::SocketAddress& turn_server_udp, |
| 157 | const rtc::SocketAddress& turn_server_tcp) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 158 | cricket::RelayServerConfig turn_server; |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 159 | turn_server.credentials = kRelayCredentials; |
| 160 | if (!turn_server_udp.IsNil()) { |
| 161 | turn_server.ports.push_back( |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 162 | cricket::ProtocolAddress(turn_server_udp, cricket::PROTO_UDP)); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 163 | } |
| 164 | if (!turn_server_tcp.IsNil()) { |
| 165 | turn_server.ports.push_back( |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 166 | cricket::ProtocolAddress(turn_server_tcp, cricket::PROTO_TCP)); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 167 | } |
| 168 | std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server); |
| 169 | |
| 170 | cricket::BasicPortAllocator* allocator = |
| 171 | new cricket::BasicPortAllocator(network_manager); |
Qingsi Wang | a2d6067 | 2018-04-11 16:57:45 -0700 | [diff] [blame] | 172 | allocator->Initialize(); |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 173 | allocator->SetConfiguration(stun_servers, turn_servers, 0, webrtc::NO_PRUNE); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 174 | return allocator; |
| 175 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 176 | } // namespace |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 177 | |
| 178 | namespace cricket { |
| 179 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 180 | // This test simulates 2 P2P endpoints that want to establish connectivity |
| 181 | // with each other over various network topologies and conditions, which can be |
| 182 | // specified in each individial test. |
| 183 | // A virtual network (via VirtualSocketServer) along with virtual firewalls and |
| 184 | // NATs (via Firewall/NATSocketServer) are used to simulate the various network |
| 185 | // conditions. We can configure the IP addresses of the endpoints, |
| 186 | // block various types of connectivity, or add arbitrary levels of NAT. |
| 187 | // We also run a STUN server and a relay server on the virtual network to allow |
| 188 | // our typical P2P mechanisms to do their thing. |
| 189 | // For each case, we expect the P2P stack to eventually settle on a specific |
| 190 | // form of connectivity to the other side. The test checks that the P2P |
| 191 | // negotiation successfully establishes connectivity within a certain time, |
| 192 | // and that the result is what we expect. |
| 193 | // Note that this class is a base class for use by other tests, who will provide |
| 194 | // specialized test behavior. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 195 | class P2PTransportChannelTestBase : public ::testing::Test, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 196 | public rtc::MessageHandler, |
| 197 | public sigslot::has_slots<> { |
| 198 | public: |
| 199 | P2PTransportChannelTestBase() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 200 | : vss_(new rtc::VirtualSocketServer()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 201 | nss_(new rtc::NATSocketServer(vss_.get())), |
| 202 | ss_(new rtc::FirewallSocketServer(nss_.get())), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 203 | main_(ss_.get()), |
| 204 | stun_server_(TestStunServer::Create(&main_, kStunAddr)), |
| 205 | turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 206 | socks_server1_(ss_.get(), |
| 207 | kSocksProxyAddrs[0], |
| 208 | ss_.get(), |
| 209 | kSocksProxyAddrs[0]), |
| 210 | socks_server2_(ss_.get(), |
| 211 | kSocksProxyAddrs[1], |
| 212 | ss_.get(), |
| 213 | kSocksProxyAddrs[1]), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 214 | force_relay_(false) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 215 | ep1_.role_ = ICEROLE_CONTROLLING; |
| 216 | ep2_.role_ = ICEROLE_CONTROLLED; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 217 | |
| 218 | ServerAddresses stun_servers; |
| 219 | stun_servers.insert(kStunAddr); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 220 | ep1_.allocator_.reset( |
| 221 | CreateBasicPortAllocator(&ep1_.network_manager_, stun_servers, |
| 222 | kTurnUdpIntAddr, rtc::SocketAddress())); |
| 223 | ep2_.allocator_.reset( |
| 224 | CreateBasicPortAllocator(&ep2_.network_manager_, stun_servers, |
| 225 | kTurnUdpIntAddr, rtc::SocketAddress())); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 226 | webrtc::metrics::Reset(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | protected: |
| 230 | enum Config { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 231 | OPEN, // Open to the Internet |
| 232 | NAT_FULL_CONE, // NAT, no filtering |
| 233 | NAT_ADDR_RESTRICTED, // NAT, must send to an addr to recv |
| 234 | NAT_PORT_RESTRICTED, // NAT, must send to an addr+port to recv |
| 235 | NAT_SYMMETRIC, // NAT, endpoint-dependent bindings |
| 236 | NAT_DOUBLE_CONE, // Double NAT, both cone |
| 237 | NAT_SYMMETRIC_THEN_CONE, // Double NAT, symmetric outer, cone inner |
| 238 | BLOCK_UDP, // Firewall, UDP in/out blocked |
| 239 | BLOCK_UDP_AND_INCOMING_TCP, // Firewall, UDP in/out and TCP in blocked |
| 240 | BLOCK_ALL_BUT_OUTGOING_HTTP, // Firewall, only TCP out on 80/443 |
| 241 | PROXY_HTTPS, // All traffic through HTTPS proxy |
| 242 | PROXY_SOCKS, // All traffic through SOCKS proxy |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 243 | NUM_CONFIGS |
| 244 | }; |
| 245 | |
| 246 | struct Result { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 247 | Result(const std::string& controlling_type, |
| 248 | const std::string& controlling_protocol, |
| 249 | const std::string& controlled_type, |
| 250 | const std::string& controlled_protocol, |
| 251 | int wait) |
| 252 | : controlling_type(controlling_type), |
| 253 | controlling_protocol(controlling_protocol), |
| 254 | controlled_type(controlled_type), |
| 255 | controlled_protocol(controlled_protocol), |
| 256 | connect_wait(wait) {} |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 257 | |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 258 | // The expected candidate type and protocol of the controlling ICE agent. |
| 259 | std::string controlling_type; |
| 260 | std::string controlling_protocol; |
| 261 | // The expected candidate type and protocol of the controlled ICE agent. |
| 262 | std::string controlled_type; |
| 263 | std::string controlled_protocol; |
| 264 | // How long to wait before the correct candidate pair is selected. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 265 | int connect_wait; |
| 266 | }; |
| 267 | |
| 268 | struct ChannelData { |
| 269 | bool CheckData(const char* data, int len) { |
| 270 | bool ret = false; |
| 271 | if (!ch_packets_.empty()) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 272 | std::string packet = ch_packets_.front(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 273 | ret = (packet == std::string(data, len)); |
| 274 | ch_packets_.pop_front(); |
| 275 | } |
| 276 | return ret; |
| 277 | } |
| 278 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 279 | std::string name_; // TODO(?) - Currently not used. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 280 | std::list<std::string> ch_packets_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 281 | std::unique_ptr<P2PTransportChannel> ch_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 284 | struct CandidatesData : public rtc::MessageData { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 285 | CandidatesData(IceTransportInternal* ch, const Candidate& c) |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 286 | : channel(ch), candidates(1, c) {} |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 287 | CandidatesData(IceTransportInternal* ch, const std::vector<Candidate>& cc) |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 288 | : channel(ch), candidates(cc) {} |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 289 | IceTransportInternal* channel; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 290 | Candidates candidates; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 293 | struct Endpoint : public sigslot::has_slots<> { |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 294 | Endpoint() |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 295 | : role_(ICEROLE_UNKNOWN), |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 296 | tiebreaker_(0), |
| 297 | role_conflict_(false), |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 298 | save_candidates_(false) {} |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 299 | bool HasTransport(const rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 300 | return (transport == cd1_.ch_.get() || transport == cd2_.ch_.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | } |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 302 | ChannelData* GetChannelData(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 303 | if (!HasTransport(transport)) |
| 304 | return NULL; |
| 305 | if (cd1_.ch_.get() == transport) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 306 | return &cd1_; |
| 307 | else |
| 308 | return &cd2_; |
| 309 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 310 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 311 | void SetIceRole(IceRole role) { role_ = role; } |
| 312 | IceRole ice_role() { return role_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 313 | void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; } |
| 314 | uint64_t GetIceTiebreaker() { return tiebreaker_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | void OnRoleConflict(bool role_conflict) { role_conflict_ = role_conflict; } |
| 316 | bool role_conflict() { return role_conflict_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 317 | void SetAllocationStepDelay(uint32_t delay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | allocator_->set_step_delay(delay); |
| 319 | } |
| 320 | void SetAllowTcpListen(bool allow_tcp_listen) { |
| 321 | allocator_->set_allow_tcp_listen(allow_tcp_listen); |
| 322 | } |
| 323 | |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 324 | void OnIceRegathering(PortAllocatorSession*, IceRegatheringReason reason) { |
| 325 | ++ice_regathering_counter_[reason]; |
| 326 | } |
| 327 | |
| 328 | int GetIceRegatheringCountForReason(IceRegatheringReason reason) { |
| 329 | return ice_regathering_counter_[reason]; |
| 330 | } |
| 331 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | rtc::FakeNetworkManager network_manager_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 333 | std::unique_ptr<BasicPortAllocator> allocator_; |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 334 | webrtc::AsyncResolverFactory* async_resolver_factory_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 335 | ChannelData cd1_; |
| 336 | ChannelData cd2_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 337 | IceRole role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 338 | uint64_t tiebreaker_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 339 | bool role_conflict_; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 340 | bool save_candidates_; |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 341 | std::vector<std::unique_ptr<CandidatesData>> saved_candidates_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 342 | bool ready_to_send_ = false; |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 343 | std::map<IceRegatheringReason, int> ice_regathering_counter_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 346 | ChannelData* GetChannelData(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 347 | if (ep1_.HasTransport(transport)) |
| 348 | return ep1_.GetChannelData(transport); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 349 | else |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 350 | return ep2_.GetChannelData(transport); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 353 | IceParameters IceParamsWithRenomination(const IceParameters& ice, |
| 354 | bool renomination) { |
| 355 | IceParameters new_ice = ice; |
| 356 | new_ice.renomination = renomination; |
| 357 | return new_ice; |
| 358 | } |
| 359 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 360 | void CreateChannels(const IceConfig& ep1_config, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 361 | const IceConfig& ep2_config, |
| 362 | bool renomination = false) { |
| 363 | IceParameters ice_ep1_cd1_ch = |
| 364 | IceParamsWithRenomination(kIceParams[0], renomination); |
| 365 | IceParameters ice_ep2_cd1_ch = |
| 366 | IceParamsWithRenomination(kIceParams[1], renomination); |
| 367 | ep1_.cd1_.ch_.reset(CreateChannel(0, ICE_CANDIDATE_COMPONENT_DEFAULT, |
| 368 | ice_ep1_cd1_ch, ice_ep2_cd1_ch)); |
| 369 | ep2_.cd1_.ch_.reset(CreateChannel(1, ICE_CANDIDATE_COMPONENT_DEFAULT, |
| 370 | ice_ep2_cd1_ch, ice_ep1_cd1_ch)); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 371 | ep1_.cd1_.ch_->SetIceConfig(ep1_config); |
| 372 | ep2_.cd1_.ch_->SetIceConfig(ep2_config); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 373 | ep1_.cd1_.ch_->MaybeStartGathering(); |
| 374 | ep2_.cd1_.ch_->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 375 | ep1_.cd1_.ch_->allocator_session()->SignalIceRegathering.connect( |
| 376 | &ep1_, &Endpoint::OnIceRegathering); |
| 377 | ep2_.cd1_.ch_->allocator_session()->SignalIceRegathering.connect( |
| 378 | &ep2_, &Endpoint::OnIceRegathering); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 379 | } |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 380 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 381 | void CreateChannels() { |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 382 | IceConfig default_config; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 383 | CreateChannels(default_config, default_config, false); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 384 | } |
| 385 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 386 | P2PTransportChannel* CreateChannel(int endpoint, |
| 387 | int component, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 388 | const IceParameters& local_ice, |
| 389 | const IceParameters& remote_ice) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 390 | P2PTransportChannel* channel = new P2PTransportChannel( |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 391 | "test content name", component, GetAllocator(endpoint), |
| 392 | GetEndpoint(endpoint)->async_resolver_factory_); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 393 | channel->SignalReadyToSend.connect( |
| 394 | this, &P2PTransportChannelTestBase::OnReadyToSend); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 395 | channel->SignalCandidateGathered.connect( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 396 | this, &P2PTransportChannelTestBase::OnCandidateGathered); |
| 397 | channel->SignalCandidatesRemoved.connect( |
| 398 | this, &P2PTransportChannelTestBase::OnCandidatesRemoved); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 399 | channel->SignalReadPacket.connect( |
| 400 | this, &P2PTransportChannelTestBase::OnReadPacket); |
| 401 | channel->SignalRoleConflict.connect( |
| 402 | this, &P2PTransportChannelTestBase::OnRoleConflict); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 403 | channel->SignalNetworkRouteChanged.connect( |
| 404 | this, &P2PTransportChannelTestBase::OnNetworkRouteChanged); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 405 | channel->SignalSentPacket.connect( |
| 406 | this, &P2PTransportChannelTestBase::OnSentPacket); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 407 | channel->SetIceParameters(local_ice); |
| 408 | if (remote_ice_parameter_source_ == FROM_SETICEPARAMETERS) { |
| 409 | channel->SetRemoteIceParameters(remote_ice); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 410 | } |
| 411 | channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); |
| 412 | channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 413 | return channel; |
| 414 | } |
| 415 | void DestroyChannels() { |
Qingsi Wang | 5b8dd4d | 2018-06-20 13:30:43 -0700 | [diff] [blame] | 416 | main_.Clear(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 417 | ep1_.cd1_.ch_.reset(); |
| 418 | ep2_.cd1_.ch_.reset(); |
| 419 | ep1_.cd2_.ch_.reset(); |
| 420 | ep2_.cd2_.ch_.reset(); |
| 421 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 422 | P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } |
| 423 | P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } |
| 424 | P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); } |
| 425 | P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 426 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 427 | TestTurnServer* test_turn_server() { return &turn_server_; } |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 428 | rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); } |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 429 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 430 | // Common results. |
| 431 | static const Result kLocalUdpToLocalUdp; |
| 432 | static const Result kLocalUdpToStunUdp; |
| 433 | static const Result kLocalUdpToPrflxUdp; |
| 434 | static const Result kPrflxUdpToLocalUdp; |
| 435 | static const Result kStunUdpToLocalUdp; |
| 436 | static const Result kStunUdpToStunUdp; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 437 | static const Result kStunUdpToPrflxUdp; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 438 | static const Result kPrflxUdpToStunUdp; |
| 439 | static const Result kLocalUdpToRelayUdp; |
| 440 | static const Result kPrflxUdpToRelayUdp; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 441 | static const Result kRelayUdpToPrflxUdp; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 442 | static const Result kLocalTcpToLocalTcp; |
| 443 | static const Result kLocalTcpToPrflxTcp; |
| 444 | static const Result kPrflxTcpToLocalTcp; |
| 445 | |
| 446 | rtc::NATSocketServer* nat() { return nss_.get(); } |
| 447 | rtc::FirewallSocketServer* fw() { return ss_.get(); } |
| 448 | |
| 449 | Endpoint* GetEndpoint(int endpoint) { |
| 450 | if (endpoint == 0) { |
| 451 | return &ep1_; |
| 452 | } else if (endpoint == 1) { |
| 453 | return &ep2_; |
| 454 | } else { |
| 455 | return NULL; |
| 456 | } |
| 457 | } |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 458 | BasicPortAllocator* GetAllocator(int endpoint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 459 | return GetEndpoint(endpoint)->allocator_.get(); |
| 460 | } |
| 461 | void AddAddress(int endpoint, const SocketAddress& addr) { |
| 462 | GetEndpoint(endpoint)->network_manager_.AddInterface(addr); |
| 463 | } |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 464 | void AddAddress(int endpoint, |
| 465 | const SocketAddress& addr, |
| 466 | const std::string& ifname, |
| 467 | rtc::AdapterType adapter_type) { |
| 468 | GetEndpoint(endpoint)->network_manager_.AddInterface(addr, ifname, |
| 469 | adapter_type); |
| 470 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 471 | void RemoveAddress(int endpoint, const SocketAddress& addr) { |
| 472 | GetEndpoint(endpoint)->network_manager_.RemoveInterface(addr); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 473 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 474 | } |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 475 | void SetProxy(int endpoint, rtc::ProxyType type) { |
| 476 | rtc::ProxyInfo info; |
| 477 | info.type = type; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 478 | info.address = (type == rtc::PROXY_HTTPS) ? kHttpsProxyAddrs[endpoint] |
| 479 | : kSocksProxyAddrs[endpoint]; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 480 | GetAllocator(endpoint)->set_proxy("unittest/1.0", info); |
| 481 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 482 | void SetAllocatorFlags(int endpoint, int flags) { |
| 483 | GetAllocator(endpoint)->set_flags(flags); |
| 484 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 485 | void SetIceRole(int endpoint, IceRole role) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 486 | GetEndpoint(endpoint)->SetIceRole(role); |
| 487 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 488 | void SetIceTiebreaker(int endpoint, uint64_t tiebreaker) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 489 | GetEndpoint(endpoint)->SetIceTiebreaker(tiebreaker); |
| 490 | } |
| 491 | bool GetRoleConflict(int endpoint) { |
| 492 | return GetEndpoint(endpoint)->role_conflict(); |
| 493 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 494 | void SetAllocationStepDelay(int endpoint, uint32_t delay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 495 | return GetEndpoint(endpoint)->SetAllocationStepDelay(delay); |
| 496 | } |
| 497 | void SetAllowTcpListen(int endpoint, bool allow_tcp_listen) { |
| 498 | return GetEndpoint(endpoint)->SetAllowTcpListen(allow_tcp_listen); |
| 499 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 500 | |
| 501 | // Return true if the approprite parts of the expected Result, based |
| 502 | // on the local and remote candidate of ep1_ch1, match. This can be |
| 503 | // used in an EXPECT_TRUE_WAIT. |
| 504 | bool CheckCandidate1(const Result& expected) { |
| 505 | const std::string& local_type = LocalCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 506 | const std::string& local_protocol = LocalCandidate(ep1_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 507 | const std::string& remote_type = RemoteCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 508 | const std::string& remote_protocol = RemoteCandidate(ep1_ch1())->protocol(); |
| 509 | return (local_protocol == expected.controlling_protocol && |
| 510 | remote_protocol == expected.controlled_protocol && |
| 511 | local_type == expected.controlling_type && |
| 512 | remote_type == expected.controlled_type); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // EXPECT_EQ on the approprite parts of the expected Result, based |
| 516 | // on the local and remote candidate of ep1_ch1. This is like |
| 517 | // CheckCandidate1, except that it will provide more detail about |
| 518 | // what didn't match. |
| 519 | void ExpectCandidate1(const Result& expected) { |
| 520 | if (CheckCandidate1(expected)) { |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | const std::string& local_type = LocalCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 525 | const std::string& local_protocol = LocalCandidate(ep1_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 526 | const std::string& remote_type = RemoteCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 527 | const std::string& remote_protocol = RemoteCandidate(ep1_ch1())->protocol(); |
| 528 | EXPECT_EQ(expected.controlling_type, local_type); |
| 529 | EXPECT_EQ(expected.controlled_type, remote_type); |
| 530 | EXPECT_EQ(expected.controlling_protocol, local_protocol); |
| 531 | EXPECT_EQ(expected.controlled_protocol, remote_protocol); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // Return true if the approprite parts of the expected Result, based |
| 535 | // on the local and remote candidate of ep2_ch1, match. This can be |
| 536 | // used in an EXPECT_TRUE_WAIT. |
| 537 | bool CheckCandidate2(const Result& expected) { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 538 | const std::string& local_type = LocalCandidate(ep2_ch1())->type(); |
| 539 | const std::string& local_protocol = LocalCandidate(ep2_ch1())->protocol(); |
| 540 | const std::string& remote_type = RemoteCandidate(ep2_ch1())->type(); |
| 541 | const std::string& remote_protocol = RemoteCandidate(ep2_ch1())->protocol(); |
| 542 | return (local_protocol == expected.controlled_protocol && |
| 543 | remote_protocol == expected.controlling_protocol && |
| 544 | local_type == expected.controlled_type && |
| 545 | remote_type == expected.controlling_type); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // EXPECT_EQ on the approprite parts of the expected Result, based |
| 549 | // on the local and remote candidate of ep2_ch1. This is like |
| 550 | // CheckCandidate2, except that it will provide more detail about |
| 551 | // what didn't match. |
| 552 | void ExpectCandidate2(const Result& expected) { |
| 553 | if (CheckCandidate2(expected)) { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | const std::string& local_type = LocalCandidate(ep2_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 558 | const std::string& local_protocol = LocalCandidate(ep2_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 559 | const std::string& remote_type = RemoteCandidate(ep2_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 560 | const std::string& remote_protocol = RemoteCandidate(ep2_ch1())->protocol(); |
| 561 | EXPECT_EQ(expected.controlled_type, local_type); |
| 562 | EXPECT_EQ(expected.controlling_type, remote_type); |
| 563 | EXPECT_EQ(expected.controlled_protocol, local_protocol); |
| 564 | EXPECT_EQ(expected.controlling_protocol, remote_protocol); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 565 | } |
| 566 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 567 | void Test(const Result& expected) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 568 | rtc::ScopedFakeClock clock; |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 569 | int64_t connect_start = rtc::TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 570 | int64_t connect_time; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 571 | |
| 572 | // Create the channels and wait for them to connect. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 573 | CreateChannels(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 574 | EXPECT_TRUE_SIMULATED_WAIT( |
| 575 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 576 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 577 | ep2_ch1()->writable(), |
| 578 | expected.connect_wait + kShortTimeout, clock); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 579 | connect_time = rtc::TimeMillis() - connect_start; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 580 | if (connect_time < expected.connect_wait) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 581 | RTC_LOG(LS_INFO) << "Connect time: " << connect_time << " ms"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 582 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 583 | RTC_LOG(LS_INFO) << "Connect time: TIMEOUT (" << expected.connect_wait |
| 584 | << " ms)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 587 | // Allow a few turns of the crank for the selected connections to emerge. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 588 | // This may take up to 2 seconds. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 589 | if (ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection()) { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 590 | int64_t converge_start = rtc::TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 591 | int64_t converge_time; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 592 | // Verifying local and remote channel selected connection information. |
| 593 | // This is done only for the RFC 5245 as controlled agent will use |
| 594 | // USE-CANDIDATE from controlling (ep1) agent. We can easily predict from |
| 595 | // EP1 result matrix. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 596 | EXPECT_TRUE_SIMULATED_WAIT( |
| 597 | CheckCandidate1(expected) && CheckCandidate2(expected), |
| 598 | kMediumTimeout, clock); |
Taylor Brandstetter | 7e1b8fb | 2016-05-26 15:21:45 -0700 | [diff] [blame] | 599 | // Also do EXPECT_EQ on each part so that failures are more verbose. |
| 600 | ExpectCandidate1(expected); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 601 | ExpectCandidate2(expected); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 602 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 603 | converge_time = rtc::TimeMillis() - converge_start; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 604 | int64_t converge_wait = 2000; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 605 | if (converge_time < converge_wait) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 606 | RTC_LOG(LS_INFO) << "Converge time: " << converge_time << " ms"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 607 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 608 | RTC_LOG(LS_INFO) << "Converge time: TIMEOUT (" << converge_wait |
| 609 | << " ms)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | // Try sending some data to other end. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 613 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 614 | |
| 615 | // Destroy the channels, and wait for them to be fully cleaned up. |
| 616 | DestroyChannels(); |
| 617 | } |
| 618 | |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 619 | void TestSendRecv(rtc::ThreadProcessingFakeClock* clock) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 620 | for (int i = 0; i < 10; ++i) { |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 621 | const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 622 | int len = static_cast<int>(strlen(data)); |
| 623 | // local_channel1 <==> remote_channel1 |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 624 | EXPECT_EQ_SIMULATED_WAIT(len, SendData(ep1_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 625 | kMediumTimeout, *clock); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 626 | EXPECT_TRUE_SIMULATED_WAIT(CheckDataOnChannel(ep2_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 627 | kMediumTimeout, *clock); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 628 | EXPECT_EQ_SIMULATED_WAIT(len, SendData(ep2_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 629 | kMediumTimeout, *clock); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 630 | EXPECT_TRUE_SIMULATED_WAIT(CheckDataOnChannel(ep1_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 631 | kMediumTimeout, *clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 635 | // This test waits for the transport to become receiving and writable on both |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 636 | // end points. Once they are, the end points set new local ice parameters and |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 637 | // restart the ice gathering. Finally it waits for the transport to select a |
| 638 | // new connection using the newly generated ice candidates. |
| 639 | // Before calling this function the end points must be configured. |
| 640 | void TestHandleIceUfragPasswordChanged() { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 641 | rtc::ScopedFakeClock clock; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 642 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 643 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 644 | EXPECT_TRUE_SIMULATED_WAIT( |
| 645 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 646 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 647 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 648 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 649 | const Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1()); |
| 650 | const Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1()); |
| 651 | const Candidate* old_remote_candidate1 = RemoteCandidate(ep1_ch1()); |
| 652 | const Candidate* old_remote_candidate2 = RemoteCandidate(ep2_ch1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 653 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 654 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 655 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 656 | ep1_ch1()->MaybeStartGathering(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 657 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 658 | |
| 659 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 660 | ep2_ch1()->MaybeStartGathering(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 661 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 662 | EXPECT_TRUE_SIMULATED_WAIT(LocalCandidate(ep1_ch1())->generation() != |
| 663 | old_local_candidate1->generation(), |
| 664 | kMediumTimeout, clock); |
| 665 | EXPECT_TRUE_SIMULATED_WAIT(LocalCandidate(ep2_ch1())->generation() != |
| 666 | old_local_candidate2->generation(), |
| 667 | kMediumTimeout, clock); |
| 668 | EXPECT_TRUE_SIMULATED_WAIT(RemoteCandidate(ep1_ch1())->generation() != |
| 669 | old_remote_candidate1->generation(), |
| 670 | kMediumTimeout, clock); |
| 671 | EXPECT_TRUE_SIMULATED_WAIT(RemoteCandidate(ep2_ch1())->generation() != |
| 672 | old_remote_candidate2->generation(), |
| 673 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 674 | EXPECT_EQ(1u, RemoteCandidate(ep2_ch1())->generation()); |
| 675 | EXPECT_EQ(1u, RemoteCandidate(ep1_ch1())->generation()); |
| 676 | } |
| 677 | |
| 678 | void TestSignalRoleConflict() { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 679 | rtc::ScopedFakeClock clock; |
| 680 | // Default EP1 is in controlling state. |
| 681 | SetIceTiebreaker(0, kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 682 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 683 | SetIceRole(1, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 684 | SetIceTiebreaker(1, kHighTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 685 | |
| 686 | // Creating channels with both channels role set to CONTROLLING. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 687 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 688 | // Since both the channels initiated with controlling state and channel2 |
| 689 | // has higher tiebreaker value, channel1 should receive SignalRoleConflict. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 690 | EXPECT_TRUE_SIMULATED_WAIT(GetRoleConflict(0), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 691 | EXPECT_FALSE(GetRoleConflict(1)); |
| 692 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 693 | EXPECT_TRUE_SIMULATED_WAIT( |
| 694 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 695 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 696 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 697 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 698 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 699 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 700 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 701 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 704 | void TestPacketInfoIsSet(rtc::PacketInfo info) { |
| 705 | EXPECT_NE(info.packet_type, rtc::PacketType::kUnknown); |
| 706 | EXPECT_NE(info.protocol, rtc::PacketInfoProtocolType::kUnknown); |
| 707 | EXPECT_TRUE(info.network_id.has_value()); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 708 | } |
| 709 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 710 | void OnReadyToSend(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 711 | GetEndpoint(transport)->ready_to_send_ = true; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 712 | } |
| 713 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 714 | // We pass the candidates directly to the other side. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 715 | void OnCandidateGathered(IceTransportInternal* ch, const Candidate& c) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 716 | if (force_relay_ && c.type() != RELAY_PORT_TYPE) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 717 | return; |
| 718 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 719 | if (GetEndpoint(ch)->save_candidates_) { |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 720 | GetEndpoint(ch)->saved_candidates_.push_back( |
| 721 | std::unique_ptr<CandidatesData>(new CandidatesData(ch, c))); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 722 | } else { |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 723 | main_.Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, |
| 724 | new CandidatesData(ch, c)); |
jiayl@webrtc.org | c5fd66d | 2014-12-29 19:23:37 +0000 | [diff] [blame] | 725 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 726 | } |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 727 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 728 | void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route) { |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 729 | // If the |network_route| is unset, don't count. This is used in the case |
| 730 | // when the network on remote side is down, the signal will be fired with an |
| 731 | // unset network route and it shouldn't trigger a connection switch. |
| 732 | if (network_route) { |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 733 | ++selected_candidate_pair_switches_; |
| 734 | } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | int reset_selected_candidate_pair_switches() { |
| 738 | int switches = selected_candidate_pair_switches_; |
| 739 | selected_candidate_pair_switches_ = 0; |
| 740 | return switches; |
| 741 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 742 | |
| 743 | void PauseCandidates(int endpoint) { |
| 744 | GetEndpoint(endpoint)->save_candidates_ = true; |
| 745 | } |
| 746 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 747 | void OnCandidatesRemoved(IceTransportInternal* ch, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 748 | const std::vector<Candidate>& candidates) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 749 | // Candidate removals are not paused. |
| 750 | CandidatesData* candidates_data = new CandidatesData(ch, candidates); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 751 | main_.Post(RTC_FROM_HERE, this, MSG_REMOVE_CANDIDATES, candidates_data); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 754 | // Tcp candidate verification has to be done when they are generated. |
| 755 | void VerifySavedTcpCandidates(int endpoint, const std::string& tcptype) { |
| 756 | for (auto& data : GetEndpoint(endpoint)->saved_candidates_) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 757 | for (auto& candidate : data->candidates) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 758 | EXPECT_EQ(candidate.protocol(), TCP_PROTOCOL_NAME); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 759 | EXPECT_EQ(candidate.tcptype(), tcptype); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 760 | if (candidate.tcptype() == TCPTYPE_ACTIVE_STR) { |
| 761 | EXPECT_EQ(candidate.address().port(), DISCARD_PORT); |
| 762 | } else if (candidate.tcptype() == TCPTYPE_PASSIVE_STR) { |
| 763 | EXPECT_NE(candidate.address().port(), DISCARD_PORT); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 764 | } else { |
| 765 | FAIL() << "Unknown tcptype: " << candidate.tcptype(); |
| 766 | } |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 771 | void ResumeCandidates(int endpoint) { |
| 772 | Endpoint* ed = GetEndpoint(endpoint); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 773 | for (auto& candidate : ed->saved_candidates_) { |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 774 | main_.Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, candidate.release()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 775 | } |
| 776 | ed->saved_candidates_.clear(); |
| 777 | ed->save_candidates_ = false; |
| 778 | } |
| 779 | |
| 780 | void OnMessage(rtc::Message* msg) { |
| 781 | switch (msg->message_id) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 782 | case MSG_ADD_CANDIDATES: { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 783 | std::unique_ptr<CandidatesData> data( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 784 | static_cast<CandidatesData*>(msg->pdata)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 785 | P2PTransportChannel* rch = GetRemoteChannel(data->channel); |
| 786 | if (!rch) { |
| 787 | return; |
| 788 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 789 | for (auto& c : data->candidates) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 790 | if (remote_ice_parameter_source_ != FROM_CANDIDATE) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 791 | c.set_username(""); |
| 792 | c.set_password(""); |
| 793 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 794 | RTC_LOG(LS_INFO) << "Candidate(" << data->channel->component() << "->" |
| 795 | << rch->component() << "): " << c.ToString(); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 796 | rch->AddRemoteCandidate(c); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 797 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 798 | break; |
| 799 | } |
| 800 | case MSG_REMOVE_CANDIDATES: { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 801 | std::unique_ptr<CandidatesData> data( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 802 | static_cast<CandidatesData*>(msg->pdata)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 803 | P2PTransportChannel* rch = GetRemoteChannel(data->channel); |
| 804 | if (!rch) { |
| 805 | return; |
| 806 | } |
| 807 | for (Candidate& c : data->candidates) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 808 | RTC_LOG(LS_INFO) << "Removed remote candidate " << c.ToString(); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 809 | rch->RemoveRemoteCandidate(c); |
| 810 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
| 813 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 814 | } |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 815 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 816 | void OnReadPacket(rtc::PacketTransportInternal* transport, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 817 | const char* data, |
| 818 | size_t len, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 819 | const int64_t& /* packet_time_us */, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 820 | int flags) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 821 | std::list<std::string>& packets = GetPacketList(transport); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 822 | packets.push_front(std::string(data, len)); |
| 823 | } |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 824 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 825 | void OnRoleConflict(IceTransportInternal* channel) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 826 | GetEndpoint(channel)->OnRoleConflict(true); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 827 | IceRole new_role = GetEndpoint(channel)->ice_role() == ICEROLE_CONTROLLING |
| 828 | ? ICEROLE_CONTROLLED |
| 829 | : ICEROLE_CONTROLLING; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 830 | channel->SetIceRole(new_role); |
| 831 | } |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 832 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 833 | void OnSentPacket(rtc::PacketTransportInternal* transport, |
| 834 | const rtc::SentPacket& packet) { |
| 835 | TestPacketInfoIsSet(packet.info); |
| 836 | } |
| 837 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 838 | int SendData(IceTransportInternal* channel, const char* data, size_t len) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 839 | rtc::PacketOptions options; |
| 840 | return channel->SendPacket(data, len, options, 0); |
| 841 | } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 842 | bool CheckDataOnChannel(IceTransportInternal* channel, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 843 | const char* data, |
| 844 | int len) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 845 | return GetChannelData(channel)->CheckData(data, len); |
| 846 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 847 | static const Candidate* LocalCandidate(P2PTransportChannel* ch) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 848 | return (ch && ch->selected_connection()) |
| 849 | ? &ch->selected_connection()->local_candidate() |
| 850 | : NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 851 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 852 | static const Candidate* RemoteCandidate(P2PTransportChannel* ch) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 853 | return (ch && ch->selected_connection()) |
| 854 | ? &ch->selected_connection()->remote_candidate() |
| 855 | : NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 856 | } |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 857 | Endpoint* GetEndpoint(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 858 | if (ep1_.HasTransport(transport)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 859 | return &ep1_; |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 860 | } else if (ep2_.HasTransport(transport)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 861 | return &ep2_; |
| 862 | } else { |
| 863 | return NULL; |
| 864 | } |
| 865 | } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 866 | P2PTransportChannel* GetRemoteChannel(IceTransportInternal* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 867 | if (ch == ep1_ch1()) |
| 868 | return ep2_ch1(); |
| 869 | else if (ch == ep1_ch2()) |
| 870 | return ep2_ch2(); |
| 871 | else if (ch == ep2_ch1()) |
| 872 | return ep1_ch1(); |
| 873 | else if (ch == ep2_ch2()) |
| 874 | return ep1_ch2(); |
| 875 | else |
| 876 | return NULL; |
| 877 | } |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 878 | std::list<std::string>& GetPacketList( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 879 | rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 880 | return GetChannelData(transport)->ch_packets_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 883 | enum RemoteIceParameterSource { FROM_CANDIDATE, FROM_SETICEPARAMETERS }; |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 884 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 885 | // How does the test pass ICE parameters to the P2PTransportChannel? |
| 886 | // On the candidate itself, or through SetRemoteIceParameters? |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 887 | // Goes through the candidate itself by default. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 888 | void set_remote_ice_parameter_source(RemoteIceParameterSource source) { |
| 889 | remote_ice_parameter_source_ = source; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 892 | void set_force_relay(bool relay) { force_relay_ = relay; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 893 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 894 | void ConnectSignalNominated(Connection* conn) { |
| 895 | conn->SignalNominated.connect(this, |
| 896 | &P2PTransportChannelTestBase::OnNominated); |
| 897 | } |
| 898 | |
| 899 | void OnNominated(Connection* conn) { nominated_ = true; } |
| 900 | bool nominated() { return nominated_; } |
| 901 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 902 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 903 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 904 | std::unique_ptr<rtc::NATSocketServer> nss_; |
| 905 | std::unique_ptr<rtc::FirewallSocketServer> ss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 906 | rtc::AutoSocketServerThread main_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 907 | std::unique_ptr<TestStunServer> stun_server_; |
| 908 | TestTurnServer turn_server_; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 909 | rtc::SocksProxyServer socks_server1_; |
| 910 | rtc::SocksProxyServer socks_server2_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 911 | Endpoint ep1_; |
| 912 | Endpoint ep2_; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 913 | RemoteIceParameterSource remote_ice_parameter_source_ = FROM_CANDIDATE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 914 | bool force_relay_; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 915 | int selected_candidate_pair_switches_ = 0; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 916 | |
| 917 | bool nominated_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 918 | }; |
| 919 | |
| 920 | // The tests have only a few outcomes, which we predefine. |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 921 | const P2PTransportChannelTestBase::Result |
| 922 | P2PTransportChannelTestBase::kLocalUdpToLocalUdp("local", |
| 923 | "udp", |
| 924 | "local", |
| 925 | "udp", |
| 926 | 1000); |
| 927 | const P2PTransportChannelTestBase::Result |
| 928 | P2PTransportChannelTestBase::kLocalUdpToStunUdp("local", |
| 929 | "udp", |
| 930 | "stun", |
| 931 | "udp", |
| 932 | 1000); |
| 933 | const P2PTransportChannelTestBase::Result |
| 934 | P2PTransportChannelTestBase::kLocalUdpToPrflxUdp("local", |
| 935 | "udp", |
| 936 | "prflx", |
| 937 | "udp", |
| 938 | 1000); |
| 939 | const P2PTransportChannelTestBase::Result |
| 940 | P2PTransportChannelTestBase::kPrflxUdpToLocalUdp("prflx", |
| 941 | "udp", |
| 942 | "local", |
| 943 | "udp", |
| 944 | 1000); |
| 945 | const P2PTransportChannelTestBase::Result |
| 946 | P2PTransportChannelTestBase::kStunUdpToLocalUdp("stun", |
| 947 | "udp", |
| 948 | "local", |
| 949 | "udp", |
| 950 | 1000); |
| 951 | const P2PTransportChannelTestBase::Result |
| 952 | P2PTransportChannelTestBase::kStunUdpToStunUdp("stun", |
| 953 | "udp", |
| 954 | "stun", |
| 955 | "udp", |
| 956 | 1000); |
| 957 | const P2PTransportChannelTestBase::Result |
| 958 | P2PTransportChannelTestBase::kStunUdpToPrflxUdp("stun", |
| 959 | "udp", |
| 960 | "prflx", |
| 961 | "udp", |
| 962 | 1000); |
| 963 | const P2PTransportChannelTestBase::Result |
| 964 | P2PTransportChannelTestBase::kPrflxUdpToStunUdp("prflx", |
| 965 | "udp", |
| 966 | "stun", |
| 967 | "udp", |
| 968 | 1000); |
| 969 | const P2PTransportChannelTestBase::Result |
| 970 | P2PTransportChannelTestBase::kLocalUdpToRelayUdp("local", |
| 971 | "udp", |
| 972 | "relay", |
| 973 | "udp", |
| 974 | 2000); |
| 975 | const P2PTransportChannelTestBase::Result |
| 976 | P2PTransportChannelTestBase::kPrflxUdpToRelayUdp("prflx", |
| 977 | "udp", |
| 978 | "relay", |
| 979 | "udp", |
| 980 | 2000); |
| 981 | const P2PTransportChannelTestBase::Result |
| 982 | P2PTransportChannelTestBase::kRelayUdpToPrflxUdp("relay", |
| 983 | "udp", |
| 984 | "prflx", |
| 985 | "udp", |
| 986 | 2000); |
| 987 | const P2PTransportChannelTestBase::Result |
| 988 | P2PTransportChannelTestBase::kLocalTcpToLocalTcp("local", |
| 989 | "tcp", |
| 990 | "local", |
| 991 | "tcp", |
| 992 | 3000); |
| 993 | const P2PTransportChannelTestBase::Result |
| 994 | P2PTransportChannelTestBase::kLocalTcpToPrflxTcp("local", |
| 995 | "tcp", |
| 996 | "prflx", |
| 997 | "tcp", |
| 998 | 3000); |
| 999 | const P2PTransportChannelTestBase::Result |
| 1000 | P2PTransportChannelTestBase::kPrflxTcpToLocalTcp("prflx", |
| 1001 | "tcp", |
| 1002 | "local", |
| 1003 | "tcp", |
| 1004 | 3000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Test the matrix of all the connectivity types we expect to see in the wild. |
| 1007 | // Just test every combination of the configs in the Config enum. |
| 1008 | class P2PTransportChannelTest : public P2PTransportChannelTestBase { |
| 1009 | protected: |
| 1010 | static const Result* kMatrix[NUM_CONFIGS][NUM_CONFIGS]; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1011 | void ConfigureEndpoints(Config config1, |
| 1012 | Config config2, |
| 1013 | int allocator_flags1, |
| 1014 | int allocator_flags2) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1015 | ConfigureEndpoint(0, config1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1016 | SetAllocatorFlags(0, allocator_flags1); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1017 | SetAllocationStepDelay(0, kMinimumStepDelay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1018 | ConfigureEndpoint(1, config2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1019 | SetAllocatorFlags(1, allocator_flags2); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1020 | SetAllocationStepDelay(1, kMinimumStepDelay); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1021 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1022 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1023 | } |
| 1024 | void ConfigureEndpoint(int endpoint, Config config) { |
| 1025 | switch (config) { |
| 1026 | case OPEN: |
| 1027 | AddAddress(endpoint, kPublicAddrs[endpoint]); |
| 1028 | break; |
| 1029 | case NAT_FULL_CONE: |
| 1030 | case NAT_ADDR_RESTRICTED: |
| 1031 | case NAT_PORT_RESTRICTED: |
| 1032 | case NAT_SYMMETRIC: |
| 1033 | AddAddress(endpoint, kPrivateAddrs[endpoint]); |
| 1034 | // Add a single NAT of the desired type |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1035 | nat() |
| 1036 | ->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint], |
| 1037 | static_cast<rtc::NATType>(config - NAT_FULL_CONE)) |
| 1038 | ->AddClient(kPrivateAddrs[endpoint]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | case NAT_DOUBLE_CONE: |
| 1041 | case NAT_SYMMETRIC_THEN_CONE: |
| 1042 | AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]); |
| 1043 | // Add a two cascaded NATs of the desired types |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1044 | nat() |
| 1045 | ->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint], |
| 1046 | (config == NAT_DOUBLE_CONE) ? rtc::NAT_OPEN_CONE |
| 1047 | : rtc::NAT_SYMMETRIC) |
| 1048 | ->AddTranslator(kPrivateAddrs[endpoint], |
| 1049 | kCascadedNatAddrs[endpoint], rtc::NAT_OPEN_CONE) |
| 1050 | ->AddClient(kCascadedPrivateAddrs[endpoint]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1051 | break; |
| 1052 | case BLOCK_UDP: |
| 1053 | case BLOCK_UDP_AND_INCOMING_TCP: |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1054 | case BLOCK_ALL_BUT_OUTGOING_HTTP: |
| 1055 | case PROXY_HTTPS: |
| 1056 | case PROXY_SOCKS: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1057 | AddAddress(endpoint, kPublicAddrs[endpoint]); |
| 1058 | // Block all UDP |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1059 | fw()->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, kPublicAddrs[endpoint]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1060 | if (config == BLOCK_UDP_AND_INCOMING_TCP) { |
| 1061 | // Block TCP inbound to the endpoint |
| 1062 | fw()->AddRule(false, rtc::FP_TCP, SocketAddress(), |
| 1063 | kPublicAddrs[endpoint]); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1064 | } else if (config == BLOCK_ALL_BUT_OUTGOING_HTTP) { |
| 1065 | // Block all TCP to/from the endpoint except 80/443 out |
| 1066 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1067 | SocketAddress(rtc::IPAddress(INADDR_ANY), 80)); |
| 1068 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1069 | SocketAddress(rtc::IPAddress(INADDR_ANY), 443)); |
| 1070 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1071 | kPublicAddrs[endpoint]); |
| 1072 | } else if (config == PROXY_HTTPS) { |
| 1073 | // Block all TCP to/from the endpoint except to the proxy server |
| 1074 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1075 | kHttpsProxyAddrs[endpoint]); |
| 1076 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1077 | kPublicAddrs[endpoint]); |
| 1078 | SetProxy(endpoint, rtc::PROXY_HTTPS); |
| 1079 | } else if (config == PROXY_SOCKS) { |
| 1080 | // Block all TCP to/from the endpoint except to the proxy server |
| 1081 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1082 | kSocksProxyAddrs[endpoint]); |
| 1083 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1084 | kPublicAddrs[endpoint]); |
| 1085 | SetProxy(endpoint, rtc::PROXY_SOCKS5); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1086 | } |
| 1087 | break; |
| 1088 | default: |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1089 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | }; |
| 1094 | |
| 1095 | // Shorthands for use in the test matrix. |
| 1096 | #define LULU &kLocalUdpToLocalUdp |
| 1097 | #define LUSU &kLocalUdpToStunUdp |
| 1098 | #define LUPU &kLocalUdpToPrflxUdp |
| 1099 | #define PULU &kPrflxUdpToLocalUdp |
| 1100 | #define SULU &kStunUdpToLocalUdp |
| 1101 | #define SUSU &kStunUdpToStunUdp |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1102 | #define SUPU &kStunUdpToPrflxUdp |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1103 | #define PUSU &kPrflxUdpToStunUdp |
| 1104 | #define LURU &kLocalUdpToRelayUdp |
| 1105 | #define PURU &kPrflxUdpToRelayUdp |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1106 | #define RUPU &kRelayUdpToPrflxUdp |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1107 | #define LTLT &kLocalTcpToLocalTcp |
| 1108 | #define LTPT &kLocalTcpToPrflxTcp |
| 1109 | #define PTLT &kPrflxTcpToLocalTcp |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1110 | // TODO(?): Enable these once TestRelayServer can accept external TCP. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1111 | #define LTRT NULL |
| 1112 | #define LSRS NULL |
| 1113 | |
| 1114 | // Test matrix. Originator behavior defined by rows, receiever by columns. |
| 1115 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1116 | // TODO(?): Fix NULLs caused by lack of TCP support in NATSocket. |
| 1117 | // TODO(?): Fix NULLs caused by no HTTP proxy support. |
| 1118 | // TODO(?): Rearrange rows/columns from best to worst. |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1119 | const P2PTransportChannelTest::Result* |
| 1120 | P2PTransportChannelTest::kMatrix[NUM_CONFIGS][NUM_CONFIGS] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1121 | // OPEN CONE ADDR PORT SYMM 2CON SCON !UDP !TCP HTTP PRXH |
| 1122 | // PRXS |
| 1123 | /*OP*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, LTPT, LTPT, LSRS, |
| 1124 | NULL, LTPT}, |
| 1125 | /*CO*/ |
| 1126 | {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, |
| 1127 | LTRT}, |
| 1128 | /*AD*/ |
| 1129 | {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, |
| 1130 | LTRT}, |
| 1131 | /*PO*/ |
| 1132 | {SULU, SUSU, SUSU, SUSU, RUPU, SUSU, RUPU, NULL, NULL, LSRS, NULL, |
| 1133 | LTRT}, |
| 1134 | /*SY*/ |
| 1135 | {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, |
| 1136 | LTRT}, |
| 1137 | /*2C*/ |
| 1138 | {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, |
| 1139 | LTRT}, |
| 1140 | /*SC*/ |
| 1141 | {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, |
| 1142 | LTRT}, |
| 1143 | /*!U*/ |
| 1144 | {LTPT, NULL, NULL, NULL, NULL, NULL, NULL, LTPT, LTPT, LSRS, NULL, |
| 1145 | LTRT}, |
| 1146 | /*!T*/ |
| 1147 | {PTLT, NULL, NULL, NULL, NULL, NULL, NULL, PTLT, LTRT, LSRS, NULL, |
| 1148 | LTRT}, |
| 1149 | /*HT*/ |
| 1150 | {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, |
| 1151 | LSRS}, |
| 1152 | /*PR*/ |
| 1153 | {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1154 | NULL}, |
| 1155 | /*PR*/ |
| 1156 | {LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LSRS, NULL, |
| 1157 | LTRT}, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1158 | }; |
| 1159 | |
| 1160 | // The actual tests that exercise all the various configurations. |
| 1161 | // Test names are of the form P2PTransportChannelTest_TestOPENToNAT_FULL_CONE |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1162 | #define P2P_TEST_DECLARATION(x, y, z) \ |
| 1163 | TEST_F(P2PTransportChannelTest, z##Test##x##To##y) { \ |
| 1164 | ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_SOCKET, \ |
| 1165 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); \ |
Taylor Brandstetter | 7e1b8fb | 2016-05-26 15:21:45 -0700 | [diff] [blame] | 1166 | if (kMatrix[x][y] != NULL) \ |
| 1167 | Test(*kMatrix[x][y]); \ |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1168 | else \ |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1169 | RTC_LOG(LS_WARNING) << "Not yet implemented"; \ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1172 | #define P2P_TEST(x, y) P2P_TEST_DECLARATION(x, y, /* empty argument */) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1173 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1174 | #define P2P_TEST_SET(x) \ |
| 1175 | P2P_TEST(x, OPEN) \ |
| 1176 | P2P_TEST(x, NAT_FULL_CONE) \ |
| 1177 | P2P_TEST(x, NAT_ADDR_RESTRICTED) \ |
| 1178 | P2P_TEST(x, NAT_PORT_RESTRICTED) \ |
| 1179 | P2P_TEST(x, NAT_SYMMETRIC) \ |
| 1180 | P2P_TEST(x, NAT_DOUBLE_CONE) \ |
| 1181 | P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \ |
| 1182 | P2P_TEST(x, BLOCK_UDP) \ |
| 1183 | P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \ |
| 1184 | P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \ |
| 1185 | P2P_TEST(x, PROXY_HTTPS) \ |
| 1186 | P2P_TEST(x, PROXY_SOCKS) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1187 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1188 | P2P_TEST_SET(OPEN) |
| 1189 | P2P_TEST_SET(NAT_FULL_CONE) |
| 1190 | P2P_TEST_SET(NAT_ADDR_RESTRICTED) |
| 1191 | P2P_TEST_SET(NAT_PORT_RESTRICTED) |
| 1192 | P2P_TEST_SET(NAT_SYMMETRIC) |
| 1193 | P2P_TEST_SET(NAT_DOUBLE_CONE) |
| 1194 | P2P_TEST_SET(NAT_SYMMETRIC_THEN_CONE) |
| 1195 | P2P_TEST_SET(BLOCK_UDP) |
| 1196 | P2P_TEST_SET(BLOCK_UDP_AND_INCOMING_TCP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1197 | P2P_TEST_SET(BLOCK_ALL_BUT_OUTGOING_HTTP) |
| 1198 | P2P_TEST_SET(PROXY_HTTPS) |
| 1199 | P2P_TEST_SET(PROXY_SOCKS) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1200 | |
| 1201 | // Test that we restart candidate allocation when local ufrag&pwd changed. |
| 1202 | // Standard Ice protocol is used. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1203 | TEST_F(P2PTransportChannelTest, HandleUfragPwdChange) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1204 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1205 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1206 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1207 | TestHandleIceUfragPasswordChanged(); |
| 1208 | DestroyChannels(); |
| 1209 | } |
| 1210 | |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1211 | // Same as above test, but with a symmetric NAT. |
| 1212 | // We should end up with relay<->prflx candidate pairs, with generation "1". |
| 1213 | TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeSymmetricNat) { |
| 1214 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 1215 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1216 | CreateChannels(); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1217 | TestHandleIceUfragPasswordChanged(); |
| 1218 | DestroyChannels(); |
| 1219 | } |
| 1220 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1221 | // Test the operation of GetStats. |
| 1222 | TEST_F(P2PTransportChannelTest, GetStats) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1223 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1224 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1225 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1226 | CreateChannels(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1227 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1228 | ep2_ch1()->receiving() && |
| 1229 | ep2_ch1()->writable(), |
| 1230 | kMediumTimeout, clock); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1231 | TestSendRecv(&clock); |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 1232 | IceTransportStats ice_transport_stats; |
| 1233 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 1234 | ASSERT_GE(ice_transport_stats.connection_infos.size(), 1u); |
| 1235 | ASSERT_GE(ice_transport_stats.candidate_stats_list.size(), 1u); |
| 1236 | EXPECT_EQ(ice_transport_stats.selected_candidate_pair_changes, 1u); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1237 | ConnectionInfo* best_conn_info = nullptr; |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 1238 | for (ConnectionInfo& info : ice_transport_stats.connection_infos) { |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1239 | if (info.best_connection) { |
| 1240 | best_conn_info = &info; |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
| 1244 | ASSERT_TRUE(best_conn_info != nullptr); |
| 1245 | EXPECT_TRUE(best_conn_info->new_connection); |
| 1246 | EXPECT_TRUE(best_conn_info->receiving); |
| 1247 | EXPECT_TRUE(best_conn_info->writable); |
| 1248 | EXPECT_FALSE(best_conn_info->timeout); |
| 1249 | EXPECT_EQ(10U, best_conn_info->sent_total_packets); |
| 1250 | EXPECT_EQ(0U, best_conn_info->sent_discarded_packets); |
| 1251 | EXPECT_EQ(10 * 36U, best_conn_info->sent_total_bytes); |
| 1252 | EXPECT_EQ(10 * 36U, best_conn_info->recv_total_bytes); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1253 | DestroyChannels(); |
| 1254 | } |
| 1255 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1256 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1257 | // is disconnected. |
| 1258 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileDisconnected) { |
| 1259 | rtc::ScopedFakeClock clock; |
| 1260 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1261 | |
| 1262 | CreateChannels(); |
| 1263 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1264 | ep2_ch1()->receiving() && |
| 1265 | ep2_ch1()->writable(), |
| 1266 | kDefaultTimeout, clock); |
| 1267 | |
| 1268 | // Drop all packets so that both channels become not writable. |
| 1269 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 1270 | const int kWriteTimeoutDelay = 8000; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1271 | EXPECT_TRUE_SIMULATED_WAIT(!ep1_ch1()->writable() && !ep2_ch1()->writable(), |
| 1272 | kWriteTimeoutDelay, clock); |
| 1273 | |
| 1274 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1275 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1276 | ep1_ch1()->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1277 | EXPECT_EQ(1, webrtc::metrics::NumEvents( |
| 1278 | "WebRTC.PeerConnection.IceRestartState", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1279 | static_cast<int>(IceRestartState::DISCONNECTED))); |
| 1280 | |
| 1281 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1282 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1283 | ep2_ch1()->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1284 | EXPECT_EQ(2, webrtc::metrics::NumEvents( |
| 1285 | "WebRTC.PeerConnection.IceRestartState", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1286 | static_cast<int>(IceRestartState::DISCONNECTED))); |
| 1287 | |
| 1288 | DestroyChannels(); |
| 1289 | } |
| 1290 | |
| 1291 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1292 | // is connected. |
| 1293 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileConnected) { |
| 1294 | rtc::ScopedFakeClock clock; |
| 1295 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1296 | |
| 1297 | CreateChannels(); |
| 1298 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1299 | ep2_ch1()->receiving() && |
| 1300 | ep2_ch1()->writable(), |
| 1301 | kDefaultTimeout, clock); |
| 1302 | |
| 1303 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1304 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1305 | ep1_ch1()->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1306 | EXPECT_EQ(1, webrtc::metrics::NumEvents( |
| 1307 | "WebRTC.PeerConnection.IceRestartState", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1308 | static_cast<int>(IceRestartState::CONNECTED))); |
| 1309 | |
| 1310 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1311 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1312 | ep2_ch1()->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1313 | EXPECT_EQ(2, webrtc::metrics::NumEvents( |
| 1314 | "WebRTC.PeerConnection.IceRestartState", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1315 | static_cast<int>(IceRestartState::CONNECTED))); |
| 1316 | |
| 1317 | DestroyChannels(); |
| 1318 | } |
| 1319 | |
| 1320 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1321 | // is connecting. |
| 1322 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileConnecting) { |
| 1323 | rtc::ScopedFakeClock clock; |
| 1324 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1325 | |
| 1326 | // Create the channels without waiting for them to become connected. |
| 1327 | CreateChannels(); |
| 1328 | |
| 1329 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1330 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1331 | ep1_ch1()->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1332 | EXPECT_EQ(1, webrtc::metrics::NumEvents( |
| 1333 | "WebRTC.PeerConnection.IceRestartState", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1334 | static_cast<int>(IceRestartState::CONNECTING))); |
| 1335 | |
| 1336 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1337 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1338 | ep2_ch1()->MaybeStartGathering(); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1339 | EXPECT_EQ(2, webrtc::metrics::NumEvents( |
| 1340 | "WebRTC.PeerConnection.IceRestartState", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1341 | static_cast<int>(IceRestartState::CONNECTING))); |
| 1342 | |
| 1343 | DestroyChannels(); |
| 1344 | } |
| 1345 | |
| 1346 | // Tests that a UMA on ICE regathering is recorded when there is a network |
| 1347 | // change if and only if continual gathering is enabled. |
| 1348 | TEST_F(P2PTransportChannelTest, |
| 1349 | TestIceRegatheringReasonContinualGatheringByNetworkChange) { |
| 1350 | rtc::ScopedFakeClock clock; |
| 1351 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1352 | |
| 1353 | // ep1 gathers continually but ep2 does not. |
| 1354 | IceConfig continual_gathering_config = |
| 1355 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1356 | IceConfig default_config; |
| 1357 | CreateChannels(continual_gathering_config, default_config); |
| 1358 | |
| 1359 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1360 | ep2_ch1()->receiving() && |
| 1361 | ep2_ch1()->writable(), |
| 1362 | kDefaultTimeout, clock); |
| 1363 | |
| 1364 | // Adding address in ep1 will trigger continual gathering. |
| 1365 | AddAddress(0, kAlternateAddrs[0]); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1366 | EXPECT_EQ_SIMULATED_WAIT(1, |
| 1367 | GetEndpoint(0)->GetIceRegatheringCountForReason( |
| 1368 | IceRegatheringReason::NETWORK_CHANGE), |
| 1369 | kDefaultTimeout, clock); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1370 | |
| 1371 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1372 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1373 | ep2_ch1()->MaybeStartGathering(); |
| 1374 | |
| 1375 | AddAddress(1, kAlternateAddrs[1]); |
| 1376 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 1377 | // ep2 has not enabled continual gathering. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1378 | EXPECT_EQ(0, GetEndpoint(1)->GetIceRegatheringCountForReason( |
| 1379 | IceRegatheringReason::NETWORK_CHANGE)); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1380 | |
| 1381 | DestroyChannels(); |
| 1382 | } |
| 1383 | |
| 1384 | // Tests that a UMA on ICE regathering is recorded when there is a network |
| 1385 | // failure if and only if continual gathering is enabled. |
| 1386 | TEST_F(P2PTransportChannelTest, |
| 1387 | TestIceRegatheringReasonContinualGatheringByNetworkFailure) { |
| 1388 | rtc::ScopedFakeClock clock; |
| 1389 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1390 | |
| 1391 | // ep1 gathers continually but ep2 does not. |
| 1392 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 1393 | config1.regather_on_failed_networks_interval = 2000; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1394 | IceConfig config2; |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 1395 | config2.regather_on_failed_networks_interval = 2000; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1396 | CreateChannels(config1, config2); |
| 1397 | |
| 1398 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1399 | ep2_ch1()->receiving() && |
| 1400 | ep2_ch1()->writable(), |
| 1401 | kDefaultTimeout, clock); |
| 1402 | |
| 1403 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 1404 | // Timeout value such that all connections are deleted. |
| 1405 | const int kNetworkFailureTimeout = 35000; |
| 1406 | SIMULATED_WAIT(false, kNetworkFailureTimeout, clock); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1407 | EXPECT_LE(1, GetEndpoint(0)->GetIceRegatheringCountForReason( |
| 1408 | IceRegatheringReason::NETWORK_FAILURE)); |
| 1409 | EXPECT_LE(1, webrtc::metrics::NumEvents( |
| 1410 | "WebRTC.PeerConnection.IceRegatheringReason", |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1411 | static_cast<int>(IceRegatheringReason::NETWORK_FAILURE))); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1412 | EXPECT_EQ(0, GetEndpoint(1)->GetIceRegatheringCountForReason( |
| 1413 | IceRegatheringReason::NETWORK_FAILURE)); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1414 | |
| 1415 | DestroyChannels(); |
| 1416 | } |
| 1417 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1418 | // Tests that ICE regathering occurs regularly when |
| 1419 | // regather_all_networks_interval_range configuration value is set. |
| 1420 | TEST_F(P2PTransportChannelTest, TestIceRegatherOnAllNetworksContinual) { |
| 1421 | rtc::ScopedFakeClock clock; |
| 1422 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1423 | |
| 1424 | // ep1 gathers continually but ep2 does not. |
| 1425 | const int kRegatherInterval = 2000; |
| 1426 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1427 | config1.regather_all_networks_interval_range.emplace(kRegatherInterval, |
| 1428 | kRegatherInterval); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1429 | IceConfig config2; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1430 | CreateChannels(config1, config2); |
| 1431 | |
| 1432 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1433 | ep2_ch1()->receiving() && |
| 1434 | ep2_ch1()->writable(), |
| 1435 | kDefaultTimeout, clock); |
| 1436 | |
| 1437 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 1438 | // Timeout value such that all connections are deleted. |
| 1439 | const int kNetworkGatherDuration = 11000; |
| 1440 | SIMULATED_WAIT(false, kNetworkGatherDuration, clock); |
| 1441 | // Expect regathering to happen 5 times in 11s with 2s interval. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1442 | EXPECT_LE(5, GetEndpoint(0)->GetIceRegatheringCountForReason( |
| 1443 | IceRegatheringReason::OCCASIONAL_REFRESH)); |
| 1444 | EXPECT_LE(5, webrtc::metrics::NumEvents( |
| 1445 | "WebRTC.PeerConnection.IceRegatheringReason", |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1446 | static_cast<int>(IceRegatheringReason::OCCASIONAL_REFRESH))); |
| 1447 | // Expect no regathering if continual gathering not configured. |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1448 | EXPECT_EQ(0, GetEndpoint(1)->GetIceRegatheringCountForReason( |
| 1449 | IceRegatheringReason::OCCASIONAL_REFRESH)); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1450 | |
| 1451 | DestroyChannels(); |
| 1452 | } |
| 1453 | |
| 1454 | // Test that ICE periodic regathering can change the selected connection on the |
| 1455 | // specified interval and that the peers can communicate over the new |
| 1456 | // connection. The test is parameterized to test that it works when regathering |
| 1457 | // is done by the ICE controlling peer and when done by the controlled peer. |
| 1458 | class P2PTransportRegatherAllNetworksTest : public P2PTransportChannelTest { |
| 1459 | protected: |
| 1460 | void TestWithRoles(IceRole p1_role, IceRole p2_role) { |
| 1461 | rtc::ScopedFakeClock clock; |
| 1462 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1463 | kDefaultPortAllocatorFlags); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1464 | set_force_relay(true); |
| 1465 | |
| 1466 | const int kRegatherInterval = 2000; |
| 1467 | const int kNumRegathers = 2; |
| 1468 | |
| 1469 | // Set up peer 1 to auto regather every 2s. |
| 1470 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1471 | config1.regather_all_networks_interval_range.emplace(kRegatherInterval, |
| 1472 | kRegatherInterval); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1473 | IceConfig config2 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1474 | |
| 1475 | // Set peer roles. |
| 1476 | SetIceRole(0, p1_role); |
| 1477 | SetIceRole(1, p2_role); |
| 1478 | |
| 1479 | CreateChannels(config1, config2); |
| 1480 | |
| 1481 | // Wait for initial connection to be made. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1482 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1483 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1484 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1485 | kMediumTimeout, clock); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1486 | |
| 1487 | const Connection* initial_selected = ep1_ch1()->selected_connection(); |
| 1488 | |
| 1489 | // Wait long enough for 2 regathering cycles to happen plus some extra so |
| 1490 | // the new connection has time to settle. |
| 1491 | const int kWaitRegather = |
| 1492 | kRegatherInterval * kNumRegathers + kRegatherInterval / 2; |
| 1493 | SIMULATED_WAIT(false, kWaitRegather, clock); |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 1494 | EXPECT_EQ(kNumRegathers, GetEndpoint(0)->GetIceRegatheringCountForReason( |
| 1495 | IceRegatheringReason::OCCASIONAL_REFRESH)); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1496 | |
| 1497 | const Connection* new_selected = ep1_ch1()->selected_connection(); |
| 1498 | |
| 1499 | // Want the new selected connection to be different. |
| 1500 | ASSERT_NE(initial_selected, new_selected); |
| 1501 | |
| 1502 | // Make sure we can communicate over the new connection too. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1503 | TestSendRecv(&clock); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1504 | } |
| 1505 | }; |
| 1506 | |
| 1507 | TEST_F(P2PTransportRegatherAllNetworksTest, TestControlling) { |
| 1508 | TestWithRoles(ICEROLE_CONTROLLING, ICEROLE_CONTROLLED); |
| 1509 | } |
| 1510 | |
| 1511 | TEST_F(P2PTransportRegatherAllNetworksTest, TestControlled) { |
| 1512 | TestWithRoles(ICEROLE_CONTROLLED, ICEROLE_CONTROLLING); |
| 1513 | } |
| 1514 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1515 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1516 | // when the signaling is slow. |
| 1517 | TEST_F(P2PTransportChannelTest, PeerReflexiveCandidateBeforeSignaling) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1518 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1519 | kDefaultPortAllocatorFlags); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1520 | // Emulate no remote parameters coming in. |
| 1521 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1522 | CreateChannels(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1523 | // Only have remote parameters come in for ep2, not ep1. |
| 1524 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1525 | |
| 1526 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1527 | // candidate. |
| 1528 | PauseCandidates(1); |
| 1529 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1530 | // Wait until the callee becomes writable to make sure that a ping request is |
| 1531 | // received by the caller before his remote ICE credentials are set. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1532 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, kMediumTimeout); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1533 | // Add two sets of remote ICE credentials, so that the ones used by the |
| 1534 | // candidate will be generation 1 instead of 0. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1535 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1536 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1537 | // The caller should have the selected connection connected to the peer |
| 1538 | // reflexive candidate. |
| 1539 | const Connection* selected_connection = nullptr; |
| 1540 | ASSERT_TRUE_WAIT( |
| 1541 | (selected_connection = ep1_ch1()->selected_connection()) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1542 | kMediumTimeout); |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1543 | EXPECT_EQ(PRFLX_PORT_TYPE, selected_connection->remote_candidate().type()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1544 | EXPECT_EQ(kIceUfrag[1], selected_connection->remote_candidate().username()); |
| 1545 | EXPECT_EQ(kIcePwd[1], selected_connection->remote_candidate().password()); |
| 1546 | EXPECT_EQ(1u, selected_connection->remote_candidate().generation()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1547 | |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1548 | ResumeCandidates(1); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1549 | // Verify ep1's selected connection is updated to use the 'local' candidate. |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1550 | EXPECT_EQ_WAIT(LOCAL_PORT_TYPE, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1551 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1552 | kMediumTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1553 | EXPECT_EQ(selected_connection, ep1_ch1()->selected_connection()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1554 | DestroyChannels(); |
| 1555 | } |
| 1556 | |
Qingsi Wang | 7627fdd | 2019-08-19 16:07:40 -0700 | [diff] [blame] | 1557 | // Test that if we learn a prflx remote candidate, its address is concealed in |
| 1558 | // 1. the selected candidate pair accessed via the public API, and |
| 1559 | // 2. the candidate pair stats |
| 1560 | // until we learn the same address from signaling. |
| 1561 | TEST_F(P2PTransportChannelTest, PeerReflexiveRemoteCandidateIsSanitized) { |
| 1562 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1563 | // Emulate no remote parameters coming in. |
| 1564 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
| 1565 | CreateChannels(); |
| 1566 | // Only have remote parameters come in for ep2, not ep1. |
| 1567 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
| 1568 | |
| 1569 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1570 | // candidate. |
| 1571 | PauseCandidates(1); |
| 1572 | |
| 1573 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, kMediumTimeout); |
| 1574 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 1575 | ASSERT_TRUE_WAIT(ep1_ch1()->selected_connection() != nullptr, kMediumTimeout); |
| 1576 | |
| 1577 | // Check the selected candidate pair. |
| 1578 | auto pair_ep1 = ep1_ch1()->GetSelectedCandidatePair(); |
| 1579 | ASSERT_TRUE(pair_ep1.has_value()); |
| 1580 | EXPECT_EQ(PRFLX_PORT_TYPE, pair_ep1->remote_candidate().type()); |
| 1581 | EXPECT_TRUE(pair_ep1->remote_candidate().address().ipaddr().IsNil()); |
| 1582 | |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 1583 | IceTransportStats ice_transport_stats; |
| 1584 | ep1_ch1()->GetStats(&ice_transport_stats); |
Qingsi Wang | 7627fdd | 2019-08-19 16:07:40 -0700 | [diff] [blame] | 1585 | // Check the candidate pair stats. |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 1586 | ASSERT_EQ(1u, ice_transport_stats.connection_infos.size()); |
| 1587 | EXPECT_EQ(PRFLX_PORT_TYPE, |
| 1588 | ice_transport_stats.connection_infos[0].remote_candidate.type()); |
| 1589 | EXPECT_TRUE(ice_transport_stats.connection_infos[0] |
| 1590 | .remote_candidate.address() |
| 1591 | .ipaddr() |
| 1592 | .IsNil()); |
Qingsi Wang | 7627fdd | 2019-08-19 16:07:40 -0700 | [diff] [blame] | 1593 | |
| 1594 | // Let ep1 receive the remote candidate to update its type from prflx to host. |
| 1595 | ResumeCandidates(1); |
| 1596 | ASSERT_TRUE_WAIT( |
| 1597 | ep1_ch1()->selected_connection() != nullptr && |
| 1598 | ep1_ch1()->selected_connection()->remote_candidate().type() == |
| 1599 | LOCAL_PORT_TYPE, |
| 1600 | kMediumTimeout); |
| 1601 | |
| 1602 | // We should be able to reveal the address after it is learnt via |
| 1603 | // AddIceCandidate. |
| 1604 | // |
| 1605 | // Check the selected candidate pair. |
| 1606 | auto updated_pair_ep1 = ep1_ch1()->GetSelectedCandidatePair(); |
| 1607 | ASSERT_TRUE(updated_pair_ep1.has_value()); |
| 1608 | EXPECT_EQ(LOCAL_PORT_TYPE, updated_pair_ep1->remote_candidate().type()); |
| 1609 | EXPECT_TRUE( |
| 1610 | updated_pair_ep1->remote_candidate().address().EqualIPs(kPublicAddrs[1])); |
| 1611 | |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 1612 | ep1_ch1()->GetStats(&ice_transport_stats); |
Qingsi Wang | 7627fdd | 2019-08-19 16:07:40 -0700 | [diff] [blame] | 1613 | // Check the candidate pair stats. |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 1614 | ASSERT_EQ(1u, ice_transport_stats.connection_infos.size()); |
| 1615 | EXPECT_EQ(LOCAL_PORT_TYPE, |
| 1616 | ice_transport_stats.connection_infos[0].remote_candidate.type()); |
| 1617 | EXPECT_TRUE(ice_transport_stats.connection_infos[0] |
| 1618 | .remote_candidate.address() |
| 1619 | .EqualIPs(kPublicAddrs[1])); |
Qingsi Wang | 7627fdd | 2019-08-19 16:07:40 -0700 | [diff] [blame] | 1620 | |
| 1621 | DestroyChannels(); |
| 1622 | } |
| 1623 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1624 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1625 | // when the signaling is slow and the end points are behind NAT. |
| 1626 | TEST_F(P2PTransportChannelTest, PeerReflexiveCandidateBeforeSignalingWithNAT) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1627 | ConfigureEndpoints(OPEN, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1628 | kDefaultPortAllocatorFlags); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1629 | // Emulate no remote parameters coming in. |
| 1630 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1631 | CreateChannels(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1632 | // Only have remote parameters come in for ep2, not ep1. |
| 1633 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1634 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1635 | // candidate. |
| 1636 | PauseCandidates(1); |
| 1637 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1638 | // Wait until the callee becomes writable to make sure that a ping request is |
| 1639 | // received by the caller before his remote ICE credentials are set. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1640 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, kMediumTimeout); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1641 | // Add two sets of remote ICE credentials, so that the ones used by the |
| 1642 | // candidate will be generation 1 instead of 0. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1643 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1644 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1645 | |
| 1646 | // The caller's selected connection should be connected to the peer reflexive |
| 1647 | // candidate. |
| 1648 | const Connection* selected_connection = nullptr; |
| 1649 | ASSERT_TRUE_WAIT( |
| 1650 | (selected_connection = ep1_ch1()->selected_connection()) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1651 | kMediumTimeout); |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1652 | EXPECT_EQ(PRFLX_PORT_TYPE, selected_connection->remote_candidate().type()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1653 | EXPECT_EQ(kIceUfrag[1], selected_connection->remote_candidate().username()); |
| 1654 | EXPECT_EQ(kIcePwd[1], selected_connection->remote_candidate().password()); |
| 1655 | EXPECT_EQ(1u, selected_connection->remote_candidate().generation()); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1656 | |
| 1657 | ResumeCandidates(1); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 1658 | |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1659 | EXPECT_EQ_WAIT(PRFLX_PORT_TYPE, |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1660 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1661 | kMediumTimeout); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1662 | EXPECT_EQ(selected_connection, ep1_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1663 | DestroyChannels(); |
| 1664 | } |
| 1665 | |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1666 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1667 | // when the signaling is slow, even if the new candidate is created due to the |
| 1668 | // remote peer doing an ICE restart, pairing this candidate across generations. |
| 1669 | // |
| 1670 | // Previously this wasn't working due to a bug where the peer reflexive |
| 1671 | // candidate was only updated for the newest generation candidate pairs, and |
| 1672 | // not older-generation candidate pairs created by pairing candidates across |
| 1673 | // generations. This resulted in the old-generation prflx candidate being |
| 1674 | // prioritized above new-generation candidate pairs. |
| 1675 | TEST_F(P2PTransportChannelTest, |
| 1676 | PeerReflexiveCandidateBeforeSignalingWithIceRestart) { |
| 1677 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1678 | kDefaultPortAllocatorFlags); |
| 1679 | // Only gather relay candidates, so that when the prflx candidate arrives |
| 1680 | // it's prioritized above the current candidate pair. |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 1681 | GetEndpoint(0)->allocator_->SetCandidateFilter(CF_RELAY); |
| 1682 | GetEndpoint(1)->allocator_->SetCandidateFilter(CF_RELAY); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1683 | // Setting this allows us to control when SetRemoteIceParameters is called. |
| 1684 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1685 | CreateChannels(); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1686 | // Wait for the initial connection to be made. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1687 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 1688 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1689 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1690 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1691 | kDefaultTimeout); |
| 1692 | |
| 1693 | // Simulate an ICE restart on ep2, but don't signal the candidate or new |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1694 | // ICE parameters until after a prflx connection has been made. |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1695 | PauseCandidates(1); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1696 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1697 | |
| 1698 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1699 | ep2_ch1()->MaybeStartGathering(); |
| 1700 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1701 | // The caller should have the selected connection connected to the peer |
| 1702 | // reflexive candidate. |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1703 | EXPECT_EQ_WAIT(PRFLX_PORT_TYPE, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1704 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1705 | kDefaultTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1706 | const Connection* prflx_selected_connection = |
| 1707 | ep1_ch1()->selected_connection(); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1708 | |
| 1709 | // Now simulate the ICE restart on ep1. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1710 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1711 | |
| 1712 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1713 | ep1_ch1()->MaybeStartGathering(); |
| 1714 | |
| 1715 | // Finally send the candidates from ep2's ICE restart and verify that ep1 uses |
| 1716 | // their information to update the peer reflexive candidate. |
| 1717 | ResumeCandidates(1); |
| 1718 | |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1719 | EXPECT_EQ_WAIT(RELAY_PORT_TYPE, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1720 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1721 | kDefaultTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1722 | EXPECT_EQ(prflx_selected_connection, ep1_ch1()->selected_connection()); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1723 | DestroyChannels(); |
| 1724 | } |
| 1725 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1726 | // Test that if remote candidates don't have ufrag and pwd, we still work. |
| 1727 | TEST_F(P2PTransportChannelTest, RemoteCandidatesWithoutUfragPwd) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1728 | rtc::ScopedFakeClock clock; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1729 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1730 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1731 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1732 | CreateChannels(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1733 | const Connection* selected_connection = NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1734 | // Wait until the callee's connections are created. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1735 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1736 | (selected_connection = ep2_ch1()->selected_connection()) != NULL, |
| 1737 | kMediumTimeout, clock); |
| 1738 | // Wait to make sure the selected connection is not changed. |
| 1739 | SIMULATED_WAIT(ep2_ch1()->selected_connection() != selected_connection, |
| 1740 | kShortTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1741 | EXPECT_TRUE(ep2_ch1()->selected_connection() == selected_connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1742 | DestroyChannels(); |
| 1743 | } |
| 1744 | |
| 1745 | // Test that a host behind NAT cannot be reached when incoming_only |
| 1746 | // is set to true. |
| 1747 | TEST_F(P2PTransportChannelTest, IncomingOnlyBlocked) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1748 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1749 | ConfigureEndpoints(NAT_FULL_CONE, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1750 | kDefaultPortAllocatorFlags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1751 | |
| 1752 | SetAllocatorFlags(0, kOnlyLocalPorts); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1753 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1754 | ep1_ch1()->set_incoming_only(true); |
| 1755 | |
| 1756 | // Pump for 1 second and verify that the channels are not connected. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1757 | SIMULATED_WAIT(false, kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1758 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1759 | EXPECT_FALSE(ep1_ch1()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1760 | EXPECT_FALSE(ep1_ch1()->writable()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1761 | EXPECT_FALSE(ep2_ch1()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1762 | EXPECT_FALSE(ep2_ch1()->writable()); |
| 1763 | |
| 1764 | DestroyChannels(); |
| 1765 | } |
| 1766 | |
| 1767 | // Test that a peer behind NAT can connect to a peer that has |
| 1768 | // incoming_only flag set. |
| 1769 | TEST_F(P2PTransportChannelTest, IncomingOnlyOpen) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1770 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1771 | ConfigureEndpoints(OPEN, NAT_FULL_CONE, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1772 | kDefaultPortAllocatorFlags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1773 | |
| 1774 | SetAllocatorFlags(0, kOnlyLocalPorts); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1775 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1776 | ep1_ch1()->set_incoming_only(true); |
| 1777 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1778 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1779 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 1780 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 1781 | ep2_ch1()->writable(), |
| 1782 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1783 | |
| 1784 | DestroyChannels(); |
| 1785 | } |
| 1786 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1787 | // Test that two peers can connect when one can only make outgoing TCP |
| 1788 | // connections. This has been observed in some scenarios involving |
| 1789 | // VPNs/firewalls. |
| 1790 | TEST_F(P2PTransportChannelTest, CanOnlyMakeOutgoingTcpConnections) { |
Mirko Bonadei | 5f4d47b | 2018-08-22 17:41:22 +0000 | [diff] [blame] | 1791 | // The PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS flag is required if the |
| 1792 | // application needs this use case to work, since the application must accept |
| 1793 | // the tradeoff that more candidates need to be allocated. |
| 1794 | // |
| 1795 | // TODO(deadbeef): Later, make this flag the default, and do more elegant |
| 1796 | // things to ensure extra candidates don't waste resources? |
| 1797 | ConfigureEndpoints( |
| 1798 | OPEN, OPEN, |
| 1799 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS, |
| 1800 | kDefaultPortAllocatorFlags); |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1801 | // In order to simulate nothing working but outgoing TCP connections, prevent |
| 1802 | // the endpoint from binding to its interface's address as well as the |
| 1803 | // "any" addresses. It can then only make a connection by using "Connect()". |
| 1804 | fw()->SetUnbindableIps({rtc::GetAnyIP(AF_INET), rtc::GetAnyIP(AF_INET6), |
| 1805 | kPublicAddrs[0].ipaddr()}); |
| 1806 | CreateChannels(); |
| 1807 | // Expect a "prflx" candidate on the side that can only make outgoing |
| 1808 | // connections, endpoint 0. |
| 1809 | Test(kPrflxTcpToLocalTcp); |
| 1810 | DestroyChannels(); |
| 1811 | } |
| 1812 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1813 | TEST_F(P2PTransportChannelTest, TestTcpConnectionsFromActiveToPassive) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1814 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1815 | AddAddress(0, kPublicAddrs[0]); |
| 1816 | AddAddress(1, kPublicAddrs[1]); |
| 1817 | |
| 1818 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1819 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1820 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1821 | int kOnlyLocalTcpPorts = PORTALLOCATOR_DISABLE_UDP | |
| 1822 | PORTALLOCATOR_DISABLE_STUN | |
| 1823 | PORTALLOCATOR_DISABLE_RELAY; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1824 | // Disable all protocols except TCP. |
| 1825 | SetAllocatorFlags(0, kOnlyLocalTcpPorts); |
| 1826 | SetAllocatorFlags(1, kOnlyLocalTcpPorts); |
| 1827 | |
| 1828 | SetAllowTcpListen(0, true); // actpass. |
| 1829 | SetAllowTcpListen(1, false); // active. |
| 1830 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1831 | // We want SetRemoteIceParameters to be called as it normally would. |
| 1832 | // Otherwise we won't know what parameters to use for the expected |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1833 | // prflx TCP candidates. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1834 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1835 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1836 | // Pause candidate so we could verify the candidate properties. |
| 1837 | PauseCandidates(0); |
| 1838 | PauseCandidates(1); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1839 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1840 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1841 | // Verify tcp candidates. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1842 | VerifySavedTcpCandidates(0, TCPTYPE_PASSIVE_STR); |
| 1843 | VerifySavedTcpCandidates(1, TCPTYPE_ACTIVE_STR); |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1844 | |
| 1845 | // Resume candidates. |
| 1846 | ResumeCandidates(0); |
| 1847 | ResumeCandidates(1); |
| 1848 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1849 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1850 | ep2_ch1()->receiving() && |
| 1851 | ep2_ch1()->writable(), |
| 1852 | kShortTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1853 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1854 | ep2_ch1()->selected_connection() && |
| 1855 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 1856 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1857 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1858 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1859 | DestroyChannels(); |
| 1860 | } |
| 1861 | |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 1862 | TEST_F(P2PTransportChannelTest, TestIceRoleConflict) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1863 | AddAddress(0, kPublicAddrs[0]); |
| 1864 | AddAddress(1, kPublicAddrs[1]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1865 | TestSignalRoleConflict(); |
| 1866 | } |
| 1867 | |
| 1868 | // Tests that the ice configs (protocol, tiebreaker and role) can be passed |
| 1869 | // down to ports. |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1870 | TEST_F(P2PTransportChannelTest, TestIceConfigWillPassDownToPort) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1871 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1872 | AddAddress(0, kPublicAddrs[0]); |
| 1873 | AddAddress(1, kPublicAddrs[1]); |
| 1874 | |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1875 | // Give the first connection the higher tiebreaker so its role won't |
| 1876 | // change unless we tell it to. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1877 | SetIceRole(0, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1878 | SetIceTiebreaker(0, kHighTiebreaker); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1879 | SetIceRole(1, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1880 | SetIceTiebreaker(1, kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1881 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1882 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1883 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1884 | EXPECT_EQ_SIMULATED_WAIT(2u, ep1_ch1()->ports().size(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1885 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1886 | const std::vector<PortInterface*> ports_before = ep1_ch1()->ports(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1887 | for (size_t i = 0; i < ports_before.size(); ++i) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1888 | EXPECT_EQ(ICEROLE_CONTROLLING, ports_before[i]->GetIceRole()); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1889 | EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1892 | ep1_ch1()->SetIceRole(ICEROLE_CONTROLLED); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1893 | ep1_ch1()->SetIceTiebreaker(kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1894 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1895 | const std::vector<PortInterface*> ports_after = ep1_ch1()->ports(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1896 | for (size_t i = 0; i < ports_after.size(); ++i) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1897 | EXPECT_EQ(ICEROLE_CONTROLLED, ports_before[i]->GetIceRole()); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1898 | // SetIceTiebreaker after ports have been created will fail. So expect the |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1899 | // original value. |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1900 | EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1903 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1904 | ep2_ch1()->receiving() && |
| 1905 | ep2_ch1()->writable(), |
| 1906 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1907 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1908 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1909 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1910 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1911 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1912 | DestroyChannels(); |
| 1913 | } |
| 1914 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1915 | // Verify that we can set DSCP value and retrieve properly from P2PTC. |
| 1916 | TEST_F(P2PTransportChannelTest, TestDefaultDscpValue) { |
| 1917 | AddAddress(0, kPublicAddrs[0]); |
| 1918 | AddAddress(1, kPublicAddrs[1]); |
| 1919 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1920 | CreateChannels(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1921 | EXPECT_EQ(rtc::DSCP_NO_CHANGE, GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1922 | EXPECT_EQ(rtc::DSCP_NO_CHANGE, GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1923 | GetEndpoint(0)->cd1_.ch_->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS6); |
| 1924 | GetEndpoint(1)->cd1_.ch_->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS6); |
| 1925 | EXPECT_EQ(rtc::DSCP_CS6, GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1926 | EXPECT_EQ(rtc::DSCP_CS6, GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1927 | GetEndpoint(0)->cd1_.ch_->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF41); |
| 1928 | GetEndpoint(1)->cd1_.ch_->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF41); |
| 1929 | EXPECT_EQ(rtc::DSCP_AF41, GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1930 | EXPECT_EQ(rtc::DSCP_AF41, GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | // Verify IPv6 connection is preferred over IPv4. |
guoweis@webrtc.org | 1f05c45 | 2014-12-15 21:25:54 +0000 | [diff] [blame] | 1934 | TEST_F(P2PTransportChannelTest, TestIPv6Connections) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1935 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1936 | AddAddress(0, kIPv6PublicAddrs[0]); |
| 1937 | AddAddress(0, kPublicAddrs[0]); |
| 1938 | AddAddress(1, kIPv6PublicAddrs[1]); |
| 1939 | AddAddress(1, kPublicAddrs[1]); |
| 1940 | |
| 1941 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1942 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1943 | |
| 1944 | // Enable IPv6 |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 1945 | SetAllocatorFlags( |
| 1946 | 0, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 1947 | SetAllocatorFlags( |
| 1948 | 1, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1949 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1950 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1951 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1952 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1953 | ep2_ch1()->receiving() && |
| 1954 | ep2_ch1()->writable(), |
| 1955 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1956 | EXPECT_TRUE( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1957 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1958 | LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) && |
| 1959 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1])); |
| 1960 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1961 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1962 | DestroyChannels(); |
| 1963 | } |
| 1964 | |
| 1965 | // Testing forceful TURN connections. |
| 1966 | TEST_F(P2PTransportChannelTest, TestForceTurn) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1967 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1968 | ConfigureEndpoints( |
| 1969 | NAT_PORT_RESTRICTED, NAT_SYMMETRIC, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1970 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 1971 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1972 | set_force_relay(true); |
| 1973 | |
| 1974 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1975 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1976 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1977 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1978 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1979 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1980 | ep2_ch1()->receiving() && |
| 1981 | ep2_ch1()->writable(), |
| 1982 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1983 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1984 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1985 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1986 | |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 1987 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 1988 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 1989 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep2_ch1())->type()); |
| 1990 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep2_ch1())->type()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1991 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 1992 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1993 | DestroyChannels(); |
| 1994 | } |
| 1995 | |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1996 | // Test that if continual gathering is set to true, ICE gathering state will |
| 1997 | // not change to "Complete", and vice versa. |
| 1998 | TEST_F(P2PTransportChannelTest, TestContinualGathering) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1999 | rtc::ScopedFakeClock clock; |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 2000 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2001 | kDefaultPortAllocatorFlags); |
| 2002 | SetAllocationStepDelay(0, kDefaultStepDelay); |
| 2003 | SetAllocationStepDelay(1, kDefaultStepDelay); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2004 | IceConfig continual_gathering_config = |
| 2005 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 2006 | // By default, ep2 does not gather continually. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2007 | IceConfig default_config; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2008 | CreateChannels(continual_gathering_config, default_config); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 2009 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2010 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2011 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 2012 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 2013 | ep2_ch1()->writable(), |
| 2014 | kMediumTimeout, clock); |
| 2015 | SIMULATED_WAIT( |
| 2016 | IceGatheringState::kIceGatheringComplete == ep1_ch1()->gathering_state(), |
| 2017 | kShortTimeout, clock); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2018 | EXPECT_EQ(IceGatheringState::kIceGatheringGathering, |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 2019 | ep1_ch1()->gathering_state()); |
| 2020 | // By now, ep2 should have completed gathering. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2021 | EXPECT_EQ(IceGatheringState::kIceGatheringComplete, |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 2022 | ep2_ch1()->gathering_state()); |
| 2023 | |
| 2024 | DestroyChannels(); |
| 2025 | } |
| 2026 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2027 | // Test that a connection succeeds when the P2PTransportChannel uses a pooled |
| 2028 | // PortAllocatorSession that has not yet finished gathering candidates. |
| 2029 | TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2030 | rtc::ScopedFakeClock clock; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2031 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2032 | kDefaultPortAllocatorFlags); |
| 2033 | // First create a pooled session for each endpoint. |
| 2034 | auto& allocator_1 = GetEndpoint(0)->allocator_; |
| 2035 | auto& allocator_2 = GetEndpoint(1)->allocator_; |
| 2036 | int pool_size = 1; |
| 2037 | allocator_1->SetConfiguration(allocator_1->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 2038 | allocator_1->turn_servers(), pool_size, |
| 2039 | webrtc::NO_PRUNE); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2040 | allocator_2->SetConfiguration(allocator_2->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 2041 | allocator_2->turn_servers(), pool_size, |
| 2042 | webrtc::NO_PRUNE); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2043 | const PortAllocatorSession* pooled_session_1 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2044 | allocator_1->GetPooledSession(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2045 | const PortAllocatorSession* pooled_session_2 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2046 | allocator_2->GetPooledSession(); |
| 2047 | ASSERT_NE(nullptr, pooled_session_1); |
| 2048 | ASSERT_NE(nullptr, pooled_session_2); |
| 2049 | // Sanity check that pooled sessions haven't gathered anything yet. |
| 2050 | EXPECT_TRUE(pooled_session_1->ReadyPorts().empty()); |
| 2051 | EXPECT_TRUE(pooled_session_1->ReadyCandidates().empty()); |
| 2052 | EXPECT_TRUE(pooled_session_2->ReadyPorts().empty()); |
| 2053 | EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty()); |
| 2054 | // Now let the endpoints connect and try exchanging some data. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2055 | CreateChannels(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2056 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2057 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 2058 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 2059 | ep2_ch1()->writable(), |
| 2060 | kMediumTimeout, clock); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 2061 | TestSendRecv(&clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2062 | // Make sure the P2PTransportChannels are actually using ports from the |
| 2063 | // pooled sessions. |
| 2064 | auto pooled_ports_1 = pooled_session_1->ReadyPorts(); |
| 2065 | auto pooled_ports_2 = pooled_session_2->ReadyPorts(); |
Steve Anton | ae226f6 | 2019-01-29 12:47:38 -0800 | [diff] [blame] | 2066 | EXPECT_THAT(pooled_ports_1, |
| 2067 | Contains(ep1_ch1()->selected_connection()->port())); |
| 2068 | EXPECT_THAT(pooled_ports_2, |
| 2069 | Contains(ep2_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | // Test that a connection succeeds when the P2PTransportChannel uses a pooled |
| 2073 | // PortAllocatorSession that already finished gathering candidates. |
| 2074 | TEST_F(P2PTransportChannelTest, TestUsingPooledSessionAfterDoneGathering) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2075 | rtc::ScopedFakeClock clock; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2076 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2077 | kDefaultPortAllocatorFlags); |
| 2078 | // First create a pooled session for each endpoint. |
| 2079 | auto& allocator_1 = GetEndpoint(0)->allocator_; |
| 2080 | auto& allocator_2 = GetEndpoint(1)->allocator_; |
| 2081 | int pool_size = 1; |
| 2082 | allocator_1->SetConfiguration(allocator_1->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 2083 | allocator_1->turn_servers(), pool_size, |
| 2084 | webrtc::NO_PRUNE); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2085 | allocator_2->SetConfiguration(allocator_2->stun_servers(), |
Honghai Zhang | f8998cf | 2019-10-14 11:27:50 -0700 | [diff] [blame] | 2086 | allocator_2->turn_servers(), pool_size, |
| 2087 | webrtc::NO_PRUNE); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2088 | const PortAllocatorSession* pooled_session_1 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2089 | allocator_1->GetPooledSession(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2090 | const PortAllocatorSession* pooled_session_2 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2091 | allocator_2->GetPooledSession(); |
| 2092 | ASSERT_NE(nullptr, pooled_session_1); |
| 2093 | ASSERT_NE(nullptr, pooled_session_2); |
| 2094 | // Wait for the pooled sessions to finish gathering before the |
| 2095 | // P2PTransportChannels try to use them. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2096 | EXPECT_TRUE_SIMULATED_WAIT(pooled_session_1->CandidatesAllocationDone() && |
| 2097 | pooled_session_2->CandidatesAllocationDone(), |
| 2098 | kDefaultTimeout, clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2099 | // Now let the endpoints connect and try exchanging some data. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2100 | CreateChannels(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2101 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2102 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 2103 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 2104 | ep2_ch1()->writable(), |
| 2105 | kMediumTimeout, clock); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 2106 | TestSendRecv(&clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2107 | // Make sure the P2PTransportChannels are actually using ports from the |
| 2108 | // pooled sessions. |
| 2109 | auto pooled_ports_1 = pooled_session_1->ReadyPorts(); |
| 2110 | auto pooled_ports_2 = pooled_session_2->ReadyPorts(); |
Steve Anton | ae226f6 | 2019-01-29 12:47:38 -0800 | [diff] [blame] | 2111 | EXPECT_THAT(pooled_ports_1, |
| 2112 | Contains(ep1_ch1()->selected_connection()->port())); |
| 2113 | EXPECT_THAT(pooled_ports_2, |
| 2114 | Contains(ep2_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2115 | } |
| 2116 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2117 | // Test that when the "presume_writable_when_fully_relayed" flag is set to |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2118 | // true and there's a TURN-TURN candidate pair, it's presumed to be writable |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2119 | // as soon as it's created. |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 2120 | // TODO(deadbeef): Move this and other "presumed writable" tests into a test |
| 2121 | // class that operates on a single P2PTransportChannel, once an appropriate one |
| 2122 | // (which supports TURN servers and TURN candidate gathering) is available. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2123 | TEST_F(P2PTransportChannelTest, TurnToTurnPresumedWritable) { |
| 2124 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2125 | kDefaultPortAllocatorFlags); |
| 2126 | // Only configure one channel so we can control when the remote candidate |
| 2127 | // is added. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2128 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2129 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2130 | IceConfig config; |
| 2131 | config.presume_writable_when_fully_relayed = true; |
| 2132 | ep1_ch1()->SetIceConfig(config); |
| 2133 | ep1_ch1()->MaybeStartGathering(); |
| 2134 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 2135 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 2136 | // Add two remote candidates; a host candidate (with higher priority) |
| 2137 | // and TURN candidate. |
| 2138 | ep1_ch1()->AddRemoteCandidate( |
| 2139 | CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 2140 | ep1_ch1()->AddRemoteCandidate( |
| 2141 | CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 0)); |
| 2142 | // Expect that the TURN-TURN candidate pair will be prioritized since it's |
| 2143 | // "probably writable". |
Jonas Oreland | 1230fb7 | 2019-10-25 15:58:14 +0200 | [diff] [blame] | 2144 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() != nullptr, kShortTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2145 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2146 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2147 | // Also expect that the channel instantly indicates that it's writable since |
| 2148 | // it has a TURN-TURN pair. |
| 2149 | EXPECT_TRUE(ep1_ch1()->writable()); |
| 2150 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2151 | // Also make sure we can immediately send packets. |
| 2152 | const char* data = "test"; |
| 2153 | int len = static_cast<int>(strlen(data)); |
| 2154 | EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2157 | // Test that a TURN/peer reflexive candidate pair is also presumed writable. |
| 2158 | TEST_F(P2PTransportChannelTest, TurnToPrflxPresumedWritable) { |
| 2159 | rtc::ScopedFakeClock fake_clock; |
| 2160 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 2161 | // We need to add artificial network delay to verify that the connection |
| 2162 | // is presumed writable before it's actually writable. Without this delay |
| 2163 | // it would become writable instantly. |
| 2164 | virtual_socket_server()->set_delay_mean(50); |
| 2165 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2166 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2167 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 2168 | kDefaultPortAllocatorFlags); |
| 2169 | // We want the remote TURN candidate to show up as prflx. To do this we need |
| 2170 | // to configure the server to accept packets from an address we haven't |
| 2171 | // explicitly installed permission for. |
| 2172 | test_turn_server()->set_enable_permission_checks(false); |
| 2173 | IceConfig config; |
| 2174 | config.presume_writable_when_fully_relayed = true; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2175 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2176 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2177 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 2178 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2179 | ep1_ch1()->SetIceConfig(config); |
| 2180 | ep2_ch1()->SetIceConfig(config); |
| 2181 | // Don't signal candidates from channel 2, so that channel 1 sees the TURN |
| 2182 | // candidate as peer reflexive. |
| 2183 | PauseCandidates(1); |
| 2184 | ep1_ch1()->MaybeStartGathering(); |
| 2185 | ep2_ch1()->MaybeStartGathering(); |
| 2186 | |
| 2187 | // Wait for the TURN<->prflx connection. |
| 2188 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2189 | kShortTimeout, fake_clock); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2190 | ASSERT_NE(nullptr, ep1_ch1()->selected_connection()); |
| 2191 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2192 | EXPECT_EQ(PRFLX_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2193 | // Make sure that at this point the connection is only presumed writable, |
| 2194 | // not fully writable. |
| 2195 | EXPECT_FALSE(ep1_ch1()->selected_connection()->writable()); |
| 2196 | |
| 2197 | // Now wait for it to actually become writable. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2198 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->writable(), |
| 2199 | kShortTimeout, fake_clock); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2200 | |
| 2201 | // Explitly destroy channels, before fake clock is destroyed. |
| 2202 | DestroyChannels(); |
| 2203 | } |
| 2204 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2205 | // Test that a presumed-writable TURN<->TURN connection is preferred above an |
| 2206 | // unreliable connection (one that has failed to be pinged for some time). |
| 2207 | TEST_F(P2PTransportChannelTest, PresumedWritablePreferredOverUnreliable) { |
| 2208 | rtc::ScopedFakeClock fake_clock; |
| 2209 | |
| 2210 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 2211 | kDefaultPortAllocatorFlags); |
| 2212 | IceConfig config; |
| 2213 | config.presume_writable_when_fully_relayed = true; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2214 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2215 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2216 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 2217 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2218 | ep1_ch1()->SetIceConfig(config); |
| 2219 | ep2_ch1()->SetIceConfig(config); |
| 2220 | ep1_ch1()->MaybeStartGathering(); |
| 2221 | ep2_ch1()->MaybeStartGathering(); |
| 2222 | // Wait for initial connection as usual. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2223 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2224 | ep1_ch1()->selected_connection()->writable() && |
| 2225 | ep2_ch1()->receiving() && |
| 2226 | ep2_ch1()->writable() && |
| 2227 | ep2_ch1()->selected_connection()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2228 | kShortTimeout, fake_clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2229 | const Connection* old_selected_connection = ep1_ch1()->selected_connection(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2230 | // Destroy the second channel and wait for the current connection on the |
| 2231 | // first channel to become "unreliable", making it no longer writable. |
| 2232 | GetEndpoint(1)->cd1_.ch_.reset(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2233 | EXPECT_TRUE_SIMULATED_WAIT(!ep1_ch1()->writable(), kDefaultTimeout, |
| 2234 | fake_clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2235 | EXPECT_NE(nullptr, ep1_ch1()->selected_connection()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2236 | // Add a remote TURN candidate. The first channel should still have a TURN |
| 2237 | // port available to make a TURN<->TURN pair that's presumed writable. |
| 2238 | ep1_ch1()->AddRemoteCandidate( |
| 2239 | CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 0)); |
| 2240 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2241 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2242 | EXPECT_TRUE(ep1_ch1()->writable()); |
| 2243 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2244 | EXPECT_NE(old_selected_connection, ep1_ch1()->selected_connection()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2245 | // Explitly destroy channels, before fake clock is destroyed. |
| 2246 | DestroyChannels(); |
| 2247 | } |
| 2248 | |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 2249 | // Ensure that "SignalReadyToSend" is fired as expected with a "presumed |
| 2250 | // writable" connection. Previously this did not work. |
| 2251 | TEST_F(P2PTransportChannelTest, SignalReadyToSendWithPresumedWritable) { |
| 2252 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2253 | kDefaultPortAllocatorFlags); |
| 2254 | // Only test one endpoint, so we can ensure the connection doesn't receive a |
| 2255 | // binding response and advance beyond being "presumed" writable. |
| 2256 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2257 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2258 | IceConfig config; |
| 2259 | config.presume_writable_when_fully_relayed = true; |
| 2260 | ep1_ch1()->SetIceConfig(config); |
| 2261 | ep1_ch1()->MaybeStartGathering(); |
| 2262 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 2263 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 2264 | ep1_ch1()->AddRemoteCandidate( |
| 2265 | CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 0)); |
| 2266 | // Sanity checking the type of the connection. |
Jonas Oreland | 1230fb7 | 2019-10-25 15:58:14 +0200 | [diff] [blame] | 2267 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() != nullptr, kShortTimeout); |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 2268 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2269 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2270 | |
| 2271 | // Tell the socket server to block packets (returning EWOULDBLOCK). |
| 2272 | virtual_socket_server()->SetSendingBlocked(true); |
| 2273 | const char* data = "test"; |
| 2274 | int len = static_cast<int>(strlen(data)); |
| 2275 | EXPECT_EQ(-1, SendData(ep1_ch1(), data, len)); |
| 2276 | |
| 2277 | // Reset |ready_to_send_| flag, which is set to true if the event fires as it |
| 2278 | // should. |
| 2279 | GetEndpoint(0)->ready_to_send_ = false; |
| 2280 | virtual_socket_server()->SetSendingBlocked(false); |
| 2281 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
| 2282 | EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); |
| 2283 | } |
| 2284 | |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 2285 | // Test that role conflict error responses are sent as expected when receiving a |
| 2286 | // ping from an unknown address over a TURN connection. Regression test for |
| 2287 | // crbug.com/webrtc/9034. |
| 2288 | TEST_F(P2PTransportChannelTest, |
| 2289 | TurnToPrflxSelectedAfterResolvingIceControllingRoleConflict) { |
| 2290 | rtc::ScopedFakeClock clock; |
| 2291 | // Gather only relay candidates. |
| 2292 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, |
| 2293 | kDefaultPortAllocatorFlags | PORTALLOCATOR_DISABLE_UDP | |
| 2294 | PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_TCP, |
| 2295 | kDefaultPortAllocatorFlags | PORTALLOCATOR_DISABLE_UDP | |
| 2296 | PORTALLOCATOR_DISABLE_STUN | |
| 2297 | PORTALLOCATOR_DISABLE_TCP); |
| 2298 | // With conflicting ICE roles, endpoint 1 has the higher tie breaker and will |
| 2299 | // send a binding error response. |
| 2300 | SetIceRole(0, ICEROLE_CONTROLLING); |
| 2301 | SetIceTiebreaker(0, kHighTiebreaker); |
| 2302 | SetIceRole(1, ICEROLE_CONTROLLING); |
| 2303 | SetIceTiebreaker(1, kLowTiebreaker); |
| 2304 | // We want the remote TURN candidate to show up as prflx. To do this we need |
| 2305 | // to configure the server to accept packets from an address we haven't |
| 2306 | // explicitly installed permission for. |
| 2307 | test_turn_server()->set_enable_permission_checks(false); |
| 2308 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2309 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2310 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 2311 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
| 2312 | // Don't signal candidates from channel 2, so that channel 1 sees the TURN |
| 2313 | // candidate as peer reflexive. |
| 2314 | PauseCandidates(1); |
| 2315 | ep1_ch1()->MaybeStartGathering(); |
| 2316 | ep2_ch1()->MaybeStartGathering(); |
| 2317 | |
| 2318 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable(), |
| 2319 | kMediumTimeout, clock); |
| 2320 | |
| 2321 | ASSERT_NE(nullptr, ep1_ch1()->selected_connection()); |
| 2322 | |
| 2323 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2324 | EXPECT_EQ(PRFLX_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2325 | |
| 2326 | DestroyChannels(); |
| 2327 | } |
| 2328 | |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 2329 | // Test that the writability can be established with the piggyback |
| 2330 | // acknowledgement in the connectivity check from the remote peer. |
| 2331 | TEST_F(P2PTransportChannelTest, |
| 2332 | CanConnectWithPiggybackCheckAcknowledgementWhenCheckResponseBlocked) { |
| 2333 | webrtc::test::ScopedFieldTrials field_trials( |
Qingsi Wang | e3cc489 | 2019-06-19 14:50:44 -0700 | [diff] [blame] | 2334 | "WebRTC-PiggybackIceCheckAcknowledgement/Enabled/"); |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 2335 | rtc::ScopedFakeClock clock; |
| 2336 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 2337 | IceConfig ep1_config; |
| 2338 | IceConfig ep2_config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 2339 | // Let ep2 be tolerable of the loss of connectivity checks, so that it keeps |
| 2340 | // sending pings even after ep1 becomes unwritable as we configure the |
| 2341 | // firewall below. |
| 2342 | ep2_config.receiving_timeout = 30 * 1000; |
| 2343 | ep2_config.ice_unwritable_timeout = 30 * 1000; |
| 2344 | ep2_config.ice_unwritable_min_checks = 30; |
| 2345 | ep2_config.ice_inactive_timeout = 60 * 1000; |
| 2346 | |
| 2347 | CreateChannels(ep1_config, ep2_config); |
| 2348 | |
| 2349 | // Wait until both sides become writable for the first time. |
| 2350 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2351 | ep1_ch1() != nullptr && ep2_ch1() != nullptr && ep1_ch1()->receiving() && |
| 2352 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 2353 | ep2_ch1()->writable(), |
| 2354 | kDefaultTimeout, clock); |
| 2355 | // Block the ingress traffic to ep1 so that there is no check response from |
| 2356 | // ep2. |
| 2357 | ASSERT_NE(nullptr, LocalCandidate(ep1_ch1())); |
| 2358 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_IN, |
| 2359 | LocalCandidate(ep1_ch1())->address()); |
| 2360 | // Wait until ep1 becomes unwritable. At the same time ep2 should be still |
| 2361 | // fine so that it will keep sending pings. |
| 2362 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1() != nullptr && !ep1_ch1()->writable(), |
| 2363 | kDefaultTimeout, clock); |
| 2364 | EXPECT_TRUE(ep2_ch1() != nullptr && ep2_ch1()->writable()); |
| 2365 | // Now let the pings from ep2 to flow but block any pings from ep1, so that |
| 2366 | // ep1 can only become writable again after receiving an incoming ping from |
| 2367 | // ep2 with piggyback acknowledgement of its previously sent pings. Note |
| 2368 | // though that ep1 should have stopped sending pings after becoming unwritable |
| 2369 | // in the current design. |
| 2370 | fw()->ClearRules(); |
| 2371 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_OUT, |
| 2372 | LocalCandidate(ep1_ch1())->address()); |
| 2373 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1() != nullptr && ep1_ch1()->writable(), |
| 2374 | kDefaultTimeout, clock); |
| 2375 | DestroyChannels(); |
| 2376 | } |
| 2377 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2378 | // Test what happens when we have 2 users behind the same NAT. This can lead |
| 2379 | // to interesting behavior because the STUN server will only give out the |
| 2380 | // address of the outermost NAT. |
| 2381 | class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase { |
| 2382 | protected: |
| 2383 | void ConfigureEndpoints(Config nat_type, Config config1, Config config2) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 2384 | RTC_CHECK_GE(nat_type, NAT_FULL_CONE); |
| 2385 | RTC_CHECK_LE(nat_type, NAT_SYMMETRIC); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2386 | rtc::NATSocketServer::Translator* outer_nat = nat()->AddTranslator( |
| 2387 | kPublicAddrs[0], kNatAddrs[0], |
| 2388 | static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2389 | ConfigureEndpoint(outer_nat, 0, config1); |
| 2390 | ConfigureEndpoint(outer_nat, 1, config2); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2391 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2392 | } |
| 2393 | void ConfigureEndpoint(rtc::NATSocketServer::Translator* nat, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2394 | int endpoint, |
| 2395 | Config config) { |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 2396 | RTC_CHECK(config <= NAT_SYMMETRIC); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2397 | if (config == OPEN) { |
| 2398 | AddAddress(endpoint, kPrivateAddrs[endpoint]); |
| 2399 | nat->AddClient(kPrivateAddrs[endpoint]); |
| 2400 | } else { |
| 2401 | AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]); |
| 2402 | nat->AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2403 | static_cast<rtc::NATType>(config - NAT_FULL_CONE)) |
| 2404 | ->AddClient(kCascadedPrivateAddrs[endpoint]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | }; |
| 2408 | |
| 2409 | TEST_F(P2PTransportChannelSameNatTest, TestConesBehindSameCone) { |
| 2410 | ConfigureEndpoints(NAT_FULL_CONE, NAT_FULL_CONE, NAT_FULL_CONE); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 2411 | Test( |
| 2412 | P2PTransportChannelTestBase::Result("prflx", "udp", "stun", "udp", 1000)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | // Test what happens when we have multiple available pathways. |
| 2416 | // In the future we will try different RTTs and configs for the different |
| 2417 | // interfaces, so that we can simulate a user with Ethernet and VPN networks. |
| 2418 | class P2PTransportChannelMultihomedTest : public P2PTransportChannelTestBase { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2419 | public: |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2420 | const Connection* GetConnectionWithRemoteAddress( |
| 2421 | P2PTransportChannel* channel, |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2422 | const SocketAddress& address) { |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2423 | for (Connection* conn : channel->connections()) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2424 | if (conn->remote_candidate().address().EqualIPs(address)) { |
| 2425 | return conn; |
| 2426 | } |
| 2427 | } |
| 2428 | return nullptr; |
| 2429 | } |
| 2430 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2431 | Connection* GetConnectionWithLocalAddress(P2PTransportChannel* channel, |
| 2432 | const SocketAddress& address) { |
| 2433 | for (Connection* conn : channel->connections()) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2434 | if (conn->local_candidate().address().EqualIPs(address)) { |
| 2435 | return conn; |
| 2436 | } |
| 2437 | } |
| 2438 | return nullptr; |
| 2439 | } |
| 2440 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2441 | Connection* GetConnection(P2PTransportChannel* channel, |
| 2442 | const SocketAddress& local, |
| 2443 | const SocketAddress& remote) { |
| 2444 | for (Connection* conn : channel->connections()) { |
| 2445 | if (conn->local_candidate().address().EqualIPs(local) && |
| 2446 | conn->remote_candidate().address().EqualIPs(remote)) { |
| 2447 | return conn; |
| 2448 | } |
| 2449 | } |
| 2450 | return nullptr; |
| 2451 | } |
| 2452 | |
| 2453 | void DestroyAllButBestConnection(P2PTransportChannel* channel) { |
| 2454 | const Connection* selected_connection = channel->selected_connection(); |
| 2455 | for (Connection* conn : channel->connections()) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2456 | if (conn != selected_connection) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2457 | conn->Destroy(); |
| 2458 | } |
| 2459 | } |
| 2460 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2461 | }; |
| 2462 | |
| 2463 | // Test that we can establish connectivity when both peers are multihomed. |
zhihuang | a3095d0 | 2016-11-10 13:59:46 -0800 | [diff] [blame] | 2464 | TEST_F(P2PTransportChannelMultihomedTest, TestBasic) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2465 | AddAddress(0, kPublicAddrs[0]); |
| 2466 | AddAddress(0, kAlternateAddrs[0]); |
| 2467 | AddAddress(1, kPublicAddrs[1]); |
| 2468 | AddAddress(1, kAlternateAddrs[1]); |
| 2469 | Test(kLocalUdpToLocalUdp); |
| 2470 | } |
| 2471 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2472 | // Test that we can quickly switch links if an interface goes down. |
| 2473 | // The controlled side has two interfaces and one will die. |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2474 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverControlledSide) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2475 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2476 | AddAddress(0, kPublicAddrs[0]); |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2477 | // Simulate failing over from Wi-Fi to cell interface. |
| 2478 | AddAddress(1, kPublicAddrs[1], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2479 | AddAddress(1, kAlternateAddrs[1], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2480 | |
| 2481 | // Use only local ports for simplicity. |
| 2482 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2483 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2484 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2485 | // Make the receiving timeout shorter for testing. |
| 2486 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2487 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2488 | CreateChannels(config, config); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2489 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2490 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2491 | ep2_ch1()->receiving() && |
| 2492 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2493 | kMediumTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2494 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2495 | ep2_ch1()->selected_connection() && |
| 2496 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2497 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2500 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2501 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2502 | // The selected connections may switch, so keep references to them. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2503 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2504 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2505 | // We should detect loss of receiving within 1 second or so. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2506 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2507 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2508 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2509 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2510 | // We should switch over to use the alternate addr on both sides |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2511 | // when we are not receiving. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2512 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2513 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2514 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2515 | EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2516 | EXPECT_TRUE( |
| 2517 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1])); |
| 2518 | EXPECT_TRUE( |
| 2519 | LocalCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[1])); |
| 2520 | |
| 2521 | DestroyChannels(); |
| 2522 | } |
| 2523 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2524 | // Test that we can quickly switch links if an interface goes down. |
| 2525 | // The controlling side has two interfaces and one will die. |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2526 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverControllingSide) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2527 | rtc::ScopedFakeClock clock; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2528 | // Simulate failing over from Wi-Fi to cell interface. |
| 2529 | AddAddress(0, kPublicAddrs[0], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2530 | AddAddress(0, kAlternateAddrs[0], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2531 | AddAddress(1, kPublicAddrs[1]); |
| 2532 | |
| 2533 | // Use only local ports for simplicity. |
| 2534 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2535 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2536 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2537 | // Make the receiving timeout shorter for testing. |
| 2538 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2539 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2540 | CreateChannels(config, config); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2541 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2542 | ep2_ch1()->receiving() && |
| 2543 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2544 | kMediumTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2545 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2546 | ep2_ch1()->selected_connection() && |
| 2547 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2548 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2549 | |
| 2550 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2551 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2552 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2553 | // The selected connections will switch, so keep references to them. |
| 2554 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2555 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2556 | // We should detect loss of receiving within 1 second or so. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2557 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2558 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2559 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2560 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2561 | // We should switch over to use the alternate addr on both sides |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2562 | // when we are not receiving. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2563 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2564 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2565 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2566 | EXPECT_TRUE( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2567 | LocalCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0])); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2568 | EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2569 | EXPECT_TRUE( |
| 2570 | RemoteCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[0])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2571 | |
| 2572 | DestroyChannels(); |
| 2573 | } |
| 2574 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2575 | // Tests that we can quickly switch links if an interface goes down when |
| 2576 | // there are many connections. |
| 2577 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverWithManyConnections) { |
| 2578 | rtc::ScopedFakeClock clock; |
| 2579 | test_turn_server()->AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 2580 | RelayServerConfig turn_server; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2581 | turn_server.credentials = kRelayCredentials; |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 2582 | turn_server.ports.push_back(ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP)); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2583 | GetAllocator(0)->AddTurnServer(turn_server); |
| 2584 | GetAllocator(1)->AddTurnServer(turn_server); |
| 2585 | // Enable IPv6 |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 2586 | SetAllocatorFlags( |
| 2587 | 0, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 2588 | SetAllocatorFlags( |
| 2589 | 1, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2590 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 2591 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 2592 | |
| 2593 | auto& wifi = kPublicAddrs; |
| 2594 | auto& cellular = kAlternateAddrs; |
| 2595 | auto& wifiIpv6 = kIPv6PublicAddrs; |
| 2596 | auto& cellularIpv6 = kIPv6AlternateAddrs; |
| 2597 | AddAddress(0, wifi[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2598 | AddAddress(0, wifiIpv6[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2599 | AddAddress(0, cellular[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2600 | AddAddress(0, cellularIpv6[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2601 | AddAddress(1, wifi[1], "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2602 | AddAddress(1, wifiIpv6[1], "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2603 | AddAddress(1, cellular[1], "cellular1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2604 | AddAddress(1, cellularIpv6[1], "cellular1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2605 | |
| 2606 | // Set smaller delay on the TCP TURN server so that TCP TURN candidates |
| 2607 | // will be created in time. |
| 2608 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 1); |
| 2609 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpExtAddr, 1); |
| 2610 | virtual_socket_server()->set_delay_mean(500); |
| 2611 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2612 | |
| 2613 | // Make the receiving timeout shorter for testing. |
| 2614 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 2615 | // Create channels and let them go writable, as usual. |
| 2616 | CreateChannels(config, config, true /* ice_renomination */); |
| 2617 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2618 | ep2_ch1()->receiving() && |
| 2619 | ep2_ch1()->writable(), |
| 2620 | kMediumTimeout, clock); |
| 2621 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2622 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
| 2623 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifiIpv6[0]) && |
| 2624 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifiIpv6[1]), |
| 2625 | kMediumTimeout, clock); |
| 2626 | |
| 2627 | // Blackhole any traffic to or from the wifi on endpoint 1. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2628 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2629 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifi[0]); |
| 2630 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifiIpv6[0]); |
| 2631 | |
| 2632 | // The selected connections may switch, so keep references to them. |
| 2633 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2634 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2635 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2636 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
| 2637 | kMediumTimeout, clock); |
| 2638 | |
| 2639 | // Per-network best connections will be pinged at relatively higher rate when |
| 2640 | // the selected connection becomes not receiving. |
| 2641 | Connection* per_network_best_connection1 = |
| 2642 | GetConnection(ep1_ch1(), cellularIpv6[0], wifiIpv6[1]); |
| 2643 | ASSERT_NE(nullptr, per_network_best_connection1); |
| 2644 | int64_t last_ping_sent1 = per_network_best_connection1->last_ping_sent(); |
| 2645 | int num_pings_sent1 = per_network_best_connection1->num_pings_sent(); |
| 2646 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2647 | num_pings_sent1 < per_network_best_connection1->num_pings_sent(), |
| 2648 | kMediumTimeout, clock); |
| 2649 | int64_t ping_interval1 = |
| 2650 | (per_network_best_connection1->last_ping_sent() - last_ping_sent1) / |
| 2651 | (per_network_best_connection1->num_pings_sent() - num_pings_sent1); |
| 2652 | constexpr int SCHEDULING_DELAY = 200; |
| 2653 | EXPECT_LT( |
| 2654 | ping_interval1, |
| 2655 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_DELAY); |
| 2656 | |
| 2657 | // It should switch over to use the cellular IPv6 addr on endpoint 1 before |
| 2658 | // it timed out on writing. |
| 2659 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2660 | ep1_ch1()->selected_connection()->receiving() && |
| 2661 | ep2_ch1()->selected_connection()->receiving() && |
| 2662 | RemoteCandidate(ep2_ch1())->address().EqualIPs(cellularIpv6[0]) && |
| 2663 | LocalCandidate(ep1_ch1())->address().EqualIPs(cellularIpv6[0]), |
| 2664 | kMediumTimeout, clock); |
| 2665 | |
| 2666 | DestroyChannels(); |
| 2667 | } |
| 2668 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2669 | // Test that when the controlling side switches the selected connection, |
| 2670 | // the nomination of the selected connection on the controlled side will |
| 2671 | // increase. |
| 2672 | TEST_F(P2PTransportChannelMultihomedTest, TestIceRenomination) { |
| 2673 | rtc::ScopedFakeClock clock; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2674 | // Simulate failing over from Wi-Fi to cell interface. |
| 2675 | AddAddress(0, kPublicAddrs[0], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2676 | AddAddress(0, kAlternateAddrs[0], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2677 | AddAddress(1, kPublicAddrs[1]); |
| 2678 | |
| 2679 | // Use only local ports for simplicity. |
| 2680 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2681 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2682 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2683 | // We want it to set the remote ICE parameters when creating channels. |
| 2684 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2685 | // Make the receiving timeout shorter for testing. |
| 2686 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2687 | // Create channels with ICE renomination and let them go writable as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2688 | CreateChannels(config, config, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2689 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2690 | ep2_ch1()->receiving() && |
| 2691 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2692 | kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2693 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2694 | ep2_ch1()->selected_connection()->remote_nomination() > 0 && |
| 2695 | ep1_ch1()->selected_connection()->acked_nomination() > 0, |
| 2696 | kDefaultTimeout, clock); |
| 2697 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2698 | Connection* selected_connection2 = |
| 2699 | const_cast<Connection*>(ep2_ch1()->selected_connection()); |
| 2700 | uint32_t remote_nomination2 = selected_connection2->remote_nomination(); |
| 2701 | // |selected_connection2| should not be nominated any more since the previous |
| 2702 | // nomination has been acknowledged. |
| 2703 | ConnectSignalNominated(selected_connection2); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2704 | SIMULATED_WAIT(nominated(), kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2705 | EXPECT_FALSE(nominated()); |
| 2706 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2707 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2708 | RTC_LOG(LS_INFO) << "Failing over..."; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2709 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 2710 | |
| 2711 | // The selected connection on the controlling side should switch. |
| 2712 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2713 | ep1_ch1()->selected_connection() != selected_connection1, kMediumTimeout, |
| 2714 | clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2715 | // The connection on the controlled side should be nominated again |
| 2716 | // and have an increased nomination. |
| 2717 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2718 | ep2_ch1()->selected_connection()->remote_nomination() > |
| 2719 | remote_nomination2, |
| 2720 | kDefaultTimeout, clock); |
| 2721 | |
| 2722 | DestroyChannels(); |
| 2723 | } |
| 2724 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2725 | // Test that if an interface fails temporarily and then recovers quickly, |
| 2726 | // the selected connection will not switch. |
| 2727 | // The case that it will switch over to the backup connection if the selected |
| 2728 | // connection does not recover after enough time is covered in |
| 2729 | // TestFailoverControlledSide and TestFailoverControllingSide. |
| 2730 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2731 | TestConnectionSwitchDampeningControlledSide) { |
| 2732 | rtc::ScopedFakeClock clock; |
| 2733 | AddAddress(0, kPublicAddrs[0]); |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2734 | // Simulate failing over from Wi-Fi to cell interface. |
| 2735 | AddAddress(1, kPublicAddrs[1], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2736 | AddAddress(1, kAlternateAddrs[1], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2737 | |
| 2738 | // Use only local ports for simplicity. |
| 2739 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2740 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2741 | |
| 2742 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2743 | CreateChannels(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2744 | |
| 2745 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2746 | ep2_ch1()->receiving() && |
| 2747 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2748 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2749 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2750 | ep2_ch1()->selected_connection() && |
| 2751 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2752 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2753 | |
| 2754 | // Make the receiving timeout shorter for testing. |
| 2755 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 2756 | ep1_ch1()->SetIceConfig(config); |
| 2757 | ep2_ch1()->SetIceConfig(config); |
| 2758 | reset_selected_candidate_pair_switches(); |
| 2759 | |
| 2760 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2761 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2762 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); |
| 2763 | |
| 2764 | // The selected connections may switch, so keep references to them. |
| 2765 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2766 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2767 | // We should detect loss of receiving within 1 second or so. |
| 2768 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2769 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2770 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2771 | // After a short while, the link recovers itself. |
| 2772 | SIMULATED_WAIT(false, 10, clock); |
| 2773 | fw()->ClearRules(); |
| 2774 | |
| 2775 | // We should remain on the public address on both sides and no connection |
| 2776 | // switches should have happened. |
| 2777 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2778 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2779 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2780 | EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2781 | EXPECT_TRUE(LocalCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2782 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 2783 | |
| 2784 | DestroyChannels(); |
| 2785 | } |
| 2786 | |
| 2787 | // Test that if an interface fails temporarily and then recovers quickly, |
| 2788 | // the selected connection will not switch. |
| 2789 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2790 | TestConnectionSwitchDampeningControllingSide) { |
| 2791 | rtc::ScopedFakeClock clock; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2792 | // Simulate failing over from Wi-Fi to cell interface. |
| 2793 | AddAddress(0, kPublicAddrs[0], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2794 | AddAddress(0, kAlternateAddrs[0], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2795 | AddAddress(1, kPublicAddrs[1]); |
| 2796 | |
| 2797 | // Use only local ports for simplicity. |
| 2798 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2799 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2800 | |
| 2801 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2802 | CreateChannels(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2803 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2804 | ep2_ch1()->receiving() && |
| 2805 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2806 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2807 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2808 | ep2_ch1()->selected_connection() && |
| 2809 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2810 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2811 | |
| 2812 | // Make the receiving timeout shorter for testing. |
| 2813 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 2814 | ep1_ch1()->SetIceConfig(config); |
| 2815 | ep2_ch1()->SetIceConfig(config); |
| 2816 | reset_selected_candidate_pair_switches(); |
| 2817 | |
| 2818 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2819 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2820 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 2821 | // The selected connections may switch, so keep references to them. |
| 2822 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2823 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2824 | // We should detect loss of receiving within 1 second or so. |
| 2825 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2826 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2827 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2828 | // The link recovers after a short while. |
| 2829 | SIMULATED_WAIT(false, 10, clock); |
| 2830 | fw()->ClearRules(); |
| 2831 | |
| 2832 | // We should not switch to the alternate addr on both sides because of the |
| 2833 | // dampening. |
| 2834 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2835 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2836 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2837 | EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2838 | EXPECT_TRUE(RemoteCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2839 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 2840 | DestroyChannels(); |
| 2841 | } |
| 2842 | |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2843 | // Tests that if the remote side's network failed, it won't cause the local |
| 2844 | // side to switch connections and networks. |
| 2845 | TEST_F(P2PTransportChannelMultihomedTest, TestRemoteFailover) { |
| 2846 | rtc::ScopedFakeClock clock; |
| 2847 | // The interface names are chosen so that |cellular| would have higher |
| 2848 | // candidate priority and higher cost. |
| 2849 | auto& wifi = kPublicAddrs; |
| 2850 | auto& cellular = kAlternateAddrs; |
| 2851 | AddAddress(0, wifi[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2852 | AddAddress(0, cellular[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2853 | AddAddress(1, wifi[1], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2854 | |
| 2855 | // Use only local ports for simplicity. |
| 2856 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2857 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2858 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2859 | CreateChannels(); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2860 | // Make the receiving timeout shorter for testing. |
| 2861 | // Set the backup connection ping interval to 25s. |
| 2862 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE, 25000); |
| 2863 | // Ping the best connection more frequently since we don't have traffic. |
| 2864 | config.stable_writable_connection_ping_interval = 900; |
| 2865 | ep1_ch1()->SetIceConfig(config); |
| 2866 | ep2_ch1()->SetIceConfig(config); |
| 2867 | // Need to wait to make sure the connections on both networks are writable. |
| 2868 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2869 | ep2_ch1()->receiving() && |
| 2870 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2871 | kMediumTimeout, clock); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2872 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2873 | ep1_ch1()->selected_connection() && |
| 2874 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2875 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2876 | kDefaultTimeout, clock); |
| 2877 | Connection* backup_conn = |
| 2878 | GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]); |
| 2879 | ASSERT_NE(nullptr, backup_conn); |
| 2880 | // After a short while, the backup connection will be writable but not |
| 2881 | // receiving because backup connection is pinged at a slower rate. |
| 2882 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2883 | backup_conn->writable() && !backup_conn->receiving(), kDefaultTimeout, |
| 2884 | clock); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2885 | reset_selected_candidate_pair_switches(); |
| 2886 | // Blackhole any traffic to or from the remote WiFi networks. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2887 | RTC_LOG(LS_INFO) << "Failing over..."; |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2888 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifi[1]); |
| 2889 | |
| 2890 | int num_switches = 0; |
| 2891 | SIMULATED_WAIT((num_switches = reset_selected_candidate_pair_switches()) > 0, |
| 2892 | 20000, clock); |
| 2893 | EXPECT_EQ(0, num_switches); |
| 2894 | DestroyChannels(); |
| 2895 | } |
| 2896 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2897 | // Tests that a Wifi-Wifi connection has the highest precedence. |
| 2898 | TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiToWifiConnection) { |
| 2899 | // The interface names are chosen so that |cellular| would have higher |
| 2900 | // candidate priority if it is not for the network type. |
| 2901 | auto& wifi = kAlternateAddrs; |
| 2902 | auto& cellular = kPublicAddrs; |
| 2903 | AddAddress(0, wifi[0], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2904 | AddAddress(0, cellular[0], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2905 | AddAddress(1, wifi[1], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2906 | AddAddress(1, cellular[1], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2907 | |
| 2908 | // Use only local ports for simplicity. |
| 2909 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2910 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2911 | |
| 2912 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2913 | CreateChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2914 | |
| 2915 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2916 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2917 | 1000, 1000); |
| 2918 | // Need to wait to make sure the connections on both networks are writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2919 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2920 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2921 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2922 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2923 | EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2924 | LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]) && |
| 2925 | RemoteCandidate(ep2_ch1())->address().EqualIPs(wifi[0]), |
| 2926 | 1000); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2927 | DestroyChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | // Tests that a Wifi-Cellular connection has higher precedence than |
| 2931 | // a Cellular-Cellular connection. |
| 2932 | TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiOverCellularNetwork) { |
| 2933 | // The interface names are chosen so that |cellular| would have higher |
| 2934 | // candidate priority if it is not for the network type. |
| 2935 | auto& wifi = kAlternateAddrs; |
| 2936 | auto& cellular = kPublicAddrs; |
| 2937 | AddAddress(0, cellular[0], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2938 | AddAddress(1, wifi[1], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2939 | AddAddress(1, cellular[1], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2940 | |
| 2941 | // Use only local ports for simplicity. |
| 2942 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2943 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2944 | |
| 2945 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2946 | CreateChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2947 | |
| 2948 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2949 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2950 | 1000, 1000); |
| 2951 | // Need to wait to make sure the connections on both networks are writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2952 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2953 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2954 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2955 | EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2956 | LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]), |
| 2957 | 1000); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2958 | DestroyChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2961 | // Test that the backup connection is pinged at a rate no faster than |
| 2962 | // what was configured. |
| 2963 | TEST_F(P2PTransportChannelMultihomedTest, TestPingBackupConnectionRate) { |
| 2964 | AddAddress(0, kPublicAddrs[0]); |
| 2965 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2966 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2967 | AddAddress(1, kAlternateAddrs[1]); |
| 2968 | AddAddress(1, kPublicAddrs[1]); |
| 2969 | |
| 2970 | // Use only local ports for simplicity. |
| 2971 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2972 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2973 | |
| 2974 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2975 | CreateChannels(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2976 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2977 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2978 | 1000, 1000); |
| 2979 | int backup_ping_interval = 2000; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2980 | ep2_ch1()->SetIceConfig( |
| 2981 | CreateIceConfig(2000, GATHER_ONCE, backup_ping_interval)); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2982 | // After the state becomes COMPLETED, the backup connection will be pinged |
| 2983 | // once every |backup_ping_interval| milliseconds. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2984 | ASSERT_TRUE_WAIT(ep2_ch1()->GetState() == IceTransportState::STATE_COMPLETED, |
| 2985 | 1000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2986 | const std::vector<Connection*>& connections = ep2_ch1()->connections(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2987 | ASSERT_EQ(2U, connections.size()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2988 | Connection* backup_conn = connections[1]; |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2989 | EXPECT_TRUE_WAIT(backup_conn->writable(), kMediumTimeout); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2990 | int64_t last_ping_response_ms = backup_conn->last_ping_response_received(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2991 | EXPECT_TRUE_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2992 | last_ping_response_ms < backup_conn->last_ping_response_received(), |
| 2993 | kDefaultTimeout); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2994 | int time_elapsed = |
| 2995 | backup_conn->last_ping_response_received() - last_ping_response_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2996 | RTC_LOG(LS_INFO) << "Time elapsed: " << time_elapsed; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2997 | EXPECT_GE(time_elapsed, backup_ping_interval); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2998 | |
| 2999 | DestroyChannels(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 3000 | } |
| 3001 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3002 | TEST_F(P2PTransportChannelMultihomedTest, TestGetState) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3003 | rtc::ScopedFakeClock clock; |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3004 | AddAddress(0, kAlternateAddrs[0]); |
| 3005 | AddAddress(0, kPublicAddrs[0]); |
| 3006 | AddAddress(1, kPublicAddrs[1]); |
| 3007 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 3008 | CreateChannels(); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3009 | |
| 3010 | // Both transport channels will reach STATE_COMPLETED quickly. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3011 | EXPECT_EQ_SIMULATED_WAIT(IceTransportState::STATE_COMPLETED, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3012 | ep1_ch1()->GetState(), kShortTimeout, clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3013 | EXPECT_EQ_SIMULATED_WAIT(IceTransportState::STATE_COMPLETED, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3014 | ep2_ch1()->GetState(), kShortTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3015 | } |
| 3016 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3017 | // Tests that when a network interface becomes inactive, if Continual Gathering |
| 3018 | // policy is GATHER_CONTINUALLY, the ports associated with that network |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3019 | // will be removed from the port list of the channel, and the respective |
| 3020 | // remote candidates on the other participant will be removed eventually. |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3021 | TEST_F(P2PTransportChannelMultihomedTest, TestNetworkBecomesInactive) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3022 | rtc::ScopedFakeClock clock; |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3023 | AddAddress(0, kPublicAddrs[0]); |
| 3024 | AddAddress(1, kPublicAddrs[1]); |
| 3025 | // Create channels and let them go writable, as usual. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 3026 | IceConfig ep1_config = CreateIceConfig(2000, GATHER_CONTINUALLY); |
| 3027 | IceConfig ep2_config = CreateIceConfig(2000, GATHER_ONCE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 3028 | CreateChannels(ep1_config, ep2_config); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3029 | |
| 3030 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 3031 | SetAllocatorFlags(1, kOnlyLocalPorts); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3032 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 3033 | ep2_ch1()->receiving() && |
| 3034 | ep2_ch1()->writable(), |
| 3035 | kDefaultTimeout, clock); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3036 | // More than one port has been created. |
| 3037 | EXPECT_LE(1U, ep1_ch1()->ports().size()); |
| 3038 | // Endpoint 1 enabled continual gathering; the port will be removed |
| 3039 | // when the interface is removed. |
| 3040 | RemoveAddress(0, kPublicAddrs[0]); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3041 | EXPECT_TRUE(ep1_ch1()->ports().empty()); |
| 3042 | // The remote candidates will be removed eventually. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3043 | EXPECT_TRUE_SIMULATED_WAIT(ep2_ch1()->remote_candidates().empty(), 1000, |
| 3044 | clock); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3045 | |
| 3046 | size_t num_ports = ep2_ch1()->ports().size(); |
| 3047 | EXPECT_LE(1U, num_ports); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3048 | size_t num_remote_candidates = ep1_ch1()->remote_candidates().size(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3049 | // Endpoint 2 did not enable continual gathering; the local port will still be |
| 3050 | // removed when the interface is removed but the remote candidates on the |
| 3051 | // other participant will not be removed. |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3052 | RemoveAddress(1, kPublicAddrs[1]); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3053 | |
| 3054 | EXPECT_EQ_SIMULATED_WAIT(0U, ep2_ch1()->ports().size(), kDefaultTimeout, |
| 3055 | clock); |
| 3056 | SIMULATED_WAIT(0U == ep1_ch1()->remote_candidates().size(), 500, clock); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3057 | EXPECT_EQ(num_remote_candidates, ep1_ch1()->remote_candidates().size()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3058 | |
| 3059 | DestroyChannels(); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 3060 | } |
| 3061 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3062 | // Tests that continual gathering will create new connections when a new |
| 3063 | // interface is added. |
| 3064 | TEST_F(P2PTransportChannelMultihomedTest, |
| 3065 | TestContinualGatheringOnNewInterface) { |
| 3066 | auto& wifi = kAlternateAddrs; |
| 3067 | auto& cellular = kPublicAddrs; |
| 3068 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 3069 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3070 | // Set continual gathering policy. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 3071 | IceConfig continual_gathering_config = |
| 3072 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 3073 | CreateChannels(continual_gathering_config, continual_gathering_config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3074 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 3075 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 3076 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 3077 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 3078 | kDefaultTimeout, kDefaultTimeout); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 3079 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3080 | // Add a new wifi interface on end point 2. We should expect a new connection |
| 3081 | // to be created and the new one will be the best connection. |
| 3082 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3083 | const Connection* conn; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3084 | EXPECT_TRUE_WAIT((conn = ep1_ch1()->selected_connection()) != nullptr && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3085 | conn->remote_candidate().address().EqualIPs(wifi[1]), |
| 3086 | kDefaultTimeout); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3087 | EXPECT_TRUE_WAIT((conn = ep2_ch1()->selected_connection()) != nullptr && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3088 | conn->local_candidate().address().EqualIPs(wifi[1]), |
| 3089 | kDefaultTimeout); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 3090 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3091 | // Add a new cellular interface on end point 1, we should expect a new |
| 3092 | // backup connection created using this new interface. |
| 3093 | AddAddress(0, cellular[0], "test_cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3094 | EXPECT_TRUE_WAIT( |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3095 | ep1_ch1()->GetState() == IceTransportState::STATE_COMPLETED && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3096 | (conn = GetConnectionWithLocalAddress(ep1_ch1(), cellular[0])) != |
| 3097 | nullptr && |
| 3098 | conn != ep1_ch1()->selected_connection() && conn->writable(), |
| 3099 | kDefaultTimeout); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3100 | EXPECT_TRUE_WAIT( |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3101 | ep2_ch1()->GetState() == IceTransportState::STATE_COMPLETED && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3102 | (conn = GetConnectionWithRemoteAddress(ep2_ch1(), cellular[0])) != |
| 3103 | nullptr && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3104 | conn != ep2_ch1()->selected_connection() && conn->receiving(), |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3105 | kDefaultTimeout); |
| 3106 | |
| 3107 | DestroyChannels(); |
| 3108 | } |
| 3109 | |
| 3110 | // Tests that we can switch links via continual gathering. |
| 3111 | TEST_F(P2PTransportChannelMultihomedTest, |
| 3112 | TestSwitchLinksViaContinualGathering) { |
| 3113 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3114 | AddAddress(0, kPublicAddrs[0]); |
| 3115 | AddAddress(1, kPublicAddrs[1]); |
| 3116 | // Use only local ports for simplicity. |
| 3117 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 3118 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 3119 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3120 | // Set continual gathering policy. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 3121 | IceConfig continual_gathering_config = |
| 3122 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 3123 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 3124 | CreateChannels(continual_gathering_config, continual_gathering_config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3125 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 3126 | ep2_ch1()->receiving() && |
| 3127 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3128 | kMediumTimeout, clock); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3129 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 3130 | ep2_ch1()->selected_connection() && |
| 3131 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 3132 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3133 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3134 | // Add the new address first and then remove the other one. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3135 | RTC_LOG(LS_INFO) << "Draining..."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3136 | AddAddress(1, kAlternateAddrs[1]); |
| 3137 | RemoveAddress(1, kPublicAddrs[1]); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3138 | // We should switch to use the alternate address after an exchange of pings. |
| 3139 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3140 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3141 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 3142 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1]), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3143 | kMediumTimeout, clock); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3144 | |
| 3145 | // Remove one address first and then add another address. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3146 | RTC_LOG(LS_INFO) << "Draining again..."; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3147 | RemoveAddress(1, kAlternateAddrs[1]); |
| 3148 | AddAddress(1, kAlternateAddrs[0]); |
| 3149 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3150 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3151 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 3152 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0]), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3153 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3154 | |
| 3155 | DestroyChannels(); |
| 3156 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3157 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3158 | // Tests that the backup connection will be restored after it is destroyed. |
| 3159 | TEST_F(P2PTransportChannelMultihomedTest, TestRestoreBackupConnection) { |
| 3160 | rtc::ScopedFakeClock clock; |
| 3161 | auto& wifi = kAlternateAddrs; |
| 3162 | auto& cellular = kPublicAddrs; |
| 3163 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 3164 | AddAddress(0, cellular[0], "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 3165 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 3166 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
| 3167 | // Use only local ports for simplicity. |
| 3168 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 3169 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 3170 | |
| 3171 | // Create channels and let them go writable, as usual. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3172 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 3173 | config.regather_on_failed_networks_interval = 2000; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 3174 | CreateChannels(config, config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3175 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 3176 | ep2_ch1()->receiving() && |
| 3177 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3178 | kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3179 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 3180 | ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3181 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 3182 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1])); |
| 3183 | |
| 3184 | // Destroy all backup connections. |
| 3185 | DestroyAllButBestConnection(ep1_ch1()); |
| 3186 | // Ensure the backup connection is removed first. |
| 3187 | EXPECT_TRUE_SIMULATED_WAIT( |
| 3188 | GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]) == nullptr, |
| 3189 | kDefaultTimeout, clock); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3190 | const Connection* conn; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3191 | EXPECT_TRUE_SIMULATED_WAIT( |
| 3192 | (conn = GetConnectionWithLocalAddress(ep1_ch1(), cellular[0])) != |
| 3193 | nullptr && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3194 | conn != ep1_ch1()->selected_connection() && conn->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3195 | kDefaultTimeout, clock); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3196 | |
| 3197 | DestroyChannels(); |
| 3198 | } |
| 3199 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3200 | // A collection of tests which tests a single P2PTransportChannel by sending |
| 3201 | // pings. |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3202 | class P2PTransportChannelPingTest : public ::testing::Test, |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3203 | public sigslot::has_slots<> { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3204 | public: |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3205 | P2PTransportChannelPingTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 3206 | : vss_(new rtc::VirtualSocketServer()), thread_(vss_.get()) {} |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3207 | |
| 3208 | protected: |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3209 | void PrepareChannel(P2PTransportChannel* ch) { |
| 3210 | ch->SetIceRole(ICEROLE_CONTROLLING); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3211 | ch->SetIceParameters(kIceParams[0]); |
| 3212 | ch->SetRemoteIceParameters(kIceParams[1]); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3213 | ch->SignalNetworkRouteChanged.connect( |
| 3214 | this, &P2PTransportChannelPingTest::OnNetworkRouteChanged); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3215 | ch->SignalReadyToSend.connect(this, |
| 3216 | &P2PTransportChannelPingTest::OnReadyToSend); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3217 | ch->SignalStateChanged.connect( |
| 3218 | this, &P2PTransportChannelPingTest::OnChannelStateChanged); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3219 | ch->SignalCandidatePairChanged.connect( |
| 3220 | this, &P2PTransportChannelPingTest::OnCandidatePairChanged); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 3223 | Connection* WaitForConnectionTo( |
| 3224 | P2PTransportChannel* ch, |
| 3225 | const std::string& ip, |
| 3226 | int port_num, |
| 3227 | rtc::ThreadProcessingFakeClock* clock = nullptr) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3228 | if (clock == nullptr) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3229 | EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, |
| 3230 | kMediumTimeout); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3231 | } else { |
| 3232 | EXPECT_TRUE_SIMULATED_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3233 | kMediumTimeout, *clock); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3234 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3235 | return GetConnectionTo(ch, ip, port_num); |
| 3236 | } |
| 3237 | |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3238 | Port* GetPort(P2PTransportChannel* ch) { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3239 | if (ch->ports().empty()) { |
| 3240 | return nullptr; |
| 3241 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3242 | return static_cast<Port*>(ch->ports()[0]); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3245 | Port* GetPrunedPort(P2PTransportChannel* ch) { |
| 3246 | if (ch->pruned_ports().empty()) { |
| 3247 | return nullptr; |
| 3248 | } |
| 3249 | return static_cast<Port*>(ch->pruned_ports()[0]); |
| 3250 | } |
| 3251 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3252 | Connection* GetConnectionTo(P2PTransportChannel* ch, |
| 3253 | const std::string& ip, |
| 3254 | int port_num) { |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3255 | Port* port = GetPort(ch); |
| 3256 | if (!port) { |
| 3257 | return nullptr; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3258 | } |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3259 | return port->GetConnection(rtc::SocketAddress(ip, port_num)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3262 | Connection* FindNextPingableConnectionAndPingIt(P2PTransportChannel* ch) { |
| 3263 | Connection* conn = ch->FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3264 | if (conn) { |
| 3265 | ch->MarkConnectionPinged(conn); |
| 3266 | } |
| 3267 | return conn; |
| 3268 | } |
| 3269 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3270 | int SendData(IceTransportInternal* channel, |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3271 | const char* data, |
| 3272 | size_t len, |
| 3273 | int packet_id) { |
| 3274 | rtc::PacketOptions options; |
| 3275 | options.packet_id = packet_id; |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3276 | return channel->SendPacket(data, len, options, 0); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3279 | Connection* CreateConnectionWithCandidate(P2PTransportChannel* channel, |
| 3280 | rtc::ScopedFakeClock* clock, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3281 | const std::string& ip_addr, |
| 3282 | int port, |
| 3283 | int priority, |
| 3284 | bool writable) { |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3285 | channel->AddRemoteCandidate( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3286 | CreateUdpCandidate(LOCAL_PORT_TYPE, ip_addr, port, priority)); |
| 3287 | EXPECT_TRUE_SIMULATED_WAIT( |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3288 | GetConnectionTo(channel, ip_addr, port) != nullptr, kMediumTimeout, |
| 3289 | *clock); |
| 3290 | Connection* conn = GetConnectionTo(channel, ip_addr, port); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3291 | |
| 3292 | if (conn && writable) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3293 | conn->ReceivedPingResponse(LOW_RTT, "id"); // make it writable |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3294 | } |
| 3295 | return conn; |
| 3296 | } |
| 3297 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3298 | void NominateConnection(Connection* conn, uint32_t remote_nomination = 1U) { |
| 3299 | conn->set_remote_nomination(remote_nomination); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3300 | conn->SignalNominated(conn); |
| 3301 | } |
| 3302 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 3303 | void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route) { |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3304 | last_network_route_ = network_route; |
| 3305 | if (last_network_route_) { |
| 3306 | last_sent_packet_id_ = last_network_route_->last_sent_packet_id; |
| 3307 | } |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3308 | ++selected_candidate_pair_switches_; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 3311 | void ReceivePingOnConnection( |
| 3312 | Connection* conn, |
| 3313 | const std::string& remote_ufrag, |
| 3314 | int priority, |
| 3315 | uint32_t nomination, |
| 3316 | const absl::optional<std::string>& piggyback_ping_id) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3317 | IceMessage msg; |
| 3318 | msg.SetType(STUN_BINDING_REQUEST); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3319 | msg.AddAttribute(std::make_unique<StunByteStringAttribute>( |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3320 | STUN_ATTR_USERNAME, |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3321 | conn->local_candidate().username() + ":" + remote_ufrag)); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3322 | msg.AddAttribute( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3323 | std::make_unique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, priority)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3324 | if (nomination != 0) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3325 | msg.AddAttribute(std::make_unique<StunUInt32Attribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3326 | STUN_ATTR_NOMINATION, nomination)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3327 | } |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 3328 | if (piggyback_ping_id) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3329 | msg.AddAttribute(std::make_unique<StunByteStringAttribute>( |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 3330 | STUN_ATTR_LAST_ICE_CHECK_RECEIVED, piggyback_ping_id.value())); |
| 3331 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3332 | msg.SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3333 | msg.AddMessageIntegrity(conn->local_candidate().password()); |
| 3334 | msg.AddFingerprint(); |
| 3335 | rtc::ByteBufferWriter buf; |
| 3336 | msg.Write(&buf); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 3337 | conn->OnReadPacket(buf.Data(), buf.Length(), rtc::TimeMicros()); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3338 | } |
| 3339 | |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 3340 | void ReceivePingOnConnection(Connection* conn, |
| 3341 | const std::string& remote_ufrag, |
| 3342 | int priority, |
| 3343 | uint32_t nomination = 0) { |
| 3344 | ReceivePingOnConnection(conn, remote_ufrag, priority, nomination, |
| 3345 | absl::nullopt); |
| 3346 | } |
| 3347 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 3348 | void OnReadyToSend(rtc::PacketTransportInternal* transport) { |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3349 | channel_ready_to_send_ = true; |
| 3350 | } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3351 | void OnChannelStateChanged(IceTransportInternal* channel) { |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3352 | channel_state_ = channel->GetState(); |
| 3353 | } |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3354 | void OnCandidatePairChanged(const CandidatePairChangeEvent& event) { |
| 3355 | last_candidate_change_event_ = event; |
| 3356 | } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3357 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3358 | int last_sent_packet_id() { return last_sent_packet_id_; } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3359 | bool channel_ready_to_send() { return channel_ready_to_send_; } |
| 3360 | void reset_channel_ready_to_send() { channel_ready_to_send_ = false; } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3361 | IceTransportState channel_state() { return channel_state_; } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3362 | int reset_selected_candidate_pair_switches() { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3363 | int switches = selected_candidate_pair_switches_; |
| 3364 | selected_candidate_pair_switches_ = 0; |
| 3365 | return switches; |
| 3366 | } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3367 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3368 | // Return true if the |pair| matches the last network route. |
| 3369 | bool CandidatePairMatchesNetworkRoute(CandidatePairInterface* pair) { |
| 3370 | if (!pair) { |
| 3371 | return !last_network_route_.has_value(); |
| 3372 | } else { |
| 3373 | return pair->local_candidate().network_id() == |
| 3374 | last_network_route_->local_network_id && |
| 3375 | pair->remote_candidate().network_id() == |
| 3376 | last_network_route_->remote_network_id; |
| 3377 | } |
| 3378 | } |
| 3379 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3380 | bool ConnectionMatchesChangeEvent(Connection* conn, std::string reason) { |
| 3381 | if (!conn) { |
| 3382 | return !last_candidate_change_event_.has_value(); |
| 3383 | } else { |
Qingsi Wang | 7cdcda9 | 2019-08-28 09:18:37 -0700 | [diff] [blame] | 3384 | const auto& last_selected_pair = |
| 3385 | last_candidate_change_event_->selected_candidate_pair; |
| 3386 | return last_selected_pair.local_candidate().IsEquivalent( |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3387 | conn->local_candidate()) && |
Qingsi Wang | 7cdcda9 | 2019-08-28 09:18:37 -0700 | [diff] [blame] | 3388 | last_selected_pair.remote_candidate().IsEquivalent( |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3389 | conn->remote_candidate()) && |
| 3390 | last_candidate_change_event_->last_data_received_ms == |
| 3391 | conn->last_data_received() && |
| 3392 | last_candidate_change_event_->reason == reason; |
| 3393 | } |
| 3394 | } |
| 3395 | |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3396 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 3397 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 3398 | rtc::AutoSocketServerThread thread_; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3399 | int selected_candidate_pair_switches_ = 0; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3400 | int last_sent_packet_id_ = -1; |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3401 | bool channel_ready_to_send_ = false; |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3402 | absl::optional<CandidatePairChangeEvent> last_candidate_change_event_; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3403 | IceTransportState channel_state_ = IceTransportState::STATE_INIT; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 3404 | absl::optional<rtc::NetworkRoute> last_network_route_; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3405 | }; |
| 3406 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3407 | TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3408 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3409 | P2PTransportChannel ch("trigger checks", 1, &pa); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3410 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3411 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3412 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3413 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3414 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3415 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3416 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3417 | ASSERT_TRUE(conn1 != nullptr); |
| 3418 | ASSERT_TRUE(conn2 != nullptr); |
| 3419 | |
| 3420 | // Before a triggered check, the first connection to ping is the |
| 3421 | // highest priority one. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3422 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3423 | |
| 3424 | // Receiving a ping causes a triggered check which should make conn1 |
| 3425 | // be pinged first instead of conn2, even though conn2 has a higher |
| 3426 | // priority. |
| 3427 | conn1->ReceivedPing(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3428 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3429 | } |
| 3430 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3431 | TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3432 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3433 | P2PTransportChannel ch("ping sufficiently", 1, &pa); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3434 | PrepareChannel(&ch); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3435 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3436 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3437 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3438 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3439 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3440 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3441 | ASSERT_TRUE(conn1 != nullptr); |
| 3442 | ASSERT_TRUE(conn2 != nullptr); |
| 3443 | |
| 3444 | // Low-priority connection becomes writable so that the other connection |
| 3445 | // is not pruned. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3446 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3447 | EXPECT_TRUE_WAIT( |
| 3448 | conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL && |
| 3449 | conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, |
| 3450 | kDefaultTimeout); |
| 3451 | } |
| 3452 | |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3453 | // Verify that the connections are pinged at the right time. |
| 3454 | TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) { |
| 3455 | rtc::ScopedFakeClock clock; |
| 3456 | int RTT_RATIO = 4; |
| 3457 | int SCHEDULING_RANGE = 200; |
| 3458 | int RTT_RANGE = 10; |
| 3459 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3460 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3461 | P2PTransportChannel ch("TestChannel", 1, &pa); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3462 | PrepareChannel(&ch); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3463 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3464 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3465 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3466 | |
| 3467 | ASSERT_TRUE(conn != nullptr); |
| 3468 | SIMULATED_WAIT(conn->num_pings_sent() == 1, kDefaultTimeout, clock); |
| 3469 | |
| 3470 | // Initializing. |
| 3471 | |
| 3472 | int64_t start = clock.TimeNanos(); |
| 3473 | SIMULATED_WAIT(conn->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, |
| 3474 | kDefaultTimeout, clock); |
| 3475 | int64_t ping_interval_ms = (clock.TimeNanos() - start) / |
| 3476 | rtc::kNumNanosecsPerMillisec / |
| 3477 | (MIN_PINGS_AT_WEAK_PING_INTERVAL - 1); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3478 | EXPECT_EQ(ping_interval_ms, WEAK_PING_INTERVAL); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3479 | |
| 3480 | // Stabilizing. |
| 3481 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3482 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3483 | int ping_sent_before = conn->num_pings_sent(); |
| 3484 | start = clock.TimeNanos(); |
| 3485 | // The connection becomes strong but not stable because we haven't been able |
| 3486 | // to converge the RTT. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3487 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3488 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3489 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3490 | EXPECT_GE(ping_interval_ms, |
| 3491 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3492 | EXPECT_LE( |
| 3493 | ping_interval_ms, |
| 3494 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3495 | |
| 3496 | // Stabilized. |
| 3497 | |
| 3498 | // The connection becomes stable after receiving more than RTT_RATIO rtt |
| 3499 | // samples. |
| 3500 | for (int i = 0; i < RTT_RATIO; i++) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3501 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3502 | } |
| 3503 | ping_sent_before = conn->num_pings_sent(); |
| 3504 | start = clock.TimeNanos(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3505 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3506 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3507 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3508 | EXPECT_GE(ping_interval_ms, |
| 3509 | STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3510 | EXPECT_LE( |
| 3511 | ping_interval_ms, |
| 3512 | STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3513 | |
| 3514 | // Destabilized. |
| 3515 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3516 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3517 | // Create a in-flight ping. |
| 3518 | conn->Ping(clock.TimeNanos() / rtc::kNumNanosecsPerMillisec); |
| 3519 | start = clock.TimeNanos(); |
| 3520 | // In-flight ping timeout and the connection will be unstable. |
| 3521 | SIMULATED_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3522 | !conn->stable(clock.TimeNanos() / rtc::kNumNanosecsPerMillisec), |
| 3523 | kMediumTimeout, clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3524 | int64_t duration_ms = |
| 3525 | (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
| 3526 | EXPECT_GE(duration_ms, 2 * conn->rtt() - RTT_RANGE); |
| 3527 | EXPECT_LE(duration_ms, 2 * conn->rtt() + RTT_RANGE); |
| 3528 | // The connection become unstable due to not receiving ping responses. |
| 3529 | ping_sent_before = conn->num_pings_sent(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3530 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3531 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3532 | // The interval is expected to be |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3533 | // WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL. |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3534 | start = clock.TimeNanos(); |
| 3535 | ping_sent_before = conn->num_pings_sent(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3536 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3537 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3538 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3539 | EXPECT_GE(ping_interval_ms, |
| 3540 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3541 | EXPECT_LE( |
| 3542 | ping_interval_ms, |
| 3543 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3546 | // Test that we start pinging as soon as we have a connection and remote ICE |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3547 | // parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3548 | TEST_F(P2PTransportChannelPingTest, PingingStartedAsSoonAsPossible) { |
| 3549 | rtc::ScopedFakeClock clock; |
| 3550 | |
| 3551 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3552 | P2PTransportChannel ch("TestChannel", 1, &pa); |
| 3553 | ch.SetIceRole(ICEROLE_CONTROLLING); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3554 | ch.SetIceParameters(kIceParams[0]); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3555 | ch.MaybeStartGathering(); |
| 3556 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, ch.gathering_state(), |
| 3557 | kDefaultTimeout); |
| 3558 | |
| 3559 | // Simulate a binding request being received, creating a peer reflexive |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3560 | // candidate pair while we still don't have remote ICE parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3561 | IceMessage request; |
| 3562 | request.SetType(STUN_BINDING_REQUEST); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3563 | request.AddAttribute(std::make_unique<StunByteStringAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3564 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3565 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3566 | request.AddAttribute(std::make_unique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, |
| 3567 | prflx_priority)); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3568 | Port* port = GetPort(&ch); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3569 | ASSERT_NE(nullptr, port); |
| 3570 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3571 | &request, kIceUfrag[1], false); |
| 3572 | Connection* conn = GetConnectionTo(&ch, "1.1.1.1", 1); |
| 3573 | ASSERT_NE(nullptr, conn); |
| 3574 | |
| 3575 | // Simulate waiting for a second (and change) and verify that no pings were |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3576 | // sent, since we don't yet have remote ICE parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3577 | SIMULATED_WAIT(conn->num_pings_sent() > 0, 1025, clock); |
| 3578 | EXPECT_EQ(0, conn->num_pings_sent()); |
| 3579 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3580 | // Set remote ICE parameters. Now we should be able to ping. Ensure that |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3581 | // the first ping is sent as soon as possible, within one simulated clock |
| 3582 | // tick. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3583 | ch.SetRemoteIceParameters(kIceParams[1]); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3584 | EXPECT_TRUE_SIMULATED_WAIT(conn->num_pings_sent() > 0, 1, clock); |
| 3585 | } |
| 3586 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3587 | TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3588 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3589 | P2PTransportChannel ch("trigger checks", 1, &pa); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3590 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3591 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3592 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3593 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3594 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3595 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3596 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3597 | ASSERT_TRUE(conn1 != nullptr); |
| 3598 | ASSERT_TRUE(conn2 != nullptr); |
| 3599 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3600 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
| 3601 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3602 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3603 | ASSERT_TRUE(conn1->writable()); |
| 3604 | conn1->ReceivedPing(); |
| 3605 | |
| 3606 | // Ping received, but the connection is already writable, so no |
| 3607 | // "triggered check" and conn2 is pinged before conn1 because it has |
| 3608 | // a higher priority. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3609 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3610 | } |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3611 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3612 | TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3613 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3614 | P2PTransportChannel ch("Do not ping failed connections", 1, &pa); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3615 | PrepareChannel(&ch); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3616 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3617 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3618 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3619 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3620 | ASSERT_TRUE(conn1 != nullptr); |
| 3621 | |
| 3622 | EXPECT_EQ(conn1, ch.FindNextPingableConnection()); |
| 3623 | conn1->Prune(); // A pruned connection may still be pingable. |
| 3624 | EXPECT_EQ(conn1, ch.FindNextPingableConnection()); |
| 3625 | conn1->FailAndPrune(); |
| 3626 | EXPECT_TRUE(nullptr == ch.FindNextPingableConnection()); |
| 3627 | } |
| 3628 | |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3629 | TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3630 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3631 | P2PTransportChannel ch("state change", 1, &pa); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3632 | PrepareChannel(&ch); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3633 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3634 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3635 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3636 | ASSERT_TRUE(conn1 != nullptr); |
| 3637 | // Pruning the connection reduces the set of active connections and changes |
| 3638 | // the channel state. |
| 3639 | conn1->Prune(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3640 | EXPECT_EQ_WAIT(IceTransportState::STATE_FAILED, channel_state(), |
| 3641 | kDefaultTimeout); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3644 | // Test adding remote candidates with different ufrags. If a remote candidate |
| 3645 | // is added with an old ufrag, it will be discarded. If it is added with a |
| 3646 | // ufrag that was not seen before, it will be used to create connections |
| 3647 | // although the ICE pwd in the remote candidate will be set when the ICE |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3648 | // parameters arrive. If a remote candidate is added with the current ICE |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3649 | // ufrag, its pwd and generation will be set properly. |
| 3650 | TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3651 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3652 | P2PTransportChannel ch("add candidate", 1, &pa); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3653 | PrepareChannel(&ch); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3654 | ch.MaybeStartGathering(); |
| 3655 | // Add a candidate with a future ufrag. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3656 | ch.AddRemoteCandidate( |
| 3657 | CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1, kIceUfrag[2])); |
| 3658 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3659 | ASSERT_TRUE(conn1 != nullptr); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3660 | const Candidate& candidate = conn1->remote_candidate(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3661 | EXPECT_EQ(kIceUfrag[2], candidate.username()); |
| 3662 | EXPECT_TRUE(candidate.password().empty()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3663 | EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3664 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3665 | // Set the remote ICE parameters with the "future" ufrag. |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3666 | // This should set the ICE pwd in the remote candidate of |conn1|, making |
| 3667 | // it pingable. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3668 | ch.SetRemoteIceParameters(kIceParams[2]); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3669 | EXPECT_EQ(kIceUfrag[2], candidate.username()); |
| 3670 | EXPECT_EQ(kIcePwd[2], candidate.password()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3671 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3672 | |
| 3673 | // Add a candidate with an old ufrag. No connection will be created. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3674 | ch.AddRemoteCandidate( |
| 3675 | CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2, kIceUfrag[1])); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3676 | rtc::Thread::Current()->ProcessMessages(500); |
| 3677 | EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr); |
| 3678 | |
| 3679 | // Add a candidate with the current ufrag, its pwd and generation will be |
| 3680 | // assigned, even if the generation is not set. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3681 | ch.AddRemoteCandidate( |
| 3682 | CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 0, kIceUfrag[2])); |
| 3683 | Connection* conn3 = nullptr; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3684 | ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3685 | kMediumTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3686 | const Candidate& new_candidate = conn3->remote_candidate(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3687 | EXPECT_EQ(kIcePwd[2], new_candidate.password()); |
| 3688 | EXPECT_EQ(1U, new_candidate.generation()); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3689 | |
| 3690 | // Check that the pwd of all remote candidates are properly assigned. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3691 | for (const RemoteCandidate& candidate : ch.remote_candidates()) { |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3692 | EXPECT_TRUE(candidate.username() == kIceUfrag[1] || |
| 3693 | candidate.username() == kIceUfrag[2]); |
| 3694 | if (candidate.username() == kIceUfrag[1]) { |
| 3695 | EXPECT_EQ(kIcePwd[1], candidate.password()); |
| 3696 | } else if (candidate.username() == kIceUfrag[2]) { |
| 3697 | EXPECT_EQ(kIcePwd[2], candidate.password()); |
| 3698 | } |
| 3699 | } |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3700 | } |
| 3701 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3702 | TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3703 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3704 | P2PTransportChannel ch("connection resurrection", 1, &pa); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3705 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3706 | ch.MaybeStartGathering(); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3707 | |
| 3708 | // Create conn1 and keep track of original candidate priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3709 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3710 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3711 | ASSERT_TRUE(conn1 != nullptr); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 3712 | uint32_t remote_priority = conn1->remote_candidate().priority(); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3713 | |
| 3714 | // Create a higher priority candidate and make the connection |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 3715 | // receiving/writable. This will prune conn1. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3716 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
| 3717 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3718 | ASSERT_TRUE(conn2 != nullptr); |
| 3719 | conn2->ReceivedPing(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3720 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3721 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3722 | // Wait for conn1 to be pruned. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3723 | EXPECT_TRUE_WAIT(conn1->pruned(), kMediumTimeout); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3724 | // Destroy the connection to test SignalUnknownAddress. |
| 3725 | conn1->Destroy(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3726 | EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, |
| 3727 | kMediumTimeout); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3728 | |
| 3729 | // Create a minimal STUN message with prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3730 | IceMessage request; |
| 3731 | request.SetType(STUN_BINDING_REQUEST); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3732 | request.AddAttribute(std::make_unique<StunByteStringAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3733 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3734 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3735 | request.AddAttribute(std::make_unique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, |
| 3736 | prflx_priority)); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3737 | EXPECT_NE(prflx_priority, remote_priority); |
| 3738 | |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3739 | Port* port = GetPort(&ch); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3740 | // conn1 should be resurrected with original priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3741 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3742 | &request, kIceUfrag[1], false); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3743 | conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3744 | ASSERT_TRUE(conn1 != nullptr); |
| 3745 | EXPECT_EQ(conn1->remote_candidate().priority(), remote_priority); |
| 3746 | |
| 3747 | // conn3, a real prflx connection, should have prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3748 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 1), PROTO_UDP, |
| 3749 | &request, kIceUfrag[1], false); |
| 3750 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 1); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3751 | ASSERT_TRUE(conn3 != nullptr); |
| 3752 | EXPECT_EQ(conn3->remote_candidate().priority(), prflx_priority); |
| 3753 | } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3754 | |
| 3755 | TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3756 | rtc::ScopedFakeClock clock; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3757 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3758 | P2PTransportChannel ch("receiving state change", 1, &pa); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3759 | PrepareChannel(&ch); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3760 | // Default receiving timeout and checking receiving interval should not be too |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3761 | // small. |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 3762 | EXPECT_LE(1000, ch.config().receiving_timeout_or_default()); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3763 | EXPECT_LE(200, ch.check_receiving_interval()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3764 | ch.SetIceConfig(CreateIceConfig(500, GATHER_ONCE)); |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 3765 | EXPECT_EQ(500, ch.config().receiving_timeout_or_default()); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3766 | EXPECT_EQ(50, ch.check_receiving_interval()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3767 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3768 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3769 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3770 | ASSERT_TRUE(conn1 != nullptr); |
| 3771 | |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 3772 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3773 | conn1->ReceivedPing(); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 3774 | conn1->OnReadPacket("ABC", 3, rtc::TimeMicros()); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3775 | EXPECT_TRUE_SIMULATED_WAIT(ch.receiving(), kShortTimeout, clock); |
| 3776 | EXPECT_TRUE_SIMULATED_WAIT(!ch.receiving(), kShortTimeout, clock); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3777 | } |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3778 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3779 | // The controlled side will select a connection as the "selected connection" |
| 3780 | // based on priority until the controlling side nominates a connection, at which |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3781 | // point the controlled side will select that connection as the |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3782 | // "selected connection". Plus, SignalNetworkRouteChanged will be fired if the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3783 | // selected connection changes and SignalReadyToSend will be fired if the new |
| 3784 | // selected connection is writable. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3785 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3786 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3787 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3788 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3789 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3790 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3791 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3792 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3793 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3794 | // Channel is not ready to send because it is not writable. |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3795 | EXPECT_FALSE(channel_ready_to_send()); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3796 | int last_packet_id = 0; |
| 3797 | const char* data = "ABCDEFGH"; |
| 3798 | int len = static_cast<int>(strlen(data)); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3799 | EXPECT_EQ(-1, SendData(&ch, data, len, ++last_packet_id)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3800 | EXPECT_EQ(-1, last_sent_packet_id()); |
| 3801 | |
| 3802 | // A connection needs to be writable before it is selected for transmission. |
| 3803 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
| 3804 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3805 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3806 | EXPECT_TRUE(ConnectionMatchesChangeEvent( |
| 3807 | conn1, "remote candidate generation maybe changed")); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3808 | EXPECT_EQ(len, SendData(&ch, data, len, ++last_packet_id)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3809 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3810 | // When a higher priority candidate comes in, the new connection is chosen |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3811 | // as the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3812 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); |
| 3813 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3814 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3815 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
| 3816 | EXPECT_EQ_WAIT(conn2, ch.selected_connection(), kDefaultTimeout); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3817 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3818 | EXPECT_TRUE( |
| 3819 | ConnectionMatchesChangeEvent(conn2, "candidate pair state changed")); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3820 | EXPECT_TRUE(channel_ready_to_send()); |
| 3821 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3822 | |
| 3823 | // If a stun request with use-candidate attribute arrives, the receiving |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3824 | // connection will be set as the selected connection, even though |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3825 | // its priority is lower. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3826 | EXPECT_EQ(len, SendData(&ch, data, len, ++last_packet_id)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3827 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 1)); |
| 3828 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3829 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3830 | // Because it has a lower priority, the selected connection is still conn2. |
| 3831 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3832 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3833 | // But if it is nominated via use_candidate, it is chosen as the selected |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3834 | // connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3835 | NominateConnection(conn3); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3836 | EXPECT_EQ(conn3, ch.selected_connection()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3837 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn3)); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3838 | EXPECT_TRUE( |
| 3839 | ConnectionMatchesChangeEvent(conn3, "nomination on the controlled side")); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3840 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3841 | EXPECT_TRUE(channel_ready_to_send()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3842 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3843 | // Even if another higher priority candidate arrives, it will not be set as |
| 3844 | // the selected connection because the selected connection is nominated by |
| 3845 | // the controlling side. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 3846 | EXPECT_EQ(len, SendData(&ch, data, len, ++last_packet_id)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3847 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "4.4.4.4", 4, 100)); |
| 3848 | Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3849 | ASSERT_TRUE(conn4 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3850 | EXPECT_EQ(conn3, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3851 | // But if it is nominated via use_candidate and writable, it will be set as |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3852 | // the selected connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3853 | NominateConnection(conn4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3854 | // Not switched yet because conn4 is not writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3855 | EXPECT_EQ(conn3, ch.selected_connection()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3856 | reset_channel_ready_to_send(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3857 | // The selected connection switches after conn4 becomes writable. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3858 | conn4->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3859 | EXPECT_EQ_WAIT(conn4, ch.selected_connection(), kDefaultTimeout); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3860 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn4)); |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 3861 | EXPECT_TRUE( |
| 3862 | ConnectionMatchesChangeEvent(conn4, "candidate pair state changed")); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3863 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3864 | // SignalReadyToSend is fired again because conn4 is writable. |
| 3865 | EXPECT_TRUE(channel_ready_to_send()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3868 | // The controlled side will select a connection as the "selected connection" |
| 3869 | // based on requests from an unknown address before the controlling side |
| 3870 | // nominates a connection, and will nominate a connection from an unknown |
| 3871 | // address if the request contains the use_candidate attribute. Plus, it will |
| 3872 | // also sends back a ping response and set the ICE pwd in the remote candidate |
| 3873 | // appropriately. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3874 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3875 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3876 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3877 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3878 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3879 | ch.MaybeStartGathering(); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3880 | // A minimal STUN message with prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3881 | IceMessage request; |
| 3882 | request.SetType(STUN_BINDING_REQUEST); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3883 | request.AddAttribute(std::make_unique<StunByteStringAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3884 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3885 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3886 | request.AddAttribute(std::make_unique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, |
| 3887 | prflx_priority)); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3888 | TestUDPPort* port = static_cast<TestUDPPort*>(GetPort(&ch)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3889 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3890 | &request, kIceUfrag[1], false); |
| 3891 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3892 | ASSERT_TRUE(conn1 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3893 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3894 | EXPECT_NE(conn1, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3895 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3896 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3897 | port->set_sent_binding_response(false); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3898 | |
| 3899 | // Another connection is nominated via use_candidate. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3900 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3901 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3902 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3903 | // Because it has a lower priority, the selected connection is still conn1. |
| 3904 | EXPECT_EQ(conn1, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3905 | // When it is nominated via use_candidate and writable, it is chosen as the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3906 | // selected connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3907 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
| 3908 | NominateConnection(conn2); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3909 | EXPECT_EQ(conn2, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3910 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3911 | // Another request with unknown address, it will not be set as the selected |
| 3912 | // connection because the selected connection was nominated by the controlling |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3913 | // side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3914 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP, |
| 3915 | &request, kIceUfrag[1], false); |
| 3916 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3917 | ASSERT_TRUE(conn3 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3918 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3919 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3920 | EXPECT_EQ(conn2, ch.selected_connection()); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3921 | port->set_sent_binding_response(false); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3922 | |
| 3923 | // However if the request contains use_candidate attribute, it will be |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3924 | // selected as the selected connection. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3925 | request.AddAttribute( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3926 | std::make_unique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3927 | port->SignalUnknownAddress(port, rtc::SocketAddress("4.4.4.4", 4), PROTO_UDP, |
| 3928 | &request, kIceUfrag[1], false); |
| 3929 | Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3930 | ASSERT_TRUE(conn4 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3931 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3932 | // conn4 is not the selected connection yet because it is not writable. |
| 3933 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3934 | conn4->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3935 | EXPECT_EQ_WAIT(conn4, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3936 | |
| 3937 | // Test that the request from an unknown address contains a ufrag from an old |
| 3938 | // generation. |
| 3939 | port->set_sent_binding_response(false); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3940 | ch.SetRemoteIceParameters(kIceParams[2]); |
| 3941 | ch.SetRemoteIceParameters(kIceParams[3]); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3942 | port->SignalUnknownAddress(port, rtc::SocketAddress("5.5.5.5", 5), PROTO_UDP, |
| 3943 | &request, kIceUfrag[2], false); |
| 3944 | Connection* conn5 = WaitForConnectionTo(&ch, "5.5.5.5", 5); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3945 | ASSERT_TRUE(conn5 != nullptr); |
| 3946 | EXPECT_TRUE(port->sent_binding_response()); |
| 3947 | EXPECT_EQ(kIcePwd[2], conn5->remote_candidate().password()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3950 | // The controlled side will select a connection as the "selected connection" |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3951 | // based on media received until the controlling side nominates a connection, |
| 3952 | // at which point the controlled side will select that connection as |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3953 | // the "selected connection". |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3954 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3955 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3956 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3957 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3958 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3959 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3960 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 10)); |
| 3961 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3962 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3963 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
| 3964 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3965 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3966 | // If a data packet is received on conn2, the selected connection should |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3967 | // switch to conn2 because the controlled side must mirror the media path |
| 3968 | // chosen by the controlling side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3969 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3970 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3971 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3972 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable and receiving. |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 3973 | conn2->OnReadPacket("ABC", 3, rtc::TimeMicros()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3974 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3975 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3976 | |
| 3977 | // Now another STUN message with an unknown address and use_candidate will |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3978 | // nominate the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3979 | IceMessage request; |
| 3980 | request.SetType(STUN_BINDING_REQUEST); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3981 | request.AddAttribute(std::make_unique<StunByteStringAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3982 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3983 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3984 | request.AddAttribute(std::make_unique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, |
| 3985 | prflx_priority)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3986 | request.AddAttribute( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 3987 | std::make_unique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3988 | Port* port = GetPort(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3989 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP, |
| 3990 | &request, kIceUfrag[1], false); |
| 3991 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3992 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3993 | EXPECT_EQ(conn2, ch.selected_connection()); // Not writable yet. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3994 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3995 | EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3996 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3997 | // Now another data packet will not switch the selected connection because the |
| 3998 | // selected connection was nominated by the controlling side. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3999 | conn2->ReceivedPing(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4000 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 4001 | conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4002 | EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); |
| 4003 | } |
| 4004 | |
| 4005 | TEST_F(P2PTransportChannelPingTest, |
| 4006 | TestControlledAgentDataReceivingTakesHigherPrecedenceThanPriority) { |
| 4007 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4008 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4009 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4010 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 4011 | PrepareChannel(&ch); |
| 4012 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4013 | ch.MaybeStartGathering(); |
| 4014 | // The connections have decreasing priority. |
| 4015 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4016 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4017 | ASSERT_TRUE(conn1 != nullptr); |
| 4018 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4019 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4020 | ASSERT_TRUE(conn2 != nullptr); |
| 4021 | |
| 4022 | // Initially, connections are selected based on priority. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4023 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4024 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4025 | |
| 4026 | // conn2 receives data; it becomes selected. |
| 4027 | // Advance the clock by 1ms so that the last data receiving timestamp of |
| 4028 | // conn2 is larger. |
| 4029 | SIMULATED_WAIT(false, 1, clock); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 4030 | conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4031 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4032 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4033 | |
| 4034 | // conn1 also receives data; it becomes selected due to priority again. |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 4035 | conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4036 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4037 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4038 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4039 | // conn2 received data more recently; it is selected now because it |
| 4040 | // received data more recently. |
| 4041 | SIMULATED_WAIT(false, 1, clock); |
| 4042 | // Need to become writable again because it was pruned. |
| 4043 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 4044 | conn2->OnReadPacket("XYZ", 3, rtc::TimeMicros()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4045 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4046 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4047 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4048 | // Make sure sorting won't reselect candidate pair. |
| 4049 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4050 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | TEST_F(P2PTransportChannelPingTest, |
| 4054 | TestControlledAgentNominationTakesHigherPrecedenceThanDataReceiving) { |
| 4055 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4056 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4057 | |
| 4058 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4059 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 4060 | PrepareChannel(&ch); |
| 4061 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4062 | ch.MaybeStartGathering(); |
| 4063 | // The connections have decreasing priority. |
| 4064 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4065 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4066 | ASSERT_TRUE(conn1 != nullptr); |
| 4067 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4068 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4069 | ASSERT_TRUE(conn2 != nullptr); |
| 4070 | |
| 4071 | // conn1 received data; it is the selected connection. |
| 4072 | // Advance the clock to have a non-zero last-data-receiving time. |
| 4073 | SIMULATED_WAIT(false, 1, clock); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 4074 | conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4075 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4076 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4077 | |
| 4078 | // conn2 is nominated; it becomes the selected connection. |
| 4079 | NominateConnection(conn2); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4080 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4081 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4082 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4083 | // conn1 is selected because it has higher priority and also nominated. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4084 | NominateConnection(conn1); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4085 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4086 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4087 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4088 | // Make sure sorting won't reselect candidate pair. |
| 4089 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4090 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | TEST_F(P2PTransportChannelPingTest, |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4094 | TestControlledAgentSelectsConnectionWithHigherNomination) { |
| 4095 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4096 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4097 | |
| 4098 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4099 | P2PTransportChannel ch("test", 1, &pa); |
| 4100 | PrepareChannel(&ch); |
| 4101 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 4102 | ch.MaybeStartGathering(); |
| 4103 | // The connections have decreasing priority. |
| 4104 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4105 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4106 | ASSERT_TRUE(conn1 != nullptr); |
| 4107 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4108 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4109 | ASSERT_TRUE(conn2 != nullptr); |
| 4110 | |
| 4111 | // conn1 is the selected connection because it has a higher priority, |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4112 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 4113 | clock); |
| 4114 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4115 | reset_selected_candidate_pair_switches(); |
| 4116 | |
| 4117 | // conn2 is nominated; it becomes selected. |
| 4118 | NominateConnection(conn2); |
| 4119 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4120 | EXPECT_EQ(conn2, ch.selected_connection()); |
| 4121 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4122 | |
| 4123 | // conn1 is selected because of its priority. |
| 4124 | NominateConnection(conn1); |
| 4125 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4126 | EXPECT_EQ(conn1, ch.selected_connection()); |
| 4127 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4128 | |
| 4129 | // conn2 gets higher remote nomination; it is selected again. |
| 4130 | NominateConnection(conn2, 2U); |
| 4131 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4132 | EXPECT_EQ(conn2, ch.selected_connection()); |
| 4133 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4134 | |
| 4135 | // Make sure sorting won't reselect candidate pair. |
| 4136 | SIMULATED_WAIT(false, 100, clock); |
| 4137 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 4138 | } |
| 4139 | |
| 4140 | TEST_F(P2PTransportChannelPingTest, |
| 4141 | TestControlledAgentIgnoresSmallerNomination) { |
| 4142 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4143 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4144 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4145 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4146 | P2PTransportChannel ch("test", 1, &pa); |
| 4147 | PrepareChannel(&ch); |
| 4148 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 4149 | ch.MaybeStartGathering(); |
| 4150 | Connection* conn = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4151 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, false); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4152 | ReceivePingOnConnection(conn, kIceUfrag[1], 1, 2U); |
| 4153 | EXPECT_EQ(2U, conn->remote_nomination()); |
| 4154 | // Smaller nomination is ignored. |
| 4155 | ReceivePingOnConnection(conn, kIceUfrag[1], 1, 1U); |
| 4156 | EXPECT_EQ(2U, conn->remote_nomination()); |
| 4157 | } |
| 4158 | |
| 4159 | TEST_F(P2PTransportChannelPingTest, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4160 | TestControlledAgentWriteStateTakesHigherPrecedenceThanNomination) { |
| 4161 | rtc::ScopedFakeClock clock; |
| 4162 | |
| 4163 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4164 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 4165 | PrepareChannel(&ch); |
| 4166 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4167 | ch.MaybeStartGathering(); |
| 4168 | // The connections have decreasing priority. |
| 4169 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4170 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, false); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4171 | ASSERT_TRUE(conn1 != nullptr); |
| 4172 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4173 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, false); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4174 | ASSERT_TRUE(conn2 != nullptr); |
| 4175 | |
| 4176 | NominateConnection(conn1); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4177 | // There is no selected connection because no connection is writable. |
| 4178 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4179 | |
| 4180 | // conn2 becomes writable; it is selected even though it is not nominated. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4181 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4182 | EXPECT_EQ_SIMULATED_WAIT(1, reset_selected_candidate_pair_switches(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4183 | kDefaultTimeout, clock); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4184 | EXPECT_EQ_SIMULATED_WAIT(conn2, ch.selected_connection(), kDefaultTimeout, |
| 4185 | clock); |
| 4186 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4187 | |
| 4188 | // If conn1 is also writable, it will become selected. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4189 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4190 | EXPECT_EQ_SIMULATED_WAIT(1, reset_selected_candidate_pair_switches(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4191 | kDefaultTimeout, clock); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 4192 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 4193 | clock); |
| 4194 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4195 | |
| 4196 | // Make sure sorting won't reselect candidate pair. |
| 4197 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4198 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 4199 | } |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4200 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4201 | // Test that if a new remote candidate has the same address and port with |
| 4202 | // an old one, it will be used to create a new connection. |
| 4203 | TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4204 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4205 | P2PTransportChannel ch("candidate reuse", 1, &pa); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4206 | PrepareChannel(&ch); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4207 | ch.MaybeStartGathering(); |
| 4208 | const std::string host_address = "1.1.1.1"; |
| 4209 | const int port_num = 1; |
| 4210 | |
| 4211 | // kIceUfrag[1] is the current generation ufrag. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4212 | Candidate candidate = CreateUdpCandidate(LOCAL_PORT_TYPE, host_address, |
| 4213 | port_num, 1, kIceUfrag[1]); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4214 | ch.AddRemoteCandidate(candidate); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4215 | Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4216 | ASSERT_TRUE(conn1 != nullptr); |
| 4217 | EXPECT_EQ(0u, conn1->remote_candidate().generation()); |
| 4218 | |
| 4219 | // Simply adding the same candidate again won't create a new connection. |
| 4220 | ch.AddRemoteCandidate(candidate); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4221 | Connection* conn2 = GetConnectionTo(&ch, host_address, port_num); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4222 | EXPECT_EQ(conn1, conn2); |
| 4223 | |
| 4224 | // Update the ufrag of the candidate and add it again. |
| 4225 | candidate.set_username(kIceUfrag[2]); |
| 4226 | ch.AddRemoteCandidate(candidate); |
| 4227 | conn2 = GetConnectionTo(&ch, host_address, port_num); |
| 4228 | EXPECT_NE(conn1, conn2); |
| 4229 | EXPECT_EQ(kIceUfrag[2], conn2->remote_candidate().username()); |
| 4230 | EXPECT_EQ(1u, conn2->remote_candidate().generation()); |
| 4231 | |
| 4232 | // Verify that a ping with the new ufrag can be received on the new |
| 4233 | // connection. |
| 4234 | EXPECT_EQ(0, conn2->last_ping_received()); |
| 4235 | ReceivePingOnConnection(conn2, kIceUfrag[2], 1 /* priority */); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4236 | EXPECT_GT(conn2->last_ping_received(), 0); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4239 | // When the current selected connection is strong, lower-priority connections |
| 4240 | // will be pruned. Otherwise, lower-priority connections are kept. |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4241 | TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4242 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4243 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4244 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4245 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4246 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4247 | ch.SetIceRole(ICEROLE_CONTROLLED); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4248 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4249 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4250 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4251 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4252 | EXPECT_EQ(nullptr, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4253 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4254 | |
| 4255 | // When a higher-priority, nominated candidate comes in, the connections with |
| 4256 | // lower-priority are pruned. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4257 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4258 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4259 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4260 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 4261 | NominateConnection(conn2); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4262 | EXPECT_TRUE_SIMULATED_WAIT(conn1->pruned(), kMediumTimeout, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4263 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4264 | ch.SetIceConfig(CreateIceConfig(500, GATHER_ONCE)); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4265 | // Wait until conn2 becomes not receiving. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4266 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->receiving(), kMediumTimeout, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4267 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4268 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 1)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4269 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3, &clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4270 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4271 | // The selected connection should still be conn2. Even through conn3 has lower |
| 4272 | // priority and is not receiving/writable, it is not pruned because the |
| 4273 | // selected connection is not receiving. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4274 | SIMULATED_WAIT(conn3->pruned(), kShortTimeout, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4275 | EXPECT_FALSE(conn3->pruned()); |
| 4276 | } |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4277 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4278 | TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { |
| 4279 | rtc::ScopedFakeClock clock; |
| 4280 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4281 | P2PTransportChannel ch("test channel", 1, &pa); |
| 4282 | PrepareChannel(&ch); |
| 4283 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4284 | ch.MaybeStartGathering(); |
| 4285 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4286 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 100, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4287 | ASSERT_TRUE(conn1 != nullptr); |
| 4288 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 4289 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 200, false); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4290 | ASSERT_TRUE(conn2 != nullptr); |
| 4291 | // Even if conn1 is writable, nominated, receiving data, it should not prune |
| 4292 | // conn2. |
| 4293 | NominateConnection(conn1); |
| 4294 | SIMULATED_WAIT(false, 1, clock); |
Niels Möller | 15ca5a9 | 2018-11-01 14:32:47 +0100 | [diff] [blame] | 4295 | conn1->OnReadPacket("XYZ", 3, rtc::TimeMicros()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4296 | SIMULATED_WAIT(conn2->pruned(), 100, clock); |
| 4297 | EXPECT_FALSE(conn2->pruned()); |
| 4298 | } |
| 4299 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4300 | // Test that GetState returns the state correctly. |
| 4301 | TEST_F(P2PTransportChannelPingTest, TestGetState) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4302 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4303 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4304 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4305 | P2PTransportChannel ch("test channel", 1, &pa); |
Jonas Olsson | 81125f0 | 2018-10-09 10:52:04 +0200 | [diff] [blame] | 4306 | EXPECT_EQ(webrtc::IceTransportState::kNew, ch.GetIceTransportState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4307 | PrepareChannel(&ch); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4308 | ch.MaybeStartGathering(); |
Seth Hampson | 98335f8 | 2019-02-22 17:50:03 -0800 | [diff] [blame] | 4309 | // After gathering we are still in the kNew state because we aren't checking |
| 4310 | // any connections yet. |
| 4311 | EXPECT_EQ(webrtc::IceTransportState::kNew, ch.GetIceTransportState()); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4312 | EXPECT_EQ(IceTransportState::STATE_INIT, ch.GetState()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4313 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 4314 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
Seth Hampson | 98335f8 | 2019-02-22 17:50:03 -0800 | [diff] [blame] | 4315 | // Checking candidates that have been added with gathered candidates. |
| 4316 | ASSERT_GT(ch.connections().size(), 0u); |
| 4317 | EXPECT_EQ(webrtc::IceTransportState::kChecking, ch.GetIceTransportState()); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4318 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
| 4319 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4320 | ASSERT_TRUE(conn1 != nullptr); |
| 4321 | ASSERT_TRUE(conn2 != nullptr); |
| 4322 | // Now there are two connections, so the transport channel is connecting. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4323 | EXPECT_EQ(IceTransportState::STATE_CONNECTING, ch.GetState()); |
Seth Hampson | 98335f8 | 2019-02-22 17:50:03 -0800 | [diff] [blame] | 4324 | // No connections are writable yet, so we should still be in the kChecking |
| 4325 | // state. |
| 4326 | EXPECT_EQ(webrtc::IceTransportState::kChecking, ch.GetIceTransportState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4327 | // |conn1| becomes writable and receiving; it then should prune |conn2|. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4328 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4329 | EXPECT_TRUE_SIMULATED_WAIT(conn2->pruned(), kShortTimeout, clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4330 | EXPECT_EQ(IceTransportState::STATE_COMPLETED, ch.GetState()); |
Seth Hampson | 98335f8 | 2019-02-22 17:50:03 -0800 | [diff] [blame] | 4331 | EXPECT_EQ(webrtc::IceTransportState::kConnected, ch.GetIceTransportState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4332 | conn1->Prune(); // All connections are pruned. |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 4333 | // Need to wait until the channel state is updated. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4334 | EXPECT_EQ_SIMULATED_WAIT(IceTransportState::STATE_FAILED, ch.GetState(), |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4335 | kShortTimeout, clock); |
Jonas Olsson | 81125f0 | 2018-10-09 10:52:04 +0200 | [diff] [blame] | 4336 | EXPECT_EQ(webrtc::IceTransportState::kFailed, ch.GetIceTransportState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | // Test that when a low-priority connection is pruned, it is not deleted |
| 4340 | // right away, and it can become active and be pruned again. |
| 4341 | TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4342 | rtc::ScopedFakeClock clock; |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4343 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4344 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4345 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4346 | P2PTransportChannel ch("test channel", 1, &pa); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4347 | PrepareChannel(&ch); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4348 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 4349 | config.receiving_switching_delay = 800; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4350 | ch.SetIceConfig(config); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4351 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4352 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4353 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4354 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4355 | EXPECT_EQ(nullptr, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4356 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4357 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 4358 | clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4359 | |
| 4360 | // Add a low-priority connection |conn2|, which will be pruned, but it will |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4361 | // not be deleted right away. Once the current selected connection becomes not |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4362 | // receiving, |conn2| will start to ping and upon receiving the ping response, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4363 | // it will become the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4364 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4365 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4366 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4367 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->active(), kDefaultTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4368 | // |conn2| should not send a ping yet. |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 4369 | EXPECT_EQ(IceCandidatePairState::WAITING, conn2->state()); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4370 | EXPECT_EQ(IceTransportState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4371 | // Wait for |conn1| becoming not receiving. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4372 | EXPECT_TRUE_SIMULATED_WAIT(!conn1->receiving(), kMediumTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4373 | // Make sure conn2 is not deleted. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4374 | conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4375 | ASSERT_TRUE(conn2 != nullptr); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 4376 | EXPECT_EQ_SIMULATED_WAIT(IceCandidatePairState::IN_PROGRESS, conn2->state(), |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4377 | kDefaultTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4378 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4379 | EXPECT_EQ_SIMULATED_WAIT(conn2, ch.selected_connection(), kDefaultTimeout, |
| 4380 | clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4381 | EXPECT_EQ(IceTransportState::STATE_CONNECTING, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4382 | |
| 4383 | // When |conn1| comes back again, |conn2| will be pruned again. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4384 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4385 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 4386 | clock); |
| 4387 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->active(), kDefaultTimeout, clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4388 | EXPECT_EQ(IceTransportState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4389 | } |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4390 | |
| 4391 | // Test that if all connections in a channel has timed out on writing, they |
| 4392 | // will all be deleted. We use Prune to simulate write_time_out. |
| 4393 | TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4394 | rtc::ScopedFakeClock clock; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4395 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4396 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4397 | PrepareChannel(&ch); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4398 | ch.MaybeStartGathering(); |
| 4399 | // Have one connection only but later becomes write-time-out. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4400 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4401 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4402 | ASSERT_TRUE(conn1 != nullptr); |
| 4403 | conn1->ReceivedPing(); // Becomes receiving |
| 4404 | conn1->Prune(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4405 | EXPECT_TRUE_SIMULATED_WAIT(ch.connections().empty(), kShortTimeout, clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4406 | |
| 4407 | // Have two connections but both become write-time-out later. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4408 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4409 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4410 | ASSERT_TRUE(conn2 != nullptr); |
| 4411 | conn2->ReceivedPing(); // Becomes receiving |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4412 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 2)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4413 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3, &clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4414 | ASSERT_TRUE(conn3 != nullptr); |
| 4415 | conn3->ReceivedPing(); // Becomes receiving |
| 4416 | // Now prune both conn2 and conn3; they will be deleted soon. |
| 4417 | conn2->Prune(); |
| 4418 | conn3->Prune(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4419 | EXPECT_TRUE_SIMULATED_WAIT(ch.connections().empty(), kShortTimeout, clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4420 | } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4421 | |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4422 | // Tests that after a port allocator session is started, it will be stopped |
| 4423 | // when a new connection becomes writable and receiving. Also tests that if a |
| 4424 | // connection belonging to an old session becomes writable, it won't stop |
| 4425 | // the current port allocator session. |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4426 | TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4427 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4428 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4429 | PrepareChannel(&ch); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4430 | ch.SetIceConfig(CreateIceConfig(2000, GATHER_ONCE)); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4431 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4432 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 4433 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4434 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4435 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4436 | EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); |
| 4437 | |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4438 | // Start a new session. Even though conn1, which belongs to an older |
| 4439 | // session, becomes unwritable and writable again, it should not stop the |
| 4440 | // current session. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4441 | ch.SetIceParameters(kIceParams[1]); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4442 | ch.MaybeStartGathering(); |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4443 | conn1->Prune(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4444 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4445 | EXPECT_TRUE(ch.allocator_session()->IsGettingPorts()); |
| 4446 | |
| 4447 | // But if a new connection created from the new session becomes writable, |
| 4448 | // it will stop the current session. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4449 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 100)); |
| 4450 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4451 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4452 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4453 | EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); |
| 4454 | } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4455 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4456 | // Test that the ICE role is updated even on ports that has been removed. |
| 4457 | // These ports may still have connections that need a correct role, in case that |
| 4458 | // the connections on it may still receive stun pings. |
| 4459 | TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnRemovedPort) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4460 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4461 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4462 | // Starts with ICEROLE_CONTROLLING. |
| 4463 | PrepareChannel(&ch); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4464 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4465 | ch.SetIceConfig(config); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4466 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4467 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4468 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4469 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4470 | ASSERT_TRUE(conn != nullptr); |
| 4471 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4472 | // Make a fake signal to remove the ports in the p2ptransportchannel. then |
| 4473 | // change the ICE role and expect it to be updated. |
| 4474 | std::vector<PortInterface*> ports(1, conn->port()); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 4475 | ch.allocator_session()->SignalPortsPruned(ch.allocator_session(), ports); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4476 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 4477 | EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | // Test that the ICE role is updated even on ports with inactive networks. |
| 4481 | // These ports may still have connections that need a correct role, for the |
| 4482 | // pings sent by those connections until they're replaced by newer-generation |
| 4483 | // connections. |
| 4484 | TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4485 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4486 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4487 | // Starts with ICEROLE_CONTROLLING. |
| 4488 | PrepareChannel(&ch); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4489 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4490 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4491 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4492 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4493 | ASSERT_TRUE(conn != nullptr); |
| 4494 | |
| 4495 | // Do an ICE restart, change the role, and expect the old port to have its |
| 4496 | // role updated. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4497 | ch.SetIceParameters(kIceParams[1]); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4498 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4499 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 4500 | EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | // Test that after some amount of time without receiving data, the connection |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4504 | // will be destroyed. The port will only be destroyed after it is marked as |
| 4505 | // "pruned." |
| 4506 | TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeoutAndPruned) { |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4507 | rtc::ScopedFakeClock fake_clock; |
| 4508 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4509 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4510 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4511 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4512 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4513 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4514 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4515 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4516 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4517 | ASSERT_TRUE(conn != nullptr); |
| 4518 | |
| 4519 | // Simulate 2 minutes going by. This should be enough time for the port to |
| 4520 | // time out. |
| 4521 | for (int second = 0; second < 120; ++second) { |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 4522 | fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4523 | } |
| 4524 | EXPECT_EQ(nullptr, GetConnectionTo(&ch, "1.1.1.1", 1)); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4525 | // Port will not be removed because it is not pruned yet. |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 4526 | PortInterface* port = GetPort(&ch); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4527 | ASSERT_NE(nullptr, port); |
| 4528 | |
| 4529 | // If the session prunes all ports, the port will be destroyed. |
| 4530 | ch.allocator_session()->PruneAllPorts(); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 4531 | EXPECT_EQ_SIMULATED_WAIT(nullptr, GetPort(&ch), 1, fake_clock); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4532 | EXPECT_EQ_SIMULATED_WAIT(nullptr, GetPrunedPort(&ch), 1, fake_clock); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4533 | } |
| 4534 | |
Jonas Oreland | c6404a1 | 2019-10-14 15:52:15 +0200 | [diff] [blame] | 4535 | TEST_F(P2PTransportChannelPingTest, TestMaxOutstandingPingsFieldTrial) { |
| 4536 | webrtc::test::ScopedFieldTrials field_trials( |
| 4537 | "WebRTC-IceFieldTrials/max_outstanding_pings:3/"); |
| 4538 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4539 | P2PTransportChannel ch("max", 1, &pa); |
| 4540 | ch.SetIceConfig(ch.config()); |
| 4541 | PrepareChannel(&ch); |
| 4542 | ch.MaybeStartGathering(); |
| 4543 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4544 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
| 4545 | |
| 4546 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 4547 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
| 4548 | ASSERT_TRUE(conn1 != nullptr); |
| 4549 | ASSERT_TRUE(conn2 != nullptr); |
| 4550 | |
| 4551 | EXPECT_TRUE_WAIT(conn1->num_pings_sent() == 3 && conn2->num_pings_sent() == 3, |
| 4552 | kDefaultTimeout); |
| 4553 | |
| 4554 | // Check that these connections don't send any more pings. |
| 4555 | EXPECT_EQ(nullptr, ch.FindNextPingableConnection()); |
| 4556 | } |
| 4557 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4558 | class P2PTransportChannelMostLikelyToWorkFirstTest |
| 4559 | : public P2PTransportChannelPingTest { |
| 4560 | public: |
| 4561 | P2PTransportChannelMostLikelyToWorkFirstTest() |
| 4562 | : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) { |
| 4563 | network_manager_.AddInterface(kPublicAddrs[0]); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 4564 | allocator_.reset( |
| 4565 | CreateBasicPortAllocator(&network_manager_, ServerAddresses(), |
| 4566 | kTurnUdpIntAddr, rtc::SocketAddress())); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4567 | allocator_->set_flags(allocator_->flags() | PORTALLOCATOR_DISABLE_STUN | |
| 4568 | PORTALLOCATOR_DISABLE_TCP); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4569 | allocator_->set_step_delay(kMinimumStepDelay); |
| 4570 | } |
| 4571 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4572 | P2PTransportChannel& StartTransportChannel( |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4573 | bool prioritize_most_likely_to_work, |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 4574 | int stable_writable_connection_ping_interval) { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 4575 | channel_.reset(new P2PTransportChannel("checks", 1, allocator())); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4576 | IceConfig config = channel_->config(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4577 | config.prioritize_most_likely_candidate_pairs = |
| 4578 | prioritize_most_likely_to_work; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 4579 | config.stable_writable_connection_ping_interval = |
| 4580 | stable_writable_connection_ping_interval; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4581 | channel_->SetIceConfig(config); |
| 4582 | PrepareChannel(channel_.get()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4583 | channel_->MaybeStartGathering(); |
| 4584 | return *channel_.get(); |
| 4585 | } |
| 4586 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4587 | BasicPortAllocator* allocator() { return allocator_.get(); } |
| 4588 | TestTurnServer* turn_server() { return &turn_server_; } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4589 | |
| 4590 | // This verifies the next pingable connection has the expected candidates' |
| 4591 | // types and, for relay local candidate, the expected relay protocol and ping |
| 4592 | // it. |
| 4593 | void VerifyNextPingableConnection( |
| 4594 | const std::string& local_candidate_type, |
| 4595 | const std::string& remote_candidate_type, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4596 | const std::string& relay_protocol_type = UDP_PROTOCOL_NAME) { |
| 4597 | Connection* conn = FindNextPingableConnectionAndPingIt(channel_.get()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4598 | EXPECT_EQ(conn->local_candidate().type(), local_candidate_type); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4599 | if (conn->local_candidate().type() == RELAY_PORT_TYPE) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4600 | EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type); |
| 4601 | } |
| 4602 | EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type); |
| 4603 | } |
| 4604 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4605 | private: |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4606 | std::unique_ptr<BasicPortAllocator> allocator_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4607 | rtc::FakeNetworkManager network_manager_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4608 | TestTurnServer turn_server_; |
| 4609 | std::unique_ptr<P2PTransportChannel> channel_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4610 | }; |
| 4611 | |
| 4612 | // Test that Relay/Relay connections will be pinged first when no other |
| 4613 | // connections have been pinged yet, unless we need to ping a trigger check or |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4614 | // we have a selected connection. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4615 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4616 | TestRelayRelayFirstWhenNothingPingedYet) { |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 4617 | const int max_strong_interval = 500; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4618 | P2PTransportChannel& ch = StartTransportChannel(true, max_strong_interval); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4619 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4620 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4621 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4622 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4623 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4624 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4625 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4626 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4627 | |
| 4628 | // Relay/Relay should be the first pingable connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4629 | Connection* conn = FindNextPingableConnectionAndPingIt(&ch); |
| 4630 | EXPECT_EQ(conn->local_candidate().type(), RELAY_PORT_TYPE); |
| 4631 | EXPECT_EQ(conn->remote_candidate().type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4632 | |
| 4633 | // Unless that we have a trigger check waiting to be pinged. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4634 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
| 4635 | EXPECT_EQ(conn2->local_candidate().type(), LOCAL_PORT_TYPE); |
| 4636 | EXPECT_EQ(conn2->remote_candidate().type(), LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4637 | conn2->ReceivedPing(); |
| 4638 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
| 4639 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4640 | // Make conn3 the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4641 | Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 4642 | EXPECT_EQ(conn3->local_candidate().type(), LOCAL_PORT_TYPE); |
| 4643 | EXPECT_EQ(conn3->remote_candidate().type(), RELAY_PORT_TYPE); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4644 | conn3->ReceivedPingResponse(LOW_RTT, "id"); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4645 | ASSERT_TRUE(conn3->writable()); |
| 4646 | conn3->ReceivedPing(); |
| 4647 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4648 | /* |
| 4649 | |
| 4650 | TODO(honghaiz): Re-enable this once we use fake clock for this test to fix |
| 4651 | the flakiness. The following test becomes flaky because we now ping the |
| 4652 | connections with fast rates until every connection is pinged at least three |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4653 | times. The selected connection may have been pinged before |
| 4654 | |max_strong_interval|, so it may not be the next connection to be pinged as |
| 4655 | expected in the test. |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4656 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4657 | // Verify that conn3 will be the "selected connection" since it is readable |
| 4658 | // and writable. After |MAX_CURRENT_STRONG_INTERVAL|, it should be the next |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4659 | // pingable connection. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4660 | EXPECT_TRUE_WAIT(conn3 == ch.selected_connection(), kDefaultTimeout); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4661 | WAIT(false, max_strong_interval + 100); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4662 | conn3->ReceivedPingResponse(LOW_RTT, "id"); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4663 | ASSERT_TRUE(conn3->writable()); |
| 4664 | EXPECT_EQ(conn3, FindNextPingableConnectionAndPingIt(&ch)); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4665 | |
| 4666 | */ |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | // Test that Relay/Relay connections will be pinged first when everything has |
| 4670 | // been pinged even if the Relay/Relay connection wasn't the first to be pinged |
| 4671 | // in the first round. |
| 4672 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4673 | TestRelayRelayFirstWhenEverythingPinged) { |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 4674 | P2PTransportChannel& ch = StartTransportChannel(true, 500); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4675 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4676 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4677 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4678 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4679 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4680 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4681 | |
| 4682 | // Initially, only have Local/Local and Local/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4683 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE); |
| 4684 | VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4685 | |
| 4686 | // Remote Relay candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4687 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 2)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4688 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4689 | |
| 4690 | // Relay/Relay should be the first since it hasn't been pinged before. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4691 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4692 | |
| 4693 | // Local/Relay is the final one. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4694 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4695 | |
| 4696 | // Now, every connection has been pinged once. The next one should be |
| 4697 | // Relay/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4698 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | // Test that when we receive a new remote candidate, they will be tried first |
| 4702 | // before we re-ping Relay/Relay connections again. |
| 4703 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4704 | TestNoStarvationOnNonRelayConnection) { |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 4705 | P2PTransportChannel& ch = StartTransportChannel(true, 500); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4706 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4707 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4708 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4709 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4710 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4711 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4712 | |
| 4713 | // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4714 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4715 | |
| 4716 | // Next, ping Local/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4717 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4718 | |
| 4719 | // Remote Local candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4720 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4721 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4722 | |
| 4723 | // Local/Local should be the first since it hasn't been pinged before. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4724 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4725 | |
| 4726 | // Relay/Local is the final one. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4727 | VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4728 | |
| 4729 | // Now, every connection has been pinged once. The next one should be |
| 4730 | // Relay/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4731 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4732 | } |
| 4733 | |
Jonas Oreland | 4af7882 | 2019-10-11 08:17:06 +0200 | [diff] [blame] | 4734 | // Test skip_relay_to_non_relay_connections field-trial. |
| 4735 | // I.e that we never create connection between relay and non-relay. |
| 4736 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4737 | TestSkipRelayToNonRelayConnectionsFieldTrial) { |
| 4738 | webrtc::test::ScopedFieldTrials field_trials( |
| 4739 | "WebRTC-IceFieldTrials/skip_relay_to_non_relay_connections:true/"); |
| 4740 | P2PTransportChannel& ch = StartTransportChannel(true, 500); |
| 4741 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
| 4742 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4743 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
| 4744 | |
| 4745 | // Remote Relay candidate arrives. |
| 4746 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4747 | EXPECT_TRUE_WAIT(ch.connections().size() == 1, kDefaultTimeout); |
| 4748 | |
| 4749 | // Remote Local candidate arrives. |
| 4750 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
| 4751 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, kDefaultTimeout); |
| 4752 | } |
| 4753 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4754 | // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay, |
| 4755 | // followed by the rest. |
| 4756 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) { |
| 4757 | // Add a Tcp Turn server. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4758 | turn_server()->AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 4759 | RelayServerConfig config; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4760 | config.credentials = kRelayCredentials; |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 4761 | config.ports.push_back(ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4762 | allocator()->AddTurnServer(config); |
| 4763 | |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 4764 | P2PTransportChannel& ch = StartTransportChannel(true, 500); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4765 | EXPECT_TRUE_WAIT(ch.ports().size() == 3, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4766 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4767 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
| 4768 | EXPECT_EQ(ch.ports()[2]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4769 | |
| 4770 | // Remote Relay candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4771 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4772 | EXPECT_TRUE_WAIT(ch.connections().size() == 3, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4773 | |
| 4774 | // UDP Relay/Relay should be pinged first. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4775 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4776 | |
| 4777 | // TCP Relay/Relay is the next. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4778 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE, |
| 4779 | TCP_PROTOCOL_NAME); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4780 | |
| 4781 | // Finally, Local/Relay will be pinged. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4782 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4783 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4784 | |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4785 | // Test that a resolver is created, asked for a result, and destroyed |
Zach Stein | 5ec2c94 | 2018-11-27 16:58:08 -0800 | [diff] [blame] | 4786 | // when the address is a hostname. The destruction should happen even |
| 4787 | // if the channel is not destroyed. |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4788 | TEST(P2PTransportChannelResolverTest, HostnameCandidateIsResolved) { |
| 4789 | rtc::MockAsyncResolver mock_async_resolver; |
| 4790 | EXPECT_CALL(mock_async_resolver, GetError()).WillOnce(Return(0)); |
| 4791 | EXPECT_CALL(mock_async_resolver, GetResolvedAddress(_, _)) |
| 4792 | .WillOnce(Return(true)); |
Zach Stein | 5ec2c94 | 2018-11-27 16:58:08 -0800 | [diff] [blame] | 4793 | // Destroy is called asynchronously after the address is resolved, |
| 4794 | // so we need a variable to wait on. |
| 4795 | bool destroy_called = false; |
| 4796 | EXPECT_CALL(mock_async_resolver, Destroy(_)) |
| 4797 | .WillOnce(Assign(&destroy_called, true)); |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4798 | webrtc::MockAsyncResolverFactory mock_async_resolver_factory; |
| 4799 | EXPECT_CALL(mock_async_resolver_factory, Create()) |
| 4800 | .WillOnce(Return(&mock_async_resolver)); |
| 4801 | |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 4802 | FakePortAllocator allocator(rtc::Thread::Current(), nullptr); |
| 4803 | P2PTransportChannel channel("tn", 0, &allocator, |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4804 | &mock_async_resolver_factory); |
| 4805 | Candidate hostname_candidate; |
| 4806 | SocketAddress hostname_address("fake.test", 1000); |
| 4807 | hostname_candidate.set_address(hostname_address); |
| 4808 | channel.AddRemoteCandidate(hostname_candidate); |
| 4809 | |
| 4810 | ASSERT_EQ_WAIT(1u, channel.remote_candidates().size(), kDefaultTimeout); |
| 4811 | const RemoteCandidate& candidate = channel.remote_candidates()[0]; |
| 4812 | EXPECT_FALSE(candidate.address().IsUnresolvedIP()); |
Zach Stein | 5ec2c94 | 2018-11-27 16:58:08 -0800 | [diff] [blame] | 4813 | WAIT(destroy_called, kShortTimeout); |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4814 | } |
| 4815 | |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4816 | // Test that if we signal a hostname candidate after the remote endpoint |
| 4817 | // discovers a prflx remote candidate with the same underlying IP address, the |
| 4818 | // prflx candidate is updated to a host candidate after the name resolution is |
| 4819 | // done. |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4820 | TEST_F(P2PTransportChannelTest, |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 4821 | PeerReflexiveCandidateBeforeSignalingWithMdnsName) { |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4822 | rtc::MockAsyncResolver mock_async_resolver; |
| 4823 | webrtc::MockAsyncResolverFactory mock_async_resolver_factory; |
| 4824 | EXPECT_CALL(mock_async_resolver_factory, Create()) |
| 4825 | .WillOnce(Return(&mock_async_resolver)); |
| 4826 | |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4827 | // ep1 and ep2 will only gather host candidates with addresses |
| 4828 | // kPublicAddrs[0] and kPublicAddrs[1], respectively. |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4829 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4830 | // ICE parameter will be set up when creating the channels. |
| 4831 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 4832 | GetEndpoint(0)->network_manager_.set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 4833 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4834 | GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4835 | CreateChannels(); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4836 | // Pause sending candidates from both endpoints until we find out what port |
| 4837 | // number is assgined to ep1's host candidate. |
| 4838 | PauseCandidates(0); |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4839 | PauseCandidates(1); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4840 | ASSERT_EQ_WAIT(1u, GetEndpoint(0)->saved_candidates_.size(), kMediumTimeout); |
| 4841 | ASSERT_EQ(1u, GetEndpoint(0)->saved_candidates_[0]->candidates.size()); |
| 4842 | const auto& local_candidate = |
| 4843 | GetEndpoint(0)->saved_candidates_[0]->candidates[0]; |
| 4844 | // The IP address of ep1's host candidate should be obfuscated. |
| 4845 | EXPECT_TRUE(local_candidate.address().IsUnresolvedIP()); |
| 4846 | // This is the underlying private IP address of the same candidate at ep1. |
| 4847 | const auto local_address = rtc::SocketAddress( |
| 4848 | kPublicAddrs[0].ipaddr(), local_candidate.address().port()); |
| 4849 | // Let ep2 signal its candidate to ep1. ep1 should form a candidate |
| 4850 | // pair and start to ping. After receiving the ping, ep2 discovers a prflx |
| 4851 | // remote candidate and form a candidate pair as well. |
| 4852 | ResumeCandidates(1); |
| 4853 | ASSERT_TRUE_WAIT(ep1_ch1()->selected_connection() != nullptr, kMediumTimeout); |
| 4854 | // ep2 should have the selected connection connected to the prflx remote |
| 4855 | // candidate. |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4856 | const Connection* selected_connection = nullptr; |
| 4857 | ASSERT_TRUE_WAIT( |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4858 | (selected_connection = ep2_ch1()->selected_connection()) != nullptr, |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4859 | kMediumTimeout); |
| 4860 | EXPECT_EQ(PRFLX_PORT_TYPE, selected_connection->remote_candidate().type()); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4861 | EXPECT_EQ(kIceUfrag[0], selected_connection->remote_candidate().username()); |
| 4862 | EXPECT_EQ(kIcePwd[0], selected_connection->remote_candidate().password()); |
| 4863 | // Set expectation before ep1 signals a hostname candidate. |
| 4864 | { |
| 4865 | InSequence sequencer; |
| 4866 | EXPECT_CALL(mock_async_resolver, Start(_)); |
| 4867 | EXPECT_CALL(mock_async_resolver, GetError()).WillOnce(Return(0)); |
| 4868 | // Let the mock resolver of ep2 receives the correct resolution. |
| 4869 | EXPECT_CALL(mock_async_resolver, GetResolvedAddress(_, _)) |
| 4870 | .WillOnce(DoAll(SetArgPointee<1>(local_address), Return(true))); |
| 4871 | } |
Zach Stein | 5ec2c94 | 2018-11-27 16:58:08 -0800 | [diff] [blame] | 4872 | // Destroy is called asynchronously after the address is resolved, |
| 4873 | // so we need a variable to wait on. |
| 4874 | bool destroy_called = false; |
| 4875 | EXPECT_CALL(mock_async_resolver, Destroy(_)) |
| 4876 | .WillOnce(Assign(&destroy_called, true)); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4877 | ResumeCandidates(0); |
| 4878 | // Verify ep2's selected connection is updated to use the 'local' candidate. |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4879 | EXPECT_EQ_WAIT(LOCAL_PORT_TYPE, |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4880 | ep2_ch1()->selected_connection()->remote_candidate().type(), |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 4881 | kMediumTimeout); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4882 | EXPECT_EQ(selected_connection, ep2_ch1()->selected_connection()); |
| 4883 | |
Zach Stein | 5ec2c94 | 2018-11-27 16:58:08 -0800 | [diff] [blame] | 4884 | WAIT(destroy_called, kShortTimeout); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4885 | DestroyChannels(); |
| 4886 | } |
| 4887 | |
| 4888 | // Test that if we discover a prflx candidate during the process of name |
| 4889 | // resolution for a remote hostname candidate, we update the prflx candidate to |
| 4890 | // a host candidate if the hostname candidate turns out to have the same IP |
| 4891 | // address after the resolution completes. |
| 4892 | TEST_F(P2PTransportChannelTest, |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 4893 | PeerReflexiveCandidateDuringResolvingHostCandidateWithMdnsName) { |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4894 | NiceMock<rtc::MockAsyncResolver> mock_async_resolver; |
| 4895 | webrtc::MockAsyncResolverFactory mock_async_resolver_factory; |
| 4896 | EXPECT_CALL(mock_async_resolver_factory, Create()) |
| 4897 | .WillOnce(Return(&mock_async_resolver)); |
| 4898 | |
| 4899 | // ep1 and ep2 will only gather host candidates with addresses |
| 4900 | // kPublicAddrs[0] and kPublicAddrs[1], respectively. |
| 4901 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 4902 | // ICE parameter will be set up when creating the channels. |
| 4903 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 4904 | GetEndpoint(0)->network_manager_.set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 4905 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4906 | GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; |
| 4907 | CreateChannels(); |
| 4908 | // Pause sending candidates from both endpoints until we find out what port |
| 4909 | // number is assgined to ep1's host candidate. |
| 4910 | PauseCandidates(0); |
| 4911 | PauseCandidates(1); |
| 4912 | ASSERT_EQ_WAIT(1u, GetEndpoint(0)->saved_candidates_.size(), kMediumTimeout); |
| 4913 | ASSERT_EQ(1u, GetEndpoint(0)->saved_candidates_[0]->candidates.size()); |
| 4914 | const auto& local_candidate = |
| 4915 | GetEndpoint(0)->saved_candidates_[0]->candidates[0]; |
| 4916 | // The IP address of ep1's host candidate should be obfuscated. |
| 4917 | EXPECT_TRUE(local_candidate.address().IsUnresolvedIP()); |
| 4918 | // This is the underlying private IP address of the same candidate at ep1. |
| 4919 | const auto local_address = rtc::SocketAddress( |
| 4920 | kPublicAddrs[0].ipaddr(), local_candidate.address().port()); |
| 4921 | bool mock_async_resolver_started = false; |
| 4922 | // Not signaling done yet, and only make sure we are in the process of |
| 4923 | // resolution. |
| 4924 | EXPECT_CALL(mock_async_resolver, Start(_)) |
| 4925 | .WillOnce(InvokeWithoutArgs([&mock_async_resolver_started]() { |
| 4926 | mock_async_resolver_started = true; |
| 4927 | })); |
| 4928 | // Let ep1 signal its hostname candidate to ep2. |
| 4929 | ResumeCandidates(0); |
| 4930 | EXPECT_TRUE_WAIT(mock_async_resolver_started, kMediumTimeout); |
| 4931 | // Now that ep2 is in the process of resolving the hostname candidate signaled |
| 4932 | // by ep1. Let ep2 signal its host candidate with an IP address to ep1, so |
| 4933 | // that ep1 can form a candidate pair, select it and start to ping ep2. |
| 4934 | ResumeCandidates(1); |
| 4935 | ASSERT_TRUE_WAIT(ep1_ch1()->selected_connection() != nullptr, kMediumTimeout); |
| 4936 | // Let the mock resolver of ep2 receives the correct resolution. |
| 4937 | EXPECT_CALL(mock_async_resolver, GetResolvedAddress(_, _)) |
| 4938 | .WillOnce(DoAll(SetArgPointee<1>(local_address), Return(true))); |
| 4939 | // Upon receiving a ping from ep1, ep2 adds a prflx candidate from the |
| 4940 | // unknown address and establishes a connection. |
| 4941 | // |
| 4942 | // There is a caveat in our implementation associated with this expectation. |
| 4943 | // See the big comment in P2PTransportChannel::OnUnknownAddress. |
| 4944 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, kMediumTimeout); |
| 4945 | EXPECT_EQ(PRFLX_PORT_TYPE, |
| 4946 | ep2_ch1()->selected_connection()->remote_candidate().type()); |
| 4947 | // ep2 should also be able resolve the hostname candidate. The resolved remote |
| 4948 | // host candidate should be merged with the prflx remote candidate. |
| 4949 | mock_async_resolver.SignalDone(&mock_async_resolver); |
| 4950 | EXPECT_EQ_WAIT(LOCAL_PORT_TYPE, |
| 4951 | ep2_ch1()->selected_connection()->remote_candidate().type(), |
| 4952 | kMediumTimeout); |
| 4953 | EXPECT_EQ(1u, ep2_ch1()->remote_candidates().size()); |
| 4954 | |
| 4955 | DestroyChannels(); |
| 4956 | } |
| 4957 | |
| 4958 | // Test that if we only gather and signal a host candidate, the IP address of |
| 4959 | // which is obfuscated by an mDNS name, and if the peer can complete the name |
| 4960 | // resolution with the correct IP address, we can have a p2p connection. |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 4961 | TEST_F(P2PTransportChannelTest, CanConnectWithHostCandidateWithMdnsName) { |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4962 | NiceMock<rtc::MockAsyncResolver> mock_async_resolver; |
| 4963 | webrtc::MockAsyncResolverFactory mock_async_resolver_factory; |
| 4964 | EXPECT_CALL(mock_async_resolver_factory, Create()) |
| 4965 | .WillOnce(Return(&mock_async_resolver)); |
| 4966 | |
| 4967 | // ep1 and ep2 will only gather host candidates with addresses |
| 4968 | // kPublicAddrs[0] and kPublicAddrs[1], respectively. |
| 4969 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 4970 | // ICE parameter will be set up when creating the channels. |
| 4971 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 4972 | GetEndpoint(0)->network_manager_.set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 4973 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4974 | GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; |
| 4975 | CreateChannels(); |
| 4976 | // Pause sending candidates from both endpoints until we find out what port |
| 4977 | // number is assgined to ep1's host candidate. |
| 4978 | PauseCandidates(0); |
| 4979 | PauseCandidates(1); |
| 4980 | ASSERT_EQ_WAIT(1u, GetEndpoint(0)->saved_candidates_.size(), kMediumTimeout); |
| 4981 | ASSERT_EQ(1u, GetEndpoint(0)->saved_candidates_[0]->candidates.size()); |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 4982 | const auto& local_candidate_ep1 = |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4983 | GetEndpoint(0)->saved_candidates_[0]->candidates[0]; |
| 4984 | // The IP address of ep1's host candidate should be obfuscated. |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 4985 | EXPECT_TRUE(local_candidate_ep1.address().IsUnresolvedIP()); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4986 | // This is the underlying private IP address of the same candidate at ep1, |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 4987 | // and let the mock resolver of ep2 receive the correct resolution. |
| 4988 | rtc::SocketAddress resolved_address_ep1(local_candidate_ep1.address()); |
| 4989 | resolved_address_ep1.SetResolvedIP(kPublicAddrs[0].ipaddr()); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4990 | |
| 4991 | EXPECT_CALL(mock_async_resolver, GetResolvedAddress(_, _)) |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 4992 | .WillOnce(DoAll(SetArgPointee<1>(resolved_address_ep1), Return(true))); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 4993 | // Let ep1 signal its hostname candidate to ep2. |
| 4994 | ResumeCandidates(0); |
| 4995 | |
| 4996 | // We should be able to receive a ping from ep2 and establish a connection |
| 4997 | // with a peer reflexive candidate from ep2. |
| 4998 | ASSERT_TRUE_WAIT((ep1_ch1()->selected_connection()) != nullptr, |
| 4999 | kMediumTimeout); |
Qingsi Wang | b49b8f1 | 2018-09-16 17:48:10 -0700 | [diff] [blame] | 5000 | EXPECT_EQ(LOCAL_PORT_TYPE, |
| 5001 | ep1_ch1()->selected_connection()->local_candidate().type()); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 5002 | EXPECT_EQ(PRFLX_PORT_TYPE, |
| 5003 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5004 | |
Zach Stein | 6fcdc2f | 2018-08-23 16:25:55 -0700 | [diff] [blame] | 5005 | DestroyChannels(); |
| 5006 | } |
| 5007 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 5008 | // Test that when the IP of a host candidate is concealed by an mDNS name, the |
| 5009 | // stats from the gathering ICE endpoint do not reveal the address of this local |
| 5010 | // host candidate or the related address of a local srflx candidate from the |
| 5011 | // same endpoint. Also, the remote ICE endpoint that successfully resolves a |
| 5012 | // signaled host candidate with an mDNS name should not reveal the address of |
| 5013 | // this remote host candidate in stats. |
| 5014 | TEST_F(P2PTransportChannelTest, |
| 5015 | CandidatesSanitizedInStatsWhenMdnsObfuscationEnabled) { |
| 5016 | NiceMock<rtc::MockAsyncResolver> mock_async_resolver; |
| 5017 | webrtc::MockAsyncResolverFactory mock_async_resolver_factory; |
| 5018 | EXPECT_CALL(mock_async_resolver_factory, Create()) |
| 5019 | .WillOnce(Return(&mock_async_resolver)); |
| 5020 | |
| 5021 | // ep1 and ep2 will gather host candidates with addresses |
| 5022 | // kPublicAddrs[0] and kPublicAddrs[1], respectively. ep1 also gathers a srflx |
| 5023 | // and a relay candidates. |
| 5024 | ConfigureEndpoints(OPEN, OPEN, |
| 5025 | kDefaultPortAllocatorFlags | PORTALLOCATOR_DISABLE_TCP, |
| 5026 | kOnlyLocalPorts); |
| 5027 | // ICE parameter will be set up when creating the channels. |
| 5028 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 5029 | GetEndpoint(0)->network_manager_.set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 5030 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 5031 | GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; |
| 5032 | CreateChannels(); |
| 5033 | // Pause sending candidates from both endpoints until we find out what port |
| 5034 | // number is assigned to ep1's host candidate. |
| 5035 | PauseCandidates(0); |
| 5036 | PauseCandidates(1); |
| 5037 | // Ep1 has a UDP host, a srflx and a relay candidates. |
| 5038 | ASSERT_EQ_WAIT(3u, GetEndpoint(0)->saved_candidates_.size(), kMediumTimeout); |
| 5039 | ASSERT_EQ_WAIT(1u, GetEndpoint(1)->saved_candidates_.size(), kMediumTimeout); |
| 5040 | |
| 5041 | for (const auto& candidates_data : GetEndpoint(0)->saved_candidates_) { |
| 5042 | ASSERT_EQ(1u, candidates_data->candidates.size()); |
| 5043 | const auto& local_candidate_ep1 = candidates_data->candidates[0]; |
| 5044 | if (local_candidate_ep1.type() == LOCAL_PORT_TYPE) { |
| 5045 | // This is the underlying private IP address of the same candidate at ep1, |
| 5046 | // and let the mock resolver of ep2 receive the correct resolution. |
| 5047 | rtc::SocketAddress resolved_address_ep1(local_candidate_ep1.address()); |
| 5048 | resolved_address_ep1.SetResolvedIP(kPublicAddrs[0].ipaddr()); |
| 5049 | EXPECT_CALL(mock_async_resolver, GetResolvedAddress(_, _)) |
| 5050 | .WillOnce( |
| 5051 | DoAll(SetArgPointee<1>(resolved_address_ep1), Return(true))); |
| 5052 | break; |
| 5053 | } |
| 5054 | } |
| 5055 | ResumeCandidates(0); |
| 5056 | ResumeCandidates(1); |
| 5057 | |
| 5058 | ASSERT_EQ_WAIT(kIceGatheringComplete, ep1_ch1()->gathering_state(), |
| 5059 | kMediumTimeout); |
| 5060 | // We should have the following candidate pairs on both endpoints: |
| 5061 | // ep1_host <-> ep2_host, ep1_srflx <-> ep2_host, ep1_relay <-> ep2_host |
| 5062 | ASSERT_EQ_WAIT(3u, ep1_ch1()->connections().size(), kMediumTimeout); |
| 5063 | ASSERT_EQ_WAIT(3u, ep2_ch1()->connections().size(), kMediumTimeout); |
| 5064 | |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 5065 | IceTransportStats ice_transport_stats1; |
| 5066 | IceTransportStats ice_transport_stats2; |
| 5067 | ep1_ch1()->GetStats(&ice_transport_stats1); |
| 5068 | ep2_ch1()->GetStats(&ice_transport_stats2); |
| 5069 | EXPECT_EQ(3u, ice_transport_stats1.connection_infos.size()); |
| 5070 | EXPECT_EQ(3u, ice_transport_stats1.candidate_stats_list.size()); |
| 5071 | EXPECT_EQ(3u, ice_transport_stats2.connection_infos.size()); |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 5072 | // Check the stats of ep1 seen by ep1. |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 5073 | for (const auto& connection_info : ice_transport_stats1.connection_infos) { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 5074 | const auto& local_candidate = connection_info.local_candidate; |
| 5075 | if (local_candidate.type() == LOCAL_PORT_TYPE) { |
| 5076 | EXPECT_TRUE(local_candidate.address().IsUnresolvedIP()); |
| 5077 | } else if (local_candidate.type() == STUN_PORT_TYPE) { |
| 5078 | EXPECT_TRUE(local_candidate.related_address().IsAnyIP()); |
| 5079 | } else if (local_candidate.type() == RELAY_PORT_TYPE) { |
| 5080 | // The related address of the relay candidate should be equal to the |
| 5081 | // srflx address. Note that NAT is not configured, hence the following |
| 5082 | // expectation. |
| 5083 | EXPECT_EQ(kPublicAddrs[0].ipaddr(), |
| 5084 | local_candidate.related_address().ipaddr()); |
| 5085 | } else { |
| 5086 | FAIL(); |
| 5087 | } |
| 5088 | } |
| 5089 | // Check the stats of ep1 seen by ep2. |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 5090 | for (const auto& connection_info : ice_transport_stats2.connection_infos) { |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 5091 | const auto& remote_candidate = connection_info.remote_candidate; |
| 5092 | if (remote_candidate.type() == LOCAL_PORT_TYPE) { |
| 5093 | EXPECT_TRUE(remote_candidate.address().IsUnresolvedIP()); |
| 5094 | } else if (remote_candidate.type() == STUN_PORT_TYPE) { |
| 5095 | EXPECT_TRUE(remote_candidate.related_address().IsAnyIP()); |
| 5096 | } else if (remote_candidate.type() == RELAY_PORT_TYPE) { |
| 5097 | EXPECT_EQ(kPublicAddrs[0].ipaddr(), |
| 5098 | remote_candidate.related_address().ipaddr()); |
| 5099 | } else { |
| 5100 | FAIL(); |
| 5101 | } |
| 5102 | } |
| 5103 | DestroyChannels(); |
| 5104 | } |
| 5105 | |
Jonas Oreland | 149dc72 | 2019-08-28 08:10:27 +0200 | [diff] [blame] | 5106 | TEST_F(P2PTransportChannelTest, |
| 5107 | ConnectingIncreasesSelectedCandidatePairChanges) { |
| 5108 | rtc::ScopedFakeClock clock; |
| 5109 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 5110 | kDefaultPortAllocatorFlags); |
| 5111 | CreateChannels(); |
| 5112 | |
| 5113 | IceTransportStats ice_transport_stats; |
| 5114 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5115 | EXPECT_EQ(0u, ice_transport_stats.selected_candidate_pair_changes); |
| 5116 | |
| 5117 | // Let the channels connect. |
| 5118 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5119 | kMediumTimeout, clock); |
| 5120 | |
| 5121 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5122 | EXPECT_EQ(1u, ice_transport_stats.selected_candidate_pair_changes); |
| 5123 | |
| 5124 | DestroyChannels(); |
| 5125 | } |
| 5126 | |
| 5127 | TEST_F(P2PTransportChannelTest, |
| 5128 | DisconnectedIncreasesSelectedCandidatePairChanges) { |
| 5129 | rtc::ScopedFakeClock clock; |
| 5130 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 5131 | kDefaultPortAllocatorFlags); |
| 5132 | CreateChannels(); |
| 5133 | |
| 5134 | IceTransportStats ice_transport_stats; |
| 5135 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5136 | EXPECT_EQ(0u, ice_transport_stats.selected_candidate_pair_changes); |
| 5137 | |
| 5138 | // Let the channels connect. |
| 5139 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5140 | kMediumTimeout, clock); |
| 5141 | |
| 5142 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5143 | EXPECT_EQ(1u, ice_transport_stats.selected_candidate_pair_changes); |
| 5144 | |
| 5145 | // Prune connections and wait for disconnect. |
| 5146 | for (Connection* con : ep1_ch1()->connections()) { |
| 5147 | con->Prune(); |
| 5148 | } |
| 5149 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() == nullptr, |
| 5150 | kMediumTimeout, clock); |
| 5151 | |
| 5152 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5153 | EXPECT_EQ(2u, ice_transport_stats.selected_candidate_pair_changes); |
| 5154 | |
| 5155 | DestroyChannels(); |
| 5156 | } |
| 5157 | |
| 5158 | TEST_F(P2PTransportChannelTest, |
| 5159 | NewSelectionIncreasesSelectedCandidatePairChanges) { |
| 5160 | rtc::ScopedFakeClock clock; |
| 5161 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 5162 | kDefaultPortAllocatorFlags); |
| 5163 | CreateChannels(); |
| 5164 | |
| 5165 | IceTransportStats ice_transport_stats; |
| 5166 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5167 | EXPECT_EQ(0u, ice_transport_stats.selected_candidate_pair_changes); |
| 5168 | |
| 5169 | // Let the channels connect. |
| 5170 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5171 | kMediumTimeout, clock); |
| 5172 | |
| 5173 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5174 | EXPECT_EQ(1u, ice_transport_stats.selected_candidate_pair_changes); |
| 5175 | |
| 5176 | // Prune the currently selected connection and wait for selection |
| 5177 | // of a new one. |
| 5178 | const Connection* selected_connection = ep1_ch1()->selected_connection(); |
| 5179 | for (Connection* con : ep1_ch1()->connections()) { |
| 5180 | if (con == selected_connection) { |
| 5181 | con->Prune(); |
| 5182 | } |
| 5183 | } |
| 5184 | EXPECT_TRUE_SIMULATED_WAIT( |
| 5185 | ep1_ch1()->selected_connection() != nullptr && |
| 5186 | (ep1_ch1()->GetStats(&ice_transport_stats), |
| 5187 | ice_transport_stats.selected_candidate_pair_changes >= 2u), |
| 5188 | kMediumTimeout, clock); |
| 5189 | |
| 5190 | ASSERT_TRUE(ep1_ch1()->GetStats(&ice_transport_stats)); |
| 5191 | EXPECT_GE(ice_transport_stats.selected_candidate_pair_changes, 2u); |
| 5192 | |
| 5193 | DestroyChannels(); |
| 5194 | } |
| 5195 | |
Qingsi Wang | e5defb1 | 2019-08-15 11:01:53 -0700 | [diff] [blame] | 5196 | // A similar test as above to check the selected candidate pair is sanitized |
| 5197 | // when it is queried via GetSelectedCandidatePair. |
| 5198 | TEST_F(P2PTransportChannelTest, |
| 5199 | SelectedCandidatePairSanitizedWhenMdnsObfuscationEnabled) { |
| 5200 | NiceMock<rtc::MockAsyncResolver> mock_async_resolver; |
| 5201 | webrtc::MockAsyncResolverFactory mock_async_resolver_factory; |
| 5202 | EXPECT_CALL(mock_async_resolver_factory, Create()) |
| 5203 | .WillOnce(Return(&mock_async_resolver)); |
| 5204 | |
| 5205 | // ep1 and ep2 will gather host candidates with addresses |
| 5206 | // kPublicAddrs[0] and kPublicAddrs[1], respectively. |
| 5207 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 5208 | // ICE parameter will be set up when creating the channels. |
| 5209 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
| 5210 | GetEndpoint(0)->network_manager_.set_mdns_responder( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 5211 | std::make_unique<webrtc::FakeMdnsResponder>(rtc::Thread::Current())); |
Qingsi Wang | e5defb1 | 2019-08-15 11:01:53 -0700 | [diff] [blame] | 5212 | GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; |
| 5213 | CreateChannels(); |
| 5214 | // Pause sending candidates from both endpoints until we find out what port |
| 5215 | // number is assigned to ep1's host candidate. |
| 5216 | PauseCandidates(0); |
| 5217 | PauseCandidates(1); |
| 5218 | ASSERT_EQ_WAIT(1u, GetEndpoint(0)->saved_candidates_.size(), kMediumTimeout); |
| 5219 | const auto& candidates_data = GetEndpoint(0)->saved_candidates_[0]; |
| 5220 | ASSERT_EQ(1u, candidates_data->candidates.size()); |
| 5221 | const auto& local_candidate_ep1 = candidates_data->candidates[0]; |
| 5222 | ASSERT_TRUE(local_candidate_ep1.type() == LOCAL_PORT_TYPE); |
| 5223 | // This is the underlying private IP address of the same candidate at ep1, |
| 5224 | // and let the mock resolver of ep2 receive the correct resolution. |
| 5225 | rtc::SocketAddress resolved_address_ep1(local_candidate_ep1.address()); |
| 5226 | resolved_address_ep1.SetResolvedIP(kPublicAddrs[0].ipaddr()); |
| 5227 | EXPECT_CALL(mock_async_resolver, GetResolvedAddress(_, _)) |
| 5228 | .WillOnce(DoAll(SetArgPointee<1>(resolved_address_ep1), Return(true))); |
| 5229 | ResumeCandidates(0); |
| 5230 | ResumeCandidates(1); |
| 5231 | |
| 5232 | ASSERT_TRUE_WAIT(ep1_ch1()->selected_connection() != nullptr && |
| 5233 | ep2_ch1()->selected_connection() != nullptr, |
| 5234 | kMediumTimeout); |
| 5235 | |
| 5236 | const auto pair_ep1 = ep1_ch1()->GetSelectedCandidatePair(); |
| 5237 | ASSERT_TRUE(pair_ep1.has_value()); |
| 5238 | EXPECT_EQ(LOCAL_PORT_TYPE, pair_ep1->local_candidate().type()); |
| 5239 | EXPECT_TRUE(pair_ep1->local_candidate().address().IsUnresolvedIP()); |
| 5240 | |
| 5241 | const auto pair_ep2 = ep2_ch1()->GetSelectedCandidatePair(); |
| 5242 | ASSERT_TRUE(pair_ep2.has_value()); |
| 5243 | EXPECT_EQ(LOCAL_PORT_TYPE, pair_ep2->remote_candidate().type()); |
| 5244 | EXPECT_TRUE(pair_ep2->remote_candidate().address().IsUnresolvedIP()); |
| 5245 | |
| 5246 | DestroyChannels(); |
| 5247 | } |
| 5248 | |
Qingsi Wang | fdf54f2 | 2019-10-18 15:51:40 -0700 | [diff] [blame] | 5249 | TEST_F(P2PTransportChannelTest, |
| 5250 | NoPairOfLocalRelayCandidateWithRemoteMdnsCandidate) { |
| 5251 | const int kOnlyRelayPorts = cricket::PORTALLOCATOR_DISABLE_UDP | |
| 5252 | cricket::PORTALLOCATOR_DISABLE_STUN | |
| 5253 | cricket::PORTALLOCATOR_DISABLE_TCP; |
| 5254 | // We use one endpoint to test the behavior of adding remote candidates, and |
| 5255 | // this endpoint only gathers relay candidates. |
| 5256 | ConfigureEndpoints(OPEN, OPEN, kOnlyRelayPorts, kDefaultPortAllocatorFlags); |
| 5257 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 5258 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 5259 | IceConfig config; |
| 5260 | // Start gathering and we should have only a single relay port. |
| 5261 | ep1_ch1()->SetIceConfig(config); |
| 5262 | ep1_ch1()->MaybeStartGathering(); |
| 5263 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 5264 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 5265 | EXPECT_EQ(1u, ep1_ch1()->ports().size()); |
| 5266 | // Add a plain remote host candidate and three remote mDNS candidates with the |
| 5267 | // host, srflx and relay types. Note that the candidates differ in their |
| 5268 | // ports. |
| 5269 | cricket::Candidate host_candidate = CreateUdpCandidate( |
| 5270 | LOCAL_PORT_TYPE, "1.1.1.1", 1 /* port */, 0 /* priority */); |
| 5271 | ep1_ch1()->AddRemoteCandidate(host_candidate); |
| 5272 | |
| 5273 | std::vector<cricket::Candidate> mdns_candidates; |
| 5274 | mdns_candidates.push_back(CreateUdpCandidate(LOCAL_PORT_TYPE, "example.local", |
| 5275 | 2 /* port */, 0 /* priority */)); |
| 5276 | mdns_candidates.push_back(CreateUdpCandidate(STUN_PORT_TYPE, "example.local", |
| 5277 | 3 /* port */, 0 /* priority */)); |
| 5278 | mdns_candidates.push_back(CreateUdpCandidate(RELAY_PORT_TYPE, "example.local", |
| 5279 | 4 /* port */, 0 /* priority */)); |
| 5280 | // We just resolve the hostname to 1.1.1.1, and add the candidates with this |
| 5281 | // address directly to simulate the process of adding remote candidates with |
| 5282 | // the name resolution. |
| 5283 | for (auto& mdns_candidate : mdns_candidates) { |
| 5284 | rtc::SocketAddress resolved_address(mdns_candidate.address()); |
| 5285 | resolved_address.SetResolvedIP(0x1111); // 1.1.1.1 |
| 5286 | mdns_candidate.set_address(resolved_address); |
| 5287 | EXPECT_FALSE(mdns_candidate.address().IsUnresolvedIP()); |
| 5288 | ep1_ch1()->AddRemoteCandidate(mdns_candidate); |
| 5289 | } |
| 5290 | |
| 5291 | // All remote candidates should have been successfully added. |
| 5292 | EXPECT_EQ(4u, ep1_ch1()->remote_candidates().size()); |
| 5293 | |
| 5294 | // Expect that there is no connection paired with any mDNS candidate. |
| 5295 | ASSERT_EQ(1u, ep1_ch1()->connections().size()); |
| 5296 | ASSERT_NE(nullptr, ep1_ch1()->connections()[0]); |
| 5297 | EXPECT_EQ( |
| 5298 | "1.1.1.1:1", |
| 5299 | ep1_ch1()->connections()[0]->remote_candidate().address().ToString()); |
| 5300 | } |
| 5301 | |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 5302 | class MockMdnsResponder : public webrtc::MdnsResponderInterface { |
| 5303 | public: |
| 5304 | MOCK_METHOD2(CreateNameForAddress, |
| 5305 | void(const rtc::IPAddress&, NameCreatedCallback)); |
| 5306 | MOCK_METHOD2(RemoveNameForAddress, |
| 5307 | void(const rtc::IPAddress&, NameRemovedCallback)); |
| 5308 | }; |
| 5309 | |
| 5310 | TEST_F(P2PTransportChannelTest, |
| 5311 | SrflxCandidateCanBeGatheredBeforeMdnsCandidateToCreateConnection) { |
| 5312 | // ep1 and ep2 will only gather host and srflx candidates with base addresses |
| 5313 | // kPublicAddrs[0] and kPublicAddrs[1], respectively, and we use a shared |
| 5314 | // socket in gathering. |
| 5315 | const auto kOnlyLocalAndStunPorts = |
| 5316 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 5317 | cricket::PORTALLOCATOR_DISABLE_TCP | |
| 5318 | cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET; |
| 5319 | // ep1 is configured with a NAT so that we do gather a srflx candidate. |
| 5320 | ConfigureEndpoints(NAT_FULL_CONE, OPEN, kOnlyLocalAndStunPorts, |
| 5321 | kOnlyLocalAndStunPorts); |
| 5322 | // ICE parameter will be set up when creating the channels. |
| 5323 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
| 5324 | // Use a mock mDNS responder, which does not complete the name registration by |
| 5325 | // ignoring the completion callback. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 5326 | auto mock_mdns_responder = std::make_unique<MockMdnsResponder>(); |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 5327 | EXPECT_CALL(*mock_mdns_responder, CreateNameForAddress(_, _)) |
| 5328 | .Times(1) |
| 5329 | .WillOnce(Return()); |
| 5330 | GetEndpoint(0)->network_manager_.set_mdns_responder( |
| 5331 | std::move(mock_mdns_responder)); |
| 5332 | |
| 5333 | CreateChannels(); |
| 5334 | |
| 5335 | // We should be able to form a srflx-host connection to ep2. |
| 5336 | ASSERT_TRUE_WAIT((ep1_ch1()->selected_connection()) != nullptr, |
| 5337 | kMediumTimeout); |
| 5338 | EXPECT_EQ(STUN_PORT_TYPE, |
| 5339 | ep1_ch1()->selected_connection()->local_candidate().type()); |
| 5340 | EXPECT_EQ(LOCAL_PORT_TYPE, |
| 5341 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5342 | |
| 5343 | DestroyChannels(); |
| 5344 | } |
| 5345 | |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5346 | // Test that after changing the candidate filter from relay-only to allowing all |
| 5347 | // types of candidates when doing continual gathering, we can gather without ICE |
| 5348 | // restart the other types of candidates that are now enabled and form candidate |
| 5349 | // pairs. Also, we verify that the relay candidates gathered previously are not |
| 5350 | // removed and are still usable for necessary route switching. |
| 5351 | TEST_F(P2PTransportChannelTest, |
| 5352 | SurfaceHostCandidateOnCandidateFilterChangeFromRelayToAll) { |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5353 | rtc::ScopedFakeClock clock; |
| 5354 | |
| 5355 | ConfigureEndpoints( |
| 5356 | OPEN, OPEN, |
| 5357 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 5358 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 5359 | auto* ep1 = GetEndpoint(0); |
| 5360 | auto* ep2 = GetEndpoint(1); |
| 5361 | ep1->allocator_->SetCandidateFilter(CF_RELAY); |
| 5362 | ep2->allocator_->SetCandidateFilter(CF_RELAY); |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5363 | // Enable continual gathering and also resurfacing gathered candidates upon |
| 5364 | // the candidate filter changed in the ICE configuration. |
| 5365 | IceConfig ice_config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 5366 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = true; |
| 5367 | CreateChannels(ice_config, ice_config); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5368 | ASSERT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5369 | kDefaultTimeout, clock); |
| 5370 | ASSERT_TRUE_SIMULATED_WAIT(ep2_ch1()->selected_connection() != nullptr, |
| 5371 | kDefaultTimeout, clock); |
| 5372 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5373 | ep1_ch1()->selected_connection()->local_candidate().type()); |
| 5374 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5375 | ep2_ch1()->selected_connection()->local_candidate().type()); |
| 5376 | |
| 5377 | // Loosen the candidate filter at ep1. |
| 5378 | ep1->allocator_->SetCandidateFilter(CF_ALL); |
| 5379 | EXPECT_TRUE_SIMULATED_WAIT( |
| 5380 | ep1_ch1()->selected_connection() != nullptr && |
| 5381 | ep1_ch1()->selected_connection()->local_candidate().type() == |
| 5382 | LOCAL_PORT_TYPE, |
| 5383 | kDefaultTimeout, clock); |
| 5384 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5385 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5386 | |
| 5387 | // Loosen the candidate filter at ep2. |
| 5388 | ep2->allocator_->SetCandidateFilter(CF_ALL); |
| 5389 | EXPECT_TRUE_SIMULATED_WAIT( |
| 5390 | ep2_ch1()->selected_connection() != nullptr && |
| 5391 | ep2_ch1()->selected_connection()->local_candidate().type() == |
| 5392 | LOCAL_PORT_TYPE, |
| 5393 | kDefaultTimeout, clock); |
| 5394 | // We have migrated to a host-host candidate pair. |
| 5395 | EXPECT_EQ(LOCAL_PORT_TYPE, |
| 5396 | ep2_ch1()->selected_connection()->remote_candidate().type()); |
| 5397 | |
| 5398 | // Block the traffic over non-relay-to-relay routes and expect a route change. |
| 5399 | fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[0], kPublicAddrs[1]); |
| 5400 | fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[1], kPublicAddrs[0]); |
| 5401 | fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[0], kTurnUdpExtAddr); |
| 5402 | fw()->AddRule(false, rtc::FP_ANY, kPublicAddrs[1], kTurnUdpExtAddr); |
| 5403 | // We should be able to reuse the previously gathered relay candidates. |
| 5404 | EXPECT_EQ_SIMULATED_WAIT( |
| 5405 | RELAY_PORT_TYPE, |
| 5406 | ep1_ch1()->selected_connection()->local_candidate().type(), |
| 5407 | kDefaultTimeout, clock); |
| 5408 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5409 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5410 | } |
| 5411 | |
| 5412 | // A similar test as SurfaceHostCandidateOnCandidateFilterChangeFromRelayToAll, |
| 5413 | // and we should surface server-reflexive candidates that are enabled after |
| 5414 | // changing the candidate filter. |
| 5415 | TEST_F(P2PTransportChannelTest, |
| 5416 | SurfaceSrflxCandidateOnCandidateFilterChangeFromRelayToNoHost) { |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5417 | rtc::ScopedFakeClock clock; |
| 5418 | // We need an actual NAT so that the host candidate is not equivalent to the |
| 5419 | // srflx candidate; otherwise, the host candidate would still surface even |
| 5420 | // though we disable it via the candidate filter below. This is a result of |
| 5421 | // the following limitation in the current implementation: |
| 5422 | // 1. We don't generate the srflx candidate when we have public IP. |
| 5423 | // 2. We keep the host candidate in this case in CheckCandidateFilter even |
| 5424 | // though we intend to filter them. |
| 5425 | ConfigureEndpoints( |
| 5426 | NAT_FULL_CONE, NAT_FULL_CONE, |
| 5427 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 5428 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 5429 | auto* ep1 = GetEndpoint(0); |
| 5430 | auto* ep2 = GetEndpoint(1); |
| 5431 | ep1->allocator_->SetCandidateFilter(CF_RELAY); |
| 5432 | ep2->allocator_->SetCandidateFilter(CF_RELAY); |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5433 | // Enable continual gathering and also resurfacing gathered candidates upon |
| 5434 | // the candidate filter changed in the ICE configuration. |
| 5435 | IceConfig ice_config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 5436 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = true; |
| 5437 | CreateChannels(ice_config, ice_config); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5438 | ASSERT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5439 | kDefaultTimeout, clock); |
| 5440 | ASSERT_TRUE_SIMULATED_WAIT(ep2_ch1()->selected_connection() != nullptr, |
| 5441 | kDefaultTimeout, clock); |
| 5442 | const uint32_t kCandidateFilterNoHost = CF_ALL & ~CF_HOST; |
| 5443 | // Loosen the candidate filter at ep1. |
| 5444 | ep1->allocator_->SetCandidateFilter(kCandidateFilterNoHost); |
| 5445 | EXPECT_TRUE_SIMULATED_WAIT( |
| 5446 | ep1_ch1()->selected_connection() != nullptr && |
| 5447 | ep1_ch1()->selected_connection()->local_candidate().type() == |
| 5448 | STUN_PORT_TYPE, |
| 5449 | kDefaultTimeout, clock); |
| 5450 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5451 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5452 | |
| 5453 | // Loosen the candidate filter at ep2. |
| 5454 | ep2->allocator_->SetCandidateFilter(kCandidateFilterNoHost); |
| 5455 | EXPECT_TRUE_SIMULATED_WAIT( |
| 5456 | ep2_ch1()->selected_connection() != nullptr && |
| 5457 | ep2_ch1()->selected_connection()->local_candidate().type() == |
| 5458 | STUN_PORT_TYPE, |
| 5459 | kDefaultTimeout, clock); |
| 5460 | // We have migrated to a srflx-srflx candidate pair. |
| 5461 | EXPECT_EQ(STUN_PORT_TYPE, |
| 5462 | ep2_ch1()->selected_connection()->remote_candidate().type()); |
| 5463 | |
| 5464 | // Block the traffic over non-relay-to-relay routes and expect a route change. |
| 5465 | fw()->AddRule(false, rtc::FP_ANY, kPrivateAddrs[0], kPublicAddrs[1]); |
| 5466 | fw()->AddRule(false, rtc::FP_ANY, kPrivateAddrs[1], kPublicAddrs[0]); |
| 5467 | fw()->AddRule(false, rtc::FP_ANY, kPrivateAddrs[0], kTurnUdpExtAddr); |
| 5468 | fw()->AddRule(false, rtc::FP_ANY, kPrivateAddrs[1], kTurnUdpExtAddr); |
| 5469 | // We should be able to reuse the previously gathered relay candidates. |
| 5470 | EXPECT_EQ_SIMULATED_WAIT( |
| 5471 | RELAY_PORT_TYPE, |
| 5472 | ep1_ch1()->selected_connection()->local_candidate().type(), |
| 5473 | kDefaultTimeout, clock); |
| 5474 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5475 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5476 | } |
| 5477 | |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5478 | // This is the complement to |
| 5479 | // SurfaceHostCandidateOnCandidateFilterChangeFromRelayToAll, and instead of |
| 5480 | // gathering continually we only gather once, which makes the config |
| 5481 | // |surface_ice_candidates_on_ice_transport_type_changed| ineffective after the |
| 5482 | // gathering stopped. |
| 5483 | TEST_F(P2PTransportChannelTest, |
| 5484 | CannotSurfaceTheNewlyAllowedOnFilterChangeIfNotGatheringContinually) { |
| 5485 | rtc::ScopedFakeClock clock; |
| 5486 | |
| 5487 | ConfigureEndpoints( |
| 5488 | OPEN, OPEN, |
| 5489 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 5490 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 5491 | auto* ep1 = GetEndpoint(0); |
| 5492 | auto* ep2 = GetEndpoint(1); |
| 5493 | ep1->allocator_->SetCandidateFilter(CF_RELAY); |
| 5494 | ep2->allocator_->SetCandidateFilter(CF_RELAY); |
| 5495 | // Only gather once. |
| 5496 | IceConfig ice_config = CreateIceConfig(1000, GATHER_ONCE); |
| 5497 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = true; |
| 5498 | CreateChannels(ice_config, ice_config); |
| 5499 | ASSERT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5500 | kDefaultTimeout, clock); |
| 5501 | ASSERT_TRUE_SIMULATED_WAIT(ep2_ch1()->selected_connection() != nullptr, |
| 5502 | kDefaultTimeout, clock); |
| 5503 | // Loosen the candidate filter at ep1. |
| 5504 | ep1->allocator_->SetCandidateFilter(CF_ALL); |
| 5505 | // Wait for a period for any potential surfacing of new candidates. |
| 5506 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 5507 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5508 | ep1_ch1()->selected_connection()->local_candidate().type()); |
| 5509 | |
| 5510 | // Loosen the candidate filter at ep2. |
| 5511 | ep2->allocator_->SetCandidateFilter(CF_ALL); |
| 5512 | EXPECT_EQ(RELAY_PORT_TYPE, |
| 5513 | ep2_ch1()->selected_connection()->local_candidate().type()); |
| 5514 | } |
| 5515 | |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5516 | // Test that when the candidate filter is updated to be more restrictive, |
| 5517 | // candidates that 1) have already been gathered and signaled 2) but no longer |
| 5518 | // match the filter, are not removed. |
| 5519 | TEST_F(P2PTransportChannelTest, |
| 5520 | RestrictingCandidateFilterDoesNotRemoveRegatheredCandidates) { |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5521 | rtc::ScopedFakeClock clock; |
| 5522 | |
| 5523 | ConfigureEndpoints( |
| 5524 | OPEN, OPEN, |
| 5525 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 5526 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
| 5527 | auto* ep1 = GetEndpoint(0); |
| 5528 | auto* ep2 = GetEndpoint(1); |
| 5529 | ep1->allocator_->SetCandidateFilter(CF_ALL); |
| 5530 | ep2->allocator_->SetCandidateFilter(CF_ALL); |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5531 | // Enable continual gathering and also resurfacing gathered candidates upon |
| 5532 | // the candidate filter changed in the ICE configuration. |
| 5533 | IceConfig ice_config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 5534 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = true; |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5535 | // Pause candidates so we can gather all types of candidates. See |
| 5536 | // P2PTransportChannel::OnConnectionStateChange, where we would stop the |
| 5537 | // gathering when we have a strongly connected candidate pair. |
| 5538 | PauseCandidates(0); |
| 5539 | PauseCandidates(1); |
Qingsi Wang | 1fe119f | 2019-05-31 16:55:33 -0700 | [diff] [blame] | 5540 | CreateChannels(ice_config, ice_config); |
Qingsi Wang | c129c35 | 2019-04-18 10:41:58 -0700 | [diff] [blame] | 5541 | |
| 5542 | // We have gathered host, srflx and relay candidates. |
| 5543 | EXPECT_TRUE_SIMULATED_WAIT(ep1->saved_candidates_.size() == 3u, |
| 5544 | kDefaultTimeout, clock); |
| 5545 | ResumeCandidates(0); |
| 5546 | ResumeCandidates(1); |
| 5547 | ASSERT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection() != nullptr, |
| 5548 | kDefaultTimeout, clock); |
| 5549 | ASSERT_TRUE_SIMULATED_WAIT(ep2_ch1()->selected_connection() != nullptr, |
| 5550 | kDefaultTimeout, clock); |
| 5551 | // Test that we have a host-host candidate pair selected and the number of |
| 5552 | // candidates signaled to the remote peer stays the same. |
| 5553 | auto test_invariants = [this]() { |
| 5554 | EXPECT_EQ(LOCAL_PORT_TYPE, |
| 5555 | ep1_ch1()->selected_connection()->local_candidate().type()); |
| 5556 | EXPECT_EQ(LOCAL_PORT_TYPE, |
| 5557 | ep1_ch1()->selected_connection()->remote_candidate().type()); |
| 5558 | EXPECT_THAT(ep2_ch1()->remote_candidates(), SizeIs(3)); |
| 5559 | }; |
| 5560 | |
| 5561 | test_invariants(); |
| 5562 | |
| 5563 | // Set a more restrictive candidate filter at ep1. |
| 5564 | ep1->allocator_->SetCandidateFilter(CF_HOST | CF_REFLEXIVE); |
| 5565 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 5566 | test_invariants(); |
| 5567 | |
| 5568 | ep1->allocator_->SetCandidateFilter(CF_HOST); |
| 5569 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 5570 | test_invariants(); |
| 5571 | |
| 5572 | ep1->allocator_->SetCandidateFilter(CF_NONE); |
| 5573 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 5574 | test_invariants(); |
| 5575 | } |
| 5576 | |
Jonas Oreland | 1230fb7 | 2019-10-25 15:58:14 +0200 | [diff] [blame] | 5577 | TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampening0) { |
| 5578 | webrtc::test::ScopedFieldTrials field_trials( |
| 5579 | "WebRTC-IceFieldTrials/initial_select_dampening:0/"); |
| 5580 | |
| 5581 | constexpr int kMargin = 10; |
| 5582 | rtc::ScopedFakeClock clock; |
| 5583 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
| 5584 | |
| 5585 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 5586 | P2PTransportChannel ch("test channel", 1, &pa); |
| 5587 | PrepareChannel(&ch); |
| 5588 | ch.SetIceConfig(ch.config()); |
| 5589 | ch.MaybeStartGathering(); |
| 5590 | |
| 5591 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 5592 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
| 5593 | ASSERT_TRUE(conn1 != nullptr); |
| 5594 | EXPECT_EQ(nullptr, ch.selected_connection()); |
| 5595 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 5596 | // It shall not be selected until 0ms has passed....i.e it should be connected |
| 5597 | // directly. |
| 5598 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kMargin, clock); |
| 5599 | } |
| 5600 | |
| 5601 | TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampening) { |
| 5602 | webrtc::test::ScopedFieldTrials field_trials( |
| 5603 | "WebRTC-IceFieldTrials/initial_select_dampening:100/"); |
| 5604 | |
| 5605 | constexpr int kMargin = 10; |
| 5606 | rtc::ScopedFakeClock clock; |
| 5607 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
| 5608 | |
| 5609 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 5610 | P2PTransportChannel ch("test channel", 1, &pa); |
| 5611 | PrepareChannel(&ch); |
| 5612 | ch.SetIceConfig(ch.config()); |
| 5613 | ch.MaybeStartGathering(); |
| 5614 | |
| 5615 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 5616 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
| 5617 | ASSERT_TRUE(conn1 != nullptr); |
| 5618 | EXPECT_EQ(nullptr, ch.selected_connection()); |
| 5619 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 5620 | // It shall not be selected until 100ms has passed. |
| 5621 | SIMULATED_WAIT(conn1 == ch.selected_connection(), 100 - kMargin, clock); |
| 5622 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), 2 * kMargin, clock); |
| 5623 | } |
| 5624 | |
| 5625 | TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampeningPingReceived) { |
| 5626 | webrtc::test::ScopedFieldTrials field_trials( |
| 5627 | "WebRTC-IceFieldTrials/initial_select_dampening_ping_received:100/"); |
| 5628 | |
| 5629 | constexpr int kMargin = 10; |
| 5630 | rtc::ScopedFakeClock clock; |
| 5631 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
| 5632 | |
| 5633 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 5634 | P2PTransportChannel ch("test channel", 1, &pa); |
| 5635 | PrepareChannel(&ch); |
| 5636 | ch.SetIceConfig(ch.config()); |
| 5637 | ch.MaybeStartGathering(); |
| 5638 | |
| 5639 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 5640 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
| 5641 | ASSERT_TRUE(conn1 != nullptr); |
| 5642 | EXPECT_EQ(nullptr, ch.selected_connection()); |
| 5643 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 5644 | conn1->ReceivedPing("id1"); // |
| 5645 | // It shall not be selected until 100ms has passed. |
| 5646 | SIMULATED_WAIT(conn1 == ch.selected_connection(), 100 - kMargin, clock); |
| 5647 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), 2 * kMargin, clock); |
| 5648 | } |
| 5649 | |
| 5650 | TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampeningBoth) { |
| 5651 | webrtc::test::ScopedFieldTrials field_trials( |
| 5652 | "WebRTC-IceFieldTrials/" |
| 5653 | "initial_select_dampening:100,initial_select_dampening_ping_received:" |
| 5654 | "50/"); |
| 5655 | |
| 5656 | constexpr int kMargin = 10; |
| 5657 | rtc::ScopedFakeClock clock; |
| 5658 | clock.AdvanceTime(webrtc::TimeDelta::seconds(1)); |
| 5659 | |
| 5660 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 5661 | P2PTransportChannel ch("test channel", 1, &pa); |
| 5662 | PrepareChannel(&ch); |
| 5663 | ch.SetIceConfig(ch.config()); |
| 5664 | ch.MaybeStartGathering(); |
| 5665 | |
| 5666 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 5667 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
| 5668 | ASSERT_TRUE(conn1 != nullptr); |
| 5669 | EXPECT_EQ(nullptr, ch.selected_connection()); |
| 5670 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 5671 | // It shall not be selected until 100ms has passed....but only wait ~50 now. |
| 5672 | SIMULATED_WAIT(conn1 == ch.selected_connection(), 50 - kMargin, clock); |
| 5673 | // Now receiving ping and new timeout should kick in. |
| 5674 | conn1->ReceivedPing("id1"); // |
| 5675 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), 2 * kMargin, clock); |
| 5676 | } |
| 5677 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame] | 5678 | } // namespace cricket |