henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 12 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/fakemetricsobserver.h" |
| 16 | #include "p2p/base/fakeportallocator.h" |
| 17 | #include "p2p/base/icetransportinternal.h" |
| 18 | #include "p2p/base/p2ptransportchannel.h" |
| 19 | #include "p2p/base/packettransportinternal.h" |
| 20 | #include "p2p/base/testrelayserver.h" |
| 21 | #include "p2p/base/teststunserver.h" |
| 22 | #include "p2p/base/testturnserver.h" |
| 23 | #include "p2p/client/basicportallocator.h" |
| 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/dscp.h" |
| 26 | #include "rtc_base/fakeclock.h" |
| 27 | #include "rtc_base/fakenetwork.h" |
| 28 | #include "rtc_base/firewallsocketserver.h" |
| 29 | #include "rtc_base/gunit.h" |
| 30 | #include "rtc_base/helpers.h" |
| 31 | #include "rtc_base/logging.h" |
| 32 | #include "rtc_base/natserver.h" |
| 33 | #include "rtc_base/natsocketfactory.h" |
| 34 | #include "rtc_base/proxyserver.h" |
| 35 | #include "rtc_base/ptr_util.h" |
| 36 | #include "rtc_base/socketaddress.h" |
| 37 | #include "rtc_base/ssladapter.h" |
| 38 | #include "rtc_base/thread.h" |
| 39 | #include "rtc_base/virtualsocketserver.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 40 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 41 | namespace { |
| 42 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | using rtc::SocketAddress; |
| 44 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 45 | // Default timeout for tests in this file. |
| 46 | // Should be large enough for slow buildbots to run the tests reliably. |
| 47 | static const int kDefaultTimeout = 10000; |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 48 | static const int kMediumTimeout = 3000; |
| 49 | static const int kShortTimeout = 1000; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 50 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | static const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN | |
| 52 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 53 | cricket::PORTALLOCATOR_DISABLE_TCP; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 54 | static const int LOW_RTT = 20; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 55 | // Addresses on the public internet. |
| 56 | static const SocketAddress kPublicAddrs[2] = |
| 57 | { SocketAddress("11.11.11.11", 0), SocketAddress("22.22.22.22", 0) }; |
| 58 | // IPv6 Addresses on the public internet. |
| 59 | static const SocketAddress kIPv6PublicAddrs[2] = { |
| 60 | SocketAddress("2400:4030:1:2c00:be30:abcd:efab:cdef", 0), |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 61 | SocketAddress("2600:0:1000:1b03:2e41:38ff:fea6:f2a4", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | // For configuring multihomed clients. |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 63 | static const SocketAddress kAlternateAddrs[2] = { |
| 64 | SocketAddress("101.101.101.101", 0), SocketAddress("202.202.202.202", 0)}; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 65 | static const SocketAddress kIPv6AlternateAddrs[2] = { |
| 66 | SocketAddress("2401:4030:1:2c00:be30:abcd:efab:cdef", 0), |
| 67 | SocketAddress("2601:0:1000:1b03:2e41:38ff:fea6:f2a4", 0)}; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 68 | // Addresses for HTTP proxy servers. |
| 69 | static const SocketAddress kHttpsProxyAddrs[2] = |
| 70 | { SocketAddress("11.11.11.1", 443), SocketAddress("22.22.22.1", 443) }; |
| 71 | // Addresses for SOCKS proxy servers. |
| 72 | static const SocketAddress kSocksProxyAddrs[2] = |
| 73 | { 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] | 74 | // Internal addresses for NAT boxes. |
| 75 | static const SocketAddress kNatAddrs[2] = |
| 76 | { SocketAddress("192.168.1.1", 0), SocketAddress("192.168.2.1", 0) }; |
| 77 | // Private addresses inside the NAT private networks. |
| 78 | static const SocketAddress kPrivateAddrs[2] = |
| 79 | { SocketAddress("192.168.1.11", 0), SocketAddress("192.168.2.22", 0) }; |
| 80 | // For cascaded NATs, the internal addresses of the inner NAT boxes. |
| 81 | static const SocketAddress kCascadedNatAddrs[2] = |
| 82 | { SocketAddress("192.168.10.1", 0), SocketAddress("192.168.20.1", 0) }; |
| 83 | // For cascaded NATs, private addresses inside the inner private networks. |
| 84 | static const SocketAddress kCascadedPrivateAddrs[2] = |
| 85 | { SocketAddress("192.168.10.11", 0), SocketAddress("192.168.20.22", 0) }; |
| 86 | // The address of the public STUN server. |
| 87 | 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] | 88 | // The addresses for the public turn server. |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 89 | static const SocketAddress kTurnUdpIntAddr("99.99.99.3", |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 90 | cricket::STUN_SERVER_PORT); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 91 | static const SocketAddress kTurnTcpIntAddr("99.99.99.4", |
| 92 | cricket::STUN_SERVER_PORT + 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); |
| 94 | static const cricket::RelayCredentials kRelayCredentials("test", "test"); |
| 95 | |
| 96 | // Based on ICE_UFRAG_LENGTH |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 97 | const char* kIceUfrag[4] = {"UF00", "UF01", "UF02", "UF03"}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 98 | // Based on ICE_PWD_LENGTH |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 99 | const char* kIcePwd[4] = { |
| 100 | "TESTICEPWD00000000000000", "TESTICEPWD00000000000001", |
| 101 | "TESTICEPWD00000000000002", "TESTICEPWD00000000000003"}; |
| 102 | const cricket::IceParameters kIceParams[4] = { |
| 103 | {kIceUfrag[0], kIcePwd[0], false}, |
| 104 | {kIceUfrag[1], kIcePwd[1], false}, |
| 105 | {kIceUfrag[2], kIcePwd[2], false}, |
| 106 | {kIceUfrag[3], kIcePwd[3], false}}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 108 | const uint64_t kLowTiebreaker = 11111; |
| 109 | const uint64_t kHighTiebreaker = 22222; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 111 | enum { MSG_ADD_CANDIDATES, MSG_REMOVE_CANDIDATES }; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 112 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 113 | cricket::IceConfig CreateIceConfig( |
| 114 | int receiving_timeout, |
| 115 | cricket::ContinualGatheringPolicy continual_gathering_policy, |
| 116 | int backup_ping_interval = -1) { |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 117 | cricket::IceConfig config; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 118 | config.receiving_timeout = receiving_timeout; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 119 | config.continual_gathering_policy = continual_gathering_policy; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 120 | config.backup_connection_ping_interval = backup_ping_interval; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 121 | return config; |
| 122 | } |
| 123 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 124 | cricket::Candidate CreateUdpCandidate(const std::string& type, |
| 125 | const std::string& ip, |
| 126 | int port, |
| 127 | int priority, |
| 128 | const std::string& ufrag = "") { |
| 129 | cricket::Candidate c; |
| 130 | c.set_address(rtc::SocketAddress(ip, port)); |
| 131 | c.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 132 | c.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 133 | c.set_priority(priority); |
| 134 | c.set_username(ufrag); |
| 135 | c.set_type(type); |
| 136 | return c; |
| 137 | } |
| 138 | |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 139 | cricket::BasicPortAllocator* CreateBasicPortAllocator( |
| 140 | rtc::NetworkManager* network_manager, |
| 141 | const cricket::ServerAddresses& stun_servers, |
| 142 | const rtc::SocketAddress& turn_server_udp, |
| 143 | const rtc::SocketAddress& turn_server_tcp) { |
| 144 | cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); |
| 145 | turn_server.credentials = kRelayCredentials; |
| 146 | if (!turn_server_udp.IsNil()) { |
| 147 | turn_server.ports.push_back( |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 148 | cricket::ProtocolAddress(turn_server_udp, cricket::PROTO_UDP)); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 149 | } |
| 150 | if (!turn_server_tcp.IsNil()) { |
| 151 | turn_server.ports.push_back( |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 152 | cricket::ProtocolAddress(turn_server_tcp, cricket::PROTO_TCP)); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 153 | } |
| 154 | std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server); |
| 155 | |
| 156 | cricket::BasicPortAllocator* allocator = |
| 157 | new cricket::BasicPortAllocator(network_manager); |
| 158 | allocator->SetConfiguration(stun_servers, turn_servers, 0, false); |
| 159 | return allocator; |
| 160 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 161 | } // namespace |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 162 | |
| 163 | namespace cricket { |
| 164 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 165 | // This test simulates 2 P2P endpoints that want to establish connectivity |
| 166 | // with each other over various network topologies and conditions, which can be |
| 167 | // specified in each individial test. |
| 168 | // A virtual network (via VirtualSocketServer) along with virtual firewalls and |
| 169 | // NATs (via Firewall/NATSocketServer) are used to simulate the various network |
| 170 | // conditions. We can configure the IP addresses of the endpoints, |
| 171 | // block various types of connectivity, or add arbitrary levels of NAT. |
| 172 | // We also run a STUN server and a relay server on the virtual network to allow |
| 173 | // our typical P2P mechanisms to do their thing. |
| 174 | // For each case, we expect the P2P stack to eventually settle on a specific |
| 175 | // form of connectivity to the other side. The test checks that the P2P |
| 176 | // negotiation successfully establishes connectivity within a certain time, |
| 177 | // and that the result is what we expect. |
| 178 | // Note that this class is a base class for use by other tests, who will provide |
| 179 | // specialized test behavior. |
| 180 | class P2PTransportChannelTestBase : public testing::Test, |
| 181 | public rtc::MessageHandler, |
| 182 | public sigslot::has_slots<> { |
| 183 | public: |
| 184 | P2PTransportChannelTestBase() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 185 | : vss_(new rtc::VirtualSocketServer()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 186 | nss_(new rtc::NATSocketServer(vss_.get())), |
| 187 | ss_(new rtc::FirewallSocketServer(nss_.get())), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 188 | main_(ss_.get()), |
| 189 | stun_server_(TestStunServer::Create(&main_, kStunAddr)), |
| 190 | turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 191 | socks_server1_(ss_.get(), |
| 192 | kSocksProxyAddrs[0], |
| 193 | ss_.get(), |
| 194 | kSocksProxyAddrs[0]), |
| 195 | socks_server2_(ss_.get(), |
| 196 | kSocksProxyAddrs[1], |
| 197 | ss_.get(), |
| 198 | kSocksProxyAddrs[1]), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | force_relay_(false) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 200 | ep1_.role_ = ICEROLE_CONTROLLING; |
| 201 | ep2_.role_ = ICEROLE_CONTROLLED; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 202 | |
| 203 | ServerAddresses stun_servers; |
| 204 | stun_servers.insert(kStunAddr); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 205 | ep1_.allocator_.reset( |
| 206 | CreateBasicPortAllocator(&ep1_.network_manager_, stun_servers, |
| 207 | kTurnUdpIntAddr, rtc::SocketAddress())); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 208 | ep1_.metrics_observer_ = |
| 209 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 210 | ep1_.allocator_->SetMetricsObserver(ep1_.metrics_observer_); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 211 | ep2_.allocator_.reset( |
| 212 | CreateBasicPortAllocator(&ep2_.network_manager_, stun_servers, |
| 213 | kTurnUdpIntAddr, rtc::SocketAddress())); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 214 | ep2_.metrics_observer_ = |
| 215 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 216 | ep2_.allocator_->SetMetricsObserver(ep2_.metrics_observer_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | protected: |
| 220 | enum Config { |
| 221 | OPEN, // Open to the Internet |
| 222 | NAT_FULL_CONE, // NAT, no filtering |
| 223 | NAT_ADDR_RESTRICTED, // NAT, must send to an addr to recv |
| 224 | NAT_PORT_RESTRICTED, // NAT, must send to an addr+port to recv |
| 225 | NAT_SYMMETRIC, // NAT, endpoint-dependent bindings |
| 226 | NAT_DOUBLE_CONE, // Double NAT, both cone |
| 227 | NAT_SYMMETRIC_THEN_CONE, // Double NAT, symmetric outer, cone inner |
| 228 | BLOCK_UDP, // Firewall, UDP in/out blocked |
| 229 | BLOCK_UDP_AND_INCOMING_TCP, // Firewall, UDP in/out and TCP in blocked |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 230 | BLOCK_ALL_BUT_OUTGOING_HTTP, // Firewall, only TCP out on 80/443 |
| 231 | PROXY_HTTPS, // All traffic through HTTPS proxy |
| 232 | PROXY_SOCKS, // All traffic through SOCKS proxy |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 233 | NUM_CONFIGS |
| 234 | }; |
| 235 | |
| 236 | struct Result { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 237 | Result(const std::string& controlling_type, |
| 238 | const std::string& controlling_protocol, |
| 239 | const std::string& controlled_type, |
| 240 | const std::string& controlled_protocol, |
| 241 | int wait) |
| 242 | : controlling_type(controlling_type), |
| 243 | controlling_protocol(controlling_protocol), |
| 244 | controlled_type(controlled_type), |
| 245 | controlled_protocol(controlled_protocol), |
| 246 | connect_wait(wait) {} |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 247 | |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 248 | // The expected candidate type and protocol of the controlling ICE agent. |
| 249 | std::string controlling_type; |
| 250 | std::string controlling_protocol; |
| 251 | // The expected candidate type and protocol of the controlled ICE agent. |
| 252 | std::string controlled_type; |
| 253 | std::string controlled_protocol; |
| 254 | // How long to wait before the correct candidate pair is selected. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 255 | int connect_wait; |
| 256 | }; |
| 257 | |
| 258 | struct ChannelData { |
| 259 | bool CheckData(const char* data, int len) { |
| 260 | bool ret = false; |
| 261 | if (!ch_packets_.empty()) { |
| 262 | std::string packet = ch_packets_.front(); |
| 263 | ret = (packet == std::string(data, len)); |
| 264 | ch_packets_.pop_front(); |
| 265 | } |
| 266 | return ret; |
| 267 | } |
| 268 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 269 | std::string name_; // TODO(?) - Currently not used. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 270 | std::list<std::string> ch_packets_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 271 | std::unique_ptr<P2PTransportChannel> ch_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 274 | struct CandidatesData : public rtc::MessageData { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 275 | CandidatesData(IceTransportInternal* ch, const Candidate& c) |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 276 | : channel(ch), candidates(1, c) {} |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 277 | CandidatesData(IceTransportInternal* ch, const std::vector<Candidate>& cc) |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 278 | : channel(ch), candidates(cc) {} |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 279 | IceTransportInternal* channel; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 280 | Candidates candidates; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 283 | struct Endpoint { |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 284 | Endpoint() |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 285 | : role_(ICEROLE_UNKNOWN), |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 286 | tiebreaker_(0), |
| 287 | role_conflict_(false), |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 288 | save_candidates_(false) {} |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 289 | bool HasTransport(const rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 290 | return (transport == cd1_.ch_.get() || transport == cd2_.ch_.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 291 | } |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 292 | ChannelData* GetChannelData(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 293 | if (!HasTransport(transport)) |
| 294 | return NULL; |
| 295 | if (cd1_.ch_.get() == transport) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 296 | return &cd1_; |
| 297 | else |
| 298 | return &cd2_; |
| 299 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 300 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 301 | void SetIceRole(IceRole role) { role_ = role; } |
| 302 | IceRole ice_role() { return role_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 303 | void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; } |
| 304 | uint64_t GetIceTiebreaker() { return tiebreaker_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 305 | void OnRoleConflict(bool role_conflict) { role_conflict_ = role_conflict; } |
| 306 | bool role_conflict() { return role_conflict_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 307 | void SetAllocationStepDelay(uint32_t delay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 308 | allocator_->set_step_delay(delay); |
| 309 | } |
| 310 | void SetAllowTcpListen(bool allow_tcp_listen) { |
| 311 | allocator_->set_allow_tcp_listen(allow_tcp_listen); |
| 312 | } |
| 313 | |
| 314 | rtc::FakeNetworkManager network_manager_; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 315 | // |metrics_observer_| should outlive |allocator_| as the former may be |
| 316 | // used by the latter. |
| 317 | rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 318 | std::unique_ptr<BasicPortAllocator> allocator_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 319 | ChannelData cd1_; |
| 320 | ChannelData cd2_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 321 | IceRole role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 322 | uint64_t tiebreaker_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 323 | bool role_conflict_; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 324 | bool save_candidates_; |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 325 | std::vector<std::unique_ptr<CandidatesData>> saved_candidates_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 326 | bool ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 329 | ChannelData* GetChannelData(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 330 | if (ep1_.HasTransport(transport)) |
| 331 | return ep1_.GetChannelData(transport); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | else |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 333 | return ep2_.GetChannelData(transport); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 336 | IceParameters IceParamsWithRenomination(const IceParameters& ice, |
| 337 | bool renomination) { |
| 338 | IceParameters new_ice = ice; |
| 339 | new_ice.renomination = renomination; |
| 340 | return new_ice; |
| 341 | } |
| 342 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 343 | void CreateChannels(const IceConfig& ep1_config, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 344 | const IceConfig& ep2_config, |
| 345 | bool renomination = false) { |
| 346 | IceParameters ice_ep1_cd1_ch = |
| 347 | IceParamsWithRenomination(kIceParams[0], renomination); |
| 348 | IceParameters ice_ep2_cd1_ch = |
| 349 | IceParamsWithRenomination(kIceParams[1], renomination); |
| 350 | ep1_.cd1_.ch_.reset(CreateChannel(0, ICE_CANDIDATE_COMPONENT_DEFAULT, |
| 351 | ice_ep1_cd1_ch, ice_ep2_cd1_ch)); |
| 352 | ep2_.cd1_.ch_.reset(CreateChannel(1, ICE_CANDIDATE_COMPONENT_DEFAULT, |
| 353 | ice_ep2_cd1_ch, ice_ep1_cd1_ch)); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 354 | ep1_.cd1_.ch_->SetMetricsObserver(ep1_.metrics_observer_); |
| 355 | ep2_.cd1_.ch_->SetMetricsObserver(ep2_.metrics_observer_); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 356 | ep1_.cd1_.ch_->SetIceConfig(ep1_config); |
| 357 | ep2_.cd1_.ch_->SetIceConfig(ep2_config); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 358 | ep1_.cd1_.ch_->MaybeStartGathering(); |
| 359 | ep2_.cd1_.ch_->MaybeStartGathering(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 360 | } |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 361 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 362 | void CreateChannels() { |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 363 | IceConfig default_config; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 364 | CreateChannels(default_config, default_config, false); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 365 | } |
| 366 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 367 | P2PTransportChannel* CreateChannel(int endpoint, |
| 368 | int component, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 369 | const IceParameters& local_ice, |
| 370 | const IceParameters& remote_ice) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 371 | P2PTransportChannel* channel = new P2PTransportChannel( |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 372 | "test content name", component, GetAllocator(endpoint)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 373 | channel->SignalReadyToSend.connect( |
| 374 | this, &P2PTransportChannelTestBase::OnReadyToSend); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 375 | channel->SignalCandidateGathered.connect( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 376 | this, &P2PTransportChannelTestBase::OnCandidateGathered); |
| 377 | channel->SignalCandidatesRemoved.connect( |
| 378 | this, &P2PTransportChannelTestBase::OnCandidatesRemoved); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 379 | channel->SignalReadPacket.connect( |
| 380 | this, &P2PTransportChannelTestBase::OnReadPacket); |
| 381 | channel->SignalRoleConflict.connect( |
| 382 | this, &P2PTransportChannelTestBase::OnRoleConflict); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 383 | channel->SignalNetworkRouteChanged.connect( |
| 384 | this, &P2PTransportChannelTestBase::OnNetworkRouteChanged); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 385 | channel->SetIceParameters(local_ice); |
| 386 | if (remote_ice_parameter_source_ == FROM_SETICEPARAMETERS) { |
| 387 | channel->SetRemoteIceParameters(remote_ice); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 388 | } |
| 389 | channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); |
| 390 | channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 391 | return channel; |
| 392 | } |
| 393 | void DestroyChannels() { |
| 394 | ep1_.cd1_.ch_.reset(); |
| 395 | ep2_.cd1_.ch_.reset(); |
| 396 | ep1_.cd2_.ch_.reset(); |
| 397 | ep2_.cd2_.ch_.reset(); |
| 398 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 399 | P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } |
| 400 | P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } |
| 401 | P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); } |
| 402 | P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 403 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 404 | TestTurnServer* test_turn_server() { return &turn_server_; } |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 405 | rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); } |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 406 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 407 | // Common results. |
| 408 | static const Result kLocalUdpToLocalUdp; |
| 409 | static const Result kLocalUdpToStunUdp; |
| 410 | static const Result kLocalUdpToPrflxUdp; |
| 411 | static const Result kPrflxUdpToLocalUdp; |
| 412 | static const Result kStunUdpToLocalUdp; |
| 413 | static const Result kStunUdpToStunUdp; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 414 | static const Result kStunUdpToPrflxUdp; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 415 | static const Result kPrflxUdpToStunUdp; |
| 416 | static const Result kLocalUdpToRelayUdp; |
| 417 | static const Result kPrflxUdpToRelayUdp; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 418 | static const Result kRelayUdpToPrflxUdp; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 419 | static const Result kLocalTcpToLocalTcp; |
| 420 | static const Result kLocalTcpToPrflxTcp; |
| 421 | static const Result kPrflxTcpToLocalTcp; |
| 422 | |
| 423 | rtc::NATSocketServer* nat() { return nss_.get(); } |
| 424 | rtc::FirewallSocketServer* fw() { return ss_.get(); } |
| 425 | |
| 426 | Endpoint* GetEndpoint(int endpoint) { |
| 427 | if (endpoint == 0) { |
| 428 | return &ep1_; |
| 429 | } else if (endpoint == 1) { |
| 430 | return &ep2_; |
| 431 | } else { |
| 432 | return NULL; |
| 433 | } |
| 434 | } |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 435 | BasicPortAllocator* GetAllocator(int endpoint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 436 | return GetEndpoint(endpoint)->allocator_.get(); |
| 437 | } |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 438 | webrtc::FakeMetricsObserver* GetMetricsObserver(int endpoint) { |
| 439 | return GetEndpoint(endpoint)->metrics_observer_; |
| 440 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 441 | void AddAddress(int endpoint, const SocketAddress& addr) { |
| 442 | GetEndpoint(endpoint)->network_manager_.AddInterface(addr); |
| 443 | } |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 444 | void AddAddress(int endpoint, |
| 445 | const SocketAddress& addr, |
| 446 | const std::string& ifname, |
| 447 | rtc::AdapterType adapter_type) { |
| 448 | GetEndpoint(endpoint)->network_manager_.AddInterface(addr, ifname, |
| 449 | adapter_type); |
| 450 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 451 | void RemoveAddress(int endpoint, const SocketAddress& addr) { |
| 452 | GetEndpoint(endpoint)->network_manager_.RemoveInterface(addr); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 453 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 454 | } |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 455 | void SetProxy(int endpoint, rtc::ProxyType type) { |
| 456 | rtc::ProxyInfo info; |
| 457 | info.type = type; |
| 458 | info.address = (type == rtc::PROXY_HTTPS) ? |
| 459 | kHttpsProxyAddrs[endpoint] : kSocksProxyAddrs[endpoint]; |
| 460 | GetAllocator(endpoint)->set_proxy("unittest/1.0", info); |
| 461 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 462 | void SetAllocatorFlags(int endpoint, int flags) { |
| 463 | GetAllocator(endpoint)->set_flags(flags); |
| 464 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 465 | void SetIceRole(int endpoint, IceRole role) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 466 | GetEndpoint(endpoint)->SetIceRole(role); |
| 467 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 468 | void SetIceTiebreaker(int endpoint, uint64_t tiebreaker) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 469 | GetEndpoint(endpoint)->SetIceTiebreaker(tiebreaker); |
| 470 | } |
| 471 | bool GetRoleConflict(int endpoint) { |
| 472 | return GetEndpoint(endpoint)->role_conflict(); |
| 473 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 474 | void SetAllocationStepDelay(int endpoint, uint32_t delay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 475 | return GetEndpoint(endpoint)->SetAllocationStepDelay(delay); |
| 476 | } |
| 477 | void SetAllowTcpListen(int endpoint, bool allow_tcp_listen) { |
| 478 | return GetEndpoint(endpoint)->SetAllowTcpListen(allow_tcp_listen); |
| 479 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 480 | |
| 481 | // Return true if the approprite parts of the expected Result, based |
| 482 | // on the local and remote candidate of ep1_ch1, match. This can be |
| 483 | // used in an EXPECT_TRUE_WAIT. |
| 484 | bool CheckCandidate1(const Result& expected) { |
| 485 | const std::string& local_type = LocalCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 486 | const std::string& local_protocol = LocalCandidate(ep1_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 487 | const std::string& remote_type = RemoteCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 488 | const std::string& remote_protocol = RemoteCandidate(ep1_ch1())->protocol(); |
| 489 | return (local_protocol == expected.controlling_protocol && |
| 490 | remote_protocol == expected.controlled_protocol && |
| 491 | local_type == expected.controlling_type && |
| 492 | remote_type == expected.controlled_type); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // EXPECT_EQ on the approprite parts of the expected Result, based |
| 496 | // on the local and remote candidate of ep1_ch1. This is like |
| 497 | // CheckCandidate1, except that it will provide more detail about |
| 498 | // what didn't match. |
| 499 | void ExpectCandidate1(const Result& expected) { |
| 500 | if (CheckCandidate1(expected)) { |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | const std::string& local_type = LocalCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 505 | const std::string& local_protocol = LocalCandidate(ep1_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 506 | const std::string& remote_type = RemoteCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 507 | const std::string& remote_protocol = RemoteCandidate(ep1_ch1())->protocol(); |
| 508 | EXPECT_EQ(expected.controlling_type, local_type); |
| 509 | EXPECT_EQ(expected.controlled_type, remote_type); |
| 510 | EXPECT_EQ(expected.controlling_protocol, local_protocol); |
| 511 | EXPECT_EQ(expected.controlled_protocol, remote_protocol); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Return true if the approprite parts of the expected Result, based |
| 515 | // on the local and remote candidate of ep2_ch1, match. This can be |
| 516 | // used in an EXPECT_TRUE_WAIT. |
| 517 | bool CheckCandidate2(const Result& expected) { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 518 | const std::string& local_type = LocalCandidate(ep2_ch1())->type(); |
| 519 | const std::string& local_protocol = LocalCandidate(ep2_ch1())->protocol(); |
| 520 | const std::string& remote_type = RemoteCandidate(ep2_ch1())->type(); |
| 521 | const std::string& remote_protocol = RemoteCandidate(ep2_ch1())->protocol(); |
| 522 | return (local_protocol == expected.controlled_protocol && |
| 523 | remote_protocol == expected.controlling_protocol && |
| 524 | local_type == expected.controlled_type && |
| 525 | remote_type == expected.controlling_type); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // EXPECT_EQ on the approprite parts of the expected Result, based |
| 529 | // on the local and remote candidate of ep2_ch1. This is like |
| 530 | // CheckCandidate2, except that it will provide more detail about |
| 531 | // what didn't match. |
| 532 | void ExpectCandidate2(const Result& expected) { |
| 533 | if (CheckCandidate2(expected)) { |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | const std::string& local_type = LocalCandidate(ep2_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 538 | const std::string& local_protocol = LocalCandidate(ep2_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 539 | const std::string& remote_type = RemoteCandidate(ep2_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 540 | const std::string& remote_protocol = RemoteCandidate(ep2_ch1())->protocol(); |
| 541 | EXPECT_EQ(expected.controlled_type, local_type); |
| 542 | EXPECT_EQ(expected.controlling_type, remote_type); |
| 543 | EXPECT_EQ(expected.controlled_protocol, local_protocol); |
| 544 | EXPECT_EQ(expected.controlling_protocol, remote_protocol); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 545 | } |
| 546 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 547 | void Test(const Result& expected) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 548 | rtc::ScopedFakeClock clock; |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 549 | int64_t connect_start = rtc::TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 550 | int64_t connect_time; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 551 | |
| 552 | // Create the channels and wait for them to connect. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 553 | CreateChannels(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 554 | EXPECT_TRUE_SIMULATED_WAIT( |
| 555 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 556 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 557 | ep2_ch1()->writable(), |
| 558 | expected.connect_wait + kShortTimeout, clock); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 559 | connect_time = rtc::TimeMillis() - connect_start; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 560 | if (connect_time < expected.connect_wait) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 561 | RTC_LOG(LS_INFO) << "Connect time: " << connect_time << " ms"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 562 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 563 | RTC_LOG(LS_INFO) << "Connect time: " |
| 564 | << "TIMEOUT (" << expected.connect_wait << " ms)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 567 | // 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] | 568 | // This may take up to 2 seconds. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 569 | if (ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection()) { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 570 | int64_t converge_start = rtc::TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 571 | int64_t converge_time; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 572 | // Verifying local and remote channel selected connection information. |
| 573 | // This is done only for the RFC 5245 as controlled agent will use |
| 574 | // USE-CANDIDATE from controlling (ep1) agent. We can easily predict from |
| 575 | // EP1 result matrix. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 576 | EXPECT_TRUE_SIMULATED_WAIT( |
| 577 | CheckCandidate1(expected) && CheckCandidate2(expected), |
| 578 | kMediumTimeout, clock); |
Taylor Brandstetter | 7e1b8fb | 2016-05-26 15:21:45 -0700 | [diff] [blame] | 579 | // Also do EXPECT_EQ on each part so that failures are more verbose. |
| 580 | ExpectCandidate1(expected); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 581 | ExpectCandidate2(expected); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 582 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 583 | converge_time = rtc::TimeMillis() - converge_start; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 584 | int64_t converge_wait = 2000; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 585 | if (converge_time < converge_wait) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 586 | RTC_LOG(LS_INFO) << "Converge time: " << converge_time << " ms"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 587 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 588 | RTC_LOG(LS_INFO) << "Converge time: " |
| 589 | << "TIMEOUT (" << converge_wait << " ms)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | // Try sending some data to other end. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 593 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 594 | |
| 595 | // Destroy the channels, and wait for them to be fully cleaned up. |
| 596 | DestroyChannels(); |
| 597 | } |
| 598 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 599 | void TestSendRecv(rtc::FakeClock* clock) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 600 | for (int i = 0; i < 10; ++i) { |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 601 | const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 602 | int len = static_cast<int>(strlen(data)); |
| 603 | // local_channel1 <==> remote_channel1 |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 604 | EXPECT_EQ_SIMULATED_WAIT(len, SendData(ep1_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 605 | kMediumTimeout, *clock); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 606 | EXPECT_TRUE_SIMULATED_WAIT(CheckDataOnChannel(ep2_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 607 | kMediumTimeout, *clock); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 608 | EXPECT_EQ_SIMULATED_WAIT(len, SendData(ep2_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 609 | kMediumTimeout, *clock); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 610 | EXPECT_TRUE_SIMULATED_WAIT(CheckDataOnChannel(ep1_ch1(), data, len), |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 611 | kMediumTimeout, *clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 615 | // 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] | 616 | // 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] | 617 | // restart the ice gathering. Finally it waits for the transport to select a |
| 618 | // new connection using the newly generated ice candidates. |
| 619 | // Before calling this function the end points must be configured. |
| 620 | void TestHandleIceUfragPasswordChanged() { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 621 | rtc::ScopedFakeClock clock; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 622 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 623 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 624 | EXPECT_TRUE_SIMULATED_WAIT( |
| 625 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 626 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 627 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 628 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 629 | const Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1()); |
| 630 | const Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1()); |
| 631 | const Candidate* old_remote_candidate1 = RemoteCandidate(ep1_ch1()); |
| 632 | const Candidate* old_remote_candidate2 = RemoteCandidate(ep2_ch1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 633 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 634 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 635 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 636 | ep1_ch1()->MaybeStartGathering(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 637 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 638 | |
| 639 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 640 | ep2_ch1()->MaybeStartGathering(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 641 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 642 | EXPECT_TRUE_SIMULATED_WAIT(LocalCandidate(ep1_ch1())->generation() != |
| 643 | old_local_candidate1->generation(), |
| 644 | kMediumTimeout, clock); |
| 645 | EXPECT_TRUE_SIMULATED_WAIT(LocalCandidate(ep2_ch1())->generation() != |
| 646 | old_local_candidate2->generation(), |
| 647 | kMediumTimeout, clock); |
| 648 | EXPECT_TRUE_SIMULATED_WAIT(RemoteCandidate(ep1_ch1())->generation() != |
| 649 | old_remote_candidate1->generation(), |
| 650 | kMediumTimeout, clock); |
| 651 | EXPECT_TRUE_SIMULATED_WAIT(RemoteCandidate(ep2_ch1())->generation() != |
| 652 | old_remote_candidate2->generation(), |
| 653 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 654 | EXPECT_EQ(1u, RemoteCandidate(ep2_ch1())->generation()); |
| 655 | EXPECT_EQ(1u, RemoteCandidate(ep1_ch1())->generation()); |
| 656 | } |
| 657 | |
| 658 | void TestSignalRoleConflict() { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 659 | rtc::ScopedFakeClock clock; |
| 660 | // Default EP1 is in controlling state. |
| 661 | SetIceTiebreaker(0, kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 662 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 663 | SetIceRole(1, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 664 | SetIceTiebreaker(1, kHighTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 665 | |
| 666 | // Creating channels with both channels role set to CONTROLLING. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 667 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 668 | // Since both the channels initiated with controlling state and channel2 |
| 669 | // has higher tiebreaker value, channel1 should receive SignalRoleConflict. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 670 | EXPECT_TRUE_SIMULATED_WAIT(GetRoleConflict(0), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 671 | EXPECT_FALSE(GetRoleConflict(1)); |
| 672 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 673 | EXPECT_TRUE_SIMULATED_WAIT( |
| 674 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 675 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 676 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 677 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 678 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 679 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 680 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 681 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 682 | } |
| 683 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 684 | void OnReadyToSend(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 685 | GetEndpoint(transport)->ready_to_send_ = true; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 686 | } |
| 687 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 688 | // We pass the candidates directly to the other side. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 689 | void OnCandidateGathered(IceTransportInternal* ch, const Candidate& c) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 690 | if (force_relay_ && c.type() != RELAY_PORT_TYPE) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 691 | return; |
| 692 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 693 | if (GetEndpoint(ch)->save_candidates_) { |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 694 | GetEndpoint(ch)->saved_candidates_.push_back( |
| 695 | std::unique_ptr<CandidatesData>(new CandidatesData(ch, c))); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 696 | } else { |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 697 | main_.Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, |
| 698 | new CandidatesData(ch, c)); |
jiayl@webrtc.org | c5fd66d | 2014-12-29 19:23:37 +0000 | [diff] [blame] | 699 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 700 | } |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 701 | |
| 702 | void OnNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute> network_route) { |
| 703 | // If the |network_route| is unset, don't count. This is used in the case |
| 704 | // when the network on remote side is down, the signal will be fired with an |
| 705 | // unset network route and it shouldn't trigger a connection switch. |
| 706 | if (network_route) { |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 707 | ++selected_candidate_pair_switches_; |
| 708 | } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | int reset_selected_candidate_pair_switches() { |
| 712 | int switches = selected_candidate_pair_switches_; |
| 713 | selected_candidate_pair_switches_ = 0; |
| 714 | return switches; |
| 715 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 716 | |
| 717 | void PauseCandidates(int endpoint) { |
| 718 | GetEndpoint(endpoint)->save_candidates_ = true; |
| 719 | } |
| 720 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 721 | void OnCandidatesRemoved(IceTransportInternal* ch, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 722 | const std::vector<Candidate>& candidates) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 723 | // Candidate removals are not paused. |
| 724 | CandidatesData* candidates_data = new CandidatesData(ch, candidates); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 725 | main_.Post(RTC_FROM_HERE, this, MSG_REMOVE_CANDIDATES, candidates_data); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 728 | // Tcp candidate verification has to be done when they are generated. |
| 729 | void VerifySavedTcpCandidates(int endpoint, const std::string& tcptype) { |
| 730 | for (auto& data : GetEndpoint(endpoint)->saved_candidates_) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 731 | for (auto& candidate : data->candidates) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 732 | EXPECT_EQ(candidate.protocol(), TCP_PROTOCOL_NAME); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 733 | EXPECT_EQ(candidate.tcptype(), tcptype); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 734 | if (candidate.tcptype() == TCPTYPE_ACTIVE_STR) { |
| 735 | EXPECT_EQ(candidate.address().port(), DISCARD_PORT); |
| 736 | } else if (candidate.tcptype() == TCPTYPE_PASSIVE_STR) { |
| 737 | EXPECT_NE(candidate.address().port(), DISCARD_PORT); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 738 | } else { |
| 739 | FAIL() << "Unknown tcptype: " << candidate.tcptype(); |
| 740 | } |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 745 | void ResumeCandidates(int endpoint) { |
| 746 | Endpoint* ed = GetEndpoint(endpoint); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 747 | for (auto& candidate : ed->saved_candidates_) { |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 748 | main_.Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, candidate.release()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 749 | } |
| 750 | ed->saved_candidates_.clear(); |
| 751 | ed->save_candidates_ = false; |
| 752 | } |
| 753 | |
| 754 | void OnMessage(rtc::Message* msg) { |
| 755 | switch (msg->message_id) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 756 | case MSG_ADD_CANDIDATES: { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 757 | std::unique_ptr<CandidatesData> data( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 758 | static_cast<CandidatesData*>(msg->pdata)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 759 | P2PTransportChannel* rch = GetRemoteChannel(data->channel); |
| 760 | if (!rch) { |
| 761 | return; |
| 762 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 763 | for (auto& c : data->candidates) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 764 | if (remote_ice_parameter_source_ != FROM_CANDIDATE) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 765 | c.set_username(""); |
| 766 | c.set_password(""); |
| 767 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 768 | RTC_LOG(LS_INFO) << "Candidate(" << data->channel->component() << "->" |
| 769 | << rch->component() << "): " << c.ToString(); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 770 | rch->AddRemoteCandidate(c); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 771 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 772 | break; |
| 773 | } |
| 774 | case MSG_REMOVE_CANDIDATES: { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 775 | std::unique_ptr<CandidatesData> data( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 776 | static_cast<CandidatesData*>(msg->pdata)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 777 | P2PTransportChannel* rch = GetRemoteChannel(data->channel); |
| 778 | if (!rch) { |
| 779 | return; |
| 780 | } |
| 781 | for (Candidate& c : data->candidates) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 782 | RTC_LOG(LS_INFO) << "Removed remote candidate " << c.ToString(); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 783 | rch->RemoveRemoteCandidate(c); |
| 784 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 785 | break; |
| 786 | } |
| 787 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 788 | } |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 789 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 790 | void OnReadPacket(rtc::PacketTransportInternal* transport, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 791 | const char* data, |
| 792 | size_t len, |
| 793 | const rtc::PacketTime& packet_time, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 794 | int flags) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 795 | std::list<std::string>& packets = GetPacketList(transport); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 796 | packets.push_front(std::string(data, len)); |
| 797 | } |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 798 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 799 | void OnRoleConflict(IceTransportInternal* channel) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 800 | GetEndpoint(channel)->OnRoleConflict(true); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 801 | IceRole new_role = GetEndpoint(channel)->ice_role() == ICEROLE_CONTROLLING |
| 802 | ? ICEROLE_CONTROLLED |
| 803 | : ICEROLE_CONTROLLING; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 804 | channel->SetIceRole(new_role); |
| 805 | } |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 806 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 807 | int SendData(IceTransportInternal* channel, const char* data, size_t len) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 808 | rtc::PacketOptions options; |
| 809 | return channel->SendPacket(data, len, options, 0); |
| 810 | } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 811 | bool CheckDataOnChannel(IceTransportInternal* channel, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 812 | const char* data, |
| 813 | int len) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 814 | return GetChannelData(channel)->CheckData(data, len); |
| 815 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 816 | static const Candidate* LocalCandidate(P2PTransportChannel* ch) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 817 | return (ch && ch->selected_connection()) |
| 818 | ? &ch->selected_connection()->local_candidate() |
| 819 | : NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 820 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 821 | static const Candidate* RemoteCandidate(P2PTransportChannel* ch) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 822 | return (ch && ch->selected_connection()) |
| 823 | ? &ch->selected_connection()->remote_candidate() |
| 824 | : NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 825 | } |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 826 | Endpoint* GetEndpoint(rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 827 | if (ep1_.HasTransport(transport)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 828 | return &ep1_; |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 829 | } else if (ep2_.HasTransport(transport)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 830 | return &ep2_; |
| 831 | } else { |
| 832 | return NULL; |
| 833 | } |
| 834 | } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 835 | P2PTransportChannel* GetRemoteChannel(IceTransportInternal* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 836 | if (ch == ep1_ch1()) |
| 837 | return ep2_ch1(); |
| 838 | else if (ch == ep1_ch2()) |
| 839 | return ep2_ch2(); |
| 840 | else if (ch == ep2_ch1()) |
| 841 | return ep1_ch1(); |
| 842 | else if (ch == ep2_ch2()) |
| 843 | return ep1_ch2(); |
| 844 | else |
| 845 | return NULL; |
| 846 | } |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 847 | std::list<std::string>& GetPacketList( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 848 | rtc::PacketTransportInternal* transport) { |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 849 | return GetChannelData(transport)->ch_packets_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 852 | enum RemoteIceParameterSource { FROM_CANDIDATE, FROM_SETICEPARAMETERS }; |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 853 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 854 | // How does the test pass ICE parameters to the P2PTransportChannel? |
| 855 | // On the candidate itself, or through SetRemoteIceParameters? |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 856 | // Goes through the candidate itself by default. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 857 | void set_remote_ice_parameter_source(RemoteIceParameterSource source) { |
| 858 | remote_ice_parameter_source_ = source; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | void set_force_relay(bool relay) { |
| 862 | force_relay_ = relay; |
| 863 | } |
| 864 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 865 | void ConnectSignalNominated(Connection* conn) { |
| 866 | conn->SignalNominated.connect(this, |
| 867 | &P2PTransportChannelTestBase::OnNominated); |
| 868 | } |
| 869 | |
| 870 | void OnNominated(Connection* conn) { nominated_ = true; } |
| 871 | bool nominated() { return nominated_; } |
| 872 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 873 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 874 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 875 | std::unique_ptr<rtc::NATSocketServer> nss_; |
| 876 | std::unique_ptr<rtc::FirewallSocketServer> ss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 877 | rtc::AutoSocketServerThread main_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 878 | std::unique_ptr<TestStunServer> stun_server_; |
| 879 | TestTurnServer turn_server_; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 880 | rtc::SocksProxyServer socks_server1_; |
| 881 | rtc::SocksProxyServer socks_server2_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 882 | Endpoint ep1_; |
| 883 | Endpoint ep2_; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 884 | RemoteIceParameterSource remote_ice_parameter_source_ = FROM_CANDIDATE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 885 | bool force_relay_; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 886 | int selected_candidate_pair_switches_ = 0; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 887 | |
| 888 | bool nominated_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 889 | }; |
| 890 | |
| 891 | // The tests have only a few outcomes, which we predefine. |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 892 | const P2PTransportChannelTestBase::Result |
| 893 | P2PTransportChannelTestBase::kLocalUdpToLocalUdp("local", |
| 894 | "udp", |
| 895 | "local", |
| 896 | "udp", |
| 897 | 1000); |
| 898 | const P2PTransportChannelTestBase::Result |
| 899 | P2PTransportChannelTestBase::kLocalUdpToStunUdp("local", |
| 900 | "udp", |
| 901 | "stun", |
| 902 | "udp", |
| 903 | 1000); |
| 904 | const P2PTransportChannelTestBase::Result |
| 905 | P2PTransportChannelTestBase::kLocalUdpToPrflxUdp("local", |
| 906 | "udp", |
| 907 | "prflx", |
| 908 | "udp", |
| 909 | 1000); |
| 910 | const P2PTransportChannelTestBase::Result |
| 911 | P2PTransportChannelTestBase::kPrflxUdpToLocalUdp("prflx", |
| 912 | "udp", |
| 913 | "local", |
| 914 | "udp", |
| 915 | 1000); |
| 916 | const P2PTransportChannelTestBase::Result |
| 917 | P2PTransportChannelTestBase::kStunUdpToLocalUdp("stun", |
| 918 | "udp", |
| 919 | "local", |
| 920 | "udp", |
| 921 | 1000); |
| 922 | const P2PTransportChannelTestBase::Result |
| 923 | P2PTransportChannelTestBase::kStunUdpToStunUdp("stun", |
| 924 | "udp", |
| 925 | "stun", |
| 926 | "udp", |
| 927 | 1000); |
| 928 | const P2PTransportChannelTestBase::Result |
| 929 | P2PTransportChannelTestBase::kStunUdpToPrflxUdp("stun", |
| 930 | "udp", |
| 931 | "prflx", |
| 932 | "udp", |
| 933 | 1000); |
| 934 | const P2PTransportChannelTestBase::Result |
| 935 | P2PTransportChannelTestBase::kPrflxUdpToStunUdp("prflx", |
| 936 | "udp", |
| 937 | "stun", |
| 938 | "udp", |
| 939 | 1000); |
| 940 | const P2PTransportChannelTestBase::Result |
| 941 | P2PTransportChannelTestBase::kLocalUdpToRelayUdp("local", |
| 942 | "udp", |
| 943 | "relay", |
| 944 | "udp", |
| 945 | 2000); |
| 946 | const P2PTransportChannelTestBase::Result |
| 947 | P2PTransportChannelTestBase::kPrflxUdpToRelayUdp("prflx", |
| 948 | "udp", |
| 949 | "relay", |
| 950 | "udp", |
| 951 | 2000); |
| 952 | const P2PTransportChannelTestBase::Result |
| 953 | P2PTransportChannelTestBase::kRelayUdpToPrflxUdp("relay", |
| 954 | "udp", |
| 955 | "prflx", |
| 956 | "udp", |
| 957 | 2000); |
| 958 | const P2PTransportChannelTestBase::Result |
| 959 | P2PTransportChannelTestBase::kLocalTcpToLocalTcp("local", |
| 960 | "tcp", |
| 961 | "local", |
| 962 | "tcp", |
| 963 | 3000); |
| 964 | const P2PTransportChannelTestBase::Result |
| 965 | P2PTransportChannelTestBase::kLocalTcpToPrflxTcp("local", |
| 966 | "tcp", |
| 967 | "prflx", |
| 968 | "tcp", |
| 969 | 3000); |
| 970 | const P2PTransportChannelTestBase::Result |
| 971 | P2PTransportChannelTestBase::kPrflxTcpToLocalTcp("prflx", |
| 972 | "tcp", |
| 973 | "local", |
| 974 | "tcp", |
| 975 | 3000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 976 | |
| 977 | // Test the matrix of all the connectivity types we expect to see in the wild. |
| 978 | // Just test every combination of the configs in the Config enum. |
| 979 | class P2PTransportChannelTest : public P2PTransportChannelTestBase { |
| 980 | protected: |
| 981 | static const Result* kMatrix[NUM_CONFIGS][NUM_CONFIGS]; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 982 | void ConfigureEndpoints(Config config1, |
| 983 | Config config2, |
| 984 | int allocator_flags1, |
| 985 | int allocator_flags2) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 986 | ConfigureEndpoint(0, config1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 987 | SetAllocatorFlags(0, allocator_flags1); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 988 | SetAllocationStepDelay(0, kMinimumStepDelay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 989 | ConfigureEndpoint(1, config2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 990 | SetAllocatorFlags(1, allocator_flags2); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 991 | SetAllocationStepDelay(1, kMinimumStepDelay); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 992 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 993 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 994 | } |
| 995 | void ConfigureEndpoint(int endpoint, Config config) { |
| 996 | switch (config) { |
| 997 | case OPEN: |
| 998 | AddAddress(endpoint, kPublicAddrs[endpoint]); |
| 999 | break; |
| 1000 | case NAT_FULL_CONE: |
| 1001 | case NAT_ADDR_RESTRICTED: |
| 1002 | case NAT_PORT_RESTRICTED: |
| 1003 | case NAT_SYMMETRIC: |
| 1004 | AddAddress(endpoint, kPrivateAddrs[endpoint]); |
| 1005 | // Add a single NAT of the desired type |
| 1006 | nat()->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint], |
| 1007 | static_cast<rtc::NATType>(config - NAT_FULL_CONE))-> |
| 1008 | AddClient(kPrivateAddrs[endpoint]); |
| 1009 | break; |
| 1010 | case NAT_DOUBLE_CONE: |
| 1011 | case NAT_SYMMETRIC_THEN_CONE: |
| 1012 | AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]); |
| 1013 | // Add a two cascaded NATs of the desired types |
| 1014 | nat()->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint], |
| 1015 | (config == NAT_DOUBLE_CONE) ? |
| 1016 | rtc::NAT_OPEN_CONE : rtc::NAT_SYMMETRIC)-> |
| 1017 | AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint], |
| 1018 | rtc::NAT_OPEN_CONE)-> |
| 1019 | AddClient(kCascadedPrivateAddrs[endpoint]); |
| 1020 | break; |
| 1021 | case BLOCK_UDP: |
| 1022 | case BLOCK_UDP_AND_INCOMING_TCP: |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1023 | case BLOCK_ALL_BUT_OUTGOING_HTTP: |
| 1024 | case PROXY_HTTPS: |
| 1025 | case PROXY_SOCKS: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1026 | AddAddress(endpoint, kPublicAddrs[endpoint]); |
| 1027 | // Block all UDP |
| 1028 | fw()->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, |
| 1029 | kPublicAddrs[endpoint]); |
| 1030 | if (config == BLOCK_UDP_AND_INCOMING_TCP) { |
| 1031 | // Block TCP inbound to the endpoint |
| 1032 | fw()->AddRule(false, rtc::FP_TCP, SocketAddress(), |
| 1033 | kPublicAddrs[endpoint]); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1034 | } else if (config == BLOCK_ALL_BUT_OUTGOING_HTTP) { |
| 1035 | // Block all TCP to/from the endpoint except 80/443 out |
| 1036 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1037 | SocketAddress(rtc::IPAddress(INADDR_ANY), 80)); |
| 1038 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1039 | SocketAddress(rtc::IPAddress(INADDR_ANY), 443)); |
| 1040 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1041 | kPublicAddrs[endpoint]); |
| 1042 | } else if (config == PROXY_HTTPS) { |
| 1043 | // Block all TCP to/from the endpoint except to the proxy server |
| 1044 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1045 | kHttpsProxyAddrs[endpoint]); |
| 1046 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1047 | kPublicAddrs[endpoint]); |
| 1048 | SetProxy(endpoint, rtc::PROXY_HTTPS); |
| 1049 | } else if (config == PROXY_SOCKS) { |
| 1050 | // Block all TCP to/from the endpoint except to the proxy server |
| 1051 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1052 | kSocksProxyAddrs[endpoint]); |
| 1053 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1054 | kPublicAddrs[endpoint]); |
| 1055 | SetProxy(endpoint, rtc::PROXY_SOCKS5); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1056 | } |
| 1057 | break; |
| 1058 | default: |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1059 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | }; |
| 1064 | |
| 1065 | // Shorthands for use in the test matrix. |
| 1066 | #define LULU &kLocalUdpToLocalUdp |
| 1067 | #define LUSU &kLocalUdpToStunUdp |
| 1068 | #define LUPU &kLocalUdpToPrflxUdp |
| 1069 | #define PULU &kPrflxUdpToLocalUdp |
| 1070 | #define SULU &kStunUdpToLocalUdp |
| 1071 | #define SUSU &kStunUdpToStunUdp |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1072 | #define SUPU &kStunUdpToPrflxUdp |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1073 | #define PUSU &kPrflxUdpToStunUdp |
| 1074 | #define LURU &kLocalUdpToRelayUdp |
| 1075 | #define PURU &kPrflxUdpToRelayUdp |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1076 | #define RUPU &kRelayUdpToPrflxUdp |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1077 | #define LTLT &kLocalTcpToLocalTcp |
| 1078 | #define LTPT &kLocalTcpToPrflxTcp |
| 1079 | #define PTLT &kPrflxTcpToLocalTcp |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1080 | // TODO(?): Enable these once TestRelayServer can accept external TCP. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1081 | #define LTRT NULL |
| 1082 | #define LSRS NULL |
| 1083 | |
| 1084 | // Test matrix. Originator behavior defined by rows, receiever by columns. |
| 1085 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1086 | // TODO(?): Fix NULLs caused by lack of TCP support in NATSocket. |
| 1087 | // TODO(?): Fix NULLs caused by no HTTP proxy support. |
| 1088 | // TODO(?): Rearrange rows/columns from best to worst. |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1089 | const P2PTransportChannelTest::Result* |
| 1090 | P2PTransportChannelTest::kMatrix[NUM_CONFIGS][NUM_CONFIGS] = { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1091 | // OPEN CONE ADDR PORT SYMM 2CON SCON !UDP !TCP HTTP PRXH PRXS |
| 1092 | /*OP*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, LTPT, LTPT, LSRS, NULL, LTPT}, |
| 1093 | /*CO*/ {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1094 | /*AD*/ {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1095 | /*PO*/ {SULU, SUSU, SUSU, SUSU, RUPU, SUSU, RUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1096 | /*SY*/ {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1097 | /*2C*/ {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1098 | /*SC*/ {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1099 | /*!U*/ {LTPT, NULL, NULL, NULL, NULL, NULL, NULL, LTPT, LTPT, LSRS, NULL, LTRT}, |
| 1100 | /*!T*/ {PTLT, NULL, NULL, NULL, NULL, NULL, NULL, PTLT, LTRT, LSRS, NULL, LTRT}, |
| 1101 | /*HT*/ {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, LSRS}, |
| 1102 | /*PR*/ {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, |
| 1103 | /*PR*/ {LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LTRT, LSRS, NULL, LTRT}, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1104 | }; |
| 1105 | |
| 1106 | // The actual tests that exercise all the various configurations. |
| 1107 | // Test names are of the form P2PTransportChannelTest_TestOPENToNAT_FULL_CONE |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1108 | #define P2P_TEST_DECLARATION(x, y, z) \ |
| 1109 | TEST_F(P2PTransportChannelTest, z##Test##x##To##y) { \ |
| 1110 | ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_SOCKET, \ |
| 1111 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); \ |
Taylor Brandstetter | 7e1b8fb | 2016-05-26 15:21:45 -0700 | [diff] [blame] | 1112 | if (kMatrix[x][y] != NULL) \ |
| 1113 | Test(*kMatrix[x][y]); \ |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1114 | else \ |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1115 | RTC_LOG(LS_WARNING) << "Not yet implemented"; \ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1118 | #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] | 1119 | |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1120 | #define P2P_TEST_SET(x) \ |
| 1121 | P2P_TEST(x, OPEN) \ |
| 1122 | P2P_TEST(x, NAT_FULL_CONE) \ |
| 1123 | P2P_TEST(x, NAT_ADDR_RESTRICTED) \ |
| 1124 | P2P_TEST(x, NAT_PORT_RESTRICTED) \ |
| 1125 | P2P_TEST(x, NAT_SYMMETRIC) \ |
| 1126 | P2P_TEST(x, NAT_DOUBLE_CONE) \ |
| 1127 | P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \ |
| 1128 | P2P_TEST(x, BLOCK_UDP) \ |
| 1129 | P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \ |
| 1130 | P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \ |
| 1131 | P2P_TEST(x, PROXY_HTTPS) \ |
| 1132 | P2P_TEST(x, PROXY_SOCKS) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1133 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1134 | P2P_TEST_SET(OPEN) |
| 1135 | P2P_TEST_SET(NAT_FULL_CONE) |
| 1136 | P2P_TEST_SET(NAT_ADDR_RESTRICTED) |
| 1137 | P2P_TEST_SET(NAT_PORT_RESTRICTED) |
| 1138 | P2P_TEST_SET(NAT_SYMMETRIC) |
| 1139 | P2P_TEST_SET(NAT_DOUBLE_CONE) |
| 1140 | P2P_TEST_SET(NAT_SYMMETRIC_THEN_CONE) |
| 1141 | P2P_TEST_SET(BLOCK_UDP) |
| 1142 | P2P_TEST_SET(BLOCK_UDP_AND_INCOMING_TCP) |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1143 | P2P_TEST_SET(BLOCK_ALL_BUT_OUTGOING_HTTP) |
| 1144 | P2P_TEST_SET(PROXY_HTTPS) |
| 1145 | P2P_TEST_SET(PROXY_SOCKS) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1146 | |
| 1147 | // Test that we restart candidate allocation when local ufrag&pwd changed. |
| 1148 | // Standard Ice protocol is used. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1149 | TEST_F(P2PTransportChannelTest, HandleUfragPwdChange) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1150 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1151 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1152 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1153 | TestHandleIceUfragPasswordChanged(); |
| 1154 | DestroyChannels(); |
| 1155 | } |
| 1156 | |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1157 | // Same as above test, but with a symmetric NAT. |
| 1158 | // We should end up with relay<->prflx candidate pairs, with generation "1". |
| 1159 | TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeSymmetricNat) { |
| 1160 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 1161 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1162 | CreateChannels(); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1163 | TestHandleIceUfragPasswordChanged(); |
| 1164 | DestroyChannels(); |
| 1165 | } |
| 1166 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1167 | // Test the operation of GetStats. |
| 1168 | TEST_F(P2PTransportChannelTest, GetStats) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1169 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1170 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1171 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1172 | CreateChannels(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1173 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1174 | ep2_ch1()->receiving() && |
| 1175 | ep2_ch1()->writable(), |
| 1176 | kMediumTimeout, clock); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1177 | TestSendRecv(&clock); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1178 | ConnectionInfos infos; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1179 | ASSERT_TRUE(ep1_ch1()->GetStats(&infos)); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1180 | ASSERT_GE(infos.size(), 1u); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1181 | ConnectionInfo* best_conn_info = nullptr; |
| 1182 | for (ConnectionInfo& info : infos) { |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1183 | if (info.best_connection) { |
| 1184 | best_conn_info = &info; |
| 1185 | break; |
| 1186 | } |
| 1187 | } |
| 1188 | ASSERT_TRUE(best_conn_info != nullptr); |
| 1189 | EXPECT_TRUE(best_conn_info->new_connection); |
| 1190 | EXPECT_TRUE(best_conn_info->receiving); |
| 1191 | EXPECT_TRUE(best_conn_info->writable); |
| 1192 | EXPECT_FALSE(best_conn_info->timeout); |
| 1193 | EXPECT_EQ(10U, best_conn_info->sent_total_packets); |
| 1194 | EXPECT_EQ(0U, best_conn_info->sent_discarded_packets); |
| 1195 | EXPECT_EQ(10 * 36U, best_conn_info->sent_total_bytes); |
| 1196 | EXPECT_EQ(10 * 36U, best_conn_info->recv_total_bytes); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1197 | DestroyChannels(); |
| 1198 | } |
| 1199 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1200 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1201 | // is disconnected. |
| 1202 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileDisconnected) { |
| 1203 | rtc::ScopedFakeClock clock; |
| 1204 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1205 | |
| 1206 | CreateChannels(); |
| 1207 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1208 | ep2_ch1()->receiving() && |
| 1209 | ep2_ch1()->writable(), |
| 1210 | kDefaultTimeout, clock); |
| 1211 | |
| 1212 | // Drop all packets so that both channels become not writable. |
| 1213 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 1214 | const int kWriteTimeoutDelay = 8000; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1215 | EXPECT_TRUE_SIMULATED_WAIT(!ep1_ch1()->writable() && !ep2_ch1()->writable(), |
| 1216 | kWriteTimeoutDelay, clock); |
| 1217 | |
| 1218 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1219 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1220 | ep1_ch1()->MaybeStartGathering(); |
| 1221 | EXPECT_EQ(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1222 | webrtc::kEnumCounterIceRestart, |
| 1223 | static_cast<int>(IceRestartState::DISCONNECTED))); |
| 1224 | |
| 1225 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1226 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1227 | ep2_ch1()->MaybeStartGathering(); |
| 1228 | EXPECT_EQ(1, GetMetricsObserver(1)->GetEnumCounter( |
| 1229 | webrtc::kEnumCounterIceRestart, |
| 1230 | static_cast<int>(IceRestartState::DISCONNECTED))); |
| 1231 | |
| 1232 | DestroyChannels(); |
| 1233 | } |
| 1234 | |
| 1235 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1236 | // is connected. |
| 1237 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileConnected) { |
| 1238 | rtc::ScopedFakeClock clock; |
| 1239 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1240 | |
| 1241 | CreateChannels(); |
| 1242 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1243 | ep2_ch1()->receiving() && |
| 1244 | ep2_ch1()->writable(), |
| 1245 | kDefaultTimeout, clock); |
| 1246 | |
| 1247 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1248 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1249 | ep1_ch1()->MaybeStartGathering(); |
| 1250 | EXPECT_EQ(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1251 | webrtc::kEnumCounterIceRestart, |
| 1252 | static_cast<int>(IceRestartState::CONNECTED))); |
| 1253 | |
| 1254 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1255 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1256 | ep2_ch1()->MaybeStartGathering(); |
| 1257 | EXPECT_EQ(1, GetMetricsObserver(1)->GetEnumCounter( |
| 1258 | webrtc::kEnumCounterIceRestart, |
| 1259 | static_cast<int>(IceRestartState::CONNECTED))); |
| 1260 | |
| 1261 | DestroyChannels(); |
| 1262 | } |
| 1263 | |
| 1264 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1265 | // is connecting. |
| 1266 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileConnecting) { |
| 1267 | rtc::ScopedFakeClock clock; |
| 1268 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1269 | |
| 1270 | // Create the channels without waiting for them to become connected. |
| 1271 | CreateChannels(); |
| 1272 | |
| 1273 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1274 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1275 | ep1_ch1()->MaybeStartGathering(); |
| 1276 | EXPECT_EQ(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1277 | webrtc::kEnumCounterIceRestart, |
| 1278 | static_cast<int>(IceRestartState::CONNECTING))); |
| 1279 | |
| 1280 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1281 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1282 | ep2_ch1()->MaybeStartGathering(); |
| 1283 | EXPECT_EQ(1, GetMetricsObserver(1)->GetEnumCounter( |
| 1284 | webrtc::kEnumCounterIceRestart, |
| 1285 | static_cast<int>(IceRestartState::CONNECTING))); |
| 1286 | |
| 1287 | DestroyChannels(); |
| 1288 | } |
| 1289 | |
| 1290 | // Tests that a UMA on ICE regathering is recorded when there is a network |
| 1291 | // change if and only if continual gathering is enabled. |
| 1292 | TEST_F(P2PTransportChannelTest, |
| 1293 | TestIceRegatheringReasonContinualGatheringByNetworkChange) { |
| 1294 | rtc::ScopedFakeClock clock; |
| 1295 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1296 | |
| 1297 | // ep1 gathers continually but ep2 does not. |
| 1298 | IceConfig continual_gathering_config = |
| 1299 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1300 | IceConfig default_config; |
| 1301 | CreateChannels(continual_gathering_config, default_config); |
| 1302 | |
| 1303 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1304 | ep2_ch1()->receiving() && |
| 1305 | ep2_ch1()->writable(), |
| 1306 | kDefaultTimeout, clock); |
| 1307 | |
| 1308 | // Adding address in ep1 will trigger continual gathering. |
| 1309 | AddAddress(0, kAlternateAddrs[0]); |
| 1310 | EXPECT_EQ_SIMULATED_WAIT( |
| 1311 | 1, GetMetricsObserver(0)->GetEnumCounter( |
| 1312 | webrtc::kEnumCounterIceRegathering, |
| 1313 | static_cast<int>(IceRegatheringReason::NETWORK_CHANGE)), |
| 1314 | kDefaultTimeout, clock); |
| 1315 | |
| 1316 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1317 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1318 | ep2_ch1()->MaybeStartGathering(); |
| 1319 | |
| 1320 | AddAddress(1, kAlternateAddrs[1]); |
| 1321 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 1322 | // ep2 has not enabled continual gathering. |
| 1323 | EXPECT_EQ(0, GetMetricsObserver(1)->GetEnumCounter( |
| 1324 | webrtc::kEnumCounterIceRegathering, |
| 1325 | static_cast<int>(IceRegatheringReason::NETWORK_CHANGE))); |
| 1326 | |
| 1327 | DestroyChannels(); |
| 1328 | } |
| 1329 | |
| 1330 | // Tests that a UMA on ICE regathering is recorded when there is a network |
| 1331 | // failure if and only if continual gathering is enabled. |
| 1332 | TEST_F(P2PTransportChannelTest, |
| 1333 | TestIceRegatheringReasonContinualGatheringByNetworkFailure) { |
| 1334 | rtc::ScopedFakeClock clock; |
| 1335 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1336 | |
| 1337 | // ep1 gathers continually but ep2 does not. |
| 1338 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 1339 | config1.regather_on_failed_networks_interval = 2000; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1340 | IceConfig config2; |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 1341 | config2.regather_on_failed_networks_interval = 2000; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 1342 | CreateChannels(config1, config2); |
| 1343 | |
| 1344 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1345 | ep2_ch1()->receiving() && |
| 1346 | ep2_ch1()->writable(), |
| 1347 | kDefaultTimeout, clock); |
| 1348 | |
| 1349 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 1350 | // Timeout value such that all connections are deleted. |
| 1351 | const int kNetworkFailureTimeout = 35000; |
| 1352 | SIMULATED_WAIT(false, kNetworkFailureTimeout, clock); |
| 1353 | EXPECT_LE(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1354 | webrtc::kEnumCounterIceRegathering, |
| 1355 | static_cast<int>(IceRegatheringReason::NETWORK_FAILURE))); |
| 1356 | EXPECT_EQ(0, GetMetricsObserver(1)->GetEnumCounter( |
| 1357 | webrtc::kEnumCounterIceRegathering, |
| 1358 | static_cast<int>(IceRegatheringReason::NETWORK_FAILURE))); |
| 1359 | |
| 1360 | DestroyChannels(); |
| 1361 | } |
| 1362 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1363 | // Tests that ICE regathering occurs regularly when |
| 1364 | // regather_all_networks_interval_range configuration value is set. |
| 1365 | TEST_F(P2PTransportChannelTest, TestIceRegatherOnAllNetworksContinual) { |
| 1366 | rtc::ScopedFakeClock clock; |
| 1367 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1368 | |
| 1369 | // ep1 gathers continually but ep2 does not. |
| 1370 | const int kRegatherInterval = 2000; |
| 1371 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1372 | config1.regather_all_networks_interval_range.emplace( |
| 1373 | kRegatherInterval, kRegatherInterval); |
| 1374 | IceConfig config2; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1375 | CreateChannels(config1, config2); |
| 1376 | |
| 1377 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1378 | ep2_ch1()->receiving() && |
| 1379 | ep2_ch1()->writable(), |
| 1380 | kDefaultTimeout, clock); |
| 1381 | |
| 1382 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 1383 | // Timeout value such that all connections are deleted. |
| 1384 | const int kNetworkGatherDuration = 11000; |
| 1385 | SIMULATED_WAIT(false, kNetworkGatherDuration, clock); |
| 1386 | // Expect regathering to happen 5 times in 11s with 2s interval. |
| 1387 | EXPECT_LE(5, GetMetricsObserver(0)->GetEnumCounter( |
| 1388 | webrtc::kEnumCounterIceRegathering, |
| 1389 | static_cast<int>(IceRegatheringReason::OCCASIONAL_REFRESH))); |
| 1390 | // Expect no regathering if continual gathering not configured. |
| 1391 | EXPECT_EQ(0, GetMetricsObserver(1)->GetEnumCounter( |
| 1392 | webrtc::kEnumCounterIceRegathering, |
| 1393 | static_cast<int>(IceRegatheringReason::OCCASIONAL_REFRESH))); |
| 1394 | |
| 1395 | DestroyChannels(); |
| 1396 | } |
| 1397 | |
| 1398 | // Test that ICE periodic regathering can change the selected connection on the |
| 1399 | // specified interval and that the peers can communicate over the new |
| 1400 | // connection. The test is parameterized to test that it works when regathering |
| 1401 | // is done by the ICE controlling peer and when done by the controlled peer. |
| 1402 | class P2PTransportRegatherAllNetworksTest : public P2PTransportChannelTest { |
| 1403 | protected: |
| 1404 | void TestWithRoles(IceRole p1_role, IceRole p2_role) { |
| 1405 | rtc::ScopedFakeClock clock; |
| 1406 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 1407 | kDefaultPortAllocatorFlags); |
| 1408 | set_force_relay(true); |
| 1409 | |
| 1410 | const int kRegatherInterval = 2000; |
| 1411 | const int kNumRegathers = 2; |
| 1412 | |
| 1413 | // Set up peer 1 to auto regather every 2s. |
| 1414 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1415 | config1.regather_all_networks_interval_range.emplace( |
| 1416 | kRegatherInterval, kRegatherInterval); |
| 1417 | IceConfig config2 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1418 | |
| 1419 | // Set peer roles. |
| 1420 | SetIceRole(0, p1_role); |
| 1421 | SetIceRole(1, p2_role); |
| 1422 | |
| 1423 | CreateChannels(config1, config2); |
| 1424 | |
| 1425 | // Wait for initial connection to be made. |
| 1426 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && |
| 1427 | ep1_ch1()->writable() && |
| 1428 | ep2_ch1()->receiving() && |
| 1429 | ep2_ch1()->writable(), |
| 1430 | kMediumTimeout, clock); |
| 1431 | |
| 1432 | const Connection* initial_selected = ep1_ch1()->selected_connection(); |
| 1433 | |
| 1434 | // Wait long enough for 2 regathering cycles to happen plus some extra so |
| 1435 | // the new connection has time to settle. |
| 1436 | const int kWaitRegather = |
| 1437 | kRegatherInterval * kNumRegathers + kRegatherInterval / 2; |
| 1438 | SIMULATED_WAIT(false, kWaitRegather, clock); |
| 1439 | EXPECT_EQ(kNumRegathers, GetMetricsObserver(0)->GetEnumCounter( |
| 1440 | webrtc::kEnumCounterIceRegathering, |
| 1441 | static_cast<int>(IceRegatheringReason::OCCASIONAL_REFRESH))); |
| 1442 | |
| 1443 | const Connection* new_selected = ep1_ch1()->selected_connection(); |
| 1444 | |
| 1445 | // Want the new selected connection to be different. |
| 1446 | ASSERT_NE(initial_selected, new_selected); |
| 1447 | |
| 1448 | // Make sure we can communicate over the new connection too. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1449 | TestSendRecv(&clock); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1450 | } |
| 1451 | }; |
| 1452 | |
| 1453 | TEST_F(P2PTransportRegatherAllNetworksTest, TestControlling) { |
| 1454 | TestWithRoles(ICEROLE_CONTROLLING, ICEROLE_CONTROLLED); |
| 1455 | } |
| 1456 | |
| 1457 | TEST_F(P2PTransportRegatherAllNetworksTest, TestControlled) { |
| 1458 | TestWithRoles(ICEROLE_CONTROLLED, ICEROLE_CONTROLLING); |
| 1459 | } |
| 1460 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1461 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1462 | // when the signaling is slow. |
| 1463 | TEST_F(P2PTransportChannelTest, PeerReflexiveCandidateBeforeSignaling) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1464 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1465 | kDefaultPortAllocatorFlags); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1466 | // Emulate no remote parameters coming in. |
| 1467 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1468 | CreateChannels(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1469 | // Only have remote parameters come in for ep2, not ep1. |
| 1470 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1471 | |
| 1472 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1473 | // candidate. |
| 1474 | PauseCandidates(1); |
| 1475 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1476 | // Wait until the callee becomes writable to make sure that a ping request is |
| 1477 | // received by the caller before his remote ICE credentials are set. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1478 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, kMediumTimeout); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1479 | // Add two sets of remote ICE credentials, so that the ones used by the |
| 1480 | // candidate will be generation 1 instead of 0. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1481 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1482 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1483 | // The caller should have the selected connection connected to the peer |
| 1484 | // reflexive candidate. |
| 1485 | const Connection* selected_connection = nullptr; |
| 1486 | ASSERT_TRUE_WAIT( |
| 1487 | (selected_connection = ep1_ch1()->selected_connection()) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1488 | kMediumTimeout); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1489 | EXPECT_EQ("prflx", selected_connection->remote_candidate().type()); |
| 1490 | EXPECT_EQ(kIceUfrag[1], selected_connection->remote_candidate().username()); |
| 1491 | EXPECT_EQ(kIcePwd[1], selected_connection->remote_candidate().password()); |
| 1492 | EXPECT_EQ(1u, selected_connection->remote_candidate().generation()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1493 | |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1494 | ResumeCandidates(1); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1495 | // Verify ep1's selected connection is updated to use the 'local' candidate. |
| 1496 | EXPECT_EQ_WAIT("local", |
| 1497 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1498 | kMediumTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1499 | EXPECT_EQ(selected_connection, ep1_ch1()->selected_connection()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1500 | DestroyChannels(); |
| 1501 | } |
| 1502 | |
| 1503 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1504 | // when the signaling is slow and the end points are behind NAT. |
| 1505 | TEST_F(P2PTransportChannelTest, PeerReflexiveCandidateBeforeSignalingWithNAT) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1506 | ConfigureEndpoints(OPEN, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1507 | kDefaultPortAllocatorFlags); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1508 | // Emulate no remote parameters coming in. |
| 1509 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1510 | CreateChannels(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1511 | // Only have remote parameters come in for ep2, not ep1. |
| 1512 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1513 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1514 | // candidate. |
| 1515 | PauseCandidates(1); |
| 1516 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1517 | // Wait until the callee becomes writable to make sure that a ping request is |
| 1518 | // received by the caller before his remote ICE credentials are set. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1519 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, kMediumTimeout); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1520 | // Add two sets of remote ICE credentials, so that the ones used by the |
| 1521 | // candidate will be generation 1 instead of 0. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1522 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1523 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1524 | |
| 1525 | // The caller's selected connection should be connected to the peer reflexive |
| 1526 | // candidate. |
| 1527 | const Connection* selected_connection = nullptr; |
| 1528 | ASSERT_TRUE_WAIT( |
| 1529 | (selected_connection = ep1_ch1()->selected_connection()) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1530 | kMediumTimeout); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1531 | EXPECT_EQ("prflx", selected_connection->remote_candidate().type()); |
| 1532 | EXPECT_EQ(kIceUfrag[1], selected_connection->remote_candidate().username()); |
| 1533 | EXPECT_EQ(kIcePwd[1], selected_connection->remote_candidate().password()); |
| 1534 | EXPECT_EQ(1u, selected_connection->remote_candidate().generation()); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1535 | |
| 1536 | ResumeCandidates(1); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 1537 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1538 | EXPECT_EQ_WAIT("prflx", |
| 1539 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1540 | kMediumTimeout); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1541 | EXPECT_EQ(selected_connection, ep1_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1542 | DestroyChannels(); |
| 1543 | } |
| 1544 | |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1545 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1546 | // when the signaling is slow, even if the new candidate is created due to the |
| 1547 | // remote peer doing an ICE restart, pairing this candidate across generations. |
| 1548 | // |
| 1549 | // Previously this wasn't working due to a bug where the peer reflexive |
| 1550 | // candidate was only updated for the newest generation candidate pairs, and |
| 1551 | // not older-generation candidate pairs created by pairing candidates across |
| 1552 | // generations. This resulted in the old-generation prflx candidate being |
| 1553 | // prioritized above new-generation candidate pairs. |
| 1554 | TEST_F(P2PTransportChannelTest, |
| 1555 | PeerReflexiveCandidateBeforeSignalingWithIceRestart) { |
| 1556 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1557 | kDefaultPortAllocatorFlags); |
| 1558 | // Only gather relay candidates, so that when the prflx candidate arrives |
| 1559 | // it's prioritized above the current candidate pair. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1560 | GetEndpoint(0)->allocator_->set_candidate_filter(CF_RELAY); |
| 1561 | GetEndpoint(1)->allocator_->set_candidate_filter(CF_RELAY); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1562 | // Setting this allows us to control when SetRemoteIceParameters is called. |
| 1563 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1564 | CreateChannels(); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1565 | // Wait for the initial connection to be made. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1566 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 1567 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1568 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1569 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1570 | kDefaultTimeout); |
| 1571 | |
| 1572 | // 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] | 1573 | // ICE parameters until after a prflx connection has been made. |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1574 | PauseCandidates(1); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1575 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1576 | |
| 1577 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1578 | ep2_ch1()->MaybeStartGathering(); |
| 1579 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1580 | // The caller should have the selected connection connected to the peer |
| 1581 | // reflexive candidate. |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1582 | EXPECT_EQ_WAIT("prflx", |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1583 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1584 | kDefaultTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1585 | const Connection* prflx_selected_connection = |
| 1586 | ep1_ch1()->selected_connection(); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1587 | |
| 1588 | // Now simulate the ICE restart on ep1. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1589 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1590 | |
| 1591 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1592 | ep1_ch1()->MaybeStartGathering(); |
| 1593 | |
| 1594 | // Finally send the candidates from ep2's ICE restart and verify that ep1 uses |
| 1595 | // their information to update the peer reflexive candidate. |
| 1596 | ResumeCandidates(1); |
| 1597 | |
| 1598 | EXPECT_EQ_WAIT("relay", |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1599 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1600 | kDefaultTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1601 | EXPECT_EQ(prflx_selected_connection, ep1_ch1()->selected_connection()); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1602 | DestroyChannels(); |
| 1603 | } |
| 1604 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1605 | // Test that if remote candidates don't have ufrag and pwd, we still work. |
| 1606 | TEST_F(P2PTransportChannelTest, RemoteCandidatesWithoutUfragPwd) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1607 | rtc::ScopedFakeClock clock; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1608 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1609 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1610 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1611 | CreateChannels(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1612 | const Connection* selected_connection = NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1613 | // Wait until the callee's connections are created. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1614 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1615 | (selected_connection = ep2_ch1()->selected_connection()) != NULL, |
| 1616 | kMediumTimeout, clock); |
| 1617 | // Wait to make sure the selected connection is not changed. |
| 1618 | SIMULATED_WAIT(ep2_ch1()->selected_connection() != selected_connection, |
| 1619 | kShortTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1620 | EXPECT_TRUE(ep2_ch1()->selected_connection() == selected_connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1621 | DestroyChannels(); |
| 1622 | } |
| 1623 | |
| 1624 | // Test that a host behind NAT cannot be reached when incoming_only |
| 1625 | // is set to true. |
| 1626 | TEST_F(P2PTransportChannelTest, IncomingOnlyBlocked) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1627 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1628 | ConfigureEndpoints(NAT_FULL_CONE, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1629 | kDefaultPortAllocatorFlags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1630 | |
| 1631 | SetAllocatorFlags(0, kOnlyLocalPorts); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1632 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1633 | ep1_ch1()->set_incoming_only(true); |
| 1634 | |
| 1635 | // Pump for 1 second and verify that the channels are not connected. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1636 | SIMULATED_WAIT(false, kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1637 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1638 | EXPECT_FALSE(ep1_ch1()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1639 | EXPECT_FALSE(ep1_ch1()->writable()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1640 | EXPECT_FALSE(ep2_ch1()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1641 | EXPECT_FALSE(ep2_ch1()->writable()); |
| 1642 | |
| 1643 | DestroyChannels(); |
| 1644 | } |
| 1645 | |
| 1646 | // Test that a peer behind NAT can connect to a peer that has |
| 1647 | // incoming_only flag set. |
| 1648 | TEST_F(P2PTransportChannelTest, IncomingOnlyOpen) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1649 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1650 | ConfigureEndpoints(OPEN, NAT_FULL_CONE, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1651 | kDefaultPortAllocatorFlags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1652 | |
| 1653 | SetAllocatorFlags(0, kOnlyLocalPorts); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1654 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1655 | ep1_ch1()->set_incoming_only(true); |
| 1656 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1657 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1658 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 1659 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 1660 | ep2_ch1()->writable(), |
| 1661 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1662 | |
| 1663 | DestroyChannels(); |
| 1664 | } |
| 1665 | |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1666 | // Test that two peers can connect when one can only make outgoing TCP |
| 1667 | // connections. This has been observed in some scenarios involving |
| 1668 | // VPNs/firewalls. |
| 1669 | TEST_F(P2PTransportChannelTest, CanOnlyMakeOutgoingTcpConnections) { |
| 1670 | // The PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS flag is required if the |
| 1671 | // application needs this use case to work, since the application must accept |
| 1672 | // the tradeoff that more candidates need to be allocated. |
| 1673 | // |
| 1674 | // TODO(deadbeef): Later, make this flag the default, and do more elegant |
| 1675 | // things to ensure extra candidates don't waste resources? |
| 1676 | ConfigureEndpoints( |
| 1677 | OPEN, OPEN, |
| 1678 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_ANY_ADDRESS_PORTS, |
| 1679 | kDefaultPortAllocatorFlags); |
| 1680 | // In order to simulate nothing working but outgoing TCP connections, prevent |
| 1681 | // the endpoint from binding to its interface's address as well as the |
| 1682 | // "any" addresses. It can then only make a connection by using "Connect()". |
| 1683 | fw()->SetUnbindableIps({rtc::GetAnyIP(AF_INET), rtc::GetAnyIP(AF_INET6), |
| 1684 | kPublicAddrs[0].ipaddr()}); |
| 1685 | CreateChannels(); |
| 1686 | // Expect a "prflx" candidate on the side that can only make outgoing |
| 1687 | // connections, endpoint 0. |
| 1688 | Test(kPrflxTcpToLocalTcp); |
| 1689 | DestroyChannels(); |
| 1690 | } |
| 1691 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1692 | TEST_F(P2PTransportChannelTest, TestTcpConnectionsFromActiveToPassive) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1693 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1694 | AddAddress(0, kPublicAddrs[0]); |
| 1695 | AddAddress(1, kPublicAddrs[1]); |
| 1696 | |
| 1697 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1698 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1699 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1700 | int kOnlyLocalTcpPorts = PORTALLOCATOR_DISABLE_UDP | |
| 1701 | PORTALLOCATOR_DISABLE_STUN | |
| 1702 | PORTALLOCATOR_DISABLE_RELAY; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1703 | // Disable all protocols except TCP. |
| 1704 | SetAllocatorFlags(0, kOnlyLocalTcpPorts); |
| 1705 | SetAllocatorFlags(1, kOnlyLocalTcpPorts); |
| 1706 | |
| 1707 | SetAllowTcpListen(0, true); // actpass. |
| 1708 | SetAllowTcpListen(1, false); // active. |
| 1709 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1710 | // We want SetRemoteIceParameters to be called as it normally would. |
| 1711 | // Otherwise we won't know what parameters to use for the expected |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1712 | // prflx TCP candidates. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1713 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1714 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1715 | // Pause candidate so we could verify the candidate properties. |
| 1716 | PauseCandidates(0); |
| 1717 | PauseCandidates(1); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1718 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1719 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1720 | // Verify tcp candidates. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1721 | VerifySavedTcpCandidates(0, TCPTYPE_PASSIVE_STR); |
| 1722 | VerifySavedTcpCandidates(1, TCPTYPE_ACTIVE_STR); |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1723 | |
| 1724 | // Resume candidates. |
| 1725 | ResumeCandidates(0); |
| 1726 | ResumeCandidates(1); |
| 1727 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1728 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1729 | ep2_ch1()->receiving() && |
| 1730 | ep2_ch1()->writable(), |
| 1731 | kShortTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1732 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1733 | ep2_ch1()->selected_connection() && |
| 1734 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 1735 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1736 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1737 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1738 | DestroyChannels(); |
| 1739 | } |
| 1740 | |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 1741 | TEST_F(P2PTransportChannelTest, TestIceRoleConflict) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1742 | AddAddress(0, kPublicAddrs[0]); |
| 1743 | AddAddress(1, kPublicAddrs[1]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1744 | TestSignalRoleConflict(); |
| 1745 | } |
| 1746 | |
| 1747 | // Tests that the ice configs (protocol, tiebreaker and role) can be passed |
| 1748 | // down to ports. |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1749 | TEST_F(P2PTransportChannelTest, TestIceConfigWillPassDownToPort) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1750 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1751 | AddAddress(0, kPublicAddrs[0]); |
| 1752 | AddAddress(1, kPublicAddrs[1]); |
| 1753 | |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1754 | // Give the first connection the higher tiebreaker so its role won't |
| 1755 | // change unless we tell it to. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1756 | SetIceRole(0, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1757 | SetIceTiebreaker(0, kHighTiebreaker); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1758 | SetIceRole(1, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1759 | SetIceTiebreaker(1, kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1760 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1761 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1762 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1763 | EXPECT_EQ_SIMULATED_WAIT(2u, ep1_ch1()->ports().size(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1764 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1765 | const std::vector<PortInterface*> ports_before = ep1_ch1()->ports(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1766 | for (size_t i = 0; i < ports_before.size(); ++i) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1767 | EXPECT_EQ(ICEROLE_CONTROLLING, ports_before[i]->GetIceRole()); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1768 | EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1771 | ep1_ch1()->SetIceRole(ICEROLE_CONTROLLED); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1772 | ep1_ch1()->SetIceTiebreaker(kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1773 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1774 | const std::vector<PortInterface*> ports_after = ep1_ch1()->ports(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1775 | for (size_t i = 0; i < ports_after.size(); ++i) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1776 | EXPECT_EQ(ICEROLE_CONTROLLED, ports_before[i]->GetIceRole()); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1777 | // SetIceTiebreaker after ports have been created will fail. So expect the |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1778 | // original value. |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1779 | EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1782 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1783 | ep2_ch1()->receiving() && |
| 1784 | ep2_ch1()->writable(), |
| 1785 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1786 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1787 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1788 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1789 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1790 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1791 | DestroyChannels(); |
| 1792 | } |
| 1793 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1794 | // Verify that we can set DSCP value and retrieve properly from P2PTC. |
| 1795 | TEST_F(P2PTransportChannelTest, TestDefaultDscpValue) { |
| 1796 | AddAddress(0, kPublicAddrs[0]); |
| 1797 | AddAddress(1, kPublicAddrs[1]); |
| 1798 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1799 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1800 | EXPECT_EQ(rtc::DSCP_NO_CHANGE, |
| 1801 | GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1802 | EXPECT_EQ(rtc::DSCP_NO_CHANGE, |
| 1803 | GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1804 | GetEndpoint(0)->cd1_.ch_->SetOption( |
| 1805 | rtc::Socket::OPT_DSCP, rtc::DSCP_CS6); |
| 1806 | GetEndpoint(1)->cd1_.ch_->SetOption( |
| 1807 | rtc::Socket::OPT_DSCP, rtc::DSCP_CS6); |
| 1808 | EXPECT_EQ(rtc::DSCP_CS6, |
| 1809 | GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1810 | EXPECT_EQ(rtc::DSCP_CS6, |
| 1811 | GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1812 | GetEndpoint(0)->cd1_.ch_->SetOption( |
| 1813 | rtc::Socket::OPT_DSCP, rtc::DSCP_AF41); |
| 1814 | GetEndpoint(1)->cd1_.ch_->SetOption( |
| 1815 | rtc::Socket::OPT_DSCP, rtc::DSCP_AF41); |
| 1816 | EXPECT_EQ(rtc::DSCP_AF41, |
| 1817 | GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1818 | EXPECT_EQ(rtc::DSCP_AF41, |
| 1819 | GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1820 | } |
| 1821 | |
| 1822 | // Verify IPv6 connection is preferred over IPv4. |
guoweis@webrtc.org | 1f05c45 | 2014-12-15 21:25:54 +0000 | [diff] [blame] | 1823 | TEST_F(P2PTransportChannelTest, TestIPv6Connections) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1824 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1825 | AddAddress(0, kIPv6PublicAddrs[0]); |
| 1826 | AddAddress(0, kPublicAddrs[0]); |
| 1827 | AddAddress(1, kIPv6PublicAddrs[1]); |
| 1828 | AddAddress(1, kPublicAddrs[1]); |
| 1829 | |
| 1830 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1831 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1832 | |
| 1833 | // Enable IPv6 |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 1834 | SetAllocatorFlags( |
| 1835 | 0, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 1836 | SetAllocatorFlags( |
| 1837 | 1, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1838 | |
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 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1841 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1842 | ep2_ch1()->receiving() && |
| 1843 | ep2_ch1()->writable(), |
| 1844 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1845 | EXPECT_TRUE( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1846 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1847 | LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) && |
| 1848 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1])); |
| 1849 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1850 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1851 | DestroyChannels(); |
| 1852 | } |
| 1853 | |
| 1854 | // Testing forceful TURN connections. |
| 1855 | TEST_F(P2PTransportChannelTest, TestForceTurn) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1856 | rtc::ScopedFakeClock clock; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1857 | ConfigureEndpoints( |
| 1858 | NAT_PORT_RESTRICTED, NAT_SYMMETRIC, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1859 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 1860 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1861 | set_force_relay(true); |
| 1862 | |
| 1863 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1864 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1865 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1866 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1867 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 1868 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1869 | ep2_ch1()->receiving() && |
| 1870 | ep2_ch1()->writable(), |
| 1871 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1872 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1873 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1874 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1875 | |
| 1876 | EXPECT_EQ("relay", RemoteCandidate(ep1_ch1())->type()); |
| 1877 | EXPECT_EQ("relay", LocalCandidate(ep1_ch1())->type()); |
| 1878 | EXPECT_EQ("relay", RemoteCandidate(ep2_ch1())->type()); |
| 1879 | EXPECT_EQ("relay", LocalCandidate(ep2_ch1())->type()); |
| 1880 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1881 | TestSendRecv(&clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1882 | DestroyChannels(); |
| 1883 | } |
| 1884 | |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1885 | // Test that if continual gathering is set to true, ICE gathering state will |
| 1886 | // not change to "Complete", and vice versa. |
| 1887 | TEST_F(P2PTransportChannelTest, TestContinualGathering) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1888 | rtc::ScopedFakeClock clock; |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1889 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1890 | kDefaultPortAllocatorFlags); |
| 1891 | SetAllocationStepDelay(0, kDefaultStepDelay); |
| 1892 | SetAllocationStepDelay(1, kDefaultStepDelay); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 1893 | IceConfig continual_gathering_config = |
| 1894 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1895 | // By default, ep2 does not gather continually. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 1896 | IceConfig default_config; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1897 | CreateChannels(continual_gathering_config, default_config); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1898 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1899 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1900 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 1901 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 1902 | ep2_ch1()->writable(), |
| 1903 | kMediumTimeout, clock); |
| 1904 | SIMULATED_WAIT( |
| 1905 | IceGatheringState::kIceGatheringComplete == ep1_ch1()->gathering_state(), |
| 1906 | kShortTimeout, clock); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1907 | EXPECT_EQ(IceGatheringState::kIceGatheringGathering, |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1908 | ep1_ch1()->gathering_state()); |
| 1909 | // By now, ep2 should have completed gathering. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1910 | EXPECT_EQ(IceGatheringState::kIceGatheringComplete, |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1911 | ep2_ch1()->gathering_state()); |
| 1912 | |
| 1913 | DestroyChannels(); |
| 1914 | } |
| 1915 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1916 | // Test that a connection succeeds when the P2PTransportChannel uses a pooled |
| 1917 | // PortAllocatorSession that has not yet finished gathering candidates. |
| 1918 | TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1919 | rtc::ScopedFakeClock clock; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1920 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1921 | kDefaultPortAllocatorFlags); |
| 1922 | // First create a pooled session for each endpoint. |
| 1923 | auto& allocator_1 = GetEndpoint(0)->allocator_; |
| 1924 | auto& allocator_2 = GetEndpoint(1)->allocator_; |
| 1925 | int pool_size = 1; |
| 1926 | allocator_1->SetConfiguration(allocator_1->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1927 | allocator_1->turn_servers(), pool_size, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1928 | allocator_2->SetConfiguration(allocator_2->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1929 | allocator_2->turn_servers(), pool_size, false); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1930 | const PortAllocatorSession* pooled_session_1 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1931 | allocator_1->GetPooledSession(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1932 | const PortAllocatorSession* pooled_session_2 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1933 | allocator_2->GetPooledSession(); |
| 1934 | ASSERT_NE(nullptr, pooled_session_1); |
| 1935 | ASSERT_NE(nullptr, pooled_session_2); |
| 1936 | // Sanity check that pooled sessions haven't gathered anything yet. |
| 1937 | EXPECT_TRUE(pooled_session_1->ReadyPorts().empty()); |
| 1938 | EXPECT_TRUE(pooled_session_1->ReadyCandidates().empty()); |
| 1939 | EXPECT_TRUE(pooled_session_2->ReadyPorts().empty()); |
| 1940 | EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty()); |
| 1941 | // Now let the endpoints connect and try exchanging some data. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1942 | CreateChannels(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1943 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1944 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 1945 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 1946 | ep2_ch1()->writable(), |
| 1947 | kMediumTimeout, clock); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1948 | TestSendRecv(&clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1949 | // Make sure the P2PTransportChannels are actually using ports from the |
| 1950 | // pooled sessions. |
| 1951 | auto pooled_ports_1 = pooled_session_1->ReadyPorts(); |
| 1952 | auto pooled_ports_2 = pooled_session_2->ReadyPorts(); |
| 1953 | EXPECT_NE(pooled_ports_1.end(), |
| 1954 | std::find(pooled_ports_1.begin(), pooled_ports_1.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1955 | ep1_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1956 | EXPECT_NE(pooled_ports_2.end(), |
| 1957 | std::find(pooled_ports_2.begin(), pooled_ports_2.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1958 | ep2_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | // Test that a connection succeeds when the P2PTransportChannel uses a pooled |
| 1962 | // PortAllocatorSession that already finished gathering candidates. |
| 1963 | TEST_F(P2PTransportChannelTest, TestUsingPooledSessionAfterDoneGathering) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1964 | rtc::ScopedFakeClock clock; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1965 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1966 | kDefaultPortAllocatorFlags); |
| 1967 | // First create a pooled session for each endpoint. |
| 1968 | auto& allocator_1 = GetEndpoint(0)->allocator_; |
| 1969 | auto& allocator_2 = GetEndpoint(1)->allocator_; |
| 1970 | int pool_size = 1; |
| 1971 | allocator_1->SetConfiguration(allocator_1->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1972 | allocator_1->turn_servers(), pool_size, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1973 | allocator_2->SetConfiguration(allocator_2->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1974 | allocator_2->turn_servers(), pool_size, false); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1975 | const PortAllocatorSession* pooled_session_1 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1976 | allocator_1->GetPooledSession(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1977 | const PortAllocatorSession* pooled_session_2 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1978 | allocator_2->GetPooledSession(); |
| 1979 | ASSERT_NE(nullptr, pooled_session_1); |
| 1980 | ASSERT_NE(nullptr, pooled_session_2); |
| 1981 | // Wait for the pooled sessions to finish gathering before the |
| 1982 | // P2PTransportChannels try to use them. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1983 | EXPECT_TRUE_SIMULATED_WAIT(pooled_session_1->CandidatesAllocationDone() && |
| 1984 | pooled_session_2->CandidatesAllocationDone(), |
| 1985 | kDefaultTimeout, clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1986 | // Now let the endpoints connect and try exchanging some data. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1987 | CreateChannels(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1988 | EXPECT_TRUE_SIMULATED_WAIT( |
| 1989 | ep1_ch1() != NULL && ep2_ch1() != NULL && ep1_ch1()->receiving() && |
| 1990 | ep1_ch1()->writable() && ep2_ch1()->receiving() && |
| 1991 | ep2_ch1()->writable(), |
| 1992 | kMediumTimeout, clock); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 1993 | TestSendRecv(&clock); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1994 | // Make sure the P2PTransportChannels are actually using ports from the |
| 1995 | // pooled sessions. |
| 1996 | auto pooled_ports_1 = pooled_session_1->ReadyPorts(); |
| 1997 | auto pooled_ports_2 = pooled_session_2->ReadyPorts(); |
| 1998 | EXPECT_NE(pooled_ports_1.end(), |
| 1999 | std::find(pooled_ports_1.begin(), pooled_ports_1.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2000 | ep1_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2001 | EXPECT_NE(pooled_ports_2.end(), |
| 2002 | std::find(pooled_ports_2.begin(), pooled_ports_2.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2003 | ep2_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2006 | // 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] | 2007 | // 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] | 2008 | // as soon as it's created. |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 2009 | // TODO(deadbeef): Move this and other "presumed writable" tests into a test |
| 2010 | // class that operates on a single P2PTransportChannel, once an appropriate one |
| 2011 | // (which supports TURN servers and TURN candidate gathering) is available. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2012 | TEST_F(P2PTransportChannelTest, TurnToTurnPresumedWritable) { |
| 2013 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2014 | kDefaultPortAllocatorFlags); |
| 2015 | // Only configure one channel so we can control when the remote candidate |
| 2016 | // is added. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2017 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2018 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2019 | IceConfig config; |
| 2020 | config.presume_writable_when_fully_relayed = true; |
| 2021 | ep1_ch1()->SetIceConfig(config); |
| 2022 | ep1_ch1()->MaybeStartGathering(); |
| 2023 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 2024 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 2025 | // Add two remote candidates; a host candidate (with higher priority) |
| 2026 | // and TURN candidate. |
| 2027 | ep1_ch1()->AddRemoteCandidate( |
| 2028 | CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 2029 | ep1_ch1()->AddRemoteCandidate( |
| 2030 | CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 0)); |
| 2031 | // Expect that the TURN-TURN candidate pair will be prioritized since it's |
| 2032 | // "probably writable". |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2033 | EXPECT_TRUE(ep1_ch1()->selected_connection() != nullptr); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2034 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2035 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2036 | // Also expect that the channel instantly indicates that it's writable since |
| 2037 | // it has a TURN-TURN pair. |
| 2038 | EXPECT_TRUE(ep1_ch1()->writable()); |
| 2039 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2040 | // Also make sure we can immediately send packets. |
| 2041 | const char* data = "test"; |
| 2042 | int len = static_cast<int>(strlen(data)); |
| 2043 | EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2046 | // Test that a TURN/peer reflexive candidate pair is also presumed writable. |
| 2047 | TEST_F(P2PTransportChannelTest, TurnToPrflxPresumedWritable) { |
| 2048 | rtc::ScopedFakeClock fake_clock; |
| 2049 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 2050 | // We need to add artificial network delay to verify that the connection |
| 2051 | // is presumed writable before it's actually writable. Without this delay |
| 2052 | // it would become writable instantly. |
| 2053 | virtual_socket_server()->set_delay_mean(50); |
| 2054 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2055 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2056 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 2057 | kDefaultPortAllocatorFlags); |
| 2058 | // We want the remote TURN candidate to show up as prflx. To do this we need |
| 2059 | // to configure the server to accept packets from an address we haven't |
| 2060 | // explicitly installed permission for. |
| 2061 | test_turn_server()->set_enable_permission_checks(false); |
| 2062 | IceConfig config; |
| 2063 | config.presume_writable_when_fully_relayed = true; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2064 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2065 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2066 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 2067 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2068 | ep1_ch1()->SetIceConfig(config); |
| 2069 | ep2_ch1()->SetIceConfig(config); |
| 2070 | // Don't signal candidates from channel 2, so that channel 1 sees the TURN |
| 2071 | // candidate as peer reflexive. |
| 2072 | PauseCandidates(1); |
| 2073 | ep1_ch1()->MaybeStartGathering(); |
| 2074 | ep2_ch1()->MaybeStartGathering(); |
| 2075 | |
| 2076 | // Wait for the TURN<->prflx connection. |
| 2077 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2078 | kShortTimeout, fake_clock); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2079 | ASSERT_NE(nullptr, ep1_ch1()->selected_connection()); |
| 2080 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2081 | EXPECT_EQ(PRFLX_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2082 | // Make sure that at this point the connection is only presumed writable, |
| 2083 | // not fully writable. |
| 2084 | EXPECT_FALSE(ep1_ch1()->selected_connection()->writable()); |
| 2085 | |
| 2086 | // Now wait for it to actually become writable. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2087 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->writable(), |
| 2088 | kShortTimeout, fake_clock); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 2089 | |
| 2090 | // Explitly destroy channels, before fake clock is destroyed. |
| 2091 | DestroyChannels(); |
| 2092 | } |
| 2093 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2094 | // Test that a presumed-writable TURN<->TURN connection is preferred above an |
| 2095 | // unreliable connection (one that has failed to be pinged for some time). |
| 2096 | TEST_F(P2PTransportChannelTest, PresumedWritablePreferredOverUnreliable) { |
| 2097 | rtc::ScopedFakeClock fake_clock; |
| 2098 | |
| 2099 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 2100 | kDefaultPortAllocatorFlags); |
| 2101 | IceConfig config; |
| 2102 | config.presume_writable_when_fully_relayed = true; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2103 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2104 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2105 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 2106 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2107 | ep1_ch1()->SetIceConfig(config); |
| 2108 | ep2_ch1()->SetIceConfig(config); |
| 2109 | ep1_ch1()->MaybeStartGathering(); |
| 2110 | ep2_ch1()->MaybeStartGathering(); |
| 2111 | // Wait for initial connection as usual. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2112 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2113 | ep1_ch1()->selected_connection()->writable() && |
| 2114 | ep2_ch1()->receiving() && |
| 2115 | ep2_ch1()->writable() && |
| 2116 | ep2_ch1()->selected_connection()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2117 | kShortTimeout, fake_clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2118 | const Connection* old_selected_connection = ep1_ch1()->selected_connection(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2119 | // Destroy the second channel and wait for the current connection on the |
| 2120 | // first channel to become "unreliable", making it no longer writable. |
| 2121 | GetEndpoint(1)->cd1_.ch_.reset(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2122 | EXPECT_TRUE_SIMULATED_WAIT(!ep1_ch1()->writable(), kDefaultTimeout, |
| 2123 | fake_clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2124 | EXPECT_NE(nullptr, ep1_ch1()->selected_connection()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2125 | // Add a remote TURN candidate. The first channel should still have a TURN |
| 2126 | // port available to make a TURN<->TURN pair that's presumed writable. |
| 2127 | ep1_ch1()->AddRemoteCandidate( |
| 2128 | CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 0)); |
| 2129 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2130 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2131 | EXPECT_TRUE(ep1_ch1()->writable()); |
| 2132 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2133 | EXPECT_NE(old_selected_connection, ep1_ch1()->selected_connection()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2134 | // Explitly destroy channels, before fake clock is destroyed. |
| 2135 | DestroyChannels(); |
| 2136 | } |
| 2137 | |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 2138 | // Ensure that "SignalReadyToSend" is fired as expected with a "presumed |
| 2139 | // writable" connection. Previously this did not work. |
| 2140 | TEST_F(P2PTransportChannelTest, SignalReadyToSendWithPresumedWritable) { |
| 2141 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 2142 | kDefaultPortAllocatorFlags); |
| 2143 | // Only test one endpoint, so we can ensure the connection doesn't receive a |
| 2144 | // binding response and advance beyond being "presumed" writable. |
| 2145 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 2146 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 2147 | IceConfig config; |
| 2148 | config.presume_writable_when_fully_relayed = true; |
| 2149 | ep1_ch1()->SetIceConfig(config); |
| 2150 | ep1_ch1()->MaybeStartGathering(); |
| 2151 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 2152 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 2153 | ep1_ch1()->AddRemoteCandidate( |
| 2154 | CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 0)); |
| 2155 | // Sanity checking the type of the connection. |
| 2156 | EXPECT_TRUE(ep1_ch1()->selected_connection() != nullptr); |
| 2157 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2158 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2159 | |
| 2160 | // Tell the socket server to block packets (returning EWOULDBLOCK). |
| 2161 | virtual_socket_server()->SetSendingBlocked(true); |
| 2162 | const char* data = "test"; |
| 2163 | int len = static_cast<int>(strlen(data)); |
| 2164 | EXPECT_EQ(-1, SendData(ep1_ch1(), data, len)); |
| 2165 | |
| 2166 | // Reset |ready_to_send_| flag, which is set to true if the event fires as it |
| 2167 | // should. |
| 2168 | GetEndpoint(0)->ready_to_send_ = false; |
| 2169 | virtual_socket_server()->SetSendingBlocked(false); |
| 2170 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
| 2171 | EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); |
| 2172 | } |
| 2173 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2174 | // Test what happens when we have 2 users behind the same NAT. This can lead |
| 2175 | // to interesting behavior because the STUN server will only give out the |
| 2176 | // address of the outermost NAT. |
| 2177 | class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase { |
| 2178 | protected: |
| 2179 | void ConfigureEndpoints(Config nat_type, Config config1, Config config2) { |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 2180 | RTC_CHECK_GE(nat_type, NAT_FULL_CONE); |
| 2181 | RTC_CHECK_LE(nat_type, NAT_SYMMETRIC); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2182 | rtc::NATSocketServer::Translator* outer_nat = |
| 2183 | nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0], |
| 2184 | static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE)); |
| 2185 | ConfigureEndpoint(outer_nat, 0, config1); |
| 2186 | ConfigureEndpoint(outer_nat, 1, config2); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2187 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2188 | } |
| 2189 | void ConfigureEndpoint(rtc::NATSocketServer::Translator* nat, |
| 2190 | int endpoint, Config config) { |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 2191 | RTC_CHECK(config <= NAT_SYMMETRIC); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2192 | if (config == OPEN) { |
| 2193 | AddAddress(endpoint, kPrivateAddrs[endpoint]); |
| 2194 | nat->AddClient(kPrivateAddrs[endpoint]); |
| 2195 | } else { |
| 2196 | AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]); |
| 2197 | nat->AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint], |
| 2198 | static_cast<rtc::NATType>(config - NAT_FULL_CONE))->AddClient( |
| 2199 | kCascadedPrivateAddrs[endpoint]); |
| 2200 | } |
| 2201 | } |
| 2202 | }; |
| 2203 | |
| 2204 | TEST_F(P2PTransportChannelSameNatTest, TestConesBehindSameCone) { |
| 2205 | ConfigureEndpoints(NAT_FULL_CONE, NAT_FULL_CONE, NAT_FULL_CONE); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 2206 | Test( |
| 2207 | P2PTransportChannelTestBase::Result("prflx", "udp", "stun", "udp", 1000)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | // Test what happens when we have multiple available pathways. |
| 2211 | // In the future we will try different RTTs and configs for the different |
| 2212 | // interfaces, so that we can simulate a user with Ethernet and VPN networks. |
| 2213 | class P2PTransportChannelMultihomedTest : public P2PTransportChannelTestBase { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2214 | public: |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2215 | const Connection* GetConnectionWithRemoteAddress( |
| 2216 | P2PTransportChannel* channel, |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2217 | const SocketAddress& address) { |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2218 | for (Connection* conn : channel->connections()) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2219 | if (conn->remote_candidate().address().EqualIPs(address)) { |
| 2220 | return conn; |
| 2221 | } |
| 2222 | } |
| 2223 | return nullptr; |
| 2224 | } |
| 2225 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2226 | Connection* GetConnectionWithLocalAddress(P2PTransportChannel* channel, |
| 2227 | const SocketAddress& address) { |
| 2228 | for (Connection* conn : channel->connections()) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2229 | if (conn->local_candidate().address().EqualIPs(address)) { |
| 2230 | return conn; |
| 2231 | } |
| 2232 | } |
| 2233 | return nullptr; |
| 2234 | } |
| 2235 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2236 | Connection* GetConnection(P2PTransportChannel* channel, |
| 2237 | const SocketAddress& local, |
| 2238 | const SocketAddress& remote) { |
| 2239 | for (Connection* conn : channel->connections()) { |
| 2240 | if (conn->local_candidate().address().EqualIPs(local) && |
| 2241 | conn->remote_candidate().address().EqualIPs(remote)) { |
| 2242 | return conn; |
| 2243 | } |
| 2244 | } |
| 2245 | return nullptr; |
| 2246 | } |
| 2247 | |
| 2248 | void DestroyAllButBestConnection(P2PTransportChannel* channel) { |
| 2249 | const Connection* selected_connection = channel->selected_connection(); |
| 2250 | for (Connection* conn : channel->connections()) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2251 | if (conn != selected_connection) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2252 | conn->Destroy(); |
| 2253 | } |
| 2254 | } |
| 2255 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2256 | }; |
| 2257 | |
| 2258 | // Test that we can establish connectivity when both peers are multihomed. |
zhihuang | a3095d0 | 2016-11-10 13:59:46 -0800 | [diff] [blame] | 2259 | TEST_F(P2PTransportChannelMultihomedTest, TestBasic) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2260 | AddAddress(0, kPublicAddrs[0]); |
| 2261 | AddAddress(0, kAlternateAddrs[0]); |
| 2262 | AddAddress(1, kPublicAddrs[1]); |
| 2263 | AddAddress(1, kAlternateAddrs[1]); |
| 2264 | Test(kLocalUdpToLocalUdp); |
| 2265 | } |
| 2266 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2267 | // Test that we can quickly switch links if an interface goes down. |
| 2268 | // The controlled side has two interfaces and one will die. |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2269 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverControlledSide) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2270 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2271 | AddAddress(0, kPublicAddrs[0]); |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2272 | // Simulate failing over from Wi-Fi to cell interface. |
| 2273 | AddAddress(1, kPublicAddrs[1], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2274 | AddAddress(1, kAlternateAddrs[1], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2275 | |
| 2276 | // Use only local ports for simplicity. |
| 2277 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2278 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2279 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2280 | // Make the receiving timeout shorter for testing. |
| 2281 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2282 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2283 | CreateChannels(config, config); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2284 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2285 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2286 | ep2_ch1()->receiving() && |
| 2287 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2288 | kMediumTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2289 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2290 | ep2_ch1()->selected_connection() && |
| 2291 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2292 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2293 | |
| 2294 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2295 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2296 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2297 | // The selected connections may switch, so keep references to them. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2298 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2299 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2300 | // We should detect loss of receiving within 1 second or so. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2301 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2302 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2303 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2304 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2305 | // We should switch over to use the alternate addr on both sides |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2306 | // when we are not receiving. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2307 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2308 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2309 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2310 | EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2311 | EXPECT_TRUE( |
| 2312 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1])); |
| 2313 | EXPECT_TRUE( |
| 2314 | LocalCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[1])); |
| 2315 | |
| 2316 | DestroyChannels(); |
| 2317 | } |
| 2318 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2319 | // Test that we can quickly switch links if an interface goes down. |
| 2320 | // The controlling side has two interfaces and one will die. |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2321 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverControllingSide) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2322 | rtc::ScopedFakeClock clock; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2323 | // Simulate failing over from Wi-Fi to cell interface. |
| 2324 | AddAddress(0, kPublicAddrs[0], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2325 | AddAddress(0, kAlternateAddrs[0], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2326 | AddAddress(1, kPublicAddrs[1]); |
| 2327 | |
| 2328 | // Use only local ports for simplicity. |
| 2329 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2330 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2331 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2332 | // Make the receiving timeout shorter for testing. |
| 2333 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2334 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2335 | CreateChannels(config, config); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2336 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2337 | ep2_ch1()->receiving() && |
| 2338 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2339 | kMediumTimeout, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2340 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2341 | ep2_ch1()->selected_connection() && |
| 2342 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2343 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2344 | |
| 2345 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2346 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2347 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2348 | // The selected connections will switch, so keep references to them. |
| 2349 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2350 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2351 | // We should detect loss of receiving within 1 second or so. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2352 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2353 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2354 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2355 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2356 | // We should switch over to use the alternate addr on both sides |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2357 | // when we are not receiving. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2358 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2359 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2360 | kMediumTimeout, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2361 | EXPECT_TRUE( |
| 2362 | LocalCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0])); |
| 2363 | EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2364 | EXPECT_TRUE( |
| 2365 | RemoteCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[0])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2366 | |
| 2367 | DestroyChannels(); |
| 2368 | } |
| 2369 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2370 | // Tests that we can quickly switch links if an interface goes down when |
| 2371 | // there are many connections. |
| 2372 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverWithManyConnections) { |
| 2373 | rtc::ScopedFakeClock clock; |
| 2374 | test_turn_server()->AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 2375 | RelayServerConfig turn_server(RELAY_TURN); |
| 2376 | turn_server.credentials = kRelayCredentials; |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 2377 | turn_server.ports.push_back(ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP)); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2378 | GetAllocator(0)->AddTurnServer(turn_server); |
| 2379 | GetAllocator(1)->AddTurnServer(turn_server); |
| 2380 | // Enable IPv6 |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 2381 | SetAllocatorFlags( |
| 2382 | 0, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
| 2383 | SetAllocatorFlags( |
| 2384 | 1, PORTALLOCATOR_ENABLE_IPV6 | PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2385 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 2386 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 2387 | |
| 2388 | auto& wifi = kPublicAddrs; |
| 2389 | auto& cellular = kAlternateAddrs; |
| 2390 | auto& wifiIpv6 = kIPv6PublicAddrs; |
| 2391 | auto& cellularIpv6 = kIPv6AlternateAddrs; |
| 2392 | AddAddress(0, wifi[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2393 | AddAddress(0, wifiIpv6[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2394 | AddAddress(0, cellular[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2395 | AddAddress(0, cellularIpv6[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2396 | AddAddress(1, wifi[1], "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2397 | AddAddress(1, wifiIpv6[1], "wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2398 | AddAddress(1, cellular[1], "cellular1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2399 | AddAddress(1, cellularIpv6[1], "cellular1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2400 | |
| 2401 | // Set smaller delay on the TCP TURN server so that TCP TURN candidates |
| 2402 | // will be created in time. |
| 2403 | virtual_socket_server()->SetDelayOnAddress(kTurnTcpIntAddr, 1); |
| 2404 | virtual_socket_server()->SetDelayOnAddress(kTurnUdpExtAddr, 1); |
| 2405 | virtual_socket_server()->set_delay_mean(500); |
| 2406 | virtual_socket_server()->UpdateDelayDistribution(); |
| 2407 | |
| 2408 | // Make the receiving timeout shorter for testing. |
| 2409 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 2410 | // Create channels and let them go writable, as usual. |
| 2411 | CreateChannels(config, config, true /* ice_renomination */); |
| 2412 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2413 | ep2_ch1()->receiving() && |
| 2414 | ep2_ch1()->writable(), |
| 2415 | kMediumTimeout, clock); |
| 2416 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2417 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
| 2418 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifiIpv6[0]) && |
| 2419 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifiIpv6[1]), |
| 2420 | kMediumTimeout, clock); |
| 2421 | |
| 2422 | // Blackhole any traffic to or from the wifi on endpoint 1. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2423 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2424 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifi[0]); |
| 2425 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifiIpv6[0]); |
| 2426 | |
| 2427 | // The selected connections may switch, so keep references to them. |
| 2428 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2429 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2430 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2431 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
| 2432 | kMediumTimeout, clock); |
| 2433 | |
| 2434 | // Per-network best connections will be pinged at relatively higher rate when |
| 2435 | // the selected connection becomes not receiving. |
| 2436 | Connection* per_network_best_connection1 = |
| 2437 | GetConnection(ep1_ch1(), cellularIpv6[0], wifiIpv6[1]); |
| 2438 | ASSERT_NE(nullptr, per_network_best_connection1); |
| 2439 | int64_t last_ping_sent1 = per_network_best_connection1->last_ping_sent(); |
| 2440 | int num_pings_sent1 = per_network_best_connection1->num_pings_sent(); |
| 2441 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2442 | num_pings_sent1 < per_network_best_connection1->num_pings_sent(), |
| 2443 | kMediumTimeout, clock); |
| 2444 | int64_t ping_interval1 = |
| 2445 | (per_network_best_connection1->last_ping_sent() - last_ping_sent1) / |
| 2446 | (per_network_best_connection1->num_pings_sent() - num_pings_sent1); |
| 2447 | constexpr int SCHEDULING_DELAY = 200; |
| 2448 | EXPECT_LT( |
| 2449 | ping_interval1, |
| 2450 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_DELAY); |
| 2451 | |
| 2452 | // It should switch over to use the cellular IPv6 addr on endpoint 1 before |
| 2453 | // it timed out on writing. |
| 2454 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2455 | ep1_ch1()->selected_connection()->receiving() && |
| 2456 | ep2_ch1()->selected_connection()->receiving() && |
| 2457 | RemoteCandidate(ep2_ch1())->address().EqualIPs(cellularIpv6[0]) && |
| 2458 | LocalCandidate(ep1_ch1())->address().EqualIPs(cellularIpv6[0]), |
| 2459 | kMediumTimeout, clock); |
| 2460 | |
| 2461 | DestroyChannels(); |
| 2462 | } |
| 2463 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2464 | // Test that when the controlling side switches the selected connection, |
| 2465 | // the nomination of the selected connection on the controlled side will |
| 2466 | // increase. |
| 2467 | TEST_F(P2PTransportChannelMultihomedTest, TestIceRenomination) { |
| 2468 | rtc::ScopedFakeClock clock; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2469 | // Simulate failing over from Wi-Fi to cell interface. |
| 2470 | AddAddress(0, kPublicAddrs[0], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2471 | AddAddress(0, kAlternateAddrs[0], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2472 | AddAddress(1, kPublicAddrs[1]); |
| 2473 | |
| 2474 | // Use only local ports for simplicity. |
| 2475 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2476 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2477 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2478 | // We want it to set the remote ICE parameters when creating channels. |
| 2479 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2480 | // Make the receiving timeout shorter for testing. |
| 2481 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2482 | // Create channels with ICE renomination and let them go writable as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2483 | CreateChannels(config, config, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2484 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2485 | ep2_ch1()->receiving() && |
| 2486 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2487 | kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2488 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2489 | ep2_ch1()->selected_connection()->remote_nomination() > 0 && |
| 2490 | ep1_ch1()->selected_connection()->acked_nomination() > 0, |
| 2491 | kDefaultTimeout, clock); |
| 2492 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2493 | Connection* selected_connection2 = |
| 2494 | const_cast<Connection*>(ep2_ch1()->selected_connection()); |
| 2495 | uint32_t remote_nomination2 = selected_connection2->remote_nomination(); |
| 2496 | // |selected_connection2| should not be nominated any more since the previous |
| 2497 | // nomination has been acknowledged. |
| 2498 | ConnectSignalNominated(selected_connection2); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2499 | SIMULATED_WAIT(nominated(), kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2500 | EXPECT_FALSE(nominated()); |
| 2501 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2502 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2503 | RTC_LOG(LS_INFO) << "Failing over..."; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2504 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 2505 | |
| 2506 | // The selected connection on the controlling side should switch. |
| 2507 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2508 | ep1_ch1()->selected_connection() != selected_connection1, kMediumTimeout, |
| 2509 | clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2510 | // The connection on the controlled side should be nominated again |
| 2511 | // and have an increased nomination. |
| 2512 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2513 | ep2_ch1()->selected_connection()->remote_nomination() > |
| 2514 | remote_nomination2, |
| 2515 | kDefaultTimeout, clock); |
| 2516 | |
| 2517 | DestroyChannels(); |
| 2518 | } |
| 2519 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2520 | // Test that if an interface fails temporarily and then recovers quickly, |
| 2521 | // the selected connection will not switch. |
| 2522 | // The case that it will switch over to the backup connection if the selected |
| 2523 | // connection does not recover after enough time is covered in |
| 2524 | // TestFailoverControlledSide and TestFailoverControllingSide. |
| 2525 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2526 | TestConnectionSwitchDampeningControlledSide) { |
| 2527 | rtc::ScopedFakeClock clock; |
| 2528 | AddAddress(0, kPublicAddrs[0]); |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2529 | // Simulate failing over from Wi-Fi to cell interface. |
| 2530 | AddAddress(1, kPublicAddrs[1], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2531 | AddAddress(1, kAlternateAddrs[1], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2532 | |
| 2533 | // Use only local ports for simplicity. |
| 2534 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2535 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2536 | |
| 2537 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2538 | CreateChannels(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2539 | |
| 2540 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2541 | ep2_ch1()->receiving() && |
| 2542 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2543 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2544 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2545 | ep2_ch1()->selected_connection() && |
| 2546 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2547 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2548 | |
| 2549 | // Make the receiving timeout shorter for testing. |
| 2550 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 2551 | ep1_ch1()->SetIceConfig(config); |
| 2552 | ep2_ch1()->SetIceConfig(config); |
| 2553 | reset_selected_candidate_pair_switches(); |
| 2554 | |
| 2555 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2556 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2557 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); |
| 2558 | |
| 2559 | // The selected connections may switch, so keep references to them. |
| 2560 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2561 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2562 | // We should detect loss of receiving within 1 second or so. |
| 2563 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2564 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2565 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2566 | // After a short while, the link recovers itself. |
| 2567 | SIMULATED_WAIT(false, 10, clock); |
| 2568 | fw()->ClearRules(); |
| 2569 | |
| 2570 | // We should remain on the public address on both sides and no connection |
| 2571 | // switches should have happened. |
| 2572 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2573 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2574 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2575 | EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2576 | EXPECT_TRUE(LocalCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2577 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 2578 | |
| 2579 | DestroyChannels(); |
| 2580 | } |
| 2581 | |
| 2582 | // Test that if an interface fails temporarily and then recovers quickly, |
| 2583 | // the selected connection will not switch. |
| 2584 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2585 | TestConnectionSwitchDampeningControllingSide) { |
| 2586 | rtc::ScopedFakeClock clock; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 2587 | // Simulate failing over from Wi-Fi to cell interface. |
| 2588 | AddAddress(0, kPublicAddrs[0], "eth0", rtc::ADAPTER_TYPE_WIFI); |
| 2589 | AddAddress(0, kAlternateAddrs[0], "wlan0", rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2590 | AddAddress(1, kPublicAddrs[1]); |
| 2591 | |
| 2592 | // Use only local ports for simplicity. |
| 2593 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2594 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2595 | |
| 2596 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2597 | CreateChannels(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2598 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2599 | ep2_ch1()->receiving() && |
| 2600 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2601 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2602 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2603 | ep2_ch1()->selected_connection() && |
| 2604 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2605 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2606 | |
| 2607 | // Make the receiving timeout shorter for testing. |
| 2608 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 2609 | ep1_ch1()->SetIceConfig(config); |
| 2610 | ep2_ch1()->SetIceConfig(config); |
| 2611 | reset_selected_candidate_pair_switches(); |
| 2612 | |
| 2613 | // Blackhole any traffic to or from the public addrs. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2614 | RTC_LOG(LS_INFO) << "Failing over..."; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2615 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 2616 | // The selected connections may switch, so keep references to them. |
| 2617 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2618 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2619 | // We should detect loss of receiving within 1 second or so. |
| 2620 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2621 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2622 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2623 | // The link recovers after a short while. |
| 2624 | SIMULATED_WAIT(false, 10, clock); |
| 2625 | fw()->ClearRules(); |
| 2626 | |
| 2627 | // We should not switch to the alternate addr on both sides because of the |
| 2628 | // dampening. |
| 2629 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2630 | ep2_ch1()->selected_connection()->receiving(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2631 | kMediumTimeout, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2632 | EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2633 | EXPECT_TRUE(RemoteCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2634 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 2635 | DestroyChannels(); |
| 2636 | } |
| 2637 | |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2638 | // Tests that if the remote side's network failed, it won't cause the local |
| 2639 | // side to switch connections and networks. |
| 2640 | TEST_F(P2PTransportChannelMultihomedTest, TestRemoteFailover) { |
| 2641 | rtc::ScopedFakeClock clock; |
| 2642 | // The interface names are chosen so that |cellular| would have higher |
| 2643 | // candidate priority and higher cost. |
| 2644 | auto& wifi = kPublicAddrs; |
| 2645 | auto& cellular = kAlternateAddrs; |
| 2646 | AddAddress(0, wifi[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2647 | AddAddress(0, cellular[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2648 | AddAddress(1, wifi[1], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2649 | |
| 2650 | // Use only local ports for simplicity. |
| 2651 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2652 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2653 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2654 | CreateChannels(); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2655 | // Make the receiving timeout shorter for testing. |
| 2656 | // Set the backup connection ping interval to 25s. |
| 2657 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE, 25000); |
| 2658 | // Ping the best connection more frequently since we don't have traffic. |
| 2659 | config.stable_writable_connection_ping_interval = 900; |
| 2660 | ep1_ch1()->SetIceConfig(config); |
| 2661 | ep2_ch1()->SetIceConfig(config); |
| 2662 | // Need to wait to make sure the connections on both networks are writable. |
| 2663 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2664 | ep2_ch1()->receiving() && |
| 2665 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2666 | kMediumTimeout, clock); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2667 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2668 | ep1_ch1()->selected_connection() && |
| 2669 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2670 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2671 | kDefaultTimeout, clock); |
| 2672 | Connection* backup_conn = |
| 2673 | GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]); |
| 2674 | ASSERT_NE(nullptr, backup_conn); |
| 2675 | // After a short while, the backup connection will be writable but not |
| 2676 | // receiving because backup connection is pinged at a slower rate. |
| 2677 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2678 | backup_conn->writable() && !backup_conn->receiving(), kDefaultTimeout, |
| 2679 | clock); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2680 | reset_selected_candidate_pair_switches(); |
| 2681 | // Blackhole any traffic to or from the remote WiFi networks. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2682 | RTC_LOG(LS_INFO) << "Failing over..."; |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2683 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifi[1]); |
| 2684 | |
| 2685 | int num_switches = 0; |
| 2686 | SIMULATED_WAIT((num_switches = reset_selected_candidate_pair_switches()) > 0, |
| 2687 | 20000, clock); |
| 2688 | EXPECT_EQ(0, num_switches); |
| 2689 | DestroyChannels(); |
| 2690 | } |
| 2691 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2692 | // Tests that a Wifi-Wifi connection has the highest precedence. |
| 2693 | TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiToWifiConnection) { |
| 2694 | // The interface names are chosen so that |cellular| would have higher |
| 2695 | // candidate priority if it is not for the network type. |
| 2696 | auto& wifi = kAlternateAddrs; |
| 2697 | auto& cellular = kPublicAddrs; |
| 2698 | AddAddress(0, wifi[0], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2699 | AddAddress(0, cellular[0], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2700 | AddAddress(1, wifi[1], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2701 | AddAddress(1, cellular[1], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2702 | |
| 2703 | // Use only local ports for simplicity. |
| 2704 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2705 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2706 | |
| 2707 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2708 | CreateChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2709 | |
| 2710 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2711 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2712 | 1000, 1000); |
| 2713 | // 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] | 2714 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2715 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2716 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2717 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2718 | EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2719 | LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]) && |
| 2720 | RemoteCandidate(ep2_ch1())->address().EqualIPs(wifi[0]), |
| 2721 | 1000); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2722 | DestroyChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | // Tests that a Wifi-Cellular connection has higher precedence than |
| 2726 | // a Cellular-Cellular connection. |
| 2727 | TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiOverCellularNetwork) { |
| 2728 | // The interface names are chosen so that |cellular| would have higher |
| 2729 | // candidate priority if it is not for the network type. |
| 2730 | auto& wifi = kAlternateAddrs; |
| 2731 | auto& cellular = kPublicAddrs; |
| 2732 | AddAddress(0, cellular[0], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2733 | AddAddress(1, wifi[1], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2734 | AddAddress(1, cellular[1], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2735 | |
| 2736 | // Use only local ports for simplicity. |
| 2737 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2738 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2739 | |
| 2740 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2741 | CreateChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2742 | |
| 2743 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2744 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2745 | 1000, 1000); |
| 2746 | // 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] | 2747 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2748 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2749 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2750 | EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2751 | LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]), |
| 2752 | 1000); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2753 | DestroyChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2754 | } |
| 2755 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2756 | // Test that the backup connection is pinged at a rate no faster than |
| 2757 | // what was configured. |
| 2758 | TEST_F(P2PTransportChannelMultihomedTest, TestPingBackupConnectionRate) { |
| 2759 | AddAddress(0, kPublicAddrs[0]); |
| 2760 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2761 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2762 | AddAddress(1, kAlternateAddrs[1]); |
| 2763 | AddAddress(1, kPublicAddrs[1]); |
| 2764 | |
| 2765 | // Use only local ports for simplicity. |
| 2766 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2767 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2768 | |
| 2769 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2770 | CreateChannels(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2771 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2772 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2773 | 1000, 1000); |
| 2774 | int backup_ping_interval = 2000; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2775 | ep2_ch1()->SetIceConfig( |
| 2776 | CreateIceConfig(2000, GATHER_ONCE, backup_ping_interval)); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2777 | // After the state becomes COMPLETED, the backup connection will be pinged |
| 2778 | // once every |backup_ping_interval| milliseconds. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2779 | ASSERT_TRUE_WAIT(ep2_ch1()->GetState() == IceTransportState::STATE_COMPLETED, |
| 2780 | 1000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2781 | const std::vector<Connection*>& connections = ep2_ch1()->connections(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2782 | ASSERT_EQ(2U, connections.size()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2783 | Connection* backup_conn = connections[1]; |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2784 | EXPECT_TRUE_WAIT(backup_conn->writable(), kMediumTimeout); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2785 | int64_t last_ping_response_ms = backup_conn->last_ping_response_received(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2786 | EXPECT_TRUE_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2787 | last_ping_response_ms < backup_conn->last_ping_response_received(), |
| 2788 | kDefaultTimeout); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2789 | int time_elapsed = |
| 2790 | backup_conn->last_ping_response_received() - last_ping_response_ms; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2791 | RTC_LOG(LS_INFO) << "Time elapsed: " << time_elapsed; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2792 | EXPECT_GE(time_elapsed, backup_ping_interval); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2793 | |
| 2794 | DestroyChannels(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2795 | } |
| 2796 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2797 | TEST_F(P2PTransportChannelMultihomedTest, TestGetState) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2798 | rtc::ScopedFakeClock clock; |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2799 | AddAddress(0, kAlternateAddrs[0]); |
| 2800 | AddAddress(0, kPublicAddrs[0]); |
| 2801 | AddAddress(1, kPublicAddrs[1]); |
| 2802 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2803 | CreateChannels(); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2804 | |
| 2805 | // Both transport channels will reach STATE_COMPLETED quickly. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2806 | EXPECT_EQ_SIMULATED_WAIT(IceTransportState::STATE_COMPLETED, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2807 | ep1_ch1()->GetState(), kShortTimeout, clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2808 | EXPECT_EQ_SIMULATED_WAIT(IceTransportState::STATE_COMPLETED, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2809 | ep2_ch1()->GetState(), kShortTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2812 | // Tests that when a network interface becomes inactive, if Continual Gathering |
| 2813 | // policy is GATHER_CONTINUALLY, the ports associated with that network |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2814 | // will be removed from the port list of the channel, and the respective |
| 2815 | // remote candidates on the other participant will be removed eventually. |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2816 | TEST_F(P2PTransportChannelMultihomedTest, TestNetworkBecomesInactive) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2817 | rtc::ScopedFakeClock clock; |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2818 | AddAddress(0, kPublicAddrs[0]); |
| 2819 | AddAddress(1, kPublicAddrs[1]); |
| 2820 | // Create channels and let them go writable, as usual. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2821 | IceConfig ep1_config = CreateIceConfig(2000, GATHER_CONTINUALLY); |
| 2822 | IceConfig ep2_config = CreateIceConfig(2000, GATHER_ONCE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2823 | CreateChannels(ep1_config, ep2_config); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2824 | |
| 2825 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2826 | SetAllocatorFlags(1, kOnlyLocalPorts); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2827 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2828 | ep2_ch1()->receiving() && |
| 2829 | ep2_ch1()->writable(), |
| 2830 | kDefaultTimeout, clock); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2831 | // More than one port has been created. |
| 2832 | EXPECT_LE(1U, ep1_ch1()->ports().size()); |
| 2833 | // Endpoint 1 enabled continual gathering; the port will be removed |
| 2834 | // when the interface is removed. |
| 2835 | RemoveAddress(0, kPublicAddrs[0]); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2836 | EXPECT_TRUE(ep1_ch1()->ports().empty()); |
| 2837 | // The remote candidates will be removed eventually. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2838 | EXPECT_TRUE_SIMULATED_WAIT(ep2_ch1()->remote_candidates().empty(), 1000, |
| 2839 | clock); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2840 | |
| 2841 | size_t num_ports = ep2_ch1()->ports().size(); |
| 2842 | EXPECT_LE(1U, num_ports); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2843 | size_t num_remote_candidates = ep1_ch1()->remote_candidates().size(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2844 | // Endpoint 2 did not enable continual gathering; the local port will still be |
| 2845 | // removed when the interface is removed but the remote candidates on the |
| 2846 | // other participant will not be removed. |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2847 | RemoveAddress(1, kPublicAddrs[1]); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2848 | |
| 2849 | EXPECT_EQ_SIMULATED_WAIT(0U, ep2_ch1()->ports().size(), kDefaultTimeout, |
| 2850 | clock); |
| 2851 | SIMULATED_WAIT(0U == ep1_ch1()->remote_candidates().size(), 500, clock); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2852 | EXPECT_EQ(num_remote_candidates, ep1_ch1()->remote_candidates().size()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2853 | |
| 2854 | DestroyChannels(); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2857 | // Tests that continual gathering will create new connections when a new |
| 2858 | // interface is added. |
| 2859 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2860 | TestContinualGatheringOnNewInterface) { |
| 2861 | auto& wifi = kAlternateAddrs; |
| 2862 | auto& cellular = kPublicAddrs; |
| 2863 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2864 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2865 | // Set continual gathering policy. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2866 | IceConfig continual_gathering_config = |
| 2867 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2868 | CreateChannels(continual_gathering_config, continual_gathering_config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2869 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2870 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2871 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2872 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2873 | kDefaultTimeout, kDefaultTimeout); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2874 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2875 | // Add a new wifi interface on end point 2. We should expect a new connection |
| 2876 | // to be created and the new one will be the best connection. |
| 2877 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2878 | const Connection* conn; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2879 | EXPECT_TRUE_WAIT((conn = ep1_ch1()->selected_connection()) != nullptr && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2880 | conn->remote_candidate().address().EqualIPs(wifi[1]), |
| 2881 | kDefaultTimeout); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2882 | EXPECT_TRUE_WAIT((conn = ep2_ch1()->selected_connection()) != nullptr && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2883 | conn->local_candidate().address().EqualIPs(wifi[1]), |
| 2884 | kDefaultTimeout); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2885 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2886 | // Add a new cellular interface on end point 1, we should expect a new |
| 2887 | // backup connection created using this new interface. |
| 2888 | AddAddress(0, cellular[0], "test_cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2889 | EXPECT_TRUE_WAIT( |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2890 | ep1_ch1()->GetState() == IceTransportState::STATE_COMPLETED && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2891 | (conn = GetConnectionWithLocalAddress(ep1_ch1(), cellular[0])) != |
| 2892 | nullptr && |
| 2893 | conn != ep1_ch1()->selected_connection() && conn->writable(), |
| 2894 | kDefaultTimeout); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2895 | EXPECT_TRUE_WAIT( |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2896 | ep2_ch1()->GetState() == IceTransportState::STATE_COMPLETED && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2897 | (conn = GetConnectionWithRemoteAddress(ep2_ch1(), cellular[0])) != |
| 2898 | nullptr && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2899 | conn != ep2_ch1()->selected_connection() && conn->receiving(), |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2900 | kDefaultTimeout); |
| 2901 | |
| 2902 | DestroyChannels(); |
| 2903 | } |
| 2904 | |
| 2905 | // Tests that we can switch links via continual gathering. |
| 2906 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2907 | TestSwitchLinksViaContinualGathering) { |
| 2908 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2909 | AddAddress(0, kPublicAddrs[0]); |
| 2910 | AddAddress(1, kPublicAddrs[1]); |
| 2911 | // Use only local ports for simplicity. |
| 2912 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2913 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2914 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2915 | // Set continual gathering policy. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2916 | IceConfig continual_gathering_config = |
| 2917 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 2918 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2919 | CreateChannels(continual_gathering_config, continual_gathering_config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2920 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2921 | ep2_ch1()->receiving() && |
| 2922 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2923 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2924 | EXPECT_TRUE( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2925 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2926 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2927 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2928 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2929 | // Add the new address first and then remove the other one. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2930 | RTC_LOG(LS_INFO) << "Draining..."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2931 | AddAddress(1, kAlternateAddrs[1]); |
| 2932 | RemoveAddress(1, kPublicAddrs[1]); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2933 | // We should switch to use the alternate address after an exchange of pings. |
| 2934 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2935 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2936 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2937 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1]), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2938 | kMediumTimeout, clock); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2939 | |
| 2940 | // Remove one address first and then add another address. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2941 | RTC_LOG(LS_INFO) << "Draining again..."; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2942 | RemoveAddress(1, kAlternateAddrs[1]); |
| 2943 | AddAddress(1, kAlternateAddrs[0]); |
| 2944 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2945 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2946 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2947 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0]), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2948 | kMediumTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2949 | |
| 2950 | DestroyChannels(); |
| 2951 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2952 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2953 | /* |
| 2954 | TODO(honghaiz) Once continual gathering fully supports |
| 2955 | GATHER_CONTINUALLY_AND_RECOVER, put this test back. |
| 2956 | |
| 2957 | // Tests that if the backup connections are lost and then the interface with the |
| 2958 | // selected connection is gone, continual gathering will restore the |
| 2959 | // connectivity. |
| 2960 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2961 | TestBackupConnectionLostThenInterfaceGone) { |
| 2962 | rtc::ScopedFakeClock clock; |
| 2963 | auto& wifi = kAlternateAddrs; |
| 2964 | auto& cellular = kPublicAddrs; |
| 2965 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2966 | AddAddress(0, cellular[0], "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2967 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2968 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2969 | // Use only local ports for simplicity. |
| 2970 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2971 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2972 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2973 | // Set continual gathering policy. |
| 2974 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY_AND_RECOVER); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2975 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2976 | CreateChannels(config, config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2977 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2978 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2979 | kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2980 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2981 | ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2982 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2983 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1])); |
| 2984 | |
| 2985 | // First destroy all backup connection. |
| 2986 | DestroyAllButBestConnection(ep1_ch1()); |
| 2987 | |
| 2988 | SIMULATED_WAIT(false, 10, clock); |
| 2989 | // Then the interface of the best connection goes away. |
| 2990 | RemoveAddress(0, wifi[0]); |
| 2991 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2992 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2993 | LocalCandidate(ep1_ch1())->address().EqualIPs(cellular[0]) && |
| 2994 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2995 | kMediumTimeout, clock); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2996 | |
| 2997 | DestroyChannels(); |
| 2998 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2999 | */ |
| 3000 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3001 | // Tests that the backup connection will be restored after it is destroyed. |
| 3002 | TEST_F(P2PTransportChannelMultihomedTest, TestRestoreBackupConnection) { |
| 3003 | rtc::ScopedFakeClock clock; |
| 3004 | auto& wifi = kAlternateAddrs; |
| 3005 | auto& cellular = kPublicAddrs; |
| 3006 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 3007 | AddAddress(0, cellular[0], "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 3008 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 3009 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
| 3010 | // Use only local ports for simplicity. |
| 3011 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 3012 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 3013 | |
| 3014 | // Create channels and let them go writable, as usual. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3015 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 3016 | config.regather_on_failed_networks_interval = 2000; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 3017 | CreateChannels(config, config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3018 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 3019 | ep2_ch1()->receiving() && |
| 3020 | ep2_ch1()->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3021 | kMediumTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3022 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 3023 | ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3024 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 3025 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1])); |
| 3026 | |
| 3027 | // Destroy all backup connections. |
| 3028 | DestroyAllButBestConnection(ep1_ch1()); |
| 3029 | // Ensure the backup connection is removed first. |
| 3030 | EXPECT_TRUE_SIMULATED_WAIT( |
| 3031 | GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]) == nullptr, |
| 3032 | kDefaultTimeout, clock); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3033 | const Connection* conn; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3034 | EXPECT_TRUE_SIMULATED_WAIT( |
| 3035 | (conn = GetConnectionWithLocalAddress(ep1_ch1(), cellular[0])) != |
| 3036 | nullptr && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3037 | conn != ep1_ch1()->selected_connection() && conn->writable(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3038 | kDefaultTimeout, clock); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3039 | |
| 3040 | DestroyChannels(); |
| 3041 | } |
| 3042 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3043 | // A collection of tests which tests a single P2PTransportChannel by sending |
| 3044 | // pings. |
| 3045 | class P2PTransportChannelPingTest : public testing::Test, |
| 3046 | public sigslot::has_slots<> { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3047 | public: |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3048 | P2PTransportChannelPingTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 3049 | : vss_(new rtc::VirtualSocketServer()), thread_(vss_.get()) {} |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3050 | |
| 3051 | protected: |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3052 | void PrepareChannel(P2PTransportChannel* ch) { |
| 3053 | ch->SetIceRole(ICEROLE_CONTROLLING); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3054 | ch->SetIceParameters(kIceParams[0]); |
| 3055 | ch->SetRemoteIceParameters(kIceParams[1]); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3056 | ch->SignalNetworkRouteChanged.connect( |
| 3057 | this, &P2PTransportChannelPingTest::OnNetworkRouteChanged); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3058 | ch->SignalReadyToSend.connect(this, |
| 3059 | &P2PTransportChannelPingTest::OnReadyToSend); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3060 | ch->SignalStateChanged.connect( |
| 3061 | this, &P2PTransportChannelPingTest::OnChannelStateChanged); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3062 | } |
| 3063 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3064 | Connection* WaitForConnectionTo(P2PTransportChannel* ch, |
| 3065 | const std::string& ip, |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3066 | int port_num, |
| 3067 | rtc::FakeClock* clock = nullptr) { |
| 3068 | if (clock == nullptr) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3069 | EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, |
| 3070 | kMediumTimeout); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3071 | } else { |
| 3072 | EXPECT_TRUE_SIMULATED_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3073 | kMediumTimeout, *clock); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3074 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3075 | return GetConnectionTo(ch, ip, port_num); |
| 3076 | } |
| 3077 | |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3078 | Port* GetPort(P2PTransportChannel* ch) { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3079 | if (ch->ports().empty()) { |
| 3080 | return nullptr; |
| 3081 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3082 | return static_cast<Port*>(ch->ports()[0]); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3085 | Port* GetPrunedPort(P2PTransportChannel* ch) { |
| 3086 | if (ch->pruned_ports().empty()) { |
| 3087 | return nullptr; |
| 3088 | } |
| 3089 | return static_cast<Port*>(ch->pruned_ports()[0]); |
| 3090 | } |
| 3091 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3092 | Connection* GetConnectionTo(P2PTransportChannel* ch, |
| 3093 | const std::string& ip, |
| 3094 | int port_num) { |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3095 | Port* port = GetPort(ch); |
| 3096 | if (!port) { |
| 3097 | return nullptr; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3098 | } |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3099 | return port->GetConnection(rtc::SocketAddress(ip, port_num)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3102 | Connection* FindNextPingableConnectionAndPingIt(P2PTransportChannel* ch) { |
| 3103 | Connection* conn = ch->FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3104 | if (conn) { |
| 3105 | ch->MarkConnectionPinged(conn); |
| 3106 | } |
| 3107 | return conn; |
| 3108 | } |
| 3109 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3110 | int SendData(IceTransportInternal* channel, |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3111 | const char* data, |
| 3112 | size_t len, |
| 3113 | int packet_id) { |
| 3114 | rtc::PacketOptions options; |
| 3115 | options.packet_id = packet_id; |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3116 | return channel->SendPacket(data, len, options, 0); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3117 | } |
| 3118 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3119 | Connection* CreateConnectionWithCandidate(P2PTransportChannel* channel, |
| 3120 | rtc::ScopedFakeClock* clock, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3121 | const std::string& ip_addr, |
| 3122 | int port, |
| 3123 | int priority, |
| 3124 | bool writable) { |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3125 | channel->AddRemoteCandidate( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3126 | CreateUdpCandidate(LOCAL_PORT_TYPE, ip_addr, port, priority)); |
| 3127 | EXPECT_TRUE_SIMULATED_WAIT( |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3128 | GetConnectionTo(channel, ip_addr, port) != nullptr, kMediumTimeout, |
| 3129 | *clock); |
| 3130 | Connection* conn = GetConnectionTo(channel, ip_addr, port); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3131 | |
| 3132 | if (conn && writable) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3133 | conn->ReceivedPingResponse(LOW_RTT, "id"); // make it writable |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3134 | } |
| 3135 | return conn; |
| 3136 | } |
| 3137 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3138 | void NominateConnection(Connection* conn, uint32_t remote_nomination = 1U) { |
| 3139 | conn->set_remote_nomination(remote_nomination); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3140 | conn->SignalNominated(conn); |
| 3141 | } |
| 3142 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3143 | void OnNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute> network_route) { |
| 3144 | last_network_route_ = network_route; |
| 3145 | if (last_network_route_) { |
| 3146 | last_sent_packet_id_ = last_network_route_->last_sent_packet_id; |
| 3147 | } |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3148 | ++selected_candidate_pair_switches_; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3149 | } |
| 3150 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3151 | void ReceivePingOnConnection(Connection* conn, |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3152 | const std::string& remote_ufrag, |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3153 | int priority, |
| 3154 | uint32_t nomination = 0) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3155 | IceMessage msg; |
| 3156 | msg.SetType(STUN_BINDING_REQUEST); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3157 | msg.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3158 | STUN_ATTR_USERNAME, |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3159 | conn->local_candidate().username() + ":" + remote_ufrag)); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3160 | msg.AddAttribute( |
| 3161 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, priority)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3162 | if (nomination != 0) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3163 | msg.AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 3164 | STUN_ATTR_NOMINATION, nomination)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3165 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3166 | msg.SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3167 | msg.AddMessageIntegrity(conn->local_candidate().password()); |
| 3168 | msg.AddFingerprint(); |
| 3169 | rtc::ByteBufferWriter buf; |
| 3170 | msg.Write(&buf); |
| 3171 | conn->OnReadPacket(buf.Data(), buf.Length(), rtc::CreatePacketTime(0)); |
| 3172 | } |
| 3173 | |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 3174 | void OnReadyToSend(rtc::PacketTransportInternal* transport) { |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3175 | channel_ready_to_send_ = true; |
| 3176 | } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3177 | void OnChannelStateChanged(IceTransportInternal* channel) { |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3178 | channel_state_ = channel->GetState(); |
| 3179 | } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3180 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3181 | int last_sent_packet_id() { return last_sent_packet_id_; } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3182 | bool channel_ready_to_send() { return channel_ready_to_send_; } |
| 3183 | void reset_channel_ready_to_send() { channel_ready_to_send_ = false; } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3184 | IceTransportState channel_state() { return channel_state_; } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3185 | int reset_selected_candidate_pair_switches() { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3186 | int switches = selected_candidate_pair_switches_; |
| 3187 | selected_candidate_pair_switches_ = 0; |
| 3188 | return switches; |
| 3189 | } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3190 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3191 | // Return true if the |pair| matches the last network route. |
| 3192 | bool CandidatePairMatchesNetworkRoute(CandidatePairInterface* pair) { |
| 3193 | if (!pair) { |
| 3194 | return !last_network_route_.has_value(); |
| 3195 | } else { |
| 3196 | return pair->local_candidate().network_id() == |
| 3197 | last_network_route_->local_network_id && |
| 3198 | pair->remote_candidate().network_id() == |
| 3199 | last_network_route_->remote_network_id; |
| 3200 | } |
| 3201 | } |
| 3202 | |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3203 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 3204 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 3205 | rtc::AutoSocketServerThread thread_; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3206 | int selected_candidate_pair_switches_ = 0; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3207 | int last_sent_packet_id_ = -1; |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3208 | bool channel_ready_to_send_ = false; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3209 | IceTransportState channel_state_ = IceTransportState::STATE_INIT; |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3210 | rtc::Optional<rtc::NetworkRoute> last_network_route_; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3211 | }; |
| 3212 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3213 | TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3214 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3215 | P2PTransportChannel ch("trigger checks", 1, &pa); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3216 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3217 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3218 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3219 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3220 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3221 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3222 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3223 | ASSERT_TRUE(conn1 != nullptr); |
| 3224 | ASSERT_TRUE(conn2 != nullptr); |
| 3225 | |
| 3226 | // Before a triggered check, the first connection to ping is the |
| 3227 | // highest priority one. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3228 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3229 | |
| 3230 | // Receiving a ping causes a triggered check which should make conn1 |
| 3231 | // be pinged first instead of conn2, even though conn2 has a higher |
| 3232 | // priority. |
| 3233 | conn1->ReceivedPing(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3234 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3235 | } |
| 3236 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3237 | TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3238 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3239 | P2PTransportChannel ch("ping sufficiently", 1, &pa); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3240 | PrepareChannel(&ch); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3241 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3242 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3243 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3244 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3245 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3246 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3247 | ASSERT_TRUE(conn1 != nullptr); |
| 3248 | ASSERT_TRUE(conn2 != nullptr); |
| 3249 | |
| 3250 | // Low-priority connection becomes writable so that the other connection |
| 3251 | // is not pruned. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3252 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 3253 | EXPECT_TRUE_WAIT( |
| 3254 | conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL && |
| 3255 | conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, |
| 3256 | kDefaultTimeout); |
| 3257 | } |
| 3258 | |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3259 | // Verify that the connections are pinged at the right time. |
| 3260 | TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) { |
| 3261 | rtc::ScopedFakeClock clock; |
| 3262 | int RTT_RATIO = 4; |
| 3263 | int SCHEDULING_RANGE = 200; |
| 3264 | int RTT_RANGE = 10; |
| 3265 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3266 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3267 | P2PTransportChannel ch("TestChannel", 1, &pa); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3268 | PrepareChannel(&ch); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3269 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3270 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3271 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3272 | |
| 3273 | ASSERT_TRUE(conn != nullptr); |
| 3274 | SIMULATED_WAIT(conn->num_pings_sent() == 1, kDefaultTimeout, clock); |
| 3275 | |
| 3276 | // Initializing. |
| 3277 | |
| 3278 | int64_t start = clock.TimeNanos(); |
| 3279 | SIMULATED_WAIT(conn->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, |
| 3280 | kDefaultTimeout, clock); |
| 3281 | int64_t ping_interval_ms = (clock.TimeNanos() - start) / |
| 3282 | rtc::kNumNanosecsPerMillisec / |
| 3283 | (MIN_PINGS_AT_WEAK_PING_INTERVAL - 1); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3284 | EXPECT_EQ(ping_interval_ms, WEAK_PING_INTERVAL); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3285 | |
| 3286 | // Stabilizing. |
| 3287 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3288 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3289 | int ping_sent_before = conn->num_pings_sent(); |
| 3290 | start = clock.TimeNanos(); |
| 3291 | // The connection becomes strong but not stable because we haven't been able |
| 3292 | // to converge the RTT. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3293 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3294 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3295 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3296 | EXPECT_GE(ping_interval_ms, |
| 3297 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3298 | EXPECT_LE( |
| 3299 | ping_interval_ms, |
| 3300 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3301 | |
| 3302 | // Stabilized. |
| 3303 | |
| 3304 | // The connection becomes stable after receiving more than RTT_RATIO rtt |
| 3305 | // samples. |
| 3306 | for (int i = 0; i < RTT_RATIO; i++) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3307 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3308 | } |
| 3309 | ping_sent_before = conn->num_pings_sent(); |
| 3310 | start = clock.TimeNanos(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3311 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3312 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3313 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3314 | EXPECT_GE(ping_interval_ms, |
| 3315 | STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3316 | EXPECT_LE( |
| 3317 | ping_interval_ms, |
| 3318 | STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3319 | |
| 3320 | // Destabilized. |
| 3321 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3322 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3323 | // Create a in-flight ping. |
| 3324 | conn->Ping(clock.TimeNanos() / rtc::kNumNanosecsPerMillisec); |
| 3325 | start = clock.TimeNanos(); |
| 3326 | // In-flight ping timeout and the connection will be unstable. |
| 3327 | SIMULATED_WAIT( |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3328 | !conn->stable(clock.TimeNanos() / rtc::kNumNanosecsPerMillisec), |
| 3329 | kMediumTimeout, clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3330 | int64_t duration_ms = |
| 3331 | (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
| 3332 | EXPECT_GE(duration_ms, 2 * conn->rtt() - RTT_RANGE); |
| 3333 | EXPECT_LE(duration_ms, 2 * conn->rtt() + RTT_RANGE); |
| 3334 | // The connection become unstable due to not receiving ping responses. |
| 3335 | ping_sent_before = conn->num_pings_sent(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3336 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3337 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3338 | // The interval is expected to be |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3339 | // WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL. |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3340 | start = clock.TimeNanos(); |
| 3341 | ping_sent_before = conn->num_pings_sent(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3342 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, kMediumTimeout, |
| 3343 | clock); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3344 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 3345 | EXPECT_GE(ping_interval_ms, |
| 3346 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3347 | EXPECT_LE( |
| 3348 | ping_interval_ms, |
| 3349 | WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3352 | // 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] | 3353 | // parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3354 | TEST_F(P2PTransportChannelPingTest, PingingStartedAsSoonAsPossible) { |
| 3355 | rtc::ScopedFakeClock clock; |
| 3356 | |
| 3357 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3358 | P2PTransportChannel ch("TestChannel", 1, &pa); |
| 3359 | ch.SetIceRole(ICEROLE_CONTROLLING); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3360 | ch.SetIceParameters(kIceParams[0]); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3361 | ch.MaybeStartGathering(); |
| 3362 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, ch.gathering_state(), |
| 3363 | kDefaultTimeout); |
| 3364 | |
| 3365 | // Simulate a binding request being received, creating a peer reflexive |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3366 | // candidate pair while we still don't have remote ICE parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3367 | IceMessage request; |
| 3368 | request.SetType(STUN_BINDING_REQUEST); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3369 | request.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
| 3370 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3371 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3372 | request.AddAttribute( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3373 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, prflx_priority)); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3374 | Port* port = GetPort(&ch); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3375 | ASSERT_NE(nullptr, port); |
| 3376 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3377 | &request, kIceUfrag[1], false); |
| 3378 | Connection* conn = GetConnectionTo(&ch, "1.1.1.1", 1); |
| 3379 | ASSERT_NE(nullptr, conn); |
| 3380 | |
| 3381 | // 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] | 3382 | // sent, since we don't yet have remote ICE parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3383 | SIMULATED_WAIT(conn->num_pings_sent() > 0, 1025, clock); |
| 3384 | EXPECT_EQ(0, conn->num_pings_sent()); |
| 3385 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3386 | // 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] | 3387 | // the first ping is sent as soon as possible, within one simulated clock |
| 3388 | // tick. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3389 | ch.SetRemoteIceParameters(kIceParams[1]); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3390 | EXPECT_TRUE_SIMULATED_WAIT(conn->num_pings_sent() > 0, 1, clock); |
| 3391 | } |
| 3392 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3393 | TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3394 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3395 | P2PTransportChannel ch("trigger checks", 1, &pa); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3396 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3397 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3398 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3399 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3400 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3401 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3402 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3403 | ASSERT_TRUE(conn1 != nullptr); |
| 3404 | ASSERT_TRUE(conn2 != nullptr); |
| 3405 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3406 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
| 3407 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3408 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3409 | ASSERT_TRUE(conn1->writable()); |
| 3410 | conn1->ReceivedPing(); |
| 3411 | |
| 3412 | // Ping received, but the connection is already writable, so no |
| 3413 | // "triggered check" and conn2 is pinged before conn1 because it has |
| 3414 | // a higher priority. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3415 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3416 | } |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3417 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3418 | TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3419 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3420 | P2PTransportChannel ch("Do not ping failed connections", 1, &pa); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3421 | PrepareChannel(&ch); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3422 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3423 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3424 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3425 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3426 | ASSERT_TRUE(conn1 != nullptr); |
| 3427 | |
| 3428 | EXPECT_EQ(conn1, ch.FindNextPingableConnection()); |
| 3429 | conn1->Prune(); // A pruned connection may still be pingable. |
| 3430 | EXPECT_EQ(conn1, ch.FindNextPingableConnection()); |
| 3431 | conn1->FailAndPrune(); |
| 3432 | EXPECT_TRUE(nullptr == ch.FindNextPingableConnection()); |
| 3433 | } |
| 3434 | |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3435 | TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3436 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3437 | P2PTransportChannel ch("state change", 1, &pa); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3438 | PrepareChannel(&ch); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3439 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3440 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3441 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3442 | ASSERT_TRUE(conn1 != nullptr); |
| 3443 | // Pruning the connection reduces the set of active connections and changes |
| 3444 | // the channel state. |
| 3445 | conn1->Prune(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 3446 | EXPECT_EQ_WAIT(IceTransportState::STATE_FAILED, channel_state(), |
| 3447 | kDefaultTimeout); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3448 | } |
| 3449 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3450 | // Test adding remote candidates with different ufrags. If a remote candidate |
| 3451 | // is added with an old ufrag, it will be discarded. If it is added with a |
| 3452 | // ufrag that was not seen before, it will be used to create connections |
| 3453 | // 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] | 3454 | // parameters arrive. If a remote candidate is added with the current ICE |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3455 | // ufrag, its pwd and generation will be set properly. |
| 3456 | TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3457 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3458 | P2PTransportChannel ch("add candidate", 1, &pa); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3459 | PrepareChannel(&ch); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3460 | ch.MaybeStartGathering(); |
| 3461 | // Add a candidate with a future ufrag. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3462 | ch.AddRemoteCandidate( |
| 3463 | CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1, kIceUfrag[2])); |
| 3464 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3465 | ASSERT_TRUE(conn1 != nullptr); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3466 | const Candidate& candidate = conn1->remote_candidate(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3467 | EXPECT_EQ(kIceUfrag[2], candidate.username()); |
| 3468 | EXPECT_TRUE(candidate.password().empty()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3469 | EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3470 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3471 | // Set the remote ICE parameters with the "future" ufrag. |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3472 | // This should set the ICE pwd in the remote candidate of |conn1|, making |
| 3473 | // it pingable. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3474 | ch.SetRemoteIceParameters(kIceParams[2]); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3475 | EXPECT_EQ(kIceUfrag[2], candidate.username()); |
| 3476 | EXPECT_EQ(kIcePwd[2], candidate.password()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3477 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3478 | |
| 3479 | // Add a candidate with an old ufrag. No connection will be created. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3480 | ch.AddRemoteCandidate( |
| 3481 | CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2, kIceUfrag[1])); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3482 | rtc::Thread::Current()->ProcessMessages(500); |
| 3483 | EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr); |
| 3484 | |
| 3485 | // Add a candidate with the current ufrag, its pwd and generation will be |
| 3486 | // assigned, even if the generation is not set. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3487 | ch.AddRemoteCandidate( |
| 3488 | CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 0, kIceUfrag[2])); |
| 3489 | Connection* conn3 = nullptr; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3490 | ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr, |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3491 | kMediumTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3492 | const Candidate& new_candidate = conn3->remote_candidate(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3493 | EXPECT_EQ(kIcePwd[2], new_candidate.password()); |
| 3494 | EXPECT_EQ(1U, new_candidate.generation()); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3495 | |
| 3496 | // Check that the pwd of all remote candidates are properly assigned. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3497 | for (const RemoteCandidate& candidate : ch.remote_candidates()) { |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3498 | EXPECT_TRUE(candidate.username() == kIceUfrag[1] || |
| 3499 | candidate.username() == kIceUfrag[2]); |
| 3500 | if (candidate.username() == kIceUfrag[1]) { |
| 3501 | EXPECT_EQ(kIcePwd[1], candidate.password()); |
| 3502 | } else if (candidate.username() == kIceUfrag[2]) { |
| 3503 | EXPECT_EQ(kIcePwd[2], candidate.password()); |
| 3504 | } |
| 3505 | } |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3506 | } |
| 3507 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3508 | TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3509 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3510 | P2PTransportChannel ch("connection resurrection", 1, &pa); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3511 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3512 | ch.MaybeStartGathering(); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3513 | |
| 3514 | // Create conn1 and keep track of original candidate priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3515 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3516 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3517 | ASSERT_TRUE(conn1 != nullptr); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 3518 | uint32_t remote_priority = conn1->remote_candidate().priority(); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3519 | |
| 3520 | // Create a higher priority candidate and make the connection |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 3521 | // receiving/writable. This will prune conn1. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3522 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
| 3523 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3524 | ASSERT_TRUE(conn2 != nullptr); |
| 3525 | conn2->ReceivedPing(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3526 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3527 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3528 | // Wait for conn1 to be pruned. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 3529 | EXPECT_TRUE_WAIT(conn1->pruned(), kMediumTimeout); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3530 | // Destroy the connection to test SignalUnknownAddress. |
| 3531 | conn1->Destroy(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3532 | EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, |
| 3533 | kMediumTimeout); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3534 | |
| 3535 | // Create a minimal STUN message with prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3536 | IceMessage request; |
| 3537 | request.SetType(STUN_BINDING_REQUEST); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3538 | request.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
| 3539 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3540 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3541 | request.AddAttribute( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3542 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, prflx_priority)); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3543 | EXPECT_NE(prflx_priority, remote_priority); |
| 3544 | |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3545 | Port* port = GetPort(&ch); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3546 | // conn1 should be resurrected with original priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3547 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3548 | &request, kIceUfrag[1], false); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3549 | conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3550 | ASSERT_TRUE(conn1 != nullptr); |
| 3551 | EXPECT_EQ(conn1->remote_candidate().priority(), remote_priority); |
| 3552 | |
| 3553 | // conn3, a real prflx connection, should have prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3554 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 1), PROTO_UDP, |
| 3555 | &request, kIceUfrag[1], false); |
| 3556 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 1); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3557 | ASSERT_TRUE(conn3 != nullptr); |
| 3558 | EXPECT_EQ(conn3->remote_candidate().priority(), prflx_priority); |
| 3559 | } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3560 | |
| 3561 | TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3562 | rtc::ScopedFakeClock clock; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3563 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3564 | P2PTransportChannel ch("receiving state change", 1, &pa); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3565 | PrepareChannel(&ch); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3566 | // Default receiving timeout and checking receiving interval should not be too |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3567 | // small. |
| 3568 | EXPECT_LE(1000, ch.receiving_timeout()); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3569 | EXPECT_LE(200, ch.check_receiving_interval()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3570 | ch.SetIceConfig(CreateIceConfig(500, GATHER_ONCE)); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3571 | EXPECT_EQ(500, ch.receiving_timeout()); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3572 | EXPECT_EQ(50, ch.check_receiving_interval()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3573 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3574 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3575 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3576 | ASSERT_TRUE(conn1 != nullptr); |
| 3577 | |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3578 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3579 | conn1->ReceivedPing(); |
| 3580 | conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3581 | EXPECT_TRUE_SIMULATED_WAIT(ch.receiving(), kShortTimeout, clock); |
| 3582 | EXPECT_TRUE_SIMULATED_WAIT(!ch.receiving(), kShortTimeout, clock); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3583 | } |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3584 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3585 | // The controlled side will select a connection as the "selected connection" |
| 3586 | // based on priority until the controlling side nominates a connection, at which |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3587 | // point the controlled side will select that connection as the |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3588 | // "selected connection". Plus, SignalNetworkRouteChanged will be fired if the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3589 | // selected connection changes and SignalReadyToSend will be fired if the new |
| 3590 | // selected connection is writable. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3591 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3592 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3593 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3594 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3595 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3596 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3597 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3598 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3599 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3600 | // Channel is not ready to send because it is not writable. |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3601 | EXPECT_FALSE(channel_ready_to_send()); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3602 | int last_packet_id = 0; |
| 3603 | const char* data = "ABCDEFGH"; |
| 3604 | int len = static_cast<int>(strlen(data)); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3605 | EXPECT_EQ(-1, SendData(&ch, data, len, ++last_packet_id)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3606 | EXPECT_EQ(-1, last_sent_packet_id()); |
| 3607 | |
| 3608 | // A connection needs to be writable before it is selected for transmission. |
| 3609 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
| 3610 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3611 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3612 | EXPECT_EQ(len, SendData(&ch, data, len, ++last_packet_id)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3613 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3614 | // When a higher priority candidate comes in, the new connection is chosen |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3615 | // as the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3616 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); |
| 3617 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3618 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3619 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
| 3620 | EXPECT_EQ_WAIT(conn2, ch.selected_connection(), kDefaultTimeout); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3621 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3622 | EXPECT_TRUE(channel_ready_to_send()); |
| 3623 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3624 | |
| 3625 | // If a stun request with use-candidate attribute arrives, the receiving |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3626 | // connection will be set as the selected connection, even though |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3627 | // its priority is lower. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3628 | EXPECT_EQ(len, SendData(&ch, data, len, ++last_packet_id)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3629 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 1)); |
| 3630 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3631 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3632 | // Because it has a lower priority, the selected connection is still conn2. |
| 3633 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3634 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3635 | // 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] | 3636 | // connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3637 | NominateConnection(conn3); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3638 | EXPECT_EQ(conn3, ch.selected_connection()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3639 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn3)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3640 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3641 | EXPECT_TRUE(channel_ready_to_send()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3642 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3643 | // Even if another higher priority candidate arrives, it will not be set as |
| 3644 | // the selected connection because the selected connection is nominated by |
| 3645 | // the controlling side. |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3646 | EXPECT_EQ(len, SendData(&ch, data, len, ++last_packet_id)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3647 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "4.4.4.4", 4, 100)); |
| 3648 | Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3649 | ASSERT_TRUE(conn4 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3650 | EXPECT_EQ(conn3, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3651 | // 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] | 3652 | // the selected connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3653 | NominateConnection(conn4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3654 | // Not switched yet because conn4 is not writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3655 | EXPECT_EQ(conn3, ch.selected_connection()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3656 | reset_channel_ready_to_send(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3657 | // The selected connection switches after conn4 becomes writable. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3658 | conn4->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3659 | EXPECT_EQ_WAIT(conn4, ch.selected_connection(), kDefaultTimeout); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3660 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn4)); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3661 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3662 | // SignalReadyToSend is fired again because conn4 is writable. |
| 3663 | EXPECT_TRUE(channel_ready_to_send()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3666 | // The controlled side will select a connection as the "selected connection" |
| 3667 | // based on requests from an unknown address before the controlling side |
| 3668 | // nominates a connection, and will nominate a connection from an unknown |
| 3669 | // address if the request contains the use_candidate attribute. Plus, it will |
| 3670 | // also sends back a ping response and set the ICE pwd in the remote candidate |
| 3671 | // appropriately. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3672 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3673 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3674 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3675 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3676 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3677 | ch.MaybeStartGathering(); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3678 | // A minimal STUN message with prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3679 | IceMessage request; |
| 3680 | request.SetType(STUN_BINDING_REQUEST); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3681 | request.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
| 3682 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3683 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3684 | request.AddAttribute( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3685 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, prflx_priority)); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3686 | TestUDPPort* port = static_cast<TestUDPPort*>(GetPort(&ch)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3687 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3688 | &request, kIceUfrag[1], false); |
| 3689 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3690 | ASSERT_TRUE(conn1 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3691 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3692 | EXPECT_NE(conn1, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3693 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3694 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3695 | port->set_sent_binding_response(false); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3696 | |
| 3697 | // Another connection is nominated via use_candidate. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3698 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3699 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3700 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3701 | // Because it has a lower priority, the selected connection is still conn1. |
| 3702 | EXPECT_EQ(conn1, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3703 | // 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] | 3704 | // selected connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3705 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
| 3706 | NominateConnection(conn2); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3707 | EXPECT_EQ(conn2, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3708 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3709 | // Another request with unknown address, it will not be set as the selected |
| 3710 | // connection because the selected connection was nominated by the controlling |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3711 | // side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3712 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP, |
| 3713 | &request, kIceUfrag[1], false); |
| 3714 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3715 | ASSERT_TRUE(conn3 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3716 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3717 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3718 | EXPECT_EQ(conn2, ch.selected_connection()); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3719 | port->set_sent_binding_response(false); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3720 | |
| 3721 | // However if the request contains use_candidate attribute, it will be |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3722 | // selected as the selected connection. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3723 | request.AddAttribute( |
| 3724 | rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3725 | port->SignalUnknownAddress(port, rtc::SocketAddress("4.4.4.4", 4), PROTO_UDP, |
| 3726 | &request, kIceUfrag[1], false); |
| 3727 | Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3728 | ASSERT_TRUE(conn4 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3729 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3730 | // conn4 is not the selected connection yet because it is not writable. |
| 3731 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3732 | conn4->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3733 | EXPECT_EQ_WAIT(conn4, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3734 | |
| 3735 | // Test that the request from an unknown address contains a ufrag from an old |
| 3736 | // generation. |
| 3737 | port->set_sent_binding_response(false); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3738 | ch.SetRemoteIceParameters(kIceParams[2]); |
| 3739 | ch.SetRemoteIceParameters(kIceParams[3]); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3740 | port->SignalUnknownAddress(port, rtc::SocketAddress("5.5.5.5", 5), PROTO_UDP, |
| 3741 | &request, kIceUfrag[2], false); |
| 3742 | Connection* conn5 = WaitForConnectionTo(&ch, "5.5.5.5", 5); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3743 | ASSERT_TRUE(conn5 != nullptr); |
| 3744 | EXPECT_TRUE(port->sent_binding_response()); |
| 3745 | EXPECT_EQ(kIcePwd[2], conn5->remote_candidate().password()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3748 | // The controlled side will select a connection as the "selected connection" |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3749 | // based on media received until the controlling side nominates a connection, |
| 3750 | // at which point the controlled side will select that connection as |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3751 | // the "selected connection". |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3752 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3753 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3754 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3755 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3756 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3757 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3758 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 10)); |
| 3759 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3760 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3761 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
| 3762 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3763 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3764 | // If a data packet is received on conn2, the selected connection should |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3765 | // switch to conn2 because the controlled side must mirror the media path |
| 3766 | // chosen by the controlling side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3767 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3768 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3769 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3770 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable and receiving. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3771 | conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3772 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3773 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3774 | |
| 3775 | // Now another STUN message with an unknown address and use_candidate will |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3776 | // nominate the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3777 | IceMessage request; |
| 3778 | request.SetType(STUN_BINDING_REQUEST); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3779 | request.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>( |
| 3780 | STUN_ATTR_USERNAME, kIceUfrag[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3781 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3782 | request.AddAttribute( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 3783 | rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_PRIORITY, prflx_priority)); |
| 3784 | request.AddAttribute( |
| 3785 | rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 3786 | Port* port = GetPort(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3787 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP, |
| 3788 | &request, kIceUfrag[1], false); |
| 3789 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3790 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3791 | EXPECT_EQ(conn2, ch.selected_connection()); // Not writable yet. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3792 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3793 | EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3794 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3795 | // Now another data packet will not switch the selected connection because the |
| 3796 | // selected connection was nominated by the controlling side. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3797 | conn2->ReceivedPing(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3798 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3799 | conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3800 | EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); |
| 3801 | } |
| 3802 | |
| 3803 | TEST_F(P2PTransportChannelPingTest, |
| 3804 | TestControlledAgentDataReceivingTakesHigherPrecedenceThanPriority) { |
| 3805 | rtc::ScopedFakeClock clock; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3806 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3807 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3808 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 3809 | PrepareChannel(&ch); |
| 3810 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3811 | ch.MaybeStartGathering(); |
| 3812 | // The connections have decreasing priority. |
| 3813 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3814 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3815 | ASSERT_TRUE(conn1 != nullptr); |
| 3816 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3817 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3818 | ASSERT_TRUE(conn2 != nullptr); |
| 3819 | |
| 3820 | // Initially, connections are selected based on priority. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3821 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3822 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3823 | |
| 3824 | // conn2 receives data; it becomes selected. |
| 3825 | // Advance the clock by 1ms so that the last data receiving timestamp of |
| 3826 | // conn2 is larger. |
| 3827 | SIMULATED_WAIT(false, 1, clock); |
| 3828 | conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3829 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3830 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3831 | |
| 3832 | // conn1 also receives data; it becomes selected due to priority again. |
| 3833 | conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3834 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3835 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3836 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3837 | // conn2 received data more recently; it is selected now because it |
| 3838 | // received data more recently. |
| 3839 | SIMULATED_WAIT(false, 1, clock); |
| 3840 | // Need to become writable again because it was pruned. |
| 3841 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
| 3842 | conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
| 3843 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3844 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3845 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3846 | // Make sure sorting won't reselect candidate pair. |
| 3847 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3848 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | TEST_F(P2PTransportChannelPingTest, |
| 3852 | TestControlledAgentNominationTakesHigherPrecedenceThanDataReceiving) { |
| 3853 | rtc::ScopedFakeClock clock; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3854 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3855 | |
| 3856 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3857 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 3858 | PrepareChannel(&ch); |
| 3859 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3860 | ch.MaybeStartGathering(); |
| 3861 | // The connections have decreasing priority. |
| 3862 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3863 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3864 | ASSERT_TRUE(conn1 != nullptr); |
| 3865 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3866 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3867 | ASSERT_TRUE(conn2 != nullptr); |
| 3868 | |
| 3869 | // conn1 received data; it is the selected connection. |
| 3870 | // Advance the clock to have a non-zero last-data-receiving time. |
| 3871 | SIMULATED_WAIT(false, 1, clock); |
| 3872 | conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3873 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3874 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3875 | |
| 3876 | // conn2 is nominated; it becomes the selected connection. |
| 3877 | NominateConnection(conn2); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3878 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3879 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3880 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3881 | // conn1 is selected because it has higher priority and also nominated. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3882 | NominateConnection(conn1); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3883 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3884 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3885 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3886 | // Make sure sorting won't reselect candidate pair. |
| 3887 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3888 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | TEST_F(P2PTransportChannelPingTest, |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3892 | TestControlledAgentSelectsConnectionWithHigherNomination) { |
| 3893 | rtc::ScopedFakeClock clock; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3894 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3895 | |
| 3896 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3897 | P2PTransportChannel ch("test", 1, &pa); |
| 3898 | PrepareChannel(&ch); |
| 3899 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 3900 | ch.MaybeStartGathering(); |
| 3901 | // The connections have decreasing priority. |
| 3902 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3903 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3904 | ASSERT_TRUE(conn1 != nullptr); |
| 3905 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3906 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3907 | ASSERT_TRUE(conn2 != nullptr); |
| 3908 | |
| 3909 | // conn1 is the selected connection because it has a higher priority, |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3910 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 3911 | clock); |
| 3912 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3913 | reset_selected_candidate_pair_switches(); |
| 3914 | |
| 3915 | // conn2 is nominated; it becomes selected. |
| 3916 | NominateConnection(conn2); |
| 3917 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3918 | EXPECT_EQ(conn2, ch.selected_connection()); |
| 3919 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3920 | |
| 3921 | // conn1 is selected because of its priority. |
| 3922 | NominateConnection(conn1); |
| 3923 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3924 | EXPECT_EQ(conn1, ch.selected_connection()); |
| 3925 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3926 | |
| 3927 | // conn2 gets higher remote nomination; it is selected again. |
| 3928 | NominateConnection(conn2, 2U); |
| 3929 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3930 | EXPECT_EQ(conn2, ch.selected_connection()); |
| 3931 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3932 | |
| 3933 | // Make sure sorting won't reselect candidate pair. |
| 3934 | SIMULATED_WAIT(false, 100, clock); |
| 3935 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 3936 | } |
| 3937 | |
| 3938 | TEST_F(P2PTransportChannelPingTest, |
| 3939 | TestControlledAgentIgnoresSmallerNomination) { |
| 3940 | rtc::ScopedFakeClock clock; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 3941 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
| 3942 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3943 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3944 | P2PTransportChannel ch("test", 1, &pa); |
| 3945 | PrepareChannel(&ch); |
| 3946 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 3947 | ch.MaybeStartGathering(); |
| 3948 | Connection* conn = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3949 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, false); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3950 | ReceivePingOnConnection(conn, kIceUfrag[1], 1, 2U); |
| 3951 | EXPECT_EQ(2U, conn->remote_nomination()); |
| 3952 | // Smaller nomination is ignored. |
| 3953 | ReceivePingOnConnection(conn, kIceUfrag[1], 1, 1U); |
| 3954 | EXPECT_EQ(2U, conn->remote_nomination()); |
| 3955 | } |
| 3956 | |
| 3957 | TEST_F(P2PTransportChannelPingTest, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3958 | TestControlledAgentWriteStateTakesHigherPrecedenceThanNomination) { |
| 3959 | rtc::ScopedFakeClock clock; |
| 3960 | |
| 3961 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3962 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 3963 | PrepareChannel(&ch); |
| 3964 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3965 | ch.MaybeStartGathering(); |
| 3966 | // The connections have decreasing priority. |
| 3967 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3968 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 10, false); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3969 | ASSERT_TRUE(conn1 != nullptr); |
| 3970 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 3971 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 9, false); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3972 | ASSERT_TRUE(conn2 != nullptr); |
| 3973 | |
| 3974 | NominateConnection(conn1); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3975 | // There is no selected connection because no connection is writable. |
| 3976 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3977 | |
| 3978 | // conn2 becomes writable; it is selected even though it is not nominated. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3979 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3980 | EXPECT_EQ_SIMULATED_WAIT(1, reset_selected_candidate_pair_switches(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3981 | kDefaultTimeout, clock); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3982 | EXPECT_EQ_SIMULATED_WAIT(conn2, ch.selected_connection(), kDefaultTimeout, |
| 3983 | clock); |
| 3984 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn2)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3985 | |
| 3986 | // If conn1 is also writable, it will become selected. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3987 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3988 | EXPECT_EQ_SIMULATED_WAIT(1, reset_selected_candidate_pair_switches(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3989 | kDefaultTimeout, clock); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 3990 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 3991 | clock); |
| 3992 | EXPECT_TRUE(CandidatePairMatchesNetworkRoute(conn1)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3993 | |
| 3994 | // Make sure sorting won't reselect candidate pair. |
| 3995 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3996 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3997 | } |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3998 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3999 | // Test that if a new remote candidate has the same address and port with |
| 4000 | // an old one, it will be used to create a new connection. |
| 4001 | TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4002 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4003 | P2PTransportChannel ch("candidate reuse", 1, &pa); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4004 | PrepareChannel(&ch); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4005 | ch.MaybeStartGathering(); |
| 4006 | const std::string host_address = "1.1.1.1"; |
| 4007 | const int port_num = 1; |
| 4008 | |
| 4009 | // kIceUfrag[1] is the current generation ufrag. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4010 | Candidate candidate = CreateUdpCandidate(LOCAL_PORT_TYPE, host_address, |
| 4011 | port_num, 1, kIceUfrag[1]); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4012 | ch.AddRemoteCandidate(candidate); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4013 | Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4014 | ASSERT_TRUE(conn1 != nullptr); |
| 4015 | EXPECT_EQ(0u, conn1->remote_candidate().generation()); |
| 4016 | |
| 4017 | // Simply adding the same candidate again won't create a new connection. |
| 4018 | ch.AddRemoteCandidate(candidate); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4019 | Connection* conn2 = GetConnectionTo(&ch, host_address, port_num); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4020 | EXPECT_EQ(conn1, conn2); |
| 4021 | |
| 4022 | // Update the ufrag of the candidate and add it again. |
| 4023 | candidate.set_username(kIceUfrag[2]); |
| 4024 | ch.AddRemoteCandidate(candidate); |
| 4025 | conn2 = GetConnectionTo(&ch, host_address, port_num); |
| 4026 | EXPECT_NE(conn1, conn2); |
| 4027 | EXPECT_EQ(kIceUfrag[2], conn2->remote_candidate().username()); |
| 4028 | EXPECT_EQ(1u, conn2->remote_candidate().generation()); |
| 4029 | |
| 4030 | // Verify that a ping with the new ufrag can be received on the new |
| 4031 | // connection. |
| 4032 | EXPECT_EQ(0, conn2->last_ping_received()); |
| 4033 | ReceivePingOnConnection(conn2, kIceUfrag[2], 1 /* priority */); |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 4034 | EXPECT_GT(conn2->last_ping_received(), 0); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 4035 | } |
| 4036 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4037 | // When the current selected connection is strong, lower-priority connections |
| 4038 | // will be pruned. Otherwise, lower-priority connections are kept. |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4039 | TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4040 | rtc::ScopedFakeClock clock; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4041 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4042 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4043 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4044 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4045 | ch.SetIceRole(ICEROLE_CONTROLLED); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4046 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4047 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4048 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4049 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4050 | EXPECT_EQ(nullptr, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4051 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4052 | |
| 4053 | // When a higher-priority, nominated candidate comes in, the connections with |
| 4054 | // lower-priority are pruned. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4055 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4056 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4057 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4058 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 4059 | NominateConnection(conn2); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4060 | EXPECT_TRUE_SIMULATED_WAIT(conn1->pruned(), kMediumTimeout, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4061 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4062 | ch.SetIceConfig(CreateIceConfig(500, GATHER_ONCE)); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4063 | // Wait until conn2 becomes not receiving. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4064 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->receiving(), kMediumTimeout, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4065 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4066 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 1)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4067 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3, &clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4068 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4069 | // The selected connection should still be conn2. Even through conn3 has lower |
| 4070 | // priority and is not receiving/writable, it is not pruned because the |
| 4071 | // selected connection is not receiving. |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4072 | SIMULATED_WAIT(conn3->pruned(), kShortTimeout, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 4073 | EXPECT_FALSE(conn3->pruned()); |
| 4074 | } |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4075 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4076 | TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { |
| 4077 | rtc::ScopedFakeClock clock; |
| 4078 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4079 | P2PTransportChannel ch("test channel", 1, &pa); |
| 4080 | PrepareChannel(&ch); |
| 4081 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4082 | ch.MaybeStartGathering(); |
| 4083 | Connection* conn1 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 4084 | CreateConnectionWithCandidate(&ch, &clock, "1.1.1.1", 1, 100, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4085 | ASSERT_TRUE(conn1 != nullptr); |
| 4086 | Connection* conn2 = |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 4087 | CreateConnectionWithCandidate(&ch, &clock, "2.2.2.2", 2, 200, false); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4088 | ASSERT_TRUE(conn2 != nullptr); |
| 4089 | // Even if conn1 is writable, nominated, receiving data, it should not prune |
| 4090 | // conn2. |
| 4091 | NominateConnection(conn1); |
| 4092 | SIMULATED_WAIT(false, 1, clock); |
| 4093 | conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
| 4094 | SIMULATED_WAIT(conn2->pruned(), 100, clock); |
| 4095 | EXPECT_FALSE(conn2->pruned()); |
| 4096 | } |
| 4097 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4098 | // Test that GetState returns the state correctly. |
| 4099 | TEST_F(P2PTransportChannelPingTest, TestGetState) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4100 | rtc::ScopedFakeClock clock; |
| 4101 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4102 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4103 | P2PTransportChannel ch("test channel", 1, &pa); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4104 | PrepareChannel(&ch); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4105 | ch.MaybeStartGathering(); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4106 | EXPECT_EQ(IceTransportState::STATE_INIT, ch.GetState()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4107 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 4108 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4109 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
| 4110 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4111 | ASSERT_TRUE(conn1 != nullptr); |
| 4112 | ASSERT_TRUE(conn2 != nullptr); |
| 4113 | // Now there are two connections, so the transport channel is connecting. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4114 | EXPECT_EQ(IceTransportState::STATE_CONNECTING, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4115 | // |conn1| becomes writable and receiving; it then should prune |conn2|. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4116 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4117 | EXPECT_TRUE_SIMULATED_WAIT(conn2->pruned(), kShortTimeout, clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4118 | EXPECT_EQ(IceTransportState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4119 | conn1->Prune(); // All connections are pruned. |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 4120 | // Need to wait until the channel state is updated. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4121 | EXPECT_EQ_SIMULATED_WAIT(IceTransportState::STATE_FAILED, ch.GetState(), |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4122 | kShortTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | // Test that when a low-priority connection is pruned, it is not deleted |
| 4126 | // right away, and it can become active and be pruned again. |
| 4127 | TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4128 | rtc::ScopedFakeClock clock; |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4129 | clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
| 4130 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4131 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4132 | P2PTransportChannel ch("test channel", 1, &pa); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4133 | PrepareChannel(&ch); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4134 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 4135 | config.receiving_switching_delay = 800; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 4136 | ch.SetIceConfig(config); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4137 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4138 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4139 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4140 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4141 | EXPECT_EQ(nullptr, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4142 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4143 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 4144 | clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4145 | |
| 4146 | // 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] | 4147 | // not be deleted right away. Once the current selected connection becomes not |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4148 | // receiving, |conn2| will start to ping and upon receiving the ping response, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4149 | // it will become the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4150 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4151 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4152 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4153 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->active(), kDefaultTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4154 | // |conn2| should not send a ping yet. |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 4155 | EXPECT_EQ(IceCandidatePairState::WAITING, conn2->state()); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4156 | EXPECT_EQ(IceTransportState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4157 | // Wait for |conn1| becoming not receiving. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4158 | EXPECT_TRUE_SIMULATED_WAIT(!conn1->receiving(), kMediumTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4159 | // Make sure conn2 is not deleted. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4160 | conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4161 | ASSERT_TRUE(conn2 != nullptr); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 4162 | EXPECT_EQ_SIMULATED_WAIT(IceCandidatePairState::IN_PROGRESS, conn2->state(), |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4163 | kDefaultTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4164 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4165 | EXPECT_EQ_SIMULATED_WAIT(conn2, ch.selected_connection(), kDefaultTimeout, |
| 4166 | clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4167 | EXPECT_EQ(IceTransportState::STATE_CONNECTING, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4168 | |
| 4169 | // When |conn1| comes back again, |conn2| will be pruned again. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4170 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 4171 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 4172 | clock); |
| 4173 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->active(), kDefaultTimeout, clock); |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 4174 | EXPECT_EQ(IceTransportState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 4175 | } |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4176 | |
| 4177 | // Test that if all connections in a channel has timed out on writing, they |
| 4178 | // will all be deleted. We use Prune to simulate write_time_out. |
| 4179 | TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4180 | rtc::ScopedFakeClock clock; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4181 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4182 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4183 | PrepareChannel(&ch); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4184 | ch.MaybeStartGathering(); |
| 4185 | // Have one connection only but later becomes write-time-out. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4186 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4187 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4188 | ASSERT_TRUE(conn1 != nullptr); |
| 4189 | conn1->ReceivedPing(); // Becomes receiving |
| 4190 | conn1->Prune(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4191 | EXPECT_TRUE_SIMULATED_WAIT(ch.connections().empty(), kShortTimeout, clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4192 | |
| 4193 | // Have two connections but both become write-time-out later. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4194 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4195 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4196 | ASSERT_TRUE(conn2 != nullptr); |
| 4197 | conn2->ReceivedPing(); // Becomes receiving |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4198 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 2)); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4199 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3, &clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4200 | ASSERT_TRUE(conn3 != nullptr); |
| 4201 | conn3->ReceivedPing(); // Becomes receiving |
| 4202 | // Now prune both conn2 and conn3; they will be deleted soon. |
| 4203 | conn2->Prune(); |
| 4204 | conn3->Prune(); |
honghaiz | e58d73d | 2016-10-24 16:38:26 -0700 | [diff] [blame] | 4205 | EXPECT_TRUE_SIMULATED_WAIT(ch.connections().empty(), kShortTimeout, clock); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 4206 | } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4207 | |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4208 | // Tests that after a port allocator session is started, it will be stopped |
| 4209 | // when a new connection becomes writable and receiving. Also tests that if a |
| 4210 | // connection belonging to an old session becomes writable, it won't stop |
| 4211 | // the current port allocator session. |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4212 | TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4213 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4214 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4215 | PrepareChannel(&ch); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4216 | ch.SetIceConfig(CreateIceConfig(2000, GATHER_ONCE)); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4217 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4218 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 4219 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4220 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4221 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4222 | EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); |
| 4223 | |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4224 | // Start a new session. Even though conn1, which belongs to an older |
| 4225 | // session, becomes unwritable and writable again, it should not stop the |
| 4226 | // current session. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4227 | ch.SetIceParameters(kIceParams[1]); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4228 | ch.MaybeStartGathering(); |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4229 | conn1->Prune(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4230 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 4231 | EXPECT_TRUE(ch.allocator_session()->IsGettingPorts()); |
| 4232 | |
| 4233 | // But if a new connection created from the new session becomes writable, |
| 4234 | // it will stop the current session. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4235 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 100)); |
| 4236 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4237 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4238 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 4239 | EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); |
| 4240 | } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4241 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4242 | // Test that the ICE role is updated even on ports that has been removed. |
| 4243 | // These ports may still have connections that need a correct role, in case that |
| 4244 | // the connections on it may still receive stun pings. |
| 4245 | TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnRemovedPort) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4246 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4247 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4248 | // Starts with ICEROLE_CONTROLLING. |
| 4249 | PrepareChannel(&ch); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4250 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4251 | ch.SetIceConfig(config); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4252 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4253 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4254 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4255 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4256 | ASSERT_TRUE(conn != nullptr); |
| 4257 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 4258 | // Make a fake signal to remove the ports in the p2ptransportchannel. then |
| 4259 | // change the ICE role and expect it to be updated. |
| 4260 | std::vector<PortInterface*> ports(1, conn->port()); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 4261 | ch.allocator_session()->SignalPortsPruned(ch.allocator_session(), ports); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4262 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 4263 | EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | // Test that the ICE role is updated even on ports with inactive networks. |
| 4267 | // These ports may still have connections that need a correct role, for the |
| 4268 | // pings sent by those connections until they're replaced by newer-generation |
| 4269 | // connections. |
| 4270 | TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4271 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4272 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4273 | // Starts with ICEROLE_CONTROLLING. |
| 4274 | PrepareChannel(&ch); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4275 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4276 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4277 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4278 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4279 | ASSERT_TRUE(conn != nullptr); |
| 4280 | |
| 4281 | // Do an ICE restart, change the role, and expect the old port to have its |
| 4282 | // role updated. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 4283 | ch.SetIceParameters(kIceParams[1]); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4284 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4285 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 4286 | EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | // Test that after some amount of time without receiving data, the connection |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4290 | // will be destroyed. The port will only be destroyed after it is marked as |
| 4291 | // "pruned." |
| 4292 | TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeoutAndPruned) { |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4293 | rtc::ScopedFakeClock fake_clock; |
| 4294 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4295 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 4296 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4297 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4298 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4299 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4300 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4301 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4302 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4303 | ASSERT_TRUE(conn != nullptr); |
| 4304 | |
| 4305 | // Simulate 2 minutes going by. This should be enough time for the port to |
| 4306 | // time out. |
| 4307 | for (int second = 0; second < 120; ++second) { |
| 4308 | fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
| 4309 | } |
| 4310 | EXPECT_EQ(nullptr, GetConnectionTo(&ch, "1.1.1.1", 1)); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4311 | // Port will not be removed because it is not pruned yet. |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 4312 | PortInterface* port = GetPort(&ch); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4313 | ASSERT_NE(nullptr, port); |
| 4314 | |
| 4315 | // If the session prunes all ports, the port will be destroyed. |
| 4316 | ch.allocator_session()->PruneAllPorts(); |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 4317 | EXPECT_EQ_SIMULATED_WAIT(nullptr, GetPort(&ch), 1, fake_clock); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4318 | EXPECT_EQ_SIMULATED_WAIT(nullptr, GetPrunedPort(&ch), 1, fake_clock); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4321 | class P2PTransportChannelMostLikelyToWorkFirstTest |
| 4322 | : public P2PTransportChannelPingTest { |
| 4323 | public: |
| 4324 | P2PTransportChannelMostLikelyToWorkFirstTest() |
| 4325 | : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) { |
| 4326 | network_manager_.AddInterface(kPublicAddrs[0]); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 4327 | allocator_.reset( |
| 4328 | CreateBasicPortAllocator(&network_manager_, ServerAddresses(), |
| 4329 | kTurnUdpIntAddr, rtc::SocketAddress())); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4330 | allocator_->set_flags(allocator_->flags() | PORTALLOCATOR_DISABLE_STUN | |
| 4331 | PORTALLOCATOR_DISABLE_TCP); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4332 | allocator_->set_step_delay(kMinimumStepDelay); |
| 4333 | } |
| 4334 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4335 | P2PTransportChannel& StartTransportChannel( |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4336 | bool prioritize_most_likely_to_work, |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 4337 | int stable_writable_connection_ping_interval) { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 4338 | channel_.reset(new P2PTransportChannel("checks", 1, allocator())); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4339 | IceConfig config = channel_->config(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4340 | config.prioritize_most_likely_candidate_pairs = |
| 4341 | prioritize_most_likely_to_work; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 4342 | config.stable_writable_connection_ping_interval = |
| 4343 | stable_writable_connection_ping_interval; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4344 | channel_->SetIceConfig(config); |
| 4345 | PrepareChannel(channel_.get()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4346 | channel_->MaybeStartGathering(); |
| 4347 | return *channel_.get(); |
| 4348 | } |
| 4349 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4350 | BasicPortAllocator* allocator() { return allocator_.get(); } |
| 4351 | TestTurnServer* turn_server() { return &turn_server_; } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4352 | |
| 4353 | // This verifies the next pingable connection has the expected candidates' |
| 4354 | // types and, for relay local candidate, the expected relay protocol and ping |
| 4355 | // it. |
| 4356 | void VerifyNextPingableConnection( |
| 4357 | const std::string& local_candidate_type, |
| 4358 | const std::string& remote_candidate_type, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4359 | const std::string& relay_protocol_type = UDP_PROTOCOL_NAME) { |
| 4360 | Connection* conn = FindNextPingableConnectionAndPingIt(channel_.get()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4361 | EXPECT_EQ(conn->local_candidate().type(), local_candidate_type); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4362 | if (conn->local_candidate().type() == RELAY_PORT_TYPE) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4363 | EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type); |
| 4364 | } |
| 4365 | EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type); |
| 4366 | } |
| 4367 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4368 | private: |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4369 | std::unique_ptr<BasicPortAllocator> allocator_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4370 | rtc::FakeNetworkManager network_manager_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4371 | TestTurnServer turn_server_; |
| 4372 | std::unique_ptr<P2PTransportChannel> channel_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4373 | }; |
| 4374 | |
| 4375 | // Test that Relay/Relay connections will be pinged first when no other |
| 4376 | // 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] | 4377 | // we have a selected connection. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4378 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4379 | TestRelayRelayFirstWhenNothingPingedYet) { |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4380 | const int max_strong_interval = 100; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4381 | P2PTransportChannel& ch = StartTransportChannel(true, max_strong_interval); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4382 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4383 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4384 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4385 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4386 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4387 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4388 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4389 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4390 | |
| 4391 | // Relay/Relay should be the first pingable connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4392 | Connection* conn = FindNextPingableConnectionAndPingIt(&ch); |
| 4393 | EXPECT_EQ(conn->local_candidate().type(), RELAY_PORT_TYPE); |
| 4394 | EXPECT_EQ(conn->remote_candidate().type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4395 | |
| 4396 | // Unless that we have a trigger check waiting to be pinged. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4397 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
| 4398 | EXPECT_EQ(conn2->local_candidate().type(), LOCAL_PORT_TYPE); |
| 4399 | EXPECT_EQ(conn2->remote_candidate().type(), LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4400 | conn2->ReceivedPing(); |
| 4401 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
| 4402 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4403 | // Make conn3 the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4404 | Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 4405 | EXPECT_EQ(conn3->local_candidate().type(), LOCAL_PORT_TYPE); |
| 4406 | EXPECT_EQ(conn3->remote_candidate().type(), RELAY_PORT_TYPE); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4407 | conn3->ReceivedPingResponse(LOW_RTT, "id"); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4408 | ASSERT_TRUE(conn3->writable()); |
| 4409 | conn3->ReceivedPing(); |
| 4410 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4411 | /* |
| 4412 | |
| 4413 | TODO(honghaiz): Re-enable this once we use fake clock for this test to fix |
| 4414 | the flakiness. The following test becomes flaky because we now ping the |
| 4415 | connections with fast rates until every connection is pinged at least three |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4416 | times. The selected connection may have been pinged before |
| 4417 | |max_strong_interval|, so it may not be the next connection to be pinged as |
| 4418 | expected in the test. |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4419 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4420 | // Verify that conn3 will be the "selected connection" since it is readable |
| 4421 | // and writable. After |MAX_CURRENT_STRONG_INTERVAL|, it should be the next |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4422 | // pingable connection. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4423 | EXPECT_TRUE_WAIT(conn3 == ch.selected_connection(), kDefaultTimeout); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4424 | WAIT(false, max_strong_interval + 100); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4425 | conn3->ReceivedPingResponse(LOW_RTT, "id"); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4426 | ASSERT_TRUE(conn3->writable()); |
| 4427 | EXPECT_EQ(conn3, FindNextPingableConnectionAndPingIt(&ch)); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4428 | |
| 4429 | */ |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | // Test that Relay/Relay connections will be pinged first when everything has |
| 4433 | // been pinged even if the Relay/Relay connection wasn't the first to be pinged |
| 4434 | // in the first round. |
| 4435 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4436 | TestRelayRelayFirstWhenEverythingPinged) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4437 | P2PTransportChannel& ch = StartTransportChannel(true, 100); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4438 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4439 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4440 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4441 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4442 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4443 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4444 | |
| 4445 | // Initially, only have Local/Local and Local/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4446 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE); |
| 4447 | VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4448 | |
| 4449 | // Remote Relay candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4450 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 2)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4451 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4452 | |
| 4453 | // Relay/Relay should be the first since it hasn't been pinged before. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4454 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4455 | |
| 4456 | // Local/Relay is the final one. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4457 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4458 | |
| 4459 | // Now, every connection has been pinged once. The next one should be |
| 4460 | // Relay/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4461 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | // Test that when we receive a new remote candidate, they will be tried first |
| 4465 | // before we re-ping Relay/Relay connections again. |
| 4466 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4467 | TestNoStarvationOnNonRelayConnection) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4468 | P2PTransportChannel& ch = StartTransportChannel(true, 100); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4469 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4470 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4471 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4472 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4473 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4474 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4475 | |
| 4476 | // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4477 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4478 | |
| 4479 | // Next, ping Local/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4480 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4481 | |
| 4482 | // Remote Local candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4483 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4484 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4485 | |
| 4486 | // Local/Local should be the first since it hasn't been pinged before. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4487 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4488 | |
| 4489 | // Relay/Local is the final one. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4490 | VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4491 | |
| 4492 | // Now, every connection has been pinged once. The next one should be |
| 4493 | // Relay/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4494 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay, |
| 4498 | // followed by the rest. |
| 4499 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) { |
| 4500 | // Add a Tcp Turn server. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4501 | turn_server()->AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 4502 | RelayServerConfig config(RELAY_TURN); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4503 | config.credentials = kRelayCredentials; |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 4504 | config.ports.push_back(ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4505 | allocator()->AddTurnServer(config); |
| 4506 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4507 | P2PTransportChannel& ch = StartTransportChannel(true, 100); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4508 | EXPECT_TRUE_WAIT(ch.ports().size() == 3, kDefaultTimeout); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4509 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4510 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
| 4511 | EXPECT_EQ(ch.ports()[2]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4512 | |
| 4513 | // Remote Relay candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4514 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 4515 | EXPECT_TRUE_WAIT(ch.connections().size() == 3, kDefaultTimeout); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4516 | |
| 4517 | // UDP Relay/Relay should be pinged first. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4518 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4519 | |
| 4520 | // TCP Relay/Relay is the next. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4521 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE, |
| 4522 | TCP_PROTOCOL_NAME); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4523 | |
| 4524 | // Finally, Local/Relay will be pinged. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4525 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4526 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4527 | |
Steve Anton | e932457 | 2017-11-29 10:18:21 -0800 | [diff] [blame^] | 4528 | } // namespace cricket |