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