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