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( |
Sameer Vijaykar | 71434ae | 2022-08-11 14:32:16 +0200 | [diff] [blame] | 1762 | "WebRTC-IPv6NetworkResolutionFixes/Enabled/"); |
Diep Bui | 1e589eb | 2022-08-02 07:37:30 +0000 | [diff] [blame] | 1763 | FakePacketSocketFactory factory; |
| 1764 | std::unique_ptr<Port> ports[5]; |
| 1765 | SocketAddress addresses[5] = { |
| 1766 | SocketAddress("2001:db8::1", 0), SocketAddress("2001:db8::2", 0), |
| 1767 | SocketAddress("fe80::1", 0), SocketAddress("fe80::2", 0), |
| 1768 | SocketAddress("::1", 0)}; |
| 1769 | for (int i = 0; i < 5; i++) { |
| 1770 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
| 1771 | factory.set_next_udp_socket(socket); |
| 1772 | ports[i] = CreateUdpPortMultipleAddrs(addresses[i], kLinkLocalIPv6Addr, |
| 1773 | &factory, field_trials); |
| 1774 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1775 | socket->SignalAddressReady(socket, addresses[i]); |
| 1776 | ports[i]->PrepareAddress(); |
| 1777 | } |
| 1778 | |
| 1779 | Port* standard1 = ports[0].get(); |
| 1780 | Port* standard2 = ports[1].get(); |
| 1781 | Port* link_local1 = ports[2].get(); |
| 1782 | Port* link_local2 = ports[3].get(); |
| 1783 | Port* localhost = ports[4].get(); |
| 1784 | |
| 1785 | ExpectPortsCanConnect(false, link_local1, standard1); |
| 1786 | ExpectPortsCanConnect(false, standard1, link_local1); |
| 1787 | ExpectPortsCanConnect(false, link_local1, localhost); |
| 1788 | ExpectPortsCanConnect(false, localhost, link_local1); |
| 1789 | |
| 1790 | ExpectPortsCanConnect(true, link_local1, link_local2); |
| 1791 | ExpectPortsCanConnect(true, localhost, standard1); |
| 1792 | ExpectPortsCanConnect(true, standard1, localhost); |
| 1793 | ExpectPortsCanConnect(true, standard2, standard1); |
| 1794 | } |
| 1795 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1796 | // This test verifies DSCP value set through SetOption interface can be |
| 1797 | // get through DefaultDscpValue. |
| 1798 | TEST_F(PortTest, TestDefaultDscpValue) { |
| 1799 | int dscp; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1800 | auto udpport = CreateUdpPort(kLocalAddr1); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1801 | 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] | 1802 | EXPECT_EQ(0, udpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1803 | auto tcpport = CreateTcpPort(kLocalAddr1); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1804 | 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] | 1805 | EXPECT_EQ(0, tcpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1806 | EXPECT_EQ(rtc::DSCP_AF31, dscp); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1807 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1808 | 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] | 1809 | EXPECT_EQ(0, stunport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1810 | EXPECT_EQ(rtc::DSCP_AF41, dscp); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1811 | auto turnport1 = |
| 1812 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1813 | // Socket is created in PrepareAddress. |
| 1814 | turnport1->PrepareAddress(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1815 | 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] | 1816 | EXPECT_EQ(0, turnport1->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1817 | EXPECT_EQ(rtc::DSCP_CS7, dscp); |
| 1818 | // This will verify correct value returned without the socket. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1819 | auto turnport2 = |
| 1820 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1821 | 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] | 1822 | EXPECT_EQ(0, turnport2->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1823 | EXPECT_EQ(rtc::DSCP_CS6, dscp); |
| 1824 | } |
| 1825 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1826 | // Test sending STUN messages. |
| 1827 | TEST_F(PortTest, TestSendStunMessage) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1828 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1829 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1830 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1831 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1832 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1833 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1834 | |
| 1835 | // Send a fake ping from lport to rport. |
| 1836 | lport->PrepareAddress(); |
| 1837 | rport->PrepareAddress(); |
| 1838 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1839 | Connection* lconn = |
| 1840 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1841 | Connection* rconn = |
| 1842 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1843 | lconn->Ping(0); |
| 1844 | |
| 1845 | // Check that it's a proper BINDING-REQUEST. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1846 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1847 | IceMessage* msg = lport->last_stun_msg(); |
| 1848 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1849 | EXPECT_FALSE(msg->IsLegacy()); |
| 1850 | const StunByteStringAttribute* username_attr = |
| 1851 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 1852 | ASSERT_TRUE(username_attr != NULL); |
| 1853 | const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 1854 | ASSERT_TRUE(priority_attr != NULL); |
| 1855 | EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value()); |
Tommi | e83500e | 2022-06-03 14:28:59 +0200 | [diff] [blame] | 1856 | EXPECT_EQ("rfrag:lfrag", username_attr->string_view()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1857 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 1858 | EXPECT_EQ(StunMessage::IntegrityStatus::kIntegrityOk, |
| 1859 | msg->ValidateMessageIntegrity("rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1860 | const StunUInt64Attribute* ice_controlling_attr = |
| 1861 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1862 | ASSERT_TRUE(ice_controlling_attr != NULL); |
| 1863 | EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value()); |
| 1864 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1865 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 1866 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1867 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1868 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1869 | |
| 1870 | // Request should not include ping count. |
| 1871 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1872 | |
| 1873 | // Save a copy of the BINDING-REQUEST for use below. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1874 | std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1875 | |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1876 | // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE. |
| 1877 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1878 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1879 | msg = rport->last_stun_msg(); |
| 1880 | ASSERT_TRUE(msg != NULL); |
| 1881 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1882 | // Received a BINDING-RESPONSE. |
| 1883 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1884 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1885 | // Verify the STUN Stats. |
| 1886 | EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total); |
| 1887 | EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response); |
| 1888 | EXPECT_EQ(1U, lconn->stats().recv_ping_responses); |
| 1889 | EXPECT_EQ(1U, rconn->stats().recv_ping_requests); |
| 1890 | EXPECT_EQ(1U, rconn->stats().sent_ping_responses); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1891 | |
| 1892 | EXPECT_FALSE(msg->IsLegacy()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1893 | const StunAddressAttribute* addr_attr = |
| 1894 | msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1895 | ASSERT_TRUE(addr_attr != NULL); |
| 1896 | EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress()); |
| 1897 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 1898 | EXPECT_EQ(StunMessage::IntegrityStatus::kIntegrityOk, |
| 1899 | msg->ValidateMessageIntegrity("rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1900 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1901 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1902 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1903 | // No USERNAME or PRIORITY in ICE responses. |
| 1904 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1905 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1906 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL); |
| 1907 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL); |
| 1908 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1909 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1910 | |
| 1911 | // Response should not include ping count. |
| 1912 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1913 | |
| 1914 | // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life, |
| 1915 | // but we can do it here. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1916 | rport->SendBindingErrorResponse( |
| 1917 | request.get(), lport->Candidates()[0].address(), STUN_ERROR_SERVER_ERROR, |
| 1918 | STUN_ERROR_REASON_SERVER_ERROR); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1919 | msg = rport->last_stun_msg(); |
| 1920 | ASSERT_TRUE(msg != NULL); |
| 1921 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1922 | EXPECT_FALSE(msg->IsLegacy()); |
| 1923 | const StunErrorCodeAttribute* error_attr = msg->GetErrorCode(); |
| 1924 | ASSERT_TRUE(error_attr != NULL); |
| 1925 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code()); |
| 1926 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason()); |
| 1927 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 1928 | EXPECT_EQ(StunMessage::IntegrityStatus::kIntegrityOk, |
| 1929 | msg->ValidateMessageIntegrity("rpass")); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1930 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1931 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1932 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1933 | // No USERNAME with ICE. |
| 1934 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1935 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1936 | |
| 1937 | // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED |
| 1938 | // and (incremented) RETRANSMIT_COUNT attributes. |
| 1939 | rport->Reset(); |
| 1940 | rport->set_send_retransmit_count_attribute(true); |
| 1941 | rconn->Ping(0); |
| 1942 | rconn->Ping(0); |
| 1943 | rconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1944 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1945 | msg = rport->last_stun_msg(); |
| 1946 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1947 | const StunUInt64Attribute* ice_controlled_attr = |
| 1948 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); |
| 1949 | ASSERT_TRUE(ice_controlled_attr != NULL); |
| 1950 | EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value()); |
| 1951 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1952 | |
| 1953 | // Request should include ping count. |
| 1954 | const StunUInt32Attribute* retransmit_attr = |
| 1955 | msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1956 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1957 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1958 | |
| 1959 | // Respond with a BINDING-RESPONSE. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1960 | request = CopyStunMessage(*msg); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1961 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1962 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1963 | msg = lport->last_stun_msg(); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1964 | // Receive the BINDING-RESPONSE. |
| 1965 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1966 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1967 | |
| 1968 | // Verify the Stun ping stats. |
| 1969 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total); |
| 1970 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); |
| 1971 | EXPECT_EQ(1U, rconn->stats().recv_ping_responses); |
| 1972 | EXPECT_EQ(1U, lconn->stats().sent_ping_responses); |
| 1973 | EXPECT_EQ(1U, lconn->stats().recv_ping_requests); |
| 1974 | // Ping after receiver the first response |
| 1975 | rconn->Ping(0); |
| 1976 | rconn->Ping(0); |
| 1977 | EXPECT_EQ(5U, rconn->stats().sent_ping_requests_total); |
| 1978 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Response should include same ping count. |
| 1981 | retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1982 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1983 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1984 | } |
| 1985 | |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1986 | TEST_F(PortTest, TestNomination) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1987 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1988 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1989 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1990 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1991 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1992 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1993 | |
| 1994 | lport->PrepareAddress(); |
| 1995 | rport->PrepareAddress(); |
| 1996 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1997 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1998 | Connection* lconn = |
| 1999 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2000 | Connection* rconn = |
| 2001 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 2002 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2003 | // `lconn` is controlling, `rconn` is controlled. |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 2004 | uint32_t nomination = 1234; |
| 2005 | lconn->set_nomination(nomination); |
| 2006 | |
| 2007 | EXPECT_FALSE(lconn->nominated()); |
| 2008 | EXPECT_FALSE(rconn->nominated()); |
| 2009 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 2010 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 2011 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2012 | // Send ping (including the nomination value) from `lconn` to `rconn`. This |
| 2013 | // should set the remote nomination of `rconn`. |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 2014 | lconn->Ping(0); |
| 2015 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 2016 | ASSERT_TRUE(lport->last_stun_buf()); |
| 2017 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2018 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 2019 | EXPECT_EQ(nomination, rconn->remote_nomination()); |
| 2020 | EXPECT_FALSE(lconn->nominated()); |
| 2021 | EXPECT_TRUE(rconn->nominated()); |
| 2022 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 2023 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 2024 | |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2025 | // This should result in an acknowledgment sent back from `rconn` to `lconn`, |
| 2026 | // updating the acknowledged nomination of `lconn`. |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 2027 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 2028 | ASSERT_TRUE(rport->last_stun_buf()); |
| 2029 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2030 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 2031 | EXPECT_EQ(nomination, lconn->acked_nomination()); |
| 2032 | EXPECT_TRUE(lconn->nominated()); |
| 2033 | EXPECT_TRUE(rconn->nominated()); |
| 2034 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 2035 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 2036 | } |
| 2037 | |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 2038 | TEST_F(PortTest, TestRoundTripTime) { |
| 2039 | rtc::ScopedFakeClock clock; |
| 2040 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2041 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 2042 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 2043 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2044 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2045 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2046 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2047 | |
| 2048 | lport->PrepareAddress(); |
| 2049 | rport->PrepareAddress(); |
| 2050 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2051 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2052 | Connection* lconn = |
| 2053 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2054 | Connection* rconn = |
| 2055 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 2056 | |
| 2057 | EXPECT_EQ(0u, lconn->stats().total_round_trip_time_ms); |
| 2058 | EXPECT_FALSE(lconn->stats().current_round_trip_time_ms); |
| 2059 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2060 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 2061 | 10); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 2062 | EXPECT_EQ(10u, lconn->stats().total_round_trip_time_ms); |
| 2063 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 2064 | EXPECT_EQ(10u, *lconn->stats().current_round_trip_time_ms); |
| 2065 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2066 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 2067 | 20); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 2068 | EXPECT_EQ(30u, lconn->stats().total_round_trip_time_ms); |
| 2069 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 2070 | EXPECT_EQ(20u, *lconn->stats().current_round_trip_time_ms); |
| 2071 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2072 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 2073 | 30); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 2074 | EXPECT_EQ(60u, lconn->stats().total_round_trip_time_ms); |
| 2075 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 2076 | EXPECT_EQ(30u, *lconn->stats().current_round_trip_time_ms); |
| 2077 | } |
| 2078 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2079 | TEST_F(PortTest, TestUseCandidateAttribute) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2080 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 2081 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2082 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2083 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2084 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2085 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2086 | |
| 2087 | // Send a fake ping from lport to rport. |
| 2088 | lport->PrepareAddress(); |
| 2089 | rport->PrepareAddress(); |
| 2090 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2091 | Connection* lconn = |
| 2092 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2093 | lconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2094 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2095 | IceMessage* msg = lport->last_stun_msg(); |
| 2096 | const StunUInt64Attribute* ice_controlling_attr = |
| 2097 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 2098 | ASSERT_TRUE(ice_controlling_attr != NULL); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2099 | const StunByteStringAttribute* use_candidate_attr = |
| 2100 | msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2101 | ASSERT_TRUE(use_candidate_attr != NULL); |
| 2102 | } |
| 2103 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2104 | // Tests that when the network type changes, the network cost of the port will |
| 2105 | // change, the network cost of the local candidates will change. Also tests that |
| 2106 | // the remote network costs are updated with the stun binding requests. |
| 2107 | TEST_F(PortTest, TestNetworkCostChange) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2108 | rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2109 | auto lport = CreateTestPort(test_network, "lfrag", "lpass"); |
| 2110 | auto rport = CreateTestPort(test_network, "rfrag", "rpass"); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2111 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2112 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2113 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2114 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2115 | lport->PrepareAddress(); |
| 2116 | rport->PrepareAddress(); |
| 2117 | |
| 2118 | // Default local port cost is rtc::kNetworkCostUnknown. |
| 2119 | EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); |
| 2120 | ASSERT_TRUE(!lport->Candidates().empty()); |
| 2121 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 2122 | EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); |
| 2123 | } |
| 2124 | |
| 2125 | // Change the network type to wifi. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2126 | test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2127 | EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
| 2128 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 2129 | EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
| 2130 | } |
| 2131 | |
| 2132 | // Add a connection and then change the network type. |
| 2133 | Connection* lconn = |
| 2134 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2135 | // Change the network type to cellular. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2136 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2137 | EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
| 2138 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 2139 | EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
| 2140 | } |
| 2141 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2142 | test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2143 | Connection* rconn = |
| 2144 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2145 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2146 | lconn->Ping(0); |
| 2147 | // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
| 2148 | // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
| 2149 | // message is handled in rconn, The rconn's remote candidate will have cost |
| 2150 | // rtc::kNetworkCostHigh; |
| 2151 | EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2152 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2153 | IceMessage* msg = lport->last_stun_msg(); |
| 2154 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 2155 | // Pass the binding request to rport. |
| 2156 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2157 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2158 | // 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] | 2159 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2160 | EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); |
| 2161 | } |
| 2162 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2163 | TEST_F(PortTest, TestNetworkInfoAttribute) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2164 | rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2165 | auto lport = CreateTestPort(test_network, "lfrag", "lpass"); |
| 2166 | auto rport = CreateTestPort(test_network, "rfrag", "rpass"); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2167 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2168 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2169 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2170 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2171 | |
| 2172 | uint16_t lnetwork_id = 9; |
Niels Möller | e0c6bdf | 2022-03-24 15:18:02 +0100 | [diff] [blame] | 2173 | test_network->set_id(lnetwork_id); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2174 | // Send a fake ping from lport to rport. |
| 2175 | lport->PrepareAddress(); |
| 2176 | rport->PrepareAddress(); |
| 2177 | Connection* lconn = |
| 2178 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2179 | lconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2180 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2181 | IceMessage* msg = lport->last_stun_msg(); |
| 2182 | const StunUInt32Attribute* network_info_attr = |
Jonas Oreland | fa54364 | 2020-09-16 10:44:54 +0200 | [diff] [blame] | 2183 | msg->GetUInt32(STUN_ATTR_GOOG_NETWORK_INFO); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2184 | ASSERT_TRUE(network_info_attr != NULL); |
| 2185 | uint32_t network_info = network_info_attr->value(); |
| 2186 | EXPECT_EQ(lnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2187 | // Default network has unknown type and cost kNetworkCostUnknown. |
| 2188 | EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2189 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2190 | // Set the network type to be cellular so its cost will be kNetworkCostHigh. |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2191 | // Send a fake ping from rport to lport. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 2192 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2193 | uint16_t rnetwork_id = 8; |
Niels Möller | e0c6bdf | 2022-03-24 15:18:02 +0100 | [diff] [blame] | 2194 | test_network->set_id(rnetwork_id); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2195 | Connection* rconn = |
| 2196 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2197 | rconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2198 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2199 | msg = rport->last_stun_msg(); |
Jonas Oreland | fa54364 | 2020-09-16 10:44:54 +0200 | [diff] [blame] | 2200 | network_info_attr = msg->GetUInt32(STUN_ATTR_GOOG_NETWORK_INFO); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2201 | ASSERT_TRUE(network_info_attr != NULL); |
| 2202 | network_info = network_info_attr->value(); |
| 2203 | EXPECT_EQ(rnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 2204 | EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2207 | // Test handling STUN messages. |
| 2208 | TEST_F(PortTest, TestHandleStunMessage) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2209 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2210 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2211 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2212 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2213 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2214 | rtc::SocketAddress addr(kLocalAddr1); |
| 2215 | std::string username; |
| 2216 | |
| 2217 | // BINDING-REQUEST from local to remote with valid ICE username, |
| 2218 | // MESSAGE-INTEGRITY, and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2219 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2220 | in_msg->AddMessageIntegrity("rpass"); |
| 2221 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2222 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2223 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2224 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2225 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2226 | EXPECT_EQ("lfrag", username); |
| 2227 | |
| 2228 | // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2229 | in_msg = CreateStunMessage(STUN_BINDING_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2230 | in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2231 | STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2232 | in_msg->AddMessageIntegrity("rpass"); |
| 2233 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2234 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2235 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2236 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2237 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2238 | EXPECT_EQ("", username); |
| 2239 | |
| 2240 | // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2241 | in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2242 | in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2243 | STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2244 | STUN_ERROR_REASON_SERVER_ERROR)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2245 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2246 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2247 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2248 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2249 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2250 | EXPECT_EQ("", username); |
| 2251 | ASSERT_TRUE(out_msg->GetErrorCode() != NULL); |
| 2252 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); |
| 2253 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2254 | out_msg->GetErrorCode()->reason()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
guoweis | d12140a | 2015-09-10 13:32:11 -0700 | [diff] [blame] | 2257 | // Tests handling of ICE binding requests with missing or incorrect usernames. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2258 | TEST_F(PortTest, TestHandleStunMessageBadUsername) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2259 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2260 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2261 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2262 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2263 | rtc::SocketAddress addr(kLocalAddr1); |
| 2264 | std::string username; |
| 2265 | |
| 2266 | // BINDING-REQUEST with no username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2267 | in_msg = CreateStunMessage(STUN_BINDING_REQUEST); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2268 | in_msg->AddMessageIntegrity("rpass"); |
| 2269 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2270 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2271 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2272 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2273 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2274 | EXPECT_EQ("", username); |
| 2275 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2276 | |
| 2277 | // BINDING-REQUEST with empty username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2278 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, ""); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2279 | in_msg->AddMessageIntegrity("rpass"); |
| 2280 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2281 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2282 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2283 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2284 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2285 | EXPECT_EQ("", username); |
| 2286 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2287 | |
| 2288 | // BINDING-REQUEST with too-short username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2289 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2290 | in_msg->AddMessageIntegrity("rpass"); |
| 2291 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2292 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2293 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2294 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2295 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2296 | EXPECT_EQ("", username); |
| 2297 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2298 | |
| 2299 | // BINDING-REQUEST with reversed username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2300 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "lfrag:rfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2301 | in_msg->AddMessageIntegrity("rpass"); |
| 2302 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2303 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2304 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2305 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2306 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2307 | EXPECT_EQ("", username); |
| 2308 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2309 | |
| 2310 | // BINDING-REQUEST with garbage username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2311 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "abcd:efgh"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2312 | in_msg->AddMessageIntegrity("rpass"); |
| 2313 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2314 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2315 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2316 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2317 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2318 | EXPECT_EQ("", username); |
| 2319 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2320 | } |
| 2321 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2322 | // Test handling STUN messages with missing or malformed M-I. |
| 2323 | TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2324 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2325 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2326 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2327 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2328 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2329 | rtc::SocketAddress addr(kLocalAddr1); |
| 2330 | std::string username; |
| 2331 | |
| 2332 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2333 | // FINGERPRINT, but no MESSAGE-INTEGRITY. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2334 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2335 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2336 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2337 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2338 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2339 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2340 | EXPECT_EQ("", username); |
| 2341 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2342 | |
| 2343 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2344 | // FINGERPRINT, but invalid MESSAGE-INTEGRITY. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2345 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2346 | in_msg->AddMessageIntegrity("invalid"); |
| 2347 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2348 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2349 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2350 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2351 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2352 | EXPECT_EQ("", username); |
| 2353 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2354 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 2355 | // TODO(?): BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2356 | // by the Connection, not the Port, since they require the remote username. |
| 2357 | // Change this test to pass in data via Connection::OnReadPacket instead. |
| 2358 | } |
| 2359 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2360 | // Test handling STUN messages with missing or malformed FINGERPRINT. |
| 2361 | TEST_F(PortTest, TestHandleStunMessageBadFingerprint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2362 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2363 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2364 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2365 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2366 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2367 | rtc::SocketAddress addr(kLocalAddr1); |
| 2368 | std::string username; |
| 2369 | |
| 2370 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2371 | // MESSAGE-INTEGRITY, but no FINGERPRINT; GetStunMessage should fail. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2372 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2373 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2374 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2375 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2376 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2377 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2378 | |
| 2379 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2380 | in_msg->AddFingerprint(); |
Tommi | 408143d | 2022-06-01 15:29:31 +0200 | [diff] [blame] | 2381 | in_msg->SetTransactionIdForTesting("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2382 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2383 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2384 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2385 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2386 | |
| 2387 | // Valid BINDING-RESPONSE, except no FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2388 | in_msg = CreateStunMessage(STUN_BINDING_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2389 | in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2390 | STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2391 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2392 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2393 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2394 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2395 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2396 | |
| 2397 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2398 | in_msg->AddFingerprint(); |
Tommi | 408143d | 2022-06-01 15:29:31 +0200 | [diff] [blame] | 2399 | in_msg->SetTransactionIdForTesting("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2400 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2401 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2402 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2403 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2404 | |
| 2405 | // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2406 | in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2407 | in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2408 | STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2409 | STUN_ERROR_REASON_SERVER_ERROR)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2410 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2411 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2412 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2413 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2414 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2415 | |
| 2416 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2417 | in_msg->AddFingerprint(); |
Tommi | 408143d | 2022-06-01 15:29:31 +0200 | [diff] [blame] | 2418 | in_msg->SetTransactionIdForTesting("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2419 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2420 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2421 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2422 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2423 | } |
| 2424 | |
Taylor Brandstetter | fb4351b | 2020-03-23 16:00:31 -0700 | [diff] [blame] | 2425 | // Test handling a STUN message with unknown attributes in the |
| 2426 | // "comprehension-required" range. Should respond with an error with the |
| 2427 | // unknown attributes' IDs. |
| 2428 | TEST_F(PortTest, |
| 2429 | TestHandleStunRequestWithUnknownComprehensionRequiredAttribute) { |
| 2430 | // Our port will act as the "remote" port. |
| 2431 | std::unique_ptr<TestPort> port(CreateTestPort(kLocalAddr2, "rfrag", "rpass")); |
| 2432 | |
| 2433 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2434 | auto buf = std::make_unique<ByteBufferWriter>(); |
| 2435 | rtc::SocketAddress addr(kLocalAddr1); |
| 2436 | std::string username; |
| 2437 | |
| 2438 | // Build ordinary message with valid ufrag/pass. |
| 2439 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
| 2440 | in_msg->AddMessageIntegrity("rpass"); |
| 2441 | // Add a couple attributes with ID in comprehension-required range. |
| 2442 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0x7777)); |
| 2443 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0x4567)); |
| 2444 | // ... And one outside the range. |
| 2445 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0xdead)); |
| 2446 | in_msg->AddFingerprint(); |
| 2447 | WriteStunMessage(*in_msg, buf.get()); |
| 2448 | ASSERT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2449 | &username)); |
| 2450 | IceMessage* error_response = port->last_stun_msg(); |
| 2451 | ASSERT_NE(nullptr, error_response); |
| 2452 | |
| 2453 | // Verify that the "unknown attribute" error response has the right error |
| 2454 | // code, and includes an attribute that lists out the unrecognized attribute |
| 2455 | // types. |
| 2456 | EXPECT_EQ(STUN_ERROR_UNKNOWN_ATTRIBUTE, error_response->GetErrorCodeValue()); |
| 2457 | const StunUInt16ListAttribute* unknown_attributes = |
| 2458 | error_response->GetUnknownAttributes(); |
| 2459 | ASSERT_NE(nullptr, unknown_attributes); |
| 2460 | ASSERT_EQ(2u, unknown_attributes->Size()); |
| 2461 | EXPECT_EQ(0x7777, unknown_attributes->GetType(0)); |
| 2462 | EXPECT_EQ(0x4567, unknown_attributes->GetType(1)); |
| 2463 | } |
| 2464 | |
| 2465 | // Similar to the above, but with a response instead of a request. In this |
| 2466 | // case the response should just be ignored and transaction treated is failed. |
| 2467 | TEST_F(PortTest, |
| 2468 | TestHandleStunResponseWithUnknownComprehensionRequiredAttribute) { |
| 2469 | // Generic setup. |
| 2470 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 2471 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2472 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
| 2473 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2474 | lport->PrepareAddress(); |
| 2475 | rport->PrepareAddress(); |
| 2476 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2477 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2478 | Connection* lconn = |
| 2479 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2480 | Connection* rconn = |
| 2481 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2482 | |
| 2483 | // Send request. |
| 2484 | lconn->Ping(0); |
| 2485 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
| 2486 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 2487 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
| 2488 | |
| 2489 | // Intercept request and add comprehension required attribute. |
| 2490 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
| 2491 | auto modified_response = rport->last_stun_msg()->Clone(); |
| 2492 | modified_response->AddAttribute(StunAttribute::CreateUInt32(0x7777)); |
| 2493 | modified_response->RemoveAttribute(STUN_ATTR_FINGERPRINT); |
| 2494 | modified_response->AddFingerprint(); |
| 2495 | ByteBufferWriter buf; |
| 2496 | WriteStunMessage(*modified_response, &buf); |
| 2497 | lconn->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); |
| 2498 | // Response should have been ignored, leaving us unwritable still. |
| 2499 | EXPECT_FALSE(lconn->writable()); |
| 2500 | } |
| 2501 | |
| 2502 | // Similar to the above, but with an indication. As with a response, it should |
| 2503 | // just be ignored. |
| 2504 | TEST_F(PortTest, |
| 2505 | TestHandleStunIndicationWithUnknownComprehensionRequiredAttribute) { |
| 2506 | // Generic set up. |
| 2507 | auto lport = CreateTestPort(kLocalAddr2, "lfrag", "lpass"); |
| 2508 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2509 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
| 2510 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2511 | lport->PrepareAddress(); |
| 2512 | rport->PrepareAddress(); |
| 2513 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2514 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2515 | Connection* lconn = |
| 2516 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2517 | |
| 2518 | // Generate indication with comprehension required attribute and verify it |
| 2519 | // doesn't update last_ping_received. |
| 2520 | auto in_msg = CreateStunMessage(STUN_BINDING_INDICATION); |
| 2521 | in_msg->AddAttribute(StunAttribute::CreateUInt32(0x7777)); |
| 2522 | in_msg->AddFingerprint(); |
| 2523 | ByteBufferWriter buf; |
| 2524 | WriteStunMessage(*in_msg, &buf); |
| 2525 | lconn->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); |
| 2526 | EXPECT_EQ(0u, lconn->last_ping_received()); |
| 2527 | } |
| 2528 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2529 | // Test handling of STUN binding indication messages . STUN binding |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2530 | // indications are allowed only to the connection which is in read mode. |
| 2531 | TEST_F(PortTest, TestHandleStunBindingIndication) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2532 | auto lport = CreateTestPort(kLocalAddr2, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2533 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2534 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2535 | |
| 2536 | // Verifying encoding and decoding STUN indication message. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2537 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2538 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2539 | rtc::SocketAddress addr(kLocalAddr1); |
| 2540 | std::string username; |
| 2541 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2542 | in_msg = CreateStunMessage(STUN_BINDING_INDICATION); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2543 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2544 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2545 | EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2546 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2547 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2548 | EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION); |
| 2549 | EXPECT_EQ("", username); |
| 2550 | |
| 2551 | // Verify connection can handle STUN indication and updates |
| 2552 | // last_ping_received. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2553 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2554 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2555 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2556 | |
| 2557 | lport->PrepareAddress(); |
| 2558 | rport->PrepareAddress(); |
| 2559 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2560 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2561 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2562 | Connection* lconn = |
| 2563 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2564 | Connection* rconn = |
| 2565 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2566 | rconn->Ping(0); |
| 2567 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2568 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2569 | IceMessage* msg = rport->last_stun_msg(); |
| 2570 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 2571 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2572 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2573 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2574 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2575 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2576 | int64_t last_ping_received1 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2577 | |
| 2578 | // Adding a delay of 100ms. |
| 2579 | rtc::Thread::Current()->ProcessMessages(100); |
| 2580 | // Pinging lconn using stun indication message. |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2581 | lconn->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2582 | int64_t last_ping_received2 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2583 | EXPECT_GT(last_ping_received2, last_ping_received1); |
| 2584 | } |
| 2585 | |
| 2586 | TEST_F(PortTest, TestComputeCandidatePriority) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2587 | auto port = CreateTestPort(kLocalAddr1, "name", "pass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2588 | port->set_type_preference(90); |
| 2589 | port->set_component(177); |
| 2590 | port->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2591 | port->AddCandidateAddress(SocketAddress("2001:db8::1234", 1234)); |
| 2592 | port->AddCandidateAddress(SocketAddress("fc12:3456::1234", 1234)); |
| 2593 | port->AddCandidateAddress(SocketAddress("::ffff:192.168.1.4", 1234)); |
| 2594 | port->AddCandidateAddress(SocketAddress("::192.168.1.4", 1234)); |
| 2595 | port->AddCandidateAddress(SocketAddress("2002::1234:5678", 1234)); |
| 2596 | port->AddCandidateAddress(SocketAddress("2001::1234:5678", 1234)); |
| 2597 | port->AddCandidateAddress(SocketAddress("fecf::1234:5678", 1234)); |
| 2598 | port->AddCandidateAddress(SocketAddress("3ffe::1234:5678", 1234)); |
| 2599 | // These should all be: |
| 2600 | // (90 << 24) | ([rfc3484 pref value] << 8) | (256 - 177) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2601 | uint32_t expected_priority_v4 = 1509957199U; |
| 2602 | uint32_t expected_priority_v6 = 1509959759U; |
| 2603 | uint32_t expected_priority_ula = 1509962319U; |
| 2604 | uint32_t expected_priority_v4mapped = expected_priority_v4; |
| 2605 | uint32_t expected_priority_v4compat = 1509949775U; |
| 2606 | uint32_t expected_priority_6to4 = 1509954639U; |
| 2607 | uint32_t expected_priority_teredo = 1509952079U; |
| 2608 | uint32_t expected_priority_sitelocal = 1509949775U; |
| 2609 | uint32_t expected_priority_6bone = 1509949775U; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2610 | ASSERT_EQ(expected_priority_v4, port->Candidates()[0].priority()); |
| 2611 | ASSERT_EQ(expected_priority_v6, port->Candidates()[1].priority()); |
| 2612 | ASSERT_EQ(expected_priority_ula, port->Candidates()[2].priority()); |
| 2613 | ASSERT_EQ(expected_priority_v4mapped, port->Candidates()[3].priority()); |
| 2614 | ASSERT_EQ(expected_priority_v4compat, port->Candidates()[4].priority()); |
| 2615 | ASSERT_EQ(expected_priority_6to4, port->Candidates()[5].priority()); |
| 2616 | ASSERT_EQ(expected_priority_teredo, port->Candidates()[6].priority()); |
| 2617 | ASSERT_EQ(expected_priority_sitelocal, port->Candidates()[7].priority()); |
| 2618 | ASSERT_EQ(expected_priority_6bone, port->Candidates()[8].priority()); |
| 2619 | } |
| 2620 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2621 | // In the case of shared socket, one port may be shared by local and stun. |
| 2622 | // Test that candidates with different types will have different foundation. |
| 2623 | TEST_F(PortTest, TestFoundation) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2624 | auto testport = CreateTestPort(kLocalAddr1, "name", "pass"); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2625 | testport->AddCandidateAddress(kLocalAddr1, kLocalAddr1, LOCAL_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2626 | cricket::ICE_TYPE_PREFERENCE_HOST, false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2627 | testport->AddCandidateAddress(kLocalAddr2, kLocalAddr1, STUN_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2628 | cricket::ICE_TYPE_PREFERENCE_SRFLX, true); |
| 2629 | EXPECT_NE(testport->Candidates()[0].foundation(), |
| 2630 | testport->Candidates()[1].foundation()); |
| 2631 | } |
| 2632 | |
| 2633 | // This test verifies the foundation of different types of ICE candidates. |
| 2634 | TEST_F(PortTest, TestCandidateFoundation) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2635 | std::unique_ptr<rtc::NATServer> nat_server( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2636 | CreateNatServer(kNatAddr1, NAT_OPEN_CONE)); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2637 | auto udpport1 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2638 | udpport1->PrepareAddress(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2639 | auto udpport2 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2640 | udpport2->PrepareAddress(); |
| 2641 | EXPECT_EQ(udpport1->Candidates()[0].foundation(), |
| 2642 | udpport2->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2643 | auto tcpport1 = CreateTcpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2644 | tcpport1->PrepareAddress(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2645 | auto tcpport2 = CreateTcpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2646 | tcpport2->PrepareAddress(); |
| 2647 | EXPECT_EQ(tcpport1->Candidates()[0].foundation(), |
| 2648 | tcpport2->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2649 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2650 | stunport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2651 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2652 | EXPECT_NE(tcpport1->Candidates()[0].foundation(), |
| 2653 | stunport->Candidates()[0].foundation()); |
| 2654 | EXPECT_NE(tcpport2->Candidates()[0].foundation(), |
| 2655 | stunport->Candidates()[0].foundation()); |
| 2656 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2657 | stunport->Candidates()[0].foundation()); |
| 2658 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2659 | stunport->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2660 | // Verifying TURN candidate foundation. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2661 | auto turnport1 = |
| 2662 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2663 | turnport1->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2664 | ASSERT_EQ_WAIT(1U, turnport1->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2665 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2666 | turnport1->Candidates()[0].foundation()); |
| 2667 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2668 | turnport1->Candidates()[0].foundation()); |
| 2669 | EXPECT_NE(stunport->Candidates()[0].foundation(), |
| 2670 | turnport1->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2671 | auto turnport2 = |
| 2672 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2673 | turnport2->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2674 | ASSERT_EQ_WAIT(1U, turnport2->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2675 | EXPECT_EQ(turnport1->Candidates()[0].foundation(), |
| 2676 | turnport2->Candidates()[0].foundation()); |
| 2677 | |
| 2678 | // Running a second turn server, to get different base IP address. |
| 2679 | SocketAddress kTurnUdpIntAddr2("99.99.98.4", STUN_SERVER_PORT); |
| 2680 | SocketAddress kTurnUdpExtAddr2("99.99.98.5", 0); |
Niels Möller | 6dd4997 | 2021-11-24 14:05:55 +0100 | [diff] [blame] | 2681 | TestTurnServer turn_server2(rtc::Thread::Current(), vss(), kTurnUdpIntAddr2, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2682 | kTurnUdpExtAddr2); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2683 | auto turnport3 = CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, |
| 2684 | PROTO_UDP, kTurnUdpIntAddr2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2685 | turnport3->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2686 | ASSERT_EQ_WAIT(1U, turnport3->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2687 | EXPECT_NE(turnport3->Candidates()[0].foundation(), |
| 2688 | turnport2->Candidates()[0].foundation()); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2689 | |
| 2690 | // Start a TCP turn server, and check that two turn candidates have |
| 2691 | // different foundations if their relay protocols are different. |
Niels Möller | 6dd4997 | 2021-11-24 14:05:55 +0100 | [diff] [blame] | 2692 | TestTurnServer turn_server3(rtc::Thread::Current(), vss(), kTurnTcpIntAddr, |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2693 | kTurnUdpExtAddr, PROTO_TCP); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2694 | auto turnport4 = |
| 2695 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_TCP, PROTO_UDP); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2696 | turnport4->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2697 | ASSERT_EQ_WAIT(1U, turnport4->Candidates().size(), kDefaultTimeout); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2698 | EXPECT_NE(turnport2->Candidates()[0].foundation(), |
| 2699 | turnport4->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | // This test verifies the related addresses of different types of |
David Sanders | 60c588d | 2022-03-19 01:43:10 -0700 | [diff] [blame] | 2703 | // ICE candidates. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2704 | TEST_F(PortTest, TestCandidateRelatedAddress) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2705 | auto nat_server = CreateNatServer(kNatAddr1, NAT_OPEN_CONE); |
| 2706 | auto udpport = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2707 | udpport->PrepareAddress(); |
| 2708 | // For UDPPort, related address will be empty. |
| 2709 | EXPECT_TRUE(udpport->Candidates()[0].related_address().IsNil()); |
| 2710 | // Testing related address for stun candidates. |
| 2711 | // For stun candidate related address must be equal to the base |
| 2712 | // socket address. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2713 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2714 | stunport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2715 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2716 | // Check STUN candidate address. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2717 | EXPECT_EQ(stunport->Candidates()[0].address().ipaddr(), kNatAddr1.ipaddr()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2718 | // Check STUN candidate related address. |
| 2719 | EXPECT_EQ(stunport->Candidates()[0].related_address(), |
| 2720 | stunport->GetLocalAddress()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2721 | // Verifying the related address for TURN candidate. |
| 2722 | // For TURN related address must be equal to the mapped address. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2723 | auto turnport = |
| 2724 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2725 | turnport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2726 | ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2727 | EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), |
| 2728 | turnport->Candidates()[0].address().ipaddr()); |
| 2729 | EXPECT_EQ(kNatAddr1.ipaddr(), |
| 2730 | turnport->Candidates()[0].related_address().ipaddr()); |
| 2731 | } |
| 2732 | |
| 2733 | // Test priority value overflow handling when preference is set to 3. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2734 | TEST_F(PortTest, TestCandidatePriority) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2735 | cricket::Candidate cand1; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2736 | cand1.set_priority(3); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2737 | cricket::Candidate cand2; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2738 | cand2.set_priority(1); |
| 2739 | EXPECT_TRUE(cand1.priority() > cand2.priority()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | // Test the Connection priority is calculated correctly. |
| 2743 | TEST_F(PortTest, TestConnectionPriority) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2744 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2745 | lport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_HOST); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2746 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 2747 | rport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_RELAY_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2748 | lport->set_component(123); |
| 2749 | lport->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2750 | rport->set_component(23); |
| 2751 | rport->AddCandidateAddress(SocketAddress("10.1.1.100", 1234)); |
| 2752 | |
| 2753 | EXPECT_EQ(0x7E001E85U, lport->Candidates()[0].priority()); |
| 2754 | EXPECT_EQ(0x2001EE9U, rport->Candidates()[0].priority()); |
| 2755 | |
| 2756 | // RFC 5245 |
| 2757 | // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0) |
| 2758 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2759 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2760 | Connection* lconn = |
| 2761 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2762 | #if defined(WEBRTC_WIN) |
| 2763 | EXPECT_EQ(0x2001EE9FC003D0BU, lconn->priority()); |
| 2764 | #else |
| 2765 | EXPECT_EQ(0x2001EE9FC003D0BLLU, lconn->priority()); |
| 2766 | #endif |
| 2767 | |
| 2768 | lport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2769 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2770 | Connection* rconn = |
| 2771 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2772 | #if defined(WEBRTC_WIN) |
| 2773 | EXPECT_EQ(0x2001EE9FC003D0AU, rconn->priority()); |
| 2774 | #else |
| 2775 | EXPECT_EQ(0x2001EE9FC003D0ALLU, rconn->priority()); |
| 2776 | #endif |
| 2777 | } |
| 2778 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2779 | // Note that UpdateState takes into account the estimated RTT, and the |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2780 | // correctness of using `kMaxExpectedSimulatedRtt` as an upper bound of RTT in |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2781 | // the following tests depends on the link rate and the delay distriubtion |
| 2782 | // configured in VirtualSocketServer::AddPacketToNetwork. The tests below use |
| 2783 | // the default setup where the RTT is deterministically one, which generates an |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2784 | // estimate given by `MINIMUM_RTT` = 100. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2785 | TEST_F(PortTest, TestWritableState) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2786 | rtc::ScopedFakeClock clock; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2787 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2788 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2789 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2790 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2791 | |
| 2792 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2793 | TestChannel ch1(std::move(port1)); |
| 2794 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2795 | |
| 2796 | // Acquire addresses. |
| 2797 | ch1.Start(); |
| 2798 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2799 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 2800 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Send a ping from src to dst. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2803 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2804 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2805 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2806 | // for TCP connect |
| 2807 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2808 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2809 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2810 | |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2811 | // Data should be sendable before the connection is accepted. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2812 | char data[] = "abcd"; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 2813 | int data_size = arraysize(data); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2814 | rtc::PacketOptions options; |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2815 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2816 | |
| 2817 | // Accept the connection to return the binding response, transition to |
| 2818 | // writable, and allow data to be sent. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2819 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2820 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2821 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2822 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2823 | |
| 2824 | // Ask the connection to update state as if enough time has passed to lose |
| 2825 | // full writability and 5 pings went unresponded to. We'll accomplish the |
| 2826 | // latter by sending pings but not pumping messages. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2827 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2828 | ch1.Ping(i); |
| 2829 | } |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2830 | int unreliable_timeout_delay = |
| 2831 | CONNECTION_WRITE_CONNECT_TIMEOUT + kMaxExpectedSimulatedRtt; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2832 | ch1.conn()->UpdateState(unreliable_timeout_delay); |
| 2833 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2834 | |
| 2835 | // Data should be able to be sent in this state. |
| 2836 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2837 | |
| 2838 | // And now allow the other side to process the pings and send binding |
| 2839 | // responses. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2840 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2841 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2842 | // Wait long enough for a full timeout (past however long we've already |
| 2843 | // waited). |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2844 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2845 | ch1.Ping(unreliable_timeout_delay + i); |
| 2846 | } |
| 2847 | ch1.conn()->UpdateState(unreliable_timeout_delay + CONNECTION_WRITE_TIMEOUT + |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2848 | kMaxExpectedSimulatedRtt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2849 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2850 | |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2851 | // Even if the connection has timed out, the Connection shouldn't block |
| 2852 | // the sending of data. |
| 2853 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2854 | |
| 2855 | ch1.Stop(); |
| 2856 | ch2.Stop(); |
| 2857 | } |
| 2858 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2859 | // Test writability states using the configured threshold value to replace |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2860 | // the default value given by `CONNECTION_WRITE_CONNECT_TIMEOUT` and |
| 2861 | // `CONNECTION_WRITE_CONNECT_FAILURES`. |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2862 | TEST_F(PortTest, TestWritableStateWithConfiguredThreshold) { |
| 2863 | rtc::ScopedFakeClock clock; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2864 | auto port1 = CreateUdpPort(kLocalAddr1); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2865 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2866 | auto port2 = CreateUdpPort(kLocalAddr2); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2867 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2868 | |
| 2869 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2870 | TestChannel ch1(std::move(port1)); |
| 2871 | TestChannel ch2(std::move(port2)); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2872 | |
| 2873 | // Acquire addresses. |
| 2874 | ch1.Start(); |
| 2875 | ch2.Start(); |
| 2876 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 2877 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
| 2878 | |
| 2879 | // Send a ping from src to dst. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2880 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2881 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2882 | ch1.Ping(); |
| 2883 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
| 2884 | |
| 2885 | // Accept the connection to return the binding response, transition to |
| 2886 | // writable, and allow data to be sent. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2887 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2888 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2889 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
| 2890 | |
| 2891 | ch1.conn()->set_unwritable_timeout(1000); |
| 2892 | ch1.conn()->set_unwritable_min_checks(3); |
| 2893 | // Send two checks. |
| 2894 | ch1.Ping(1); |
| 2895 | ch1.Ping(2); |
| 2896 | // We have not reached the timeout nor have we sent the minimum number of |
| 2897 | // checks to change the state to Unreliable. |
| 2898 | ch1.conn()->UpdateState(999); |
| 2899 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2900 | // We have not sent the minimum number of checks without responses. |
| 2901 | ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt); |
| 2902 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2903 | // Last ping after which the candidate pair should become Unreliable after |
| 2904 | // timeout. |
| 2905 | ch1.Ping(3); |
| 2906 | // We have not reached the timeout. |
| 2907 | ch1.conn()->UpdateState(999); |
| 2908 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2909 | // We should be in the state Unreliable now. |
| 2910 | ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt); |
| 2911 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2912 | |
| 2913 | ch1.Stop(); |
| 2914 | ch2.Stop(); |
| 2915 | } |
| 2916 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2917 | TEST_F(PortTest, TestTimeoutForNeverWritable) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2918 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2919 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2920 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2921 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2922 | |
| 2923 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2924 | TestChannel ch1(std::move(port1)); |
| 2925 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2926 | |
| 2927 | // Acquire addresses. |
| 2928 | ch1.Start(); |
| 2929 | ch2.Start(); |
| 2930 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2931 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2932 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2933 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2934 | |
| 2935 | // Attempt to go directly to write timeout. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2936 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2937 | ch1.Ping(i); |
| 2938 | } |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2939 | ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + kMaxExpectedSimulatedRtt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2940 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2941 | } |
| 2942 | |
| 2943 | // This test verifies the connection setup between ICEMODE_FULL |
| 2944 | // and ICEMODE_LITE. |
Artem Titov | 2dbb4c9 | 2021-07-26 15:12:41 +0200 | [diff] [blame] | 2945 | // 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] | 2946 | // port which responds to the ping message just like LITE client. |
| 2947 | TEST_F(PortTest, TestIceLiteConnectivity) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2948 | auto ice_full_port = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2949 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 2950 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2951 | auto* ice_full_port_ptr = ice_full_port.get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2952 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2953 | auto ice_lite_port = CreateTestPort( |
| 2954 | kLocalAddr2, "rfrag", "rpass", cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2955 | // Setup TestChannel. This behaves like FULL mode client. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2956 | TestChannel ch1(std::move(ice_full_port)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2957 | ch1.SetIceMode(ICEMODE_FULL); |
| 2958 | |
| 2959 | // Start gathering candidates. |
| 2960 | ch1.Start(); |
| 2961 | ice_lite_port->PrepareAddress(); |
| 2962 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2963 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2964 | ASSERT_FALSE(ice_lite_port->Candidates().empty()); |
| 2965 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2966 | ch1.CreateConnection(GetCandidate(ice_lite_port.get())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2967 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2968 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2969 | |
| 2970 | // Send ping from full mode client. |
| 2971 | // This ping must not have USE_CANDIDATE_ATTR. |
| 2972 | ch1.Ping(); |
| 2973 | |
| 2974 | // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly |
| 2975 | // from port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2976 | ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout); |
| 2977 | IceMessage* msg = ice_full_port_ptr->last_stun_msg(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2978 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 2979 | |
| 2980 | // Respond with a BINDING-RESPONSE from litemode client. |
| 2981 | // NOTE: Ideally we should't create connection at this stage from lite |
| 2982 | // port, as it should be done only after receiving ping with USE_CANDIDATE. |
| 2983 | // But we need a connection to send a response message. |
Jonas Oreland | d003662 | 2019-11-29 15:58:13 +0100 | [diff] [blame] | 2984 | auto* con = ice_lite_port->CreateConnection( |
| 2985 | ice_full_port_ptr->Candidates()[0], cricket::Port::ORIGIN_MESSAGE); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2986 | std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 2987 | con->SendStunBindingResponse(request.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2988 | |
| 2989 | // Feeding the respone message from litemode to the full mode connection. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2990 | ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(), |
| 2991 | ice_lite_port->last_stun_buf()->size(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2992 | /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2993 | // Verifying full mode connection becomes writable from the response. |
| 2994 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2995 | kDefaultTimeout); |
| 2996 | EXPECT_TRUE_WAIT(ch1.nominated(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2997 | |
| 2998 | // Clear existing stun messsages. Otherwise we will process old stun |
| 2999 | // message right after we send ping. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3000 | ice_full_port_ptr->Reset(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3001 | // Send ping. This must have USE_CANDIDATE_ATTR. |
| 3002 | ch1.Ping(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3003 | ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout); |
| 3004 | msg = ice_full_port_ptr->last_stun_msg(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3005 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 3006 | ch1.Stop(); |
| 3007 | } |
| 3008 | |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3009 | namespace { |
| 3010 | |
| 3011 | // Utility function for testing goog ping. |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3012 | absl::optional<int> GetSupportedGoogPingVersion(const StunMessage* msg) { |
| 3013 | auto goog_misc = msg->GetUInt16List(STUN_ATTR_GOOG_MISC_INFO); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3014 | if (goog_misc == nullptr) { |
| 3015 | return absl::nullopt; |
| 3016 | } |
| 3017 | |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3018 | if (msg->type() == STUN_BINDING_REQUEST) { |
| 3019 | if (goog_misc->Size() < |
| 3020 | static_cast<int>(cricket::IceGoogMiscInfoBindingRequestAttributeIndex:: |
| 3021 | SUPPORT_GOOG_PING_VERSION)) { |
| 3022 | return absl::nullopt; |
| 3023 | } |
| 3024 | |
| 3025 | return goog_misc->GetType( |
| 3026 | static_cast<int>(cricket::IceGoogMiscInfoBindingRequestAttributeIndex:: |
| 3027 | SUPPORT_GOOG_PING_VERSION)); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3028 | } |
| 3029 | |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3030 | if (msg->type() == STUN_BINDING_RESPONSE) { |
| 3031 | if (goog_misc->Size() < |
| 3032 | static_cast<int>(cricket::IceGoogMiscInfoBindingResponseAttributeIndex:: |
| 3033 | SUPPORT_GOOG_PING_VERSION)) { |
| 3034 | return absl::nullopt; |
| 3035 | } |
| 3036 | |
| 3037 | return goog_misc->GetType( |
| 3038 | static_cast<int>(cricket::IceGoogMiscInfoBindingResponseAttributeIndex:: |
| 3039 | SUPPORT_GOOG_PING_VERSION)); |
| 3040 | } |
| 3041 | return absl::nullopt; |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | } // namespace |
| 3045 | |
| 3046 | class GoogPingTest |
| 3047 | : public PortTest, |
| 3048 | public ::testing::WithParamInterface<std::pair<bool, bool>> {}; |
| 3049 | |
| 3050 | // This test verifies the announce/enable on/off behavior |
| 3051 | TEST_P(GoogPingTest, TestGoogPingAnnounceEnable) { |
| 3052 | IceFieldTrials trials; |
| 3053 | trials.announce_goog_ping = GetParam().first; |
| 3054 | trials.enable_goog_ping = GetParam().second; |
| 3055 | RTC_LOG(LS_INFO) << "Testing combination: " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 3056 | " announce: " |
| 3057 | << trials.announce_goog_ping |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3058 | << " enable:" << trials.enable_goog_ping; |
| 3059 | |
| 3060 | auto port1_unique = |
| 3061 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3062 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3063 | auto* port1 = port1_unique.get(); |
| 3064 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3065 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3066 | |
| 3067 | TestChannel ch1(std::move(port1_unique)); |
| 3068 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3069 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3070 | // This only makes test a bit shorter... |
| 3071 | ch1.SetIceMode(ICEMODE_LITE); |
| 3072 | // Start gathering candidates. |
| 3073 | ch1.Start(); |
| 3074 | port2->PrepareAddress(); |
| 3075 | |
| 3076 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3077 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3078 | |
| 3079 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3080 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3081 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3082 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3083 | |
| 3084 | // Send ping. |
| 3085 | ch1.Ping(); |
| 3086 | |
| 3087 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3088 | const IceMessage* request1 = port1->last_stun_msg(); |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3089 | |
| 3090 | ASSERT_EQ(trials.enable_goog_ping, |
| 3091 | GetSupportedGoogPingVersion(request1) && |
| 3092 | GetSupportedGoogPingVersion(request1) >= kGoogPingVersion); |
| 3093 | |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3094 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3095 | cricket::Port::ORIGIN_MESSAGE); |
| 3096 | con->SetIceFieldTrials(&trials); |
| 3097 | |
| 3098 | con->SendStunBindingResponse(request1); |
| 3099 | |
| 3100 | // Then check the response matches the settings. |
| 3101 | const auto* response = port2->last_stun_msg(); |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3102 | EXPECT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3103 | EXPECT_EQ(trials.enable_goog_ping && trials.announce_goog_ping, |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3104 | GetSupportedGoogPingVersion(response) && |
| 3105 | GetSupportedGoogPingVersion(response) >= kGoogPingVersion); |
| 3106 | |
| 3107 | // Feeding the respone message back. |
| 3108 | ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), |
| 3109 | port2->last_stun_buf()->size(), |
| 3110 | /* packet_time_us */ -1); |
| 3111 | |
| 3112 | port1->Reset(); |
| 3113 | port2->Reset(); |
| 3114 | |
| 3115 | ch1.Ping(); |
| 3116 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3117 | const IceMessage* request2 = port1->last_stun_msg(); |
| 3118 | |
| 3119 | // It should be a GOOG_PING if both of these are TRUE |
| 3120 | if (trials.announce_goog_ping && trials.enable_goog_ping) { |
| 3121 | ASSERT_EQ(request2->type(), GOOG_PING_REQUEST); |
| 3122 | con->SendGoogPingResponse(request2); |
| 3123 | } else { |
| 3124 | ASSERT_EQ(request2->type(), STUN_BINDING_REQUEST); |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3125 | // If we sent a BINDING with enable, and we got a reply that |
| 3126 | // didn't contain announce, the next ping should not contain |
| 3127 | // the enable again. |
| 3128 | ASSERT_FALSE(GetSupportedGoogPingVersion(request2).has_value()); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3129 | con->SendStunBindingResponse(request2); |
| 3130 | } |
| 3131 | |
| 3132 | const auto* response2 = port2->last_stun_msg(); |
| 3133 | ASSERT_TRUE(response2 != nullptr); |
| 3134 | |
| 3135 | // It should be a GOOG_PING_RESPONSE if both of these are TRUE |
| 3136 | if (trials.announce_goog_ping && trials.enable_goog_ping) { |
| 3137 | ASSERT_EQ(response2->type(), GOOG_PING_RESPONSE); |
| 3138 | } else { |
| 3139 | ASSERT_EQ(response2->type(), STUN_BINDING_RESPONSE); |
| 3140 | } |
| 3141 | |
| 3142 | ch1.Stop(); |
| 3143 | } |
| 3144 | |
Jonas Oreland | 219d8ce | 2020-01-15 15:29:48 +0100 | [diff] [blame] | 3145 | // This test if a someone send a STUN_BINDING with unsupported version |
| 3146 | // (kGoogPingVersion == 0) |
| 3147 | TEST_F(PortTest, TestGoogPingUnsupportedVersionInStunBinding) { |
| 3148 | IceFieldTrials trials; |
| 3149 | trials.announce_goog_ping = true; |
| 3150 | trials.enable_goog_ping = true; |
| 3151 | |
| 3152 | auto port1_unique = |
| 3153 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3154 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3155 | auto* port1 = port1_unique.get(); |
| 3156 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3157 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3158 | |
| 3159 | TestChannel ch1(std::move(port1_unique)); |
| 3160 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3161 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3162 | // This only makes test a bit shorter... |
| 3163 | ch1.SetIceMode(ICEMODE_LITE); |
| 3164 | // Start gathering candidates. |
| 3165 | ch1.Start(); |
| 3166 | port2->PrepareAddress(); |
| 3167 | |
| 3168 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3169 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3170 | |
| 3171 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3172 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3173 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3174 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3175 | |
| 3176 | // Send ping. |
| 3177 | ch1.Ping(); |
| 3178 | |
| 3179 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3180 | const IceMessage* request1 = port1->last_stun_msg(); |
| 3181 | |
| 3182 | ASSERT_TRUE(GetSupportedGoogPingVersion(request1) && |
| 3183 | GetSupportedGoogPingVersion(request1) >= kGoogPingVersion); |
| 3184 | |
| 3185 | // Modify the STUN message request1 to send GetSupportedGoogPingVersion == 0 |
| 3186 | auto modified_request1 = request1->Clone(); |
| 3187 | ASSERT_TRUE(modified_request1->RemoveAttribute(STUN_ATTR_GOOG_MISC_INFO) != |
| 3188 | nullptr); |
| 3189 | ASSERT_TRUE(modified_request1->RemoveAttribute(STUN_ATTR_MESSAGE_INTEGRITY) != |
| 3190 | nullptr); |
| 3191 | { |
| 3192 | auto list = |
| 3193 | StunAttribute::CreateUInt16ListAttribute(STUN_ATTR_GOOG_MISC_INFO); |
| 3194 | list->AddTypeAtIndex( |
| 3195 | static_cast<uint16_t>( |
| 3196 | cricket::IceGoogMiscInfoBindingRequestAttributeIndex:: |
| 3197 | SUPPORT_GOOG_PING_VERSION), |
| 3198 | /* version */ 0); |
| 3199 | modified_request1->AddAttribute(std::move(list)); |
| 3200 | modified_request1->AddMessageIntegrity("rpass"); |
| 3201 | } |
| 3202 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3203 | cricket::Port::ORIGIN_MESSAGE); |
| 3204 | con->SetIceFieldTrials(&trials); |
| 3205 | |
| 3206 | con->SendStunBindingResponse(modified_request1.get()); |
| 3207 | |
| 3208 | // Then check the response matches the settings. |
| 3209 | const auto* response = port2->last_stun_msg(); |
| 3210 | EXPECT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3211 | EXPECT_FALSE(GetSupportedGoogPingVersion(response)); |
| 3212 | |
| 3213 | ch1.Stop(); |
| 3214 | } |
| 3215 | |
| 3216 | // This test if a someone send a STUN_BINDING_RESPONSE with unsupported version |
| 3217 | // (kGoogPingVersion == 0) |
| 3218 | TEST_F(PortTest, TestGoogPingUnsupportedVersionInStunBindingResponse) { |
| 3219 | IceFieldTrials trials; |
| 3220 | trials.announce_goog_ping = true; |
| 3221 | trials.enable_goog_ping = true; |
| 3222 | |
| 3223 | auto port1_unique = |
| 3224 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3225 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3226 | auto* port1 = port1_unique.get(); |
| 3227 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3228 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3229 | |
| 3230 | TestChannel ch1(std::move(port1_unique)); |
| 3231 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3232 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3233 | // This only makes test a bit shorter... |
| 3234 | ch1.SetIceMode(ICEMODE_LITE); |
| 3235 | // Start gathering candidates. |
| 3236 | ch1.Start(); |
| 3237 | port2->PrepareAddress(); |
| 3238 | |
| 3239 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3240 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3241 | |
| 3242 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3243 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3244 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3245 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3246 | |
| 3247 | // Send ping. |
| 3248 | ch1.Ping(); |
| 3249 | |
| 3250 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3251 | const IceMessage* request1 = port1->last_stun_msg(); |
| 3252 | |
| 3253 | ASSERT_TRUE(GetSupportedGoogPingVersion(request1) && |
| 3254 | GetSupportedGoogPingVersion(request1) >= kGoogPingVersion); |
| 3255 | |
| 3256 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3257 | cricket::Port::ORIGIN_MESSAGE); |
| 3258 | con->SetIceFieldTrials(&trials); |
| 3259 | |
| 3260 | con->SendStunBindingResponse(request1); |
| 3261 | |
| 3262 | // Then check the response matches the settings. |
| 3263 | const auto* response = port2->last_stun_msg(); |
| 3264 | EXPECT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3265 | EXPECT_TRUE(GetSupportedGoogPingVersion(response)); |
| 3266 | |
| 3267 | // Modify the STUN message response to contain GetSupportedGoogPingVersion == |
| 3268 | // 0 |
| 3269 | auto modified_response = response->Clone(); |
| 3270 | ASSERT_TRUE(modified_response->RemoveAttribute(STUN_ATTR_GOOG_MISC_INFO) != |
| 3271 | nullptr); |
| 3272 | ASSERT_TRUE(modified_response->RemoveAttribute(STUN_ATTR_MESSAGE_INTEGRITY) != |
| 3273 | nullptr); |
| 3274 | ASSERT_TRUE(modified_response->RemoveAttribute(STUN_ATTR_FINGERPRINT) != |
| 3275 | nullptr); |
| 3276 | { |
| 3277 | auto list = |
| 3278 | StunAttribute::CreateUInt16ListAttribute(STUN_ATTR_GOOG_MISC_INFO); |
| 3279 | list->AddTypeAtIndex( |
| 3280 | static_cast<uint16_t>( |
| 3281 | cricket::IceGoogMiscInfoBindingResponseAttributeIndex:: |
| 3282 | SUPPORT_GOOG_PING_VERSION), |
| 3283 | /* version */ 0); |
| 3284 | modified_response->AddAttribute(std::move(list)); |
| 3285 | modified_response->AddMessageIntegrity("rpass"); |
| 3286 | modified_response->AddFingerprint(); |
| 3287 | } |
| 3288 | |
| 3289 | rtc::ByteBufferWriter buf; |
| 3290 | modified_response->Write(&buf); |
| 3291 | |
| 3292 | // Feeding the modified respone message back. |
| 3293 | ch1.conn()->OnReadPacket(buf.Data(), buf.Length(), /* packet_time_us */ -1); |
| 3294 | |
| 3295 | port1->Reset(); |
| 3296 | port2->Reset(); |
| 3297 | |
| 3298 | ch1.Ping(); |
| 3299 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3300 | |
| 3301 | // This should now be a STUN_BINDING...without a kGoogPingVersion |
| 3302 | const IceMessage* request2 = port1->last_stun_msg(); |
| 3303 | EXPECT_EQ(request2->type(), STUN_BINDING_REQUEST); |
| 3304 | EXPECT_FALSE(GetSupportedGoogPingVersion(request2)); |
| 3305 | |
| 3306 | ch1.Stop(); |
| 3307 | } |
| 3308 | |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3309 | INSTANTIATE_TEST_SUITE_P(GoogPingTest, |
| 3310 | GoogPingTest, |
| 3311 | // test all combinations of <announce, enable> pairs. |
| 3312 | ::testing::Values(std::make_pair(false, false), |
| 3313 | std::make_pair(true, false), |
| 3314 | std::make_pair(false, true), |
| 3315 | std::make_pair(true, true))); |
| 3316 | |
| 3317 | // This test checks that a change in attributes falls back to STUN_BINDING |
| 3318 | TEST_F(PortTest, TestChangeInAttributeMakesGoogPingFallsbackToStunBinding) { |
| 3319 | IceFieldTrials trials; |
| 3320 | trials.announce_goog_ping = true; |
| 3321 | trials.enable_goog_ping = true; |
| 3322 | |
| 3323 | auto port1_unique = |
| 3324 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3325 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3326 | auto* port1 = port1_unique.get(); |
| 3327 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3328 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3329 | |
| 3330 | TestChannel ch1(std::move(port1_unique)); |
| 3331 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3332 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3333 | // This only makes test a bit shorter... |
| 3334 | ch1.SetIceMode(ICEMODE_LITE); |
| 3335 | // Start gathering candidates. |
| 3336 | ch1.Start(); |
| 3337 | port2->PrepareAddress(); |
| 3338 | |
| 3339 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3340 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3341 | |
| 3342 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3343 | ASSERT_TRUE(ch1.conn() != nullptr); |
| 3344 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3345 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3346 | |
| 3347 | // Send ping. |
| 3348 | ch1.Ping(); |
| 3349 | |
| 3350 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3351 | const IceMessage* msg = port1->last_stun_msg(); |
| 3352 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3353 | cricket::Port::ORIGIN_MESSAGE); |
| 3354 | con->SetIceFieldTrials(&trials); |
| 3355 | |
| 3356 | // Feed the message into the connection. |
| 3357 | con->SendStunBindingResponse(msg); |
| 3358 | |
| 3359 | // The check reply wrt to settings. |
| 3360 | const auto* response = port2->last_stun_msg(); |
| 3361 | ASSERT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3362 | ASSERT_TRUE(GetSupportedGoogPingVersion(response) >= kGoogPingVersion); |
| 3363 | |
| 3364 | // Feeding the respone message back. |
| 3365 | ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), |
| 3366 | port2->last_stun_buf()->size(), |
| 3367 | /* packet_time_us */ -1); |
| 3368 | |
| 3369 | port1->Reset(); |
| 3370 | port2->Reset(); |
| 3371 | |
| 3372 | ch1.Ping(); |
| 3373 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3374 | const IceMessage* msg2 = port1->last_stun_msg(); |
| 3375 | |
| 3376 | // It should be a GOOG_PING if both of these are TRUE |
| 3377 | ASSERT_EQ(msg2->type(), GOOG_PING_REQUEST); |
| 3378 | con->SendGoogPingResponse(msg2); |
| 3379 | |
| 3380 | const auto* response2 = port2->last_stun_msg(); |
| 3381 | ASSERT_TRUE(response2 != nullptr); |
| 3382 | |
| 3383 | // It should be a GOOG_PING_RESPONSE. |
| 3384 | ASSERT_EQ(response2->type(), GOOG_PING_RESPONSE); |
| 3385 | |
| 3386 | // And now the third ping. |
| 3387 | port1->Reset(); |
| 3388 | port2->Reset(); |
| 3389 | |
| 3390 | // Modify the message to be sent. |
| 3391 | ch1.conn()->set_use_candidate_attr(!ch1.conn()->use_candidate_attr()); |
| 3392 | |
| 3393 | ch1.Ping(); |
| 3394 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3395 | const IceMessage* msg3 = port1->last_stun_msg(); |
| 3396 | |
| 3397 | // It should be a STUN_BINDING_REQUEST |
| 3398 | ASSERT_EQ(msg3->type(), STUN_BINDING_REQUEST); |
| 3399 | |
| 3400 | ch1.Stop(); |
| 3401 | } |
| 3402 | |
| 3403 | // This test that an error response fall back to STUN_BINDING. |
| 3404 | TEST_F(PortTest, TestErrorResponseMakesGoogPingFallBackToStunBinding) { |
| 3405 | IceFieldTrials trials; |
| 3406 | trials.announce_goog_ping = true; |
| 3407 | trials.enable_goog_ping = true; |
| 3408 | |
| 3409 | auto port1_unique = |
| 3410 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 3411 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
| 3412 | auto* port1 = port1_unique.get(); |
| 3413 | auto port2 = CreateTestPort(kLocalAddr2, "rfrag", "rpass", |
| 3414 | cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
| 3415 | |
| 3416 | TestChannel ch1(std::move(port1_unique)); |
| 3417 | // Block usage of STUN_ATTR_USE_CANDIDATE so that |
| 3418 | // ch1.conn() will sent GOOG_PING_REQUEST directly. |
| 3419 | // This only makes test a bit shorter... |
| 3420 | ch1.SetIceMode(ICEMODE_LITE); |
| 3421 | // Start gathering candidates. |
| 3422 | ch1.Start(); |
| 3423 | port2->PrepareAddress(); |
| 3424 | |
| 3425 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 3426 | ASSERT_FALSE(port2->Candidates().empty()); |
| 3427 | |
| 3428 | ch1.CreateConnection(GetCandidate(port2.get())); |
| 3429 | ASSERT_TRUE(ch1.conn() != NULL); |
| 3430 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 3431 | ch1.conn()->SetIceFieldTrials(&trials); |
| 3432 | |
| 3433 | // Send ping. |
| 3434 | ch1.Ping(); |
| 3435 | |
| 3436 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3437 | const IceMessage* msg = port1->last_stun_msg(); |
| 3438 | auto* con = port2->CreateConnection(port1->Candidates()[0], |
| 3439 | cricket::Port::ORIGIN_MESSAGE); |
| 3440 | con->SetIceFieldTrials(&trials); |
| 3441 | |
| 3442 | // Feed the message into the connection. |
| 3443 | con->SendStunBindingResponse(msg); |
| 3444 | |
| 3445 | // The check reply wrt to settings. |
| 3446 | const auto* response = port2->last_stun_msg(); |
| 3447 | ASSERT_EQ(response->type(), STUN_BINDING_RESPONSE); |
| 3448 | ASSERT_TRUE(GetSupportedGoogPingVersion(response) >= kGoogPingVersion); |
| 3449 | |
| 3450 | // Feeding the respone message back. |
| 3451 | ch1.conn()->OnReadPacket(port2->last_stun_buf()->data<char>(), |
| 3452 | port2->last_stun_buf()->size(), |
| 3453 | /* packet_time_us */ -1); |
| 3454 | |
| 3455 | port1->Reset(); |
| 3456 | port2->Reset(); |
| 3457 | |
| 3458 | ch1.Ping(); |
| 3459 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3460 | const IceMessage* msg2 = port1->last_stun_msg(); |
| 3461 | |
| 3462 | // It should be a GOOG_PING. |
| 3463 | ASSERT_EQ(msg2->type(), GOOG_PING_REQUEST); |
| 3464 | con->SendGoogPingResponse(msg2); |
| 3465 | |
| 3466 | const auto* response2 = port2->last_stun_msg(); |
| 3467 | ASSERT_TRUE(response2 != nullptr); |
| 3468 | |
| 3469 | // It should be a GOOG_PING_RESPONSE. |
| 3470 | ASSERT_EQ(response2->type(), GOOG_PING_RESPONSE); |
| 3471 | |
| 3472 | // But rather than the RESPONSE...feedback an error. |
Tommi | b5af6ee | 2022-06-04 09:50:22 +0200 | [diff] [blame] | 3473 | StunMessage error_response(GOOG_PING_ERROR_RESPONSE); |
Tommi | 408143d | 2022-06-01 15:29:31 +0200 | [diff] [blame] | 3474 | error_response.SetTransactionIdForTesting(response2->transaction_id()); |
Jonas Oreland | 9a52bd7 | 2019-12-11 11:35:48 +0100 | [diff] [blame] | 3475 | error_response.AddMessageIntegrity32("rpass"); |
| 3476 | rtc::ByteBufferWriter buf; |
| 3477 | error_response.Write(&buf); |
| 3478 | |
| 3479 | ch1.conn()->OnReadPacket(buf.Data(), buf.Length(), |
| 3480 | /* packet_time_us */ -1); |
| 3481 | |
| 3482 | // And now the third ping...this should be a binding. |
| 3483 | port1->Reset(); |
| 3484 | port2->Reset(); |
| 3485 | |
| 3486 | ch1.Ping(); |
| 3487 | ASSERT_TRUE_WAIT(port1->last_stun_msg() != NULL, kDefaultTimeout); |
| 3488 | const IceMessage* msg3 = port1->last_stun_msg(); |
| 3489 | |
| 3490 | // It should be a STUN_BINDING_REQUEST |
| 3491 | ASSERT_EQ(msg3->type(), STUN_BINDING_REQUEST); |
| 3492 | |
| 3493 | ch1.Stop(); |
| 3494 | } |
| 3495 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3496 | // This test case verifies that both the controlling port and the controlled |
| 3497 | // port will time out after connectivity is lost, if they are not marked as |
| 3498 | // "keep alive until pruned." |
| 3499 | TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) { |
| 3500 | rtc::ScopedFakeClock clock; |
| 3501 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3502 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 3503 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3504 | port1->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3505 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3506 | port1->SetIceTiebreaker(kTiebreaker1); |
| 3507 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3508 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 3509 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3510 | port2->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3511 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3512 | port2->SetIceTiebreaker(kTiebreaker2); |
| 3513 | |
| 3514 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3515 | TestChannel ch1(std::move(port1)); |
| 3516 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3517 | |
| 3518 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 3519 | StartConnectAndStopChannels(&ch1, &ch2); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3520 | // After the connection is destroyed, the port will be destroyed because |
| 3521 | // none of them is marked as "keep alive until pruned. |
| 3522 | EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), 110, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3525 | // Test that if after all connection are destroyed, new connections are created |
| 3526 | // and destroyed again, ports won't be destroyed until a timeout period passes |
| 3527 | // after the last set of connections are all destroyed. |
| 3528 | TEST_F(PortTest, TestPortTimeoutAfterNewConnectionCreatedAndDestroyed) { |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3529 | rtc::ScopedFakeClock clock; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3530 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3531 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 3532 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3533 | port1->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3534 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3535 | port1->SetIceTiebreaker(kTiebreaker1); |
| 3536 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3537 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 3538 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3539 | port2->set_timeout_delay(timeout_delay); // milliseconds |
| 3540 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3541 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3542 | port2->SetIceTiebreaker(kTiebreaker2); |
| 3543 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3544 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3545 | TestChannel ch1(std::move(port1)); |
| 3546 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3547 | |
| 3548 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 3549 | StartConnectAndStopChannels(&ch1, &ch2); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3550 | SIMULATED_WAIT(ports_destroyed() > 0, 80, clock); |
| 3551 | EXPECT_EQ(0, ports_destroyed()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3552 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3553 | // Start the second set of connection and destroy them. |
| 3554 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3555 | ch2.CreateConnection(GetCandidate(ch1.port())); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3556 | ch1.Stop(); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3557 | ch2.Stop(); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 3558 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3559 | SIMULATED_WAIT(ports_destroyed() > 0, 80, clock); |
| 3560 | EXPECT_EQ(0, ports_destroyed()); |
| 3561 | |
| 3562 | // The ports on both sides should be destroyed after timeout. |
| 3563 | EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 30, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 3564 | } |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3565 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3566 | // This test case verifies that neither the controlling port nor the controlled |
| 3567 | // port will time out after connectivity is lost if they are marked as "keep |
| 3568 | // alive until pruned". They will time out after they are pruned. |
| 3569 | TEST_F(PortTest, TestPortNotTimeoutUntilPruned) { |
| 3570 | rtc::ScopedFakeClock clock; |
| 3571 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3572 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 3573 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3574 | port1->set_timeout_delay(timeout_delay); // milliseconds |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3575 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3576 | port1->SetIceTiebreaker(kTiebreaker1); |
| 3577 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3578 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 3579 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3580 | port2->set_timeout_delay(timeout_delay); // milliseconds |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3581 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3582 | port2->SetIceTiebreaker(kTiebreaker2); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3583 | // The connection must not be destroyed before a connection is attempted. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3584 | EXPECT_EQ(0, ports_destroyed()); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3585 | |
| 3586 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 3587 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 3588 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3589 | // Set up channels and keep the port alive. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3590 | TestChannel ch1(std::move(port1)); |
| 3591 | TestChannel ch2(std::move(port2)); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3592 | // Simulate a connection that succeeds, and then is destroyed. But ports |
| 3593 | // are kept alive. Ports won't be destroyed. |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3594 | StartConnectAndStopChannels(&ch1, &ch2); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3595 | ch1.port()->KeepAliveUntilPruned(); |
| 3596 | ch2.port()->KeepAliveUntilPruned(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3597 | SIMULATED_WAIT(ports_destroyed() > 0, 150, clock); |
| 3598 | EXPECT_EQ(0, ports_destroyed()); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3599 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3600 | // If they are pruned now, they will be destroyed right away. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3601 | ch1.port()->Prune(); |
| 3602 | ch2.port()->Prune(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 3603 | // The ports on both sides should be destroyed after timeout. |
| 3604 | EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 1, clock); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 3605 | } |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3606 | |
| 3607 | TEST_F(PortTest, TestSupportsProtocol) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3608 | auto udp_port = CreateUdpPort(kLocalAddr1); |
Ali Tofigh | 42b163f | 2022-06-28 11:56:18 +0200 | [diff] [blame] | 3609 | EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3610 | EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3611 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3612 | auto stun_port = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
Ali Tofigh | 42b163f | 2022-06-28 11:56:18 +0200 | [diff] [blame] | 3613 | EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3614 | EXPECT_FALSE(stun_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3615 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3616 | auto tcp_port = CreateTcpPort(kLocalAddr1); |
Ali Tofigh | 42b163f | 2022-06-28 11:56:18 +0200 | [diff] [blame] | 3617 | EXPECT_TRUE(tcp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 3618 | EXPECT_TRUE(tcp_port->SupportsProtocol(SSLTCP_PROTOCOL_NAME)); |
| 3619 | EXPECT_FALSE(tcp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3620 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3621 | auto turn_port = |
| 3622 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
Ali Tofigh | 42b163f | 2022-06-28 11:56:18 +0200 | [diff] [blame] | 3623 | EXPECT_TRUE(turn_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 3624 | EXPECT_FALSE(turn_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 3625 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3626 | |
| 3627 | // Test that SetIceParameters updates the component, ufrag and password |
| 3628 | // on both the port itself and its candidates. |
| 3629 | TEST_F(PortTest, TestSetIceParameters) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3630 | auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1"); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 3631 | port->PrepareAddress(); |
| 3632 | EXPECT_EQ(1UL, port->Candidates().size()); |
| 3633 | port->SetIceParameters(1, "ufrag2", "password2"); |
| 3634 | EXPECT_EQ(1, port->component()); |
| 3635 | EXPECT_EQ("ufrag2", port->username_fragment()); |
| 3636 | EXPECT_EQ("password2", port->password()); |
| 3637 | const Candidate& candidate = port->Candidates()[0]; |
| 3638 | EXPECT_EQ(1, candidate.component()); |
| 3639 | EXPECT_EQ("ufrag2", candidate.username()); |
| 3640 | EXPECT_EQ("password2", candidate.password()); |
| 3641 | } |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3642 | |
| 3643 | TEST_F(PortTest, TestAddConnectionWithSameAddress) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 3644 | auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1"); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 3645 | port->PrepareAddress(); |
| 3646 | EXPECT_EQ(1u, port->Candidates().size()); |
| 3647 | rtc::SocketAddress address("1.1.1.1", 5000); |
| 3648 | cricket::Candidate candidate(1, "udp", address, 0, "", "", "relay", 0, ""); |
| 3649 | cricket::Connection* conn1 = |
| 3650 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 3651 | cricket::Connection* conn_in_use = port->GetConnection(address); |
| 3652 | EXPECT_EQ(conn1, conn_in_use); |
| 3653 | EXPECT_EQ(0u, conn_in_use->remote_candidate().generation()); |
| 3654 | |
| 3655 | // Creating with a candidate with the same address again will get us a |
| 3656 | // different connection with the new candidate. |
| 3657 | candidate.set_generation(2); |
| 3658 | cricket::Connection* conn2 = |
| 3659 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 3660 | EXPECT_NE(conn1, conn2); |
| 3661 | conn_in_use = port->GetConnection(address); |
| 3662 | EXPECT_EQ(conn2, conn_in_use); |
| 3663 | EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
| 3664 | |
| 3665 | // Make sure the new connection was not deleted. |
| 3666 | rtc::Thread::Current()->ProcessMessages(300); |
| 3667 | EXPECT_TRUE(port->GetConnection(address) != nullptr); |
| 3668 | } |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 3669 | |
Jonas Oreland | ef60c2b | 2020-03-26 15:10:39 +0100 | [diff] [blame] | 3670 | // TODO(webrtc:11463) : Move Connection tests into separate unit test |
| 3671 | // splitting out shared test code as needed. |
| 3672 | |
| 3673 | class ConnectionTest : public PortTest { |
| 3674 | public: |
| 3675 | ConnectionTest() { |
| 3676 | lport_ = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 3677 | rport_ = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
| 3678 | lport_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 3679 | lport_->SetIceTiebreaker(kTiebreaker1); |
| 3680 | rport_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 3681 | rport_->SetIceTiebreaker(kTiebreaker2); |
| 3682 | |
| 3683 | lport_->PrepareAddress(); |
| 3684 | rport_->PrepareAddress(); |
| 3685 | } |
| 3686 | |
| 3687 | rtc::ScopedFakeClock clock_; |
| 3688 | int num_state_changes_ = 0; |
| 3689 | |
| 3690 | Connection* CreateConnection(IceRole role) { |
| 3691 | Connection* conn; |
| 3692 | if (role == cricket::ICEROLE_CONTROLLING) { |
| 3693 | conn = lport_->CreateConnection(rport_->Candidates()[0], |
| 3694 | Port::ORIGIN_MESSAGE); |
| 3695 | } else { |
| 3696 | conn = rport_->CreateConnection(lport_->Candidates()[0], |
| 3697 | Port::ORIGIN_MESSAGE); |
| 3698 | } |
| 3699 | conn->SignalStateChange.connect(this, |
| 3700 | &ConnectionTest::OnConnectionStateChange); |
| 3701 | return conn; |
| 3702 | } |
| 3703 | |
| 3704 | void SendPingAndCaptureReply(Connection* lconn, |
| 3705 | Connection* rconn, |
| 3706 | int64_t ms, |
| 3707 | rtc::BufferT<uint8_t>* reply) { |
| 3708 | TestPort* lport = |
| 3709 | lconn->PortForTest() == lport_.get() ? lport_.get() : rport_.get(); |
| 3710 | TestPort* rport = |
| 3711 | rconn->PortForTest() == rport_.get() ? rport_.get() : lport_.get(); |
| 3712 | lconn->Ping(rtc::TimeMillis()); |
| 3713 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 3714 | ASSERT_TRUE(lport->last_stun_buf()); |
| 3715 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
| 3716 | lport->last_stun_buf()->size(), |
| 3717 | /* packet_time_us */ -1); |
| 3718 | clock_.AdvanceTime(webrtc::TimeDelta::Millis(ms)); |
| 3719 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 3720 | ASSERT_TRUE(rport->last_stun_buf()); |
| 3721 | *reply = std::move(*rport->last_stun_buf()); |
| 3722 | } |
| 3723 | |
| 3724 | void SendPingAndReceiveResponse(Connection* lconn, |
| 3725 | Connection* rconn, |
| 3726 | int64_t ms) { |
| 3727 | rtc::BufferT<uint8_t> reply; |
| 3728 | SendPingAndCaptureReply(lconn, rconn, ms, &reply); |
| 3729 | lconn->OnReadPacket(reply.data<char>(), reply.size(), |
| 3730 | /* packet_time_us */ -1); |
| 3731 | } |
| 3732 | |
| 3733 | void OnConnectionStateChange(Connection* connection) { num_state_changes_++; } |
| 3734 | |
| 3735 | private: |
| 3736 | std::unique_ptr<TestPort> lport_; |
| 3737 | std::unique_ptr<TestPort> rport_; |
| 3738 | }; |
| 3739 | |
| 3740 | TEST_F(ConnectionTest, ConnectionForgetLearnedState) { |
| 3741 | Connection* lconn = CreateConnection(ICEROLE_CONTROLLING); |
| 3742 | Connection* rconn = CreateConnection(ICEROLE_CONTROLLED); |
| 3743 | |
| 3744 | EXPECT_FALSE(lconn->writable()); |
| 3745 | EXPECT_FALSE(lconn->receiving()); |
| 3746 | EXPECT_TRUE(std::isnan(lconn->GetRttEstimate().GetAverage())); |
| 3747 | EXPECT_EQ(lconn->GetRttEstimate().GetVariance(), |
| 3748 | std::numeric_limits<double>::infinity()); |
| 3749 | |
| 3750 | SendPingAndReceiveResponse(lconn, rconn, 10); |
| 3751 | |
| 3752 | EXPECT_TRUE(lconn->writable()); |
| 3753 | EXPECT_TRUE(lconn->receiving()); |
| 3754 | EXPECT_EQ(lconn->GetRttEstimate().GetAverage(), 10); |
| 3755 | EXPECT_EQ(lconn->GetRttEstimate().GetVariance(), |
| 3756 | std::numeric_limits<double>::infinity()); |
| 3757 | |
| 3758 | SendPingAndReceiveResponse(lconn, rconn, 11); |
| 3759 | |
| 3760 | EXPECT_TRUE(lconn->writable()); |
| 3761 | EXPECT_TRUE(lconn->receiving()); |
| 3762 | EXPECT_NEAR(lconn->GetRttEstimate().GetAverage(), 10, 0.5); |
| 3763 | EXPECT_LT(lconn->GetRttEstimate().GetVariance(), |
| 3764 | std::numeric_limits<double>::infinity()); |
| 3765 | |
| 3766 | lconn->ForgetLearnedState(); |
| 3767 | |
| 3768 | EXPECT_FALSE(lconn->writable()); |
| 3769 | EXPECT_FALSE(lconn->receiving()); |
| 3770 | EXPECT_TRUE(std::isnan(lconn->GetRttEstimate().GetAverage())); |
| 3771 | EXPECT_EQ(lconn->GetRttEstimate().GetVariance(), |
| 3772 | std::numeric_limits<double>::infinity()); |
| 3773 | } |
| 3774 | |
| 3775 | TEST_F(ConnectionTest, ConnectionForgetLearnedStateDiscardsPendingPings) { |
| 3776 | Connection* lconn = CreateConnection(ICEROLE_CONTROLLING); |
| 3777 | Connection* rconn = CreateConnection(ICEROLE_CONTROLLED); |
| 3778 | |
| 3779 | SendPingAndReceiveResponse(lconn, rconn, 10); |
| 3780 | |
| 3781 | EXPECT_TRUE(lconn->writable()); |
| 3782 | EXPECT_TRUE(lconn->receiving()); |
| 3783 | |
| 3784 | rtc::BufferT<uint8_t> reply; |
| 3785 | SendPingAndCaptureReply(lconn, rconn, 10, &reply); |
| 3786 | |
| 3787 | lconn->ForgetLearnedState(); |
| 3788 | |
| 3789 | EXPECT_FALSE(lconn->writable()); |
| 3790 | EXPECT_FALSE(lconn->receiving()); |
| 3791 | |
| 3792 | lconn->OnReadPacket(reply.data<char>(), reply.size(), |
| 3793 | /* packet_time_us */ -1); |
| 3794 | |
| 3795 | // That reply was discarded due to the ForgetLearnedState() while it was |
| 3796 | // outstanding. |
| 3797 | EXPECT_FALSE(lconn->writable()); |
| 3798 | EXPECT_FALSE(lconn->receiving()); |
| 3799 | |
| 3800 | // But sending a new ping and getting a reply works. |
| 3801 | SendPingAndReceiveResponse(lconn, rconn, 11); |
| 3802 | EXPECT_TRUE(lconn->writable()); |
| 3803 | EXPECT_TRUE(lconn->receiving()); |
| 3804 | } |
| 3805 | |
| 3806 | TEST_F(ConnectionTest, ConnectionForgetLearnedStateDoesNotTriggerStateChange) { |
| 3807 | Connection* lconn = CreateConnection(ICEROLE_CONTROLLING); |
| 3808 | Connection* rconn = CreateConnection(ICEROLE_CONTROLLED); |
| 3809 | |
| 3810 | EXPECT_EQ(num_state_changes_, 0); |
| 3811 | SendPingAndReceiveResponse(lconn, rconn, 10); |
| 3812 | |
| 3813 | EXPECT_TRUE(lconn->writable()); |
| 3814 | EXPECT_TRUE(lconn->receiving()); |
| 3815 | EXPECT_EQ(num_state_changes_, 2); |
| 3816 | |
| 3817 | lconn->ForgetLearnedState(); |
| 3818 | |
| 3819 | EXPECT_FALSE(lconn->writable()); |
| 3820 | EXPECT_FALSE(lconn->receiving()); |
| 3821 | EXPECT_EQ(num_state_changes_, 2); |
| 3822 | } |
| 3823 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 3824 | } // namespace cricket |