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