henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 11 | #include <algorithm> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 12 | #include <memory> |
| 13 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 14 | #include "webrtc/api/fakemetricsobserver.h" |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 15 | #include "webrtc/p2p/base/fakeportallocator.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include "webrtc/p2p/base/p2ptransportchannel.h" |
| 17 | #include "webrtc/p2p/base/testrelayserver.h" |
| 18 | #include "webrtc/p2p/base/teststunserver.h" |
| 19 | #include "webrtc/p2p/base/testturnserver.h" |
| 20 | #include "webrtc/p2p/client/basicportallocator.h" |
| 21 | #include "webrtc/base/dscp.h" |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 22 | #include "webrtc/base/fakeclock.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 23 | #include "webrtc/base/fakenetwork.h" |
| 24 | #include "webrtc/base/firewallsocketserver.h" |
| 25 | #include "webrtc/base/gunit.h" |
| 26 | #include "webrtc/base/helpers.h" |
| 27 | #include "webrtc/base/logging.h" |
| 28 | #include "webrtc/base/natserver.h" |
| 29 | #include "webrtc/base/natsocketfactory.h" |
| 30 | #include "webrtc/base/physicalsocketserver.h" |
| 31 | #include "webrtc/base/proxyserver.h" |
| 32 | #include "webrtc/base/socketaddress.h" |
| 33 | #include "webrtc/base/ssladapter.h" |
| 34 | #include "webrtc/base/thread.h" |
| 35 | #include "webrtc/base/virtualsocketserver.h" |
| 36 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 37 | namespace { |
| 38 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | using rtc::SocketAddress; |
| 40 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 41 | // Default timeout for tests in this file. |
| 42 | // Should be large enough for slow buildbots to run the tests reliably. |
| 43 | static const int kDefaultTimeout = 10000; |
| 44 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 45 | static const int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN | |
| 46 | cricket::PORTALLOCATOR_DISABLE_RELAY | |
| 47 | cricket::PORTALLOCATOR_DISABLE_TCP; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 48 | static const int LOW_RTT = 20; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | // Addresses on the public internet. |
| 50 | static const SocketAddress kPublicAddrs[2] = |
| 51 | { SocketAddress("11.11.11.11", 0), SocketAddress("22.22.22.22", 0) }; |
| 52 | // IPv6 Addresses on the public internet. |
| 53 | static const SocketAddress kIPv6PublicAddrs[2] = { |
| 54 | SocketAddress("2400:4030:1:2c00:be30:abcd:efab:cdef", 0), |
| 55 | SocketAddress("2620:0:1000:1b03:2e41:38ff:fea6:f2a4", 0) |
| 56 | }; |
| 57 | // For configuring multihomed clients. |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 58 | static const SocketAddress kAlternateAddrs[2] = { |
| 59 | SocketAddress("101.101.101.101", 0), SocketAddress("202.202.202.202", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | // Addresses for HTTP proxy servers. |
| 61 | static const SocketAddress kHttpsProxyAddrs[2] = |
| 62 | { SocketAddress("11.11.11.1", 443), SocketAddress("22.22.22.1", 443) }; |
| 63 | // Addresses for SOCKS proxy servers. |
| 64 | static const SocketAddress kSocksProxyAddrs[2] = |
| 65 | { SocketAddress("11.11.11.1", 1080), SocketAddress("22.22.22.1", 1080) }; |
| 66 | // Internal addresses for NAT boxes. |
| 67 | static const SocketAddress kNatAddrs[2] = |
| 68 | { SocketAddress("192.168.1.1", 0), SocketAddress("192.168.2.1", 0) }; |
| 69 | // Private addresses inside the NAT private networks. |
| 70 | static const SocketAddress kPrivateAddrs[2] = |
| 71 | { SocketAddress("192.168.1.11", 0), SocketAddress("192.168.2.22", 0) }; |
| 72 | // For cascaded NATs, the internal addresses of the inner NAT boxes. |
| 73 | static const SocketAddress kCascadedNatAddrs[2] = |
| 74 | { SocketAddress("192.168.10.1", 0), SocketAddress("192.168.20.1", 0) }; |
| 75 | // For cascaded NATs, private addresses inside the inner private networks. |
| 76 | static const SocketAddress kCascadedPrivateAddrs[2] = |
| 77 | { SocketAddress("192.168.10.11", 0), SocketAddress("192.168.20.22", 0) }; |
| 78 | // The address of the public STUN server. |
| 79 | 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] | 80 | // The addresses for the public turn server. |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 81 | static const SocketAddress kTurnUdpIntAddr("99.99.99.3", |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | cricket::STUN_SERVER_PORT); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 83 | static const SocketAddress kTurnTcpIntAddr("99.99.99.4", |
| 84 | cricket::STUN_SERVER_PORT + 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 85 | static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); |
| 86 | static const cricket::RelayCredentials kRelayCredentials("test", "test"); |
| 87 | |
| 88 | // Based on ICE_UFRAG_LENGTH |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 89 | const char* kIceUfrag[4] = {"UF00", "UF01", "UF02", "UF03"}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 90 | // Based on ICE_PWD_LENGTH |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 91 | const char* kIcePwd[4] = { |
| 92 | "TESTICEPWD00000000000000", "TESTICEPWD00000000000001", |
| 93 | "TESTICEPWD00000000000002", "TESTICEPWD00000000000003"}; |
| 94 | const cricket::IceParameters kIceParams[4] = { |
| 95 | {kIceUfrag[0], kIcePwd[0], false}, |
| 96 | {kIceUfrag[1], kIcePwd[1], false}, |
| 97 | {kIceUfrag[2], kIcePwd[2], false}, |
| 98 | {kIceUfrag[3], kIcePwd[3], false}}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 100 | const uint64_t kLowTiebreaker = 11111; |
| 101 | const uint64_t kHighTiebreaker = 22222; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 103 | enum { MSG_ADD_CANDIDATES, MSG_REMOVE_CANDIDATES }; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 104 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 105 | cricket::IceConfig CreateIceConfig( |
| 106 | int receiving_timeout, |
| 107 | cricket::ContinualGatheringPolicy continual_gathering_policy, |
| 108 | int backup_ping_interval = -1) { |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 109 | cricket::IceConfig config; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 110 | config.receiving_timeout = receiving_timeout; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 111 | config.continual_gathering_policy = continual_gathering_policy; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 112 | config.backup_connection_ping_interval = backup_ping_interval; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 113 | return config; |
| 114 | } |
| 115 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 116 | cricket::Candidate CreateUdpCandidate(const std::string& type, |
| 117 | const std::string& ip, |
| 118 | int port, |
| 119 | int priority, |
| 120 | const std::string& ufrag = "") { |
| 121 | cricket::Candidate c; |
| 122 | c.set_address(rtc::SocketAddress(ip, port)); |
| 123 | c.set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 124 | c.set_protocol(cricket::UDP_PROTOCOL_NAME); |
| 125 | c.set_priority(priority); |
| 126 | c.set_username(ufrag); |
| 127 | c.set_type(type); |
| 128 | return c; |
| 129 | } |
| 130 | |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 131 | cricket::BasicPortAllocator* CreateBasicPortAllocator( |
| 132 | rtc::NetworkManager* network_manager, |
| 133 | const cricket::ServerAddresses& stun_servers, |
| 134 | const rtc::SocketAddress& turn_server_udp, |
| 135 | const rtc::SocketAddress& turn_server_tcp) { |
| 136 | cricket::RelayServerConfig turn_server(cricket::RELAY_TURN); |
| 137 | turn_server.credentials = kRelayCredentials; |
| 138 | if (!turn_server_udp.IsNil()) { |
| 139 | turn_server.ports.push_back( |
| 140 | cricket::ProtocolAddress(turn_server_udp, cricket::PROTO_UDP, false)); |
| 141 | } |
| 142 | if (!turn_server_tcp.IsNil()) { |
| 143 | turn_server.ports.push_back( |
| 144 | cricket::ProtocolAddress(turn_server_tcp, cricket::PROTO_TCP, false)); |
| 145 | } |
| 146 | std::vector<cricket::RelayServerConfig> turn_servers(1, turn_server); |
| 147 | |
| 148 | cricket::BasicPortAllocator* allocator = |
| 149 | new cricket::BasicPortAllocator(network_manager); |
| 150 | allocator->SetConfiguration(stun_servers, turn_servers, 0, false); |
| 151 | return allocator; |
| 152 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 153 | } // namespace |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 154 | |
| 155 | namespace cricket { |
| 156 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | // This test simulates 2 P2P endpoints that want to establish connectivity |
| 158 | // with each other over various network topologies and conditions, which can be |
| 159 | // specified in each individial test. |
| 160 | // A virtual network (via VirtualSocketServer) along with virtual firewalls and |
| 161 | // NATs (via Firewall/NATSocketServer) are used to simulate the various network |
| 162 | // conditions. We can configure the IP addresses of the endpoints, |
| 163 | // block various types of connectivity, or add arbitrary levels of NAT. |
| 164 | // We also run a STUN server and a relay server on the virtual network to allow |
| 165 | // our typical P2P mechanisms to do their thing. |
| 166 | // For each case, we expect the P2P stack to eventually settle on a specific |
| 167 | // form of connectivity to the other side. The test checks that the P2P |
| 168 | // negotiation successfully establishes connectivity within a certain time, |
| 169 | // and that the result is what we expect. |
| 170 | // Note that this class is a base class for use by other tests, who will provide |
| 171 | // specialized test behavior. |
| 172 | class P2PTransportChannelTestBase : public testing::Test, |
| 173 | public rtc::MessageHandler, |
| 174 | public sigslot::has_slots<> { |
| 175 | public: |
| 176 | P2PTransportChannelTestBase() |
| 177 | : main_(rtc::Thread::Current()), |
| 178 | pss_(new rtc::PhysicalSocketServer), |
| 179 | vss_(new rtc::VirtualSocketServer(pss_.get())), |
| 180 | nss_(new rtc::NATSocketServer(vss_.get())), |
| 181 | ss_(new rtc::FirewallSocketServer(nss_.get())), |
| 182 | ss_scope_(ss_.get()), |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 183 | stun_server_(TestStunServer::Create(main_, kStunAddr)), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 185 | socks_server1_(ss_.get(), |
| 186 | kSocksProxyAddrs[0], |
| 187 | ss_.get(), |
| 188 | kSocksProxyAddrs[0]), |
| 189 | socks_server2_(ss_.get(), |
| 190 | kSocksProxyAddrs[1], |
| 191 | ss_.get(), |
| 192 | kSocksProxyAddrs[1]), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 193 | force_relay_(false) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 194 | ep1_.role_ = ICEROLE_CONTROLLING; |
| 195 | ep2_.role_ = ICEROLE_CONTROLLED; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 196 | |
| 197 | ServerAddresses stun_servers; |
| 198 | stun_servers.insert(kStunAddr); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 199 | ep1_.allocator_.reset( |
| 200 | CreateBasicPortAllocator(&ep1_.network_manager_, stun_servers, |
| 201 | kTurnUdpIntAddr, rtc::SocketAddress())); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 202 | ep1_.metrics_observer_ = |
| 203 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 204 | ep1_.allocator_->SetMetricsObserver(ep1_.metrics_observer_); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 205 | ep2_.allocator_.reset( |
| 206 | CreateBasicPortAllocator(&ep2_.network_manager_, stun_servers, |
| 207 | kTurnUdpIntAddr, rtc::SocketAddress())); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 208 | ep2_.metrics_observer_ = |
| 209 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 210 | ep2_.allocator_->SetMetricsObserver(ep2_.metrics_observer_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | protected: |
| 214 | enum Config { |
| 215 | OPEN, // Open to the Internet |
| 216 | NAT_FULL_CONE, // NAT, no filtering |
| 217 | NAT_ADDR_RESTRICTED, // NAT, must send to an addr to recv |
| 218 | NAT_PORT_RESTRICTED, // NAT, must send to an addr+port to recv |
| 219 | NAT_SYMMETRIC, // NAT, endpoint-dependent bindings |
| 220 | NAT_DOUBLE_CONE, // Double NAT, both cone |
| 221 | NAT_SYMMETRIC_THEN_CONE, // Double NAT, symmetric outer, cone inner |
| 222 | BLOCK_UDP, // Firewall, UDP in/out blocked |
| 223 | BLOCK_UDP_AND_INCOMING_TCP, // Firewall, UDP in/out and TCP in blocked |
| 224 | BLOCK_ALL_BUT_OUTGOING_HTTP, // Firewall, only TCP out on 80/443 |
| 225 | PROXY_HTTPS, // All traffic through HTTPS proxy |
| 226 | PROXY_SOCKS, // All traffic through SOCKS proxy |
| 227 | NUM_CONFIGS |
| 228 | }; |
| 229 | |
| 230 | struct Result { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 231 | Result(const std::string& controlling_type, |
| 232 | const std::string& controlling_protocol, |
| 233 | const std::string& controlled_type, |
| 234 | const std::string& controlled_protocol, |
| 235 | int wait) |
| 236 | : controlling_type(controlling_type), |
| 237 | controlling_protocol(controlling_protocol), |
| 238 | controlled_type(controlled_type), |
| 239 | controlled_protocol(controlled_protocol), |
| 240 | connect_wait(wait) {} |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 241 | |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 242 | // The expected candidate type and protocol of the controlling ICE agent. |
| 243 | std::string controlling_type; |
| 244 | std::string controlling_protocol; |
| 245 | // The expected candidate type and protocol of the controlled ICE agent. |
| 246 | std::string controlled_type; |
| 247 | std::string controlled_protocol; |
| 248 | // How long to wait before the correct candidate pair is selected. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 249 | int connect_wait; |
| 250 | }; |
| 251 | |
| 252 | struct ChannelData { |
| 253 | bool CheckData(const char* data, int len) { |
| 254 | bool ret = false; |
| 255 | if (!ch_packets_.empty()) { |
| 256 | std::string packet = ch_packets_.front(); |
| 257 | ret = (packet == std::string(data, len)); |
| 258 | ch_packets_.pop_front(); |
| 259 | } |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | std::string name_; // TODO - Currently not used. |
| 264 | std::list<std::string> ch_packets_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 265 | std::unique_ptr<P2PTransportChannel> ch_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 268 | struct CandidatesData : public rtc::MessageData { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 269 | CandidatesData(TransportChannel* ch, const Candidate& c) |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 270 | : channel(ch), candidates(1, c) {} |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 271 | CandidatesData(TransportChannel* ch, const std::vector<Candidate>& cc) |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 272 | : channel(ch), candidates(cc) {} |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 273 | TransportChannel* channel; |
| 274 | Candidates candidates; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 277 | struct Endpoint { |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 278 | Endpoint() |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 279 | : role_(ICEROLE_UNKNOWN), |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 280 | tiebreaker_(0), |
| 281 | role_conflict_(false), |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 282 | save_candidates_(false) {} |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 283 | bool HasChannel(TransportChannel* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | return (ch == cd1_.ch_.get() || ch == cd2_.ch_.get()); |
| 285 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 286 | ChannelData* GetChannelData(TransportChannel* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 287 | if (!HasChannel(ch)) return NULL; |
| 288 | if (cd1_.ch_.get() == ch) |
| 289 | return &cd1_; |
| 290 | else |
| 291 | return &cd2_; |
| 292 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 293 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 294 | void SetIceRole(IceRole role) { role_ = role; } |
| 295 | IceRole ice_role() { return role_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 296 | void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; } |
| 297 | uint64_t GetIceTiebreaker() { return tiebreaker_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 298 | void OnRoleConflict(bool role_conflict) { role_conflict_ = role_conflict; } |
| 299 | bool role_conflict() { return role_conflict_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 300 | void SetAllocationStepDelay(uint32_t delay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | allocator_->set_step_delay(delay); |
| 302 | } |
| 303 | void SetAllowTcpListen(bool allow_tcp_listen) { |
| 304 | allocator_->set_allow_tcp_listen(allow_tcp_listen); |
| 305 | } |
| 306 | |
| 307 | rtc::FakeNetworkManager network_manager_; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 308 | // |metrics_observer_| should outlive |allocator_| as the former may be |
| 309 | // used by the latter. |
| 310 | rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 311 | std::unique_ptr<BasicPortAllocator> allocator_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 312 | ChannelData cd1_; |
| 313 | ChannelData cd2_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 314 | IceRole role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 315 | uint64_t tiebreaker_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 316 | bool role_conflict_; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 317 | bool save_candidates_; |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 318 | std::vector<std::unique_ptr<CandidatesData>> saved_candidates_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 319 | bool ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 322 | ChannelData* GetChannelData(TransportChannel* channel) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 323 | if (ep1_.HasChannel(channel)) |
| 324 | return ep1_.GetChannelData(channel); |
| 325 | else |
| 326 | return ep2_.GetChannelData(channel); |
| 327 | } |
| 328 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 329 | IceParameters IceParamsWithRenomination(const IceParameters& ice, |
| 330 | bool renomination) { |
| 331 | IceParameters new_ice = ice; |
| 332 | new_ice.renomination = renomination; |
| 333 | return new_ice; |
| 334 | } |
| 335 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 336 | void CreateChannels(const IceConfig& ep1_config, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 337 | const IceConfig& ep2_config, |
| 338 | bool renomination = false) { |
| 339 | IceParameters ice_ep1_cd1_ch = |
| 340 | IceParamsWithRenomination(kIceParams[0], renomination); |
| 341 | IceParameters ice_ep2_cd1_ch = |
| 342 | IceParamsWithRenomination(kIceParams[1], renomination); |
| 343 | ep1_.cd1_.ch_.reset(CreateChannel(0, ICE_CANDIDATE_COMPONENT_DEFAULT, |
| 344 | ice_ep1_cd1_ch, ice_ep2_cd1_ch)); |
| 345 | ep2_.cd1_.ch_.reset(CreateChannel(1, ICE_CANDIDATE_COMPONENT_DEFAULT, |
| 346 | ice_ep2_cd1_ch, ice_ep1_cd1_ch)); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 347 | ep1_.cd1_.ch_->SetMetricsObserver(ep1_.metrics_observer_); |
| 348 | ep2_.cd1_.ch_->SetMetricsObserver(ep2_.metrics_observer_); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 349 | ep1_.cd1_.ch_->SetIceConfig(ep1_config); |
| 350 | ep2_.cd1_.ch_->SetIceConfig(ep2_config); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 351 | ep1_.cd1_.ch_->MaybeStartGathering(); |
| 352 | ep2_.cd1_.ch_->MaybeStartGathering(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 353 | } |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 354 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 355 | void CreateChannels() { |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 356 | IceConfig default_config; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 357 | CreateChannels(default_config, default_config, false); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 358 | } |
| 359 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 360 | P2PTransportChannel* CreateChannel(int endpoint, |
| 361 | int component, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 362 | const IceParameters& local_ice, |
| 363 | const IceParameters& remote_ice) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 364 | P2PTransportChannel* channel = new P2PTransportChannel( |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 365 | "test content name", component, GetAllocator(endpoint)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 366 | channel->SignalReadyToSend.connect( |
| 367 | this, &P2PTransportChannelTestBase::OnReadyToSend); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 368 | channel->SignalCandidateGathered.connect( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 369 | this, &P2PTransportChannelTestBase::OnCandidateGathered); |
| 370 | channel->SignalCandidatesRemoved.connect( |
| 371 | this, &P2PTransportChannelTestBase::OnCandidatesRemoved); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 372 | channel->SignalReadPacket.connect( |
| 373 | this, &P2PTransportChannelTestBase::OnReadPacket); |
| 374 | channel->SignalRoleConflict.connect( |
| 375 | this, &P2PTransportChannelTestBase::OnRoleConflict); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 376 | channel->SignalSelectedCandidatePairChanged.connect( |
| 377 | this, &P2PTransportChannelTestBase::OnSelectedCandidatePairChanged); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 378 | channel->SetIceParameters(local_ice); |
| 379 | if (remote_ice_parameter_source_ == FROM_SETICEPARAMETERS) { |
| 380 | channel->SetRemoteIceParameters(remote_ice); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 381 | } |
| 382 | channel->SetIceRole(GetEndpoint(endpoint)->ice_role()); |
| 383 | channel->SetIceTiebreaker(GetEndpoint(endpoint)->GetIceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | return channel; |
| 385 | } |
| 386 | void DestroyChannels() { |
| 387 | ep1_.cd1_.ch_.reset(); |
| 388 | ep2_.cd1_.ch_.reset(); |
| 389 | ep1_.cd2_.ch_.reset(); |
| 390 | ep2_.cd2_.ch_.reset(); |
| 391 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 392 | P2PTransportChannel* ep1_ch1() { return ep1_.cd1_.ch_.get(); } |
| 393 | P2PTransportChannel* ep1_ch2() { return ep1_.cd2_.ch_.get(); } |
| 394 | P2PTransportChannel* ep2_ch1() { return ep2_.cd1_.ch_.get(); } |
| 395 | P2PTransportChannel* ep2_ch2() { return ep2_.cd2_.ch_.get(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 396 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 397 | TestTurnServer* test_turn_server() { return &turn_server_; } |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 398 | rtc::VirtualSocketServer* virtual_socket_server() { return vss_.get(); } |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 399 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 400 | // Common results. |
| 401 | static const Result kLocalUdpToLocalUdp; |
| 402 | static const Result kLocalUdpToStunUdp; |
| 403 | static const Result kLocalUdpToPrflxUdp; |
| 404 | static const Result kPrflxUdpToLocalUdp; |
| 405 | static const Result kStunUdpToLocalUdp; |
| 406 | static const Result kStunUdpToStunUdp; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 407 | static const Result kStunUdpToPrflxUdp; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 408 | static const Result kPrflxUdpToStunUdp; |
| 409 | static const Result kLocalUdpToRelayUdp; |
| 410 | static const Result kPrflxUdpToRelayUdp; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 411 | static const Result kRelayUdpToPrflxUdp; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 412 | static const Result kLocalTcpToLocalTcp; |
| 413 | static const Result kLocalTcpToPrflxTcp; |
| 414 | static const Result kPrflxTcpToLocalTcp; |
| 415 | |
| 416 | rtc::NATSocketServer* nat() { return nss_.get(); } |
| 417 | rtc::FirewallSocketServer* fw() { return ss_.get(); } |
| 418 | |
| 419 | Endpoint* GetEndpoint(int endpoint) { |
| 420 | if (endpoint == 0) { |
| 421 | return &ep1_; |
| 422 | } else if (endpoint == 1) { |
| 423 | return &ep2_; |
| 424 | } else { |
| 425 | return NULL; |
| 426 | } |
| 427 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 428 | PortAllocator* GetAllocator(int endpoint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 429 | return GetEndpoint(endpoint)->allocator_.get(); |
| 430 | } |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 431 | webrtc::FakeMetricsObserver* GetMetricsObserver(int endpoint) { |
| 432 | return GetEndpoint(endpoint)->metrics_observer_; |
| 433 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 434 | void AddAddress(int endpoint, const SocketAddress& addr) { |
| 435 | GetEndpoint(endpoint)->network_manager_.AddInterface(addr); |
| 436 | } |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 437 | void AddAddress(int endpoint, |
| 438 | const SocketAddress& addr, |
| 439 | const std::string& ifname, |
| 440 | rtc::AdapterType adapter_type) { |
| 441 | GetEndpoint(endpoint)->network_manager_.AddInterface(addr, ifname, |
| 442 | adapter_type); |
| 443 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 444 | void RemoveAddress(int endpoint, const SocketAddress& addr) { |
| 445 | GetEndpoint(endpoint)->network_manager_.RemoveInterface(addr); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 446 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 447 | } |
| 448 | void SetProxy(int endpoint, rtc::ProxyType type) { |
| 449 | rtc::ProxyInfo info; |
| 450 | info.type = type; |
| 451 | info.address = (type == rtc::PROXY_HTTPS) ? |
| 452 | kHttpsProxyAddrs[endpoint] : kSocksProxyAddrs[endpoint]; |
| 453 | GetAllocator(endpoint)->set_proxy("unittest/1.0", info); |
| 454 | } |
| 455 | void SetAllocatorFlags(int endpoint, int flags) { |
| 456 | GetAllocator(endpoint)->set_flags(flags); |
| 457 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 458 | void SetIceRole(int endpoint, IceRole role) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 459 | GetEndpoint(endpoint)->SetIceRole(role); |
| 460 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 461 | void SetIceTiebreaker(int endpoint, uint64_t tiebreaker) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 462 | GetEndpoint(endpoint)->SetIceTiebreaker(tiebreaker); |
| 463 | } |
| 464 | bool GetRoleConflict(int endpoint) { |
| 465 | return GetEndpoint(endpoint)->role_conflict(); |
| 466 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 467 | void SetAllocationStepDelay(int endpoint, uint32_t delay) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 468 | return GetEndpoint(endpoint)->SetAllocationStepDelay(delay); |
| 469 | } |
| 470 | void SetAllowTcpListen(int endpoint, bool allow_tcp_listen) { |
| 471 | return GetEndpoint(endpoint)->SetAllowTcpListen(allow_tcp_listen); |
| 472 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 473 | |
| 474 | // Return true if the approprite parts of the expected Result, based |
| 475 | // on the local and remote candidate of ep1_ch1, match. This can be |
| 476 | // used in an EXPECT_TRUE_WAIT. |
| 477 | bool CheckCandidate1(const Result& expected) { |
| 478 | const std::string& local_type = LocalCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 479 | const std::string& local_protocol = LocalCandidate(ep1_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 480 | const std::string& remote_type = RemoteCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 481 | const std::string& remote_protocol = RemoteCandidate(ep1_ch1())->protocol(); |
| 482 | return (local_protocol == expected.controlling_protocol && |
| 483 | remote_protocol == expected.controlled_protocol && |
| 484 | local_type == expected.controlling_type && |
| 485 | remote_type == expected.controlled_type); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // EXPECT_EQ on the approprite parts of the expected Result, based |
| 489 | // on the local and remote candidate of ep1_ch1. This is like |
| 490 | // CheckCandidate1, except that it will provide more detail about |
| 491 | // what didn't match. |
| 492 | void ExpectCandidate1(const Result& expected) { |
| 493 | if (CheckCandidate1(expected)) { |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | const std::string& local_type = LocalCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 498 | const std::string& local_protocol = LocalCandidate(ep1_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 499 | const std::string& remote_type = RemoteCandidate(ep1_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 500 | const std::string& remote_protocol = RemoteCandidate(ep1_ch1())->protocol(); |
| 501 | EXPECT_EQ(expected.controlling_type, local_type); |
| 502 | EXPECT_EQ(expected.controlled_type, remote_type); |
| 503 | EXPECT_EQ(expected.controlling_protocol, local_protocol); |
| 504 | EXPECT_EQ(expected.controlled_protocol, remote_protocol); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Return true if the approprite parts of the expected Result, based |
| 508 | // on the local and remote candidate of ep2_ch1, match. This can be |
| 509 | // used in an EXPECT_TRUE_WAIT. |
| 510 | bool CheckCandidate2(const Result& expected) { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 511 | const std::string& local_type = LocalCandidate(ep2_ch1())->type(); |
| 512 | const std::string& local_protocol = LocalCandidate(ep2_ch1())->protocol(); |
| 513 | const std::string& remote_type = RemoteCandidate(ep2_ch1())->type(); |
| 514 | const std::string& remote_protocol = RemoteCandidate(ep2_ch1())->protocol(); |
| 515 | return (local_protocol == expected.controlled_protocol && |
| 516 | remote_protocol == expected.controlling_protocol && |
| 517 | local_type == expected.controlled_type && |
| 518 | remote_type == expected.controlling_type); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // EXPECT_EQ on the approprite parts of the expected Result, based |
| 522 | // on the local and remote candidate of ep2_ch1. This is like |
| 523 | // CheckCandidate2, except that it will provide more detail about |
| 524 | // what didn't match. |
| 525 | void ExpectCandidate2(const Result& expected) { |
| 526 | if (CheckCandidate2(expected)) { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | const std::string& local_type = LocalCandidate(ep2_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 531 | const std::string& local_protocol = LocalCandidate(ep2_ch1())->protocol(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 532 | const std::string& remote_type = RemoteCandidate(ep2_ch1())->type(); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 533 | const std::string& remote_protocol = RemoteCandidate(ep2_ch1())->protocol(); |
| 534 | EXPECT_EQ(expected.controlled_type, local_type); |
| 535 | EXPECT_EQ(expected.controlling_type, remote_type); |
| 536 | EXPECT_EQ(expected.controlled_protocol, local_protocol); |
| 537 | EXPECT_EQ(expected.controlling_protocol, remote_protocol); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 538 | } |
| 539 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 540 | void Test(const Result& expected) { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 541 | int64_t connect_start = rtc::TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 542 | int64_t connect_time; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 543 | |
| 544 | // Create the channels and wait for them to connect. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 545 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 546 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && |
| 547 | ep2_ch1() != NULL && |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 548 | ep1_ch1()->receiving() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 549 | ep1_ch1()->writable() && |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 550 | ep2_ch1()->receiving() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 551 | ep2_ch1()->writable(), |
| 552 | expected.connect_wait, |
| 553 | 1000); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 554 | connect_time = rtc::TimeMillis() - connect_start; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 555 | if (connect_time < expected.connect_wait) { |
| 556 | LOG(LS_INFO) << "Connect time: " << connect_time << " ms"; |
| 557 | } else { |
| 558 | LOG(LS_INFO) << "Connect time: " << "TIMEOUT (" |
| 559 | << expected.connect_wait << " ms)"; |
| 560 | } |
| 561 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 562 | // 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] | 563 | // This may take up to 2 seconds. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 564 | if (ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection()) { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 565 | int64_t converge_start = rtc::TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 566 | int64_t converge_time; |
| 567 | int64_t converge_wait = 2000; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 568 | // Verifying local and remote channel selected connection information. |
| 569 | // This is done only for the RFC 5245 as controlled agent will use |
| 570 | // USE-CANDIDATE from controlling (ep1) agent. We can easily predict from |
| 571 | // EP1 result matrix. |
Taylor Brandstetter | 7e1b8fb | 2016-05-26 15:21:45 -0700 | [diff] [blame] | 572 | EXPECT_TRUE_WAIT_MARGIN( |
| 573 | CheckCandidate1(expected) && CheckCandidate2(expected), converge_wait, |
| 574 | converge_wait); |
| 575 | // Also do EXPECT_EQ on each part so that failures are more verbose. |
| 576 | ExpectCandidate1(expected); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 577 | ExpectCandidate2(expected); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 578 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 579 | converge_time = rtc::TimeMillis() - converge_start; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 580 | if (converge_time < converge_wait) { |
| 581 | LOG(LS_INFO) << "Converge time: " << converge_time << " ms"; |
| 582 | } else { |
| 583 | LOG(LS_INFO) << "Converge time: " << "TIMEOUT (" |
| 584 | << converge_wait << " ms)"; |
| 585 | } |
| 586 | } |
| 587 | // Try sending some data to other end. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 588 | TestSendRecv(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 589 | |
| 590 | // Destroy the channels, and wait for them to be fully cleaned up. |
| 591 | DestroyChannels(); |
| 592 | } |
| 593 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 594 | void TestSendRecv() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 595 | for (int i = 0; i < 10; ++i) { |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 596 | const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 597 | int len = static_cast<int>(strlen(data)); |
| 598 | // local_channel1 <==> remote_channel1 |
| 599 | EXPECT_EQ_WAIT(len, SendData(ep1_ch1(), data, len), 1000); |
| 600 | EXPECT_TRUE_WAIT(CheckDataOnChannel(ep2_ch1(), data, len), 1000); |
| 601 | EXPECT_EQ_WAIT(len, SendData(ep2_ch1(), data, len), 1000); |
| 602 | EXPECT_TRUE_WAIT(CheckDataOnChannel(ep1_ch1(), data, len), 1000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 606 | // 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] | 607 | // 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] | 608 | // restart the ice gathering. Finally it waits for the transport to select a |
| 609 | // new connection using the newly generated ice candidates. |
| 610 | // Before calling this function the end points must be configured. |
| 611 | void TestHandleIceUfragPasswordChanged() { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 612 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 613 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 614 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 615 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 616 | 1000, 1000); |
| 617 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 618 | const Candidate* old_local_candidate1 = LocalCandidate(ep1_ch1()); |
| 619 | const Candidate* old_local_candidate2 = LocalCandidate(ep2_ch1()); |
| 620 | const Candidate* old_remote_candidate1 = RemoteCandidate(ep1_ch1()); |
| 621 | const Candidate* old_remote_candidate2 = RemoteCandidate(ep2_ch1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 622 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 623 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 624 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 625 | ep1_ch1()->MaybeStartGathering(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 626 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 627 | |
| 628 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 629 | ep2_ch1()->MaybeStartGathering(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 630 | |
| 631 | EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep1_ch1())->generation() != |
| 632 | old_local_candidate1->generation(), |
| 633 | 1000, 1000); |
| 634 | EXPECT_TRUE_WAIT_MARGIN(LocalCandidate(ep2_ch1())->generation() != |
| 635 | old_local_candidate2->generation(), |
| 636 | 1000, 1000); |
| 637 | EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep1_ch1())->generation() != |
| 638 | old_remote_candidate1->generation(), |
| 639 | 1000, 1000); |
| 640 | EXPECT_TRUE_WAIT_MARGIN(RemoteCandidate(ep2_ch1())->generation() != |
| 641 | old_remote_candidate2->generation(), |
| 642 | 1000, 1000); |
| 643 | EXPECT_EQ(1u, RemoteCandidate(ep2_ch1())->generation()); |
| 644 | EXPECT_EQ(1u, RemoteCandidate(ep1_ch1())->generation()); |
| 645 | } |
| 646 | |
| 647 | void TestSignalRoleConflict() { |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 648 | SetIceTiebreaker(0, |
| 649 | kLowTiebreaker); // Default EP1 is in controlling state. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 650 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 651 | SetIceRole(1, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 652 | SetIceTiebreaker(1, kHighTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 653 | |
| 654 | // Creating channels with both channels role set to CONTROLLING. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 655 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 656 | // Since both the channels initiated with controlling state and channel2 |
| 657 | // has higher tiebreaker value, channel1 should receive SignalRoleConflict. |
| 658 | EXPECT_TRUE_WAIT(GetRoleConflict(0), 1000); |
| 659 | EXPECT_FALSE(GetRoleConflict(1)); |
| 660 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 661 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 662 | ep1_ch1()->writable() && |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 663 | ep2_ch1()->receiving() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 664 | ep2_ch1()->writable(), |
| 665 | 1000); |
| 666 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 667 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 668 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 669 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 670 | TestSendRecv(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 671 | } |
| 672 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 673 | void OnReadyToSend(TransportChannel* ch) { |
| 674 | GetEndpoint(ch)->ready_to_send_ = true; |
| 675 | } |
| 676 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 677 | // We pass the candidates directly to the other side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 678 | void OnCandidateGathered(TransportChannelImpl* ch, const Candidate& c) { |
| 679 | if (force_relay_ && c.type() != RELAY_PORT_TYPE) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 680 | return; |
| 681 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 682 | if (GetEndpoint(ch)->save_candidates_) { |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 683 | GetEndpoint(ch)->saved_candidates_.push_back( |
| 684 | std::unique_ptr<CandidatesData>(new CandidatesData(ch, c))); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 685 | } else { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 686 | main_->Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, |
| 687 | new CandidatesData(ch, c)); |
jiayl@webrtc.org | c5fd66d | 2014-12-29 19:23:37 +0000 | [diff] [blame] | 688 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 689 | } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 690 | void OnSelectedCandidatePairChanged( |
| 691 | TransportChannel* transport_channel, |
| 692 | CandidatePairInterface* selected_candidate_pair, |
| 693 | int last_sent_packet_id, |
| 694 | bool ready_to_send) { |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 695 | // Do not count if it switches to nullptr. This may happen if all |
| 696 | // connections timed out. |
| 697 | if (selected_candidate_pair != nullptr) { |
| 698 | ++selected_candidate_pair_switches_; |
| 699 | } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | int reset_selected_candidate_pair_switches() { |
| 703 | int switches = selected_candidate_pair_switches_; |
| 704 | selected_candidate_pair_switches_ = 0; |
| 705 | return switches; |
| 706 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 707 | |
| 708 | void PauseCandidates(int endpoint) { |
| 709 | GetEndpoint(endpoint)->save_candidates_ = true; |
| 710 | } |
| 711 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 712 | void OnCandidatesRemoved(TransportChannelImpl* ch, |
| 713 | const std::vector<Candidate>& candidates) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 714 | // Candidate removals are not paused. |
| 715 | CandidatesData* candidates_data = new CandidatesData(ch, candidates); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 716 | main_->Post(RTC_FROM_HERE, this, MSG_REMOVE_CANDIDATES, candidates_data); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 719 | // Tcp candidate verification has to be done when they are generated. |
| 720 | void VerifySavedTcpCandidates(int endpoint, const std::string& tcptype) { |
| 721 | for (auto& data : GetEndpoint(endpoint)->saved_candidates_) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 722 | for (auto& candidate : data->candidates) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 723 | EXPECT_EQ(candidate.protocol(), TCP_PROTOCOL_NAME); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 724 | EXPECT_EQ(candidate.tcptype(), tcptype); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 725 | if (candidate.tcptype() == TCPTYPE_ACTIVE_STR) { |
| 726 | EXPECT_EQ(candidate.address().port(), DISCARD_PORT); |
| 727 | } else if (candidate.tcptype() == TCPTYPE_PASSIVE_STR) { |
| 728 | EXPECT_NE(candidate.address().port(), DISCARD_PORT); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 729 | } else { |
| 730 | FAIL() << "Unknown tcptype: " << candidate.tcptype(); |
| 731 | } |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 736 | void ResumeCandidates(int endpoint) { |
| 737 | Endpoint* ed = GetEndpoint(endpoint); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 738 | for (auto& candidate : ed->saved_candidates_) { |
| 739 | main_->Post(RTC_FROM_HERE, this, MSG_ADD_CANDIDATES, candidate.release()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 740 | } |
| 741 | ed->saved_candidates_.clear(); |
| 742 | ed->save_candidates_ = false; |
| 743 | } |
| 744 | |
| 745 | void OnMessage(rtc::Message* msg) { |
| 746 | switch (msg->message_id) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 747 | case MSG_ADD_CANDIDATES: { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 748 | std::unique_ptr<CandidatesData> data( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 749 | static_cast<CandidatesData*>(msg->pdata)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 750 | P2PTransportChannel* rch = GetRemoteChannel(data->channel); |
| 751 | if (!rch) { |
| 752 | return; |
| 753 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 754 | for (auto& c : data->candidates) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 755 | if (remote_ice_parameter_source_ != FROM_CANDIDATE) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 756 | c.set_username(""); |
| 757 | c.set_password(""); |
| 758 | } |
| 759 | LOG(LS_INFO) << "Candidate(" << data->channel->component() << "->" |
| 760 | << rch->component() << "): " << c.ToString(); |
| 761 | rch->AddRemoteCandidate(c); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 762 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 763 | break; |
| 764 | } |
| 765 | case MSG_REMOVE_CANDIDATES: { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 766 | std::unique_ptr<CandidatesData> data( |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 767 | static_cast<CandidatesData*>(msg->pdata)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 768 | P2PTransportChannel* rch = GetRemoteChannel(data->channel); |
| 769 | if (!rch) { |
| 770 | return; |
| 771 | } |
| 772 | for (Candidate& c : data->candidates) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 773 | LOG(LS_INFO) << "Removed remote candidate " << c.ToString(); |
| 774 | rch->RemoveRemoteCandidate(c); |
| 775 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 779 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 780 | void OnReadPacket(TransportChannel* channel, |
| 781 | const char* data, |
| 782 | size_t len, |
| 783 | const rtc::PacketTime& packet_time, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 784 | int flags) { |
| 785 | std::list<std::string>& packets = GetPacketList(channel); |
| 786 | packets.push_front(std::string(data, len)); |
| 787 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 788 | void OnRoleConflict(TransportChannelImpl* channel) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 789 | GetEndpoint(channel)->OnRoleConflict(true); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 790 | IceRole new_role = GetEndpoint(channel)->ice_role() == ICEROLE_CONTROLLING |
| 791 | ? ICEROLE_CONTROLLED |
| 792 | : ICEROLE_CONTROLLING; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 793 | channel->SetIceRole(new_role); |
| 794 | } |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 795 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 796 | int SendData(TransportChannel* channel, const char* data, size_t len) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 797 | rtc::PacketOptions options; |
| 798 | return channel->SendPacket(data, len, options, 0); |
| 799 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 800 | bool CheckDataOnChannel(TransportChannel* channel, |
| 801 | const char* data, |
| 802 | int len) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 803 | return GetChannelData(channel)->CheckData(data, len); |
| 804 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 805 | static const Candidate* LocalCandidate(P2PTransportChannel* ch) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 806 | return (ch && ch->selected_connection()) |
| 807 | ? &ch->selected_connection()->local_candidate() |
| 808 | : NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 809 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 810 | static const Candidate* RemoteCandidate(P2PTransportChannel* ch) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 811 | return (ch && ch->selected_connection()) |
| 812 | ? &ch->selected_connection()->remote_candidate() |
| 813 | : NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 814 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 815 | Endpoint* GetEndpoint(TransportChannel* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 816 | if (ep1_.HasChannel(ch)) { |
| 817 | return &ep1_; |
| 818 | } else if (ep2_.HasChannel(ch)) { |
| 819 | return &ep2_; |
| 820 | } else { |
| 821 | return NULL; |
| 822 | } |
| 823 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 824 | P2PTransportChannel* GetRemoteChannel(TransportChannel* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 825 | if (ch == ep1_ch1()) |
| 826 | return ep2_ch1(); |
| 827 | else if (ch == ep1_ch2()) |
| 828 | return ep2_ch2(); |
| 829 | else if (ch == ep2_ch1()) |
| 830 | return ep1_ch1(); |
| 831 | else if (ch == ep2_ch2()) |
| 832 | return ep1_ch2(); |
| 833 | else |
| 834 | return NULL; |
| 835 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 836 | std::list<std::string>& GetPacketList(TransportChannel* ch) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 837 | return GetChannelData(ch)->ch_packets_; |
| 838 | } |
| 839 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 840 | enum RemoteIceParameterSource { FROM_CANDIDATE, FROM_SETICEPARAMETERS }; |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 841 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 842 | // How does the test pass ICE parameters to the P2PTransportChannel? |
| 843 | // On the candidate itself, or through SetRemoteIceParameters? |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 844 | // Goes through the candidate itself by default. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 845 | void set_remote_ice_parameter_source(RemoteIceParameterSource source) { |
| 846 | remote_ice_parameter_source_ = source; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | void set_force_relay(bool relay) { |
| 850 | force_relay_ = relay; |
| 851 | } |
| 852 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 853 | void ConnectSignalNominated(Connection* conn) { |
| 854 | conn->SignalNominated.connect(this, |
| 855 | &P2PTransportChannelTestBase::OnNominated); |
| 856 | } |
| 857 | |
| 858 | void OnNominated(Connection* conn) { nominated_ = true; } |
| 859 | bool nominated() { return nominated_; } |
| 860 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 861 | private: |
| 862 | rtc::Thread* main_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 863 | std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 864 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 865 | std::unique_ptr<rtc::NATSocketServer> nss_; |
| 866 | std::unique_ptr<rtc::FirewallSocketServer> ss_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 867 | rtc::SocketServerScope ss_scope_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 868 | std::unique_ptr<TestStunServer> stun_server_; |
| 869 | TestTurnServer turn_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 870 | rtc::SocksProxyServer socks_server1_; |
| 871 | rtc::SocksProxyServer socks_server2_; |
| 872 | Endpoint ep1_; |
| 873 | Endpoint ep2_; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 874 | RemoteIceParameterSource remote_ice_parameter_source_ = FROM_CANDIDATE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 875 | bool force_relay_; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 876 | int selected_candidate_pair_switches_ = 0; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 877 | |
| 878 | bool nominated_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 879 | }; |
| 880 | |
| 881 | // The tests have only a few outcomes, which we predefine. |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 882 | const P2PTransportChannelTestBase::Result |
| 883 | P2PTransportChannelTestBase::kLocalUdpToLocalUdp("local", |
| 884 | "udp", |
| 885 | "local", |
| 886 | "udp", |
| 887 | 1000); |
| 888 | const P2PTransportChannelTestBase::Result |
| 889 | P2PTransportChannelTestBase::kLocalUdpToStunUdp("local", |
| 890 | "udp", |
| 891 | "stun", |
| 892 | "udp", |
| 893 | 1000); |
| 894 | const P2PTransportChannelTestBase::Result |
| 895 | P2PTransportChannelTestBase::kLocalUdpToPrflxUdp("local", |
| 896 | "udp", |
| 897 | "prflx", |
| 898 | "udp", |
| 899 | 1000); |
| 900 | const P2PTransportChannelTestBase::Result |
| 901 | P2PTransportChannelTestBase::kPrflxUdpToLocalUdp("prflx", |
| 902 | "udp", |
| 903 | "local", |
| 904 | "udp", |
| 905 | 1000); |
| 906 | const P2PTransportChannelTestBase::Result |
| 907 | P2PTransportChannelTestBase::kStunUdpToLocalUdp("stun", |
| 908 | "udp", |
| 909 | "local", |
| 910 | "udp", |
| 911 | 1000); |
| 912 | const P2PTransportChannelTestBase::Result |
| 913 | P2PTransportChannelTestBase::kStunUdpToStunUdp("stun", |
| 914 | "udp", |
| 915 | "stun", |
| 916 | "udp", |
| 917 | 1000); |
| 918 | const P2PTransportChannelTestBase::Result |
| 919 | P2PTransportChannelTestBase::kStunUdpToPrflxUdp("stun", |
| 920 | "udp", |
| 921 | "prflx", |
| 922 | "udp", |
| 923 | 1000); |
| 924 | const P2PTransportChannelTestBase::Result |
| 925 | P2PTransportChannelTestBase::kPrflxUdpToStunUdp("prflx", |
| 926 | "udp", |
| 927 | "stun", |
| 928 | "udp", |
| 929 | 1000); |
| 930 | const P2PTransportChannelTestBase::Result |
| 931 | P2PTransportChannelTestBase::kLocalUdpToRelayUdp("local", |
| 932 | "udp", |
| 933 | "relay", |
| 934 | "udp", |
| 935 | 2000); |
| 936 | const P2PTransportChannelTestBase::Result |
| 937 | P2PTransportChannelTestBase::kPrflxUdpToRelayUdp("prflx", |
| 938 | "udp", |
| 939 | "relay", |
| 940 | "udp", |
| 941 | 2000); |
| 942 | const P2PTransportChannelTestBase::Result |
| 943 | P2PTransportChannelTestBase::kRelayUdpToPrflxUdp("relay", |
| 944 | "udp", |
| 945 | "prflx", |
| 946 | "udp", |
| 947 | 2000); |
| 948 | const P2PTransportChannelTestBase::Result |
| 949 | P2PTransportChannelTestBase::kLocalTcpToLocalTcp("local", |
| 950 | "tcp", |
| 951 | "local", |
| 952 | "tcp", |
| 953 | 3000); |
| 954 | const P2PTransportChannelTestBase::Result |
| 955 | P2PTransportChannelTestBase::kLocalTcpToPrflxTcp("local", |
| 956 | "tcp", |
| 957 | "prflx", |
| 958 | "tcp", |
| 959 | 3000); |
| 960 | const P2PTransportChannelTestBase::Result |
| 961 | P2PTransportChannelTestBase::kPrflxTcpToLocalTcp("prflx", |
| 962 | "tcp", |
| 963 | "local", |
| 964 | "tcp", |
| 965 | 3000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 966 | |
| 967 | // Test the matrix of all the connectivity types we expect to see in the wild. |
| 968 | // Just test every combination of the configs in the Config enum. |
| 969 | class P2PTransportChannelTest : public P2PTransportChannelTestBase { |
| 970 | protected: |
| 971 | static const Result* kMatrix[NUM_CONFIGS][NUM_CONFIGS]; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 972 | void ConfigureEndpoints(Config config1, |
| 973 | Config config2, |
| 974 | int allocator_flags1, |
| 975 | int allocator_flags2) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 976 | ConfigureEndpoint(0, config1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 977 | SetAllocatorFlags(0, allocator_flags1); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 978 | SetAllocationStepDelay(0, kMinimumStepDelay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 979 | ConfigureEndpoint(1, config2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 980 | SetAllocatorFlags(1, allocator_flags2); |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 981 | SetAllocationStepDelay(1, kMinimumStepDelay); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 982 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 983 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 984 | } |
| 985 | void ConfigureEndpoint(int endpoint, Config config) { |
| 986 | switch (config) { |
| 987 | case OPEN: |
| 988 | AddAddress(endpoint, kPublicAddrs[endpoint]); |
| 989 | break; |
| 990 | case NAT_FULL_CONE: |
| 991 | case NAT_ADDR_RESTRICTED: |
| 992 | case NAT_PORT_RESTRICTED: |
| 993 | case NAT_SYMMETRIC: |
| 994 | AddAddress(endpoint, kPrivateAddrs[endpoint]); |
| 995 | // Add a single NAT of the desired type |
| 996 | nat()->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint], |
| 997 | static_cast<rtc::NATType>(config - NAT_FULL_CONE))-> |
| 998 | AddClient(kPrivateAddrs[endpoint]); |
| 999 | break; |
| 1000 | case NAT_DOUBLE_CONE: |
| 1001 | case NAT_SYMMETRIC_THEN_CONE: |
| 1002 | AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]); |
| 1003 | // Add a two cascaded NATs of the desired types |
| 1004 | nat()->AddTranslator(kPublicAddrs[endpoint], kNatAddrs[endpoint], |
| 1005 | (config == NAT_DOUBLE_CONE) ? |
| 1006 | rtc::NAT_OPEN_CONE : rtc::NAT_SYMMETRIC)-> |
| 1007 | AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint], |
| 1008 | rtc::NAT_OPEN_CONE)-> |
| 1009 | AddClient(kCascadedPrivateAddrs[endpoint]); |
| 1010 | break; |
| 1011 | case BLOCK_UDP: |
| 1012 | case BLOCK_UDP_AND_INCOMING_TCP: |
| 1013 | case BLOCK_ALL_BUT_OUTGOING_HTTP: |
| 1014 | case PROXY_HTTPS: |
| 1015 | case PROXY_SOCKS: |
| 1016 | AddAddress(endpoint, kPublicAddrs[endpoint]); |
| 1017 | // Block all UDP |
| 1018 | fw()->AddRule(false, rtc::FP_UDP, rtc::FD_ANY, |
| 1019 | kPublicAddrs[endpoint]); |
| 1020 | if (config == BLOCK_UDP_AND_INCOMING_TCP) { |
| 1021 | // Block TCP inbound to the endpoint |
| 1022 | fw()->AddRule(false, rtc::FP_TCP, SocketAddress(), |
| 1023 | kPublicAddrs[endpoint]); |
| 1024 | } else if (config == BLOCK_ALL_BUT_OUTGOING_HTTP) { |
| 1025 | // Block all TCP to/from the endpoint except 80/443 out |
| 1026 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1027 | SocketAddress(rtc::IPAddress(INADDR_ANY), 80)); |
| 1028 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1029 | SocketAddress(rtc::IPAddress(INADDR_ANY), 443)); |
| 1030 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1031 | kPublicAddrs[endpoint]); |
| 1032 | } else if (config == PROXY_HTTPS) { |
| 1033 | // Block all TCP to/from the endpoint except to the proxy server |
| 1034 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1035 | kHttpsProxyAddrs[endpoint]); |
| 1036 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1037 | kPublicAddrs[endpoint]); |
| 1038 | SetProxy(endpoint, rtc::PROXY_HTTPS); |
| 1039 | } else if (config == PROXY_SOCKS) { |
| 1040 | // Block all TCP to/from the endpoint except to the proxy server |
| 1041 | fw()->AddRule(true, rtc::FP_TCP, kPublicAddrs[endpoint], |
| 1042 | kSocksProxyAddrs[endpoint]); |
| 1043 | fw()->AddRule(false, rtc::FP_TCP, rtc::FD_ANY, |
| 1044 | kPublicAddrs[endpoint]); |
| 1045 | SetProxy(endpoint, rtc::PROXY_SOCKS5); |
| 1046 | } |
| 1047 | break; |
| 1048 | default: |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | }; |
| 1053 | |
| 1054 | // Shorthands for use in the test matrix. |
| 1055 | #define LULU &kLocalUdpToLocalUdp |
| 1056 | #define LUSU &kLocalUdpToStunUdp |
| 1057 | #define LUPU &kLocalUdpToPrflxUdp |
| 1058 | #define PULU &kPrflxUdpToLocalUdp |
| 1059 | #define SULU &kStunUdpToLocalUdp |
| 1060 | #define SUSU &kStunUdpToStunUdp |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1061 | #define SUPU &kStunUdpToPrflxUdp |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1062 | #define PUSU &kPrflxUdpToStunUdp |
| 1063 | #define LURU &kLocalUdpToRelayUdp |
| 1064 | #define PURU &kPrflxUdpToRelayUdp |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1065 | #define RUPU &kRelayUdpToPrflxUdp |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1066 | #define LTLT &kLocalTcpToLocalTcp |
| 1067 | #define LTPT &kLocalTcpToPrflxTcp |
| 1068 | #define PTLT &kPrflxTcpToLocalTcp |
| 1069 | // TODO: Enable these once TestRelayServer can accept external TCP. |
| 1070 | #define LTRT NULL |
| 1071 | #define LSRS NULL |
| 1072 | |
| 1073 | // Test matrix. Originator behavior defined by rows, receiever by columns. |
| 1074 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1075 | // TODO: Fix NULLs caused by lack of TCP support in NATSocket. |
| 1076 | // TODO: Fix NULLs caused by no HTTP proxy support. |
| 1077 | // TODO: Rearrange rows/columns from best to worst. |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1078 | const P2PTransportChannelTest::Result* |
| 1079 | P2PTransportChannelTest::kMatrix[NUM_CONFIGS][NUM_CONFIGS] = { |
| 1080 | // OPEN CONE ADDR PORT SYMM 2CON SCON !UDP !TCP HTTP PRXH PRXS |
| 1081 | /*OP*/ {LULU, LUSU, LUSU, LUSU, LUPU, LUSU, LUPU, LTPT, LTPT, LSRS, NULL, LTPT}, |
| 1082 | /*CO*/ {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1083 | /*AD*/ {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1084 | /*PO*/ {SULU, SUSU, SUSU, SUSU, RUPU, SUSU, RUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1085 | /*SY*/ {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1086 | /*2C*/ {SULU, SUSU, SUSU, SUSU, SUPU, SUSU, SUPU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1087 | /*SC*/ {PULU, PUSU, PUSU, PURU, PURU, PUSU, PURU, NULL, NULL, LSRS, NULL, LTRT}, |
| 1088 | /*!U*/ {LTPT, NULL, NULL, NULL, NULL, NULL, NULL, LTPT, LTPT, LSRS, NULL, LTRT}, |
| 1089 | /*!T*/ {PTLT, NULL, NULL, NULL, NULL, NULL, NULL, PTLT, LTRT, LSRS, NULL, LTRT}, |
| 1090 | /*HT*/ {LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, LSRS, NULL, LSRS}, |
| 1091 | /*PR*/ {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, |
| 1092 | /*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] | 1093 | }; |
| 1094 | |
| 1095 | // The actual tests that exercise all the various configurations. |
| 1096 | // Test names are of the form P2PTransportChannelTest_TestOPENToNAT_FULL_CONE |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1097 | #define P2P_TEST_DECLARATION(x, y, z) \ |
| 1098 | TEST_F(P2PTransportChannelTest, z##Test##x##To##y) { \ |
| 1099 | ConfigureEndpoints(x, y, PORTALLOCATOR_ENABLE_SHARED_SOCKET, \ |
| 1100 | PORTALLOCATOR_ENABLE_SHARED_SOCKET); \ |
Taylor Brandstetter | 7e1b8fb | 2016-05-26 15:21:45 -0700 | [diff] [blame] | 1101 | if (kMatrix[x][y] != NULL) \ |
| 1102 | Test(*kMatrix[x][y]); \ |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1103 | else \ |
| 1104 | LOG(LS_WARNING) << "Not yet implemented"; \ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | #define P2P_TEST(x, y) \ |
| 1108 | P2P_TEST_DECLARATION(x, y,) |
| 1109 | |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1110 | #define P2P_TEST_SET(x) \ |
| 1111 | P2P_TEST(x, OPEN) \ |
| 1112 | P2P_TEST(x, NAT_FULL_CONE) \ |
| 1113 | P2P_TEST(x, NAT_ADDR_RESTRICTED) \ |
| 1114 | P2P_TEST(x, NAT_PORT_RESTRICTED) \ |
| 1115 | P2P_TEST(x, NAT_SYMMETRIC) \ |
| 1116 | P2P_TEST(x, NAT_DOUBLE_CONE) \ |
| 1117 | P2P_TEST(x, NAT_SYMMETRIC_THEN_CONE) \ |
| 1118 | P2P_TEST(x, BLOCK_UDP) \ |
| 1119 | P2P_TEST(x, BLOCK_UDP_AND_INCOMING_TCP) \ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1120 | P2P_TEST(x, BLOCK_ALL_BUT_OUTGOING_HTTP) \ |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1121 | P2P_TEST(x, PROXY_HTTPS) \ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1122 | P2P_TEST(x, PROXY_SOCKS) |
| 1123 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1124 | P2P_TEST_SET(OPEN) |
| 1125 | P2P_TEST_SET(NAT_FULL_CONE) |
| 1126 | P2P_TEST_SET(NAT_ADDR_RESTRICTED) |
| 1127 | P2P_TEST_SET(NAT_PORT_RESTRICTED) |
| 1128 | P2P_TEST_SET(NAT_SYMMETRIC) |
| 1129 | P2P_TEST_SET(NAT_DOUBLE_CONE) |
| 1130 | P2P_TEST_SET(NAT_SYMMETRIC_THEN_CONE) |
| 1131 | P2P_TEST_SET(BLOCK_UDP) |
| 1132 | P2P_TEST_SET(BLOCK_UDP_AND_INCOMING_TCP) |
| 1133 | P2P_TEST_SET(BLOCK_ALL_BUT_OUTGOING_HTTP) |
| 1134 | P2P_TEST_SET(PROXY_HTTPS) |
| 1135 | P2P_TEST_SET(PROXY_SOCKS) |
| 1136 | |
| 1137 | // Test that we restart candidate allocation when local ufrag&pwd changed. |
| 1138 | // Standard Ice protocol is used. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1139 | TEST_F(P2PTransportChannelTest, HandleUfragPwdChange) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1140 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1141 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1142 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1143 | TestHandleIceUfragPasswordChanged(); |
| 1144 | DestroyChannels(); |
| 1145 | } |
| 1146 | |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1147 | // Same as above test, but with a symmetric NAT. |
| 1148 | // We should end up with relay<->prflx candidate pairs, with generation "1". |
| 1149 | TEST_F(P2PTransportChannelTest, HandleUfragPwdChangeSymmetricNat) { |
| 1150 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 1151 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1152 | CreateChannels(); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1153 | TestHandleIceUfragPasswordChanged(); |
| 1154 | DestroyChannels(); |
| 1155 | } |
| 1156 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1157 | // Test the operation of GetStats. |
| 1158 | TEST_F(P2PTransportChannelTest, GetStats) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1159 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1160 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1161 | CreateChannels(); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1162 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1163 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1164 | 1000, 1000); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1165 | TestSendRecv(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1166 | ConnectionInfos infos; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1167 | ASSERT_TRUE(ep1_ch1()->GetStats(&infos)); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1168 | ASSERT_TRUE(infos.size() >= 1); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1169 | ConnectionInfo* best_conn_info = nullptr; |
| 1170 | for (ConnectionInfo& info : infos) { |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1171 | if (info.best_connection) { |
| 1172 | best_conn_info = &info; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | ASSERT_TRUE(best_conn_info != nullptr); |
| 1177 | EXPECT_TRUE(best_conn_info->new_connection); |
| 1178 | EXPECT_TRUE(best_conn_info->receiving); |
| 1179 | EXPECT_TRUE(best_conn_info->writable); |
| 1180 | EXPECT_FALSE(best_conn_info->timeout); |
| 1181 | EXPECT_EQ(10U, best_conn_info->sent_total_packets); |
| 1182 | EXPECT_EQ(0U, best_conn_info->sent_discarded_packets); |
| 1183 | EXPECT_EQ(10 * 36U, best_conn_info->sent_total_bytes); |
| 1184 | EXPECT_EQ(10 * 36U, best_conn_info->recv_total_bytes); |
| 1185 | EXPECT_GT(best_conn_info->rtt, 0U); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1186 | DestroyChannels(); |
| 1187 | } |
| 1188 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 1189 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1190 | // is disconnected. |
| 1191 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileDisconnected) { |
| 1192 | rtc::ScopedFakeClock clock; |
| 1193 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1194 | |
| 1195 | CreateChannels(); |
| 1196 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1197 | ep2_ch1()->receiving() && |
| 1198 | ep2_ch1()->writable(), |
| 1199 | kDefaultTimeout, clock); |
| 1200 | |
| 1201 | // Drop all packets so that both channels become not writable. |
| 1202 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 1203 | const int kWriteTimeoutDelay = 6000; |
| 1204 | EXPECT_TRUE_SIMULATED_WAIT(!ep1_ch1()->writable() && !ep2_ch1()->writable(), |
| 1205 | kWriteTimeoutDelay, clock); |
| 1206 | |
| 1207 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1208 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1209 | ep1_ch1()->MaybeStartGathering(); |
| 1210 | EXPECT_EQ(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1211 | webrtc::kEnumCounterIceRestart, |
| 1212 | static_cast<int>(IceRestartState::DISCONNECTED))); |
| 1213 | |
| 1214 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1215 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1216 | ep2_ch1()->MaybeStartGathering(); |
| 1217 | EXPECT_EQ(1, GetMetricsObserver(1)->GetEnumCounter( |
| 1218 | webrtc::kEnumCounterIceRestart, |
| 1219 | static_cast<int>(IceRestartState::DISCONNECTED))); |
| 1220 | |
| 1221 | DestroyChannels(); |
| 1222 | } |
| 1223 | |
| 1224 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1225 | // is connected. |
| 1226 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileConnected) { |
| 1227 | rtc::ScopedFakeClock clock; |
| 1228 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1229 | |
| 1230 | CreateChannels(); |
| 1231 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1232 | ep2_ch1()->receiving() && |
| 1233 | ep2_ch1()->writable(), |
| 1234 | kDefaultTimeout, clock); |
| 1235 | |
| 1236 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1237 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1238 | ep1_ch1()->MaybeStartGathering(); |
| 1239 | EXPECT_EQ(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1240 | webrtc::kEnumCounterIceRestart, |
| 1241 | static_cast<int>(IceRestartState::CONNECTED))); |
| 1242 | |
| 1243 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1244 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1245 | ep2_ch1()->MaybeStartGathering(); |
| 1246 | EXPECT_EQ(1, GetMetricsObserver(1)->GetEnumCounter( |
| 1247 | webrtc::kEnumCounterIceRestart, |
| 1248 | static_cast<int>(IceRestartState::CONNECTED))); |
| 1249 | |
| 1250 | DestroyChannels(); |
| 1251 | } |
| 1252 | |
| 1253 | // Tests that UMAs are recorded when ICE restarts while the channel |
| 1254 | // is connecting. |
| 1255 | TEST_F(P2PTransportChannelTest, TestUMAIceRestartWhileConnecting) { |
| 1256 | rtc::ScopedFakeClock clock; |
| 1257 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1258 | |
| 1259 | // Create the channels without waiting for them to become connected. |
| 1260 | CreateChannels(); |
| 1261 | |
| 1262 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1263 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1264 | ep1_ch1()->MaybeStartGathering(); |
| 1265 | EXPECT_EQ(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1266 | webrtc::kEnumCounterIceRestart, |
| 1267 | static_cast<int>(IceRestartState::CONNECTING))); |
| 1268 | |
| 1269 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1270 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1271 | ep2_ch1()->MaybeStartGathering(); |
| 1272 | EXPECT_EQ(1, GetMetricsObserver(1)->GetEnumCounter( |
| 1273 | webrtc::kEnumCounterIceRestart, |
| 1274 | static_cast<int>(IceRestartState::CONNECTING))); |
| 1275 | |
| 1276 | DestroyChannels(); |
| 1277 | } |
| 1278 | |
| 1279 | // Tests that a UMA on ICE regathering is recorded when there is a network |
| 1280 | // change if and only if continual gathering is enabled. |
| 1281 | TEST_F(P2PTransportChannelTest, |
| 1282 | TestIceRegatheringReasonContinualGatheringByNetworkChange) { |
| 1283 | rtc::ScopedFakeClock clock; |
| 1284 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1285 | |
| 1286 | // ep1 gathers continually but ep2 does not. |
| 1287 | IceConfig continual_gathering_config = |
| 1288 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1289 | IceConfig default_config; |
| 1290 | CreateChannels(continual_gathering_config, default_config); |
| 1291 | |
| 1292 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1293 | ep2_ch1()->receiving() && |
| 1294 | ep2_ch1()->writable(), |
| 1295 | kDefaultTimeout, clock); |
| 1296 | |
| 1297 | // Adding address in ep1 will trigger continual gathering. |
| 1298 | AddAddress(0, kAlternateAddrs[0]); |
| 1299 | EXPECT_EQ_SIMULATED_WAIT( |
| 1300 | 1, GetMetricsObserver(0)->GetEnumCounter( |
| 1301 | webrtc::kEnumCounterIceRegathering, |
| 1302 | static_cast<int>(IceRegatheringReason::NETWORK_CHANGE)), |
| 1303 | kDefaultTimeout, clock); |
| 1304 | |
| 1305 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1306 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
| 1307 | ep2_ch1()->MaybeStartGathering(); |
| 1308 | |
| 1309 | AddAddress(1, kAlternateAddrs[1]); |
| 1310 | SIMULATED_WAIT(false, kDefaultTimeout, clock); |
| 1311 | // ep2 has not enabled continual gathering. |
| 1312 | EXPECT_EQ(0, GetMetricsObserver(1)->GetEnumCounter( |
| 1313 | webrtc::kEnumCounterIceRegathering, |
| 1314 | static_cast<int>(IceRegatheringReason::NETWORK_CHANGE))); |
| 1315 | |
| 1316 | DestroyChannels(); |
| 1317 | } |
| 1318 | |
| 1319 | // Tests that a UMA on ICE regathering is recorded when there is a network |
| 1320 | // failure if and only if continual gathering is enabled. |
| 1321 | TEST_F(P2PTransportChannelTest, |
| 1322 | TestIceRegatheringReasonContinualGatheringByNetworkFailure) { |
| 1323 | rtc::ScopedFakeClock clock; |
| 1324 | ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); |
| 1325 | |
| 1326 | // ep1 gathers continually but ep2 does not. |
| 1327 | IceConfig config1 = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 1328 | config1.regather_on_failed_networks_interval = rtc::Optional<int>(2000); |
| 1329 | IceConfig config2; |
| 1330 | config2.regather_on_failed_networks_interval = rtc::Optional<int>(2000); |
| 1331 | CreateChannels(config1, config2); |
| 1332 | |
| 1333 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1334 | ep2_ch1()->receiving() && |
| 1335 | ep2_ch1()->writable(), |
| 1336 | kDefaultTimeout, clock); |
| 1337 | |
| 1338 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 1339 | // Timeout value such that all connections are deleted. |
| 1340 | const int kNetworkFailureTimeout = 35000; |
| 1341 | SIMULATED_WAIT(false, kNetworkFailureTimeout, clock); |
| 1342 | EXPECT_LE(1, GetMetricsObserver(0)->GetEnumCounter( |
| 1343 | webrtc::kEnumCounterIceRegathering, |
| 1344 | static_cast<int>(IceRegatheringReason::NETWORK_FAILURE))); |
| 1345 | EXPECT_EQ(0, GetMetricsObserver(1)->GetEnumCounter( |
| 1346 | webrtc::kEnumCounterIceRegathering, |
| 1347 | static_cast<int>(IceRegatheringReason::NETWORK_FAILURE))); |
| 1348 | |
| 1349 | DestroyChannels(); |
| 1350 | } |
| 1351 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1352 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1353 | // when the signaling is slow. |
| 1354 | TEST_F(P2PTransportChannelTest, PeerReflexiveCandidateBeforeSignaling) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1355 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1356 | kDefaultPortAllocatorFlags); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1357 | // Emulate no remote parameters coming in. |
| 1358 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1359 | CreateChannels(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1360 | // Only have remote parameters come in for ep2, not ep1. |
| 1361 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1364 | // candidate. |
| 1365 | PauseCandidates(1); |
| 1366 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1367 | // Wait until the callee becomes writable to make sure that a ping request is |
| 1368 | // received by the caller before his remote ICE credentials are set. |
| 1369 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, 3000); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1370 | // Add two sets of remote ICE credentials, so that the ones used by the |
| 1371 | // candidate will be generation 1 instead of 0. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1372 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1373 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1374 | // The caller should have the selected connection connected to the peer |
| 1375 | // reflexive candidate. |
| 1376 | const Connection* selected_connection = nullptr; |
| 1377 | ASSERT_TRUE_WAIT( |
| 1378 | (selected_connection = ep1_ch1()->selected_connection()) != nullptr, |
| 1379 | 2000); |
| 1380 | EXPECT_EQ("prflx", selected_connection->remote_candidate().type()); |
| 1381 | EXPECT_EQ(kIceUfrag[1], selected_connection->remote_candidate().username()); |
| 1382 | EXPECT_EQ(kIcePwd[1], selected_connection->remote_candidate().password()); |
| 1383 | EXPECT_EQ(1u, selected_connection->remote_candidate().generation()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1384 | |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1385 | ResumeCandidates(1); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1386 | // Verify ep1's selected connection is updated to use the 'local' candidate. |
| 1387 | EXPECT_EQ_WAIT("local", |
| 1388 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
| 1389 | 2000); |
| 1390 | EXPECT_EQ(selected_connection, ep1_ch1()->selected_connection()); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1391 | DestroyChannels(); |
| 1392 | } |
| 1393 | |
| 1394 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1395 | // when the signaling is slow and the end points are behind NAT. |
| 1396 | TEST_F(P2PTransportChannelTest, PeerReflexiveCandidateBeforeSignalingWithNAT) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1397 | ConfigureEndpoints(OPEN, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1398 | kDefaultPortAllocatorFlags); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1399 | // Emulate no remote parameters coming in. |
| 1400 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1401 | CreateChannels(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1402 | // Only have remote parameters come in for ep2, not ep1. |
| 1403 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1404 | // Pause sending ep2's candidates to ep1 until ep1 receives the peer reflexive |
| 1405 | // candidate. |
| 1406 | PauseCandidates(1); |
| 1407 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1408 | // Wait until the callee becomes writable to make sure that a ping request is |
| 1409 | // received by the caller before his remote ICE credentials are set. |
| 1410 | ASSERT_TRUE_WAIT(ep2_ch1()->selected_connection() != nullptr, 3000); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1411 | // Add two sets of remote ICE credentials, so that the ones used by the |
| 1412 | // candidate will be generation 1 instead of 0. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1413 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
| 1414 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1415 | |
| 1416 | // The caller's selected connection should be connected to the peer reflexive |
| 1417 | // candidate. |
| 1418 | const Connection* selected_connection = nullptr; |
| 1419 | ASSERT_TRUE_WAIT( |
| 1420 | (selected_connection = ep1_ch1()->selected_connection()) != nullptr, |
| 1421 | 2000); |
| 1422 | EXPECT_EQ("prflx", selected_connection->remote_candidate().type()); |
| 1423 | EXPECT_EQ(kIceUfrag[1], selected_connection->remote_candidate().username()); |
| 1424 | EXPECT_EQ(kIcePwd[1], selected_connection->remote_candidate().password()); |
| 1425 | EXPECT_EQ(1u, selected_connection->remote_candidate().generation()); |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1426 | |
| 1427 | ResumeCandidates(1); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 1428 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1429 | EXPECT_EQ_WAIT("prflx", |
| 1430 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
| 1431 | 2000); |
| 1432 | EXPECT_EQ(selected_connection, ep1_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1433 | DestroyChannels(); |
| 1434 | } |
| 1435 | |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1436 | // Test that we properly create a connection on a STUN ping from unknown address |
| 1437 | // when the signaling is slow, even if the new candidate is created due to the |
| 1438 | // remote peer doing an ICE restart, pairing this candidate across generations. |
| 1439 | // |
| 1440 | // Previously this wasn't working due to a bug where the peer reflexive |
| 1441 | // candidate was only updated for the newest generation candidate pairs, and |
| 1442 | // not older-generation candidate pairs created by pairing candidates across |
| 1443 | // generations. This resulted in the old-generation prflx candidate being |
| 1444 | // prioritized above new-generation candidate pairs. |
| 1445 | TEST_F(P2PTransportChannelTest, |
| 1446 | PeerReflexiveCandidateBeforeSignalingWithIceRestart) { |
| 1447 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1448 | kDefaultPortAllocatorFlags); |
| 1449 | // Only gather relay candidates, so that when the prflx candidate arrives |
| 1450 | // it's prioritized above the current candidate pair. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1451 | GetEndpoint(0)->allocator_->set_candidate_filter(CF_RELAY); |
| 1452 | GetEndpoint(1)->allocator_->set_candidate_filter(CF_RELAY); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1453 | // Setting this allows us to control when SetRemoteIceParameters is called. |
| 1454 | set_remote_ice_parameter_source(FROM_CANDIDATE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1455 | CreateChannels(); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1456 | // Wait for the initial connection to be made. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1457 | ep1_ch1()->SetRemoteIceParameters(kIceParams[1]); |
| 1458 | ep2_ch1()->SetRemoteIceParameters(kIceParams[0]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1459 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1460 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1461 | kDefaultTimeout); |
| 1462 | |
| 1463 | // 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] | 1464 | // ICE parameters until after a prflx connection has been made. |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1465 | PauseCandidates(1); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1466 | ep2_ch1()->SetIceParameters(kIceParams[3]); |
| 1467 | |
| 1468 | ep1_ch1()->SetRemoteIceParameters(kIceParams[3]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1469 | ep2_ch1()->MaybeStartGathering(); |
| 1470 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1471 | // The caller should have the selected connection connected to the peer |
| 1472 | // reflexive candidate. |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1473 | EXPECT_EQ_WAIT("prflx", |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1474 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1475 | kDefaultTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1476 | const Connection* prflx_selected_connection = |
| 1477 | ep1_ch1()->selected_connection(); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1478 | |
| 1479 | // Now simulate the ICE restart on ep1. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1480 | ep1_ch1()->SetIceParameters(kIceParams[2]); |
| 1481 | |
| 1482 | ep2_ch1()->SetRemoteIceParameters(kIceParams[2]); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1483 | ep1_ch1()->MaybeStartGathering(); |
| 1484 | |
| 1485 | // Finally send the candidates from ep2's ICE restart and verify that ep1 uses |
| 1486 | // their information to update the peer reflexive candidate. |
| 1487 | ResumeCandidates(1); |
| 1488 | |
| 1489 | EXPECT_EQ_WAIT("relay", |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1490 | ep1_ch1()->selected_connection()->remote_candidate().type(), |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1491 | kDefaultTimeout); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1492 | EXPECT_EQ(prflx_selected_connection, ep1_ch1()->selected_connection()); |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 1493 | DestroyChannels(); |
| 1494 | } |
| 1495 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1496 | // Test that if remote candidates don't have ufrag and pwd, we still work. |
| 1497 | TEST_F(P2PTransportChannelTest, RemoteCandidatesWithoutUfragPwd) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1498 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1499 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1500 | kDefaultPortAllocatorFlags); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1501 | CreateChannels(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1502 | const Connection* selected_connection = NULL; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1503 | // Wait until the callee's connections are created. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1504 | WAIT((selected_connection = ep2_ch1()->selected_connection()) != NULL, 1000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1505 | // Wait to see if they get culled; they shouldn't. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1506 | WAIT(ep2_ch1()->selected_connection() != selected_connection, 1000); |
| 1507 | EXPECT_TRUE(ep2_ch1()->selected_connection() == selected_connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1508 | DestroyChannels(); |
| 1509 | } |
| 1510 | |
| 1511 | // Test that a host behind NAT cannot be reached when incoming_only |
| 1512 | // is set to true. |
| 1513 | TEST_F(P2PTransportChannelTest, IncomingOnlyBlocked) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1514 | ConfigureEndpoints(NAT_FULL_CONE, OPEN, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1515 | kDefaultPortAllocatorFlags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1516 | |
| 1517 | SetAllocatorFlags(0, kOnlyLocalPorts); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1518 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1519 | ep1_ch1()->set_incoming_only(true); |
| 1520 | |
| 1521 | // Pump for 1 second and verify that the channels are not connected. |
| 1522 | rtc::Thread::Current()->ProcessMessages(1000); |
| 1523 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1524 | EXPECT_FALSE(ep1_ch1()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1525 | EXPECT_FALSE(ep1_ch1()->writable()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1526 | EXPECT_FALSE(ep2_ch1()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1527 | EXPECT_FALSE(ep2_ch1()->writable()); |
| 1528 | |
| 1529 | DestroyChannels(); |
| 1530 | } |
| 1531 | |
| 1532 | // Test that a peer behind NAT can connect to a peer that has |
| 1533 | // incoming_only flag set. |
| 1534 | TEST_F(P2PTransportChannelTest, IncomingOnlyOpen) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1535 | ConfigureEndpoints(OPEN, NAT_FULL_CONE, kDefaultPortAllocatorFlags, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1536 | kDefaultPortAllocatorFlags); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1537 | |
| 1538 | SetAllocatorFlags(0, kOnlyLocalPorts); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1539 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1540 | ep1_ch1()->set_incoming_only(true); |
| 1541 | |
| 1542 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1543 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1544 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1545 | 1000, 1000); |
| 1546 | |
| 1547 | DestroyChannels(); |
| 1548 | } |
| 1549 | |
| 1550 | TEST_F(P2PTransportChannelTest, TestTcpConnectionsFromActiveToPassive) { |
| 1551 | AddAddress(0, kPublicAddrs[0]); |
| 1552 | AddAddress(1, kPublicAddrs[1]); |
| 1553 | |
| 1554 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1555 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1556 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1557 | int kOnlyLocalTcpPorts = PORTALLOCATOR_DISABLE_UDP | |
| 1558 | PORTALLOCATOR_DISABLE_STUN | |
| 1559 | PORTALLOCATOR_DISABLE_RELAY; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1560 | // Disable all protocols except TCP. |
| 1561 | SetAllocatorFlags(0, kOnlyLocalTcpPorts); |
| 1562 | SetAllocatorFlags(1, kOnlyLocalTcpPorts); |
| 1563 | |
| 1564 | SetAllowTcpListen(0, true); // actpass. |
| 1565 | SetAllowTcpListen(1, false); // active. |
| 1566 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1567 | // We want SetRemoteIceParameters to be called as it normally would. |
| 1568 | // Otherwise we won't know what parameters to use for the expected |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1569 | // prflx TCP candidates. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1570 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1571 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1572 | // Pause candidate so we could verify the candidate properties. |
| 1573 | PauseCandidates(0); |
| 1574 | PauseCandidates(1); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1575 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1576 | |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1577 | // Verify tcp candidates. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1578 | VerifySavedTcpCandidates(0, TCPTYPE_PASSIVE_STR); |
| 1579 | VerifySavedTcpCandidates(1, TCPTYPE_ACTIVE_STR); |
Guo-wei Shieh | 310b093 | 2015-11-17 19:15:50 -0800 | [diff] [blame] | 1580 | |
| 1581 | // Resume candidates. |
| 1582 | ResumeCandidates(0); |
| 1583 | ResumeCandidates(1); |
| 1584 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1585 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1586 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1587 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1588 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1589 | ep2_ch1()->selected_connection() && |
| 1590 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 1591 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1592 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1593 | TestSendRecv(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1594 | DestroyChannels(); |
| 1595 | } |
| 1596 | |
Peter Thatcher | 73ba7a6 | 2015-04-14 09:26:03 -0700 | [diff] [blame] | 1597 | TEST_F(P2PTransportChannelTest, TestIceRoleConflict) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1598 | AddAddress(0, kPublicAddrs[0]); |
| 1599 | AddAddress(1, kPublicAddrs[1]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1600 | TestSignalRoleConflict(); |
| 1601 | } |
| 1602 | |
| 1603 | // Tests that the ice configs (protocol, tiebreaker and role) can be passed |
| 1604 | // down to ports. |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1605 | TEST_F(P2PTransportChannelTest, TestIceConfigWillPassDownToPort) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1606 | AddAddress(0, kPublicAddrs[0]); |
| 1607 | AddAddress(1, kPublicAddrs[1]); |
| 1608 | |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1609 | // Give the first connection the higher tiebreaker so its role won't |
| 1610 | // change unless we tell it to. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1611 | SetIceRole(0, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1612 | SetIceTiebreaker(0, kHighTiebreaker); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1613 | SetIceRole(1, ICEROLE_CONTROLLING); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1614 | SetIceTiebreaker(1, kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1615 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1616 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1617 | |
| 1618 | EXPECT_EQ_WAIT(2u, ep1_ch1()->ports().size(), 1000); |
| 1619 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1620 | const std::vector<PortInterface*> ports_before = ep1_ch1()->ports(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1621 | for (size_t i = 0; i < ports_before.size(); ++i) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1622 | EXPECT_EQ(ICEROLE_CONTROLLING, ports_before[i]->GetIceRole()); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1623 | EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1626 | ep1_ch1()->SetIceRole(ICEROLE_CONTROLLED); |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1627 | ep1_ch1()->SetIceTiebreaker(kLowTiebreaker); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1628 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1629 | const std::vector<PortInterface*> ports_after = ep1_ch1()->ports(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1630 | for (size_t i = 0; i < ports_after.size(); ++i) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1631 | EXPECT_EQ(ICEROLE_CONTROLLED, ports_before[i]->GetIceRole()); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1632 | // SetIceTiebreaker after ports have been created will fail. So expect the |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1633 | // original value. |
Taylor Brandstetter | f3d8d32 | 2016-06-29 11:07:36 -0700 | [diff] [blame] | 1634 | EXPECT_EQ(kHighTiebreaker, ports_before[i]->IceTiebreaker()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1637 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1638 | ep1_ch1()->writable() && |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1639 | ep2_ch1()->receiving() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1640 | ep2_ch1()->writable(), |
| 1641 | 1000); |
| 1642 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1643 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1644 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1645 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1646 | TestSendRecv(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1647 | DestroyChannels(); |
| 1648 | } |
| 1649 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1650 | // Verify that we can set DSCP value and retrieve properly from P2PTC. |
| 1651 | TEST_F(P2PTransportChannelTest, TestDefaultDscpValue) { |
| 1652 | AddAddress(0, kPublicAddrs[0]); |
| 1653 | AddAddress(1, kPublicAddrs[1]); |
| 1654 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1655 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1656 | EXPECT_EQ(rtc::DSCP_NO_CHANGE, |
| 1657 | GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1658 | EXPECT_EQ(rtc::DSCP_NO_CHANGE, |
| 1659 | GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1660 | GetEndpoint(0)->cd1_.ch_->SetOption( |
| 1661 | rtc::Socket::OPT_DSCP, rtc::DSCP_CS6); |
| 1662 | GetEndpoint(1)->cd1_.ch_->SetOption( |
| 1663 | rtc::Socket::OPT_DSCP, rtc::DSCP_CS6); |
| 1664 | EXPECT_EQ(rtc::DSCP_CS6, |
| 1665 | GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1666 | EXPECT_EQ(rtc::DSCP_CS6, |
| 1667 | GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1668 | GetEndpoint(0)->cd1_.ch_->SetOption( |
| 1669 | rtc::Socket::OPT_DSCP, rtc::DSCP_AF41); |
| 1670 | GetEndpoint(1)->cd1_.ch_->SetOption( |
| 1671 | rtc::Socket::OPT_DSCP, rtc::DSCP_AF41); |
| 1672 | EXPECT_EQ(rtc::DSCP_AF41, |
| 1673 | GetEndpoint(0)->cd1_.ch_->DefaultDscpValue()); |
| 1674 | EXPECT_EQ(rtc::DSCP_AF41, |
| 1675 | GetEndpoint(1)->cd1_.ch_->DefaultDscpValue()); |
| 1676 | } |
| 1677 | |
| 1678 | // Verify IPv6 connection is preferred over IPv4. |
guoweis@webrtc.org | 1f05c45 | 2014-12-15 21:25:54 +0000 | [diff] [blame] | 1679 | TEST_F(P2PTransportChannelTest, TestIPv6Connections) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1680 | AddAddress(0, kIPv6PublicAddrs[0]); |
| 1681 | AddAddress(0, kPublicAddrs[0]); |
| 1682 | AddAddress(1, kIPv6PublicAddrs[1]); |
| 1683 | AddAddress(1, kPublicAddrs[1]); |
| 1684 | |
| 1685 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1686 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1687 | |
| 1688 | // Enable IPv6 |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1689 | SetAllocatorFlags(0, PORTALLOCATOR_ENABLE_IPV6); |
| 1690 | SetAllocatorFlags(1, PORTALLOCATOR_ENABLE_IPV6); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1691 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1692 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1693 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1694 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1695 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1696 | 1000); |
| 1697 | EXPECT_TRUE( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1698 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1699 | LocalCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[0]) && |
| 1700 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kIPv6PublicAddrs[1])); |
| 1701 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1702 | TestSendRecv(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1703 | DestroyChannels(); |
| 1704 | } |
| 1705 | |
| 1706 | // Testing forceful TURN connections. |
| 1707 | TEST_F(P2PTransportChannelTest, TestForceTurn) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1708 | ConfigureEndpoints( |
| 1709 | NAT_PORT_RESTRICTED, NAT_SYMMETRIC, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1710 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET, |
| 1711 | kDefaultPortAllocatorFlags | PORTALLOCATOR_ENABLE_SHARED_SOCKET); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1712 | set_force_relay(true); |
| 1713 | |
| 1714 | SetAllocationStepDelay(0, kMinimumStepDelay); |
| 1715 | SetAllocationStepDelay(1, kMinimumStepDelay); |
| 1716 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1717 | CreateChannels(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1718 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1719 | EXPECT_TRUE_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1720 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1721 | 2000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1722 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1723 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 1724 | ep2_ch1()->selected_connection()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1725 | |
| 1726 | EXPECT_EQ("relay", RemoteCandidate(ep1_ch1())->type()); |
| 1727 | EXPECT_EQ("relay", LocalCandidate(ep1_ch1())->type()); |
| 1728 | EXPECT_EQ("relay", RemoteCandidate(ep2_ch1())->type()); |
| 1729 | EXPECT_EQ("relay", LocalCandidate(ep2_ch1())->type()); |
| 1730 | |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1731 | TestSendRecv(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1732 | DestroyChannels(); |
| 1733 | } |
| 1734 | |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1735 | // Test that if continual gathering is set to true, ICE gathering state will |
| 1736 | // not change to "Complete", and vice versa. |
| 1737 | TEST_F(P2PTransportChannelTest, TestContinualGathering) { |
| 1738 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1739 | kDefaultPortAllocatorFlags); |
| 1740 | SetAllocationStepDelay(0, kDefaultStepDelay); |
| 1741 | SetAllocationStepDelay(1, kDefaultStepDelay); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 1742 | IceConfig continual_gathering_config = |
| 1743 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1744 | // By default, ep2 does not gather continually. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 1745 | IceConfig default_config; |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1746 | CreateChannels(continual_gathering_config, default_config); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1747 | |
| 1748 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && |
| 1749 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1750 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1751 | 1000, 1000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1752 | WAIT(IceGatheringState::kIceGatheringComplete == ep1_ch1()->gathering_state(), |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1753 | 1000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1754 | EXPECT_EQ(IceGatheringState::kIceGatheringGathering, |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1755 | ep1_ch1()->gathering_state()); |
| 1756 | // By now, ep2 should have completed gathering. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1757 | EXPECT_EQ(IceGatheringState::kIceGatheringComplete, |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1758 | ep2_ch1()->gathering_state()); |
| 1759 | |
| 1760 | DestroyChannels(); |
| 1761 | } |
| 1762 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1763 | // Test that a connection succeeds when the P2PTransportChannel uses a pooled |
| 1764 | // PortAllocatorSession that has not yet finished gathering candidates. |
| 1765 | TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { |
| 1766 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1767 | kDefaultPortAllocatorFlags); |
| 1768 | // First create a pooled session for each endpoint. |
| 1769 | auto& allocator_1 = GetEndpoint(0)->allocator_; |
| 1770 | auto& allocator_2 = GetEndpoint(1)->allocator_; |
| 1771 | int pool_size = 1; |
| 1772 | allocator_1->SetConfiguration(allocator_1->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1773 | allocator_1->turn_servers(), pool_size, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1774 | allocator_2->SetConfiguration(allocator_2->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1775 | allocator_2->turn_servers(), pool_size, false); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1776 | const PortAllocatorSession* pooled_session_1 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1777 | allocator_1->GetPooledSession(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1778 | const PortAllocatorSession* pooled_session_2 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1779 | allocator_2->GetPooledSession(); |
| 1780 | ASSERT_NE(nullptr, pooled_session_1); |
| 1781 | ASSERT_NE(nullptr, pooled_session_2); |
| 1782 | // Sanity check that pooled sessions haven't gathered anything yet. |
| 1783 | EXPECT_TRUE(pooled_session_1->ReadyPorts().empty()); |
| 1784 | EXPECT_TRUE(pooled_session_1->ReadyCandidates().empty()); |
| 1785 | EXPECT_TRUE(pooled_session_2->ReadyPorts().empty()); |
| 1786 | EXPECT_TRUE(pooled_session_2->ReadyCandidates().empty()); |
| 1787 | // Now let the endpoints connect and try exchanging some data. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1788 | CreateChannels(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1789 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && |
| 1790 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1791 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1792 | 1000, 1000); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1793 | TestSendRecv(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1794 | // Make sure the P2PTransportChannels are actually using ports from the |
| 1795 | // pooled sessions. |
| 1796 | auto pooled_ports_1 = pooled_session_1->ReadyPorts(); |
| 1797 | auto pooled_ports_2 = pooled_session_2->ReadyPorts(); |
| 1798 | EXPECT_NE(pooled_ports_1.end(), |
| 1799 | std::find(pooled_ports_1.begin(), pooled_ports_1.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1800 | ep1_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1801 | EXPECT_NE(pooled_ports_2.end(), |
| 1802 | std::find(pooled_ports_2.begin(), pooled_ports_2.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1803 | ep2_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | // Test that a connection succeeds when the P2PTransportChannel uses a pooled |
| 1807 | // PortAllocatorSession that already finished gathering candidates. |
| 1808 | TEST_F(P2PTransportChannelTest, TestUsingPooledSessionAfterDoneGathering) { |
| 1809 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1810 | kDefaultPortAllocatorFlags); |
| 1811 | // First create a pooled session for each endpoint. |
| 1812 | auto& allocator_1 = GetEndpoint(0)->allocator_; |
| 1813 | auto& allocator_2 = GetEndpoint(1)->allocator_; |
| 1814 | int pool_size = 1; |
| 1815 | allocator_1->SetConfiguration(allocator_1->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1816 | allocator_1->turn_servers(), pool_size, false); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1817 | allocator_2->SetConfiguration(allocator_2->stun_servers(), |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1818 | allocator_2->turn_servers(), pool_size, false); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1819 | const PortAllocatorSession* pooled_session_1 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1820 | allocator_1->GetPooledSession(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1821 | const PortAllocatorSession* pooled_session_2 = |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1822 | allocator_2->GetPooledSession(); |
| 1823 | ASSERT_NE(nullptr, pooled_session_1); |
| 1824 | ASSERT_NE(nullptr, pooled_session_2); |
| 1825 | // Wait for the pooled sessions to finish gathering before the |
| 1826 | // P2PTransportChannels try to use them. |
| 1827 | EXPECT_TRUE_WAIT(pooled_session_1->CandidatesAllocationDone() && |
| 1828 | pooled_session_2->CandidatesAllocationDone(), |
| 1829 | kDefaultTimeout); |
| 1830 | // Now let the endpoints connect and try exchanging some data. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1831 | CreateChannels(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1832 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1() != NULL && ep2_ch1() != NULL && |
| 1833 | ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1834 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 1835 | 1000, 1000); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 1836 | TestSendRecv(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1837 | // Make sure the P2PTransportChannels are actually using ports from the |
| 1838 | // pooled sessions. |
| 1839 | auto pooled_ports_1 = pooled_session_1->ReadyPorts(); |
| 1840 | auto pooled_ports_2 = pooled_session_2->ReadyPorts(); |
| 1841 | EXPECT_NE(pooled_ports_1.end(), |
| 1842 | std::find(pooled_ports_1.begin(), pooled_ports_1.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1843 | ep1_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1844 | EXPECT_NE(pooled_ports_2.end(), |
| 1845 | std::find(pooled_ports_2.begin(), pooled_ports_2.end(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1846 | ep2_ch1()->selected_connection()->port())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1849 | // 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] | 1850 | // 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] | 1851 | // as soon as it's created. |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 1852 | // TODO(deadbeef): Move this and other "presumed writable" tests into a test |
| 1853 | // class that operates on a single P2PTransportChannel, once an appropriate one |
| 1854 | // (which supports TURN servers and TURN candidate gathering) is available. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1855 | TEST_F(P2PTransportChannelTest, TurnToTurnPresumedWritable) { |
| 1856 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1857 | kDefaultPortAllocatorFlags); |
| 1858 | // Only configure one channel so we can control when the remote candidate |
| 1859 | // is added. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1860 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 1861 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1862 | IceConfig config; |
| 1863 | config.presume_writable_when_fully_relayed = true; |
| 1864 | ep1_ch1()->SetIceConfig(config); |
| 1865 | ep1_ch1()->MaybeStartGathering(); |
| 1866 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 1867 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 1868 | // Add two remote candidates; a host candidate (with higher priority) |
| 1869 | // and TURN candidate. |
| 1870 | ep1_ch1()->AddRemoteCandidate( |
| 1871 | CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 1872 | ep1_ch1()->AddRemoteCandidate( |
| 1873 | CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 0)); |
| 1874 | // Expect that the TURN-TURN candidate pair will be prioritized since it's |
| 1875 | // "probably writable". |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1876 | EXPECT_TRUE(ep1_ch1()->selected_connection() != nullptr); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1877 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 1878 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 1879 | // Also expect that the channel instantly indicates that it's writable since |
| 1880 | // it has a TURN-TURN pair. |
| 1881 | EXPECT_TRUE(ep1_ch1()->writable()); |
| 1882 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 1883 | // Also make sure we can immediately send packets. |
| 1884 | const char* data = "test"; |
| 1885 | int len = static_cast<int>(strlen(data)); |
| 1886 | EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 1889 | // Test that a TURN/peer reflexive candidate pair is also presumed writable. |
| 1890 | TEST_F(P2PTransportChannelTest, TurnToPrflxPresumedWritable) { |
| 1891 | rtc::ScopedFakeClock fake_clock; |
| 1892 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1893 | // We need to add artificial network delay to verify that the connection |
| 1894 | // is presumed writable before it's actually writable. Without this delay |
| 1895 | // it would become writable instantly. |
| 1896 | virtual_socket_server()->set_delay_mean(50); |
| 1897 | virtual_socket_server()->UpdateDelayDistribution(); |
| 1898 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 1899 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 1900 | kDefaultPortAllocatorFlags); |
| 1901 | // We want the remote TURN candidate to show up as prflx. To do this we need |
| 1902 | // to configure the server to accept packets from an address we haven't |
| 1903 | // explicitly installed permission for. |
| 1904 | test_turn_server()->set_enable_permission_checks(false); |
| 1905 | IceConfig config; |
| 1906 | config.presume_writable_when_fully_relayed = true; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1907 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 1908 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 1909 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 1910 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 1911 | ep1_ch1()->SetIceConfig(config); |
| 1912 | ep2_ch1()->SetIceConfig(config); |
| 1913 | // Don't signal candidates from channel 2, so that channel 1 sees the TURN |
| 1914 | // candidate as peer reflexive. |
| 1915 | PauseCandidates(1); |
| 1916 | ep1_ch1()->MaybeStartGathering(); |
| 1917 | ep2_ch1()->MaybeStartGathering(); |
| 1918 | |
| 1919 | // Wait for the TURN<->prflx connection. |
| 1920 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable(), |
| 1921 | 1000, fake_clock); |
| 1922 | ASSERT_NE(nullptr, ep1_ch1()->selected_connection()); |
| 1923 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 1924 | EXPECT_EQ(PRFLX_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 1925 | // Make sure that at this point the connection is only presumed writable, |
| 1926 | // not fully writable. |
| 1927 | EXPECT_FALSE(ep1_ch1()->selected_connection()->writable()); |
| 1928 | |
| 1929 | // Now wait for it to actually become writable. |
| 1930 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->writable(), 1000, |
| 1931 | fake_clock); |
| 1932 | |
| 1933 | // Explitly destroy channels, before fake clock is destroyed. |
| 1934 | DestroyChannels(); |
| 1935 | } |
| 1936 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1937 | // Test that a presumed-writable TURN<->TURN connection is preferred above an |
| 1938 | // unreliable connection (one that has failed to be pinged for some time). |
| 1939 | TEST_F(P2PTransportChannelTest, PresumedWritablePreferredOverUnreliable) { |
| 1940 | rtc::ScopedFakeClock fake_clock; |
| 1941 | |
| 1942 | ConfigureEndpoints(NAT_SYMMETRIC, NAT_SYMMETRIC, kDefaultPortAllocatorFlags, |
| 1943 | kDefaultPortAllocatorFlags); |
| 1944 | IceConfig config; |
| 1945 | config.presume_writable_when_fully_relayed = true; |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1946 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 1947 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 1948 | GetEndpoint(1)->cd1_.ch_.reset(CreateChannel( |
| 1949 | 1, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[1], kIceParams[0])); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1950 | ep1_ch1()->SetIceConfig(config); |
| 1951 | ep2_ch1()->SetIceConfig(config); |
| 1952 | ep1_ch1()->MaybeStartGathering(); |
| 1953 | ep2_ch1()->MaybeStartGathering(); |
| 1954 | // Wait for initial connection as usual. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1955 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 1956 | ep1_ch1()->selected_connection()->writable() && |
| 1957 | ep2_ch1()->receiving() && |
| 1958 | ep2_ch1()->writable() && |
| 1959 | ep2_ch1()->selected_connection()->writable(), |
| 1960 | 1000, fake_clock); |
| 1961 | const Connection* old_selected_connection = ep1_ch1()->selected_connection(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1962 | // Destroy the second channel and wait for the current connection on the |
| 1963 | // first channel to become "unreliable", making it no longer writable. |
| 1964 | GetEndpoint(1)->cd1_.ch_.reset(); |
| 1965 | EXPECT_TRUE_SIMULATED_WAIT(!ep1_ch1()->writable(), 10000, fake_clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1966 | EXPECT_NE(nullptr, ep1_ch1()->selected_connection()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1967 | // Add a remote TURN candidate. The first channel should still have a TURN |
| 1968 | // port available to make a TURN<->TURN pair that's presumed writable. |
| 1969 | ep1_ch1()->AddRemoteCandidate( |
| 1970 | CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 0)); |
| 1971 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 1972 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 1973 | EXPECT_TRUE(ep1_ch1()->writable()); |
| 1974 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1975 | EXPECT_NE(old_selected_connection, ep1_ch1()->selected_connection()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1976 | // Explitly destroy channels, before fake clock is destroyed. |
| 1977 | DestroyChannels(); |
| 1978 | } |
| 1979 | |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 1980 | // Ensure that "SignalReadyToSend" is fired as expected with a "presumed |
| 1981 | // writable" connection. Previously this did not work. |
| 1982 | TEST_F(P2PTransportChannelTest, SignalReadyToSendWithPresumedWritable) { |
| 1983 | ConfigureEndpoints(OPEN, OPEN, kDefaultPortAllocatorFlags, |
| 1984 | kDefaultPortAllocatorFlags); |
| 1985 | // Only test one endpoint, so we can ensure the connection doesn't receive a |
| 1986 | // binding response and advance beyond being "presumed" writable. |
| 1987 | GetEndpoint(0)->cd1_.ch_.reset(CreateChannel( |
| 1988 | 0, ICE_CANDIDATE_COMPONENT_DEFAULT, kIceParams[0], kIceParams[1])); |
| 1989 | IceConfig config; |
| 1990 | config.presume_writable_when_fully_relayed = true; |
| 1991 | ep1_ch1()->SetIceConfig(config); |
| 1992 | ep1_ch1()->MaybeStartGathering(); |
| 1993 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, |
| 1994 | ep1_ch1()->gathering_state(), kDefaultTimeout); |
| 1995 | ep1_ch1()->AddRemoteCandidate( |
| 1996 | CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 0)); |
| 1997 | // Sanity checking the type of the connection. |
| 1998 | EXPECT_TRUE(ep1_ch1()->selected_connection() != nullptr); |
| 1999 | EXPECT_EQ(RELAY_PORT_TYPE, LocalCandidate(ep1_ch1())->type()); |
| 2000 | EXPECT_EQ(RELAY_PORT_TYPE, RemoteCandidate(ep1_ch1())->type()); |
| 2001 | |
| 2002 | // Tell the socket server to block packets (returning EWOULDBLOCK). |
| 2003 | virtual_socket_server()->SetSendingBlocked(true); |
| 2004 | const char* data = "test"; |
| 2005 | int len = static_cast<int>(strlen(data)); |
| 2006 | EXPECT_EQ(-1, SendData(ep1_ch1(), data, len)); |
| 2007 | |
| 2008 | // Reset |ready_to_send_| flag, which is set to true if the event fires as it |
| 2009 | // should. |
| 2010 | GetEndpoint(0)->ready_to_send_ = false; |
| 2011 | virtual_socket_server()->SetSendingBlocked(false); |
| 2012 | EXPECT_TRUE(GetEndpoint(0)->ready_to_send_); |
| 2013 | EXPECT_EQ(len, SendData(ep1_ch1(), data, len)); |
| 2014 | } |
| 2015 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2016 | // Test what happens when we have 2 users behind the same NAT. This can lead |
| 2017 | // to interesting behavior because the STUN server will only give out the |
| 2018 | // address of the outermost NAT. |
| 2019 | class P2PTransportChannelSameNatTest : public P2PTransportChannelTestBase { |
| 2020 | protected: |
| 2021 | void ConfigureEndpoints(Config nat_type, Config config1, Config config2) { |
| 2022 | ASSERT(nat_type >= NAT_FULL_CONE && nat_type <= NAT_SYMMETRIC); |
| 2023 | rtc::NATSocketServer::Translator* outer_nat = |
| 2024 | nat()->AddTranslator(kPublicAddrs[0], kNatAddrs[0], |
| 2025 | static_cast<rtc::NATType>(nat_type - NAT_FULL_CONE)); |
| 2026 | ConfigureEndpoint(outer_nat, 0, config1); |
| 2027 | ConfigureEndpoint(outer_nat, 1, config2); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2028 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2029 | } |
| 2030 | void ConfigureEndpoint(rtc::NATSocketServer::Translator* nat, |
| 2031 | int endpoint, Config config) { |
| 2032 | ASSERT(config <= NAT_SYMMETRIC); |
| 2033 | if (config == OPEN) { |
| 2034 | AddAddress(endpoint, kPrivateAddrs[endpoint]); |
| 2035 | nat->AddClient(kPrivateAddrs[endpoint]); |
| 2036 | } else { |
| 2037 | AddAddress(endpoint, kCascadedPrivateAddrs[endpoint]); |
| 2038 | nat->AddTranslator(kPrivateAddrs[endpoint], kCascadedNatAddrs[endpoint], |
| 2039 | static_cast<rtc::NATType>(config - NAT_FULL_CONE))->AddClient( |
| 2040 | kCascadedPrivateAddrs[endpoint]); |
| 2041 | } |
| 2042 | } |
| 2043 | }; |
| 2044 | |
| 2045 | TEST_F(P2PTransportChannelSameNatTest, TestConesBehindSameCone) { |
| 2046 | ConfigureEndpoints(NAT_FULL_CONE, NAT_FULL_CONE, NAT_FULL_CONE); |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 2047 | Test( |
| 2048 | P2PTransportChannelTestBase::Result("prflx", "udp", "stun", "udp", 1000)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | // Test what happens when we have multiple available pathways. |
| 2052 | // In the future we will try different RTTs and configs for the different |
| 2053 | // interfaces, so that we can simulate a user with Ethernet and VPN networks. |
| 2054 | class P2PTransportChannelMultihomedTest : public P2PTransportChannelTestBase { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2055 | public: |
| 2056 | const cricket::Connection* GetConnectionWithRemoteAddress( |
| 2057 | cricket::P2PTransportChannel* channel, |
| 2058 | const SocketAddress& address) { |
| 2059 | for (cricket::Connection* conn : channel->connections()) { |
| 2060 | if (conn->remote_candidate().address().EqualIPs(address)) { |
| 2061 | return conn; |
| 2062 | } |
| 2063 | } |
| 2064 | return nullptr; |
| 2065 | } |
| 2066 | |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2067 | cricket::Connection* GetConnectionWithLocalAddress( |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2068 | cricket::P2PTransportChannel* channel, |
| 2069 | const SocketAddress& address) { |
| 2070 | for (cricket::Connection* conn : channel->connections()) { |
| 2071 | if (conn->local_candidate().address().EqualIPs(address)) { |
| 2072 | return conn; |
| 2073 | } |
| 2074 | } |
| 2075 | return nullptr; |
| 2076 | } |
| 2077 | |
| 2078 | void DestroyAllButBestConnection(cricket::P2PTransportChannel* channel) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2079 | const cricket::Connection* selected_connection = |
| 2080 | channel->selected_connection(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2081 | for (cricket::Connection* conn : channel->connections()) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2082 | if (conn != selected_connection) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2083 | conn->Destroy(); |
| 2084 | } |
| 2085 | } |
| 2086 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2087 | }; |
| 2088 | |
| 2089 | // Test that we can establish connectivity when both peers are multihomed. |
| 2090 | TEST_F(P2PTransportChannelMultihomedTest, DISABLED_TestBasic) { |
| 2091 | AddAddress(0, kPublicAddrs[0]); |
| 2092 | AddAddress(0, kAlternateAddrs[0]); |
| 2093 | AddAddress(1, kPublicAddrs[1]); |
| 2094 | AddAddress(1, kAlternateAddrs[1]); |
| 2095 | Test(kLocalUdpToLocalUdp); |
| 2096 | } |
| 2097 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2098 | // Test that we can quickly switch links if an interface goes down. |
| 2099 | // The controlled side has two interfaces and one will die. |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2100 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverControlledSide) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2101 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2102 | AddAddress(0, kPublicAddrs[0]); |
| 2103 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2104 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2105 | AddAddress(1, kAlternateAddrs[1]); |
| 2106 | AddAddress(1, kPublicAddrs[1]); |
| 2107 | |
| 2108 | // Use only local ports for simplicity. |
| 2109 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2110 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2111 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2112 | // Make the receiving timeout shorter for testing. |
| 2113 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2114 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2115 | CreateChannels(config, config); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2116 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2117 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2118 | ep2_ch1()->receiving() && |
| 2119 | ep2_ch1()->writable(), |
| 2120 | 3000, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2121 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2122 | ep2_ch1()->selected_connection() && |
| 2123 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2124 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Blackhole any traffic to or from the public addrs. |
| 2127 | LOG(LS_INFO) << "Failing over..."; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2128 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2129 | // The selected connections may switch, so keep references to them. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2130 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2131 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2132 | // We should detect loss of receiving within 1 second or so. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2133 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2134 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2135 | 3000, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2136 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2137 | // We should switch over to use the alternate addr on both sides |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2138 | // when we are not receiving. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2139 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2140 | ep2_ch1()->selected_connection()->receiving(), |
| 2141 | 3000, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2142 | EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2143 | EXPECT_TRUE( |
| 2144 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1])); |
| 2145 | EXPECT_TRUE( |
| 2146 | LocalCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[1])); |
| 2147 | |
| 2148 | DestroyChannels(); |
| 2149 | } |
| 2150 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2151 | // Test that we can quickly switch links if an interface goes down. |
| 2152 | // The controlling side has two interfaces and one will die. |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2153 | TEST_F(P2PTransportChannelMultihomedTest, TestFailoverControllingSide) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2154 | rtc::ScopedFakeClock clock; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2155 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2156 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2157 | AddAddress(0, kAlternateAddrs[0]); |
| 2158 | AddAddress(0, kPublicAddrs[0]); |
| 2159 | AddAddress(1, kPublicAddrs[1]); |
| 2160 | |
| 2161 | // Use only local ports for simplicity. |
| 2162 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2163 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2164 | |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2165 | // Make the receiving timeout shorter for testing. |
| 2166 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2167 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2168 | CreateChannels(config, config); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2169 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2170 | ep2_ch1()->receiving() && |
| 2171 | ep2_ch1()->writable(), |
| 2172 | 3000, clock); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2173 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2174 | ep2_ch1()->selected_connection() && |
| 2175 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2176 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2177 | |
| 2178 | // Blackhole any traffic to or from the public addrs. |
| 2179 | LOG(LS_INFO) << "Failing over..."; |
| 2180 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2181 | // The selected connections will switch, so keep references to them. |
| 2182 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2183 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2184 | // We should detect loss of receiving within 1 second or so. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2185 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2186 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2187 | 3000, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2188 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2189 | // We should switch over to use the alternate addr on both sides |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2190 | // when we are not receiving. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2191 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2192 | ep2_ch1()->selected_connection()->receiving(), |
| 2193 | 3000, clock); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 2194 | EXPECT_TRUE( |
| 2195 | LocalCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0])); |
| 2196 | EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2197 | EXPECT_TRUE( |
| 2198 | RemoteCandidate(ep2_ch1())->address().EqualIPs(kAlternateAddrs[0])); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2199 | |
| 2200 | DestroyChannels(); |
| 2201 | } |
| 2202 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2203 | // Test that when the controlling side switches the selected connection, |
| 2204 | // the nomination of the selected connection on the controlled side will |
| 2205 | // increase. |
| 2206 | TEST_F(P2PTransportChannelMultihomedTest, TestIceRenomination) { |
| 2207 | rtc::ScopedFakeClock clock; |
| 2208 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2209 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2210 | AddAddress(0, kAlternateAddrs[0]); |
| 2211 | AddAddress(0, kPublicAddrs[0]); |
| 2212 | AddAddress(1, kPublicAddrs[1]); |
| 2213 | |
| 2214 | // Use only local ports for simplicity. |
| 2215 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2216 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2217 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2218 | // We want it to set the remote ICE parameters when creating channels. |
| 2219 | set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2220 | // Make the receiving timeout shorter for testing. |
| 2221 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2222 | // Create channels with ICE renomination and let them go writable as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2223 | CreateChannels(config, config, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2224 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2225 | ep2_ch1()->receiving() && |
| 2226 | ep2_ch1()->writable(), |
| 2227 | 3000, clock); |
| 2228 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2229 | ep2_ch1()->selected_connection()->remote_nomination() > 0 && |
| 2230 | ep1_ch1()->selected_connection()->acked_nomination() > 0, |
| 2231 | kDefaultTimeout, clock); |
| 2232 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2233 | Connection* selected_connection2 = |
| 2234 | const_cast<Connection*>(ep2_ch1()->selected_connection()); |
| 2235 | uint32_t remote_nomination2 = selected_connection2->remote_nomination(); |
| 2236 | // |selected_connection2| should not be nominated any more since the previous |
| 2237 | // nomination has been acknowledged. |
| 2238 | ConnectSignalNominated(selected_connection2); |
| 2239 | SIMULATED_WAIT(nominated(), 3000, clock); |
| 2240 | EXPECT_FALSE(nominated()); |
| 2241 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2242 | // Blackhole any traffic to or from the public addrs. |
| 2243 | LOG(LS_INFO) << "Failing over..."; |
| 2244 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 2245 | |
| 2246 | // The selected connection on the controlling side should switch. |
| 2247 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2248 | ep1_ch1()->selected_connection() != selected_connection1, 3000, clock); |
| 2249 | // The connection on the controlled side should be nominated again |
| 2250 | // and have an increased nomination. |
| 2251 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2252 | ep2_ch1()->selected_connection()->remote_nomination() > |
| 2253 | remote_nomination2, |
| 2254 | kDefaultTimeout, clock); |
| 2255 | |
| 2256 | DestroyChannels(); |
| 2257 | } |
| 2258 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2259 | // Test that if an interface fails temporarily and then recovers quickly, |
| 2260 | // the selected connection will not switch. |
| 2261 | // The case that it will switch over to the backup connection if the selected |
| 2262 | // connection does not recover after enough time is covered in |
| 2263 | // TestFailoverControlledSide and TestFailoverControllingSide. |
| 2264 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2265 | TestConnectionSwitchDampeningControlledSide) { |
| 2266 | rtc::ScopedFakeClock clock; |
| 2267 | AddAddress(0, kPublicAddrs[0]); |
| 2268 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2269 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2270 | AddAddress(1, kAlternateAddrs[1]); |
| 2271 | AddAddress(1, kPublicAddrs[1]); |
| 2272 | |
| 2273 | // Use only local ports for simplicity. |
| 2274 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2275 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2276 | |
| 2277 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2278 | CreateChannels(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2279 | |
| 2280 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2281 | ep2_ch1()->receiving() && |
| 2282 | ep2_ch1()->writable(), |
| 2283 | 3000, clock); |
| 2284 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2285 | ep2_ch1()->selected_connection() && |
| 2286 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2287 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2288 | |
| 2289 | // Make the receiving timeout shorter for testing. |
| 2290 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 2291 | ep1_ch1()->SetIceConfig(config); |
| 2292 | ep2_ch1()->SetIceConfig(config); |
| 2293 | reset_selected_candidate_pair_switches(); |
| 2294 | |
| 2295 | // Blackhole any traffic to or from the public addrs. |
| 2296 | LOG(LS_INFO) << "Failing over..."; |
| 2297 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[1]); |
| 2298 | |
| 2299 | // The selected connections may switch, so keep references to them. |
| 2300 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2301 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2302 | // We should detect loss of receiving within 1 second or so. |
| 2303 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2304 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
| 2305 | 3000, clock); |
| 2306 | // After a short while, the link recovers itself. |
| 2307 | SIMULATED_WAIT(false, 10, clock); |
| 2308 | fw()->ClearRules(); |
| 2309 | |
| 2310 | // We should remain on the public address on both sides and no connection |
| 2311 | // switches should have happened. |
| 2312 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2313 | ep2_ch1()->selected_connection()->receiving(), |
| 2314 | 3000, clock); |
| 2315 | EXPECT_TRUE(RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2316 | EXPECT_TRUE(LocalCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2317 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 2318 | |
| 2319 | DestroyChannels(); |
| 2320 | } |
| 2321 | |
| 2322 | // Test that if an interface fails temporarily and then recovers quickly, |
| 2323 | // the selected connection will not switch. |
| 2324 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2325 | TestConnectionSwitchDampeningControllingSide) { |
| 2326 | rtc::ScopedFakeClock clock; |
| 2327 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2328 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2329 | AddAddress(0, kAlternateAddrs[0]); |
| 2330 | AddAddress(0, kPublicAddrs[0]); |
| 2331 | AddAddress(1, kPublicAddrs[1]); |
| 2332 | |
| 2333 | // Use only local ports for simplicity. |
| 2334 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2335 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2336 | |
| 2337 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2338 | CreateChannels(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2339 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2340 | ep2_ch1()->receiving() && |
| 2341 | ep2_ch1()->writable(), |
| 2342 | 3000, clock); |
| 2343 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2344 | ep2_ch1()->selected_connection() && |
| 2345 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2346 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2347 | |
| 2348 | // Make the receiving timeout shorter for testing. |
| 2349 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 2350 | ep1_ch1()->SetIceConfig(config); |
| 2351 | ep2_ch1()->SetIceConfig(config); |
| 2352 | reset_selected_candidate_pair_switches(); |
| 2353 | |
| 2354 | // Blackhole any traffic to or from the public addrs. |
| 2355 | LOG(LS_INFO) << "Failing over..."; |
| 2356 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, kPublicAddrs[0]); |
| 2357 | // The selected connections may switch, so keep references to them. |
| 2358 | const Connection* selected_connection1 = ep1_ch1()->selected_connection(); |
| 2359 | const Connection* selected_connection2 = ep2_ch1()->selected_connection(); |
| 2360 | // We should detect loss of receiving within 1 second or so. |
| 2361 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2362 | !selected_connection1->receiving() && !selected_connection2->receiving(), |
| 2363 | 3000, clock); |
| 2364 | // The link recovers after a short while. |
| 2365 | SIMULATED_WAIT(false, 10, clock); |
| 2366 | fw()->ClearRules(); |
| 2367 | |
| 2368 | // We should not switch to the alternate addr on both sides because of the |
| 2369 | // dampening. |
| 2370 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->selected_connection()->receiving() && |
| 2371 | ep2_ch1()->selected_connection()->receiving(), |
| 2372 | 3000, clock); |
| 2373 | EXPECT_TRUE(LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2374 | EXPECT_TRUE(RemoteCandidate(ep2_ch1())->address().EqualIPs(kPublicAddrs[0])); |
| 2375 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 2376 | DestroyChannels(); |
| 2377 | } |
| 2378 | |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2379 | // Tests that if the remote side's network failed, it won't cause the local |
| 2380 | // side to switch connections and networks. |
| 2381 | TEST_F(P2PTransportChannelMultihomedTest, TestRemoteFailover) { |
| 2382 | rtc::ScopedFakeClock clock; |
| 2383 | // The interface names are chosen so that |cellular| would have higher |
| 2384 | // candidate priority and higher cost. |
| 2385 | auto& wifi = kPublicAddrs; |
| 2386 | auto& cellular = kAlternateAddrs; |
| 2387 | AddAddress(0, wifi[0], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2388 | AddAddress(0, cellular[0], "cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2389 | AddAddress(1, wifi[1], "wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2390 | |
| 2391 | // Use only local ports for simplicity. |
| 2392 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2393 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2394 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2395 | CreateChannels(); |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 2396 | // Make the receiving timeout shorter for testing. |
| 2397 | // Set the backup connection ping interval to 25s. |
| 2398 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE, 25000); |
| 2399 | // Ping the best connection more frequently since we don't have traffic. |
| 2400 | config.stable_writable_connection_ping_interval = 900; |
| 2401 | ep1_ch1()->SetIceConfig(config); |
| 2402 | ep2_ch1()->SetIceConfig(config); |
| 2403 | // Need to wait to make sure the connections on both networks are writable. |
| 2404 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2405 | ep2_ch1()->receiving() && |
| 2406 | ep2_ch1()->writable(), |
| 2407 | 3000, clock); |
| 2408 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2409 | ep1_ch1()->selected_connection() && |
| 2410 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2411 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2412 | kDefaultTimeout, clock); |
| 2413 | Connection* backup_conn = |
| 2414 | GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]); |
| 2415 | ASSERT_NE(nullptr, backup_conn); |
| 2416 | // After a short while, the backup connection will be writable but not |
| 2417 | // receiving because backup connection is pinged at a slower rate. |
| 2418 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2419 | backup_conn->writable() && !backup_conn->receiving(), 5000, clock); |
| 2420 | reset_selected_candidate_pair_switches(); |
| 2421 | // Blackhole any traffic to or from the remote WiFi networks. |
| 2422 | LOG(LS_INFO) << "Failing over..."; |
| 2423 | fw()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, wifi[1]); |
| 2424 | |
| 2425 | int num_switches = 0; |
| 2426 | SIMULATED_WAIT((num_switches = reset_selected_candidate_pair_switches()) > 0, |
| 2427 | 20000, clock); |
| 2428 | EXPECT_EQ(0, num_switches); |
| 2429 | DestroyChannels(); |
| 2430 | } |
| 2431 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2432 | // Tests that a Wifi-Wifi connection has the highest precedence. |
| 2433 | TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiToWifiConnection) { |
| 2434 | // The interface names are chosen so that |cellular| would have higher |
| 2435 | // candidate priority if it is not for the network type. |
| 2436 | auto& wifi = kAlternateAddrs; |
| 2437 | auto& cellular = kPublicAddrs; |
| 2438 | AddAddress(0, wifi[0], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2439 | AddAddress(0, cellular[0], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2440 | AddAddress(1, wifi[1], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2441 | AddAddress(1, cellular[1], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2442 | |
| 2443 | // Use only local ports for simplicity. |
| 2444 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2445 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2446 | |
| 2447 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2448 | CreateChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2449 | |
| 2450 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2451 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2452 | 1000, 1000); |
| 2453 | // 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] | 2454 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2455 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2456 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2457 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2458 | EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2459 | LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]) && |
| 2460 | RemoteCandidate(ep2_ch1())->address().EqualIPs(wifi[0]), |
| 2461 | 1000); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2462 | DestroyChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | // Tests that a Wifi-Cellular connection has higher precedence than |
| 2466 | // a Cellular-Cellular connection. |
| 2467 | TEST_F(P2PTransportChannelMultihomedTest, TestPreferWifiOverCellularNetwork) { |
| 2468 | // The interface names are chosen so that |cellular| would have higher |
| 2469 | // candidate priority if it is not for the network type. |
| 2470 | auto& wifi = kAlternateAddrs; |
| 2471 | auto& cellular = kPublicAddrs; |
| 2472 | AddAddress(0, cellular[0], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2473 | AddAddress(1, wifi[1], "test0", rtc::ADAPTER_TYPE_WIFI); |
| 2474 | AddAddress(1, cellular[1], "test1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2475 | |
| 2476 | // Use only local ports for simplicity. |
| 2477 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2478 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2479 | |
| 2480 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2481 | CreateChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2482 | |
| 2483 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2484 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2485 | 1000, 1000); |
| 2486 | // 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] | 2487 | EXPECT_TRUE_WAIT(ep1_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2488 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2489 | 1000); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2490 | EXPECT_TRUE_WAIT(ep2_ch1()->selected_connection() && |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2491 | LocalCandidate(ep2_ch1())->address().EqualIPs(wifi[1]), |
| 2492 | 1000); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2493 | DestroyChannels(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 2494 | } |
| 2495 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2496 | // Test that the backup connection is pinged at a rate no faster than |
| 2497 | // what was configured. |
| 2498 | TEST_F(P2PTransportChannelMultihomedTest, TestPingBackupConnectionRate) { |
| 2499 | AddAddress(0, kPublicAddrs[0]); |
| 2500 | // Adding alternate address will make sure |kPublicAddrs| has the higher |
| 2501 | // priority than others. This is due to FakeNetwork::AddInterface method. |
| 2502 | AddAddress(1, kAlternateAddrs[1]); |
| 2503 | AddAddress(1, kPublicAddrs[1]); |
| 2504 | |
| 2505 | // Use only local ports for simplicity. |
| 2506 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2507 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2508 | |
| 2509 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2510 | CreateChannels(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2511 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2512 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2513 | 1000, 1000); |
| 2514 | int backup_ping_interval = 2000; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2515 | ep2_ch1()->SetIceConfig( |
| 2516 | CreateIceConfig(2000, GATHER_ONCE, backup_ping_interval)); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2517 | // After the state becomes COMPLETED, the backup connection will be pinged |
| 2518 | // once every |backup_ping_interval| milliseconds. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2519 | ASSERT_TRUE_WAIT(ep2_ch1()->GetState() == STATE_COMPLETED, 1000); |
| 2520 | const std::vector<Connection*>& connections = ep2_ch1()->connections(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2521 | ASSERT_EQ(2U, connections.size()); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2522 | Connection* backup_conn = connections[1]; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2523 | EXPECT_TRUE_WAIT(backup_conn->writable(), 3000); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2524 | int64_t last_ping_response_ms = backup_conn->last_ping_response_received(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2525 | EXPECT_TRUE_WAIT( |
| 2526 | last_ping_response_ms < backup_conn->last_ping_response_received(), 5000); |
| 2527 | int time_elapsed = |
| 2528 | backup_conn->last_ping_response_received() - last_ping_response_ms; |
| 2529 | LOG(LS_INFO) << "Time elapsed: " << time_elapsed; |
| 2530 | EXPECT_GE(time_elapsed, backup_ping_interval); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2531 | |
| 2532 | DestroyChannels(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 2533 | } |
| 2534 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2535 | TEST_F(P2PTransportChannelMultihomedTest, TestGetState) { |
| 2536 | AddAddress(0, kAlternateAddrs[0]); |
| 2537 | AddAddress(0, kPublicAddrs[0]); |
| 2538 | AddAddress(1, kPublicAddrs[1]); |
| 2539 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2540 | CreateChannels(); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2541 | |
| 2542 | // Both transport channels will reach STATE_COMPLETED quickly. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2543 | EXPECT_EQ_WAIT(TransportChannelState::STATE_COMPLETED, ep1_ch1()->GetState(), |
| 2544 | 1000); |
| 2545 | EXPECT_EQ_WAIT(TransportChannelState::STATE_COMPLETED, ep2_ch1()->GetState(), |
| 2546 | 1000); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2549 | // Tests that when a network interface becomes inactive, if Continual Gathering |
| 2550 | // policy is GATHER_CONTINUALLY, the ports associated with that network |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2551 | // will be removed from the port list of the channel, and the respective |
| 2552 | // remote candidates on the other participant will be removed eventually. |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2553 | TEST_F(P2PTransportChannelMultihomedTest, TestNetworkBecomesInactive) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2554 | rtc::ScopedFakeClock clock; |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2555 | AddAddress(0, kPublicAddrs[0]); |
| 2556 | AddAddress(1, kPublicAddrs[1]); |
| 2557 | // Create channels and let them go writable, as usual. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2558 | IceConfig ep1_config = CreateIceConfig(2000, GATHER_CONTINUALLY); |
| 2559 | IceConfig ep2_config = CreateIceConfig(2000, GATHER_ONCE); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2560 | CreateChannels(ep1_config, ep2_config); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2561 | |
| 2562 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2563 | SetAllocatorFlags(1, kOnlyLocalPorts); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2564 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2565 | ep2_ch1()->receiving() && |
| 2566 | ep2_ch1()->writable(), |
| 2567 | kDefaultTimeout, clock); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2568 | // More than one port has been created. |
| 2569 | EXPECT_LE(1U, ep1_ch1()->ports().size()); |
| 2570 | // Endpoint 1 enabled continual gathering; the port will be removed |
| 2571 | // when the interface is removed. |
| 2572 | RemoveAddress(0, kPublicAddrs[0]); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2573 | EXPECT_TRUE(ep1_ch1()->ports().empty()); |
| 2574 | // The remote candidates will be removed eventually. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2575 | EXPECT_TRUE_SIMULATED_WAIT(ep2_ch1()->remote_candidates().empty(), 1000, |
| 2576 | clock); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2577 | |
| 2578 | size_t num_ports = ep2_ch1()->ports().size(); |
| 2579 | EXPECT_LE(1U, num_ports); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2580 | size_t num_remote_candidates = ep1_ch1()->remote_candidates().size(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2581 | // Endpoint 2 did not enable continual gathering; the local port will still be |
| 2582 | // removed when the interface is removed but the remote candidates on the |
| 2583 | // other participant will not be removed. |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2584 | RemoveAddress(1, kPublicAddrs[1]); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2585 | |
| 2586 | EXPECT_EQ_SIMULATED_WAIT(0U, ep2_ch1()->ports().size(), kDefaultTimeout, |
| 2587 | clock); |
| 2588 | SIMULATED_WAIT(0U == ep1_ch1()->remote_candidates().size(), 500, clock); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2589 | EXPECT_EQ(num_remote_candidates, ep1_ch1()->remote_candidates().size()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2590 | |
| 2591 | DestroyChannels(); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 2592 | } |
| 2593 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2594 | // Tests that continual gathering will create new connections when a new |
| 2595 | // interface is added. |
| 2596 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2597 | TestContinualGatheringOnNewInterface) { |
| 2598 | auto& wifi = kAlternateAddrs; |
| 2599 | auto& cellular = kPublicAddrs; |
| 2600 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2601 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2602 | // Set continual gathering policy. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2603 | IceConfig continual_gathering_config = |
| 2604 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2605 | CreateChannels(continual_gathering_config, continual_gathering_config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2606 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2607 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2608 | EXPECT_TRUE_WAIT_MARGIN(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2609 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2610 | kDefaultTimeout, kDefaultTimeout); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2611 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2612 | // Add a new wifi interface on end point 2. We should expect a new connection |
| 2613 | // to be created and the new one will be the best connection. |
| 2614 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2615 | const cricket::Connection* conn; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2616 | EXPECT_TRUE_WAIT((conn = ep1_ch1()->selected_connection()) != nullptr && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2617 | conn->remote_candidate().address().EqualIPs(wifi[1]), |
| 2618 | kDefaultTimeout); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2619 | EXPECT_TRUE_WAIT((conn = ep2_ch1()->selected_connection()) != nullptr && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2620 | conn->local_candidate().address().EqualIPs(wifi[1]), |
| 2621 | kDefaultTimeout); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2622 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2623 | // Add a new cellular interface on end point 1, we should expect a new |
| 2624 | // backup connection created using this new interface. |
| 2625 | AddAddress(0, cellular[0], "test_cellular0", rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2626 | EXPECT_TRUE_WAIT( |
| 2627 | ep1_ch1()->GetState() == cricket::STATE_COMPLETED && |
| 2628 | (conn = GetConnectionWithLocalAddress(ep1_ch1(), cellular[0])) != |
| 2629 | nullptr && |
| 2630 | conn != ep1_ch1()->selected_connection() && conn->writable(), |
| 2631 | kDefaultTimeout); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2632 | EXPECT_TRUE_WAIT( |
| 2633 | ep2_ch1()->GetState() == cricket::STATE_COMPLETED && |
| 2634 | (conn = GetConnectionWithRemoteAddress(ep2_ch1(), cellular[0])) != |
| 2635 | nullptr && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2636 | conn != ep2_ch1()->selected_connection() && conn->receiving(), |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2637 | kDefaultTimeout); |
| 2638 | |
| 2639 | DestroyChannels(); |
| 2640 | } |
| 2641 | |
| 2642 | // Tests that we can switch links via continual gathering. |
| 2643 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2644 | TestSwitchLinksViaContinualGathering) { |
| 2645 | rtc::ScopedFakeClock clock; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2646 | AddAddress(0, kPublicAddrs[0]); |
| 2647 | AddAddress(1, kPublicAddrs[1]); |
| 2648 | // Use only local ports for simplicity. |
| 2649 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2650 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2651 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2652 | // Set continual gathering policy. |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2653 | IceConfig continual_gathering_config = |
| 2654 | CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 2655 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2656 | CreateChannels(continual_gathering_config, continual_gathering_config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2657 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2658 | ep2_ch1()->receiving() && |
| 2659 | ep2_ch1()->writable(), |
| 2660 | 3000, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2661 | EXPECT_TRUE( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2662 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2663 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2664 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[1])); |
| 2665 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2666 | // Add the new address first and then remove the other one. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2667 | LOG(LS_INFO) << "Draining..."; |
| 2668 | AddAddress(1, kAlternateAddrs[1]); |
| 2669 | RemoveAddress(1, kPublicAddrs[1]); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2670 | // We should switch to use the alternate address after an exchange of pings. |
| 2671 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2672 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2673 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2674 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[1]), |
| 2675 | 3000, clock); |
| 2676 | |
| 2677 | // Remove one address first and then add another address. |
| 2678 | LOG(LS_INFO) << "Draining again..."; |
| 2679 | RemoveAddress(1, kAlternateAddrs[1]); |
| 2680 | AddAddress(1, kAlternateAddrs[0]); |
| 2681 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2682 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2683 | LocalCandidate(ep1_ch1())->address().EqualIPs(kPublicAddrs[0]) && |
| 2684 | RemoteCandidate(ep1_ch1())->address().EqualIPs(kAlternateAddrs[0]), |
| 2685 | 3000, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2686 | |
| 2687 | DestroyChannels(); |
| 2688 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2689 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2690 | /* |
| 2691 | TODO(honghaiz) Once continual gathering fully supports |
| 2692 | GATHER_CONTINUALLY_AND_RECOVER, put this test back. |
| 2693 | |
| 2694 | // Tests that if the backup connections are lost and then the interface with the |
| 2695 | // selected connection is gone, continual gathering will restore the |
| 2696 | // connectivity. |
| 2697 | TEST_F(P2PTransportChannelMultihomedTest, |
| 2698 | TestBackupConnectionLostThenInterfaceGone) { |
| 2699 | rtc::ScopedFakeClock clock; |
| 2700 | auto& wifi = kAlternateAddrs; |
| 2701 | auto& cellular = kPublicAddrs; |
| 2702 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2703 | AddAddress(0, cellular[0], "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2704 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2705 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2706 | // Use only local ports for simplicity. |
| 2707 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2708 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2709 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2710 | // Set continual gathering policy. |
| 2711 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY_AND_RECOVER); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 2712 | // Create channels and let them go writable, as usual. |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2713 | CreateChannels(config, config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2714 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2715 | ep2_ch1()->receiving() && ep2_ch1()->writable(), |
| 2716 | 3000, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2717 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2718 | ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2719 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2720 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1])); |
| 2721 | |
| 2722 | // First destroy all backup connection. |
| 2723 | DestroyAllButBestConnection(ep1_ch1()); |
| 2724 | |
| 2725 | SIMULATED_WAIT(false, 10, clock); |
| 2726 | // Then the interface of the best connection goes away. |
| 2727 | RemoveAddress(0, wifi[0]); |
| 2728 | EXPECT_TRUE_SIMULATED_WAIT( |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2729 | ep1_ch1()->selected_connection() && ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2730 | LocalCandidate(ep1_ch1())->address().EqualIPs(cellular[0]) && |
| 2731 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1]), |
| 2732 | 3000, clock); |
| 2733 | |
| 2734 | DestroyChannels(); |
| 2735 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2736 | */ |
| 2737 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2738 | // Tests that the backup connection will be restored after it is destroyed. |
| 2739 | TEST_F(P2PTransportChannelMultihomedTest, TestRestoreBackupConnection) { |
| 2740 | rtc::ScopedFakeClock clock; |
| 2741 | auto& wifi = kAlternateAddrs; |
| 2742 | auto& cellular = kPublicAddrs; |
| 2743 | AddAddress(0, wifi[0], "test_wifi0", rtc::ADAPTER_TYPE_WIFI); |
| 2744 | AddAddress(0, cellular[0], "test_cell0", rtc::ADAPTER_TYPE_CELLULAR); |
| 2745 | AddAddress(1, wifi[1], "test_wifi1", rtc::ADAPTER_TYPE_WIFI); |
| 2746 | AddAddress(1, cellular[1], "test_cell1", rtc::ADAPTER_TYPE_CELLULAR); |
| 2747 | // Use only local ports for simplicity. |
| 2748 | SetAllocatorFlags(0, kOnlyLocalPorts); |
| 2749 | SetAllocatorFlags(1, kOnlyLocalPorts); |
| 2750 | |
| 2751 | // Create channels and let them go writable, as usual. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2752 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
| 2753 | config.regather_on_failed_networks_interval = rtc::Optional<int>(2000); |
johan | 02bd512 | 2016-09-20 00:23:27 -0700 | [diff] [blame] | 2754 | CreateChannels(config, config); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2755 | EXPECT_TRUE_SIMULATED_WAIT(ep1_ch1()->receiving() && ep1_ch1()->writable() && |
| 2756 | ep2_ch1()->receiving() && |
| 2757 | ep2_ch1()->writable(), |
| 2758 | 3000, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2759 | EXPECT_TRUE(ep1_ch1()->selected_connection() && |
| 2760 | ep2_ch1()->selected_connection() && |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2761 | LocalCandidate(ep1_ch1())->address().EqualIPs(wifi[0]) && |
| 2762 | RemoteCandidate(ep1_ch1())->address().EqualIPs(wifi[1])); |
| 2763 | |
| 2764 | // Destroy all backup connections. |
| 2765 | DestroyAllButBestConnection(ep1_ch1()); |
| 2766 | // Ensure the backup connection is removed first. |
| 2767 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2768 | GetConnectionWithLocalAddress(ep1_ch1(), cellular[0]) == nullptr, |
| 2769 | kDefaultTimeout, clock); |
| 2770 | const cricket::Connection* conn; |
| 2771 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2772 | (conn = GetConnectionWithLocalAddress(ep1_ch1(), cellular[0])) != |
| 2773 | nullptr && |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2774 | conn != ep1_ch1()->selected_connection() && conn->writable(), |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 2775 | 5000, clock); |
| 2776 | |
| 2777 | DestroyChannels(); |
| 2778 | } |
| 2779 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 2780 | // A collection of tests which tests a single P2PTransportChannel by sending |
| 2781 | // pings. |
| 2782 | class P2PTransportChannelPingTest : public testing::Test, |
| 2783 | public sigslot::has_slots<> { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2784 | public: |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 2785 | P2PTransportChannelPingTest() |
| 2786 | : pss_(new rtc::PhysicalSocketServer), |
| 2787 | vss_(new rtc::VirtualSocketServer(pss_.get())), |
| 2788 | ss_scope_(vss_.get()) {} |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2789 | |
| 2790 | protected: |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2791 | void PrepareChannel(P2PTransportChannel* ch) { |
| 2792 | ch->SetIceRole(ICEROLE_CONTROLLING); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2793 | ch->SetIceParameters(kIceParams[0]); |
| 2794 | ch->SetRemoteIceParameters(kIceParams[1]); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2795 | ch->SignalSelectedCandidatePairChanged.connect( |
| 2796 | this, &P2PTransportChannelPingTest::OnSelectedCandidatePairChanged); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 2797 | ch->SignalReadyToSend.connect(this, |
| 2798 | &P2PTransportChannelPingTest::OnReadyToSend); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 2799 | ch->SignalStateChanged.connect( |
| 2800 | this, &P2PTransportChannelPingTest::OnChannelStateChanged); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2803 | Connection* WaitForConnectionTo(P2PTransportChannel* ch, |
| 2804 | const std::string& ip, |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 2805 | int port_num, |
| 2806 | rtc::FakeClock* clock = nullptr) { |
| 2807 | if (clock == nullptr) { |
| 2808 | EXPECT_TRUE_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, 3000); |
| 2809 | } else { |
| 2810 | EXPECT_TRUE_SIMULATED_WAIT(GetConnectionTo(ch, ip, port_num) != nullptr, |
| 2811 | 3000, *clock); |
| 2812 | } |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2813 | return GetConnectionTo(ch, ip, port_num); |
| 2814 | } |
| 2815 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2816 | Port* GetPort(P2PTransportChannel* ch) { |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2817 | if (ch->ports().empty()) { |
| 2818 | return nullptr; |
| 2819 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2820 | return static_cast<Port*>(ch->ports()[0]); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2823 | Port* GetPrunedPort(P2PTransportChannel* ch) { |
| 2824 | if (ch->pruned_ports().empty()) { |
| 2825 | return nullptr; |
| 2826 | } |
| 2827 | return static_cast<Port*>(ch->pruned_ports()[0]); |
| 2828 | } |
| 2829 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2830 | Connection* GetConnectionTo(P2PTransportChannel* ch, |
| 2831 | const std::string& ip, |
| 2832 | int port_num) { |
| 2833 | Port* port = GetPort(ch); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2834 | if (!port) { |
| 2835 | return nullptr; |
| 2836 | } |
| 2837 | return port->GetConnection(rtc::SocketAddress(ip, port_num)); |
| 2838 | } |
| 2839 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2840 | Connection* FindNextPingableConnectionAndPingIt(P2PTransportChannel* ch) { |
| 2841 | Connection* conn = ch->FindNextPingableConnection(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2842 | if (conn) { |
| 2843 | ch->MarkConnectionPinged(conn); |
| 2844 | } |
| 2845 | return conn; |
| 2846 | } |
| 2847 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2848 | int SendData(TransportChannel& channel, |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2849 | const char* data, |
| 2850 | size_t len, |
| 2851 | int packet_id) { |
| 2852 | rtc::PacketOptions options; |
| 2853 | options.packet_id = packet_id; |
| 2854 | return channel.SendPacket(data, len, options, 0); |
| 2855 | } |
| 2856 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2857 | Connection* CreateConnectionWithCandidate(P2PTransportChannel& channel, |
| 2858 | rtc::ScopedFakeClock& clock, |
| 2859 | const std::string& ip_addr, |
| 2860 | int port, |
| 2861 | int priority, |
| 2862 | bool writable) { |
| 2863 | channel.AddRemoteCandidate( |
| 2864 | CreateUdpCandidate(LOCAL_PORT_TYPE, ip_addr, port, priority)); |
| 2865 | EXPECT_TRUE_SIMULATED_WAIT( |
| 2866 | GetConnectionTo(&channel, ip_addr, port) != nullptr, 3000, clock); |
| 2867 | Connection* conn = GetConnectionTo(&channel, ip_addr, port); |
| 2868 | |
| 2869 | if (conn && writable) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2870 | conn->ReceivedPingResponse(LOW_RTT, "id"); // make it writable |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2871 | } |
| 2872 | return conn; |
| 2873 | } |
| 2874 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2875 | void NominateConnection(Connection* conn, uint32_t remote_nomination = 1U) { |
| 2876 | conn->set_remote_nomination(remote_nomination); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2877 | conn->SignalNominated(conn); |
| 2878 | } |
| 2879 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2880 | void OnSelectedCandidatePairChanged( |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2881 | TransportChannel* transport_channel, |
| 2882 | CandidatePairInterface* selected_candidate_pair, |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2883 | int last_sent_packet_id, |
| 2884 | bool ready_to_send) { |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2885 | last_selected_candidate_pair_ = selected_candidate_pair; |
| 2886 | last_sent_packet_id_ = last_sent_packet_id; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2887 | ++selected_candidate_pair_switches_; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2890 | void ReceivePingOnConnection(Connection* conn, |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 2891 | const std::string& remote_ufrag, |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2892 | int priority, |
| 2893 | uint32_t nomination = 0) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2894 | IceMessage msg; |
| 2895 | msg.SetType(STUN_BINDING_REQUEST); |
| 2896 | msg.AddAttribute(new StunByteStringAttribute( |
| 2897 | STUN_ATTR_USERNAME, |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 2898 | conn->local_candidate().username() + ":" + remote_ufrag)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2899 | msg.AddAttribute(new StunUInt32Attribute(STUN_ATTR_PRIORITY, priority)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2900 | if (nomination != 0) { |
| 2901 | msg.AddAttribute( |
| 2902 | new StunUInt32Attribute(STUN_ATTR_NOMINATION, nomination)); |
| 2903 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2904 | msg.SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 2905 | msg.AddMessageIntegrity(conn->local_candidate().password()); |
| 2906 | msg.AddFingerprint(); |
| 2907 | rtc::ByteBufferWriter buf; |
| 2908 | msg.Write(&buf); |
| 2909 | conn->OnReadPacket(buf.Data(), buf.Length(), rtc::CreatePacketTime(0)); |
| 2910 | } |
| 2911 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2912 | void OnReadyToSend(TransportChannel* channel) { |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 2913 | channel_ready_to_send_ = true; |
| 2914 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2915 | void OnChannelStateChanged(TransportChannelImpl* channel) { |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 2916 | channel_state_ = channel->GetState(); |
| 2917 | } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 2918 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2919 | CandidatePairInterface* last_selected_candidate_pair() { |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2920 | return last_selected_candidate_pair_; |
| 2921 | } |
| 2922 | int last_sent_packet_id() { return last_sent_packet_id_; } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 2923 | bool channel_ready_to_send() { return channel_ready_to_send_; } |
| 2924 | void reset_channel_ready_to_send() { channel_ready_to_send_ = false; } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2925 | TransportChannelState channel_state() { return channel_state_; } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2926 | int reset_selected_candidate_pair_switches() { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2927 | int switches = selected_candidate_pair_switches_; |
| 2928 | selected_candidate_pair_switches_ = 0; |
| 2929 | return switches; |
| 2930 | } |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 2931 | |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2932 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2933 | std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 2934 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2935 | rtc::SocketServerScope ss_scope_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2936 | CandidatePairInterface* last_selected_candidate_pair_ = nullptr; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2937 | int selected_candidate_pair_switches_ = 0; |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 2938 | int last_sent_packet_id_ = -1; |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 2939 | bool channel_ready_to_send_ = false; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2940 | TransportChannelState channel_state_ = STATE_INIT; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2941 | }; |
| 2942 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 2943 | TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2944 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 2945 | P2PTransportChannel ch("trigger checks", 1, &pa); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2946 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2947 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2948 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 2949 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2950 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2951 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 2952 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2953 | ASSERT_TRUE(conn1 != nullptr); |
| 2954 | ASSERT_TRUE(conn2 != nullptr); |
| 2955 | |
| 2956 | // Before a triggered check, the first connection to ping is the |
| 2957 | // highest priority one. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2958 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2959 | |
| 2960 | // Receiving a ping causes a triggered check which should make conn1 |
| 2961 | // be pinged first instead of conn2, even though conn2 has a higher |
| 2962 | // priority. |
| 2963 | conn1->ReceivedPing(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2964 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 2967 | TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2968 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 2969 | P2PTransportChannel ch("ping sufficiently", 1, &pa); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 2970 | PrepareChannel(&ch); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 2971 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2972 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 2973 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 2974 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2975 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 2976 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 2977 | ASSERT_TRUE(conn1 != nullptr); |
| 2978 | ASSERT_TRUE(conn2 != nullptr); |
| 2979 | |
| 2980 | // Low-priority connection becomes writable so that the other connection |
| 2981 | // is not pruned. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 2982 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 2983 | EXPECT_TRUE_WAIT( |
| 2984 | conn1->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL && |
| 2985 | conn2->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, |
| 2986 | kDefaultTimeout); |
| 2987 | } |
| 2988 | |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 2989 | // Verify that the connections are pinged at the right time. |
| 2990 | TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) { |
| 2991 | rtc::ScopedFakeClock clock; |
| 2992 | int RTT_RATIO = 4; |
| 2993 | int SCHEDULING_RANGE = 200; |
| 2994 | int RTT_RANGE = 10; |
| 2995 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 2996 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 2997 | P2PTransportChannel ch("TestChannel", 1, &pa); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 2998 | PrepareChannel(&ch); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 2999 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3000 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3001 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3002 | |
| 3003 | ASSERT_TRUE(conn != nullptr); |
| 3004 | SIMULATED_WAIT(conn->num_pings_sent() == 1, kDefaultTimeout, clock); |
| 3005 | |
| 3006 | // Initializing. |
| 3007 | |
| 3008 | int64_t start = clock.TimeNanos(); |
| 3009 | SIMULATED_WAIT(conn->num_pings_sent() >= MIN_PINGS_AT_WEAK_PING_INTERVAL, |
| 3010 | kDefaultTimeout, clock); |
| 3011 | int64_t ping_interval_ms = (clock.TimeNanos() - start) / |
| 3012 | rtc::kNumNanosecsPerMillisec / |
| 3013 | (MIN_PINGS_AT_WEAK_PING_INTERVAL - 1); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3014 | EXPECT_EQ(ping_interval_ms, WEAK_PING_INTERVAL); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3015 | |
| 3016 | // Stabilizing. |
| 3017 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3018 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3019 | int ping_sent_before = conn->num_pings_sent(); |
| 3020 | start = clock.TimeNanos(); |
| 3021 | // The connection becomes strong but not stable because we haven't been able |
| 3022 | // to converge the RTT. |
| 3023 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, 3000, clock); |
| 3024 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3025 | EXPECT_GE(ping_interval_ms, STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3026 | EXPECT_LE(ping_interval_ms, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3027 | STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3028 | |
| 3029 | // Stabilized. |
| 3030 | |
| 3031 | // The connection becomes stable after receiving more than RTT_RATIO rtt |
| 3032 | // samples. |
| 3033 | for (int i = 0; i < RTT_RATIO; i++) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3034 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3035 | } |
| 3036 | ping_sent_before = conn->num_pings_sent(); |
| 3037 | start = clock.TimeNanos(); |
| 3038 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, 3000, clock); |
| 3039 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3040 | EXPECT_GE(ping_interval_ms, STABLE_WRITABLE_CONNECTION_PING_INTERVAL); |
| 3041 | EXPECT_LE(ping_interval_ms, |
| 3042 | STABLE_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3043 | |
| 3044 | // Destabilized. |
| 3045 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3046 | conn->ReceivedPingResponse(LOW_RTT, "id"); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3047 | // Create a in-flight ping. |
| 3048 | conn->Ping(clock.TimeNanos() / rtc::kNumNanosecsPerMillisec); |
| 3049 | start = clock.TimeNanos(); |
| 3050 | // In-flight ping timeout and the connection will be unstable. |
| 3051 | SIMULATED_WAIT( |
| 3052 | !conn->stable(clock.TimeNanos() / rtc::kNumNanosecsPerMillisec), 3000, |
| 3053 | clock); |
| 3054 | int64_t duration_ms = |
| 3055 | (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
| 3056 | EXPECT_GE(duration_ms, 2 * conn->rtt() - RTT_RANGE); |
| 3057 | EXPECT_LE(duration_ms, 2 * conn->rtt() + RTT_RANGE); |
| 3058 | // The connection become unstable due to not receiving ping responses. |
| 3059 | ping_sent_before = conn->num_pings_sent(); |
| 3060 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, 3000, clock); |
| 3061 | // The interval is expected to be |
| 3062 | // STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL. |
| 3063 | start = clock.TimeNanos(); |
| 3064 | ping_sent_before = conn->num_pings_sent(); |
| 3065 | SIMULATED_WAIT(conn->num_pings_sent() == ping_sent_before + 1, 3000, clock); |
| 3066 | ping_interval_ms = (clock.TimeNanos() - start) / rtc::kNumNanosecsPerMillisec; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3067 | EXPECT_GE(ping_interval_ms, STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3068 | EXPECT_LE(ping_interval_ms, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3069 | STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL + SCHEDULING_RANGE); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3072 | // 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] | 3073 | // parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3074 | TEST_F(P2PTransportChannelPingTest, PingingStartedAsSoonAsPossible) { |
| 3075 | rtc::ScopedFakeClock clock; |
| 3076 | |
| 3077 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3078 | P2PTransportChannel ch("TestChannel", 1, &pa); |
| 3079 | ch.SetIceRole(ICEROLE_CONTROLLING); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3080 | ch.SetIceParameters(kIceParams[0]); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3081 | ch.MaybeStartGathering(); |
| 3082 | EXPECT_EQ_WAIT(IceGatheringState::kIceGatheringComplete, ch.gathering_state(), |
| 3083 | kDefaultTimeout); |
| 3084 | |
| 3085 | // Simulate a binding request being received, creating a peer reflexive |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3086 | // candidate pair while we still don't have remote ICE parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3087 | IceMessage request; |
| 3088 | request.SetType(STUN_BINDING_REQUEST); |
| 3089 | request.AddAttribute( |
| 3090 | new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1])); |
| 3091 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3092 | request.AddAttribute( |
| 3093 | new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); |
| 3094 | Port* port = GetPort(&ch); |
| 3095 | ASSERT_NE(nullptr, port); |
| 3096 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3097 | &request, kIceUfrag[1], false); |
| 3098 | Connection* conn = GetConnectionTo(&ch, "1.1.1.1", 1); |
| 3099 | ASSERT_NE(nullptr, conn); |
| 3100 | |
| 3101 | // 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] | 3102 | // sent, since we don't yet have remote ICE parameters. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3103 | SIMULATED_WAIT(conn->num_pings_sent() > 0, 1025, clock); |
| 3104 | EXPECT_EQ(0, conn->num_pings_sent()); |
| 3105 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3106 | // 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] | 3107 | // the first ping is sent as soon as possible, within one simulated clock |
| 3108 | // tick. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3109 | ch.SetRemoteIceParameters(kIceParams[1]); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 3110 | EXPECT_TRUE_SIMULATED_WAIT(conn->num_pings_sent() > 0, 1, clock); |
| 3111 | } |
| 3112 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3113 | TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3114 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3115 | P2PTransportChannel ch("trigger checks", 1, &pa); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3116 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3117 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3118 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3119 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3120 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3121 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3122 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3123 | ASSERT_TRUE(conn1 != nullptr); |
| 3124 | ASSERT_TRUE(conn2 != nullptr); |
| 3125 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3126 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
| 3127 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3128 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3129 | ASSERT_TRUE(conn1->writable()); |
| 3130 | conn1->ReceivedPing(); |
| 3131 | |
| 3132 | // Ping received, but the connection is already writable, so no |
| 3133 | // "triggered check" and conn2 is pinged before conn1 because it has |
| 3134 | // a higher priority. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3135 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 3136 | } |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3137 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3138 | TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3139 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3140 | P2PTransportChannel ch("Do not ping failed connections", 1, &pa); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3141 | PrepareChannel(&ch); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3142 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3143 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3144 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3145 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 3146 | ASSERT_TRUE(conn1 != nullptr); |
| 3147 | |
| 3148 | EXPECT_EQ(conn1, ch.FindNextPingableConnection()); |
| 3149 | conn1->Prune(); // A pruned connection may still be pingable. |
| 3150 | EXPECT_EQ(conn1, ch.FindNextPingableConnection()); |
| 3151 | conn1->FailAndPrune(); |
| 3152 | EXPECT_TRUE(nullptr == ch.FindNextPingableConnection()); |
| 3153 | } |
| 3154 | |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3155 | TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3156 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3157 | P2PTransportChannel ch("state change", 1, &pa); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3158 | PrepareChannel(&ch); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3159 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3160 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3161 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3162 | ASSERT_TRUE(conn1 != nullptr); |
| 3163 | // Pruning the connection reduces the set of active connections and changes |
| 3164 | // the channel state. |
| 3165 | conn1->Prune(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3166 | EXPECT_EQ_WAIT(STATE_FAILED, channel_state(), kDefaultTimeout); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3169 | // Test adding remote candidates with different ufrags. If a remote candidate |
| 3170 | // is added with an old ufrag, it will be discarded. If it is added with a |
| 3171 | // ufrag that was not seen before, it will be used to create connections |
| 3172 | // 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] | 3173 | // parameters arrive. If a remote candidate is added with the current ICE |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3174 | // ufrag, its pwd and generation will be set properly. |
| 3175 | TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3176 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3177 | P2PTransportChannel ch("add candidate", 1, &pa); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3178 | PrepareChannel(&ch); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3179 | ch.MaybeStartGathering(); |
| 3180 | // Add a candidate with a future ufrag. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3181 | ch.AddRemoteCandidate( |
| 3182 | CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1, kIceUfrag[2])); |
| 3183 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3184 | ASSERT_TRUE(conn1 != nullptr); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3185 | const Candidate& candidate = conn1->remote_candidate(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3186 | EXPECT_EQ(kIceUfrag[2], candidate.username()); |
| 3187 | EXPECT_TRUE(candidate.password().empty()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3188 | EXPECT_TRUE(FindNextPingableConnectionAndPingIt(&ch) == nullptr); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3189 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3190 | // Set the remote ICE parameters with the "future" ufrag. |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3191 | // This should set the ICE pwd in the remote candidate of |conn1|, making |
| 3192 | // it pingable. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3193 | ch.SetRemoteIceParameters(kIceParams[2]); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3194 | EXPECT_EQ(kIceUfrag[2], candidate.username()); |
| 3195 | EXPECT_EQ(kIcePwd[2], candidate.password()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3196 | EXPECT_EQ(conn1, FindNextPingableConnectionAndPingIt(&ch)); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3197 | |
| 3198 | // Add a candidate with an old ufrag. No connection will be created. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3199 | ch.AddRemoteCandidate( |
| 3200 | CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2, kIceUfrag[1])); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3201 | rtc::Thread::Current()->ProcessMessages(500); |
| 3202 | EXPECT_TRUE(GetConnectionTo(&ch, "2.2.2.2", 2) == nullptr); |
| 3203 | |
| 3204 | // Add a candidate with the current ufrag, its pwd and generation will be |
| 3205 | // assigned, even if the generation is not set. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3206 | ch.AddRemoteCandidate( |
| 3207 | CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 0, kIceUfrag[2])); |
| 3208 | Connection* conn3 = nullptr; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3209 | ASSERT_TRUE_WAIT((conn3 = GetConnectionTo(&ch, "3.3.3.3", 3)) != nullptr, |
| 3210 | 3000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3211 | const Candidate& new_candidate = conn3->remote_candidate(); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3212 | EXPECT_EQ(kIcePwd[2], new_candidate.password()); |
| 3213 | EXPECT_EQ(1U, new_candidate.generation()); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3214 | |
| 3215 | // Check that the pwd of all remote candidates are properly assigned. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3216 | for (const RemoteCandidate& candidate : ch.remote_candidates()) { |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3217 | EXPECT_TRUE(candidate.username() == kIceUfrag[1] || |
| 3218 | candidate.username() == kIceUfrag[2]); |
| 3219 | if (candidate.username() == kIceUfrag[1]) { |
| 3220 | EXPECT_EQ(kIcePwd[1], candidate.password()); |
| 3221 | } else if (candidate.username() == kIceUfrag[2]) { |
| 3222 | EXPECT_EQ(kIcePwd[2], candidate.password()); |
| 3223 | } |
| 3224 | } |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 3225 | } |
| 3226 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3227 | TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3228 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3229 | P2PTransportChannel ch("connection resurrection", 1, &pa); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3230 | PrepareChannel(&ch); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3231 | ch.MaybeStartGathering(); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3232 | |
| 3233 | // Create conn1 and keep track of original candidate priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3234 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3235 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3236 | ASSERT_TRUE(conn1 != nullptr); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 3237 | uint32_t remote_priority = conn1->remote_candidate().priority(); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3238 | |
| 3239 | // Create a higher priority candidate and make the connection |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 3240 | // receiving/writable. This will prune conn1. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3241 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
| 3242 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3243 | ASSERT_TRUE(conn2 != nullptr); |
| 3244 | conn2->ReceivedPing(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3245 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3246 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3247 | // Wait for conn1 to be pruned. |
| 3248 | EXPECT_TRUE_WAIT(conn1->pruned(), 3000); |
| 3249 | // Destroy the connection to test SignalUnknownAddress. |
| 3250 | conn1->Destroy(); |
| 3251 | EXPECT_TRUE_WAIT(GetConnectionTo(&ch, "1.1.1.1", 1) == nullptr, 1000); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3252 | |
| 3253 | // Create a minimal STUN message with prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3254 | IceMessage request; |
| 3255 | request.SetType(STUN_BINDING_REQUEST); |
| 3256 | request.AddAttribute( |
| 3257 | new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1])); |
| 3258 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3259 | request.AddAttribute( |
| 3260 | new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3261 | EXPECT_NE(prflx_priority, remote_priority); |
| 3262 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3263 | Port* port = GetPort(&ch); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3264 | // conn1 should be resurrected with original priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3265 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3266 | &request, kIceUfrag[1], false); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3267 | conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3268 | ASSERT_TRUE(conn1 != nullptr); |
| 3269 | EXPECT_EQ(conn1->remote_candidate().priority(), remote_priority); |
| 3270 | |
| 3271 | // conn3, a real prflx connection, should have prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3272 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 1), PROTO_UDP, |
| 3273 | &request, kIceUfrag[1], false); |
| 3274 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 1); |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 3275 | ASSERT_TRUE(conn3 != nullptr); |
| 3276 | EXPECT_EQ(conn3->remote_candidate().priority(), prflx_priority); |
| 3277 | } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3278 | |
| 3279 | TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3280 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3281 | P2PTransportChannel ch("receiving state change", 1, &pa); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3282 | PrepareChannel(&ch); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3283 | // Default receiving timeout and checking receiving interval should not be too |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3284 | // small. |
| 3285 | EXPECT_LE(1000, ch.receiving_timeout()); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3286 | EXPECT_LE(200, ch.check_receiving_interval()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3287 | ch.SetIceConfig(CreateIceConfig(500, GATHER_ONCE)); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3288 | EXPECT_EQ(500, ch.receiving_timeout()); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 3289 | EXPECT_EQ(50, ch.check_receiving_interval()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3290 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3291 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3292 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3293 | ASSERT_TRUE(conn1 != nullptr); |
| 3294 | |
| 3295 | conn1->ReceivedPing(); |
| 3296 | conn1->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 3297 | EXPECT_TRUE_WAIT(ch.receiving(), 1000); |
| 3298 | EXPECT_TRUE_WAIT(!ch.receiving(), 1000); |
| 3299 | } |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3300 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3301 | // The controlled side will select a connection as the "selected connection" |
| 3302 | // based on priority until the controlling side nominates a connection, at which |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3303 | // point the controlled side will select that connection as the |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3304 | // "selected connection". Plus, SignalSelectedCandidatePair will be fired if the |
| 3305 | // selected connection changes and SignalReadyToSend will be fired if the new |
| 3306 | // selected connection is writable. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3307 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3308 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3309 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3310 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3311 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3312 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3313 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3314 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3315 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3316 | // Channel is not ready to send because it is not writable. |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3317 | EXPECT_FALSE(channel_ready_to_send()); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3318 | int last_packet_id = 0; |
| 3319 | const char* data = "ABCDEFGH"; |
| 3320 | int len = static_cast<int>(strlen(data)); |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 3321 | EXPECT_EQ(-1, SendData(ch, data, len, ++last_packet_id)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3322 | EXPECT_EQ(-1, last_sent_packet_id()); |
| 3323 | |
| 3324 | // A connection needs to be writable before it is selected for transmission. |
| 3325 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
| 3326 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
| 3327 | EXPECT_EQ(conn1, last_selected_candidate_pair()); |
| 3328 | EXPECT_EQ(len, SendData(ch, data, len, ++last_packet_id)); |
| 3329 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3330 | // When a higher priority candidate comes in, the new connection is chosen |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3331 | // as the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3332 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); |
| 3333 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3334 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3335 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
| 3336 | EXPECT_EQ_WAIT(conn2, ch.selected_connection(), kDefaultTimeout); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3337 | EXPECT_EQ(conn2, last_selected_candidate_pair()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3338 | EXPECT_TRUE(channel_ready_to_send()); |
| 3339 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3340 | |
| 3341 | // If a stun request with use-candidate attribute arrives, the receiving |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3342 | // connection will be set as the selected connection, even though |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3343 | // its priority is lower. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3344 | EXPECT_EQ(len, SendData(ch, data, len, ++last_packet_id)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3345 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 1)); |
| 3346 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3347 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3348 | // Because it has a lower priority, the selected connection is still conn2. |
| 3349 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3350 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3351 | // 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] | 3352 | // connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3353 | NominateConnection(conn3); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3354 | EXPECT_EQ(conn3, ch.selected_connection()); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3355 | EXPECT_EQ(conn3, last_selected_candidate_pair()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3356 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3357 | EXPECT_TRUE(channel_ready_to_send()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3358 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3359 | // Even if another higher priority candidate arrives, it will not be set as |
| 3360 | // the selected connection because the selected connection is nominated by |
| 3361 | // the controlling side. |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 3362 | EXPECT_EQ(len, SendData(ch, data, len, ++last_packet_id)); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3363 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "4.4.4.4", 4, 100)); |
| 3364 | Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3365 | ASSERT_TRUE(conn4 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3366 | EXPECT_EQ(conn3, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3367 | // 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] | 3368 | // the selected connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3369 | NominateConnection(conn4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3370 | // Not switched yet because conn4 is not writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3371 | EXPECT_EQ(conn3, ch.selected_connection()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3372 | reset_channel_ready_to_send(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3373 | // The selected connection switches after conn4 becomes writable. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3374 | conn4->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3375 | EXPECT_EQ_WAIT(conn4, ch.selected_connection(), kDefaultTimeout); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 3376 | EXPECT_EQ(conn4, last_selected_candidate_pair()); |
| 3377 | EXPECT_EQ(last_packet_id, last_sent_packet_id()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 3378 | // SignalReadyToSend is fired again because conn4 is writable. |
| 3379 | EXPECT_TRUE(channel_ready_to_send()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3382 | // The controlled side will select a connection as the "selected connection" |
| 3383 | // based on requests from an unknown address before the controlling side |
| 3384 | // nominates a connection, and will nominate a connection from an unknown |
| 3385 | // address if the request contains the use_candidate attribute. Plus, it will |
| 3386 | // also sends back a ping response and set the ICE pwd in the remote candidate |
| 3387 | // appropriately. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3388 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3389 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3390 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3391 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3392 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3393 | ch.MaybeStartGathering(); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3394 | // A minimal STUN message with prflx priority. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3395 | IceMessage request; |
| 3396 | request.SetType(STUN_BINDING_REQUEST); |
| 3397 | request.AddAttribute( |
| 3398 | new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1])); |
| 3399 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3400 | request.AddAttribute( |
| 3401 | new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); |
| 3402 | TestUDPPort* port = static_cast<TestUDPPort*>(GetPort(&ch)); |
| 3403 | port->SignalUnknownAddress(port, rtc::SocketAddress("1.1.1.1", 1), PROTO_UDP, |
| 3404 | &request, kIceUfrag[1], false); |
| 3405 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3406 | ASSERT_TRUE(conn1 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3407 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3408 | EXPECT_NE(conn1, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3409 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3410 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3411 | port->set_sent_binding_response(false); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3412 | |
| 3413 | // Another connection is nominated via use_candidate. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3414 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3415 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3416 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3417 | // Because it has a lower priority, the selected connection is still conn1. |
| 3418 | EXPECT_EQ(conn1, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3419 | // 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] | 3420 | // selected connection. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3421 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
| 3422 | NominateConnection(conn2); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3423 | EXPECT_EQ(conn2, ch.selected_connection()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3424 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3425 | // Another request with unknown address, it will not be set as the selected |
| 3426 | // connection because the selected connection was nominated by the controlling |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3427 | // side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3428 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP, |
| 3429 | &request, kIceUfrag[1], false); |
| 3430 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3431 | ASSERT_TRUE(conn3 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3432 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3433 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3434 | EXPECT_EQ(conn2, ch.selected_connection()); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3435 | port->set_sent_binding_response(false); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3436 | |
| 3437 | // However if the request contains use_candidate attribute, it will be |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3438 | // selected as the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3439 | request.AddAttribute(new StunByteStringAttribute(STUN_ATTR_USE_CANDIDATE)); |
| 3440 | port->SignalUnknownAddress(port, rtc::SocketAddress("4.4.4.4", 4), PROTO_UDP, |
| 3441 | &request, kIceUfrag[1], false); |
| 3442 | Connection* conn4 = WaitForConnectionTo(&ch, "4.4.4.4", 4); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3443 | ASSERT_TRUE(conn4 != nullptr); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 3444 | EXPECT_TRUE(port->sent_binding_response()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3445 | // conn4 is not the selected connection yet because it is not writable. |
| 3446 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3447 | conn4->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3448 | EXPECT_EQ_WAIT(conn4, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3449 | |
| 3450 | // Test that the request from an unknown address contains a ufrag from an old |
| 3451 | // generation. |
| 3452 | port->set_sent_binding_response(false); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3453 | ch.SetRemoteIceParameters(kIceParams[2]); |
| 3454 | ch.SetRemoteIceParameters(kIceParams[3]); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3455 | port->SignalUnknownAddress(port, rtc::SocketAddress("5.5.5.5", 5), PROTO_UDP, |
| 3456 | &request, kIceUfrag[2], false); |
| 3457 | Connection* conn5 = WaitForConnectionTo(&ch, "5.5.5.5", 5); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 3458 | ASSERT_TRUE(conn5 != nullptr); |
| 3459 | EXPECT_TRUE(port->sent_binding_response()); |
| 3460 | EXPECT_EQ(kIcePwd[2], conn5->remote_candidate().password()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3461 | } |
| 3462 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3463 | // The controlled side will select a connection as the "selected connection" |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3464 | // based on media received until the controlling side nominates a connection, |
| 3465 | // at which point the controlled side will select that connection as |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3466 | // the "selected connection". |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3467 | TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3468 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3469 | P2PTransportChannel ch("receiving state change", 1, &pa); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3470 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3471 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3472 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3473 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 10)); |
| 3474 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3475 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3476 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
| 3477 | EXPECT_EQ_WAIT(conn1, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3478 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3479 | // If a data packet is received on conn2, the selected connection should |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3480 | // switch to conn2 because the controlled side must mirror the media path |
| 3481 | // chosen by the controlling side. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3482 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3483 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3484 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3485 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable and receiving. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3486 | conn2->OnReadPacket("ABC", 3, rtc::CreatePacketTime(0)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3487 | EXPECT_EQ(conn2, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3488 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3489 | |
| 3490 | // Now another STUN message with an unknown address and use_candidate will |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3491 | // nominate the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3492 | IceMessage request; |
| 3493 | request.SetType(STUN_BINDING_REQUEST); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3494 | request.AddAttribute( |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3495 | new StunByteStringAttribute(STUN_ATTR_USERNAME, kIceUfrag[1])); |
| 3496 | uint32_t prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24; |
| 3497 | request.AddAttribute( |
| 3498 | new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); |
| 3499 | request.AddAttribute(new StunByteStringAttribute(STUN_ATTR_USE_CANDIDATE)); |
| 3500 | Port* port = GetPort(&ch); |
| 3501 | port->SignalUnknownAddress(port, rtc::SocketAddress("3.3.3.3", 3), PROTO_UDP, |
| 3502 | &request, kIceUfrag[1], false); |
| 3503 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3504 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3505 | EXPECT_EQ(conn2, ch.selected_connection()); // Not writable yet. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3506 | conn3->ReceivedPingResponse(LOW_RTT, "id"); // Become writable. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3507 | EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3508 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3509 | // Now another data packet will not switch the selected connection because the |
| 3510 | // selected connection was nominated by the controlling side. |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3511 | conn2->ReceivedPing(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3512 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3513 | conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3514 | EXPECT_EQ_WAIT(conn3, ch.selected_connection(), kDefaultTimeout); |
| 3515 | } |
| 3516 | |
| 3517 | TEST_F(P2PTransportChannelPingTest, |
| 3518 | TestControlledAgentDataReceivingTakesHigherPrecedenceThanPriority) { |
| 3519 | rtc::ScopedFakeClock clock; |
| 3520 | |
| 3521 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3522 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 3523 | PrepareChannel(&ch); |
| 3524 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3525 | ch.MaybeStartGathering(); |
| 3526 | // The connections have decreasing priority. |
| 3527 | Connection* conn1 = |
| 3528 | CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, true); |
| 3529 | ASSERT_TRUE(conn1 != nullptr); |
| 3530 | Connection* conn2 = |
| 3531 | CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, true); |
| 3532 | ASSERT_TRUE(conn2 != nullptr); |
| 3533 | |
| 3534 | // Initially, connections are selected based on priority. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3535 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3536 | EXPECT_EQ(conn1, last_selected_candidate_pair()); |
| 3537 | |
| 3538 | // conn2 receives data; it becomes selected. |
| 3539 | // Advance the clock by 1ms so that the last data receiving timestamp of |
| 3540 | // conn2 is larger. |
| 3541 | SIMULATED_WAIT(false, 1, clock); |
| 3542 | conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3543 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3544 | EXPECT_EQ(conn2, last_selected_candidate_pair()); |
| 3545 | |
| 3546 | // conn1 also receives data; it becomes selected due to priority again. |
| 3547 | conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3548 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3549 | EXPECT_EQ(conn1, last_selected_candidate_pair()); |
| 3550 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3551 | // conn2 received data more recently; it is selected now because it |
| 3552 | // received data more recently. |
| 3553 | SIMULATED_WAIT(false, 1, clock); |
| 3554 | // Need to become writable again because it was pruned. |
| 3555 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
| 3556 | conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
| 3557 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
| 3558 | EXPECT_EQ(conn2, last_selected_candidate_pair()); |
| 3559 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3560 | // Make sure sorting won't reselect candidate pair. |
| 3561 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3562 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | TEST_F(P2PTransportChannelPingTest, |
| 3566 | TestControlledAgentNominationTakesHigherPrecedenceThanDataReceiving) { |
| 3567 | rtc::ScopedFakeClock clock; |
| 3568 | |
| 3569 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3570 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 3571 | PrepareChannel(&ch); |
| 3572 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3573 | ch.MaybeStartGathering(); |
| 3574 | // The connections have decreasing priority. |
| 3575 | Connection* conn1 = |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3576 | CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3577 | ASSERT_TRUE(conn1 != nullptr); |
| 3578 | Connection* conn2 = |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3579 | CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3580 | ASSERT_TRUE(conn2 != nullptr); |
| 3581 | |
| 3582 | // conn1 received data; it is the selected connection. |
| 3583 | // Advance the clock to have a non-zero last-data-receiving time. |
| 3584 | SIMULATED_WAIT(false, 1, clock); |
| 3585 | conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3586 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3587 | EXPECT_EQ(conn1, last_selected_candidate_pair()); |
| 3588 | |
| 3589 | // conn2 is nominated; it becomes the selected connection. |
| 3590 | NominateConnection(conn2); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3591 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3592 | EXPECT_EQ(conn2, last_selected_candidate_pair()); |
| 3593 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3594 | // conn1 is selected because it has higher priority and also nominated. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3595 | NominateConnection(conn1); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3596 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3597 | EXPECT_EQ(conn1, last_selected_candidate_pair()); |
| 3598 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3599 | // Make sure sorting won't reselect candidate pair. |
| 3600 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3601 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | TEST_F(P2PTransportChannelPingTest, |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3605 | TestControlledAgentSelectsConnectionWithHigherNomination) { |
| 3606 | rtc::ScopedFakeClock clock; |
| 3607 | |
| 3608 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3609 | P2PTransportChannel ch("test", 1, &pa); |
| 3610 | PrepareChannel(&ch); |
| 3611 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 3612 | ch.MaybeStartGathering(); |
| 3613 | // The connections have decreasing priority. |
| 3614 | Connection* conn1 = |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3615 | CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3616 | ASSERT_TRUE(conn1 != nullptr); |
| 3617 | Connection* conn2 = |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3618 | CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, true); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3619 | ASSERT_TRUE(conn2 != nullptr); |
| 3620 | |
| 3621 | // conn1 is the selected connection because it has a higher priority, |
| 3622 | EXPECT_EQ_SIMULATED_WAIT(conn1, last_selected_candidate_pair(), |
| 3623 | kDefaultTimeout, clock); |
| 3624 | reset_selected_candidate_pair_switches(); |
| 3625 | |
| 3626 | // conn2 is nominated; it becomes selected. |
| 3627 | NominateConnection(conn2); |
| 3628 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
| 3629 | EXPECT_EQ(conn2, last_selected_candidate_pair()); |
| 3630 | |
| 3631 | // conn1 is selected because of its priority. |
| 3632 | NominateConnection(conn1); |
| 3633 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
| 3634 | EXPECT_EQ(conn1, last_selected_candidate_pair()); |
| 3635 | |
| 3636 | // conn2 gets higher remote nomination; it is selected again. |
| 3637 | NominateConnection(conn2, 2U); |
| 3638 | EXPECT_EQ(1, reset_selected_candidate_pair_switches()); |
| 3639 | EXPECT_EQ(conn2, last_selected_candidate_pair()); |
| 3640 | |
| 3641 | // Make sure sorting won't reselect candidate pair. |
| 3642 | SIMULATED_WAIT(false, 100, clock); |
| 3643 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
| 3644 | } |
| 3645 | |
| 3646 | TEST_F(P2PTransportChannelPingTest, |
| 3647 | TestControlledAgentIgnoresSmallerNomination) { |
| 3648 | rtc::ScopedFakeClock clock; |
| 3649 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3650 | P2PTransportChannel ch("test", 1, &pa); |
| 3651 | PrepareChannel(&ch); |
| 3652 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 3653 | ch.MaybeStartGathering(); |
| 3654 | Connection* conn = |
| 3655 | CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, false); |
| 3656 | ReceivePingOnConnection(conn, kIceUfrag[1], 1, 2U); |
| 3657 | EXPECT_EQ(2U, conn->remote_nomination()); |
| 3658 | // Smaller nomination is ignored. |
| 3659 | ReceivePingOnConnection(conn, kIceUfrag[1], 1, 1U); |
| 3660 | EXPECT_EQ(2U, conn->remote_nomination()); |
| 3661 | } |
| 3662 | |
| 3663 | TEST_F(P2PTransportChannelPingTest, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3664 | TestControlledAgentWriteStateTakesHigherPrecedenceThanNomination) { |
| 3665 | rtc::ScopedFakeClock clock; |
| 3666 | |
| 3667 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3668 | P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); |
| 3669 | PrepareChannel(&ch); |
| 3670 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3671 | ch.MaybeStartGathering(); |
| 3672 | // The connections have decreasing priority. |
| 3673 | Connection* conn1 = |
| 3674 | CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 10, false); |
| 3675 | ASSERT_TRUE(conn1 != nullptr); |
| 3676 | Connection* conn2 = |
| 3677 | CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 9, false); |
| 3678 | ASSERT_TRUE(conn2 != nullptr); |
| 3679 | |
| 3680 | NominateConnection(conn1); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3681 | // There is no selected connection because no connection is writable. |
| 3682 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3683 | |
| 3684 | // conn2 becomes writable; it is selected even though it is not nominated. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3685 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3686 | EXPECT_EQ_SIMULATED_WAIT(1, reset_selected_candidate_pair_switches(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3687 | kDefaultTimeout, clock); |
| 3688 | EXPECT_EQ_SIMULATED_WAIT(conn2, last_selected_candidate_pair(), |
| 3689 | kDefaultTimeout, clock); |
| 3690 | |
| 3691 | // If conn1 is also writable, it will become selected. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3692 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3693 | EXPECT_EQ_SIMULATED_WAIT(1, reset_selected_candidate_pair_switches(), |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3694 | kDefaultTimeout, clock); |
| 3695 | EXPECT_EQ_SIMULATED_WAIT(conn1, last_selected_candidate_pair(), |
| 3696 | kDefaultTimeout, clock); |
| 3697 | |
| 3698 | // Make sure sorting won't reselect candidate pair. |
| 3699 | SIMULATED_WAIT(false, 10, clock); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3700 | EXPECT_EQ(0, reset_selected_candidate_pair_switches()); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 3701 | } |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3702 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3703 | // Test that if a new remote candidate has the same address and port with |
| 3704 | // an old one, it will be used to create a new connection. |
| 3705 | TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3706 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3707 | P2PTransportChannel ch("candidate reuse", 1, &pa); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3708 | PrepareChannel(&ch); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3709 | ch.MaybeStartGathering(); |
| 3710 | const std::string host_address = "1.1.1.1"; |
| 3711 | const int port_num = 1; |
| 3712 | |
| 3713 | // kIceUfrag[1] is the current generation ufrag. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3714 | Candidate candidate = CreateUdpCandidate(LOCAL_PORT_TYPE, host_address, |
| 3715 | port_num, 1, kIceUfrag[1]); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3716 | ch.AddRemoteCandidate(candidate); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3717 | Connection* conn1 = WaitForConnectionTo(&ch, host_address, port_num); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3718 | ASSERT_TRUE(conn1 != nullptr); |
| 3719 | EXPECT_EQ(0u, conn1->remote_candidate().generation()); |
| 3720 | |
| 3721 | // Simply adding the same candidate again won't create a new connection. |
| 3722 | ch.AddRemoteCandidate(candidate); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3723 | Connection* conn2 = GetConnectionTo(&ch, host_address, port_num); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3724 | EXPECT_EQ(conn1, conn2); |
| 3725 | |
| 3726 | // Update the ufrag of the candidate and add it again. |
| 3727 | candidate.set_username(kIceUfrag[2]); |
| 3728 | ch.AddRemoteCandidate(candidate); |
| 3729 | conn2 = GetConnectionTo(&ch, host_address, port_num); |
| 3730 | EXPECT_NE(conn1, conn2); |
| 3731 | EXPECT_EQ(kIceUfrag[2], conn2->remote_candidate().username()); |
| 3732 | EXPECT_EQ(1u, conn2->remote_candidate().generation()); |
| 3733 | |
| 3734 | // Verify that a ping with the new ufrag can be received on the new |
| 3735 | // connection. |
| 3736 | EXPECT_EQ(0, conn2->last_ping_received()); |
| 3737 | ReceivePingOnConnection(conn2, kIceUfrag[2], 1 /* priority */); |
| 3738 | EXPECT_TRUE(conn2->last_ping_received() > 0); |
| 3739 | } |
| 3740 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3741 | // When the current selected connection is strong, lower-priority connections |
| 3742 | // will be pruned. Otherwise, lower-priority connections are kept. |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3743 | TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3744 | rtc::ScopedFakeClock clock; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3745 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3746 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3747 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3748 | ch.SetIceRole(ICEROLE_CONTROLLED); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3749 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3750 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 3751 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3752 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3753 | EXPECT_EQ(nullptr, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3754 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3755 | |
| 3756 | // When a higher-priority, nominated candidate comes in, the connections with |
| 3757 | // lower-priority are pruned. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3758 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 10)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3759 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3760 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3761 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
| 3762 | NominateConnection(conn2); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3763 | EXPECT_TRUE_SIMULATED_WAIT(conn1->pruned(), 3000, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3764 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3765 | ch.SetIceConfig(CreateIceConfig(500, GATHER_ONCE)); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3766 | // Wait until conn2 becomes not receiving. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3767 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->receiving(), 3000, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3768 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3769 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 1)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3770 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3, &clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3771 | ASSERT_TRUE(conn3 != nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3772 | // The selected connection should still be conn2. Even through conn3 has lower |
| 3773 | // priority and is not receiving/writable, it is not pruned because the |
| 3774 | // selected connection is not receiving. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3775 | SIMULATED_WAIT(conn3->pruned(), 1000, clock); |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 3776 | EXPECT_FALSE(conn3->pruned()); |
| 3777 | } |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3778 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3779 | TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { |
| 3780 | rtc::ScopedFakeClock clock; |
| 3781 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3782 | P2PTransportChannel ch("test channel", 1, &pa); |
| 3783 | PrepareChannel(&ch); |
| 3784 | ch.SetIceRole(ICEROLE_CONTROLLED); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3785 | ch.MaybeStartGathering(); |
| 3786 | Connection* conn1 = |
| 3787 | CreateConnectionWithCandidate(ch, clock, "1.1.1.1", 1, 100, true); |
| 3788 | ASSERT_TRUE(conn1 != nullptr); |
| 3789 | Connection* conn2 = |
| 3790 | CreateConnectionWithCandidate(ch, clock, "2.2.2.2", 2, 200, false); |
| 3791 | ASSERT_TRUE(conn2 != nullptr); |
| 3792 | // Even if conn1 is writable, nominated, receiving data, it should not prune |
| 3793 | // conn2. |
| 3794 | NominateConnection(conn1); |
| 3795 | SIMULATED_WAIT(false, 1, clock); |
| 3796 | conn1->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); |
| 3797 | SIMULATED_WAIT(conn2->pruned(), 100, clock); |
| 3798 | EXPECT_FALSE(conn2->pruned()); |
| 3799 | } |
| 3800 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3801 | // Test that GetState returns the state correctly. |
| 3802 | TEST_F(P2PTransportChannelPingTest, TestGetState) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3803 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3804 | P2PTransportChannel ch("test channel", 1, &pa); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3805 | PrepareChannel(&ch); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3806 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3807 | EXPECT_EQ(TransportChannelState::STATE_INIT, ch.GetState()); |
| 3808 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 3809 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3810 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 3811 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3812 | ASSERT_TRUE(conn1 != nullptr); |
| 3813 | ASSERT_TRUE(conn2 != nullptr); |
| 3814 | // Now there are two connections, so the transport channel is connecting. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3815 | EXPECT_EQ(TransportChannelState::STATE_CONNECTING, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3816 | // |conn1| becomes writable and receiving; it then should prune |conn2|. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3817 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3818 | EXPECT_TRUE_WAIT(conn2->pruned(), 1000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3819 | EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3820 | conn1->Prune(); // All connections are pruned. |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 3821 | // Need to wait until the channel state is updated. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3822 | EXPECT_EQ_WAIT(TransportChannelState::STATE_FAILED, ch.GetState(), 1000); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | // Test that when a low-priority connection is pruned, it is not deleted |
| 3826 | // right away, and it can become active and be pruned again. |
| 3827 | TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3828 | rtc::ScopedFakeClock clock; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3829 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3830 | P2PTransportChannel ch("test channel", 1, &pa); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3831 | PrepareChannel(&ch); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 3832 | IceConfig config = CreateIceConfig(1000, GATHER_ONCE); |
| 3833 | config.receiving_switching_delay = rtc::Optional<int>(800); |
| 3834 | ch.SetIceConfig(config); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3835 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3836 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3837 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3838 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3839 | EXPECT_EQ(nullptr, ch.selected_connection()); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3840 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3841 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 3842 | clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3843 | |
| 3844 | // 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] | 3845 | // not be deleted right away. Once the current selected connection becomes not |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3846 | // receiving, |conn2| will start to ping and upon receiving the ping response, |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 3847 | // it will become the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3848 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3849 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3850 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3851 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->active(), kDefaultTimeout, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3852 | // |conn2| should not send a ping yet. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3853 | EXPECT_EQ(Connection::STATE_WAITING, conn2->state()); |
| 3854 | EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3855 | // Wait for |conn1| becoming not receiving. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3856 | EXPECT_TRUE_SIMULATED_WAIT(!conn1->receiving(), 3000, clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3857 | // Make sure conn2 is not deleted. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3858 | conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2, &clock); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3859 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3860 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_INPROGRESS, conn2->state(), |
| 3861 | kDefaultTimeout, clock); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3862 | conn2->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3863 | EXPECT_EQ_SIMULATED_WAIT(conn2, ch.selected_connection(), kDefaultTimeout, |
| 3864 | clock); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3865 | EXPECT_EQ(TransportChannelState::STATE_CONNECTING, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3866 | |
| 3867 | // When |conn1| comes back again, |conn2| will be pruned again. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3868 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 3869 | EXPECT_EQ_SIMULATED_WAIT(conn1, ch.selected_connection(), kDefaultTimeout, |
| 3870 | clock); |
| 3871 | EXPECT_TRUE_SIMULATED_WAIT(!conn2->active(), kDefaultTimeout, clock); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3872 | EXPECT_EQ(TransportChannelState::STATE_COMPLETED, ch.GetState()); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 3873 | } |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 3874 | |
| 3875 | // Test that if all connections in a channel has timed out on writing, they |
| 3876 | // will all be deleted. We use Prune to simulate write_time_out. |
| 3877 | TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3878 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3879 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 3880 | PrepareChannel(&ch); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 3881 | ch.MaybeStartGathering(); |
| 3882 | // Have one connection only but later becomes write-time-out. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3883 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 3884 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 3885 | ASSERT_TRUE(conn1 != nullptr); |
| 3886 | conn1->ReceivedPing(); // Becomes receiving |
| 3887 | conn1->Prune(); |
| 3888 | EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); |
| 3889 | |
| 3890 | // Have two connections but both become write-time-out later. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3891 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 1)); |
| 3892 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 3893 | ASSERT_TRUE(conn2 != nullptr); |
| 3894 | conn2->ReceivedPing(); // Becomes receiving |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3895 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "3.3.3.3", 3, 2)); |
| 3896 | Connection* conn3 = WaitForConnectionTo(&ch, "3.3.3.3", 3); |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 3897 | ASSERT_TRUE(conn3 != nullptr); |
| 3898 | conn3->ReceivedPing(); // Becomes receiving |
| 3899 | // Now prune both conn2 and conn3; they will be deleted soon. |
| 3900 | conn2->Prune(); |
| 3901 | conn3->Prune(); |
| 3902 | EXPECT_TRUE_WAIT(ch.connections().empty(), 1000); |
| 3903 | } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3904 | |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 3905 | // Tests that after a port allocator session is started, it will be stopped |
| 3906 | // when a new connection becomes writable and receiving. Also tests that if a |
| 3907 | // connection belonging to an old session becomes writable, it won't stop |
| 3908 | // the current port allocator session. |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3909 | TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3910 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3911 | P2PTransportChannel ch("test channel", 1, &pa); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3912 | PrepareChannel(&ch); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3913 | ch.SetIceConfig(CreateIceConfig(2000, GATHER_ONCE)); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3914 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3915 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 100)); |
| 3916 | Connection* conn1 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3917 | ASSERT_TRUE(conn1 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3918 | conn1->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3919 | EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); |
| 3920 | |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 3921 | // Start a new session. Even though conn1, which belongs to an older |
| 3922 | // session, becomes unwritable and writable again, it should not stop the |
| 3923 | // current session. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3924 | ch.SetIceParameters(kIceParams[1]); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3925 | ch.MaybeStartGathering(); |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 3926 | conn1->Prune(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3927 | conn1->ReceivedPingResponse(LOW_RTT, "id"); |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 3928 | EXPECT_TRUE(ch.allocator_session()->IsGettingPorts()); |
| 3929 | |
| 3930 | // But if a new connection created from the new session becomes writable, |
| 3931 | // it will stop the current session. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3932 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 100)); |
| 3933 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3934 | ASSERT_TRUE(conn2 != nullptr); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 3935 | conn2->ReceivedPingResponse(LOW_RTT, "id"); // Becomes writable and receiving |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 3936 | EXPECT_TRUE(!ch.allocator_session()->IsGettingPorts()); |
| 3937 | } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 3938 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3939 | // Test that the ICE role is updated even on ports that has been removed. |
| 3940 | // These ports may still have connections that need a correct role, in case that |
| 3941 | // the connections on it may still receive stun pings. |
| 3942 | TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnRemovedPort) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3943 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3944 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3945 | // Starts with ICEROLE_CONTROLLING. |
| 3946 | PrepareChannel(&ch); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3947 | IceConfig config = CreateIceConfig(1000, GATHER_CONTINUALLY); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3948 | ch.SetIceConfig(config); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3949 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3950 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3951 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3952 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3953 | ASSERT_TRUE(conn != nullptr); |
| 3954 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 3955 | // Make a fake signal to remove the ports in the p2ptransportchannel. then |
| 3956 | // change the ICE role and expect it to be updated. |
| 3957 | std::vector<PortInterface*> ports(1, conn->port()); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 3958 | ch.allocator_session()->SignalPortsPruned(ch.allocator_session(), ports); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3959 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 3960 | EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | // Test that the ICE role is updated even on ports with inactive networks. |
| 3964 | // These ports may still have connections that need a correct role, for the |
| 3965 | // pings sent by those connections until they're replaced by newer-generation |
| 3966 | // connections. |
| 3967 | TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3968 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3969 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3970 | // Starts with ICEROLE_CONTROLLING. |
| 3971 | PrepareChannel(&ch); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3972 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3973 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3974 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3975 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3976 | ASSERT_TRUE(conn != nullptr); |
| 3977 | |
| 3978 | // Do an ICE restart, change the role, and expect the old port to have its |
| 3979 | // role updated. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3980 | ch.SetIceParameters(kIceParams[1]); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3981 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3982 | ch.SetIceRole(ICEROLE_CONTROLLED); |
| 3983 | EXPECT_EQ(ICEROLE_CONTROLLED, conn->port()->GetIceRole()); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | // Test that after some amount of time without receiving data, the connection |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3987 | // will be destroyed. The port will only be destroyed after it is marked as |
| 3988 | // "pruned." |
| 3989 | TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeoutAndPruned) { |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3990 | rtc::ScopedFakeClock fake_clock; |
| 3991 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3992 | FakePortAllocator pa(rtc::Thread::Current(), nullptr); |
| 3993 | P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3994 | PrepareChannel(&ch); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3995 | ch.SetIceRole(ICEROLE_CONTROLLED); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3996 | ch.MaybeStartGathering(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3997 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 3998 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 3999 | Connection* conn = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4000 | ASSERT_TRUE(conn != nullptr); |
| 4001 | |
| 4002 | // Simulate 2 minutes going by. This should be enough time for the port to |
| 4003 | // time out. |
| 4004 | for (int second = 0; second < 120; ++second) { |
| 4005 | fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1)); |
| 4006 | } |
| 4007 | EXPECT_EQ(nullptr, GetConnectionTo(&ch, "1.1.1.1", 1)); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 4008 | // Port will not be removed because it is not pruned yet. |
| 4009 | PortInterface* port = GetPort(&ch); |
| 4010 | ASSERT_NE(nullptr, port); |
| 4011 | |
| 4012 | // If the session prunes all ports, the port will be destroyed. |
| 4013 | ch.allocator_session()->PruneAllPorts(); |
| 4014 | EXPECT_EQ_SIMULATED_WAIT(nullptr, GetPort(&ch), 1, fake_clock); |
| 4015 | EXPECT_EQ_SIMULATED_WAIT(nullptr, GetPrunedPort(&ch), 1, fake_clock); |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4018 | class P2PTransportChannelMostLikelyToWorkFirstTest |
| 4019 | : public P2PTransportChannelPingTest { |
| 4020 | public: |
| 4021 | P2PTransportChannelMostLikelyToWorkFirstTest() |
| 4022 | : turn_server_(rtc::Thread::Current(), kTurnUdpIntAddr, kTurnUdpExtAddr) { |
| 4023 | network_manager_.AddInterface(kPublicAddrs[0]); |
Honghai Zhang | b73d269 | 2016-09-29 22:46:09 -0700 | [diff] [blame] | 4024 | allocator_.reset( |
| 4025 | CreateBasicPortAllocator(&network_manager_, ServerAddresses(), |
| 4026 | kTurnUdpIntAddr, rtc::SocketAddress())); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4027 | allocator_->set_flags(allocator_->flags() | PORTALLOCATOR_DISABLE_STUN | |
| 4028 | PORTALLOCATOR_DISABLE_TCP); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4029 | allocator_->set_step_delay(kMinimumStepDelay); |
| 4030 | } |
| 4031 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4032 | P2PTransportChannel& StartTransportChannel( |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4033 | bool prioritize_most_likely_to_work, |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 4034 | int stable_writable_connection_ping_interval) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4035 | channel_.reset(new P2PTransportChannel("checks", 1, nullptr, allocator())); |
| 4036 | IceConfig config = channel_->config(); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4037 | config.prioritize_most_likely_candidate_pairs = |
| 4038 | prioritize_most_likely_to_work; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 4039 | config.stable_writable_connection_ping_interval = |
| 4040 | stable_writable_connection_ping_interval; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4041 | channel_->SetIceConfig(config); |
| 4042 | PrepareChannel(channel_.get()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4043 | channel_->MaybeStartGathering(); |
| 4044 | return *channel_.get(); |
| 4045 | } |
| 4046 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4047 | BasicPortAllocator* allocator() { return allocator_.get(); } |
| 4048 | TestTurnServer* turn_server() { return &turn_server_; } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4049 | |
| 4050 | // This verifies the next pingable connection has the expected candidates' |
| 4051 | // types and, for relay local candidate, the expected relay protocol and ping |
| 4052 | // it. |
| 4053 | void VerifyNextPingableConnection( |
| 4054 | const std::string& local_candidate_type, |
| 4055 | const std::string& remote_candidate_type, |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4056 | const std::string& relay_protocol_type = UDP_PROTOCOL_NAME) { |
| 4057 | Connection* conn = FindNextPingableConnectionAndPingIt(channel_.get()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4058 | EXPECT_EQ(conn->local_candidate().type(), local_candidate_type); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4059 | if (conn->local_candidate().type() == RELAY_PORT_TYPE) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4060 | EXPECT_EQ(conn->local_candidate().relay_protocol(), relay_protocol_type); |
| 4061 | } |
| 4062 | EXPECT_EQ(conn->remote_candidate().type(), remote_candidate_type); |
| 4063 | } |
| 4064 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4065 | private: |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4066 | std::unique_ptr<BasicPortAllocator> allocator_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4067 | rtc::FakeNetworkManager network_manager_; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4068 | TestTurnServer turn_server_; |
| 4069 | std::unique_ptr<P2PTransportChannel> channel_; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4070 | }; |
| 4071 | |
| 4072 | // Test that Relay/Relay connections will be pinged first when no other |
| 4073 | // 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] | 4074 | // we have a selected connection. |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4075 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4076 | TestRelayRelayFirstWhenNothingPingedYet) { |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4077 | const int max_strong_interval = 100; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4078 | P2PTransportChannel& ch = StartTransportChannel(true, max_strong_interval); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4079 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4080 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4081 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4082 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4083 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
| 4084 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4085 | |
| 4086 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000); |
| 4087 | |
| 4088 | // Relay/Relay should be the first pingable connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4089 | Connection* conn = FindNextPingableConnectionAndPingIt(&ch); |
| 4090 | EXPECT_EQ(conn->local_candidate().type(), RELAY_PORT_TYPE); |
| 4091 | EXPECT_EQ(conn->remote_candidate().type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4092 | |
| 4093 | // Unless that we have a trigger check waiting to be pinged. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4094 | Connection* conn2 = WaitForConnectionTo(&ch, "2.2.2.2", 2); |
| 4095 | EXPECT_EQ(conn2->local_candidate().type(), LOCAL_PORT_TYPE); |
| 4096 | EXPECT_EQ(conn2->remote_candidate().type(), LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4097 | conn2->ReceivedPing(); |
| 4098 | EXPECT_EQ(conn2, FindNextPingableConnectionAndPingIt(&ch)); |
| 4099 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4100 | // Make conn3 the selected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4101 | Connection* conn3 = WaitForConnectionTo(&ch, "1.1.1.1", 1); |
| 4102 | EXPECT_EQ(conn3->local_candidate().type(), LOCAL_PORT_TYPE); |
| 4103 | EXPECT_EQ(conn3->remote_candidate().type(), RELAY_PORT_TYPE); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4104 | conn3->ReceivedPingResponse(LOW_RTT, "id"); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4105 | ASSERT_TRUE(conn3->writable()); |
| 4106 | conn3->ReceivedPing(); |
| 4107 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4108 | /* |
| 4109 | |
| 4110 | TODO(honghaiz): Re-enable this once we use fake clock for this test to fix |
| 4111 | the flakiness. The following test becomes flaky because we now ping the |
| 4112 | connections with fast rates until every connection is pinged at least three |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4113 | times. The selected connection may have been pinged before |
| 4114 | |max_strong_interval|, so it may not be the next connection to be pinged as |
| 4115 | expected in the test. |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4116 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4117 | // Verify that conn3 will be the "selected connection" since it is readable |
| 4118 | // and writable. After |MAX_CURRENT_STRONG_INTERVAL|, it should be the next |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4119 | // pingable connection. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 4120 | EXPECT_TRUE_WAIT(conn3 == ch.selected_connection(), 5000); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 4121 | WAIT(false, max_strong_interval + 100); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 4122 | conn3->ReceivedPingResponse(LOW_RTT, "id"); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4123 | ASSERT_TRUE(conn3->writable()); |
| 4124 | EXPECT_EQ(conn3, FindNextPingableConnectionAndPingIt(&ch)); |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 4125 | |
| 4126 | */ |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | // Test that Relay/Relay connections will be pinged first when everything has |
| 4130 | // been pinged even if the Relay/Relay connection wasn't the first to be pinged |
| 4131 | // in the first round. |
| 4132 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4133 | TestRelayRelayFirstWhenEverythingPinged) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4134 | P2PTransportChannel& ch = StartTransportChannel(true, 100); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4135 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4136 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4137 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4138 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4139 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "1.1.1.1", 1, 1)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4140 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000); |
| 4141 | |
| 4142 | // Initially, only have Local/Local and Local/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4143 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE); |
| 4144 | VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4145 | |
| 4146 | // Remote Relay candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4147 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "2.2.2.2", 2, 2)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4148 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000); |
| 4149 | |
| 4150 | // Relay/Relay should be the first since it hasn't been pinged before. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4151 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4152 | |
| 4153 | // Local/Relay is the final one. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4154 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4155 | |
| 4156 | // Now, every connection has been pinged once. The next one should be |
| 4157 | // Relay/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4158 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | // Test that when we receive a new remote candidate, they will be tried first |
| 4162 | // before we re-ping Relay/Relay connections again. |
| 4163 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, |
| 4164 | TestNoStarvationOnNonRelayConnection) { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4165 | P2PTransportChannel& ch = StartTransportChannel(true, 100); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4166 | EXPECT_TRUE_WAIT(ch.ports().size() == 2, 5000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4167 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4168 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4169 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4170 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4171 | EXPECT_TRUE_WAIT(ch.connections().size() == 2, 5000); |
| 4172 | |
| 4173 | // Initially, only have Relay/Relay and Local/Relay. Ping Relay/Relay first. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4174 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4175 | |
| 4176 | // Next, ping Local/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4177 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4178 | |
| 4179 | // Remote Local candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4180 | ch.AddRemoteCandidate(CreateUdpCandidate(LOCAL_PORT_TYPE, "2.2.2.2", 2, 2)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4181 | EXPECT_TRUE_WAIT(ch.connections().size() == 4, 5000); |
| 4182 | |
| 4183 | // Local/Local should be the first since it hasn't been pinged before. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4184 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4185 | |
| 4186 | // Relay/Local is the final one. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4187 | VerifyNextPingableConnection(RELAY_PORT_TYPE, LOCAL_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4188 | |
| 4189 | // Now, every connection has been pinged once. The next one should be |
| 4190 | // Relay/Relay. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4191 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | // Test the ping sequence is UDP Relay/Relay followed by TCP Relay/Relay, |
| 4195 | // followed by the rest. |
| 4196 | TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) { |
| 4197 | // Add a Tcp Turn server. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4198 | turn_server()->AddInternalSocket(kTurnTcpIntAddr, PROTO_TCP); |
| 4199 | RelayServerConfig config(RELAY_TURN); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4200 | config.credentials = kRelayCredentials; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4201 | config.ports.push_back(ProtocolAddress(kTurnTcpIntAddr, PROTO_TCP, false)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4202 | allocator()->AddTurnServer(config); |
| 4203 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4204 | P2PTransportChannel& ch = StartTransportChannel(true, 100); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4205 | EXPECT_TRUE_WAIT(ch.ports().size() == 3, 5000); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4206 | EXPECT_EQ(ch.ports()[0]->Type(), LOCAL_PORT_TYPE); |
| 4207 | EXPECT_EQ(ch.ports()[1]->Type(), RELAY_PORT_TYPE); |
| 4208 | EXPECT_EQ(ch.ports()[2]->Type(), RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4209 | |
| 4210 | // Remote Relay candidate arrives. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4211 | ch.AddRemoteCandidate(CreateUdpCandidate(RELAY_PORT_TYPE, "1.1.1.1", 1, 1)); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4212 | EXPECT_TRUE_WAIT(ch.connections().size() == 3, 5000); |
| 4213 | |
| 4214 | // UDP Relay/Relay should be pinged first. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4215 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4216 | |
| 4217 | // TCP Relay/Relay is the next. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4218 | VerifyNextPingableConnection(RELAY_PORT_TYPE, RELAY_PORT_TYPE, |
| 4219 | TCP_PROTOCOL_NAME); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4220 | |
| 4221 | // Finally, Local/Relay will be pinged. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4222 | VerifyNextPingableConnection(LOCAL_PORT_TYPE, RELAY_PORT_TYPE); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 4223 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 4224 | |
| 4225 | } // namespace cricket { |