henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "p2p/base/port.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <cstdint> |
Jonas Oreland | ef60c2b | 2020-03-26 15:10:39 +0100 | [diff] [blame] | 16 | #include <limits> |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 17 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 18 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 22 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "absl/types/optional.h" |
| 24 | #include "api/candidate.h" |
Patrik Höglund | 7d00342 | 2019-09-17 12:16:35 +0200 | [diff] [blame] | 25 | #include "api/packet_socket_factory.h" |
Patrik Höglund | 56d9452 | 2019-11-18 15:53:32 +0100 | [diff] [blame] | 26 | #include "api/transport/stun.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 27 | #include "api/units/time_delta.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "p2p/base/basic_packet_socket_factory.h" |
| 29 | #include "p2p/base/p2p_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "p2p/base/port_allocator.h" |
| 31 | #include "p2p/base/port_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "p2p/base/stun_port.h" |
| 33 | #include "p2p/base/stun_server.h" |
| 34 | #include "p2p/base/tcp_port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "p2p/base/test_stun_server.h" |
| 36 | #include "p2p/base/test_turn_server.h" |
| 37 | #include "p2p/base/transport_description.h" |
| 38 | #include "p2p/base/turn_port.h" |
| 39 | #include "p2p/base/turn_server.h" |
| 40 | #include "p2p/client/relay_port_factory_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 42 | #include "rtc_base/async_packet_socket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 44 | #include "rtc_base/byte_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/dscp.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 47 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 48 | #include "rtc_base/gunit.h" |
| 49 | #include "rtc_base/helpers.h" |
| 50 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 51 | #include "rtc_base/nat_server.h" |
| 52 | #include "rtc_base/nat_socket_factory.h" |
| 53 | #include "rtc_base/nat_types.h" |
| 54 | #include "rtc_base/net_helper.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 55 | #include "rtc_base/network.h" |
| 56 | #include "rtc_base/network/sent_packet.h" |
| 57 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 58 | #include "rtc_base/proxy_info.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 59 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 60 | #include "rtc_base/socket_adapters.h" |
| 61 | #include "rtc_base/socket_address.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 62 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 63 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 64 | #include "rtc_base/time_utils.h" |
| 65 | #include "rtc_base/virtual_socket_server.h" |
Jonas Oreland | 21433ca | 2020-05-13 14:11:25 +0200 | [diff] [blame] | 66 | #include "test/field_trial.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 67 | #include "test/gtest.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 68 | |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 69 | using rtc::AsyncListenSocket; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 70 | using rtc::AsyncPacketSocket; |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 71 | using rtc::ByteBufferReader; |
| 72 | using rtc::ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | using rtc::NAT_ADDR_RESTRICTED; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 74 | using rtc::NAT_OPEN_CONE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 75 | using rtc::NAT_PORT_RESTRICTED; |
| 76 | using rtc::NAT_SYMMETRIC; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 77 | using rtc::NATType; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 78 | using rtc::PacketSocketFactory; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | using rtc::Socket; |
| 80 | using rtc::SocketAddress; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 81 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 82 | namespace cricket { |
| 83 | namespace { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 84 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 85 | constexpr int kDefaultTimeout = 3000; |
| 86 | constexpr int kShortTimeout = 1000; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 87 | constexpr int kMaxExpectedSimulatedRtt = 200; |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 88 | const SocketAddress kLocalAddr1("192.168.1.2", 0); |
| 89 | const SocketAddress kLocalAddr2("192.168.1.3", 0); |
| 90 | const SocketAddress kNatAddr1("77.77.77.77", rtc::NAT_SERVER_UDP_PORT); |
| 91 | const SocketAddress kNatAddr2("88.88.88.88", rtc::NAT_SERVER_UDP_PORT); |
| 92 | const SocketAddress kStunAddr("99.99.99.1", STUN_SERVER_PORT); |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 93 | const SocketAddress kTurnUdpIntAddr("99.99.99.4", STUN_SERVER_PORT); |
| 94 | const SocketAddress kTurnTcpIntAddr("99.99.99.4", 5010); |
| 95 | const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); |
| 96 | const RelayCredentials kRelayCredentials("test", "test"); |
| 97 | |
| 98 | // TODO(?): Update these when RFC5245 is completely supported. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | // Magic value of 30 is from RFC3484, for IPv4 addresses. |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 100 | const uint32_t kDefaultPrflxPriority = ICE_TYPE_PREFERENCE_PRFLX << 24 | |
| 101 | 30 << 8 | |
| 102 | (256 - ICE_CANDIDATE_COMPONENT_DEFAULT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 103 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 104 | constexpr int kTiebreaker1 = 11111; |
| 105 | constexpr int kTiebreaker2 = 22222; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 106 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 107 | const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 108 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 109 | Candidate GetCandidate(Port* port) { |
| 110 | RTC_DCHECK_GE(port->Candidates().size(), 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | return port->Candidates()[0]; |
| 112 | } |
| 113 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 114 | SocketAddress GetAddress(Port* port) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 115 | return GetCandidate(port).address(); |
| 116 | } |
| 117 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 118 | std::unique_ptr<IceMessage> CopyStunMessage(const IceMessage& src) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 119 | auto dst = std::make_unique<IceMessage>(); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 120 | ByteBufferWriter buf; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 121 | src.Write(&buf); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 122 | ByteBufferReader read_buf(buf); |
| 123 | dst->Read(&read_buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | return dst; |
| 125 | } |
| 126 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 127 | bool WriteStunMessage(const StunMessage& msg, ByteBufferWriter* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 128 | buf->Resize(0); // clear out any existing buffer contents |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 129 | return msg.Write(buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 132 | } // namespace |
| 133 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | // Stub port class for testing STUN generation and processing. |
| 135 | class TestPort : public Port { |
| 136 | public: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 137 | TestPort(rtc::Thread* thread, |
| 138 | const std::string& type, |
| 139 | rtc::PacketSocketFactory* factory, |
| 140 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 141 | uint16_t min_port, |
| 142 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 143 | const std::string& username_fragment, |
| 144 | const std::string& password) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 145 | : Port(thread, |
| 146 | type, |
| 147 | factory, |
| 148 | network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 149 | min_port, |
| 150 | max_port, |
| 151 | username_fragment, |
| 152 | password) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 153 | ~TestPort() {} |
| 154 | |
| 155 | // Expose GetStunMessage so that we can test it. |
| 156 | using cricket::Port::GetStunMessage; |
| 157 | |
| 158 | // The last StunMessage that was sent on this Port. |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 159 | // TODO(?): Make these const; requires changes to SendXXXXResponse. |
Karl Wiberg | 4b6c2ec | 2018-10-03 15:17:14 +0200 | [diff] [blame] | 160 | rtc::BufferT<uint8_t>* last_stun_buf() { return last_stun_buf_.get(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 161 | IceMessage* last_stun_msg() { return last_stun_msg_.get(); } |
| 162 | int last_stun_error_code() { |
| 163 | int code = 0; |
| 164 | if (last_stun_msg_) { |
| 165 | const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode(); |
| 166 | if (error_attr) { |
| 167 | code = error_attr->code(); |
| 168 | } |
| 169 | } |
| 170 | return code; |
| 171 | } |
| 172 | |
| 173 | virtual void PrepareAddress() { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 174 | // Act as if the socket was bound to the best IP on the network, to the |
| 175 | // first port in the allowed range. |
| 176 | rtc::SocketAddress addr(Network()->GetBestIP(), min_port()); |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 177 | AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 178 | ICE_TYPE_PREFERENCE_HOST, 0, "", true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 181 | virtual bool SupportsProtocol(const std::string& protocol) const { |
| 182 | return true; |
| 183 | } |
| 184 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 185 | virtual ProtocolType GetProtocol() const { return PROTO_UDP; } |
| 186 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 187 | // Exposed for testing candidate building. |
| 188 | void AddCandidateAddress(const rtc::SocketAddress& addr) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 189 | AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 190 | type_preference_, 0, "", false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 191 | } |
| 192 | void AddCandidateAddress(const rtc::SocketAddress& addr, |
| 193 | const rtc::SocketAddress& base_address, |
| 194 | const std::string& type, |
| 195 | int type_preference, |
| 196 | bool final) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 197 | AddAddress(addr, base_address, rtc::SocketAddress(), "udp", "", "", type, |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 198 | type_preference, 0, "", final); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | virtual Connection* CreateConnection(const Candidate& remote_candidate, |
| 202 | CandidateOrigin origin) { |
| 203 | Connection* conn = new ProxyConnection(this, 0, remote_candidate); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 204 | AddOrReplaceConnection(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 205 | // Set use-candidate attribute flag as this will add USE-CANDIDATE attribute |
| 206 | // in STUN binding requests. |
| 207 | conn->set_use_candidate_attr(true); |
| 208 | return conn; |
| 209 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 210 | virtual int SendTo(const void* data, |
| 211 | size_t size, |
| 212 | const rtc::SocketAddress& addr, |
| 213 | const rtc::PacketOptions& options, |
| 214 | bool payload) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 215 | if (!payload) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 216 | auto msg = std::make_unique<IceMessage>(); |
| 217 | auto buf = std::make_unique<rtc::BufferT<uint8_t>>( |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 218 | static_cast<const char*>(data), size); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 219 | ByteBufferReader read_buf(*buf); |
| 220 | if (!msg->Read(&read_buf)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 221 | return -1; |
| 222 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 223 | last_stun_buf_ = std::move(buf); |
| 224 | last_stun_msg_ = std::move(msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 225 | } |
| 226 | return static_cast<int>(size); |
| 227 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 228 | virtual int SetOption(rtc::Socket::Option opt, int value) { return 0; } |
| 229 | virtual int GetOption(rtc::Socket::Option opt, int* value) { return -1; } |
| 230 | virtual int GetError() { return 0; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | void Reset() { |
| 232 | last_stun_buf_.reset(); |
| 233 | last_stun_msg_.reset(); |
| 234 | } |
| 235 | void set_type_preference(int type_preference) { |
| 236 | type_preference_ = type_preference; |
| 237 | } |
| 238 | |
| 239 | private: |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 240 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 241 | const rtc::SentPacket& sent_packet) { |
| 242 | PortInterface::SignalSentPacket(sent_packet); |
| 243 | } |
Karl Wiberg | 4b6c2ec | 2018-10-03 15:17:14 +0200 | [diff] [blame] | 244 | std::unique_ptr<rtc::BufferT<uint8_t>> last_stun_buf_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 245 | std::unique_ptr<IceMessage> last_stun_msg_; |
pbos | 7640ffa | 2015-11-30 09:16:59 -0800 | [diff] [blame] | 246 | int type_preference_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 249 | static void SendPingAndReceiveResponse(Connection* lconn, |
| 250 | TestPort* lport, |
| 251 | Connection* rconn, |
| 252 | TestPort* rport, |
| 253 | rtc::ScopedFakeClock* clock, |
| 254 | int64_t ms) { |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 255 | lconn->Ping(rtc::TimeMillis()); |
| 256 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 257 | ASSERT_TRUE(lport->last_stun_buf()); |
| 258 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 259 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 260 | clock->AdvanceTime(webrtc::TimeDelta::Millis(ms)); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 261 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 262 | ASSERT_TRUE(rport->last_stun_buf()); |
| 263 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 264 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 265 | } |
| 266 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 267 | class TestChannel : public sigslot::has_slots<> { |
| 268 | public: |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 269 | // Takes ownership of `p1` (but not `p2`). |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 270 | explicit TestChannel(std::unique_ptr<Port> p1) : port_(std::move(p1)) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 271 | port_->SignalPortComplete.connect(this, &TestChannel::OnPortComplete); |
| 272 | port_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress); |
Lahiru Ginnaliya Gamathige | 3ba7beb | 2021-02-01 02:06:11 -0800 | [diff] [blame] | 273 | port_->SubscribePortDestroyed( |
| 274 | [this](PortInterface* port) { OnSrcPortDestroyed(port); }); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | int complete_count() { return complete_count_; } |
| 278 | Connection* conn() { return conn_; } |
| 279 | const SocketAddress& remote_address() { return remote_address_; } |
| 280 | const std::string remote_fragment() { return remote_frag_; } |
| 281 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 282 | void Start() { port_->PrepareAddress(); } |
| 283 | void CreateConnection(const Candidate& remote_candidate) { |
| 284 | conn_ = port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 285 | IceMode remote_ice_mode = |
| 286 | (ice_mode_ == ICEMODE_FULL) ? ICEMODE_LITE : ICEMODE_FULL; |
| 287 | conn_->set_remote_ice_mode(remote_ice_mode); |
| 288 | conn_->set_use_candidate_attr(remote_ice_mode == ICEMODE_FULL); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 289 | conn_->SignalStateChange.connect(this, |
| 290 | &TestChannel::OnConnectionStateChange); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 291 | conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 292 | conn_->SignalReadyToSend.connect(this, |
| 293 | &TestChannel::OnConnectionReadyToSend); |
| 294 | connection_ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 295 | } |
| 296 | void OnConnectionStateChange(Connection* conn) { |
| 297 | if (conn->write_state() == Connection::STATE_WRITABLE) { |
| 298 | conn->set_use_candidate_attr(true); |
| 299 | nominated_ = true; |
| 300 | } |
| 301 | } |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 302 | void AcceptConnection(const Candidate& remote_candidate) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 303 | ASSERT_TRUE(remote_request_.get() != NULL); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 304 | Candidate c = remote_candidate; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 305 | c.set_address(remote_address_); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 306 | conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 307 | conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 308 | conn_->SendStunBindingResponse(remote_request_.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 309 | remote_request_.reset(); |
| 310 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 311 | void Ping() { Ping(0); } |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 312 | void Ping(int64_t now) { conn_->Ping(now); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 313 | void Stop() { |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 314 | if (conn_) { |
| 315 | conn_->Destroy(); |
| 316 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 319 | void OnPortComplete(Port* port) { complete_count_++; } |
| 320 | void SetIceMode(IceMode ice_mode) { ice_mode_ = ice_mode; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 321 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 322 | int SendData(const char* data, size_t len) { |
| 323 | rtc::PacketOptions options; |
| 324 | return conn_->Send(data, len, options); |
| 325 | } |
| 326 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 327 | void OnUnknownAddress(PortInterface* port, |
| 328 | const SocketAddress& addr, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 329 | ProtocolType proto, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 330 | IceMessage* msg, |
| 331 | const std::string& rf, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | bool /*port_muxed*/) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 333 | ASSERT_EQ(port_.get(), port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 334 | if (!remote_address_.IsNil()) { |
| 335 | ASSERT_EQ(remote_address_, addr); |
| 336 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 337 | const cricket::StunUInt32Attribute* priority_attr = |
| 338 | msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 339 | const cricket::StunByteStringAttribute* mi_attr = |
| 340 | msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 341 | const cricket::StunUInt32Attribute* fingerprint_attr = |
| 342 | msg->GetUInt32(STUN_ATTR_FINGERPRINT); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 343 | EXPECT_TRUE(priority_attr != NULL); |
| 344 | EXPECT_TRUE(mi_attr != NULL); |
| 345 | EXPECT_TRUE(fingerprint_attr != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 346 | remote_address_ = addr; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 347 | remote_request_ = CopyStunMessage(*msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 348 | remote_frag_ = rf; |
| 349 | } |
| 350 | |
| 351 | void OnDestroyed(Connection* conn) { |
| 352 | ASSERT_EQ(conn_, conn); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 353 | RTC_LOG(INFO) << "OnDestroy connection " << conn << " deleted"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 354 | conn_ = NULL; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 355 | // When the connection is destroyed, also clear these fields so future |
| 356 | // connections are possible. |
| 357 | remote_request_.reset(); |
| 358 | remote_address_.Clear(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void OnSrcPortDestroyed(PortInterface* port) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 362 | Port* destroyed_src = port_.release(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 363 | ASSERT_EQ(destroyed_src, port); |
| 364 | } |
| 365 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 366 | Port* port() { return port_.get(); } |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 367 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 368 | bool nominated() const { return nominated_; } |
| 369 | |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 370 | void set_connection_ready_to_send(bool ready) { |
| 371 | connection_ready_to_send_ = ready; |
| 372 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 373 | bool connection_ready_to_send() const { return connection_ready_to_send_; } |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 374 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 375 | private: |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 376 | // ReadyToSend will only issue after a Connection recovers from ENOTCONN |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 377 | void OnConnectionReadyToSend(Connection* conn) { |
| 378 | ASSERT_EQ(conn, conn_); |
| 379 | connection_ready_to_send_ = true; |
| 380 | } |
| 381 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 382 | IceMode ice_mode_ = ICEMODE_FULL; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 383 | std::unique_ptr<Port> port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 385 | int complete_count_ = 0; |
| 386 | Connection* conn_ = nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 387 | SocketAddress remote_address_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 388 | std::unique_ptr<StunMessage> remote_request_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 389 | std::string remote_frag_; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 390 | bool nominated_ = false; |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 391 | bool connection_ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 394 | class PortTest : public ::testing::Test, public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 395 | public: |
| 396 | PortTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 397 | : ss_(new rtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 398 | main_(ss_.get()), |
Niels Möller | 9def994 | 2021-09-07 09:16:49 +0200 | [diff] [blame] | 399 | socket_factory_(ss_.get()), |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 400 | nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()), |
| 401 | nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 402 | nat_socket_factory1_(&nat_factory1_), |
| 403 | nat_socket_factory2_(&nat_factory2_), |
Niels Möller | 091617d | 2020-12-02 15:32:08 +0100 | [diff] [blame] | 404 | stun_server_(TestStunServer::Create(ss_.get(), kStunAddr)), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 405 | turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 406 | username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
| 407 | password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 408 | role_conflict_(false), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 409 | ports_destroyed_(0) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 410 | |
| 411 | protected: |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 412 | std::string password() { return password_; } |
| 413 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 414 | void TestLocalToLocal() { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 415 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 416 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 417 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 418 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 419 | TestConnectivity("udp", std::move(port1), "udp", std::move(port2), true, |
| 420 | true, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 421 | } |
| 422 | void TestLocalToStun(NATType ntype) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 423 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 424 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 425 | nat_server2_ = CreateNatServer(kNatAddr2, ntype); |
| 426 | auto port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 427 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 428 | TestConnectivity("udp", std::move(port1), StunName(ntype), std::move(port2), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 429 | ntype == NAT_OPEN_CONE, true, ntype != NAT_SYMMETRIC, |
| 430 | true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 431 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 432 | void TestLocalToRelay(ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 433 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 434 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 435 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 436 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 437 | TestConnectivity("udp", std::move(port1), RelayName(proto), |
| 438 | std::move(port2), false, true, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 439 | } |
| 440 | void TestStunToLocal(NATType ntype) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 441 | nat_server1_ = CreateNatServer(kNatAddr1, ntype); |
| 442 | auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 443 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 444 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 445 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 446 | TestConnectivity(StunName(ntype), std::move(port1), "udp", std::move(port2), |
| 447 | true, ntype != NAT_SYMMETRIC, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 448 | } |
| 449 | void TestStunToStun(NATType ntype1, NATType ntype2) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 450 | nat_server1_ = CreateNatServer(kNatAddr1, ntype1); |
| 451 | auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 452 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 453 | nat_server2_ = CreateNatServer(kNatAddr2, ntype2); |
| 454 | auto port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 455 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 456 | TestConnectivity(StunName(ntype1), std::move(port1), StunName(ntype2), |
| 457 | std::move(port2), ntype2 == NAT_OPEN_CONE, |
| 458 | ntype1 != NAT_SYMMETRIC, ntype2 != NAT_SYMMETRIC, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 459 | ntype1 + ntype2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC)); |
| 460 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 461 | void TestStunToRelay(NATType ntype, ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 462 | nat_server1_ = CreateNatServer(kNatAddr1, ntype); |
| 463 | auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 464 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 465 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 466 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 467 | TestConnectivity(StunName(ntype), std::move(port1), RelayName(proto), |
| 468 | std::move(port2), false, ntype != NAT_SYMMETRIC, true, |
| 469 | true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 470 | } |
| 471 | void TestTcpToTcp() { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 472 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 473 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 474 | auto port2 = CreateTcpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 475 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 476 | TestConnectivity("tcp", std::move(port1), "tcp", std::move(port2), true, |
| 477 | false, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 478 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 479 | void TestTcpToRelay(ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 480 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 481 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 482 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_TCP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 483 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 484 | TestConnectivity("tcp", std::move(port1), RelayName(proto), |
| 485 | std::move(port2), false, false, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 486 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 487 | void TestSslTcpToRelay(ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 488 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 489 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 490 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_SSLTCP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 491 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 492 | TestConnectivity("ssltcp", std::move(port1), RelayName(proto), |
| 493 | std::move(port2), false, false, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 494 | } |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 495 | |
| 496 | rtc::Network* MakeNetwork(const SocketAddress& addr) { |
| 497 | networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32); |
| 498 | networks_.back().AddIP(addr.ipaddr()); |
| 499 | return &networks_.back(); |
| 500 | } |
| 501 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 502 | // helpers for above functions |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 503 | std::unique_ptr<UDPPort> CreateUdpPort(const SocketAddress& addr) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 504 | return CreateUdpPort(addr, &socket_factory_); |
| 505 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 506 | std::unique_ptr<UDPPort> CreateUdpPort(const SocketAddress& addr, |
| 507 | PacketSocketFactory* socket_factory) { |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 508 | return UDPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
Philipp Hancke | 11a89c9 | 2021-10-15 09:58:32 +0200 | [diff] [blame^] | 509 | username_, password_, true, absl::nullopt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 510 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 511 | std::unique_ptr<TCPPort> CreateTcpPort(const SocketAddress& addr) { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 512 | return CreateTcpPort(addr, &socket_factory_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 513 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 514 | std::unique_ptr<TCPPort> CreateTcpPort(const SocketAddress& addr, |
| 515 | PacketSocketFactory* socket_factory) { |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 516 | return TCPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
| 517 | username_, password_, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 518 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 519 | std::unique_ptr<StunPort> CreateStunPort(const SocketAddress& addr, |
| 520 | rtc::PacketSocketFactory* factory) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 521 | ServerAddresses stun_servers; |
| 522 | stun_servers.insert(kStunAddr); |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 523 | return StunPort::Create(&main_, factory, MakeNetwork(addr), 0, 0, username_, |
Philipp Hancke | 11a89c9 | 2021-10-15 09:58:32 +0200 | [diff] [blame^] | 524 | password_, stun_servers, absl::nullopt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 525 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 526 | std::unique_ptr<Port> CreateRelayPort(const SocketAddress& addr, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 527 | ProtocolType int_proto, |
| 528 | ProtocolType ext_proto) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 529 | return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 530 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 531 | std::unique_ptr<TurnPort> CreateTurnPort(const SocketAddress& addr, |
| 532 | PacketSocketFactory* socket_factory, |
| 533 | ProtocolType int_proto, |
| 534 | ProtocolType ext_proto) { |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 535 | SocketAddress server_addr = |
| 536 | int_proto == PROTO_TCP ? kTurnTcpIntAddr : kTurnUdpIntAddr; |
| 537 | return CreateTurnPort(addr, socket_factory, int_proto, ext_proto, |
| 538 | server_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 539 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 540 | std::unique_ptr<TurnPort> CreateTurnPort( |
| 541 | const SocketAddress& addr, |
| 542 | PacketSocketFactory* socket_factory, |
| 543 | ProtocolType int_proto, |
| 544 | ProtocolType ext_proto, |
| 545 | const rtc::SocketAddress& server_addr) { |
Steve Anton | f7dd9df | 2018-10-10 15:06:28 -0700 | [diff] [blame] | 546 | return TurnPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
| 547 | username_, password_, |
| 548 | ProtocolAddress(server_addr, int_proto), |
Philipp Hancke | 11a89c9 | 2021-10-15 09:58:32 +0200 | [diff] [blame^] | 549 | kRelayCredentials, 0, {}, {}, nullptr, nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 550 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 551 | std::unique_ptr<rtc::NATServer> CreateNatServer(const SocketAddress& addr, |
| 552 | rtc::NATType type) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 553 | return std::make_unique<rtc::NATServer>(type, ss_.get(), addr, addr, |
| 554 | ss_.get(), addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 555 | } |
| 556 | static const char* StunName(NATType type) { |
| 557 | switch (type) { |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 558 | case NAT_OPEN_CONE: |
| 559 | return "stun(open cone)"; |
| 560 | case NAT_ADDR_RESTRICTED: |
| 561 | return "stun(addr restricted)"; |
| 562 | case NAT_PORT_RESTRICTED: |
| 563 | return "stun(port restricted)"; |
| 564 | case NAT_SYMMETRIC: |
| 565 | return "stun(symmetric)"; |
| 566 | default: |
| 567 | return "stun(?)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 570 | static const char* RelayName(ProtocolType proto) { |
| 571 | switch (proto) { |
| 572 | case PROTO_UDP: |
| 573 | return "turn(udp)"; |
| 574 | case PROTO_TCP: |
| 575 | return "turn(tcp)"; |
| 576 | case PROTO_SSLTCP: |
| 577 | return "turn(ssltcp)"; |
| 578 | case PROTO_TLS: |
| 579 | return "turn(tls)"; |
| 580 | default: |
| 581 | return "turn(?)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
| 585 | void TestCrossFamilyPorts(int type); |
| 586 | |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 587 | void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2); |
| 588 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 589 | // This does all the work and then deletes `port1` and `port2`. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 590 | void TestConnectivity(const char* name1, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 591 | std::unique_ptr<Port> port1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 592 | const char* name2, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 593 | std::unique_ptr<Port> port2, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 594 | bool accept, |
| 595 | bool same_addr1, |
| 596 | bool same_addr2, |
| 597 | bool possible); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 598 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 599 | // This connects the provided channels which have already started. `ch1` |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 600 | // should have its Connection created (either through CreateConnection() or |
| 601 | // TCP reconnecting mechanism before entering this function. |
| 602 | void ConnectStartedChannels(TestChannel* ch1, TestChannel* ch2) { |
| 603 | ASSERT_TRUE(ch1->conn()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 604 | EXPECT_TRUE_WAIT(ch1->conn()->connected(), |
| 605 | kDefaultTimeout); // for TCP connect |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 606 | ch1->Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 607 | WAIT(!ch2->remote_address().IsNil(), kShortTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 608 | |
| 609 | // Send a ping from dst to src. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 610 | ch2->AcceptConnection(GetCandidate(ch1->port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 611 | ch2->Ping(); |
| 612 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2->conn()->write_state(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 613 | kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 614 | } |
| 615 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 616 | // This connects and disconnects the provided channels in the same sequence as |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 617 | // TestConnectivity with all options set to `true`. It does not delete either |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 618 | // channel. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 619 | void StartConnectAndStopChannels(TestChannel* ch1, TestChannel* ch2) { |
| 620 | // Acquire addresses. |
| 621 | ch1->Start(); |
| 622 | ch2->Start(); |
| 623 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 624 | ch1->CreateConnection(GetCandidate(ch2->port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 625 | ConnectStartedChannels(ch1, ch2); |
| 626 | |
| 627 | // Destroy the connections. |
| 628 | ch1->Stop(); |
| 629 | ch2->Stop(); |
| 630 | } |
| 631 | |
| 632 | // This disconnects both end's Connection and make sure ch2 ready for new |
| 633 | // connection. |
| 634 | void DisconnectTcpTestChannels(TestChannel* ch1, TestChannel* ch2) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 635 | TCPConnection* tcp_conn1 = static_cast<TCPConnection*>(ch1->conn()); |
| 636 | TCPConnection* tcp_conn2 = static_cast<TCPConnection*>(ch2->conn()); |
| 637 | ASSERT_TRUE( |
| 638 | ss_->CloseTcpConnections(tcp_conn1->socket()->GetLocalAddress(), |
| 639 | tcp_conn2->socket()->GetLocalAddress())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 640 | |
| 641 | // Wait for both OnClose are delivered. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 642 | EXPECT_TRUE_WAIT(!ch1->conn()->connected(), kDefaultTimeout); |
| 643 | EXPECT_TRUE_WAIT(!ch2->conn()->connected(), kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 644 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 645 | // Ensure redundant SignalClose events on TcpConnection won't break tcp |
| 646 | // reconnection. Chromium will fire SignalClose for all outstanding IPC |
| 647 | // packets during reconnection. |
| 648 | tcp_conn1->socket()->SignalClose(tcp_conn1->socket(), 0); |
| 649 | tcp_conn2->socket()->SignalClose(tcp_conn2->socket(), 0); |
| 650 | |
| 651 | // Speed up destroying ch2's connection such that the test is ready to |
| 652 | // accept a new connection from ch1 before ch1's connection destroys itself. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 653 | ch2->conn()->Destroy(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 654 | EXPECT_TRUE_WAIT(ch2->conn() == NULL, kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void TestTcpReconnect(bool ping_after_disconnected, |
| 658 | bool send_after_disconnected) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 659 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 660 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 661 | auto port2 = CreateTcpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 662 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 663 | |
| 664 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 665 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 666 | |
| 667 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 668 | TestChannel ch1(std::move(port1)); |
| 669 | TestChannel ch2(std::move(port2)); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 670 | EXPECT_EQ(0, ch1.complete_count()); |
| 671 | EXPECT_EQ(0, ch2.complete_count()); |
| 672 | |
| 673 | ch1.Start(); |
| 674 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 675 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 676 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 677 | |
| 678 | // Initial connecting the channel, create connection on channel1. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 679 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 680 | ConnectStartedChannels(&ch1, &ch2); |
| 681 | |
| 682 | // Shorten the timeout period. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 683 | const int kTcpReconnectTimeout = kDefaultTimeout; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 684 | static_cast<TCPConnection*>(ch1.conn()) |
| 685 | ->set_reconnection_timeout(kTcpReconnectTimeout); |
| 686 | static_cast<TCPConnection*>(ch2.conn()) |
| 687 | ->set_reconnection_timeout(kTcpReconnectTimeout); |
| 688 | |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 689 | EXPECT_FALSE(ch1.connection_ready_to_send()); |
| 690 | EXPECT_FALSE(ch2.connection_ready_to_send()); |
| 691 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 692 | // Once connected, disconnect them. |
| 693 | DisconnectTcpTestChannels(&ch1, &ch2); |
| 694 | |
| 695 | if (send_after_disconnected || ping_after_disconnected) { |
| 696 | if (send_after_disconnected) { |
| 697 | // First SendData after disconnect should fail but will trigger |
| 698 | // reconnect. |
| 699 | EXPECT_EQ(-1, ch1.SendData(data, static_cast<int>(strlen(data)))); |
| 700 | } |
| 701 | |
| 702 | if (ping_after_disconnected) { |
| 703 | // Ping should trigger reconnect. |
| 704 | ch1.Ping(); |
| 705 | } |
| 706 | |
| 707 | // Wait for channel's outgoing TCPConnection connected. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 708 | EXPECT_TRUE_WAIT(ch1.conn()->connected(), kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 709 | |
| 710 | // Verify that we could still connect channels. |
| 711 | ConnectStartedChannels(&ch1, &ch2); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 712 | EXPECT_TRUE_WAIT(ch1.connection_ready_to_send(), kTcpReconnectTimeout); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 713 | // Channel2 is the passive one so a new connection is created during |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 714 | // reconnect. This new connection should never have issued ENOTCONN |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 715 | // hence the connection_ready_to_send() should be false. |
| 716 | EXPECT_FALSE(ch2.connection_ready_to_send()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 717 | } else { |
| 718 | EXPECT_EQ(ch1.conn()->write_state(), Connection::STATE_WRITABLE); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 719 | // Since the reconnection never happens, the connections should have been |
| 720 | // destroyed after the timeout. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 721 | EXPECT_TRUE_WAIT(!ch1.conn(), kTcpReconnectTimeout + kDefaultTimeout); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 722 | EXPECT_TRUE(!ch2.conn()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Tear down and ensure that goes smoothly. |
| 726 | ch1.Stop(); |
| 727 | ch2.Stop(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 728 | EXPECT_TRUE_WAIT(ch1.conn() == NULL, kDefaultTimeout); |
| 729 | EXPECT_TRUE_WAIT(ch2.conn() == NULL, kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 730 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 731 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 732 | std::unique_ptr<IceMessage> CreateStunMessage(int type) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 733 | auto msg = std::make_unique<IceMessage>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 734 | msg->SetType(type); |
| 735 | msg->SetTransactionID("TESTTESTTEST"); |
| 736 | return msg; |
| 737 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 738 | std::unique_ptr<IceMessage> CreateStunMessageWithUsername( |
| 739 | int type, |
| 740 | const std::string& username) { |
| 741 | std::unique_ptr<IceMessage> msg = CreateStunMessage(type); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 742 | msg->AddAttribute(std::make_unique<StunByteStringAttribute>( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 743 | STUN_ATTR_USERNAME, username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 744 | return msg; |
| 745 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 746 | std::unique_ptr<TestPort> CreateTestPort(const rtc::SocketAddress& addr, |
| 747 | const std::string& username, |
| 748 | const std::string& password) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 749 | auto port = |
| 750 | std::make_unique<TestPort>(&main_, "test", &socket_factory_, |
| 751 | MakeNetwork(addr), 0, 0, username, password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 752 | port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
| 753 | return port; |
| 754 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 755 | std::unique_ptr<TestPort> CreateTestPort(const rtc::SocketAddress& addr, |
| 756 | const std::string& username, |
| 757 | const std::string& password, |
| 758 | cricket::IceRole role, |
| 759 | int tiebreaker) { |
| 760 | auto port = CreateTestPort(addr, username, password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 761 | port->SetIceRole(role); |
| 762 | port->SetIceTiebreaker(tiebreaker); |
| 763 | return port; |
| 764 | } |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 765 | // Overload to create a test port given an rtc::Network directly. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 766 | std::unique_ptr<TestPort> CreateTestPort(rtc::Network* network, |
| 767 | const std::string& username, |
| 768 | const std::string& password) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 769 | auto port = std::make_unique<TestPort>(&main_, "test", &socket_factory_, |
| 770 | network, 0, 0, username, password); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 771 | port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
| 772 | return port; |
| 773 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 774 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 775 | void OnRoleConflict(PortInterface* port) { role_conflict_ = true; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 776 | bool role_conflict() const { return role_conflict_; } |
| 777 | |
| 778 | void ConnectToSignalDestroyed(PortInterface* port) { |
Lahiru Ginnaliya Gamathige | c3a486c | 2021-02-09 00:31:39 -0800 | [diff] [blame] | 779 | port->SubscribePortDestroyed( |
| 780 | [this](PortInterface* port) { OnDestroyed(port); }); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 783 | void OnDestroyed(PortInterface* port) { ++ports_destroyed_; } |
| 784 | int ports_destroyed() const { return ports_destroyed_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 785 | |
| 786 | rtc::BasicPacketSocketFactory* nat_socket_factory1() { |
| 787 | return &nat_socket_factory1_; |
| 788 | } |
| 789 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 790 | rtc::VirtualSocketServer* vss() { return ss_.get(); } |
| 791 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 792 | private: |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 793 | // When a "create port" helper method is called with an IP, we create a |
| 794 | // Network with that IP and add it to this list. Using a list instead of a |
| 795 | // vector so that when it grows, pointers aren't invalidated. |
| 796 | std::list<rtc::Network> networks_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 797 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 798 | rtc::AutoSocketServerThread main_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 799 | rtc::BasicPacketSocketFactory socket_factory_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 800 | std::unique_ptr<rtc::NATServer> nat_server1_; |
| 801 | std::unique_ptr<rtc::NATServer> nat_server2_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 802 | rtc::NATSocketFactory nat_factory1_; |
| 803 | rtc::NATSocketFactory nat_factory2_; |
| 804 | rtc::BasicPacketSocketFactory nat_socket_factory1_; |
| 805 | rtc::BasicPacketSocketFactory nat_socket_factory2_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 806 | std::unique_ptr<TestStunServer> stun_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 807 | TestTurnServer turn_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 808 | std::string username_; |
| 809 | std::string password_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 810 | bool role_conflict_; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 811 | int ports_destroyed_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 812 | }; |
| 813 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 814 | void PortTest::TestConnectivity(const char* name1, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 815 | std::unique_ptr<Port> port1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 816 | const char* name2, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 817 | std::unique_ptr<Port> port2, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 818 | bool accept, |
| 819 | bool same_addr1, |
| 820 | bool same_addr2, |
| 821 | bool possible) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 822 | rtc::ScopedFakeClock clock; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 823 | RTC_LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": "; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 824 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 825 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 826 | |
| 827 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 828 | TestChannel ch1(std::move(port1)); |
| 829 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 830 | EXPECT_EQ(0, ch1.complete_count()); |
| 831 | EXPECT_EQ(0, ch2.complete_count()); |
| 832 | |
| 833 | // Acquire addresses. |
| 834 | ch1.Start(); |
| 835 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 836 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 837 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 838 | |
| 839 | // Send a ping from src to dst. This may or may not make it. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 840 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 841 | ASSERT_TRUE(ch1.conn() != NULL); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 842 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, |
| 843 | clock); // for TCP connect |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 844 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 845 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 846 | |
| 847 | if (accept) { |
| 848 | // We are able to send a ping from src to dst. This is the case when |
| 849 | // sending to UDP ports and cone NATs. |
| 850 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 851 | EXPECT_EQ(ch2.remote_fragment(), ch1.port()->username_fragment()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 852 | |
| 853 | // Ensure the ping came from the same address used for src. |
| 854 | // This is the case unless the source NAT was symmetric. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 855 | if (same_addr1) |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 856 | EXPECT_EQ(ch2.remote_address(), GetAddress(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 857 | EXPECT_TRUE(same_addr2); |
| 858 | |
| 859 | // Send a ping from dst to src. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 860 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 861 | ASSERT_TRUE(ch2.conn() != NULL); |
| 862 | ch2.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 863 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 864 | ch2.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 865 | } else { |
| 866 | // We can't send a ping from src to dst, so flip it around. This will happen |
| 867 | // when the destination NAT is addr/port restricted or symmetric. |
| 868 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 869 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 870 | |
| 871 | // Send a ping from dst to src. Again, this may or may not make it. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 872 | ch2.CreateConnection(GetCandidate(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 873 | ASSERT_TRUE(ch2.conn() != NULL); |
| 874 | ch2.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 875 | SIMULATED_WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, |
| 876 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 877 | |
| 878 | if (same_addr1 && same_addr2) { |
| 879 | // The new ping got back to the source. |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 880 | EXPECT_TRUE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 881 | EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 882 | |
| 883 | // First connection may not be writable if the first ping did not get |
| 884 | // through. So we will have to do another. |
| 885 | if (ch1.conn()->write_state() == Connection::STATE_WRITE_INIT) { |
| 886 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 887 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 888 | ch1.conn()->write_state(), kDefaultTimeout, |
| 889 | clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 890 | } |
| 891 | } else if (!same_addr1 && possible) { |
| 892 | // The new ping went to the candidate address, but that address was bad. |
| 893 | // This will happen when the source NAT is symmetric. |
| 894 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 895 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 896 | |
| 897 | // However, since we have now sent a ping to the source IP, we should be |
| 898 | // able to get a ping from it. This gives us the real source address. |
| 899 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 900 | EXPECT_TRUE_SIMULATED_WAIT(!ch2.remote_address().IsNil(), kDefaultTimeout, |
| 901 | clock); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 902 | EXPECT_FALSE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 903 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 904 | |
| 905 | // Pick up the actual address and establish the connection. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 906 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 907 | ASSERT_TRUE(ch2.conn() != NULL); |
| 908 | ch2.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 909 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 910 | ch2.conn()->write_state(), kDefaultTimeout, |
| 911 | clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 912 | } else if (!same_addr2 && possible) { |
| 913 | // The new ping came in, but from an unexpected address. This will happen |
| 914 | // when the destination NAT is symmetric. |
| 915 | EXPECT_FALSE(ch1.remote_address().IsNil()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 916 | EXPECT_FALSE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 917 | |
| 918 | // Update our address and complete the connection. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 919 | ch1.AcceptConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 920 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 921 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 922 | ch1.conn()->write_state(), kDefaultTimeout, |
| 923 | clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 924 | } else { // (!possible) |
| 925 | // There should be s no way for the pings to reach each other. Check it. |
| 926 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 927 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 928 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 929 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 930 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 931 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // Everything should be good, unless we know the situation is impossible. |
| 936 | ASSERT_TRUE(ch1.conn() != NULL); |
| 937 | ASSERT_TRUE(ch2.conn() != NULL); |
| 938 | if (possible) { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 939 | EXPECT_TRUE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 940 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 941 | EXPECT_TRUE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 942 | EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 943 | } else { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 944 | EXPECT_FALSE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 945 | EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 946 | EXPECT_FALSE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 947 | EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 948 | } |
| 949 | |
| 950 | // Tear down and ensure that goes smoothly. |
| 951 | ch1.Stop(); |
| 952 | ch2.Stop(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 953 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn() == NULL, kDefaultTimeout, clock); |
| 954 | EXPECT_TRUE_SIMULATED_WAIT(ch2.conn() == NULL, kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 955 | } |
| 956 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 957 | class FakePacketSocketFactory : public rtc::PacketSocketFactory { |
| 958 | public: |
| 959 | FakePacketSocketFactory() |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 960 | : next_udp_socket_(NULL), next_server_tcp_socket_(NULL) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 961 | ~FakePacketSocketFactory() override {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 962 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 963 | AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 964 | uint16_t min_port, |
| 965 | uint16_t max_port) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 966 | EXPECT_TRUE(next_udp_socket_ != NULL); |
| 967 | AsyncPacketSocket* result = next_udp_socket_; |
| 968 | next_udp_socket_ = NULL; |
| 969 | return result; |
| 970 | } |
| 971 | |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 972 | AsyncListenSocket* CreateServerTcpSocket(const SocketAddress& local_address, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 973 | uint16_t min_port, |
| 974 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 975 | int opts) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 976 | EXPECT_TRUE(next_server_tcp_socket_ != NULL); |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 977 | AsyncListenSocket* result = next_server_tcp_socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 978 | next_server_tcp_socket_ = NULL; |
| 979 | return result; |
| 980 | } |
| 981 | |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame] | 982 | AsyncPacketSocket* CreateClientTcpSocket( |
| 983 | const SocketAddress& local_address, |
| 984 | const SocketAddress& remote_address, |
| 985 | const rtc::ProxyInfo& proxy_info, |
| 986 | const std::string& user_agent, |
| 987 | const rtc::PacketSocketTcpOptions& opts) override { |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 988 | EXPECT_TRUE(next_client_tcp_socket_.has_value()); |
| 989 | AsyncPacketSocket* result = *next_client_tcp_socket_; |
| 990 | next_client_tcp_socket_ = nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 991 | return result; |
| 992 | } |
| 993 | |
| 994 | void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) { |
| 995 | next_udp_socket_ = next_udp_socket; |
| 996 | } |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 997 | void set_next_server_tcp_socket(AsyncListenSocket* next_server_tcp_socket) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 998 | next_server_tcp_socket_ = next_server_tcp_socket; |
| 999 | } |
| 1000 | void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) { |
| 1001 | next_client_tcp_socket_ = next_client_tcp_socket; |
| 1002 | } |
Harald Alvestrand | 6f8fa5a | 2021-10-14 13:57:07 +0000 | [diff] [blame] | 1003 | std::unique_ptr<webrtc::AsyncDnsResolverInterface> CreateAsyncDnsResolver() |
| 1004 | override { |
| 1005 | return nullptr; |
| 1006 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1007 | |
| 1008 | private: |
| 1009 | AsyncPacketSocket* next_udp_socket_; |
Niels Möller | 6d19d14 | 2021-10-06 11:19:03 +0200 | [diff] [blame] | 1010 | AsyncListenSocket* next_server_tcp_socket_; |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1011 | absl::optional<AsyncPacketSocket*> next_client_tcp_socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1012 | }; |
| 1013 | |
| 1014 | class FakeAsyncPacketSocket : public AsyncPacketSocket { |
| 1015 | public: |
| 1016 | // Returns current local address. Address may be set to NULL if the |
| 1017 | // socket is not bound yet (GetState() returns STATE_BINDING). |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1018 | virtual SocketAddress GetLocalAddress() const { return local_address_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Returns remote address. Returns zeroes if this is not a client TCP socket. |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1021 | virtual SocketAddress GetRemoteAddress() const { return remote_address_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Send a packet. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1024 | virtual int Send(const void* pv, |
| 1025 | size_t cb, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1026 | const rtc::PacketOptions& options) { |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1027 | if (error_ == 0) { |
| 1028 | return static_cast<int>(cb); |
| 1029 | } else { |
| 1030 | return -1; |
| 1031 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1032 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1033 | virtual int SendTo(const void* pv, |
| 1034 | size_t cb, |
| 1035 | const SocketAddress& addr, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1036 | const rtc::PacketOptions& options) { |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1037 | if (error_ == 0) { |
| 1038 | return static_cast<int>(cb); |
| 1039 | } else { |
| 1040 | return -1; |
| 1041 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1042 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1043 | virtual int Close() { return 0; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1044 | |
| 1045 | virtual State GetState() const { return state_; } |
| 1046 | virtual int GetOption(Socket::Option opt, int* value) { return 0; } |
| 1047 | virtual int SetOption(Socket::Option opt, int value) { return 0; } |
| 1048 | virtual int GetError() const { return 0; } |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1049 | virtual void SetError(int error) { error_ = error; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1050 | |
| 1051 | void set_state(State state) { state_ = state; } |
| 1052 | |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1053 | SocketAddress local_address_; |
| 1054 | SocketAddress remote_address_; |
| 1055 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1056 | private: |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1057 | int error_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1058 | State state_; |
| 1059 | }; |
| 1060 | |
| 1061 | // Local -> XXXX |
| 1062 | TEST_F(PortTest, TestLocalToLocal) { |
| 1063 | TestLocalToLocal(); |
| 1064 | } |
| 1065 | |
| 1066 | TEST_F(PortTest, TestLocalToConeNat) { |
| 1067 | TestLocalToStun(NAT_OPEN_CONE); |
| 1068 | } |
| 1069 | |
| 1070 | TEST_F(PortTest, TestLocalToARNat) { |
| 1071 | TestLocalToStun(NAT_ADDR_RESTRICTED); |
| 1072 | } |
| 1073 | |
| 1074 | TEST_F(PortTest, TestLocalToPRNat) { |
| 1075 | TestLocalToStun(NAT_PORT_RESTRICTED); |
| 1076 | } |
| 1077 | |
| 1078 | TEST_F(PortTest, TestLocalToSymNat) { |
| 1079 | TestLocalToStun(NAT_SYMMETRIC); |
| 1080 | } |
| 1081 | |
| 1082 | // Flaky: https://code.google.com/p/webrtc/issues/detail?id=3316. |
| 1083 | TEST_F(PortTest, DISABLED_TestLocalToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1084 | TestLocalToRelay(PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | // Cone NAT -> XXXX |
| 1088 | TEST_F(PortTest, TestConeNatToLocal) { |
| 1089 | TestStunToLocal(NAT_OPEN_CONE); |
| 1090 | } |
| 1091 | |
| 1092 | TEST_F(PortTest, TestConeNatToConeNat) { |
| 1093 | TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE); |
| 1094 | } |
| 1095 | |
| 1096 | TEST_F(PortTest, TestConeNatToARNat) { |
| 1097 | TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED); |
| 1098 | } |
| 1099 | |
| 1100 | TEST_F(PortTest, TestConeNatToPRNat) { |
| 1101 | TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED); |
| 1102 | } |
| 1103 | |
| 1104 | TEST_F(PortTest, TestConeNatToSymNat) { |
| 1105 | TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC); |
| 1106 | } |
| 1107 | |
| 1108 | TEST_F(PortTest, TestConeNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1109 | TestStunToRelay(NAT_OPEN_CONE, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | // Address-restricted NAT -> XXXX |
| 1113 | TEST_F(PortTest, TestARNatToLocal) { |
| 1114 | TestStunToLocal(NAT_ADDR_RESTRICTED); |
| 1115 | } |
| 1116 | |
| 1117 | TEST_F(PortTest, TestARNatToConeNat) { |
| 1118 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE); |
| 1119 | } |
| 1120 | |
| 1121 | TEST_F(PortTest, TestARNatToARNat) { |
| 1122 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED); |
| 1123 | } |
| 1124 | |
| 1125 | TEST_F(PortTest, TestARNatToPRNat) { |
| 1126 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED); |
| 1127 | } |
| 1128 | |
| 1129 | TEST_F(PortTest, TestARNatToSymNat) { |
| 1130 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC); |
| 1131 | } |
| 1132 | |
| 1133 | TEST_F(PortTest, TestARNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1134 | TestStunToRelay(NAT_ADDR_RESTRICTED, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | // Port-restricted NAT -> XXXX |
| 1138 | TEST_F(PortTest, TestPRNatToLocal) { |
| 1139 | TestStunToLocal(NAT_PORT_RESTRICTED); |
| 1140 | } |
| 1141 | |
| 1142 | TEST_F(PortTest, TestPRNatToConeNat) { |
| 1143 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE); |
| 1144 | } |
| 1145 | |
| 1146 | TEST_F(PortTest, TestPRNatToARNat) { |
| 1147 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED); |
| 1148 | } |
| 1149 | |
| 1150 | TEST_F(PortTest, TestPRNatToPRNat) { |
| 1151 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED); |
| 1152 | } |
| 1153 | |
| 1154 | TEST_F(PortTest, TestPRNatToSymNat) { |
| 1155 | // Will "fail" |
| 1156 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC); |
| 1157 | } |
| 1158 | |
| 1159 | TEST_F(PortTest, TestPRNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1160 | TestStunToRelay(NAT_PORT_RESTRICTED, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | // Symmetric NAT -> XXXX |
| 1164 | TEST_F(PortTest, TestSymNatToLocal) { |
| 1165 | TestStunToLocal(NAT_SYMMETRIC); |
| 1166 | } |
| 1167 | |
| 1168 | TEST_F(PortTest, TestSymNatToConeNat) { |
| 1169 | TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE); |
| 1170 | } |
| 1171 | |
| 1172 | TEST_F(PortTest, TestSymNatToARNat) { |
| 1173 | TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED); |
| 1174 | } |
| 1175 | |
| 1176 | TEST_F(PortTest, TestSymNatToPRNat) { |
| 1177 | // Will "fail" |
| 1178 | TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED); |
| 1179 | } |
| 1180 | |
| 1181 | TEST_F(PortTest, TestSymNatToSymNat) { |
| 1182 | // Will "fail" |
| 1183 | TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC); |
| 1184 | } |
| 1185 | |
| 1186 | TEST_F(PortTest, TestSymNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1187 | TestStunToRelay(NAT_SYMMETRIC, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | // Outbound TCP -> XXXX |
| 1191 | TEST_F(PortTest, TestTcpToTcp) { |
| 1192 | TestTcpToTcp(); |
| 1193 | } |
| 1194 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 1195 | TEST_F(PortTest, TestTcpReconnectOnSendPacket) { |
| 1196 | TestTcpReconnect(false /* ping */, true /* send */); |
| 1197 | } |
| 1198 | |
| 1199 | TEST_F(PortTest, TestTcpReconnectOnPing) { |
| 1200 | TestTcpReconnect(true /* ping */, false /* send */); |
| 1201 | } |
| 1202 | |
| 1203 | TEST_F(PortTest, TestTcpReconnectTimeout) { |
| 1204 | TestTcpReconnect(false /* ping */, false /* send */); |
| 1205 | } |
| 1206 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1207 | // Test when TcpConnection never connects, the OnClose() will be called to |
| 1208 | // destroy the connection. |
| 1209 | TEST_F(PortTest, TestTcpNeverConnect) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1210 | auto port1 = CreateTcpPort(kLocalAddr1); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1211 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1212 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 1213 | |
| 1214 | // Set up a channel and ensure the port will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1215 | TestChannel ch1(std::move(port1)); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1216 | EXPECT_EQ(0, ch1.complete_count()); |
| 1217 | |
| 1218 | ch1.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1219 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1220 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 1221 | std::unique_ptr<rtc::Socket> server( |
| 1222 | vss()->CreateSocket(kLocalAddr2.family(), SOCK_STREAM)); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1223 | // Bind but not listen. |
| 1224 | EXPECT_EQ(0, server->Bind(kLocalAddr2)); |
| 1225 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1226 | Candidate c = GetCandidate(ch1.port()); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1227 | c.set_address(server->GetLocalAddress()); |
| 1228 | |
| 1229 | ch1.CreateConnection(c); |
| 1230 | EXPECT_TRUE(ch1.conn()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1231 | EXPECT_TRUE_WAIT(!ch1.conn(), kDefaultTimeout); // for TCP connect |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 1234 | /* TODO(?): Enable these once testrelayserver can accept external TCP. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1235 | TEST_F(PortTest, TestTcpToTcpRelay) { |
| 1236 | TestTcpToRelay(PROTO_TCP); |
| 1237 | } |
| 1238 | |
| 1239 | TEST_F(PortTest, TestTcpToSslTcpRelay) { |
| 1240 | TestTcpToRelay(PROTO_SSLTCP); |
| 1241 | } |
| 1242 | */ |
| 1243 | |
| 1244 | // Outbound SSLTCP -> XXXX |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 1245 | /* TODO(?): Enable these once testrelayserver can accept external SSL. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1246 | TEST_F(PortTest, TestSslTcpToTcpRelay) { |
| 1247 | TestSslTcpToRelay(PROTO_TCP); |
| 1248 | } |
| 1249 | |
| 1250 | TEST_F(PortTest, TestSslTcpToSslTcpRelay) { |
| 1251 | TestSslTcpToRelay(PROTO_SSLTCP); |
| 1252 | } |
| 1253 | */ |
| 1254 | |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1255 | // Test that a connection will be dead and deleted if |
| 1256 | // i) it has never received anything for MIN_CONNECTION_LIFETIME milliseconds |
| 1257 | // since it was created, or |
| 1258 | // ii) it has not received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT |
| 1259 | // milliseconds since last receiving. |
| 1260 | TEST_F(PortTest, TestConnectionDead) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1261 | TestChannel ch1(CreateUdpPort(kLocalAddr1)); |
| 1262 | TestChannel ch2(CreateUdpPort(kLocalAddr2)); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1263 | // Acquire address. |
| 1264 | ch1.Start(); |
| 1265 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1266 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 1267 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1268 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1269 | // Test case that the connection has never received anything. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1270 | int64_t before_created = rtc::TimeMillis(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1271 | ch1.CreateConnection(GetCandidate(ch2.port())); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1272 | int64_t after_created = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1273 | Connection* conn = ch1.conn(); |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 1274 | ASSERT_NE(conn, nullptr); |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1275 | // It is not dead if it is after MIN_CONNECTION_LIFETIME but not pruned. |
| 1276 | conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); |
| 1277 | rtc::Thread::Current()->ProcessMessages(0); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1278 | EXPECT_TRUE(ch1.conn() != nullptr); |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1279 | // It is not dead if it is before MIN_CONNECTION_LIFETIME and pruned. |
| 1280 | conn->UpdateState(before_created + MIN_CONNECTION_LIFETIME - 1); |
| 1281 | conn->Prune(); |
| 1282 | rtc::Thread::Current()->ProcessMessages(0); |
| 1283 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1284 | // It will be dead after MIN_CONNECTION_LIFETIME and pruned. |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1285 | conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1286 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1287 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1288 | // Test case that the connection has received something. |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1289 | // Create a connection again and receive a ping. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1290 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1291 | conn = ch1.conn(); |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 1292 | ASSERT_NE(conn, nullptr); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1293 | int64_t before_last_receiving = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1294 | conn->ReceivedPing(); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1295 | int64_t after_last_receiving = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1296 | // The connection will be dead after DEAD_CONNECTION_RECEIVE_TIMEOUT |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1297 | conn->UpdateState(before_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT - |
| 1298 | 1); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1299 | rtc::Thread::Current()->ProcessMessages(100); |
| 1300 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1301 | conn->UpdateState(after_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT + 1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1302 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Jonas Oreland | 21433ca | 2020-05-13 14:11:25 +0200 | [diff] [blame] | 1305 | TEST_F(PortTest, TestConnectionDeadWithDeadConnectionTimeout) { |
| 1306 | TestChannel ch1(CreateUdpPort(kLocalAddr1)); |
| 1307 | TestChannel ch2(CreateUdpPort(kLocalAddr2)); |
| 1308 | // Acquire address. |
| 1309 | ch1.Start(); |
| 1310 | ch2.Start(); |
| 1311 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 1312 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout); |
| 1313 | |
| 1314 | // Note: set field trials manually since they are parsed by |
| 1315 | // P2PTransportChannel but P2PTransportChannel is not used in this test. |
| 1316 | IceFieldTrials field_trials; |
| 1317 | field_trials.dead_connection_timeout_ms = 90000; |
| 1318 | |
| 1319 | // Create a connection again and receive a ping. |
| 1320 | ch1.CreateConnection(GetCandidate(ch2.port())); |
| 1321 | auto conn = ch1.conn(); |
| 1322 | conn->SetIceFieldTrials(&field_trials); |
| 1323 | |
| 1324 | ASSERT_NE(conn, nullptr); |
| 1325 | int64_t before_last_receiving = rtc::TimeMillis(); |
| 1326 | conn->ReceivedPing(); |
| 1327 | int64_t after_last_receiving = rtc::TimeMillis(); |
| 1328 | // The connection will be dead after 90s |
| 1329 | conn->UpdateState(before_last_receiving + 90000 - 1); |
| 1330 | rtc::Thread::Current()->ProcessMessages(100); |
| 1331 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1332 | conn->UpdateState(after_last_receiving + 90000 + 1); |
| 1333 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout); |
| 1334 | } |
| 1335 | |
| 1336 | TEST_F(PortTest, TestConnectionDeadOutstandingPing) { |
| 1337 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 1338 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1339 | port1->SetIceTiebreaker(kTiebreaker1); |
| 1340 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 1341 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1342 | port2->SetIceTiebreaker(kTiebreaker2); |
| 1343 | |
| 1344 | TestChannel ch1(std::move(port1)); |
| 1345 | TestChannel ch2(std::move(port2)); |
| 1346 | // Acquire address. |
| 1347 | ch1.Start(); |
| 1348 | ch2.Start(); |
| 1349 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 1350 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout); |
| 1351 | |
| 1352 | // Note: set field trials manually since they are parsed by |
| 1353 | // P2PTransportChannel but P2PTransportChannel is not used in this test. |
| 1354 | IceFieldTrials field_trials; |
| 1355 | field_trials.dead_connection_timeout_ms = 360000; |
| 1356 | |
| 1357 | // Create a connection again and receive a ping and then send |
| 1358 | // a ping and keep it outstanding. |
| 1359 | ch1.CreateConnection(GetCandidate(ch2.port())); |
| 1360 | auto conn = ch1.conn(); |
| 1361 | conn->SetIceFieldTrials(&field_trials); |
| 1362 | |
| 1363 | ASSERT_NE(conn, nullptr); |
| 1364 | conn->ReceivedPing(); |
| 1365 | int64_t send_ping_timestamp = rtc::TimeMillis(); |
| 1366 | conn->Ping(send_ping_timestamp); |
| 1367 | |
| 1368 | // The connection will be dead 30s after the ping was sent. |
| 1369 | conn->UpdateState(send_ping_timestamp + DEAD_CONNECTION_RECEIVE_TIMEOUT - 1); |
| 1370 | rtc::Thread::Current()->ProcessMessages(100); |
| 1371 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1372 | conn->UpdateState(send_ping_timestamp + DEAD_CONNECTION_RECEIVE_TIMEOUT + 1); |
| 1373 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout); |
| 1374 | } |
| 1375 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1376 | // This test case verifies standard ICE features in STUN messages. Currently it |
| 1377 | // verifies Message Integrity attribute in STUN messages and username in STUN |
| 1378 | // binding request will have colon (":") between remote and local username. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1379 | TEST_F(PortTest, TestLocalToLocalStandard) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1380 | auto port1 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1381 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1382 | port1->SetIceTiebreaker(kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1383 | auto port2 = CreateUdpPort(kLocalAddr2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1384 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1385 | port2->SetIceTiebreaker(kTiebreaker2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1386 | // Same parameters as TestLocalToLocal above. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1387 | TestConnectivity("udp", std::move(port1), "udp", std::move(port2), true, true, |
| 1388 | true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | // This test is trying to validate a successful and failure scenario in a |
| 1392 | // loopback test when protocol is RFC5245. For success IceTiebreaker, username |
| 1393 | // should remain equal to the request generated by the port and role of port |
| 1394 | // must be in controlling. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1395 | TEST_F(PortTest, TestLoopbackCall) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1396 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1397 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1398 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1399 | lport->PrepareAddress(); |
| 1400 | ASSERT_FALSE(lport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1401 | Connection* conn = |
| 1402 | lport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1403 | conn->Ping(0); |
| 1404 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1405 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1406 | IceMessage* msg = lport->last_stun_msg(); |
| 1407 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1408 | conn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1409 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1410 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1411 | msg = lport->last_stun_msg(); |
| 1412 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
| 1413 | |
| 1414 | // If the tiebreaker value is different from port, we expect a error |
| 1415 | // response. |
| 1416 | lport->Reset(); |
| 1417 | lport->AddCandidateAddress(kLocalAddr2); |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 1418 | // Creating a different connection as `conn` is receiving. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1419 | Connection* conn1 = |
| 1420 | lport->CreateConnection(lport->Candidates()[1], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1421 | conn1->Ping(0); |
| 1422 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1423 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1424 | msg = lport->last_stun_msg(); |
| 1425 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1426 | std::unique_ptr<IceMessage> modified_req( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1427 | CreateStunMessage(STUN_BINDING_REQUEST)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1428 | const StunByteStringAttribute* username_attr = |
| 1429 | msg->GetByteString(STUN_ATTR_USERNAME); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1430 | modified_req->AddAttribute(std::make_unique<StunByteStringAttribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1431 | STUN_ATTR_USERNAME, username_attr->GetString())); |
| 1432 | // To make sure we receive error response, adding tiebreaker less than |
| 1433 | // what's present in request. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1434 | modified_req->AddAttribute(std::make_unique<StunUInt64Attribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1435 | STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1)); |
| 1436 | modified_req->AddMessageIntegrity("lpass"); |
| 1437 | modified_req->AddFingerprint(); |
| 1438 | |
| 1439 | lport->Reset(); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1440 | auto buf = std::make_unique<ByteBufferWriter>(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1441 | WriteStunMessage(*modified_req, buf.get()); |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1442 | conn1->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1443 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1444 | msg = lport->last_stun_msg(); |
| 1445 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1446 | } |
| 1447 | |
| 1448 | // This test verifies role conflict signal is received when there is |
| 1449 | // conflict in the role. In this case both ports are in controlling and |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 1450 | // `rport` has higher tiebreaker value than `lport`. Since `lport` has lower |
| 1451 | // value of tiebreaker, when it receives ping request from `rport` it will |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1452 | // send role conflict signal. |
| 1453 | TEST_F(PortTest, TestIceRoleConflict) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1454 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1455 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1456 | lport->SetIceTiebreaker(kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1457 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1458 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1459 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1460 | |
| 1461 | lport->PrepareAddress(); |
| 1462 | rport->PrepareAddress(); |
| 1463 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1464 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1465 | Connection* lconn = |
| 1466 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1467 | Connection* rconn = |
| 1468 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1469 | rconn->Ping(0); |
| 1470 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1471 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1472 | IceMessage* msg = rport->last_stun_msg(); |
| 1473 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1474 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1475 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1476 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1477 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1478 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1479 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
| 1480 | EXPECT_TRUE(role_conflict()); |
| 1481 | } |
| 1482 | |
| 1483 | TEST_F(PortTest, TestTcpNoDelay) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1484 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1485 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1486 | int option_value = -1; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1487 | int success = port1->GetOption(rtc::Socket::OPT_NODELAY, &option_value); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1488 | ASSERT_EQ(0, success); // GetOption() should complete successfully w/ 0 |
| 1489 | ASSERT_EQ(1, option_value); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | TEST_F(PortTest, TestDelayedBindingUdp) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1493 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1494 | FakePacketSocketFactory socket_factory; |
| 1495 | |
| 1496 | socket_factory.set_next_udp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1497 | auto port = CreateUdpPort(kLocalAddr1, &socket_factory); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1498 | |
| 1499 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1500 | port->PrepareAddress(); |
| 1501 | |
| 1502 | EXPECT_EQ(0U, port->Candidates().size()); |
| 1503 | socket->SignalAddressReady(socket, kLocalAddr2); |
| 1504 | |
| 1505 | EXPECT_EQ(1U, port->Candidates().size()); |
| 1506 | } |
| 1507 | |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1508 | TEST_F(PortTest, TestDisableInterfaceOfTcpPort) { |
| 1509 | FakeAsyncPacketSocket* lsocket = new FakeAsyncPacketSocket(); |
| 1510 | FakeAsyncPacketSocket* rsocket = new FakeAsyncPacketSocket(); |
| 1511 | FakePacketSocketFactory socket_factory; |
| 1512 | |
| 1513 | socket_factory.set_next_server_tcp_socket(lsocket); |
| 1514 | auto lport = CreateTcpPort(kLocalAddr1, &socket_factory); |
| 1515 | |
| 1516 | socket_factory.set_next_server_tcp_socket(rsocket); |
| 1517 | auto rport = CreateTcpPort(kLocalAddr2, &socket_factory); |
| 1518 | |
Niels Möller | 4a1c2c4 | 2021-09-28 10:17:07 +0200 | [diff] [blame] | 1519 | lsocket->set_state(AsyncPacketSocket::STATE_BOUND); |
| 1520 | lsocket->local_address_ = kLocalAddr1; |
| 1521 | rsocket->set_state(AsyncPacketSocket::STATE_BOUND); |
| 1522 | rsocket->local_address_ = kLocalAddr2; |
Jonas Oreland | 7a284e1 | 2020-01-28 09:21:54 +0100 | [diff] [blame] | 1523 | |
| 1524 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1525 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1526 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1527 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1528 | |
| 1529 | lport->PrepareAddress(); |
| 1530 | rport->PrepareAddress(); |
| 1531 | ASSERT_FALSE(rport->Candidates().empty()); |
| 1532 | |
| 1533 | // A client socket. |
| 1534 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
| 1535 | socket->local_address_ = kLocalAddr1; |
| 1536 | socket->remote_address_ = kLocalAddr2; |
| 1537 | socket_factory.set_next_client_tcp_socket(socket); |
| 1538 | Connection* lconn = |
| 1539 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1540 | ASSERT_NE(lconn, nullptr); |
| 1541 | socket->SignalConnect(socket); |
| 1542 | lconn->Ping(0); |
| 1543 | |
| 1544 | // Now disconnect the client socket... |
| 1545 | socket->SignalClose(socket, 1); |
| 1546 | |
| 1547 | // And prevent new sockets from being created. |
| 1548 | socket_factory.set_next_client_tcp_socket(nullptr); |
| 1549 | |
| 1550 | // Test that Ping() does not cause SEGV. |
| 1551 | lconn->Ping(0); |
| 1552 | } |
| 1553 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1554 | void PortTest::TestCrossFamilyPorts(int type) { |
| 1555 | FakePacketSocketFactory factory; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1556 | std::unique_ptr<Port> ports[4]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1557 | SocketAddress addresses[4] = { |
| 1558 | SocketAddress("192.168.1.3", 0), SocketAddress("192.168.1.4", 0), |
| 1559 | SocketAddress("2001:db8::1", 0), SocketAddress("2001:db8::2", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1560 | for (int i = 0; i < 4; i++) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1561 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1562 | if (type == SOCK_DGRAM) { |
| 1563 | factory.set_next_udp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1564 | ports[i] = CreateUdpPort(addresses[i], &factory); |
Niels Möller | 4a1c2c4 | 2021-09-28 10:17:07 +0200 | [diff] [blame] | 1565 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1566 | socket->SignalAddressReady(socket, addresses[i]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1567 | } else if (type == SOCK_STREAM) { |
| 1568 | factory.set_next_server_tcp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1569 | ports[i] = CreateTcpPort(addresses[i], &factory); |
Niels Möller | 4a1c2c4 | 2021-09-28 10:17:07 +0200 | [diff] [blame] | 1570 | socket->set_state(AsyncPacketSocket::STATE_BOUND); |
| 1571 | socket->local_address_ = addresses[i]; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1572 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1573 | ports[i]->PrepareAddress(); |
| 1574 | } |
| 1575 | |
| 1576 | // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate. |
| 1577 | if (type == SOCK_STREAM) { |
| 1578 | FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); |
| 1579 | factory.set_next_client_tcp_socket(clientsocket); |
| 1580 | } |
| 1581 | Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()), |
| 1582 | Port::ORIGIN_MESSAGE); |
| 1583 | EXPECT_TRUE(NULL == c); |
| 1584 | EXPECT_EQ(0U, ports[0]->connections().size()); |
| 1585 | c = ports[0]->CreateConnection(GetCandidate(ports[1].get()), |
| 1586 | Port::ORIGIN_MESSAGE); |
| 1587 | EXPECT_FALSE(NULL == c); |
| 1588 | EXPECT_EQ(1U, ports[0]->connections().size()); |
| 1589 | |
| 1590 | // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate. |
| 1591 | if (type == SOCK_STREAM) { |
| 1592 | FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); |
| 1593 | factory.set_next_client_tcp_socket(clientsocket); |
| 1594 | } |
| 1595 | c = ports[2]->CreateConnection(GetCandidate(ports[0].get()), |
| 1596 | Port::ORIGIN_MESSAGE); |
| 1597 | EXPECT_TRUE(NULL == c); |
| 1598 | EXPECT_EQ(0U, ports[2]->connections().size()); |
| 1599 | c = ports[2]->CreateConnection(GetCandidate(ports[3].get()), |
| 1600 | Port::ORIGIN_MESSAGE); |
| 1601 | EXPECT_FALSE(NULL == c); |
| 1602 | EXPECT_EQ(1U, ports[2]->connections().size()); |
| 1603 | } |
| 1604 | |
| 1605 | TEST_F(PortTest, TestSkipCrossFamilyTcp) { |
| 1606 | TestCrossFamilyPorts(SOCK_STREAM); |
| 1607 | } |
| 1608 | |
| 1609 | TEST_F(PortTest, TestSkipCrossFamilyUdp) { |
| 1610 | TestCrossFamilyPorts(SOCK_DGRAM); |
| 1611 | } |
| 1612 | |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1613 | void PortTest::ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1614 | Connection* c = p1->CreateConnection(GetCandidate(p2), Port::ORIGIN_MESSAGE); |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1615 | if (can_connect) { |
| 1616 | EXPECT_FALSE(NULL == c); |
| 1617 | EXPECT_EQ(1U, p1->connections().size()); |
| 1618 | } else { |
| 1619 | EXPECT_TRUE(NULL == c); |
| 1620 | EXPECT_EQ(0U, p1->connections().size()); |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | TEST_F(PortTest, TestUdpV6CrossTypePorts) { |
| 1625 | FakePacketSocketFactory factory; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1626 | std::unique_ptr<Port> ports[4]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1627 | SocketAddress addresses[4] = { |
| 1628 | SocketAddress("2001:db8::1", 0), SocketAddress("fe80::1", 0), |
| 1629 | SocketAddress("fe80::2", 0), SocketAddress("::1", 0)}; |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1630 | for (int i = 0; i < 4; i++) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1631 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1632 | factory.set_next_udp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1633 | ports[i] = CreateUdpPort(addresses[i], &factory); |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1634 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1635 | socket->SignalAddressReady(socket, addresses[i]); |
| 1636 | ports[i]->PrepareAddress(); |
| 1637 | } |
| 1638 | |
| 1639 | Port* standard = ports[0].get(); |
| 1640 | Port* link_local1 = ports[1].get(); |
| 1641 | Port* link_local2 = ports[2].get(); |
| 1642 | Port* localhost = ports[3].get(); |
| 1643 | |
| 1644 | ExpectPortsCanConnect(false, link_local1, standard); |
| 1645 | ExpectPortsCanConnect(false, standard, link_local1); |
| 1646 | ExpectPortsCanConnect(false, link_local1, localhost); |
| 1647 | ExpectPortsCanConnect(false, localhost, link_local1); |
| 1648 | |
| 1649 | ExpectPortsCanConnect(true, link_local1, link_local2); |
| 1650 | ExpectPortsCanConnect(true, localhost, standard); |
| 1651 | ExpectPortsCanConnect(true, standard, localhost); |
| 1652 | } |
| 1653 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1654 | // This test verifies DSCP value set through SetOption interface can be |
| 1655 | // get through DefaultDscpValue. |
| 1656 | TEST_F(PortTest, TestDefaultDscpValue) { |
| 1657 | int dscp; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1658 | auto udpport = CreateUdpPort(kLocalAddr1); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1659 | EXPECT_EQ(0, udpport->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS6)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1660 | EXPECT_EQ(0, udpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1661 | auto tcpport = CreateTcpPort(kLocalAddr1); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1662 | EXPECT_EQ(0, tcpport->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF31)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1663 | EXPECT_EQ(0, tcpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1664 | EXPECT_EQ(rtc::DSCP_AF31, dscp); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1665 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1666 | EXPECT_EQ(0, stunport->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF41)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1667 | EXPECT_EQ(0, stunport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1668 | EXPECT_EQ(rtc::DSCP_AF41, dscp); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1669 | auto turnport1 = |
| 1670 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1671 | // Socket is created in PrepareAddress. |
| 1672 | turnport1->PrepareAddress(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1673 | EXPECT_EQ(0, turnport1->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS7)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1674 | EXPECT_EQ(0, turnport1->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1675 | EXPECT_EQ(rtc::DSCP_CS7, dscp); |
| 1676 | // This will verify correct value returned without the socket. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1677 | auto turnport2 = |
| 1678 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1679 | EXPECT_EQ(0, turnport2->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS6)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1680 | EXPECT_EQ(0, turnport2->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1681 | EXPECT_EQ(rtc::DSCP_CS6, dscp); |
| 1682 | } |
| 1683 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1684 | // Test sending STUN messages. |
| 1685 | TEST_F(PortTest, TestSendStunMessage) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1686 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1687 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1688 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1689 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1690 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1691 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1692 | |
| 1693 | // Send a fake ping from lport to rport. |
| 1694 | lport->PrepareAddress(); |
| 1695 | rport->PrepareAddress(); |
| 1696 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1697 | Connection* lconn = |
| 1698 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1699 | Connection* rconn = |
| 1700 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1701 | lconn->Ping(0); |
| 1702 | |
| 1703 | // Check that it's a proper BINDING-REQUEST. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1704 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1705 | IceMessage* msg = lport->last_stun_msg(); |
| 1706 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1707 | EXPECT_FALSE(msg->IsLegacy()); |
| 1708 | const StunByteStringAttribute* username_attr = |
| 1709 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 1710 | ASSERT_TRUE(username_attr != NULL); |
| 1711 | const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 1712 | ASSERT_TRUE(priority_attr != NULL); |
| 1713 | EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value()); |
| 1714 | EXPECT_EQ("rfrag:lfrag", username_attr->GetString()); |
| 1715 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 1716 | EXPECT_EQ(StunMessage::IntegrityStatus::kIntegrityOk, |
| 1717 | msg->ValidateMessageIntegrity("rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1718 | const StunUInt64Attribute* ice_controlling_attr = |
| 1719 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1720 | ASSERT_TRUE(ice_controlling_attr != NULL); |
| 1721 | EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value()); |
| 1722 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1723 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 1724 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1725 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1726 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1727 | |
| 1728 | // Request should not include ping count. |
| 1729 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1730 | |
| 1731 | // Save a copy of the BINDING-REQUEST for use below. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1732 | std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1733 | |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1734 | // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE. |
| 1735 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1736 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1737 | msg = rport->last_stun_msg(); |
| 1738 | ASSERT_TRUE(msg != NULL); |
| 1739 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1740 | // Received a BINDING-RESPONSE. |
| 1741 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1742 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1743 | // Verify the STUN Stats. |
| 1744 | EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total); |
| 1745 | EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response); |
| 1746 | EXPECT_EQ(1U, lconn->stats().recv_ping_responses); |
| 1747 | EXPECT_EQ(1U, rconn->stats().recv_ping_requests); |
| 1748 | EXPECT_EQ(1U, rconn->stats().sent_ping_responses); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1749 | |
| 1750 | EXPECT_FALSE(msg->IsLegacy()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1751 | const StunAddressAttribute* addr_attr = |
| 1752 | msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1753 | ASSERT_TRUE(addr_attr != NULL); |
| 1754 | EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress()); |
| 1755 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 1756 | EXPECT_EQ(StunMessage::IntegrityStatus::kIntegrityOk, |
| 1757 | msg->ValidateMessageIntegrity("rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1758 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1759 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1760 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1761 | // No USERNAME or PRIORITY in ICE responses. |
| 1762 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1763 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1764 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL); |
| 1765 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL); |
| 1766 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1767 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1768 | |
| 1769 | // Response should not include ping count. |
| 1770 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1771 | |
| 1772 | // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life, |
| 1773 | // but we can do it here. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1774 | rport->SendBindingErrorResponse( |
| 1775 | request.get(), lport->Candidates()[0].address(), STUN_ERROR_SERVER_ERROR, |
| 1776 | STUN_ERROR_REASON_SERVER_ERROR); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1777 | msg = rport->last_stun_msg(); |
| 1778 | ASSERT_TRUE(msg != NULL); |
| 1779 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1780 | EXPECT_FALSE(msg->IsLegacy()); |
| 1781 | const StunErrorCodeAttribute* error_attr = msg->GetErrorCode(); |
| 1782 | ASSERT_TRUE(error_attr != NULL); |
| 1783 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code()); |
| 1784 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason()); |
| 1785 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 1786 | EXPECT_EQ(StunMessage::IntegrityStatus::kIntegrityOk, |
| 1787 | msg->ValidateMessageIntegrity("rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1788 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1789 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1790 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1791 | // No USERNAME with ICE. |
| 1792 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1793 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1794 | |
| 1795 | // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED |
| 1796 | // and (incremented) RETRANSMIT_COUNT attributes. |
| 1797 | rport->Reset(); |
| 1798 | rport->set_send_retransmit_count_attribute(true); |
| 1799 | rconn->Ping(0); |
| 1800 | rconn->Ping(0); |
| 1801 | rconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1802 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1803 | msg = rport->last_stun_msg(); |
| 1804 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1805 | const StunUInt64Attribute* ice_controlled_attr = |
| 1806 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); |
| 1807 | ASSERT_TRUE(ice_controlled_attr != NULL); |
| 1808 | EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value()); |
| 1809 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1810 | |
| 1811 | // Request should include ping count. |
| 1812 | const StunUInt32Attribute* retransmit_attr = |
| 1813 | msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1814 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1815 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1816 | |
| 1817 | // Respond with a BINDING-RESPONSE. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1818 | request = CopyStunMessage(*msg); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1819 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1820 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1821 | msg = lport->last_stun_msg(); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1822 | // Receive the BINDING-RESPONSE. |
| 1823 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1824 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1825 | |
| 1826 | // Verify the Stun ping stats. |
| 1827 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total); |
| 1828 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); |
| 1829 | EXPECT_EQ(1U, rconn->stats().recv_ping_responses); |
| 1830 | EXPECT_EQ(1U, lconn->stats().sent_ping_responses); |
| 1831 | EXPECT_EQ(1U, lconn->stats().recv_ping_requests); |
| 1832 | // Ping after receiver the first response |
| 1833 | rconn->Ping(0); |
| 1834 | rconn->Ping(0); |
| 1835 | EXPECT_EQ(5U, rconn->stats().sent_ping_requests_total); |
| 1836 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1837 | |
| 1838 | // Response should include same ping count. |
| 1839 | retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1840 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1841 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1842 | } |
| 1843 | |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1844 | TEST_F(PortTest, TestNomination) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1845 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1846 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1847 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1848 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1849 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1850 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1851 | |
| 1852 | lport->PrepareAddress(); |
| 1853 | rport->PrepareAddress(); |
| 1854 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1855 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1856 | Connection* lconn = |
| 1857 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1858 | Connection* rconn = |
| 1859 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1860 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 1861 | // `lconn` is controlling, `rconn` is controlled. |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1862 | uint32_t nomination = 1234; |
| 1863 | lconn->set_nomination(nomination); |
| 1864 | |
| 1865 | EXPECT_FALSE(lconn->nominated()); |
| 1866 | EXPECT_FALSE(rconn->nominated()); |
| 1867 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 1868 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 1869 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 1870 | // Send ping (including the nomination value) from `lconn` to `rconn`. This |
| 1871 | // should set the remote nomination of `rconn`. |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1872 | lconn->Ping(0); |
| 1873 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 1874 | ASSERT_TRUE(lport->last_stun_buf()); |
| 1875 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1876 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1877 | EXPECT_EQ(nomination, rconn->remote_nomination()); |
| 1878 | EXPECT_FALSE(lconn->nominated()); |
| 1879 | EXPECT_TRUE(rconn->nominated()); |
| 1880 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 1881 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 1882 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 1883 | // This should result in an acknowledgment sent back from `rconn` to `lconn`, |
| 1884 | // updating the acknowledged nomination of `lconn`. |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1885 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 1886 | ASSERT_TRUE(rport->last_stun_buf()); |
| 1887 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1888 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1889 | EXPECT_EQ(nomination, lconn->acked_nomination()); |
| 1890 | EXPECT_TRUE(lconn->nominated()); |
| 1891 | EXPECT_TRUE(rconn->nominated()); |
| 1892 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 1893 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 1894 | } |
| 1895 | |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1896 | TEST_F(PortTest, TestRoundTripTime) { |
| 1897 | rtc::ScopedFakeClock clock; |
| 1898 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1899 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1900 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1901 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1902 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1903 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1904 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1905 | |
| 1906 | lport->PrepareAddress(); |
| 1907 | rport->PrepareAddress(); |
| 1908 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1909 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1910 | Connection* lconn = |
| 1911 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1912 | Connection* rconn = |
| 1913 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1914 | |
| 1915 | EXPECT_EQ(0u, lconn->stats().total_round_trip_time_ms); |
| 1916 | EXPECT_FALSE(lconn->stats().current_round_trip_time_ms); |
| 1917 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1918 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 1919 | 10); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1920 | EXPECT_EQ(10u, lconn->stats().total_round_trip_time_ms); |
| 1921 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 1922 | EXPECT_EQ(10u, *lconn->stats().current_round_trip_time_ms); |
| 1923 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1924 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 1925 | 20); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1926 | EXPECT_EQ(30u, lconn->stats().total_round_trip_time_ms); |
| 1927 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 1928 | EXPECT_EQ(20u, *lconn->stats().current_round_trip_time_ms); |
| 1929 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1930 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 1931 | 30); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1932 | EXPECT_EQ(60u, lconn->stats().total_round_trip_time_ms); |
| 1933 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 1934 | EXPECT_EQ(30u, *lconn->stats().current_round_trip_time_ms); |
| 1935 | } |
| 1936 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1937 | TEST_F(PortTest, TestUseCandidateAttribute) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1938 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1939 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1940 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1941 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1942 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1943 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1944 | |
| 1945 | // Send a fake ping from lport to rport. |
| 1946 | lport->PrepareAddress(); |
| 1947 | rport->PrepareAddress(); |
| 1948 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1949 | Connection* lconn = |
| 1950 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1951 | lconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1952 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1953 | IceMessage* msg = lport->last_stun_msg(); |
| 1954 | const StunUInt64Attribute* ice_controlling_attr = |
| 1955 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1956 | ASSERT_TRUE(ice_controlling_attr != NULL); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1957 | const StunByteStringAttribute* use_candidate_attr = |
| 1958 | msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1959 | ASSERT_TRUE(use_candidate_attr != NULL); |
| 1960 | } |
| 1961 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1962 | // Tests that when the network type changes, the network cost of the port will |
| 1963 | // change, the network cost of the local candidates will change. Also tests that |
| 1964 | // the remote network costs are updated with the stun binding requests. |
| 1965 | TEST_F(PortTest, TestNetworkCostChange) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1966 | rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1967 | auto lport = CreateTestPort(test_network, "lfrag", "lpass"); |
| 1968 | auto rport = CreateTestPort(test_network, "rfrag", "rpass"); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1969 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1970 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1971 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1972 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1973 | lport->PrepareAddress(); |
| 1974 | rport->PrepareAddress(); |
| 1975 | |
| 1976 | // Default local port cost is rtc::kNetworkCostUnknown. |
| 1977 | EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); |
| 1978 | ASSERT_TRUE(!lport->Candidates().empty()); |
| 1979 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1980 | EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); |
| 1981 | } |
| 1982 | |
| 1983 | // Change the network type to wifi. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1984 | test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1985 | EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
| 1986 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1987 | EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
| 1988 | } |
| 1989 | |
| 1990 | // Add a connection and then change the network type. |
| 1991 | Connection* lconn = |
| 1992 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1993 | // Change the network type to cellular. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1994 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1995 | EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
| 1996 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1997 | EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
| 1998 | } |
| 1999 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2000 | test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2001 | Connection* rconn = |
| 2002 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2003 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2004 | lconn->Ping(0); |
| 2005 | // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
| 2006 | // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
| 2007 | // message is handled in rconn, The rconn's remote candidate will have cost |
| 2008 | // rtc::kNetworkCostHigh; |
| 2009 | EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2010 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2011 | IceMessage* msg = lport->last_stun_msg(); |
| 2012 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 2013 | // Pass the binding request to rport. |
| 2014 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2015 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2016 | // Wait until rport sends the response and then check the remote network cost. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2017 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2018 | EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); |
| 2019 | } |
| 2020 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2021 | TEST_F(PortTest, TestNetworkInfoAttribute) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2022 | rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2023 | auto lport = CreateTestPort(test_network, "lfrag", "lpass"); |
| 2024 | auto rport = CreateTestPort(test_network, "rfrag", "rpass"); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2025 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2026 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2027 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2028 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2029 | |
| 2030 | uint16_t lnetwork_id = 9; |
| 2031 | lport->Network()->set_id(lnetwork_id); |
| 2032 | // Send a fake ping from lport to rport. |
| 2033 | lport->PrepareAddress(); |
| 2034 | rport->PrepareAddress(); |
| 2035 | Connection* lconn = |
| 2036 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2037 | lconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2038 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2039 | IceMessage* msg = lport->last_stun_msg(); |
| 2040 | const StunUInt32Attribute* network_info_attr = |
Jonas Oreland | fa54364 | 2020-09-16 10:44:54 +0200 | [diff] [blame] | 2041 | msg->GetUInt32(STUN_ATTR_GOOG_NETWORK_INFO); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2042 | ASSERT_TRUE(network_info_attr != NULL); |
| 2043 | uint32_t network_info = network_info_attr->value(); |
| 2044 | EXPECT_EQ(lnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2045 | // Default network has unknown type and cost kNetworkCostUnknown. |
| 2046 | EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2047 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2048 | // Set the network type to be cellular so its cost will be kNetworkCostHigh. |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2049 | // Send a fake ping from rport to lport. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2050 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2051 | uint16_t rnetwork_id = 8; |
| 2052 | rport->Network()->set_id(rnetwork_id); |
| 2053 | Connection* rconn = |
| 2054 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2055 | rconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2056 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2057 | msg = rport->last_stun_msg(); |
Jonas Oreland | fa54364 | 2020-09-16 10:44:54 +0200 | [diff] [blame] | 2058 | network_info_attr = msg->GetUInt32(STUN_ATTR_GOOG_NETWORK_INFO); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2059 | ASSERT_TRUE(network_info_attr != NULL); |
| 2060 | network_info = network_info_attr->value(); |
| 2061 | EXPECT_EQ(rnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2062 | EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2065 | // Test handling STUN messages. |
| 2066 | TEST_F(PortTest, TestHandleStunMessage) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2067 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2068 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2069 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2070 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2071 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2072 | rtc::SocketAddress addr(kLocalAddr1); |
| 2073 | std::string username; |
| 2074 | |
| 2075 | // BINDING-REQUEST from local to remote with valid ICE username, |
| 2076 | // MESSAGE-INTEGRITY, and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2077 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2078 | in_msg->AddMessageIntegrity("rpass"); |
| 2079 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2080 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2081 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2082 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2083 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2084 | EXPECT_EQ("lfrag", username); |
| 2085 | |
| 2086 | // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2087 | in_msg = CreateStunMessage(STUN_BINDING_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2088 | in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2089 | STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2090 | in_msg->AddMessageIntegrity("rpass"); |
| 2091 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2092 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2093 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2094 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2095 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2096 | EXPECT_EQ("", username); |
| 2097 | |
| 2098 | // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2099 | in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2100 | in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2101 | STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2102 | STUN_ERROR_REASON_SERVER_ERROR)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2103 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2104 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2105 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2106 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2107 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2108 | EXPECT_EQ("", username); |
| 2109 | ASSERT_TRUE(out_msg->GetErrorCode() != NULL); |
| 2110 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); |
| 2111 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2112 | out_msg->GetErrorCode()->reason()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
guoweis | d12140a | 2015-09-10 13:32:11 -0700 | [diff] [blame] | 2115 | // Tests handling of ICE binding requests with missing or incorrect usernames. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2116 | TEST_F(PortTest, TestHandleStunMessageBadUsername) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2117 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2118 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2119 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2120 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2121 | rtc::SocketAddress addr(kLocalAddr1); |
| 2122 | std::string username; |
| 2123 | |
| 2124 | // BINDING-REQUEST with no username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2125 | in_msg = CreateStunMessage(STUN_BINDING_REQUEST); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2126 | in_msg->AddMessageIntegrity("rpass"); |
| 2127 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2128 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2129 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2130 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2131 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2132 | EXPECT_EQ("", username); |
| 2133 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2134 | |
| 2135 | // BINDING-REQUEST with empty username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2136 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, ""); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2137 | in_msg->AddMessageIntegrity("rpass"); |
| 2138 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2139 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2140 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2141 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2142 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2143 | EXPECT_EQ("", username); |
| 2144 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2145 | |
| 2146 | // BINDING-REQUEST with too-short username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2147 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2148 | in_msg->AddMessageIntegrity("rpass"); |
| 2149 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2150 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2151 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2152 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2153 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2154 | EXPECT_EQ("", username); |
| 2155 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2156 | |
| 2157 | // BINDING-REQUEST with reversed username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2158 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "lfrag:rfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2159 | in_msg->AddMessageIntegrity("rpass"); |
| 2160 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2161 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2162 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2163 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2164 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2165 | EXPECT_EQ("", username); |
| 2166 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2167 | |
| 2168 | // BINDING-REQUEST with garbage username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2169 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "abcd:efgh"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2170 | in_msg->AddMessageIntegrity("rpass"); |
| 2171 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2172 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2173 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2174 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2175 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2176 | EXPECT_EQ("", username); |
| 2177 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2178 | } |
| 2179 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2180 | // Test handling STUN messages with missing or malformed M-I. |
| 2181 | TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2182 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2183 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2184 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2185 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2186 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2187 | rtc::SocketAddress addr(kLocalAddr1); |
| 2188 | std::string username; |
| 2189 | |
| 2190 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2191 | // FINGERPRINT, but no MESSAGE-INTEGRITY. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2192 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2193 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2194 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2195 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2196 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2197 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2198 | EXPECT_EQ("", username); |
| 2199 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2200 | |
| 2201 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2202 | // FINGERPRINT, but invalid MESSAGE-INTEGRITY. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2203 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2204 | in_msg->AddMessageIntegrity("invalid"); |
| 2205 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2206 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2207 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2208 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2209 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2210 | EXPECT_EQ("", username); |
| 2211 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2212 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 2213 | // TODO(?): BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2214 | // by the Connection, not the Port, since they require the remote username. |
| 2215 | // Change this test to pass in data via Connection::OnReadPacket instead. |
| 2216 | } |
| 2217 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2218 | // Test handling STUN messages with missing or malformed FINGERPRINT. |
| 2219 | TEST_F(PortTest, TestHandleStunMessageBadFingerprint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2220 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2221 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2222 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2223 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2224 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2225 | rtc::SocketAddress addr(kLocalAddr1); |
| 2226 | std::string username; |
| 2227 | |
| 2228 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2229 | // MESSAGE-INTEGRITY, but no FINGERPRINT; GetStunMessage should fail. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2230 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2231 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2232 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2233 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2234 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2235 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2236 | |
| 2237 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2238 | in_msg->AddFingerprint(); |
| 2239 | in_msg->SetTransactionID("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2240 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2241 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2242 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2243 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2244 | |
| 2245 | // Valid BINDING-RESPONSE, except no FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2246 | in_msg = CreateStunMessage(STUN_BINDING_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2247 | in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2248 | STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2249 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2250 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2251 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2252 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2253 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2254 | |
| 2255 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2256 | in_msg->AddFingerprint(); |
| 2257 | in_msg->SetTransactionID("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2258 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2259 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2260 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2261 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2262 | |
| 2263 | // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2264 | in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2265 | in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2266 | STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2267 | STUN_ERROR_REASON_SERVER_ERROR)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2268 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2269 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2270 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2271 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2272 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2273 | |
| 2274 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2275 | in_msg->AddFingerprint(); |
| 2276 | in_msg->SetTransactionID("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2277 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2278 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2279 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2280 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2281 | } |
| 2282 | |
Taylor Brandstetter | fb4351b | 2020-03-23 16:00:31 -0700 | [diff] [blame] | 2283 | // Test handling a STUN message with unknown attributes in the |
| 2284 | // "comprehension-required" range. Should respond with an error with the |
| 2285 | // unknown attributes' IDs. |
| 2286 | TEST_F(PortTest, |
| 2287 | TestHandleStunRequestWithUnknownComprehensionRequiredAttribute) { |
| 2288 | // Our port will act as the "remote" port. |
| 2289 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
| 2290 | |
| 2291 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2292 | auto buf = std::make_unique<ByteBufferWriter>(); |
| 2293 | rtc::SocketAddress addr(kLocalAddr1); |
| 2294 | std::string username; |
| 2295 | |
| 2296 | // Build ordinary message with valid ufrag/pass. |
| 2297 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
| 2298 | in_msg->AddMessageIntegrity("rpass"); |
| 2299 | // Add a couple attributes with ID in comprehension-required range. |
| 2300 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0x7777)); |
| 2301 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0x4567)); |
| 2302 | // ... And one outside the range. |
| 2303 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0xdead)); |
| 2304 | in_msg->AddFingerprint(); |
| 2305 | WriteStunMessage(*in_msg, buf.get()); |
| 2306 | ASSERT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2307 | &username)); |
| 2308 | IceMessage* error_response = port->last_stun_msg(); |
| 2309 | ASSERT_NE(nullptr, error_response); |
| 2310 | |
| 2311 | // Verify that the "unknown attribute" error response has the right error |
| 2312 | // code, and includes an attribute that lists out the unrecognized attribute |
| 2313 | // types. |
| 2314 | EXPECT_EQ(STUN_ERROR_UNKNOWN_ATTRIBUTE, error_response->GetErrorCodeValue()); |
| 2315 | const StunUInt16ListAttribute* unknown_attributes = |
| 2316 | error_response->GetUnknownAttributes(); |
| 2317 | ASSERT_NE(nullptr, unknown_attributes); |
| 2318 | ASSERT_EQ(2u, unknown_attributes->Size()); |
| 2319 | EXPECT_EQ(0x7777, unknown_attributes->GetType(0)); |
| 2320 | EXPECT_EQ(0x4567, unknown_attributes->GetType(1)); |
| 2321 | } |
| 2322 | |
| 2323 | // Similar to the above, but with a response instead of a request. In this |
| 2324 | // case the response should just be ignored and transaction treated is failed. |
| 2325 | TEST_F(PortTest, |
| 2326 | TestHandleStunResponseWithUnknownComprehensionRequiredAttribute) { |
| 2327 | // Generic setup. |
| 2328 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 2329 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2330 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
| 2331 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2332 | lport->PrepareAddress(); |
| 2333 | rport->PrepareAddress(); |
| 2334 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2335 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2336 | Connection* lconn = |
| 2337 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2338 | Connection* rconn = |
| 2339 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2340 | |
| 2341 | // Send request. |
| 2342 | lconn->Ping(0); |
| 2343 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
| 2344 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 2345 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
| 2346 | |
| 2347 | // Intercept request and add comprehension required attribute. |
| 2348 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
| 2349 | auto modified_response = rport->last_stun_msg()->Clone(); |
| 2350 | modified_response->AddAttribute(StunAttribute::CreateUInt32(0x7777)); |
| 2351 | modified_response->RemoveAttribute(STUN_ATTR_FINGERPRINT); |
| 2352 | modified_response->AddFingerprint(); |
| 2353 | ByteBufferWriter buf; |
| 2354 | WriteStunMessage(*modified_response, &buf); |
| 2355 | lconn->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); |
| 2356 | // Response should have been ignored, leaving us unwritable still. |
| 2357 | EXPECT_FALSE(lconn->writable()); |
| 2358 | } |
| 2359 | |
| 2360 | // Similar to the above, but with an indication. As with a response, it should |
| 2361 | // just be ignored. |
| 2362 | TEST_F(PortTest, |
| 2363 | TestHandleStunIndicationWithUnknownComprehensionRequiredAttribute) { |
| 2364 | // Generic set up. |
| 2365 | auto lport = CreateTestPort(kLocalAddr2, "lfrag", "lpass"); |
| 2366 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2367 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
| 2368 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2369 | lport->PrepareAddress(); |
| 2370 | rport->PrepareAddress(); |
| 2371 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2372 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2373 | Connection* lconn = |
| 2374 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2375 | |
| 2376 | // Generate indication with comprehension required attribute and verify it |
| 2377 | // doesn't update last_ping_received. |
| 2378 | auto in_msg = CreateStunMessage(STUN_BINDING_INDICATION); |
| 2379 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0x7777)); |
| 2380 | in_msg->AddFingerprint(); |
| 2381 | ByteBufferWriter buf; |
| 2382 | WriteStunMessage(*in_msg, &buf); |
| 2383 | lconn->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); |
| 2384 | EXPECT_EQ(0u, lconn->last_ping_received()); |
| 2385 | } |
| 2386 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2387 | // Test handling of STUN binding indication messages . STUN binding |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2388 | // indications are allowed only to the connection which is in read mode. |
| 2389 | TEST_F(PortTest, TestHandleStunBindingIndication) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2390 | auto lport = CreateTestPort(kLocalAddr2, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2391 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2392 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2393 | |
| 2394 | // Verifying encoding and decoding STUN indication message. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2395 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2396 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2397 | rtc::SocketAddress addr(kLocalAddr1); |
| 2398 | std::string username; |
| 2399 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2400 | in_msg = CreateStunMessage(STUN_BINDING_INDICATION); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2401 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2402 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2403 | EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2404 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2405 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2406 | EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION); |
| 2407 | EXPECT_EQ("", username); |
| 2408 | |
| 2409 | // Verify connection can handle STUN indication and updates |
| 2410 | // last_ping_received. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2411 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2412 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2413 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2414 | |
| 2415 | lport->PrepareAddress(); |
| 2416 | rport->PrepareAddress(); |
| 2417 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2418 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2419 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2420 | Connection* lconn = |
| 2421 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2422 | Connection* rconn = |
| 2423 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2424 | rconn->Ping(0); |
| 2425 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2426 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2427 | IceMessage* msg = rport->last_stun_msg(); |
| 2428 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 2429 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2430 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2431 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2432 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2433 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2434 | int64_t last_ping_received1 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2435 | |
| 2436 | // Adding a delay of 100ms. |
| 2437 | rtc::Thread::Current()->ProcessMessages(100); |
| 2438 | // Pinging lconn using stun indication message. |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2439 | lconn->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2440 | int64_t last_ping_received2 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2441 | EXPECT_GT(last_ping_received2, last_ping_received1); |
| 2442 | } |
| 2443 | |
| 2444 | TEST_F(PortTest, TestComputeCandidatePriority) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2445 | auto port = CreateTestPort(kLocalAddr1, "name", "pass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2446 | port->set_type_preference(90); |
| 2447 | port->set_component(177); |
| 2448 | port->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2449 | port->AddCandidateAddress(SocketAddress("2001:db8::1234", 1234)); |
| 2450 | port->AddCandidateAddress(SocketAddress("fc12:3456::1234", 1234)); |
| 2451 | port->AddCandidateAddress(SocketAddress("::ffff:192.168.1.4", 1234)); |
| 2452 | port->AddCandidateAddress(SocketAddress("::192.168.1.4", 1234)); |
| 2453 | port->AddCandidateAddress(SocketAddress("2002::1234:5678", 1234)); |
| 2454 | port->AddCandidateAddress(SocketAddress("2001::1234:5678", 1234)); |
| 2455 | port->AddCandidateAddress(SocketAddress("fecf::1234:5678", 1234)); |
| 2456 | port->AddCandidateAddress(SocketAddress("3ffe::1234:5678", 1234)); |
| 2457 | // These should all be: |
| 2458 | // (90 << 24) | ([rfc3484 pref value] << 8) | (256 - 177) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2459 | uint32_t expected_priority_v4 = 1509957199U; |
| 2460 | uint32_t expected_priority_v6 = 1509959759U; |
| 2461 | uint32_t expected_priority_ula = 1509962319U; |
| 2462 | uint32_t expected_priority_v4mapped = expected_priority_v4; |
| 2463 | uint32_t expected_priority_v4compat = 1509949775U; |
| 2464 | uint32_t expected_priority_6to4 = 1509954639U; |
| 2465 | uint32_t expected_priority_teredo = 1509952079U; |
| 2466 | uint32_t expected_priority_sitelocal = 1509949775U; |
| 2467 | uint32_t expected_priority_6bone = 1509949775U; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2468 | ASSERT_EQ(expected_priority_v4, port->Candidates()[0].priority()); |
| 2469 | ASSERT_EQ(expected_priority_v6, port->Candidates()[1].priority()); |
| 2470 | ASSERT_EQ(expected_priority_ula, port->Candidates()[2].priority()); |
| 2471 | ASSERT_EQ(expected_priority_v4mapped, port->Candidates()[3].priority()); |
| 2472 | ASSERT_EQ(expected_priority_v4compat, port->Candidates()[4].priority()); |
| 2473 | ASSERT_EQ(expected_priority_6to4, port->Candidates()[5].priority()); |
| 2474 | ASSERT_EQ(expected_priority_teredo, port->Candidates()[6].priority()); |
| 2475 | ASSERT_EQ(expected_priority_sitelocal, port->Candidates()[7].priority()); |
| 2476 | ASSERT_EQ(expected_priority_6bone, port->Candidates()[8].priority()); |
| 2477 | } |
| 2478 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2479 | // In the case of shared socket, one port may be shared by local and stun. |
| 2480 | // Test that candidates with different types will have different foundation. |
| 2481 | TEST_F(PortTest, TestFoundation) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2482 | auto testport = CreateTestPort(kLocalAddr1, "name", "pass"); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2483 | testport->AddCandidateAddress(kLocalAddr1, kLocalAddr1, LOCAL_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2484 | cricket::ICE_TYPE_PREFERENCE_HOST, false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2485 | testport->AddCandidateAddress(kLocalAddr2, kLocalAddr1, STUN_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2486 | cricket::ICE_TYPE_PREFERENCE_SRFLX, true); |
| 2487 | EXPECT_NE(testport->Candidates()[0].foundation(), |
| 2488 | testport->Candidates()[1].foundation()); |
| 2489 | } |
| 2490 | |
| 2491 | // This test verifies the foundation of different types of ICE candidates. |
| 2492 | TEST_F(PortTest, TestCandidateFoundation) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2493 | std::unique_ptr<rtc::NATServer> nat_server( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2494 | CreateNatServer(kNatAddr1, NAT_OPEN_CONE)); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2495 | auto udpport1 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2496 | udpport1->PrepareAddress(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2497 | auto udpport2 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2498 | udpport2->PrepareAddress(); |
| 2499 | EXPECT_EQ(udpport1->Candidates()[0].foundation(), |
| 2500 | udpport2->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2501 | auto tcpport1 = CreateTcpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2502 | tcpport1->PrepareAddress(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2503 | auto tcpport2 = CreateTcpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2504 | tcpport2->PrepareAddress(); |
| 2505 | EXPECT_EQ(tcpport1->Candidates()[0].foundation(), |
| 2506 | tcpport2->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2507 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2508 | stunport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2509 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2510 | EXPECT_NE(tcpport1->Candidates()[0].foundation(), |
| 2511 | stunport->Candidates()[0].foundation()); |
| 2512 | EXPECT_NE(tcpport2->Candidates()[0].foundation(), |
| 2513 | stunport->Candidates()[0].foundation()); |
| 2514 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2515 | stunport->Candidates()[0].foundation()); |
| 2516 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2517 | stunport->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2518 | // Verifying TURN candidate foundation. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2519 | auto turnport1 = |
| 2520 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2521 | turnport1->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2522 | ASSERT_EQ_WAIT(1U, turnport1->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2523 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2524 | turnport1->Candidates()[0].foundation()); |
| 2525 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2526 | turnport1->Candidates()[0].foundation()); |
| 2527 | EXPECT_NE(stunport->Candidates()[0].foundation(), |
| 2528 | turnport1->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2529 | auto turnport2 = |
| 2530 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2531 | turnport2->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2532 | ASSERT_EQ_WAIT(1U, turnport2->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2533 | EXPECT_EQ(turnport1->Candidates()[0].foundation(), |
| 2534 | turnport2->Candidates()[0].foundation()); |
| 2535 | |
| 2536 | // Running a second turn server, to get different base IP address. |
| 2537 | SocketAddress kTurnUdpIntAddr2("99.99.98.4", STUN_SERVER_PORT); |
| 2538 | SocketAddress kTurnUdpExtAddr2("99.99.98.5", 0); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2539 | TestTurnServer turn_server2(rtc::Thread::Current(), kTurnUdpIntAddr2, |
| 2540 | kTurnUdpExtAddr2); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2541 | auto turnport3 = CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, |
| 2542 | PROTO_UDP, kTurnUdpIntAddr2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2543 | turnport3->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2544 | ASSERT_EQ_WAIT(1U, turnport3->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2545 | EXPECT_NE(turnport3->Candidates()[0].foundation(), |
| 2546 | turnport2->Candidates()[0].foundation()); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2547 | |
| 2548 | // Start a TCP turn server, and check that two turn candidates have |
| 2549 | // different foundations if their relay protocols are different. |
| 2550 | TestTurnServer turn_server3(rtc::Thread::Current(), kTurnTcpIntAddr, |
| 2551 | kTurnUdpExtAddr, PROTO_TCP); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2552 | auto turnport4 = |
| 2553 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_TCP, PROTO_UDP); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2554 | turnport4->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2555 | ASSERT_EQ_WAIT(1U, turnport4->Candidates().size(), kDefaultTimeout); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2556 | EXPECT_NE(turnport2->Candidates()[0].foundation(), |
| 2557 | turnport4->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | // This test verifies the related addresses of different types of |
| 2561 | // ICE candiates. |
| 2562 | TEST_F(PortTest, TestCandidateRelatedAddress) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2563 | auto nat_server = CreateNatServer(kNatAddr1, NAT_OPEN_CONE); |
| 2564 | auto udpport = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2565 | udpport->PrepareAddress(); |
| 2566 | // For UDPPort, related address will be empty. |
| 2567 | EXPECT_TRUE(udpport->Candidates()[0].related_address().IsNil()); |
| 2568 | // Testing related address for stun candidates. |
| 2569 | // For stun candidate related address must be equal to the base |
| 2570 | // socket address. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2571 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2572 | stunport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2573 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2574 | // Check STUN candidate address. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2575 | EXPECT_EQ(stunport->Candidates()[0].address().ipaddr(), kNatAddr1.ipaddr()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2576 | // Check STUN candidate related address. |
| 2577 | EXPECT_EQ(stunport->Candidates()[0].related_address(), |
| 2578 | stunport->GetLocalAddress()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2579 | // Verifying the related address for TURN candidate. |
| 2580 | // For TURN related address must be equal to the mapped address. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2581 | auto turnport = |
| 2582 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2583 | turnport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2584 | ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2585 | EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), |
| 2586 | turnport->Candidates()[0].address().ipaddr()); |
| 2587 | EXPECT_EQ(kNatAddr1.ipaddr(), |
| 2588 | turnport->Candidates()[0].related_address().ipaddr()); |
| 2589 | } |
| 2590 | |
| 2591 | // Test priority value overflow handling when preference is set to 3. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2592 | TEST_F(PortTest, TestCandidatePriority) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2593 | cricket::Candidate cand1; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2594 | cand1.set_priority(3); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2595 | cricket::Candidate cand2; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2596 | cand2.set_priority(1); |
| 2597 | EXPECT_TRUE(cand1.priority() > cand2.priority()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | // Test the Connection priority is calculated correctly. |
| 2601 | TEST_F(PortTest, TestConnectionPriority) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2602 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2603 | lport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_HOST); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2604 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 2605 | rport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_RELAY_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2606 | lport->set_component(123); |
| 2607 | lport->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2608 | rport->set_component(23); |
| 2609 | rport->AddCandidateAddress(SocketAddress("10.1.1.100", 1234)); |
| 2610 | |
| 2611 | EXPECT_EQ(0x7E001E85U, lport->Candidates()[0].priority()); |
| 2612 | EXPECT_EQ(0x2001EE9U, rport->Candidates()[0].priority()); |
| 2613 | |
| 2614 | // RFC 5245 |
| 2615 | // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0) |
| 2616 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2617 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2618 | Connection* lconn = |
| 2619 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2620 | #if defined(WEBRTC_WIN) |
| 2621 | EXPECT_EQ(0x2001EE9FC003D0BU, lconn->priority()); |
| 2622 | #else |
| 2623 | EXPECT_EQ(0x2001EE9FC003D0BLLU, lconn->priority()); |
| 2624 | #endif |
| 2625 | |
| 2626 | lport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2627 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2628 | Connection* rconn = |
| 2629 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2630 | #if defined(WEBRTC_WIN) |
| 2631 | EXPECT_EQ(0x2001EE9FC003D0AU, rconn->priority()); |
| 2632 | #else |
| 2633 | EXPECT_EQ(0x2001EE9FC003D0ALLU, rconn->priority()); |
| 2634 | #endif |
| 2635 | } |
| 2636 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2637 | // Note that UpdateState takes into account the estimated RTT, and the |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2638 | // correctness of using `kMaxExpectedSimulatedRtt` as an upper bound of RTT in |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2639 | // the following tests depends on the link rate and the delay distriubtion |
| 2640 | // configured in VirtualSocketServer::AddPacketToNetwork. The tests below use |
| 2641 | // the default setup where the RTT is deterministically one, which generates an |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2642 | // estimate given by `MINIMUM_RTT` = 100. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2643 | TEST_F(PortTest, TestWritableState) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2644 | rtc::ScopedFakeClock clock; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2645 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2646 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2647 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2648 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2649 | |
| 2650 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2651 | TestChannel ch1(std::move(port1)); |
| 2652 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2653 | |
| 2654 | // Acquire addresses. |
| 2655 | ch1.Start(); |
| 2656 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2657 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 2658 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2659 | |
| 2660 | // Send a ping from src to dst. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2661 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2662 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2663 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2664 | // for TCP connect |
| 2665 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2666 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2667 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2668 | |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2669 | // Data should be sendable before the connection is accepted. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2670 | char data[] = "abcd"; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 2671 | int data_size = arraysize(data); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2672 | rtc::PacketOptions options; |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2673 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2674 | |
| 2675 | // Accept the connection to return the binding response, transition to |
| 2676 | // writable, and allow data to be sent. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2677 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2678 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2679 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2680 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2681 | |
| 2682 | // Ask the connection to update state as if enough time has passed to lose |
| 2683 | // full writability and 5 pings went unresponded to. We'll accomplish the |
| 2684 | // latter by sending pings but not pumping messages. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2685 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2686 | ch1.Ping(i); |
| 2687 | } |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2688 | int unreliable_timeout_delay = |
| 2689 | CONNECTION_WRITE_CONNECT_TIMEOUT + kMaxExpectedSimulatedRtt; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2690 | ch1.conn()->UpdateState(unreliable_timeout_delay); |
| 2691 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2692 | |
| 2693 | // Data should be able to be sent in this state. |
| 2694 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2695 | |
| 2696 | // And now allow the other side to process the pings and send binding |
| 2697 | // responses. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2698 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2699 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2700 | // Wait long enough for a full timeout (past however long we've already |
| 2701 | // waited). |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2702 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2703 | ch1.Ping(unreliable_timeout_delay + i); |
| 2704 | } |
| 2705 | ch1.conn()->UpdateState(unreliable_timeout_delay + CONNECTION_WRITE_TIMEOUT + |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2706 | kMaxExpectedSimulatedRtt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2707 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2708 | |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2709 | // Even if the connection has timed out, the Connection shouldn't block |
| 2710 | // the sending of data. |
| 2711 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2712 | |
| 2713 | ch1.Stop(); |
| 2714 | ch2.Stop(); |
| 2715 | } |
| 2716 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2717 | // Test writability states using the configured threshold value to replace |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2718 | // the default value given by `CONNECTION_WRITE_CONNECT_TIMEOUT` and |
| 2719 | // `CONNECTION_WRITE_CONNECT_FAILURES`. |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2720 | TEST_F(PortTest, TestWritableStateWithConfiguredThreshold) { |
| 2721 | rtc::ScopedFakeClock clock; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2722 | auto port1 = CreateUdpPort(kLocalAddr1); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2723 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2724 | auto port2 = CreateUdpPort(kLocalAddr2); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2725 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2726 | |
| 2727 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2728 | TestChannel ch1(std::move(port1)); |
| 2729 | TestChannel ch2(std::move(port2)); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2730 | |
| 2731 | // Acquire addresses. |
| 2732 | ch1.Start(); |
| 2733 | ch2.Start(); |
| 2734 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 2735 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
| 2736 | |
| 2737 | // Send a ping from src to dst. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2738 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2739 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2740 | ch1.Ping(); |
| 2741 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
| 2742 | |
| 2743 | // Accept the connection to return the binding response, transition to |
| 2744 | // writable, and allow data to be sent. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2745 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2746 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2747 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
| 2748 | |
| 2749 | ch1.conn()->set_unwritable_timeout(1000); |
| 2750 | ch1.conn()->set_unwritable_min_checks(3); |
| 2751 | // Send two checks. |
| 2752 | ch1.Ping(1); |
| 2753 | ch1.Ping(2); |
| 2754 | // We have not reached the timeout nor have we sent the minimum number of |
| 2755 | // checks to change the state to Unreliable. |
| 2756 | ch1.conn()->UpdateState(999); |
| 2757 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2758 | // We have not sent the minimum number of checks without responses. |
| 2759 | ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt); |
| 2760 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2761 | // Last ping after which the candidate pair should become Unreliable after |
| 2762 | // timeout. |
| 2763 | ch1.Ping(3); |
| 2764 | // We have not reached the timeout. |
| 2765 | ch1.conn()->UpdateState(999); |
| 2766 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2767 | // We should be in the state Unreliable now. |
| 2768 | ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt); |
| 2769 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2770 | |
| 2771 | ch1.Stop(); |
| 2772 | ch2.Stop(); |
| 2773 | } |
| 2774 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2775 | TEST_F(PortTest, TestTimeoutForNeverWritable) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2776 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2777 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2778 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2779 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2780 | |
| 2781 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2782 | TestChannel ch1(std::move(port1)); |
| 2783 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2784 | |
| 2785 | // Acquire addresses. |
| 2786 | ch1.Start(); |
| 2787 | ch2.Start(); |
| 2788 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2789 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2790 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2791 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2792 | |
| 2793 | // Attempt to go directly to write timeout. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2794 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2795 | ch1.Ping(i); |
| 2796 | } |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2797 | ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + kMaxExpectedSimulatedRtt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2798 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2799 | } |
| 2800 | |
| 2801 | // This test verifies the connection setup between ICEMODE_FULL |
| 2802 | // and ICEMODE_LITE. |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2803 | // In this test `ch1` behaves like FULL mode client and we have created |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2804 | // port which responds to the ping message just like LITE client. |
| 2805 | TEST_F(PortTest, TestIceLiteConnectivity) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2806 | auto ice_full_port = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2807 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 2808 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2809 | auto* ice_full_port_ptr = ice_full_port.get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2810 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2811 | auto ice_lite_port = CreateTestPort( |
| 2812 | kLocalAddr2, "rfrag", "rpass", cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2813 | // Setup TestChannel. This behaves like FULL mode client. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2814 | TestChannel ch1(std::move(ice_full_port)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2815 | ch1.SetIceMode(ICEMODE_FULL); |
| 2816 | |
| 2817 | // Start gathering candidates. |
| 2818 | ch1.Start(); |
| 2819 | ice_lite_port->PrepareAddress(); |
| 2820 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2821 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2822 | ASSERT_FALSE(ice_lite_port->Candidates().empty()); |
| 2823 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2824 | ch1.CreateConnection(GetCandidate(ice_lite_port.get())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2825 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2826 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2827 | |
| 2828 | // Send ping from full mode client. |
| 2829 | // This ping must not have USE_CANDIDATE_ATTR. |
| 2830 | ch1.Ping(); |
| 2831 | |
| 2832 | // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly |
| 2833 | // from port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2834 | ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout); |
| 2835 | IceMessage* msg = ice_full_port_ptr->last_stun_msg(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2836 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 2837 | |
| 2838 | // Respond with a BINDING-RESPONSE from litemode client. |
| 2839 | // NOTE: Ideally we should't create connection at this stage from lite |
| 2840 | // port, as it should be done only after receiving ping with USE_CANDIDATE. |
| 2841 | // But we need a connection to send a response message. |
Jonas Oreland | d003662 | 2019-11-29 15:58:13 +0100 | [diff] [blame] | 2842 | auto* con = ice_lite_port->CreateConnection( |
| 2843 | ice_full_port_ptr->Candidates()[0], cricket::Port::ORIGIN_MESSAGE); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2844 | std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2845 | con->SendStunBindingResponse(request.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2846 | |
| 2847 | // Feeding the respone message from litemode to the full mode connection. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2848 | ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(), |
| 2849 | ice_lite_port->last_stun_buf()->size(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2850 | /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2851 | // Verifying full mode connection becomes writable from the response. |
| 2852 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2853 | kDefaultTimeout); |
| 2854 | EXPECT_TRUE_WAIT(ch1.nominated(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2855 | |
| 2856 | // Clear existing stun messsages. Otherwise we will process old stun |
| 2857 | // message right after we send ping. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2858 | ice_full_port_ptr->Reset(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2859 | // Send ping. This must have USE_CANDIDATE_ATTR. |
| 2860 | ch1.Ping(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2861 | ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout); |
| 2862 | msg = ice_full_port_ptr->last_stun_msg(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2863 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 2864 | ch1.Stop(); |
| 2865 | } |
| 2866 | |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2867 | namespace { |
| 2868 | |
| 2869 | // Utility function for testing goog ping. |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 2870 | absl::optional<int> GetSupportedGoogPingVersion(const StunMessage* msg) { |
| 2871 | auto goog_misc = msg->GetUInt16List(STUN_ATTR_GOOG_MISC_INFO); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2872 | if (goog_misc == nullptr) { |
| 2873 | return absl::nullopt; |
| 2874 | } |
| 2875 | |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 2876 | if (msg->type() == STUN_BINDING_REQUEST) { |
| 2877 | if (goog_misc->Size() < |
| 2878 | static_cast<int>(cricket::IceGoogMiscInfoBindingRequestAttributeIndex:: |
| 2879 | SUPPORT_GOOG_PING_VERSION)) { |
| 2880 | return absl::nullopt; |
| 2881 | } |
| 2882 | |
| 2883 | return goog_misc->GetType( |
| 2884 | static_cast<int>(cricket::IceGoogMiscInfoBindingRequestAttributeIndex:: |
| 2885 | SUPPORT_GOOG_PING_VERSION)); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 2888 | if (msg->type() == STUN_BINDING_RESPONSE) { |
| 2889 | if (goog_misc->Size() < |
| 2890 | static_cast<int>(cricket::IceGoogMiscInfoBindingResponseAttributeIndex:: |
| 2891 | SUPPORT_GOOG_PING_VERSION)) { |
| 2892 | return absl::nullopt; |
| 2893 | } |
| 2894 | |
| 2895 | return goog_misc->GetType( |
| 2896 | static_cast<int>(cricket::IceGoogMiscInfoBindingResponseAttributeIndex:: |
| 2897 | SUPPORT_GOOG_PING_VERSION)); |
| 2898 | } |
| 2899 | return absl::nullopt; |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | } // namespace |
| 2903 | |
| 2904 | class GoogPingTest |
| 2905 | : public PortTest, |
| 2906 | public ::testing::WithParamInterface<std::pair<bool, bool>> {}; |
| 2907 | |
| 2908 | // This test verifies the announce/enable on/off behavior |
| 2909 | TEST_P(GoogPingTest, TestGoogPingAnnounceEnable) { |
| 2910 | IceFieldTrials trials; |
| 2911 | trials.announce_goog_ping = GetParam().first; |
| 2912 | trials.enable_goog_ping = GetParam().second; |
| 2913 | RTC_LOG(LS_INFO) << "Testing combination: " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 2914 | " announce: " |
| 2915 | << trials.announce_goog_ping |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2916 | << " enable:" << trials.enable_goog_ping; |
| 2917 | |
| 2918 | auto port1_unique = |
| 2919 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 2920 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 2921 | auto* port1 = port1_unique.get(); |
| 2922 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 2923 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 2924 | |
| 2925 | TestChannel ch1(std::move(port1_unique)); |
| 2926 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 2927 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 2928 | // This only makes test a bit shorter... |
| 2929 | ch1.SetIceMode(ICEMODE_LITE); |
| 2930 | // Start gathering candidates. |
| 2931 | ch1.Start(); |
| 2932 | port2->PrepareAddress(); |
| 2933 | |
| 2934 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 2935 | ASSERT_FALSE(port2->Candidates().empty()); |
| 2936 | |
| 2937 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 2938 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2939 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2940 | ch1.conn()->SetIceFieldTrials(&trials); |
| 2941 | |
| 2942 | // Send ping. |
| 2943 | ch1.Ping(); |
| 2944 | |
| 2945 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 2946 | const IceMessage* request1 = port1->last_stun_msg(); |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 2947 | |
| 2948 | ASSERT_EQ(trials.enable_goog_ping, |
| 2949 | GetSupportedGoogPingVersion(request1) && |
| 2950 | GetSupportedGoogPingVersion(request1) >= kGoogPingVersion); |
| 2951 | |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2952 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 2953 | cricket::Port::ORIGIN_MESSAGE); |
| 2954 | con->SetIceFieldTrials(&trials); |
| 2955 | |
| 2956 | con->SendStunBindingResponse(request1); |
| 2957 | |
| 2958 | // Then check the response matches the settings. |
| 2959 | const auto* response = port2->last_stun_msg(); |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 2960 | EXPECT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 2961 | EXPECT_EQ(trials.enable_goog_ping && trials.announce_goog_ping, |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2962 | GetSupportedGoogPingVersion(response) && |
| 2963 | GetSupportedGoogPingVersion(response) >= kGoogPingVersion); |
| 2964 | |
| 2965 | // Feeding the respone message back. |
| 2966 | ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), |
| 2967 | port2->last_stun_buf()->size(), |
| 2968 | /* packet_time_us */ -1); |
| 2969 | |
| 2970 | port1->Reset(); |
| 2971 | port2->Reset(); |
| 2972 | |
| 2973 | ch1.Ping(); |
| 2974 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 2975 | const IceMessage* request2 = port1->last_stun_msg(); |
| 2976 | |
| 2977 | // It should be a GOOG_PING if both of these are TRUE |
| 2978 | if (trials.announce_goog_ping && trials.enable_goog_ping) { |
| 2979 | ASSERT_EQ(request2->type(), GOOG_PING_REQUEST); |
| 2980 | con->SendGoogPingResponse(request2); |
| 2981 | } else { |
| 2982 | ASSERT_EQ(request2->type(), STUN_BINDING_REQUEST); |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 2983 | // If we sent a BINDING with enable, and we got a reply that |
| 2984 | // didn't contain announce, the next ping should not contain |
| 2985 | // the enable again. |
| 2986 | ASSERT_FALSE(GetSupportedGoogPingVersion(request2).has_value()); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2987 | con->SendStunBindingResponse(request2); |
| 2988 | } |
| 2989 | |
| 2990 | const auto* response2 = port2->last_stun_msg(); |
| 2991 | ASSERT_TRUE(response2 != nullptr); |
| 2992 | |
| 2993 | // It should be a GOOG_PING_RESPONSE if both of these are TRUE |
| 2994 | if (trials.announce_goog_ping && trials.enable_goog_ping) { |
| 2995 | ASSERT_EQ(response2->type(), GOOG_PING_RESPONSE); |
| 2996 | } else { |
| 2997 | ASSERT_EQ(response2->type(), STUN_BINDING_RESPONSE); |
| 2998 | } |
| 2999 | |
| 3000 | ch1.Stop(); |
| 3001 | } |
| 3002 | |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3003 | // This test if a someone send a STUN_BINDING with unsupported version |
| 3004 | // (kGoogPingVersion == 0) |
| 3005 | TEST_F(PortTest, TestGoogPingUnsupportedVersionInStunBinding) { |
| 3006 | IceFieldTrials trials; |
| 3007 | trials.announce_goog_ping = true; |
| 3008 | trials.enable_goog_ping = true; |
| 3009 | |
| 3010 | auto port1_unique = |
| 3011 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3012 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3013 | auto* port1 = port1_unique.get(); |
| 3014 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3015 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3016 | |
| 3017 | TestChannel ch1(std::move(port1_unique)); |
| 3018 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3019 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3020 | // This only makes test a bit shorter... |
| 3021 | ch1.SetIceMode(ICEMODE_LITE); |
| 3022 | // Start gathering candidates. |
| 3023 | ch1.Start(); |
| 3024 | port2->PrepareAddress(); |
| 3025 | |
| 3026 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3027 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3028 | |
| 3029 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3030 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3031 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3032 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3033 | |
| 3034 | // Send ping. |
| 3035 | ch1.Ping(); |
| 3036 | |
| 3037 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3038 | const IceMessage* request1 = port1->last_stun_msg(); |
| 3039 | |
| 3040 | ASSERT_TRUE(GetSupportedGoogPingVersion(request1) && |
| 3041 | GetSupportedGoogPingVersion(request1) >= kGoogPingVersion); |
| 3042 | |
| 3043 | // Modify the STUN message request1 to send GetSupportedGoogPingVersion == 0 |
| 3044 | auto modified_request1 = request1->Clone(); |
| 3045 | ASSERT_TRUE(modified_request1->RemoveAttribute(STUN_ATTR_GOOG_MISC_INFO) != |
| 3046 | nullptr); |
| 3047 | ASSERT_TRUE(modified_request1->RemoveAttribute(STUN_ATTR_MESSAGE_INTEGRITY) != |
| 3048 | nullptr); |
| 3049 | { |
| 3050 | auto list = |
| 3051 | StunAttribute::CreateUInt16ListAttribute(STUN_ATTR_GOOG_MISC_INFO); |
| 3052 | list->AddTypeAtIndex( |
| 3053 | static_cast<uint16_t>( |
| 3054 | cricket::IceGoogMiscInfoBindingRequestAttributeIndex:: |
| 3055 | SUPPORT_GOOG_PING_VERSION), |
| 3056 | /* version */ 0); |
| 3057 | modified_request1->AddAttribute(std::move(list)); |
| 3058 | modified_request1->AddMessageIntegrity("rpass"); |
| 3059 | } |
| 3060 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3061 | cricket::Port::ORIGIN_MESSAGE); |
| 3062 | con->SetIceFieldTrials(&trials); |
| 3063 | |
| 3064 | con->SendStunBindingResponse(modified_request1.get()); |
| 3065 | |
| 3066 | // Then check the response matches the settings. |
| 3067 | const auto* response = port2->last_stun_msg(); |
| 3068 | EXPECT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3069 | EXPECT_FALSE(GetSupportedGoogPingVersion(response)); |
| 3070 | |
| 3071 | ch1.Stop(); |
| 3072 | } |
| 3073 | |
| 3074 | // This test if a someone send a STUN_BINDING_RESPONSE with unsupported version |
| 3075 | // (kGoogPingVersion == 0) |
| 3076 | TEST_F(PortTest, TestGoogPingUnsupportedVersionInStunBindingResponse) { |
| 3077 | IceFieldTrials trials; |
| 3078 | trials.announce_goog_ping = true; |
| 3079 | trials.enable_goog_ping = true; |
| 3080 | |
| 3081 | auto port1_unique = |
| 3082 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3083 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3084 | auto* port1 = port1_unique.get(); |
| 3085 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3086 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3087 | |
| 3088 | TestChannel ch1(std::move(port1_unique)); |
| 3089 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3090 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3091 | // This only makes test a bit shorter... |
| 3092 | ch1.SetIceMode(ICEMODE_LITE); |
| 3093 | // Start gathering candidates. |
| 3094 | ch1.Start(); |
| 3095 | port2->PrepareAddress(); |
| 3096 | |
| 3097 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3098 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3099 | |
| 3100 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3101 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3102 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3103 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3104 | |
| 3105 | // Send ping. |
| 3106 | ch1.Ping(); |
| 3107 | |
| 3108 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3109 | const IceMessage* request1 = port1->last_stun_msg(); |
| 3110 | |
| 3111 | ASSERT_TRUE(GetSupportedGoogPingVersion(request1) && |
| 3112 | GetSupportedGoogPingVersion(request1) >= kGoogPingVersion); |
| 3113 | |
| 3114 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3115 | cricket::Port::ORIGIN_MESSAGE); |
| 3116 | con->SetIceFieldTrials(&trials); |
| 3117 | |
| 3118 | con->SendStunBindingResponse(request1); |
| 3119 | |
| 3120 | // Then check the response matches the settings. |
| 3121 | const auto* response = port2->last_stun_msg(); |
| 3122 | EXPECT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3123 | EXPECT_TRUE(GetSupportedGoogPingVersion(response)); |
| 3124 | |
| 3125 | // Modify the STUN message response to contain GetSupportedGoogPingVersion == |
| 3126 | // 0 |
| 3127 | auto modified_response = response->Clone(); |
| 3128 | ASSERT_TRUE(modified_response->RemoveAttribute(STUN_ATTR_GOOG_MISC_INFO) != |
| 3129 | nullptr); |
| 3130 | ASSERT_TRUE(modified_response->RemoveAttribute(STUN_ATTR_MESSAGE_INTEGRITY) != |
| 3131 | nullptr); |
| 3132 | ASSERT_TRUE(modified_response->RemoveAttribute(STUN_ATTR_FINGERPRINT) != |
| 3133 | nullptr); |
| 3134 | { |
| 3135 | auto list = |
| 3136 | StunAttribute::CreateUInt16ListAttribute(STUN_ATTR_GOOG_MISC_INFO); |
| 3137 | list->AddTypeAtIndex( |
| 3138 | static_cast<uint16_t>( |
| 3139 | cricket::IceGoogMiscInfoBindingResponseAttributeIndex:: |
| 3140 | SUPPORT_GOOG_PING_VERSION), |
| 3141 | /* version */ 0); |
| 3142 | modified_response->AddAttribute(std::move(list)); |
| 3143 | modified_response->AddMessageIntegrity("rpass"); |
| 3144 | modified_response->AddFingerprint(); |
| 3145 | } |
| 3146 | |
| 3147 | rtc::ByteBufferWriter buf; |
| 3148 | modified_response->Write(&buf); |
| 3149 | |
| 3150 | // Feeding the modified respone message back. |
| 3151 | ch1.conn()->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); |
| 3152 | |
| 3153 | port1->Reset(); |
| 3154 | port2->Reset(); |
| 3155 | |
| 3156 | ch1.Ping(); |
| 3157 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3158 | |
| 3159 | // This should now be a STUN_BINDING...without a kGoogPingVersion |
| 3160 | const IceMessage* request2 = port1->last_stun_msg(); |
| 3161 | EXPECT_EQ(request2->type(), STUN_BINDING_REQUEST); |
| 3162 | EXPECT_FALSE(GetSupportedGoogPingVersion(request2)); |
| 3163 | |
| 3164 | ch1.Stop(); |
| 3165 | } |
| 3166 | |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3167 | INSTANTIATE_TEST_SUITE_P(GoogPingTest, |
| 3168 | GoogPingTest, |
| 3169 | // test all combinations of <announce, enable> pairs. |
| 3170 | ::testing::Values(std::make_pair(false, false), |
| 3171 | std::make_pair(true, false), |
| 3172 | std::make_pair(false, true), |
| 3173 | std::make_pair(true, true))); |
| 3174 | |
| 3175 | // This test checks that a change in attributes falls back to STUN_BINDING |
| 3176 | TEST_F(PortTest, TestChangeInAttributeMakesGoogPingFallsbackToStunBinding) { |
| 3177 | IceFieldTrials trials; |
| 3178 | trials.announce_goog_ping = true; |
| 3179 | trials.enable_goog_ping = true; |
| 3180 | |
| 3181 | auto port1_unique = |
| 3182 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3183 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3184 | auto* port1 = port1_unique.get(); |
| 3185 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3186 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3187 | |
| 3188 | TestChannel ch1(std::move(port1_unique)); |
| 3189 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3190 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3191 | // This only makes test a bit shorter... |
| 3192 | ch1.SetIceMode(ICEMODE_LITE); |
| 3193 | // Start gathering candidates. |
| 3194 | ch1.Start(); |
| 3195 | port2->PrepareAddress(); |
| 3196 | |
| 3197 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3198 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3199 | |
| 3200 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3201 | ASSERT_TRUE(ch1.conn() != nullptr); |
| 3202 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3203 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3204 | |
| 3205 | // Send ping. |
| 3206 | ch1.Ping(); |
| 3207 | |
| 3208 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3209 | const IceMessage* msg = port1->last_stun_msg(); |
| 3210 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3211 | cricket::Port::ORIGIN_MESSAGE); |
| 3212 | con->SetIceFieldTrials(&trials); |
| 3213 | |
| 3214 | // Feed the message into the connection. |
| 3215 | con->SendStunBindingResponse(msg); |
| 3216 | |
| 3217 | // The check reply wrt to settings. |
| 3218 | const auto* response = port2->last_stun_msg(); |
| 3219 | ASSERT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3220 | ASSERT_TRUE(GetSupportedGoogPingVersion(response) >= kGoogPingVersion); |
| 3221 | |
| 3222 | // Feeding the respone message back. |
| 3223 | ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), |
| 3224 | port2->last_stun_buf()->size(), |
| 3225 | /* packet_time_us */ -1); |
| 3226 | |
| 3227 | port1->Reset(); |
| 3228 | port2->Reset(); |
| 3229 | |
| 3230 | ch1.Ping(); |
| 3231 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3232 | const IceMessage* msg2 = port1->last_stun_msg(); |
| 3233 | |
| 3234 | // It should be a GOOG_PING if both of these are TRUE |
| 3235 | ASSERT_EQ(msg2->type(), GOOG_PING_REQUEST); |
| 3236 | con->SendGoogPingResponse(msg2); |
| 3237 | |
| 3238 | const auto* response2 = port2->last_stun_msg(); |
| 3239 | ASSERT_TRUE(response2 != nullptr); |
| 3240 | |
| 3241 | // It should be a GOOG_PING_RESPONSE. |
| 3242 | ASSERT_EQ(response2->type(), GOOG_PING_RESPONSE); |
| 3243 | |
| 3244 | // And now the third ping. |
| 3245 | port1->Reset(); |
| 3246 | port2->Reset(); |
| 3247 | |
| 3248 | // Modify the message to be sent. |
| 3249 | ch1.conn()->set_use_candidate_attr(!ch1.conn()->use_candidate_attr()); |
| 3250 | |
| 3251 | ch1.Ping(); |
| 3252 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3253 | const IceMessage* msg3 = port1->last_stun_msg(); |
| 3254 | |
| 3255 | // It should be a STUN_BINDING_REQUEST |
| 3256 | ASSERT_EQ(msg3->type(), STUN_BINDING_REQUEST); |
| 3257 | |
| 3258 | ch1.Stop(); |
| 3259 | } |
| 3260 | |
| 3261 | // This test that an error response fall back to STUN_BINDING. |
| 3262 | TEST_F(PortTest, TestErrorResponseMakesGoogPingFallBackToStunBinding) { |
| 3263 | IceFieldTrials trials; |
| 3264 | trials.announce_goog_ping = true; |
| 3265 | trials.enable_goog_ping = true; |
| 3266 | |
| 3267 | auto port1_unique = |
| 3268 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3269 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3270 | auto* port1 = port1_unique.get(); |
| 3271 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3272 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3273 | |
| 3274 | TestChannel ch1(std::move(port1_unique)); |
| 3275 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3276 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3277 | // This only makes test a bit shorter... |
| 3278 | ch1.SetIceMode(ICEMODE_LITE); |
| 3279 | // Start gathering candidates. |
| 3280 | ch1.Start(); |
| 3281 | port2->PrepareAddress(); |
| 3282 | |
| 3283 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3284 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3285 | |
| 3286 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3287 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3288 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3289 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3290 | |
| 3291 | // Send ping. |
| 3292 | ch1.Ping(); |
| 3293 | |
| 3294 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3295 | const IceMessage* msg = port1->last_stun_msg(); |
| 3296 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3297 | cricket::Port::ORIGIN_MESSAGE); |
| 3298 | con->SetIceFieldTrials(&trials); |
| 3299 | |
| 3300 | // Feed the message into the connection. |
| 3301 | con->SendStunBindingResponse(msg); |
| 3302 | |
| 3303 | // The check reply wrt to settings. |
| 3304 | const auto* response = port2->last_stun_msg(); |
| 3305 | ASSERT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3306 | ASSERT_TRUE(GetSupportedGoogPingVersion(response) >= kGoogPingVersion); |
| 3307 | |
| 3308 | // Feeding the respone message back. |
| 3309 | ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), |
| 3310 | port2->last_stun_buf()->size(), |
| 3311 | /* packet_time_us */ -1); |
| 3312 | |
| 3313 | port1->Reset(); |
| 3314 | port2->Reset(); |
| 3315 | |
| 3316 | ch1.Ping(); |
| 3317 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3318 | const IceMessage* msg2 = port1->last_stun_msg(); |
| 3319 | |
| 3320 | // It should be a GOOG_PING. |
| 3321 | ASSERT_EQ(msg2->type(), GOOG_PING_REQUEST); |
| 3322 | con->SendGoogPingResponse(msg2); |
| 3323 | |
| 3324 | const auto* response2 = port2->last_stun_msg(); |
| 3325 | ASSERT_TRUE(response2 != nullptr); |
| 3326 | |
| 3327 | // It should be a GOOG_PING_RESPONSE. |
| 3328 | ASSERT_EQ(response2->type(), GOOG_PING_RESPONSE); |
| 3329 | |
| 3330 | // But rather than the RESPONSE...feedback an error. |
| 3331 | StunMessage error_response; |
| 3332 | error_response.SetType(GOOG_PING_ERROR_RESPONSE); |
| 3333 | error_response.SetTransactionID(response2->transaction_id()); |
| 3334 | error_response.AddMessageIntegrity32("rpass"); |
| 3335 | rtc::ByteBufferWriter buf; |
| 3336 | error_response.Write(&buf); |
| 3337 | |
| 3338 | ch1.conn()->OnReadPacket(buf.Data(), buf.Length(), |
| 3339 | /* packet_time_us */ -1); |
| 3340 | |
| 3341 | // And now the third ping...this should be a binding. |
| 3342 | port1->Reset(); |
| 3343 | port2->Reset(); |
| 3344 | |
| 3345 | ch1.Ping(); |
| 3346 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3347 | const IceMessage* msg3 = port1->last_stun_msg(); |
| 3348 | |
| 3349 | // It should be a STUN_BINDING_REQUEST |
| 3350 | ASSERT_EQ(msg3->type(), STUN_BINDING_REQUEST); |
| 3351 | |
| 3352 | ch1.Stop(); |
| 3353 | } |
| 3354 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3355 | // This test case verifies that both the controlling port and the controlled |
| 3356 | // port will time out after connectivity is lost, if they are not marked as |
| 3357 | // "keep alive until pruned." |
| 3358 | TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) { |
| 3359 | rtc::ScopedFakeClock clock; |
| 3360 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3361 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 3362 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3363 | port1->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3364 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3365 | port1->SetIceTiebreaker(kTiebreaker1); |
| 3366 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3367 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 3368 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3369 | port2->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3370 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3371 | port2->SetIceTiebreaker(kTiebreaker2); |
| 3372 | |
| 3373 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3374 | TestChannel ch1(std::move(port1)); |
| 3375 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3376 | |
| 3377 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 3378 | StartConnectAndStopChannels(&ch1, &ch2); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3379 | // After the connection is destroyed, the port will be destroyed because |
| 3380 | // none of them is marked as "keep alive until pruned. |
| 3381 | EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), 110, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3384 | // Test that if after all connection are destroyed, new connections are created |
| 3385 | // and destroyed again, ports won't be destroyed until a timeout period passes |
| 3386 | // after the last set of connections are all destroyed. |
| 3387 | TEST_F(PortTest, TestPortTimeoutAfterNewConnectionCreatedAndDestroyed) { |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3388 | rtc::ScopedFakeClock clock; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3389 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3390 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 3391 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3392 | port1->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3393 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3394 | port1->SetIceTiebreaker(kTiebreaker1); |
| 3395 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3396 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 3397 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3398 | port2->set_timeout_delay(timeout_delay); // milliseconds |
| 3399 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3400 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3401 | port2->SetIceTiebreaker(kTiebreaker2); |
| 3402 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3403 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3404 | TestChannel ch1(std::move(port1)); |
| 3405 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3406 | |
| 3407 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 3408 | StartConnectAndStopChannels(&ch1, &ch2); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3409 | SIMULATED_WAIT(ports_destroyed() > 0, 80, clock); |
| 3410 | EXPECT_EQ(0, ports_destroyed()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3411 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3412 | // Start the second set of connection and destroy them. |
| 3413 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3414 | ch2.CreateConnection(GetCandidate(ch1.port())); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3415 | ch1.Stop(); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3416 | ch2.Stop(); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3417 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3418 | SIMULATED_WAIT(ports_destroyed() > 0, 80, clock); |
| 3419 | EXPECT_EQ(0, ports_destroyed()); |
| 3420 | |
| 3421 | // The ports on both sides should be destroyed after timeout. |
| 3422 | EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 30, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3423 | } |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3424 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3425 | // This test case verifies that neither the controlling port nor the controlled |
| 3426 | // port will time out after connectivity is lost if they are marked as "keep |
| 3427 | // alive until pruned". They will time out after they are pruned. |
| 3428 | TEST_F(PortTest, TestPortNotTimeoutUntilPruned) { |
| 3429 | rtc::ScopedFakeClock clock; |
| 3430 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3431 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 3432 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3433 | port1->set_timeout_delay(timeout_delay); // milliseconds |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3434 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3435 | port1->SetIceTiebreaker(kTiebreaker1); |
| 3436 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3437 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 3438 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3439 | port2->set_timeout_delay(timeout_delay); // milliseconds |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3440 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3441 | port2->SetIceTiebreaker(kTiebreaker2); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3442 | // The connection must not be destroyed before a connection is attempted. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3443 | EXPECT_EQ(0, ports_destroyed()); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3444 | |
| 3445 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 3446 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 3447 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3448 | // Set up channels and keep the port alive. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3449 | TestChannel ch1(std::move(port1)); |
| 3450 | TestChannel ch2(std::move(port2)); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3451 | // Simulate a connection that succeeds, and then is destroyed. But ports |
| 3452 | // are kept alive. Ports won't be destroyed. |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3453 | StartConnectAndStopChannels(&ch1, &ch2); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3454 | ch1.port()->KeepAliveUntilPruned(); |
| 3455 | ch2.port()->KeepAliveUntilPruned(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3456 | SIMULATED_WAIT(ports_destroyed() > 0, 150, clock); |
| 3457 | EXPECT_EQ(0, ports_destroyed()); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3458 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3459 | // If they are pruned now, they will be destroyed right away. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3460 | ch1.port()->Prune(); |
| 3461 | ch2.port()->Prune(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3462 | // The ports on both sides should be destroyed after timeout. |
| 3463 | EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 1, clock); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3464 | } |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3465 | |
| 3466 | TEST_F(PortTest, TestSupportsProtocol) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3467 | auto udp_port = CreateUdpPort(kLocalAddr1); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3468 | EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3469 | EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 3470 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3471 | auto stun_port = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3472 | EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3473 | EXPECT_FALSE(stun_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 3474 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3475 | auto tcp_port = CreateTcpPort(kLocalAddr1); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3476 | EXPECT_TRUE(tcp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 3477 | EXPECT_TRUE(tcp_port->SupportsProtocol(SSLTCP_PROTOCOL_NAME)); |
| 3478 | EXPECT_FALSE(tcp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3479 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3480 | auto turn_port = |
| 3481 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3482 | EXPECT_TRUE(turn_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3483 | EXPECT_FALSE(turn_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 3484 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3485 | |
| 3486 | // Test that SetIceParameters updates the component, ufrag and password |
| 3487 | // on both the port itself and its candidates. |
| 3488 | TEST_F(PortTest, TestSetIceParameters) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3489 | auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1"); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3490 | port->PrepareAddress(); |
| 3491 | EXPECT_EQ(1UL, port->Candidates().size()); |
| 3492 | port->SetIceParameters(1, "ufrag2", "password2"); |
| 3493 | EXPECT_EQ(1, port->component()); |
| 3494 | EXPECT_EQ("ufrag2", port->username_fragment()); |
| 3495 | EXPECT_EQ("password2", port->password()); |
| 3496 | const Candidate& candidate = port->Candidates()[0]; |
| 3497 | EXPECT_EQ(1, candidate.component()); |
| 3498 | EXPECT_EQ("ufrag2", candidate.username()); |
| 3499 | EXPECT_EQ("password2", candidate.password()); |
| 3500 | } |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3501 | |
| 3502 | TEST_F(PortTest, TestAddConnectionWithSameAddress) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3503 | auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1"); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3504 | port->PrepareAddress(); |
| 3505 | EXPECT_EQ(1u, port->Candidates().size()); |
| 3506 | rtc::SocketAddress address("1.1.1.1", 5000); |
| 3507 | cricket::Candidate candidate(1, "udp", address, 0, "", "", "relay", 0, ""); |
| 3508 | cricket::Connection* conn1 = |
| 3509 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 3510 | cricket::Connection* conn_in_use = port->GetConnection(address); |
| 3511 | EXPECT_EQ(conn1, conn_in_use); |
| 3512 | EXPECT_EQ(0u, conn_in_use->remote_candidate().generation()); |
| 3513 | |
| 3514 | // Creating with a candidate with the same address again will get us a |
| 3515 | // different connection with the new candidate. |
| 3516 | candidate.set_generation(2); |
| 3517 | cricket::Connection* conn2 = |
| 3518 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 3519 | EXPECT_NE(conn1, conn2); |
| 3520 | conn_in_use = port->GetConnection(address); |
| 3521 | EXPECT_EQ(conn2, conn_in_use); |
| 3522 | EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
| 3523 | |
| 3524 | // Make sure the new connection was not deleted. |
| 3525 | rtc::Thread::Current()->ProcessMessages(300); |
| 3526 | EXPECT_TRUE(port->GetConnection(address) != nullptr); |
| 3527 | } |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 3528 | |
Jonas Oreland | ef60c2b | 2020-03-26 15:10:39 +0100 | [diff] [blame] | 3529 | // TODO(webrtc:11463) : Move Connection tests into separate unit test |
| 3530 | // splitting out shared test code as needed. |
| 3531 | |
| 3532 | class ConnectionTest : public PortTest { |
| 3533 | public: |
| 3534 | ConnectionTest() { |
| 3535 | lport_ = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 3536 | rport_ = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
| 3537 | lport_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3538 | lport_->SetIceTiebreaker(kTiebreaker1); |
| 3539 | rport_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3540 | rport_->SetIceTiebreaker(kTiebreaker2); |
| 3541 | |
| 3542 | lport_->PrepareAddress(); |
| 3543 | rport_->PrepareAddress(); |
| 3544 | } |
| 3545 | |
| 3546 | rtc::ScopedFakeClock clock_; |
| 3547 | int num_state_changes_ = 0; |
| 3548 | |
| 3549 | Connection* CreateConnection(IceRole role) { |
| 3550 | Connection* conn; |
| 3551 | if (role == cricket::ICEROLE_CONTROLLING) { |
| 3552 | conn = lport_->CreateConnection(rport_->Candidates()[0], |
| 3553 | Port::ORIGIN_MESSAGE); |
| 3554 | } else { |
| 3555 | conn = rport_->CreateConnection(lport_->Candidates()[0], |
| 3556 | Port::ORIGIN_MESSAGE); |
| 3557 | } |
| 3558 | conn->SignalStateChange.connect(this, |
| 3559 | &ConnectionTest::OnConnectionStateChange); |
| 3560 | return conn; |
| 3561 | } |
| 3562 | |
| 3563 | void SendPingAndCaptureReply(Connection* lconn, |
| 3564 | Connection* rconn, |
| 3565 | int64_t ms, |
| 3566 | rtc::BufferT<uint8_t>* reply) { |
| 3567 | TestPort* lport = |
| 3568 | lconn->PortForTest() == lport_.get() ? lport_.get() : rport_.get(); |
| 3569 | TestPort* rport = |
| 3570 | rconn->PortForTest() == rport_.get() ? rport_.get() : lport_.get(); |
| 3571 | lconn->Ping(rtc::TimeMillis()); |
| 3572 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 3573 | ASSERT_TRUE(lport->last_stun_buf()); |
| 3574 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 3575 | lport->last_stun_buf()->size(), |
| 3576 | /* packet_time_us */ -1); |
| 3577 | clock_.AdvanceTime(webrtc::TimeDelta::Millis(ms)); |
| 3578 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 3579 | ASSERT_TRUE(rport->last_stun_buf()); |
| 3580 | *reply = std::move(*rport->last_stun_buf()); |
| 3581 | } |
| 3582 | |
| 3583 | void SendPingAndReceiveResponse(Connection* lconn, |
| 3584 | Connection* rconn, |
| 3585 | int64_t ms) { |
| 3586 | rtc::BufferT<uint8_t> reply; |
| 3587 | SendPingAndCaptureReply(lconn, rconn, ms, &reply); |
| 3588 | lconn->OnReadPacket(reply.data<char>(), reply.size(), |
| 3589 | /* packet_time_us */ -1); |
| 3590 | } |
| 3591 | |
| 3592 | void OnConnectionStateChange(Connection* connection) { num_state_changes_++; } |
| 3593 | |
| 3594 | private: |
| 3595 | std::unique_ptr<TestPort> lport_; |
| 3596 | std::unique_ptr<TestPort> rport_; |
| 3597 | }; |
| 3598 | |
| 3599 | TEST_F(ConnectionTest, ConnectionForgetLearnedState) { |
| 3600 | Connection* lconn = CreateConnection(ICEROLE_CONTROLLING); |
| 3601 | Connection* rconn = CreateConnection(ICEROLE_CONTROLLED); |
| 3602 | |
| 3603 | EXPECT_FALSE(lconn->writable()); |
| 3604 | EXPECT_FALSE(lconn->receiving()); |
| 3605 | EXPECT_TRUE(std::isnan(lconn->GetRttEstimate().GetAverage())); |
| 3606 | EXPECT_EQ(lconn->GetRttEstimate().GetVariance(), |
| 3607 | std::numeric_limits<double>::infinity()); |
| 3608 | |
| 3609 | SendPingAndReceiveResponse(lconn, rconn, 10); |
| 3610 | |
| 3611 | EXPECT_TRUE(lconn->writable()); |
| 3612 | EXPECT_TRUE(lconn->receiving()); |
| 3613 | EXPECT_EQ(lconn->GetRttEstimate().GetAverage(), 10); |
| 3614 | EXPECT_EQ(lconn->GetRttEstimate().GetVariance(), |
| 3615 | std::numeric_limits<double>::infinity()); |
| 3616 | |
| 3617 | SendPingAndReceiveResponse(lconn, rconn, 11); |
| 3618 | |
| 3619 | EXPECT_TRUE(lconn->writable()); |
| 3620 | EXPECT_TRUE(lconn->receiving()); |
| 3621 | EXPECT_NEAR(lconn->GetRttEstimate().GetAverage(), 10, 0.5); |
| 3622 | EXPECT_LT(lconn->GetRttEstimate().GetVariance(), |
| 3623 | std::numeric_limits<double>::infinity()); |
| 3624 | |
| 3625 | lconn->ForgetLearnedState(); |
| 3626 | |
| 3627 | EXPECT_FALSE(lconn->writable()); |
| 3628 | EXPECT_FALSE(lconn->receiving()); |
| 3629 | EXPECT_TRUE(std::isnan(lconn->GetRttEstimate().GetAverage())); |
| 3630 | EXPECT_EQ(lconn->GetRttEstimate().GetVariance(), |
| 3631 | std::numeric_limits<double>::infinity()); |
| 3632 | } |
| 3633 | |
| 3634 | TEST_F(ConnectionTest, ConnectionForgetLearnedStateDiscardsPendingPings) { |
| 3635 | Connection* lconn = CreateConnection(ICEROLE_CONTROLLING); |
| 3636 | Connection* rconn = CreateConnection(ICEROLE_CONTROLLED); |
| 3637 | |
| 3638 | SendPingAndReceiveResponse(lconn, rconn, 10); |
| 3639 | |
| 3640 | EXPECT_TRUE(lconn->writable()); |
| 3641 | EXPECT_TRUE(lconn->receiving()); |
| 3642 | |
| 3643 | rtc::BufferT<uint8_t> reply; |
| 3644 | SendPingAndCaptureReply(lconn, rconn, 10, &reply); |
| 3645 | |
| 3646 | lconn->ForgetLearnedState(); |
| 3647 | |
| 3648 | EXPECT_FALSE(lconn->writable()); |
| 3649 | EXPECT_FALSE(lconn->receiving()); |
| 3650 | |
| 3651 | lconn->OnReadPacket(reply.data<char>(), reply.size(), |
| 3652 | /* packet_time_us */ -1); |
| 3653 | |
| 3654 | // That reply was discarded due to the ForgetLearnedState() while it was |
| 3655 | // outstanding. |
| 3656 | EXPECT_FALSE(lconn->writable()); |
| 3657 | EXPECT_FALSE(lconn->receiving()); |
| 3658 | |
| 3659 | // But sending a new ping and getting a reply works. |
| 3660 | SendPingAndReceiveResponse(lconn, rconn, 11); |
| 3661 | EXPECT_TRUE(lconn->writable()); |
| 3662 | EXPECT_TRUE(lconn->receiving()); |
| 3663 | } |
| 3664 | |
| 3665 | TEST_F(ConnectionTest, ConnectionForgetLearnedStateDoesNotTriggerStateChange) { |
| 3666 | Connection* lconn = CreateConnection(ICEROLE_CONTROLLING); |
| 3667 | Connection* rconn = CreateConnection(ICEROLE_CONTROLLED); |
| 3668 | |
| 3669 | EXPECT_EQ(num_state_changes_, 0); |
| 3670 | SendPingAndReceiveResponse(lconn, rconn, 10); |
| 3671 | |
| 3672 | EXPECT_TRUE(lconn->writable()); |
| 3673 | EXPECT_TRUE(lconn->receiving()); |
| 3674 | EXPECT_EQ(num_state_changes_, 2); |
| 3675 | |
| 3676 | lconn->ForgetLearnedState(); |
| 3677 | |
| 3678 | EXPECT_FALSE(lconn->writable()); |
| 3679 | EXPECT_FALSE(lconn->receiving()); |
| 3680 | EXPECT_EQ(num_state_changes_, 2); |
| 3681 | } |
| 3682 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 3683 | } // namespace cricket |