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