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