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