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