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> |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 16 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 17 | #include <memory> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <utility> |
| 20 | #include <vector> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 21 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 22 | #include "absl/types/optional.h" |
| 23 | #include "api/candidate.h" |
Patrik Höglund | 7d00342 | 2019-09-17 12:16:35 +0200 | [diff] [blame] | 24 | #include "api/packet_socket_factory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 25 | #include "api/units/time_delta.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "p2p/base/basic_packet_socket_factory.h" |
| 27 | #include "p2p/base/p2p_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "p2p/base/port_allocator.h" |
| 29 | #include "p2p/base/port_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 30 | #include "p2p/base/stun.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "p2p/base/stun_port.h" |
| 32 | #include "p2p/base/stun_server.h" |
| 33 | #include "p2p/base/tcp_port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "p2p/base/test_stun_server.h" |
| 35 | #include "p2p/base/test_turn_server.h" |
| 36 | #include "p2p/base/transport_description.h" |
| 37 | #include "p2p/base/turn_port.h" |
| 38 | #include "p2p/base/turn_server.h" |
| 39 | #include "p2p/client/relay_port_factory_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 41 | #include "rtc_base/async_packet_socket.h" |
| 42 | #include "rtc_base/async_socket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 44 | #include "rtc_base/byte_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/dscp.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 47 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 48 | #include "rtc_base/gunit.h" |
| 49 | #include "rtc_base/helpers.h" |
| 50 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 51 | #include "rtc_base/nat_server.h" |
| 52 | #include "rtc_base/nat_socket_factory.h" |
| 53 | #include "rtc_base/nat_types.h" |
| 54 | #include "rtc_base/net_helper.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 55 | #include "rtc_base/network.h" |
| 56 | #include "rtc_base/network/sent_packet.h" |
| 57 | #include "rtc_base/network_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 58 | #include "rtc_base/proxy_info.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 59 | #include "rtc_base/socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 60 | #include "rtc_base/socket_adapters.h" |
| 61 | #include "rtc_base/socket_address.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 62 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 63 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 64 | #include "rtc_base/time_utils.h" |
| 65 | #include "rtc_base/virtual_socket_server.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 66 | #include "test/gtest.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | |
| 68 | using rtc::AsyncPacketSocket; |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 69 | using rtc::ByteBufferReader; |
| 70 | using rtc::ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 71 | using rtc::NAT_ADDR_RESTRICTED; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 72 | using rtc::NAT_OPEN_CONE; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 73 | using rtc::NAT_PORT_RESTRICTED; |
| 74 | using rtc::NAT_SYMMETRIC; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 75 | using rtc::NATType; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | using rtc::PacketSocketFactory; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 77 | using rtc::Socket; |
| 78 | using rtc::SocketAddress; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 80 | namespace cricket { |
| 81 | namespace { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 83 | constexpr int kDefaultTimeout = 3000; |
| 84 | constexpr int kShortTimeout = 1000; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 85 | constexpr int kMaxExpectedSimulatedRtt = 200; |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 86 | const SocketAddress kLocalAddr1("192.168.1.2", 0); |
| 87 | const SocketAddress kLocalAddr2("192.168.1.3", 0); |
| 88 | const SocketAddress kNatAddr1("77.77.77.77", rtc::NAT_SERVER_UDP_PORT); |
| 89 | const SocketAddress kNatAddr2("88.88.88.88", rtc::NAT_SERVER_UDP_PORT); |
| 90 | const SocketAddress kStunAddr("99.99.99.1", STUN_SERVER_PORT); |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 91 | const SocketAddress kTurnUdpIntAddr("99.99.99.4", STUN_SERVER_PORT); |
| 92 | const SocketAddress kTurnTcpIntAddr("99.99.99.4", 5010); |
| 93 | const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); |
| 94 | const RelayCredentials kRelayCredentials("test", "test"); |
| 95 | |
| 96 | // TODO(?): Update these when RFC5245 is completely supported. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | // Magic value of 30 is from RFC3484, for IPv4 addresses. |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 98 | const uint32_t kDefaultPrflxPriority = ICE_TYPE_PREFERENCE_PRFLX << 24 | |
| 99 | 30 << 8 | |
| 100 | (256 - ICE_CANDIDATE_COMPONENT_DEFAULT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 101 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 102 | constexpr int kTiebreaker1 = 11111; |
| 103 | constexpr int kTiebreaker2 = 22222; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 104 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 105 | const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 106 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 107 | Candidate GetCandidate(Port* port) { |
| 108 | RTC_DCHECK_GE(port->Candidates().size(), 1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 109 | return port->Candidates()[0]; |
| 110 | } |
| 111 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 112 | SocketAddress GetAddress(Port* port) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 113 | return GetCandidate(port).address(); |
| 114 | } |
| 115 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 116 | std::unique_ptr<IceMessage> CopyStunMessage(const IceMessage& src) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 117 | auto dst = std::make_unique<IceMessage>(); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 118 | ByteBufferWriter buf; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 119 | src.Write(&buf); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 120 | ByteBufferReader read_buf(buf); |
| 121 | dst->Read(&read_buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 122 | return dst; |
| 123 | } |
| 124 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 125 | bool WriteStunMessage(const StunMessage& msg, ByteBufferWriter* buf) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 126 | buf->Resize(0); // clear out any existing buffer contents |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 127 | return msg.Write(buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 130 | } // namespace |
| 131 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 132 | // Stub port class for testing STUN generation and processing. |
| 133 | class TestPort : public Port { |
| 134 | public: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 135 | TestPort(rtc::Thread* thread, |
| 136 | const std::string& type, |
| 137 | rtc::PacketSocketFactory* factory, |
| 138 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 139 | uint16_t min_port, |
| 140 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 141 | const std::string& username_fragment, |
| 142 | const std::string& password) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 143 | : Port(thread, |
| 144 | type, |
| 145 | factory, |
| 146 | network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 147 | min_port, |
| 148 | max_port, |
| 149 | username_fragment, |
| 150 | password) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | ~TestPort() {} |
| 152 | |
| 153 | // Expose GetStunMessage so that we can test it. |
| 154 | using cricket::Port::GetStunMessage; |
| 155 | |
| 156 | // The last StunMessage that was sent on this Port. |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 157 | // TODO(?): Make these const; requires changes to SendXXXXResponse. |
Karl Wiberg | 4b6c2ec | 2018-10-03 15:17:14 +0200 | [diff] [blame] | 158 | 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] | 159 | IceMessage* last_stun_msg() { return last_stun_msg_.get(); } |
| 160 | int last_stun_error_code() { |
| 161 | int code = 0; |
| 162 | if (last_stun_msg_) { |
| 163 | const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode(); |
| 164 | if (error_attr) { |
| 165 | code = error_attr->code(); |
| 166 | } |
| 167 | } |
| 168 | return code; |
| 169 | } |
| 170 | |
| 171 | virtual void PrepareAddress() { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 172 | // Act as if the socket was bound to the best IP on the network, to the |
| 173 | // first port in the allowed range. |
| 174 | rtc::SocketAddress addr(Network()->GetBestIP(), min_port()); |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 175 | AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 176 | ICE_TYPE_PREFERENCE_HOST, 0, "", true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 179 | virtual bool SupportsProtocol(const std::string& protocol) const { |
| 180 | return true; |
| 181 | } |
| 182 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 183 | virtual ProtocolType GetProtocol() const { return PROTO_UDP; } |
| 184 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | // Exposed for testing candidate building. |
| 186 | void AddCandidateAddress(const rtc::SocketAddress& addr) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 187 | AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(), |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 188 | type_preference_, 0, "", false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | } |
| 190 | void AddCandidateAddress(const rtc::SocketAddress& addr, |
| 191 | const rtc::SocketAddress& base_address, |
| 192 | const std::string& type, |
| 193 | int type_preference, |
| 194 | bool final) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 195 | AddAddress(addr, base_address, rtc::SocketAddress(), "udp", "", "", type, |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 196 | type_preference, 0, "", final); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | virtual Connection* CreateConnection(const Candidate& remote_candidate, |
| 200 | CandidateOrigin origin) { |
| 201 | Connection* conn = new ProxyConnection(this, 0, remote_candidate); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 202 | AddOrReplaceConnection(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 203 | // Set use-candidate attribute flag as this will add USE-CANDIDATE attribute |
| 204 | // in STUN binding requests. |
| 205 | conn->set_use_candidate_attr(true); |
| 206 | return conn; |
| 207 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 208 | virtual int SendTo(const void* data, |
| 209 | size_t size, |
| 210 | const rtc::SocketAddress& addr, |
| 211 | const rtc::PacketOptions& options, |
| 212 | bool payload) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | if (!payload) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 214 | auto msg = std::make_unique<IceMessage>(); |
| 215 | auto buf = std::make_unique<rtc::BufferT<uint8_t>>( |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 216 | static_cast<const char*>(data), size); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 217 | ByteBufferReader read_buf(*buf); |
| 218 | if (!msg->Read(&read_buf)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 219 | return -1; |
| 220 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 221 | last_stun_buf_ = std::move(buf); |
| 222 | last_stun_msg_ = std::move(msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 223 | } |
| 224 | return static_cast<int>(size); |
| 225 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 226 | virtual int SetOption(rtc::Socket::Option opt, int value) { return 0; } |
| 227 | virtual int GetOption(rtc::Socket::Option opt, int* value) { return -1; } |
| 228 | virtual int GetError() { return 0; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | void Reset() { |
| 230 | last_stun_buf_.reset(); |
| 231 | last_stun_msg_.reset(); |
| 232 | } |
| 233 | void set_type_preference(int type_preference) { |
| 234 | type_preference_ = type_preference; |
| 235 | } |
| 236 | |
| 237 | private: |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 238 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 239 | const rtc::SentPacket& sent_packet) { |
| 240 | PortInterface::SignalSentPacket(sent_packet); |
| 241 | } |
Karl Wiberg | 4b6c2ec | 2018-10-03 15:17:14 +0200 | [diff] [blame] | 242 | std::unique_ptr<rtc::BufferT<uint8_t>> last_stun_buf_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 243 | std::unique_ptr<IceMessage> last_stun_msg_; |
pbos | 7640ffa | 2015-11-30 09:16:59 -0800 | [diff] [blame] | 244 | int type_preference_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 247 | static void SendPingAndReceiveResponse(Connection* lconn, |
| 248 | TestPort* lport, |
| 249 | Connection* rconn, |
| 250 | TestPort* rport, |
| 251 | rtc::ScopedFakeClock* clock, |
| 252 | int64_t ms) { |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 253 | lconn->Ping(rtc::TimeMillis()); |
| 254 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 255 | ASSERT_TRUE(lport->last_stun_buf()); |
| 256 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 257 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 258 | clock->AdvanceTime(webrtc::TimeDelta::ms(ms)); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 259 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 260 | ASSERT_TRUE(rport->last_stun_buf()); |
| 261 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 262 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 263 | } |
| 264 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 265 | class TestChannel : public sigslot::has_slots<> { |
| 266 | public: |
| 267 | // Takes ownership of |p1| (but not |p2|). |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 268 | explicit TestChannel(std::unique_ptr<Port> p1) : port_(std::move(p1)) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 269 | port_->SignalPortComplete.connect(this, &TestChannel::OnPortComplete); |
| 270 | port_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress); |
| 271 | port_->SignalDestroyed.connect(this, &TestChannel::OnSrcPortDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int complete_count() { return complete_count_; } |
| 275 | Connection* conn() { return conn_; } |
| 276 | const SocketAddress& remote_address() { return remote_address_; } |
| 277 | const std::string remote_fragment() { return remote_frag_; } |
| 278 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 279 | void Start() { port_->PrepareAddress(); } |
| 280 | void CreateConnection(const Candidate& remote_candidate) { |
| 281 | conn_ = port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 282 | IceMode remote_ice_mode = |
| 283 | (ice_mode_ == ICEMODE_FULL) ? ICEMODE_LITE : ICEMODE_FULL; |
| 284 | conn_->set_remote_ice_mode(remote_ice_mode); |
| 285 | conn_->set_use_candidate_attr(remote_ice_mode == ICEMODE_FULL); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 286 | conn_->SignalStateChange.connect(this, |
| 287 | &TestChannel::OnConnectionStateChange); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 288 | conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 289 | conn_->SignalReadyToSend.connect(this, |
| 290 | &TestChannel::OnConnectionReadyToSend); |
| 291 | connection_ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 292 | } |
| 293 | void OnConnectionStateChange(Connection* conn) { |
| 294 | if (conn->write_state() == Connection::STATE_WRITABLE) { |
| 295 | conn->set_use_candidate_attr(true); |
| 296 | nominated_ = true; |
| 297 | } |
| 298 | } |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 299 | void AcceptConnection(const Candidate& remote_candidate) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 300 | ASSERT_TRUE(remote_request_.get() != NULL); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 301 | Candidate c = remote_candidate; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 302 | c.set_address(remote_address_); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 303 | conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 304 | conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 305 | port_->SendBindingResponse(remote_request_.get(), remote_address_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 306 | remote_request_.reset(); |
| 307 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 308 | void Ping() { Ping(0); } |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 309 | void Ping(int64_t now) { conn_->Ping(now); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 310 | void Stop() { |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 311 | if (conn_) { |
| 312 | conn_->Destroy(); |
| 313 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 316 | void OnPortComplete(Port* port) { complete_count_++; } |
| 317 | void SetIceMode(IceMode ice_mode) { ice_mode_ = ice_mode; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 319 | int SendData(const char* data, size_t len) { |
| 320 | rtc::PacketOptions options; |
| 321 | return conn_->Send(data, len, options); |
| 322 | } |
| 323 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 324 | void OnUnknownAddress(PortInterface* port, |
| 325 | const SocketAddress& addr, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 326 | ProtocolType proto, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 327 | IceMessage* msg, |
| 328 | const std::string& rf, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 329 | bool /*port_muxed*/) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 330 | ASSERT_EQ(port_.get(), port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 331 | if (!remote_address_.IsNil()) { |
| 332 | ASSERT_EQ(remote_address_, addr); |
| 333 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 334 | const cricket::StunUInt32Attribute* priority_attr = |
| 335 | msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 336 | const cricket::StunByteStringAttribute* mi_attr = |
| 337 | msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 338 | const cricket::StunUInt32Attribute* fingerprint_attr = |
| 339 | msg->GetUInt32(STUN_ATTR_FINGERPRINT); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 340 | EXPECT_TRUE(priority_attr != NULL); |
| 341 | EXPECT_TRUE(mi_attr != NULL); |
| 342 | EXPECT_TRUE(fingerprint_attr != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 343 | remote_address_ = addr; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 344 | remote_request_ = CopyStunMessage(*msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 345 | remote_frag_ = rf; |
| 346 | } |
| 347 | |
| 348 | void OnDestroyed(Connection* conn) { |
| 349 | ASSERT_EQ(conn_, conn); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 350 | RTC_LOG(INFO) << "OnDestroy connection " << conn << " deleted"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 351 | conn_ = NULL; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 352 | // When the connection is destroyed, also clear these fields so future |
| 353 | // connections are possible. |
| 354 | remote_request_.reset(); |
| 355 | remote_address_.Clear(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void OnSrcPortDestroyed(PortInterface* port) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 359 | Port* destroyed_src = port_.release(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 360 | ASSERT_EQ(destroyed_src, port); |
| 361 | } |
| 362 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 363 | Port* port() { return port_.get(); } |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 364 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 365 | bool nominated() const { return nominated_; } |
| 366 | |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 367 | void set_connection_ready_to_send(bool ready) { |
| 368 | connection_ready_to_send_ = ready; |
| 369 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 370 | bool connection_ready_to_send() const { return connection_ready_to_send_; } |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 371 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 372 | private: |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 373 | // ReadyToSend will only issue after a Connection recovers from ENOTCONN |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 374 | void OnConnectionReadyToSend(Connection* conn) { |
| 375 | ASSERT_EQ(conn, conn_); |
| 376 | connection_ready_to_send_ = true; |
| 377 | } |
| 378 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 379 | IceMode ice_mode_ = ICEMODE_FULL; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 380 | std::unique_ptr<Port> port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 381 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 382 | int complete_count_ = 0; |
| 383 | Connection* conn_ = nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | SocketAddress remote_address_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 385 | std::unique_ptr<StunMessage> remote_request_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 386 | std::string remote_frag_; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 387 | bool nominated_ = false; |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 388 | bool connection_ready_to_send_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 389 | }; |
| 390 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 391 | class PortTest : public ::testing::Test, public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 392 | public: |
| 393 | PortTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 394 | : ss_(new rtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 395 | main_(ss_.get()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 396 | socket_factory_(rtc::Thread::Current()), |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 397 | nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()), |
| 398 | nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 399 | nat_socket_factory1_(&nat_factory1_), |
| 400 | nat_socket_factory2_(&nat_factory2_), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 401 | stun_server_(TestStunServer::Create(&main_, kStunAddr)), |
| 402 | turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 403 | username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), |
| 404 | password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 405 | role_conflict_(false), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 406 | ports_destroyed_(0) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 407 | |
| 408 | protected: |
| 409 | void TestLocalToLocal() { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 410 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 411 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 412 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 413 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 414 | TestConnectivity("udp", std::move(port1), "udp", std::move(port2), true, |
| 415 | true, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 416 | } |
| 417 | void TestLocalToStun(NATType ntype) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 418 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 419 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 420 | nat_server2_ = CreateNatServer(kNatAddr2, ntype); |
| 421 | auto port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 422 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 423 | TestConnectivity("udp", std::move(port1), StunName(ntype), std::move(port2), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 424 | ntype == NAT_OPEN_CONE, true, ntype != NAT_SYMMETRIC, |
| 425 | true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 426 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 427 | void TestLocalToRelay(ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 428 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 429 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 430 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 431 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 432 | TestConnectivity("udp", std::move(port1), RelayName(proto), |
| 433 | std::move(port2), false, true, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 434 | } |
| 435 | void TestStunToLocal(NATType ntype) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 436 | nat_server1_ = CreateNatServer(kNatAddr1, ntype); |
| 437 | auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 438 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 439 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 440 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 441 | TestConnectivity(StunName(ntype), std::move(port1), "udp", std::move(port2), |
| 442 | true, ntype != NAT_SYMMETRIC, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 443 | } |
| 444 | void TestStunToStun(NATType ntype1, NATType ntype2) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 445 | nat_server1_ = CreateNatServer(kNatAddr1, ntype1); |
| 446 | auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 447 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 448 | nat_server2_ = CreateNatServer(kNatAddr2, ntype2); |
| 449 | auto port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 450 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 451 | TestConnectivity(StunName(ntype1), std::move(port1), StunName(ntype2), |
| 452 | std::move(port2), ntype2 == NAT_OPEN_CONE, |
| 453 | ntype1 != NAT_SYMMETRIC, ntype2 != NAT_SYMMETRIC, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 454 | ntype1 + ntype2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC)); |
| 455 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 456 | void TestStunToRelay(NATType ntype, ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 457 | nat_server1_ = CreateNatServer(kNatAddr1, ntype); |
| 458 | auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 459 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 460 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_UDP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 461 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 462 | TestConnectivity(StunName(ntype), std::move(port1), RelayName(proto), |
| 463 | std::move(port2), false, ntype != NAT_SYMMETRIC, true, |
| 464 | true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 465 | } |
| 466 | void TestTcpToTcp() { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 467 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 468 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 469 | auto port2 = CreateTcpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 470 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 471 | TestConnectivity("tcp", std::move(port1), "tcp", std::move(port2), true, |
| 472 | false, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 473 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 474 | void TestTcpToRelay(ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 475 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 476 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 477 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_TCP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 478 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 479 | TestConnectivity("tcp", std::move(port1), RelayName(proto), |
| 480 | std::move(port2), false, false, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 481 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 482 | void TestSslTcpToRelay(ProtocolType proto) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 483 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 484 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 485 | auto port2 = CreateRelayPort(kLocalAddr2, proto, PROTO_SSLTCP); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 486 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 487 | TestConnectivity("ssltcp", std::move(port1), RelayName(proto), |
| 488 | std::move(port2), false, false, true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 489 | } |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 490 | |
| 491 | rtc::Network* MakeNetwork(const SocketAddress& addr) { |
| 492 | networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32); |
| 493 | networks_.back().AddIP(addr.ipaddr()); |
| 494 | return &networks_.back(); |
| 495 | } |
| 496 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 497 | // helpers for above functions |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 498 | std::unique_ptr<UDPPort> CreateUdpPort(const SocketAddress& addr) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 499 | return CreateUdpPort(addr, &socket_factory_); |
| 500 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 501 | std::unique_ptr<UDPPort> CreateUdpPort(const SocketAddress& addr, |
| 502 | PacketSocketFactory* socket_factory) { |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 503 | return UDPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
| 504 | username_, password_, std::string(), true, |
| 505 | absl::nullopt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 506 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 507 | std::unique_ptr<TCPPort> CreateTcpPort(const SocketAddress& addr) { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 508 | return CreateTcpPort(addr, &socket_factory_); |
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, |
| 511 | PacketSocketFactory* socket_factory) { |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 512 | return TCPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
| 513 | username_, password_, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 514 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 515 | std::unique_ptr<StunPort> CreateStunPort(const SocketAddress& addr, |
| 516 | rtc::PacketSocketFactory* factory) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 517 | ServerAddresses stun_servers; |
| 518 | stun_servers.insert(kStunAddr); |
Steve Anton | a8f1e56 | 2018-10-10 11:29:44 -0700 | [diff] [blame] | 519 | return StunPort::Create(&main_, factory, MakeNetwork(addr), 0, 0, username_, |
| 520 | password_, stun_servers, std::string(), |
| 521 | absl::nullopt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 522 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 523 | std::unique_ptr<Port> CreateRelayPort(const SocketAddress& addr, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 524 | ProtocolType int_proto, |
| 525 | ProtocolType ext_proto) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 526 | return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 527 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 528 | std::unique_ptr<TurnPort> CreateTurnPort(const SocketAddress& addr, |
| 529 | PacketSocketFactory* socket_factory, |
| 530 | ProtocolType int_proto, |
| 531 | ProtocolType ext_proto) { |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 532 | SocketAddress server_addr = |
| 533 | int_proto == PROTO_TCP ? kTurnTcpIntAddr : kTurnUdpIntAddr; |
| 534 | return CreateTurnPort(addr, socket_factory, int_proto, ext_proto, |
| 535 | server_addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 536 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 537 | std::unique_ptr<TurnPort> CreateTurnPort( |
| 538 | const SocketAddress& addr, |
| 539 | PacketSocketFactory* socket_factory, |
| 540 | ProtocolType int_proto, |
| 541 | ProtocolType ext_proto, |
| 542 | const rtc::SocketAddress& server_addr) { |
Steve Anton | f7dd9df | 2018-10-10 15:06:28 -0700 | [diff] [blame] | 543 | return TurnPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0, |
| 544 | username_, password_, |
| 545 | ProtocolAddress(server_addr, int_proto), |
| 546 | kRelayCredentials, 0, "", {}, {}, nullptr, nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 547 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 548 | std::unique_ptr<rtc::NATServer> CreateNatServer(const SocketAddress& addr, |
| 549 | rtc::NATType type) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 550 | return std::make_unique<rtc::NATServer>(type, ss_.get(), addr, addr, |
| 551 | ss_.get(), addr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 552 | } |
| 553 | static const char* StunName(NATType type) { |
| 554 | switch (type) { |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 555 | case NAT_OPEN_CONE: |
| 556 | return "stun(open cone)"; |
| 557 | case NAT_ADDR_RESTRICTED: |
| 558 | return "stun(addr restricted)"; |
| 559 | case NAT_PORT_RESTRICTED: |
| 560 | return "stun(port restricted)"; |
| 561 | case NAT_SYMMETRIC: |
| 562 | return "stun(symmetric)"; |
| 563 | default: |
| 564 | return "stun(?)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 567 | static const char* RelayName(ProtocolType proto) { |
| 568 | switch (proto) { |
| 569 | case PROTO_UDP: |
| 570 | return "turn(udp)"; |
| 571 | case PROTO_TCP: |
| 572 | return "turn(tcp)"; |
| 573 | case PROTO_SSLTCP: |
| 574 | return "turn(ssltcp)"; |
| 575 | case PROTO_TLS: |
| 576 | return "turn(tls)"; |
| 577 | default: |
| 578 | return "turn(?)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | void TestCrossFamilyPorts(int type); |
| 583 | |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 584 | void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2); |
| 585 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 586 | // This does all the work and then deletes |port1| and |port2|. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 587 | void TestConnectivity(const char* name1, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 588 | std::unique_ptr<Port> port1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 589 | const char* name2, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 590 | std::unique_ptr<Port> port2, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 591 | bool accept, |
| 592 | bool same_addr1, |
| 593 | bool same_addr2, |
| 594 | bool possible); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 595 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 596 | // This connects the provided channels which have already started. |ch1| |
| 597 | // should have its Connection created (either through CreateConnection() or |
| 598 | // TCP reconnecting mechanism before entering this function. |
| 599 | void ConnectStartedChannels(TestChannel* ch1, TestChannel* ch2) { |
| 600 | ASSERT_TRUE(ch1->conn()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 601 | EXPECT_TRUE_WAIT(ch1->conn()->connected(), |
| 602 | kDefaultTimeout); // for TCP connect |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 603 | ch1->Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 604 | WAIT(!ch2->remote_address().IsNil(), kShortTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 605 | |
| 606 | // Send a ping from dst to src. |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 607 | ch2->AcceptConnection(GetCandidate(ch1->port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 608 | ch2->Ping(); |
| 609 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2->conn()->write_state(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 610 | kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 611 | } |
| 612 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 613 | // This connects and disconnects the provided channels in the same sequence as |
| 614 | // TestConnectivity with all options set to |true|. It does not delete either |
| 615 | // channel. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 616 | void StartConnectAndStopChannels(TestChannel* ch1, TestChannel* ch2) { |
| 617 | // Acquire addresses. |
| 618 | ch1->Start(); |
| 619 | ch2->Start(); |
| 620 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 621 | ch1->CreateConnection(GetCandidate(ch2->port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 622 | ConnectStartedChannels(ch1, ch2); |
| 623 | |
| 624 | // Destroy the connections. |
| 625 | ch1->Stop(); |
| 626 | ch2->Stop(); |
| 627 | } |
| 628 | |
| 629 | // This disconnects both end's Connection and make sure ch2 ready for new |
| 630 | // connection. |
| 631 | void DisconnectTcpTestChannels(TestChannel* ch1, TestChannel* ch2) { |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 632 | TCPConnection* tcp_conn1 = static_cast<TCPConnection*>(ch1->conn()); |
| 633 | TCPConnection* tcp_conn2 = static_cast<TCPConnection*>(ch2->conn()); |
| 634 | ASSERT_TRUE( |
| 635 | ss_->CloseTcpConnections(tcp_conn1->socket()->GetLocalAddress(), |
| 636 | tcp_conn2->socket()->GetLocalAddress())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 637 | |
| 638 | // Wait for both OnClose are delivered. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 639 | EXPECT_TRUE_WAIT(!ch1->conn()->connected(), kDefaultTimeout); |
| 640 | EXPECT_TRUE_WAIT(!ch2->conn()->connected(), kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 641 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 642 | // Ensure redundant SignalClose events on TcpConnection won't break tcp |
| 643 | // reconnection. Chromium will fire SignalClose for all outstanding IPC |
| 644 | // packets during reconnection. |
| 645 | tcp_conn1->socket()->SignalClose(tcp_conn1->socket(), 0); |
| 646 | tcp_conn2->socket()->SignalClose(tcp_conn2->socket(), 0); |
| 647 | |
| 648 | // Speed up destroying ch2's connection such that the test is ready to |
| 649 | // 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] | 650 | ch2->conn()->Destroy(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 651 | EXPECT_TRUE_WAIT(ch2->conn() == NULL, kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | void TestTcpReconnect(bool ping_after_disconnected, |
| 655 | bool send_after_disconnected) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 656 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 657 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 658 | auto port2 = CreateTcpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 659 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 660 | |
| 661 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 662 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 663 | |
| 664 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 665 | TestChannel ch1(std::move(port1)); |
| 666 | TestChannel ch2(std::move(port2)); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 667 | EXPECT_EQ(0, ch1.complete_count()); |
| 668 | EXPECT_EQ(0, ch2.complete_count()); |
| 669 | |
| 670 | ch1.Start(); |
| 671 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 672 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 673 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 674 | |
| 675 | // Initial connecting the channel, create connection on channel1. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 676 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 677 | ConnectStartedChannels(&ch1, &ch2); |
| 678 | |
| 679 | // Shorten the timeout period. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 680 | const int kTcpReconnectTimeout = kDefaultTimeout; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 681 | static_cast<TCPConnection*>(ch1.conn()) |
| 682 | ->set_reconnection_timeout(kTcpReconnectTimeout); |
| 683 | static_cast<TCPConnection*>(ch2.conn()) |
| 684 | ->set_reconnection_timeout(kTcpReconnectTimeout); |
| 685 | |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 686 | EXPECT_FALSE(ch1.connection_ready_to_send()); |
| 687 | EXPECT_FALSE(ch2.connection_ready_to_send()); |
| 688 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 689 | // Once connected, disconnect them. |
| 690 | DisconnectTcpTestChannels(&ch1, &ch2); |
| 691 | |
| 692 | if (send_after_disconnected || ping_after_disconnected) { |
| 693 | if (send_after_disconnected) { |
| 694 | // First SendData after disconnect should fail but will trigger |
| 695 | // reconnect. |
| 696 | EXPECT_EQ(-1, ch1.SendData(data, static_cast<int>(strlen(data)))); |
| 697 | } |
| 698 | |
| 699 | if (ping_after_disconnected) { |
| 700 | // Ping should trigger reconnect. |
| 701 | ch1.Ping(); |
| 702 | } |
| 703 | |
| 704 | // Wait for channel's outgoing TCPConnection connected. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 705 | EXPECT_TRUE_WAIT(ch1.conn()->connected(), kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 706 | |
| 707 | // Verify that we could still connect channels. |
| 708 | ConnectStartedChannels(&ch1, &ch2); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 709 | EXPECT_TRUE_WAIT(ch1.connection_ready_to_send(), kTcpReconnectTimeout); |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 710 | // Channel2 is the passive one so a new connection is created during |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 711 | // reconnect. This new connection should never have issued ENOTCONN |
Guo-wei Shieh | b594041 | 2015-08-24 11:58:03 -0700 | [diff] [blame] | 712 | // hence the connection_ready_to_send() should be false. |
| 713 | EXPECT_FALSE(ch2.connection_ready_to_send()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 714 | } else { |
| 715 | EXPECT_EQ(ch1.conn()->write_state(), Connection::STATE_WRITABLE); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 716 | // Since the reconnection never happens, the connections should have been |
| 717 | // destroyed after the timeout. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 718 | EXPECT_TRUE_WAIT(!ch1.conn(), kTcpReconnectTimeout + kDefaultTimeout); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 719 | EXPECT_TRUE(!ch2.conn()); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // Tear down and ensure that goes smoothly. |
| 723 | ch1.Stop(); |
| 724 | ch2.Stop(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 725 | EXPECT_TRUE_WAIT(ch1.conn() == NULL, kDefaultTimeout); |
| 726 | EXPECT_TRUE_WAIT(ch2.conn() == NULL, kDefaultTimeout); |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 727 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 728 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 729 | std::unique_ptr<IceMessage> CreateStunMessage(int type) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 730 | auto msg = std::make_unique<IceMessage>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 731 | msg->SetType(type); |
| 732 | msg->SetTransactionID("TESTTESTTEST"); |
| 733 | return msg; |
| 734 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 735 | std::unique_ptr<IceMessage> CreateStunMessageWithUsername( |
| 736 | int type, |
| 737 | const std::string& username) { |
| 738 | std::unique_ptr<IceMessage> msg = CreateStunMessage(type); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 739 | msg->AddAttribute(std::make_unique<StunByteStringAttribute>( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 740 | STUN_ATTR_USERNAME, username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 741 | return msg; |
| 742 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 743 | std::unique_ptr<TestPort> CreateTestPort(const rtc::SocketAddress& addr, |
| 744 | const std::string& username, |
| 745 | const std::string& password) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 746 | auto port = |
| 747 | std::make_unique<TestPort>(&main_, "test", &socket_factory_, |
| 748 | MakeNetwork(addr), 0, 0, username, password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 749 | port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
| 750 | return port; |
| 751 | } |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 752 | std::unique_ptr<TestPort> CreateTestPort(const rtc::SocketAddress& addr, |
| 753 | const std::string& username, |
| 754 | const std::string& password, |
| 755 | cricket::IceRole role, |
| 756 | int tiebreaker) { |
| 757 | auto port = CreateTestPort(addr, username, password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 758 | port->SetIceRole(role); |
| 759 | port->SetIceTiebreaker(tiebreaker); |
| 760 | return port; |
| 761 | } |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 762 | // Overload to create a test port given an rtc::Network directly. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 763 | std::unique_ptr<TestPort> CreateTestPort(rtc::Network* network, |
| 764 | const std::string& username, |
| 765 | const std::string& password) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 766 | auto port = std::make_unique<TestPort>(&main_, "test", &socket_factory_, |
| 767 | network, 0, 0, username, password); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 768 | port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict); |
| 769 | return port; |
| 770 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 771 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 772 | void OnRoleConflict(PortInterface* port) { role_conflict_ = true; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 773 | bool role_conflict() const { return role_conflict_; } |
| 774 | |
| 775 | void ConnectToSignalDestroyed(PortInterface* port) { |
| 776 | port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); |
| 777 | } |
| 778 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 779 | void OnDestroyed(PortInterface* port) { ++ports_destroyed_; } |
| 780 | int ports_destroyed() const { return ports_destroyed_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 781 | |
| 782 | rtc::BasicPacketSocketFactory* nat_socket_factory1() { |
| 783 | return &nat_socket_factory1_; |
| 784 | } |
| 785 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 786 | rtc::VirtualSocketServer* vss() { return ss_.get(); } |
| 787 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 788 | private: |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 789 | // When a "create port" helper method is called with an IP, we create a |
| 790 | // Network with that IP and add it to this list. Using a list instead of a |
| 791 | // vector so that when it grows, pointers aren't invalidated. |
| 792 | std::list<rtc::Network> networks_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 793 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 794 | rtc::AutoSocketServerThread main_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 795 | rtc::BasicPacketSocketFactory socket_factory_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 796 | std::unique_ptr<rtc::NATServer> nat_server1_; |
| 797 | std::unique_ptr<rtc::NATServer> nat_server2_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 798 | rtc::NATSocketFactory nat_factory1_; |
| 799 | rtc::NATSocketFactory nat_factory2_; |
| 800 | rtc::BasicPacketSocketFactory nat_socket_factory1_; |
| 801 | rtc::BasicPacketSocketFactory nat_socket_factory2_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 802 | std::unique_ptr<TestStunServer> stun_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 803 | TestTurnServer turn_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 804 | std::string username_; |
| 805 | std::string password_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 806 | bool role_conflict_; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 807 | int ports_destroyed_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 808 | }; |
| 809 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 810 | void PortTest::TestConnectivity(const char* name1, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 811 | std::unique_ptr<Port> port1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 812 | const char* name2, |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 813 | std::unique_ptr<Port> port2, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 814 | bool accept, |
| 815 | bool same_addr1, |
| 816 | bool same_addr2, |
| 817 | bool possible) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 818 | rtc::ScopedFakeClock clock; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 819 | RTC_LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": "; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 820 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 821 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 822 | |
| 823 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 824 | TestChannel ch1(std::move(port1)); |
| 825 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 826 | EXPECT_EQ(0, ch1.complete_count()); |
| 827 | EXPECT_EQ(0, ch2.complete_count()); |
| 828 | |
| 829 | // Acquire addresses. |
| 830 | ch1.Start(); |
| 831 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 832 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 833 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 834 | |
| 835 | // 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] | 836 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 837 | ASSERT_TRUE(ch1.conn() != NULL); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 838 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, |
| 839 | clock); // for TCP connect |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 840 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 841 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 842 | |
| 843 | if (accept) { |
| 844 | // We are able to send a ping from src to dst. This is the case when |
| 845 | // sending to UDP ports and cone NATs. |
| 846 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 847 | EXPECT_EQ(ch2.remote_fragment(), ch1.port()->username_fragment()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 848 | |
| 849 | // Ensure the ping came from the same address used for src. |
| 850 | // This is the case unless the source NAT was symmetric. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 851 | if (same_addr1) |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 852 | EXPECT_EQ(ch2.remote_address(), GetAddress(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 853 | EXPECT_TRUE(same_addr2); |
| 854 | |
| 855 | // Send a ping from dst to src. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 856 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 857 | ASSERT_TRUE(ch2.conn() != NULL); |
| 858 | ch2.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 859 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 860 | ch2.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 861 | } else { |
| 862 | // We can't send a ping from src to dst, so flip it around. This will happen |
| 863 | // when the destination NAT is addr/port restricted or symmetric. |
| 864 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 865 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 866 | |
| 867 | // 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] | 868 | ch2.CreateConnection(GetCandidate(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 869 | ASSERT_TRUE(ch2.conn() != NULL); |
| 870 | ch2.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 871 | SIMULATED_WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE, |
| 872 | kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 873 | |
| 874 | if (same_addr1 && same_addr2) { |
| 875 | // The new ping got back to the source. |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 876 | EXPECT_TRUE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 877 | EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 878 | |
| 879 | // First connection may not be writable if the first ping did not get |
| 880 | // through. So we will have to do another. |
| 881 | if (ch1.conn()->write_state() == Connection::STATE_WRITE_INIT) { |
| 882 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 883 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 884 | ch1.conn()->write_state(), kDefaultTimeout, |
| 885 | clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 886 | } |
| 887 | } else if (!same_addr1 && possible) { |
| 888 | // The new ping went to the candidate address, but that address was bad. |
| 889 | // This will happen when the source NAT is symmetric. |
| 890 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 891 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 892 | |
| 893 | // However, since we have now sent a ping to the source IP, we should be |
| 894 | // able to get a ping from it. This gives us the real source address. |
| 895 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 896 | EXPECT_TRUE_SIMULATED_WAIT(!ch2.remote_address().IsNil(), kDefaultTimeout, |
| 897 | clock); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 898 | EXPECT_FALSE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 899 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 900 | |
| 901 | // Pick up the actual address and establish the connection. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 902 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 903 | ASSERT_TRUE(ch2.conn() != NULL); |
| 904 | ch2.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 905 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 906 | ch2.conn()->write_state(), kDefaultTimeout, |
| 907 | clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 908 | } else if (!same_addr2 && possible) { |
| 909 | // The new ping came in, but from an unexpected address. This will happen |
| 910 | // when the destination NAT is symmetric. |
| 911 | EXPECT_FALSE(ch1.remote_address().IsNil()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 912 | EXPECT_FALSE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 913 | |
| 914 | // Update our address and complete the connection. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 915 | ch1.AcceptConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 916 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 917 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 918 | ch1.conn()->write_state(), kDefaultTimeout, |
| 919 | clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 920 | } else { // (!possible) |
| 921 | // There should be s no way for the pings to reach each other. Check it. |
| 922 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 923 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 924 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 925 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 926 | EXPECT_TRUE(ch1.remote_address().IsNil()); |
| 927 | EXPECT_TRUE(ch2.remote_address().IsNil()); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // Everything should be good, unless we know the situation is impossible. |
| 932 | ASSERT_TRUE(ch1.conn() != NULL); |
| 933 | ASSERT_TRUE(ch2.conn() != NULL); |
| 934 | if (possible) { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 935 | EXPECT_TRUE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 936 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 937 | EXPECT_TRUE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 938 | EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 939 | } else { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 940 | EXPECT_FALSE(ch1.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 941 | EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 942 | EXPECT_FALSE(ch2.conn()->receiving()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 943 | EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state()); |
| 944 | } |
| 945 | |
| 946 | // Tear down and ensure that goes smoothly. |
| 947 | ch1.Stop(); |
| 948 | ch2.Stop(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 949 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn() == NULL, kDefaultTimeout, clock); |
| 950 | EXPECT_TRUE_SIMULATED_WAIT(ch2.conn() == NULL, kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 951 | } |
| 952 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 953 | class FakePacketSocketFactory : public rtc::PacketSocketFactory { |
| 954 | public: |
| 955 | FakePacketSocketFactory() |
| 956 | : next_udp_socket_(NULL), |
| 957 | next_server_tcp_socket_(NULL), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 958 | next_client_tcp_socket_(NULL) {} |
| 959 | ~FakePacketSocketFactory() override {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 960 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 961 | AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 962 | uint16_t min_port, |
| 963 | uint16_t max_port) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 964 | EXPECT_TRUE(next_udp_socket_ != NULL); |
| 965 | AsyncPacketSocket* result = next_udp_socket_; |
| 966 | next_udp_socket_ = NULL; |
| 967 | return result; |
| 968 | } |
| 969 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 970 | AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 971 | uint16_t min_port, |
| 972 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 973 | int opts) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 974 | EXPECT_TRUE(next_server_tcp_socket_ != NULL); |
| 975 | AsyncPacketSocket* result = next_server_tcp_socket_; |
| 976 | next_server_tcp_socket_ = NULL; |
| 977 | return result; |
| 978 | } |
| 979 | |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame] | 980 | AsyncPacketSocket* CreateClientTcpSocket( |
| 981 | const SocketAddress& local_address, |
| 982 | const SocketAddress& remote_address, |
| 983 | const rtc::ProxyInfo& proxy_info, |
| 984 | const std::string& user_agent, |
| 985 | const rtc::PacketSocketTcpOptions& opts) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 986 | EXPECT_TRUE(next_client_tcp_socket_ != NULL); |
| 987 | AsyncPacketSocket* result = next_client_tcp_socket_; |
| 988 | next_client_tcp_socket_ = NULL; |
| 989 | return result; |
| 990 | } |
| 991 | |
| 992 | void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) { |
| 993 | next_udp_socket_ = next_udp_socket; |
| 994 | } |
| 995 | void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) { |
| 996 | next_server_tcp_socket_ = next_server_tcp_socket; |
| 997 | } |
| 998 | void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) { |
| 999 | next_client_tcp_socket_ = next_client_tcp_socket; |
| 1000 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1001 | rtc::AsyncResolverInterface* CreateAsyncResolver() override { return NULL; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1002 | |
| 1003 | private: |
| 1004 | AsyncPacketSocket* next_udp_socket_; |
| 1005 | AsyncPacketSocket* next_server_tcp_socket_; |
| 1006 | AsyncPacketSocket* next_client_tcp_socket_; |
| 1007 | }; |
| 1008 | |
| 1009 | class FakeAsyncPacketSocket : public AsyncPacketSocket { |
| 1010 | public: |
| 1011 | // Returns current local address. Address may be set to NULL if the |
| 1012 | // socket is not bound yet (GetState() returns STATE_BINDING). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1013 | virtual SocketAddress GetLocalAddress() const { return SocketAddress(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Returns remote address. Returns zeroes if this is not a client TCP socket. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1016 | virtual SocketAddress GetRemoteAddress() const { return SocketAddress(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Send a packet. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1019 | virtual int Send(const void* pv, |
| 1020 | size_t cb, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1021 | const rtc::PacketOptions& options) { |
| 1022 | return static_cast<int>(cb); |
| 1023 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1024 | virtual int SendTo(const void* pv, |
| 1025 | size_t cb, |
| 1026 | const SocketAddress& addr, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1027 | const rtc::PacketOptions& options) { |
| 1028 | return static_cast<int>(cb); |
| 1029 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1030 | virtual int Close() { return 0; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1031 | |
| 1032 | virtual State GetState() const { return state_; } |
| 1033 | virtual int GetOption(Socket::Option opt, int* value) { return 0; } |
| 1034 | virtual int SetOption(Socket::Option opt, int value) { return 0; } |
| 1035 | virtual int GetError() const { return 0; } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1036 | virtual void SetError(int error) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1037 | |
| 1038 | void set_state(State state) { state_ = state; } |
| 1039 | |
| 1040 | private: |
| 1041 | State state_; |
| 1042 | }; |
| 1043 | |
| 1044 | // Local -> XXXX |
| 1045 | TEST_F(PortTest, TestLocalToLocal) { |
| 1046 | TestLocalToLocal(); |
| 1047 | } |
| 1048 | |
| 1049 | TEST_F(PortTest, TestLocalToConeNat) { |
| 1050 | TestLocalToStun(NAT_OPEN_CONE); |
| 1051 | } |
| 1052 | |
| 1053 | TEST_F(PortTest, TestLocalToARNat) { |
| 1054 | TestLocalToStun(NAT_ADDR_RESTRICTED); |
| 1055 | } |
| 1056 | |
| 1057 | TEST_F(PortTest, TestLocalToPRNat) { |
| 1058 | TestLocalToStun(NAT_PORT_RESTRICTED); |
| 1059 | } |
| 1060 | |
| 1061 | TEST_F(PortTest, TestLocalToSymNat) { |
| 1062 | TestLocalToStun(NAT_SYMMETRIC); |
| 1063 | } |
| 1064 | |
| 1065 | // Flaky: https://code.google.com/p/webrtc/issues/detail?id=3316. |
| 1066 | TEST_F(PortTest, DISABLED_TestLocalToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1067 | TestLocalToRelay(PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // Cone NAT -> XXXX |
| 1071 | TEST_F(PortTest, TestConeNatToLocal) { |
| 1072 | TestStunToLocal(NAT_OPEN_CONE); |
| 1073 | } |
| 1074 | |
| 1075 | TEST_F(PortTest, TestConeNatToConeNat) { |
| 1076 | TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE); |
| 1077 | } |
| 1078 | |
| 1079 | TEST_F(PortTest, TestConeNatToARNat) { |
| 1080 | TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED); |
| 1081 | } |
| 1082 | |
| 1083 | TEST_F(PortTest, TestConeNatToPRNat) { |
| 1084 | TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED); |
| 1085 | } |
| 1086 | |
| 1087 | TEST_F(PortTest, TestConeNatToSymNat) { |
| 1088 | TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC); |
| 1089 | } |
| 1090 | |
| 1091 | TEST_F(PortTest, TestConeNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1092 | TestStunToRelay(NAT_OPEN_CONE, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | // Address-restricted NAT -> XXXX |
| 1096 | TEST_F(PortTest, TestARNatToLocal) { |
| 1097 | TestStunToLocal(NAT_ADDR_RESTRICTED); |
| 1098 | } |
| 1099 | |
| 1100 | TEST_F(PortTest, TestARNatToConeNat) { |
| 1101 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE); |
| 1102 | } |
| 1103 | |
| 1104 | TEST_F(PortTest, TestARNatToARNat) { |
| 1105 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED); |
| 1106 | } |
| 1107 | |
| 1108 | TEST_F(PortTest, TestARNatToPRNat) { |
| 1109 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED); |
| 1110 | } |
| 1111 | |
| 1112 | TEST_F(PortTest, TestARNatToSymNat) { |
| 1113 | TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC); |
| 1114 | } |
| 1115 | |
| 1116 | TEST_F(PortTest, TestARNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1117 | TestStunToRelay(NAT_ADDR_RESTRICTED, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | // Port-restricted NAT -> XXXX |
| 1121 | TEST_F(PortTest, TestPRNatToLocal) { |
| 1122 | TestStunToLocal(NAT_PORT_RESTRICTED); |
| 1123 | } |
| 1124 | |
| 1125 | TEST_F(PortTest, TestPRNatToConeNat) { |
| 1126 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE); |
| 1127 | } |
| 1128 | |
| 1129 | TEST_F(PortTest, TestPRNatToARNat) { |
| 1130 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED); |
| 1131 | } |
| 1132 | |
| 1133 | TEST_F(PortTest, TestPRNatToPRNat) { |
| 1134 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED); |
| 1135 | } |
| 1136 | |
| 1137 | TEST_F(PortTest, TestPRNatToSymNat) { |
| 1138 | // Will "fail" |
| 1139 | TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC); |
| 1140 | } |
| 1141 | |
| 1142 | TEST_F(PortTest, TestPRNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1143 | TestStunToRelay(NAT_PORT_RESTRICTED, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | // Symmetric NAT -> XXXX |
| 1147 | TEST_F(PortTest, TestSymNatToLocal) { |
| 1148 | TestStunToLocal(NAT_SYMMETRIC); |
| 1149 | } |
| 1150 | |
| 1151 | TEST_F(PortTest, TestSymNatToConeNat) { |
| 1152 | TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE); |
| 1153 | } |
| 1154 | |
| 1155 | TEST_F(PortTest, TestSymNatToARNat) { |
| 1156 | TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED); |
| 1157 | } |
| 1158 | |
| 1159 | TEST_F(PortTest, TestSymNatToPRNat) { |
| 1160 | // Will "fail" |
| 1161 | TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED); |
| 1162 | } |
| 1163 | |
| 1164 | TEST_F(PortTest, TestSymNatToSymNat) { |
| 1165 | // Will "fail" |
| 1166 | TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC); |
| 1167 | } |
| 1168 | |
| 1169 | TEST_F(PortTest, TestSymNatToTurn) { |
Niels Möller | 191e38f | 2019-11-04 08:49:12 +0100 | [diff] [blame] | 1170 | TestStunToRelay(NAT_SYMMETRIC, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | // Outbound TCP -> XXXX |
| 1174 | TEST_F(PortTest, TestTcpToTcp) { |
| 1175 | TestTcpToTcp(); |
| 1176 | } |
| 1177 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 1178 | TEST_F(PortTest, TestTcpReconnectOnSendPacket) { |
| 1179 | TestTcpReconnect(false /* ping */, true /* send */); |
| 1180 | } |
| 1181 | |
| 1182 | TEST_F(PortTest, TestTcpReconnectOnPing) { |
| 1183 | TestTcpReconnect(true /* ping */, false /* send */); |
| 1184 | } |
| 1185 | |
| 1186 | TEST_F(PortTest, TestTcpReconnectTimeout) { |
| 1187 | TestTcpReconnect(false /* ping */, false /* send */); |
| 1188 | } |
| 1189 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1190 | // Test when TcpConnection never connects, the OnClose() will be called to |
| 1191 | // destroy the connection. |
| 1192 | TEST_F(PortTest, TestTcpNeverConnect) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1193 | auto port1 = CreateTcpPort(kLocalAddr1); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1194 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1195 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 1196 | |
| 1197 | // Set up a channel and ensure the port will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1198 | TestChannel ch1(std::move(port1)); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1199 | EXPECT_EQ(0, ch1.complete_count()); |
| 1200 | |
| 1201 | ch1.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1202 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1203 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1204 | std::unique_ptr<rtc::AsyncSocket> server( |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1205 | vss()->CreateAsyncSocket(kLocalAddr2.family(), SOCK_STREAM)); |
| 1206 | // Bind but not listen. |
| 1207 | EXPECT_EQ(0, server->Bind(kLocalAddr2)); |
| 1208 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1209 | Candidate c = GetCandidate(ch1.port()); |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1210 | c.set_address(server->GetLocalAddress()); |
| 1211 | |
| 1212 | ch1.CreateConnection(c); |
| 1213 | EXPECT_TRUE(ch1.conn()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1214 | EXPECT_TRUE_WAIT(!ch1.conn(), kDefaultTimeout); // for TCP connect |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 1217 | /* TODO(?): Enable these once testrelayserver can accept external TCP. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1218 | TEST_F(PortTest, TestTcpToTcpRelay) { |
| 1219 | TestTcpToRelay(PROTO_TCP); |
| 1220 | } |
| 1221 | |
| 1222 | TEST_F(PortTest, TestTcpToSslTcpRelay) { |
| 1223 | TestTcpToRelay(PROTO_SSLTCP); |
| 1224 | } |
| 1225 | */ |
| 1226 | |
| 1227 | // Outbound SSLTCP -> XXXX |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 1228 | /* TODO(?): Enable these once testrelayserver can accept external SSL. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1229 | TEST_F(PortTest, TestSslTcpToTcpRelay) { |
| 1230 | TestSslTcpToRelay(PROTO_TCP); |
| 1231 | } |
| 1232 | |
| 1233 | TEST_F(PortTest, TestSslTcpToSslTcpRelay) { |
| 1234 | TestSslTcpToRelay(PROTO_SSLTCP); |
| 1235 | } |
| 1236 | */ |
| 1237 | |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1238 | // Test that a connection will be dead and deleted if |
| 1239 | // i) it has never received anything for MIN_CONNECTION_LIFETIME milliseconds |
| 1240 | // since it was created, or |
| 1241 | // ii) it has not received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT |
| 1242 | // milliseconds since last receiving. |
| 1243 | TEST_F(PortTest, TestConnectionDead) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1244 | TestChannel ch1(CreateUdpPort(kLocalAddr1)); |
| 1245 | TestChannel ch2(CreateUdpPort(kLocalAddr2)); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1246 | // Acquire address. |
| 1247 | ch1.Start(); |
| 1248 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1249 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
| 1250 | ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1251 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1252 | // Test case that the connection has never received anything. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1253 | int64_t before_created = rtc::TimeMillis(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1254 | ch1.CreateConnection(GetCandidate(ch2.port())); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1255 | int64_t after_created = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1256 | Connection* conn = ch1.conn(); |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 1257 | ASSERT_NE(conn, nullptr); |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1258 | // It is not dead if it is after MIN_CONNECTION_LIFETIME but not pruned. |
| 1259 | conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); |
| 1260 | rtc::Thread::Current()->ProcessMessages(0); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1261 | EXPECT_TRUE(ch1.conn() != nullptr); |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1262 | // It is not dead if it is before MIN_CONNECTION_LIFETIME and pruned. |
| 1263 | conn->UpdateState(before_created + MIN_CONNECTION_LIFETIME - 1); |
| 1264 | conn->Prune(); |
| 1265 | rtc::Thread::Current()->ProcessMessages(0); |
| 1266 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1267 | // It will be dead after MIN_CONNECTION_LIFETIME and pruned. |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1268 | conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1269 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1270 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1271 | // Test case that the connection has received something. |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1272 | // Create a connection again and receive a ping. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1273 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1274 | conn = ch1.conn(); |
nisse | c8ee882 | 2017-01-18 07:20:55 -0800 | [diff] [blame] | 1275 | ASSERT_NE(conn, nullptr); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1276 | int64_t before_last_receiving = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1277 | conn->ReceivedPing(); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1278 | int64_t after_last_receiving = rtc::TimeMillis(); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1279 | // The connection will be dead after DEAD_CONNECTION_RECEIVE_TIMEOUT |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1280 | conn->UpdateState(before_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT - |
| 1281 | 1); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1282 | rtc::Thread::Current()->ProcessMessages(100); |
| 1283 | EXPECT_TRUE(ch1.conn() != nullptr); |
| 1284 | conn->UpdateState(after_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT + 1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1285 | EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout); |
Honghai Zhang | 2cd7afe | 2015-11-12 11:14:33 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1288 | // This test case verifies standard ICE features in STUN messages. Currently it |
| 1289 | // verifies Message Integrity attribute in STUN messages and username in STUN |
| 1290 | // binding request will have colon (":") between remote and local username. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1291 | TEST_F(PortTest, TestLocalToLocalStandard) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1292 | auto port1 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1293 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1294 | port1->SetIceTiebreaker(kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1295 | auto port2 = CreateUdpPort(kLocalAddr2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1296 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1297 | port2->SetIceTiebreaker(kTiebreaker2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1298 | // Same parameters as TestLocalToLocal above. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1299 | TestConnectivity("udp", std::move(port1), "udp", std::move(port2), true, true, |
| 1300 | true, true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | // This test is trying to validate a successful and failure scenario in a |
| 1304 | // loopback test when protocol is RFC5245. For success IceTiebreaker, username |
| 1305 | // should remain equal to the request generated by the port and role of port |
| 1306 | // must be in controlling. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1307 | TEST_F(PortTest, TestLoopbackCall) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1308 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1309 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1310 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1311 | lport->PrepareAddress(); |
| 1312 | ASSERT_FALSE(lport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1313 | Connection* conn = |
| 1314 | lport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1315 | conn->Ping(0); |
| 1316 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1317 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1318 | IceMessage* msg = lport->last_stun_msg(); |
| 1319 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1320 | conn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1321 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1322 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1323 | msg = lport->last_stun_msg(); |
| 1324 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
| 1325 | |
| 1326 | // If the tiebreaker value is different from port, we expect a error |
| 1327 | // response. |
| 1328 | lport->Reset(); |
| 1329 | lport->AddCandidateAddress(kLocalAddr2); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1330 | // Creating a different connection as |conn| is receiving. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1331 | Connection* conn1 = |
| 1332 | lport->CreateConnection(lport->Candidates()[1], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1333 | conn1->Ping(0); |
| 1334 | |
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_REQUEST, msg->type()); |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1338 | std::unique_ptr<IceMessage> modified_req( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1339 | CreateStunMessage(STUN_BINDING_REQUEST)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1340 | const StunByteStringAttribute* username_attr = |
| 1341 | msg->GetByteString(STUN_ATTR_USERNAME); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1342 | modified_req->AddAttribute(std::make_unique<StunByteStringAttribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1343 | STUN_ATTR_USERNAME, username_attr->GetString())); |
| 1344 | // To make sure we receive error response, adding tiebreaker less than |
| 1345 | // what's present in request. |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1346 | modified_req->AddAttribute(std::make_unique<StunUInt64Attribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1347 | STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1)); |
| 1348 | modified_req->AddMessageIntegrity("lpass"); |
| 1349 | modified_req->AddFingerprint(); |
| 1350 | |
| 1351 | lport->Reset(); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1352 | auto buf = std::make_unique<ByteBufferWriter>(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1353 | WriteStunMessage(*modified_req, buf.get()); |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1354 | conn1->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1355 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1356 | msg = lport->last_stun_msg(); |
| 1357 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1358 | } |
| 1359 | |
| 1360 | // This test verifies role conflict signal is received when there is |
| 1361 | // conflict in the role. In this case both ports are in controlling and |
| 1362 | // |rport| has higher tiebreaker value than |lport|. Since |lport| has lower |
| 1363 | // value of tiebreaker, when it receives ping request from |rport| it will |
| 1364 | // send role conflict signal. |
| 1365 | TEST_F(PortTest, TestIceRoleConflict) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1366 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1367 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1368 | lport->SetIceTiebreaker(kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1369 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1370 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1371 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1372 | |
| 1373 | lport->PrepareAddress(); |
| 1374 | rport->PrepareAddress(); |
| 1375 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1376 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1377 | Connection* lconn = |
| 1378 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1379 | Connection* rconn = |
| 1380 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1381 | rconn->Ping(0); |
| 1382 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1383 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1384 | IceMessage* msg = rport->last_stun_msg(); |
| 1385 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1386 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1387 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1388 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1389 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1390 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1391 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
| 1392 | EXPECT_TRUE(role_conflict()); |
| 1393 | } |
| 1394 | |
| 1395 | TEST_F(PortTest, TestTcpNoDelay) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1396 | auto port1 = CreateTcpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1397 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1398 | int option_value = -1; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1399 | int success = port1->GetOption(rtc::Socket::OPT_NODELAY, &option_value); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1400 | ASSERT_EQ(0, success); // GetOption() should complete successfully w/ 0 |
| 1401 | ASSERT_EQ(1, option_value); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | TEST_F(PortTest, TestDelayedBindingUdp) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1405 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1406 | FakePacketSocketFactory socket_factory; |
| 1407 | |
| 1408 | socket_factory.set_next_udp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1409 | auto port = CreateUdpPort(kLocalAddr1, &socket_factory); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1410 | |
| 1411 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1412 | port->PrepareAddress(); |
| 1413 | |
| 1414 | EXPECT_EQ(0U, port->Candidates().size()); |
| 1415 | socket->SignalAddressReady(socket, kLocalAddr2); |
| 1416 | |
| 1417 | EXPECT_EQ(1U, port->Candidates().size()); |
| 1418 | } |
| 1419 | |
| 1420 | TEST_F(PortTest, TestDelayedBindingTcp) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1421 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1422 | FakePacketSocketFactory socket_factory; |
| 1423 | |
| 1424 | socket_factory.set_next_server_tcp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1425 | auto port = CreateTcpPort(kLocalAddr1, &socket_factory); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1426 | |
| 1427 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1428 | port->PrepareAddress(); |
| 1429 | |
| 1430 | EXPECT_EQ(0U, port->Candidates().size()); |
| 1431 | socket->SignalAddressReady(socket, kLocalAddr2); |
| 1432 | |
| 1433 | EXPECT_EQ(1U, port->Candidates().size()); |
| 1434 | } |
| 1435 | |
| 1436 | void PortTest::TestCrossFamilyPorts(int type) { |
| 1437 | FakePacketSocketFactory factory; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1438 | std::unique_ptr<Port> ports[4]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1439 | SocketAddress addresses[4] = { |
| 1440 | SocketAddress("192.168.1.3", 0), SocketAddress("192.168.1.4", 0), |
| 1441 | SocketAddress("2001:db8::1", 0), SocketAddress("2001:db8::2", 0)}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1442 | for (int i = 0; i < 4; i++) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1443 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1444 | if (type == SOCK_DGRAM) { |
| 1445 | factory.set_next_udp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1446 | ports[i] = CreateUdpPort(addresses[i], &factory); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1447 | } else if (type == SOCK_STREAM) { |
| 1448 | factory.set_next_server_tcp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1449 | ports[i] = CreateTcpPort(addresses[i], &factory); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1450 | } |
| 1451 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1452 | socket->SignalAddressReady(socket, addresses[i]); |
| 1453 | ports[i]->PrepareAddress(); |
| 1454 | } |
| 1455 | |
| 1456 | // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate. |
| 1457 | if (type == SOCK_STREAM) { |
| 1458 | FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); |
| 1459 | factory.set_next_client_tcp_socket(clientsocket); |
| 1460 | } |
| 1461 | Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()), |
| 1462 | Port::ORIGIN_MESSAGE); |
| 1463 | EXPECT_TRUE(NULL == c); |
| 1464 | EXPECT_EQ(0U, ports[0]->connections().size()); |
| 1465 | c = ports[0]->CreateConnection(GetCandidate(ports[1].get()), |
| 1466 | Port::ORIGIN_MESSAGE); |
| 1467 | EXPECT_FALSE(NULL == c); |
| 1468 | EXPECT_EQ(1U, ports[0]->connections().size()); |
| 1469 | |
| 1470 | // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate. |
| 1471 | if (type == SOCK_STREAM) { |
| 1472 | FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket(); |
| 1473 | factory.set_next_client_tcp_socket(clientsocket); |
| 1474 | } |
| 1475 | c = ports[2]->CreateConnection(GetCandidate(ports[0].get()), |
| 1476 | Port::ORIGIN_MESSAGE); |
| 1477 | EXPECT_TRUE(NULL == c); |
| 1478 | EXPECT_EQ(0U, ports[2]->connections().size()); |
| 1479 | c = ports[2]->CreateConnection(GetCandidate(ports[3].get()), |
| 1480 | Port::ORIGIN_MESSAGE); |
| 1481 | EXPECT_FALSE(NULL == c); |
| 1482 | EXPECT_EQ(1U, ports[2]->connections().size()); |
| 1483 | } |
| 1484 | |
| 1485 | TEST_F(PortTest, TestSkipCrossFamilyTcp) { |
| 1486 | TestCrossFamilyPorts(SOCK_STREAM); |
| 1487 | } |
| 1488 | |
| 1489 | TEST_F(PortTest, TestSkipCrossFamilyUdp) { |
| 1490 | TestCrossFamilyPorts(SOCK_DGRAM); |
| 1491 | } |
| 1492 | |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1493 | void PortTest::ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1494 | Connection* c = p1->CreateConnection(GetCandidate(p2), Port::ORIGIN_MESSAGE); |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1495 | if (can_connect) { |
| 1496 | EXPECT_FALSE(NULL == c); |
| 1497 | EXPECT_EQ(1U, p1->connections().size()); |
| 1498 | } else { |
| 1499 | EXPECT_TRUE(NULL == c); |
| 1500 | EXPECT_EQ(0U, p1->connections().size()); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | TEST_F(PortTest, TestUdpV6CrossTypePorts) { |
| 1505 | FakePacketSocketFactory factory; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1506 | std::unique_ptr<Port> ports[4]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1507 | SocketAddress addresses[4] = { |
| 1508 | SocketAddress("2001:db8::1", 0), SocketAddress("fe80::1", 0), |
| 1509 | SocketAddress("fe80::2", 0), SocketAddress("::1", 0)}; |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1510 | for (int i = 0; i < 4; i++) { |
Benjamin Wright | d6f86e8 | 2018-05-08 13:12:25 -0700 | [diff] [blame] | 1511 | FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket(); |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1512 | factory.set_next_udp_socket(socket); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1513 | ports[i] = CreateUdpPort(addresses[i], &factory); |
Peter Thatcher | b8b0143 | 2015-07-07 16:45:53 -0700 | [diff] [blame] | 1514 | socket->set_state(AsyncPacketSocket::STATE_BINDING); |
| 1515 | socket->SignalAddressReady(socket, addresses[i]); |
| 1516 | ports[i]->PrepareAddress(); |
| 1517 | } |
| 1518 | |
| 1519 | Port* standard = ports[0].get(); |
| 1520 | Port* link_local1 = ports[1].get(); |
| 1521 | Port* link_local2 = ports[2].get(); |
| 1522 | Port* localhost = ports[3].get(); |
| 1523 | |
| 1524 | ExpectPortsCanConnect(false, link_local1, standard); |
| 1525 | ExpectPortsCanConnect(false, standard, link_local1); |
| 1526 | ExpectPortsCanConnect(false, link_local1, localhost); |
| 1527 | ExpectPortsCanConnect(false, localhost, link_local1); |
| 1528 | |
| 1529 | ExpectPortsCanConnect(true, link_local1, link_local2); |
| 1530 | ExpectPortsCanConnect(true, localhost, standard); |
| 1531 | ExpectPortsCanConnect(true, standard, localhost); |
| 1532 | } |
| 1533 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1534 | // This test verifies DSCP value set through SetOption interface can be |
| 1535 | // get through DefaultDscpValue. |
| 1536 | TEST_F(PortTest, TestDefaultDscpValue) { |
| 1537 | int dscp; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1538 | auto udpport = CreateUdpPort(kLocalAddr1); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1539 | 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] | 1540 | EXPECT_EQ(0, udpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1541 | auto tcpport = CreateTcpPort(kLocalAddr1); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1542 | 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] | 1543 | EXPECT_EQ(0, tcpport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1544 | EXPECT_EQ(rtc::DSCP_AF31, dscp); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1545 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1546 | 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] | 1547 | EXPECT_EQ(0, stunport->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1548 | EXPECT_EQ(rtc::DSCP_AF41, dscp); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1549 | auto turnport1 = |
| 1550 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1551 | // Socket is created in PrepareAddress. |
| 1552 | turnport1->PrepareAddress(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1553 | 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] | 1554 | EXPECT_EQ(0, turnport1->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1555 | EXPECT_EQ(rtc::DSCP_CS7, dscp); |
| 1556 | // This will verify correct value returned without the socket. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1557 | auto turnport2 = |
| 1558 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1559 | 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] | 1560 | EXPECT_EQ(0, turnport2->GetOption(rtc::Socket::OPT_DSCP, &dscp)); |
| 1561 | EXPECT_EQ(rtc::DSCP_CS6, dscp); |
| 1562 | } |
| 1563 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1564 | // Test sending STUN messages. |
| 1565 | TEST_F(PortTest, TestSendStunMessage) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1566 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1567 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1568 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1569 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1570 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1571 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1572 | |
| 1573 | // Send a fake ping from lport to rport. |
| 1574 | lport->PrepareAddress(); |
| 1575 | rport->PrepareAddress(); |
| 1576 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1577 | Connection* lconn = |
| 1578 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1579 | Connection* rconn = |
| 1580 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1581 | lconn->Ping(0); |
| 1582 | |
| 1583 | // Check that it's a proper BINDING-REQUEST. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1584 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1585 | IceMessage* msg = lport->last_stun_msg(); |
| 1586 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1587 | EXPECT_FALSE(msg->IsLegacy()); |
| 1588 | const StunByteStringAttribute* username_attr = |
| 1589 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 1590 | ASSERT_TRUE(username_attr != NULL); |
| 1591 | const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 1592 | ASSERT_TRUE(priority_attr != NULL); |
| 1593 | EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value()); |
| 1594 | EXPECT_EQ("rfrag:lfrag", username_attr->GetString()); |
| 1595 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
| 1596 | EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1597 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1598 | "rpass")); |
| 1599 | const StunUInt64Attribute* ice_controlling_attr = |
| 1600 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1601 | ASSERT_TRUE(ice_controlling_attr != NULL); |
| 1602 | EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value()); |
| 1603 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1604 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 1605 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1606 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1607 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Request should not include ping count. |
| 1610 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1611 | |
| 1612 | // Save a copy of the BINDING-REQUEST for use below. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1613 | std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1614 | |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1615 | // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE. |
| 1616 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1617 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1618 | msg = rport->last_stun_msg(); |
| 1619 | ASSERT_TRUE(msg != NULL); |
| 1620 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1621 | // Received a BINDING-RESPONSE. |
| 1622 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1623 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1624 | // Verify the STUN Stats. |
| 1625 | EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total); |
| 1626 | EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response); |
| 1627 | EXPECT_EQ(1U, lconn->stats().recv_ping_responses); |
| 1628 | EXPECT_EQ(1U, rconn->stats().recv_ping_requests); |
| 1629 | EXPECT_EQ(1U, rconn->stats().sent_ping_responses); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1630 | |
| 1631 | EXPECT_FALSE(msg->IsLegacy()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1632 | const StunAddressAttribute* addr_attr = |
| 1633 | msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1634 | ASSERT_TRUE(addr_attr != NULL); |
| 1635 | EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress()); |
| 1636 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
| 1637 | EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1638 | rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1639 | "rpass")); |
| 1640 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1641 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1642 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1643 | // No USERNAME or PRIORITY in ICE responses. |
| 1644 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1645 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1646 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL); |
| 1647 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL); |
| 1648 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL); |
| 1649 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1650 | |
| 1651 | // Response should not include ping count. |
| 1652 | ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL); |
| 1653 | |
| 1654 | // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life, |
| 1655 | // but we can do it here. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1656 | rport->SendBindingErrorResponse( |
| 1657 | request.get(), lport->Candidates()[0].address(), STUN_ERROR_SERVER_ERROR, |
| 1658 | STUN_ERROR_REASON_SERVER_ERROR); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1659 | msg = rport->last_stun_msg(); |
| 1660 | ASSERT_TRUE(msg != NULL); |
| 1661 | EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type()); |
| 1662 | EXPECT_FALSE(msg->IsLegacy()); |
| 1663 | const StunErrorCodeAttribute* error_attr = msg->GetErrorCode(); |
| 1664 | ASSERT_TRUE(error_attr != NULL); |
| 1665 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code()); |
| 1666 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason()); |
| 1667 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL); |
| 1668 | EXPECT_TRUE(StunMessage::ValidateMessageIntegrity( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1669 | rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1670 | "rpass")); |
| 1671 | EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL); |
| 1672 | EXPECT_TRUE(StunMessage::ValidateFingerprint( |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 1673 | lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1674 | // No USERNAME with ICE. |
| 1675 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL); |
| 1676 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL); |
| 1677 | |
| 1678 | // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED |
| 1679 | // and (incremented) RETRANSMIT_COUNT attributes. |
| 1680 | rport->Reset(); |
| 1681 | rport->set_send_retransmit_count_attribute(true); |
| 1682 | rconn->Ping(0); |
| 1683 | rconn->Ping(0); |
| 1684 | rconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1685 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1686 | msg = rport->last_stun_msg(); |
| 1687 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1688 | const StunUInt64Attribute* ice_controlled_attr = |
| 1689 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); |
| 1690 | ASSERT_TRUE(ice_controlled_attr != NULL); |
| 1691 | EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value()); |
| 1692 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 1693 | |
| 1694 | // Request should include ping count. |
| 1695 | const StunUInt32Attribute* retransmit_attr = |
| 1696 | msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1697 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1698 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1699 | |
| 1700 | // Respond with a BINDING-RESPONSE. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1701 | request = CopyStunMessage(*msg); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1702 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1703 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1704 | msg = lport->last_stun_msg(); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1705 | // Receive the BINDING-RESPONSE. |
| 1706 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1707 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1708 | |
| 1709 | // Verify the Stun ping stats. |
| 1710 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total); |
| 1711 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); |
| 1712 | EXPECT_EQ(1U, rconn->stats().recv_ping_responses); |
| 1713 | EXPECT_EQ(1U, lconn->stats().sent_ping_responses); |
| 1714 | EXPECT_EQ(1U, lconn->stats().recv_ping_requests); |
| 1715 | // Ping after receiver the first response |
| 1716 | rconn->Ping(0); |
| 1717 | rconn->Ping(0); |
| 1718 | EXPECT_EQ(5U, rconn->stats().sent_ping_requests_total); |
| 1719 | EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Response should include same ping count. |
| 1722 | retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 1723 | ASSERT_TRUE(retransmit_attr != NULL); |
| 1724 | EXPECT_EQ(2U, retransmit_attr->value()); |
| 1725 | } |
| 1726 | |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1727 | TEST_F(PortTest, TestNomination) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1728 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1729 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1730 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1731 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1732 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1733 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1734 | |
| 1735 | lport->PrepareAddress(); |
| 1736 | rport->PrepareAddress(); |
| 1737 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1738 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1739 | Connection* lconn = |
| 1740 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1741 | Connection* rconn = |
| 1742 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1743 | |
| 1744 | // |lconn| is controlling, |rconn| is controlled. |
| 1745 | uint32_t nomination = 1234; |
| 1746 | lconn->set_nomination(nomination); |
| 1747 | |
| 1748 | EXPECT_FALSE(lconn->nominated()); |
| 1749 | EXPECT_FALSE(rconn->nominated()); |
| 1750 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 1751 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 1752 | |
| 1753 | // Send ping (including the nomination value) from |lconn| to |rconn|. This |
| 1754 | // should set the remote nomination of |rconn|. |
| 1755 | lconn->Ping(0); |
| 1756 | ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout); |
| 1757 | ASSERT_TRUE(lport->last_stun_buf()); |
| 1758 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1759 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1760 | EXPECT_EQ(nomination, rconn->remote_nomination()); |
| 1761 | EXPECT_FALSE(lconn->nominated()); |
| 1762 | EXPECT_TRUE(rconn->nominated()); |
| 1763 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 1764 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 1765 | |
| 1766 | // This should result in an acknowledgment sent back from |rconn| to |lconn|, |
| 1767 | // updating the acknowledged nomination of |lconn|. |
| 1768 | ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout); |
| 1769 | ASSERT_TRUE(rport->last_stun_buf()); |
| 1770 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1771 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1772 | EXPECT_EQ(nomination, lconn->acked_nomination()); |
| 1773 | EXPECT_TRUE(lconn->nominated()); |
| 1774 | EXPECT_TRUE(rconn->nominated()); |
| 1775 | EXPECT_EQ(lconn->nominated(), lconn->stats().nominated); |
| 1776 | EXPECT_EQ(rconn->nominated(), rconn->stats().nominated); |
| 1777 | } |
| 1778 | |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1779 | TEST_F(PortTest, TestRoundTripTime) { |
| 1780 | rtc::ScopedFakeClock clock; |
| 1781 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1782 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1783 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1784 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1785 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1786 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1787 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1788 | |
| 1789 | lport->PrepareAddress(); |
| 1790 | rport->PrepareAddress(); |
| 1791 | ASSERT_FALSE(lport->Candidates().empty()); |
| 1792 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1793 | Connection* lconn = |
| 1794 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1795 | Connection* rconn = |
| 1796 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1797 | |
| 1798 | EXPECT_EQ(0u, lconn->stats().total_round_trip_time_ms); |
| 1799 | EXPECT_FALSE(lconn->stats().current_round_trip_time_ms); |
| 1800 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1801 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 1802 | 10); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1803 | EXPECT_EQ(10u, lconn->stats().total_round_trip_time_ms); |
| 1804 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 1805 | EXPECT_EQ(10u, *lconn->stats().current_round_trip_time_ms); |
| 1806 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1807 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 1808 | 20); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1809 | EXPECT_EQ(30u, lconn->stats().total_round_trip_time_ms); |
| 1810 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 1811 | EXPECT_EQ(20u, *lconn->stats().current_round_trip_time_ms); |
| 1812 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1813 | SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock, |
| 1814 | 30); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1815 | EXPECT_EQ(60u, lconn->stats().total_round_trip_time_ms); |
| 1816 | ASSERT_TRUE(lconn->stats().current_round_trip_time_ms); |
| 1817 | EXPECT_EQ(30u, *lconn->stats().current_round_trip_time_ms); |
| 1818 | } |
| 1819 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1820 | TEST_F(PortTest, TestUseCandidateAttribute) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1821 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
| 1822 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1823 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1824 | lport->SetIceTiebreaker(kTiebreaker1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1825 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1826 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1827 | |
| 1828 | // Send a fake ping from lport to rport. |
| 1829 | lport->PrepareAddress(); |
| 1830 | rport->PrepareAddress(); |
| 1831 | ASSERT_FALSE(rport->Candidates().empty()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1832 | Connection* lconn = |
| 1833 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1834 | lconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1835 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1836 | IceMessage* msg = lport->last_stun_msg(); |
| 1837 | const StunUInt64Attribute* ice_controlling_attr = |
| 1838 | msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 1839 | ASSERT_TRUE(ice_controlling_attr != NULL); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1840 | const StunByteStringAttribute* use_candidate_attr = |
| 1841 | msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1842 | ASSERT_TRUE(use_candidate_attr != NULL); |
| 1843 | } |
| 1844 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1845 | // Tests that when the network type changes, the network cost of the port will |
| 1846 | // change, the network cost of the local candidates will change. Also tests that |
| 1847 | // the remote network costs are updated with the stun binding requests. |
| 1848 | TEST_F(PortTest, TestNetworkCostChange) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1849 | rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1850 | auto lport = CreateTestPort(test_network, "lfrag", "lpass"); |
| 1851 | auto rport = CreateTestPort(test_network, "rfrag", "rpass"); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1852 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1853 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1854 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1855 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1856 | lport->PrepareAddress(); |
| 1857 | rport->PrepareAddress(); |
| 1858 | |
| 1859 | // Default local port cost is rtc::kNetworkCostUnknown. |
| 1860 | EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost()); |
| 1861 | ASSERT_TRUE(!lport->Candidates().empty()); |
| 1862 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1863 | EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost()); |
| 1864 | } |
| 1865 | |
| 1866 | // Change the network type to wifi. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1867 | test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1868 | EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost()); |
| 1869 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1870 | EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost()); |
| 1871 | } |
| 1872 | |
| 1873 | // Add a connection and then change the network type. |
| 1874 | Connection* lconn = |
| 1875 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1876 | // Change the network type to cellular. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1877 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1878 | EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost()); |
| 1879 | for (const cricket::Candidate& candidate : lport->Candidates()) { |
| 1880 | EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost()); |
| 1881 | } |
| 1882 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1883 | test_network->set_type(rtc::ADAPTER_TYPE_WIFI); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1884 | Connection* rconn = |
| 1885 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1886 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1887 | lconn->Ping(0); |
| 1888 | // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping |
| 1889 | // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the |
| 1890 | // message is handled in rconn, The rconn's remote candidate will have cost |
| 1891 | // rtc::kNetworkCostHigh; |
| 1892 | EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1893 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1894 | IceMessage* msg = lport->last_stun_msg(); |
| 1895 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 1896 | // Pass the binding request to rport. |
| 1897 | rconn->OnReadPacket(lport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 1898 | lport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1899 | // 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] | 1900 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1901 | EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost()); |
| 1902 | } |
| 1903 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1904 | TEST_F(PortTest, TestNetworkInfoAttribute) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1905 | rtc::Network* test_network = MakeNetwork(kLocalAddr1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1906 | auto lport = CreateTestPort(test_network, "lfrag", "lpass"); |
| 1907 | auto rport = CreateTestPort(test_network, "rfrag", "rpass"); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1908 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1909 | lport->SetIceTiebreaker(kTiebreaker1); |
| 1910 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1911 | rport->SetIceTiebreaker(kTiebreaker2); |
| 1912 | |
| 1913 | uint16_t lnetwork_id = 9; |
| 1914 | lport->Network()->set_id(lnetwork_id); |
| 1915 | // Send a fake ping from lport to rport. |
| 1916 | lport->PrepareAddress(); |
| 1917 | rport->PrepareAddress(); |
| 1918 | Connection* lconn = |
| 1919 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1920 | lconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1921 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1922 | IceMessage* msg = lport->last_stun_msg(); |
| 1923 | const StunUInt32Attribute* network_info_attr = |
| 1924 | msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 1925 | ASSERT_TRUE(network_info_attr != NULL); |
| 1926 | uint32_t network_info = network_info_attr->value(); |
| 1927 | EXPECT_EQ(lnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1928 | // Default network has unknown type and cost kNetworkCostUnknown. |
| 1929 | EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1930 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1931 | // Set the network type to be cellular so its cost will be kNetworkCostHigh. |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1932 | // Send a fake ping from rport to lport. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 1933 | test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1934 | uint16_t rnetwork_id = 8; |
| 1935 | rport->Network()->set_id(rnetwork_id); |
| 1936 | Connection* rconn = |
| 1937 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 1938 | rconn->Ping(0); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 1939 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1940 | msg = rport->last_stun_msg(); |
| 1941 | network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 1942 | ASSERT_TRUE(network_info_attr != NULL); |
| 1943 | network_info = network_info_attr->value(); |
| 1944 | EXPECT_EQ(rnetwork_id, network_info >> 16); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1945 | EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1948 | // Test handling STUN messages. |
| 1949 | TEST_F(PortTest, TestHandleStunMessage) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1950 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1951 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1952 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1953 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1954 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1955 | rtc::SocketAddress addr(kLocalAddr1); |
| 1956 | std::string username; |
| 1957 | |
| 1958 | // BINDING-REQUEST from local to remote with valid ICE username, |
| 1959 | // MESSAGE-INTEGRITY, and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1960 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1961 | in_msg->AddMessageIntegrity("rpass"); |
| 1962 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1963 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1964 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1965 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1966 | EXPECT_TRUE(out_msg.get() != NULL); |
| 1967 | EXPECT_EQ("lfrag", username); |
| 1968 | |
| 1969 | // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1970 | in_msg = CreateStunMessage(STUN_BINDING_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1971 | in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 1972 | STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1973 | in_msg->AddMessageIntegrity("rpass"); |
| 1974 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1975 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1976 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1977 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1978 | EXPECT_TRUE(out_msg.get() != NULL); |
| 1979 | EXPECT_EQ("", username); |
| 1980 | |
| 1981 | // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1982 | in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1983 | in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 1984 | STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 1985 | STUN_ERROR_REASON_SERVER_ERROR)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1986 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 1987 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1988 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 1989 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1990 | EXPECT_TRUE(out_msg.get() != NULL); |
| 1991 | EXPECT_EQ("", username); |
| 1992 | ASSERT_TRUE(out_msg->GetErrorCode() != NULL); |
| 1993 | EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code()); |
| 1994 | EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1995 | out_msg->GetErrorCode()->reason()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
guoweis | d12140a | 2015-09-10 13:32:11 -0700 | [diff] [blame] | 1998 | // Tests handling of ICE binding requests with missing or incorrect usernames. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1999 | TEST_F(PortTest, TestHandleStunMessageBadUsername) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2000 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2001 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2002 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2003 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2004 | rtc::SocketAddress addr(kLocalAddr1); |
| 2005 | std::string username; |
| 2006 | |
| 2007 | // BINDING-REQUEST with no username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2008 | in_msg = CreateStunMessage(STUN_BINDING_REQUEST); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2009 | in_msg->AddMessageIntegrity("rpass"); |
| 2010 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2011 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2012 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2013 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2014 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2015 | EXPECT_EQ("", username); |
| 2016 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2017 | |
| 2018 | // BINDING-REQUEST with empty username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2019 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, ""); |
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("", username); |
| 2027 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2028 | |
| 2029 | // BINDING-REQUEST with too-short username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2030 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2031 | in_msg->AddMessageIntegrity("rpass"); |
| 2032 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2033 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2034 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2035 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2036 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2037 | EXPECT_EQ("", username); |
| 2038 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2039 | |
| 2040 | // BINDING-REQUEST with reversed username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2041 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "lfrag:rfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2042 | in_msg->AddMessageIntegrity("rpass"); |
| 2043 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2044 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2045 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2046 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2047 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2048 | EXPECT_EQ("", username); |
| 2049 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2050 | |
| 2051 | // BINDING-REQUEST with garbage username. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2052 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "abcd:efgh"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2053 | in_msg->AddMessageIntegrity("rpass"); |
| 2054 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2055 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2056 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2057 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2058 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2059 | EXPECT_EQ("", username); |
| 2060 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2061 | } |
| 2062 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2063 | // Test handling STUN messages with missing or malformed M-I. |
| 2064 | TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2065 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2066 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2067 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2068 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2069 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2070 | rtc::SocketAddress addr(kLocalAddr1); |
| 2071 | std::string username; |
| 2072 | |
| 2073 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2074 | // FINGERPRINT, but no MESSAGE-INTEGRITY. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2075 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2076 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2077 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2078 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2079 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2080 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2081 | EXPECT_EQ("", username); |
| 2082 | EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code()); |
| 2083 | |
| 2084 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2085 | // FINGERPRINT, but invalid MESSAGE-INTEGRITY. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2086 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2087 | in_msg->AddMessageIntegrity("invalid"); |
| 2088 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2089 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2090 | EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2091 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2092 | EXPECT_TRUE(out_msg.get() == NULL); |
| 2093 | EXPECT_EQ("", username); |
| 2094 | EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code()); |
| 2095 | |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 2096 | // TODO(?): BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2097 | // by the Connection, not the Port, since they require the remote username. |
| 2098 | // Change this test to pass in data via Connection::OnReadPacket instead. |
| 2099 | } |
| 2100 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2101 | // Test handling STUN messages with missing or malformed FINGERPRINT. |
| 2102 | TEST_F(PortTest, TestHandleStunMessageBadFingerprint) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2103 | // Our port will act as the "remote" port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2104 | auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2105 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2106 | std::unique_ptr<IceMessage> in_msg, out_msg; |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2107 | auto buf = std::make_unique<ByteBufferWriter>(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2108 | rtc::SocketAddress addr(kLocalAddr1); |
| 2109 | std::string username; |
| 2110 | |
| 2111 | // BINDING-REQUEST from local to remote with valid ICE username and |
| 2112 | // MESSAGE-INTEGRITY, but no FINGERPRINT; GetStunMessage should fail. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2113 | in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2114 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2115 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2116 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2117 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2118 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2119 | |
| 2120 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2121 | in_msg->AddFingerprint(); |
| 2122 | in_msg->SetTransactionID("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2123 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2124 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2125 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2126 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2127 | |
| 2128 | // Valid BINDING-RESPONSE, except no FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2129 | in_msg = CreateStunMessage(STUN_BINDING_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2130 | in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2131 | STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2132 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2133 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2134 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2135 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2136 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2137 | |
| 2138 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2139 | in_msg->AddFingerprint(); |
| 2140 | in_msg->SetTransactionID("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2141 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2142 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2143 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2144 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2145 | |
| 2146 | // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2147 | in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 2148 | in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 2149 | STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR, |
| 2150 | STUN_ERROR_REASON_SERVER_ERROR)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2151 | in_msg->AddMessageIntegrity("rpass"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2152 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2153 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2154 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2155 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2156 | |
| 2157 | // Now, add a fingerprint, but munge the message so it's not valid. |
| 2158 | in_msg->AddFingerprint(); |
| 2159 | in_msg->SetTransactionID("TESTTESTBADD"); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2160 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2161 | EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2162 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2163 | EXPECT_EQ(0, port->last_stun_error_code()); |
| 2164 | } |
| 2165 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2166 | // Test handling of STUN binding indication messages . STUN binding |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2167 | // indications are allowed only to the connection which is in read mode. |
| 2168 | TEST_F(PortTest, TestHandleStunBindingIndication) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2169 | auto lport = CreateTestPort(kLocalAddr2, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2170 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2171 | lport->SetIceTiebreaker(kTiebreaker1); |
| 2172 | |
| 2173 | // Verifying encoding and decoding STUN indication message. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2174 | std::unique_ptr<IceMessage> in_msg, out_msg; |
| 2175 | std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2176 | rtc::SocketAddress addr(kLocalAddr1); |
| 2177 | std::string username; |
| 2178 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2179 | in_msg = CreateStunMessage(STUN_BINDING_INDICATION); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2180 | in_msg->AddFingerprint(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2181 | WriteStunMessage(*in_msg, buf.get()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 2182 | EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg, |
| 2183 | &username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2184 | EXPECT_TRUE(out_msg.get() != NULL); |
| 2185 | EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION); |
| 2186 | EXPECT_EQ("", username); |
| 2187 | |
| 2188 | // Verify connection can handle STUN indication and updates |
| 2189 | // last_ping_received. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2190 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2191 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2192 | rport->SetIceTiebreaker(kTiebreaker2); |
| 2193 | |
| 2194 | lport->PrepareAddress(); |
| 2195 | rport->PrepareAddress(); |
| 2196 | ASSERT_FALSE(lport->Candidates().empty()); |
| 2197 | ASSERT_FALSE(rport->Candidates().empty()); |
| 2198 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2199 | Connection* lconn = |
| 2200 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
| 2201 | Connection* rconn = |
| 2202 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2203 | rconn->Ping(0); |
| 2204 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2205 | ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2206 | IceMessage* msg = rport->last_stun_msg(); |
| 2207 | EXPECT_EQ(STUN_BINDING_REQUEST, msg->type()); |
| 2208 | // Send rport binding request to lport. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2209 | lconn->OnReadPacket(rport->last_stun_buf()->data<char>(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2210 | rport->last_stun_buf()->size(), /* packet_time_us */ -1); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2211 | ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2212 | EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type()); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2213 | int64_t last_ping_received1 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Adding a delay of 100ms. |
| 2216 | rtc::Thread::Current()->ProcessMessages(100); |
| 2217 | // Pinging lconn using stun indication message. |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2218 | lconn->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2219 | int64_t last_ping_received2 = lconn->last_ping_received(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2220 | EXPECT_GT(last_ping_received2, last_ping_received1); |
| 2221 | } |
| 2222 | |
| 2223 | TEST_F(PortTest, TestComputeCandidatePriority) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2224 | auto port = CreateTestPort(kLocalAddr1, "name", "pass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2225 | port->set_type_preference(90); |
| 2226 | port->set_component(177); |
| 2227 | port->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2228 | port->AddCandidateAddress(SocketAddress("2001:db8::1234", 1234)); |
| 2229 | port->AddCandidateAddress(SocketAddress("fc12:3456::1234", 1234)); |
| 2230 | port->AddCandidateAddress(SocketAddress("::ffff:192.168.1.4", 1234)); |
| 2231 | port->AddCandidateAddress(SocketAddress("::192.168.1.4", 1234)); |
| 2232 | port->AddCandidateAddress(SocketAddress("2002::1234:5678", 1234)); |
| 2233 | port->AddCandidateAddress(SocketAddress("2001::1234:5678", 1234)); |
| 2234 | port->AddCandidateAddress(SocketAddress("fecf::1234:5678", 1234)); |
| 2235 | port->AddCandidateAddress(SocketAddress("3ffe::1234:5678", 1234)); |
| 2236 | // These should all be: |
| 2237 | // (90 << 24) | ([rfc3484 pref value] << 8) | (256 - 177) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2238 | uint32_t expected_priority_v4 = 1509957199U; |
| 2239 | uint32_t expected_priority_v6 = 1509959759U; |
| 2240 | uint32_t expected_priority_ula = 1509962319U; |
| 2241 | uint32_t expected_priority_v4mapped = expected_priority_v4; |
| 2242 | uint32_t expected_priority_v4compat = 1509949775U; |
| 2243 | uint32_t expected_priority_6to4 = 1509954639U; |
| 2244 | uint32_t expected_priority_teredo = 1509952079U; |
| 2245 | uint32_t expected_priority_sitelocal = 1509949775U; |
| 2246 | uint32_t expected_priority_6bone = 1509949775U; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2247 | ASSERT_EQ(expected_priority_v4, port->Candidates()[0].priority()); |
| 2248 | ASSERT_EQ(expected_priority_v6, port->Candidates()[1].priority()); |
| 2249 | ASSERT_EQ(expected_priority_ula, port->Candidates()[2].priority()); |
| 2250 | ASSERT_EQ(expected_priority_v4mapped, port->Candidates()[3].priority()); |
| 2251 | ASSERT_EQ(expected_priority_v4compat, port->Candidates()[4].priority()); |
| 2252 | ASSERT_EQ(expected_priority_6to4, port->Candidates()[5].priority()); |
| 2253 | ASSERT_EQ(expected_priority_teredo, port->Candidates()[6].priority()); |
| 2254 | ASSERT_EQ(expected_priority_sitelocal, port->Candidates()[7].priority()); |
| 2255 | ASSERT_EQ(expected_priority_6bone, port->Candidates()[8].priority()); |
| 2256 | } |
| 2257 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2258 | // In the case of shared socket, one port may be shared by local and stun. |
| 2259 | // Test that candidates with different types will have different foundation. |
| 2260 | TEST_F(PortTest, TestFoundation) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2261 | auto testport = CreateTestPort(kLocalAddr1, "name", "pass"); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2262 | testport->AddCandidateAddress(kLocalAddr1, kLocalAddr1, LOCAL_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2263 | cricket::ICE_TYPE_PREFERENCE_HOST, false); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2264 | testport->AddCandidateAddress(kLocalAddr2, kLocalAddr1, STUN_PORT_TYPE, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2265 | cricket::ICE_TYPE_PREFERENCE_SRFLX, true); |
| 2266 | EXPECT_NE(testport->Candidates()[0].foundation(), |
| 2267 | testport->Candidates()[1].foundation()); |
| 2268 | } |
| 2269 | |
| 2270 | // This test verifies the foundation of different types of ICE candidates. |
| 2271 | TEST_F(PortTest, TestCandidateFoundation) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 2272 | std::unique_ptr<rtc::NATServer> nat_server( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2273 | CreateNatServer(kNatAddr1, NAT_OPEN_CONE)); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2274 | auto udpport1 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2275 | udpport1->PrepareAddress(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2276 | auto udpport2 = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2277 | udpport2->PrepareAddress(); |
| 2278 | EXPECT_EQ(udpport1->Candidates()[0].foundation(), |
| 2279 | udpport2->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2280 | auto tcpport1 = CreateTcpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2281 | tcpport1->PrepareAddress(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2282 | auto tcpport2 = CreateTcpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2283 | tcpport2->PrepareAddress(); |
| 2284 | EXPECT_EQ(tcpport1->Candidates()[0].foundation(), |
| 2285 | tcpport2->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2286 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2287 | stunport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2288 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2289 | EXPECT_NE(tcpport1->Candidates()[0].foundation(), |
| 2290 | stunport->Candidates()[0].foundation()); |
| 2291 | EXPECT_NE(tcpport2->Candidates()[0].foundation(), |
| 2292 | stunport->Candidates()[0].foundation()); |
| 2293 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2294 | stunport->Candidates()[0].foundation()); |
| 2295 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2296 | stunport->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2297 | // Verifying TURN candidate foundation. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2298 | auto turnport1 = |
| 2299 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2300 | turnport1->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2301 | ASSERT_EQ_WAIT(1U, turnport1->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2302 | EXPECT_NE(udpport1->Candidates()[0].foundation(), |
| 2303 | turnport1->Candidates()[0].foundation()); |
| 2304 | EXPECT_NE(udpport2->Candidates()[0].foundation(), |
| 2305 | turnport1->Candidates()[0].foundation()); |
| 2306 | EXPECT_NE(stunport->Candidates()[0].foundation(), |
| 2307 | turnport1->Candidates()[0].foundation()); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2308 | auto turnport2 = |
| 2309 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2310 | turnport2->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2311 | ASSERT_EQ_WAIT(1U, turnport2->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2312 | EXPECT_EQ(turnport1->Candidates()[0].foundation(), |
| 2313 | turnport2->Candidates()[0].foundation()); |
| 2314 | |
| 2315 | // Running a second turn server, to get different base IP address. |
| 2316 | SocketAddress kTurnUdpIntAddr2("99.99.98.4", STUN_SERVER_PORT); |
| 2317 | SocketAddress kTurnUdpExtAddr2("99.99.98.5", 0); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2318 | TestTurnServer turn_server2(rtc::Thread::Current(), kTurnUdpIntAddr2, |
| 2319 | kTurnUdpExtAddr2); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2320 | auto turnport3 = CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, |
| 2321 | PROTO_UDP, kTurnUdpIntAddr2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2322 | turnport3->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2323 | ASSERT_EQ_WAIT(1U, turnport3->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2324 | EXPECT_NE(turnport3->Candidates()[0].foundation(), |
| 2325 | turnport2->Candidates()[0].foundation()); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2326 | |
| 2327 | // Start a TCP turn server, and check that two turn candidates have |
| 2328 | // different foundations if their relay protocols are different. |
| 2329 | TestTurnServer turn_server3(rtc::Thread::Current(), kTurnTcpIntAddr, |
| 2330 | kTurnUdpExtAddr, PROTO_TCP); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2331 | auto turnport4 = |
| 2332 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_TCP, PROTO_UDP); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2333 | turnport4->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2334 | ASSERT_EQ_WAIT(1U, turnport4->Candidates().size(), kDefaultTimeout); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 2335 | EXPECT_NE(turnport2->Candidates()[0].foundation(), |
| 2336 | turnport4->Candidates()[0].foundation()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | // This test verifies the related addresses of different types of |
| 2340 | // ICE candiates. |
| 2341 | TEST_F(PortTest, TestCandidateRelatedAddress) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2342 | auto nat_server = CreateNatServer(kNatAddr1, NAT_OPEN_CONE); |
| 2343 | auto udpport = CreateUdpPort(kLocalAddr1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2344 | udpport->PrepareAddress(); |
| 2345 | // For UDPPort, related address will be empty. |
| 2346 | EXPECT_TRUE(udpport->Candidates()[0].related_address().IsNil()); |
| 2347 | // Testing related address for stun candidates. |
| 2348 | // For stun candidate related address must be equal to the base |
| 2349 | // socket address. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2350 | auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2351 | stunport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2352 | ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2353 | // Check STUN candidate address. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2354 | EXPECT_EQ(stunport->Candidates()[0].address().ipaddr(), kNatAddr1.ipaddr()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2355 | // Check STUN candidate related address. |
| 2356 | EXPECT_EQ(stunport->Candidates()[0].related_address(), |
| 2357 | stunport->GetLocalAddress()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2358 | // Verifying the related address for TURN candidate. |
| 2359 | // For TURN related address must be equal to the mapped address. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2360 | auto turnport = |
| 2361 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2362 | turnport->PrepareAddress(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2363 | ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2364 | EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), |
| 2365 | turnport->Candidates()[0].address().ipaddr()); |
| 2366 | EXPECT_EQ(kNatAddr1.ipaddr(), |
| 2367 | turnport->Candidates()[0].related_address().ipaddr()); |
| 2368 | } |
| 2369 | |
| 2370 | // Test priority value overflow handling when preference is set to 3. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2371 | TEST_F(PortTest, TestCandidatePriority) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2372 | cricket::Candidate cand1; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2373 | cand1.set_priority(3); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2374 | cricket::Candidate cand2; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2375 | cand2.set_priority(1); |
| 2376 | EXPECT_TRUE(cand1.priority() > cand2.priority()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | // Test the Connection priority is calculated correctly. |
| 2380 | TEST_F(PortTest, TestConnectionPriority) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2381 | auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2382 | lport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_HOST); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2383 | auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass"); |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 2384 | rport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_RELAY_UDP); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2385 | lport->set_component(123); |
| 2386 | lport->AddCandidateAddress(SocketAddress("192.168.1.4", 1234)); |
| 2387 | rport->set_component(23); |
| 2388 | rport->AddCandidateAddress(SocketAddress("10.1.1.100", 1234)); |
| 2389 | |
| 2390 | EXPECT_EQ(0x7E001E85U, lport->Candidates()[0].priority()); |
| 2391 | EXPECT_EQ(0x2001EE9U, rport->Candidates()[0].priority()); |
| 2392 | |
| 2393 | // RFC 5245 |
| 2394 | // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0) |
| 2395 | lport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2396 | rport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2397 | Connection* lconn = |
| 2398 | lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2399 | #if defined(WEBRTC_WIN) |
| 2400 | EXPECT_EQ(0x2001EE9FC003D0BU, lconn->priority()); |
| 2401 | #else |
| 2402 | EXPECT_EQ(0x2001EE9FC003D0BLLU, lconn->priority()); |
| 2403 | #endif |
| 2404 | |
| 2405 | lport->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2406 | rport->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2407 | Connection* rconn = |
| 2408 | rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2409 | #if defined(WEBRTC_WIN) |
| 2410 | EXPECT_EQ(0x2001EE9FC003D0AU, rconn->priority()); |
| 2411 | #else |
| 2412 | EXPECT_EQ(0x2001EE9FC003D0ALLU, rconn->priority()); |
| 2413 | #endif |
| 2414 | } |
| 2415 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2416 | // Note that UpdateState takes into account the estimated RTT, and the |
| 2417 | // correctness of using |kMaxExpectedSimulatedRtt| as an upper bound of RTT in |
| 2418 | // the following tests depends on the link rate and the delay distriubtion |
| 2419 | // configured in VirtualSocketServer::AddPacketToNetwork. The tests below use |
| 2420 | // the default setup where the RTT is deterministically one, which generates an |
| 2421 | // estimate given by |MINIMUM_RTT| = 100. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2422 | TEST_F(PortTest, TestWritableState) { |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2423 | rtc::ScopedFakeClock clock; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2424 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2425 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2426 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2427 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2428 | |
| 2429 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2430 | TestChannel ch1(std::move(port1)); |
| 2431 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2432 | |
| 2433 | // Acquire addresses. |
| 2434 | ch1.Start(); |
| 2435 | ch2.Start(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2436 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 2437 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Send a ping from src to dst. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2440 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2441 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2442 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2443 | // for TCP connect |
| 2444 | EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2445 | ch1.Ping(); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2446 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2447 | |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2448 | // Data should be sendable before the connection is accepted. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2449 | char data[] = "abcd"; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 2450 | int data_size = arraysize(data); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2451 | rtc::PacketOptions options; |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2452 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Accept the connection to return the binding response, transition to |
| 2455 | // writable, and allow data to be sent. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2456 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2457 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2458 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2459 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2460 | |
| 2461 | // Ask the connection to update state as if enough time has passed to lose |
| 2462 | // full writability and 5 pings went unresponded to. We'll accomplish the |
| 2463 | // latter by sending pings but not pumping messages. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2464 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2465 | ch1.Ping(i); |
| 2466 | } |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2467 | int unreliable_timeout_delay = |
| 2468 | CONNECTION_WRITE_CONNECT_TIMEOUT + kMaxExpectedSimulatedRtt; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2469 | ch1.conn()->UpdateState(unreliable_timeout_delay); |
| 2470 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2471 | |
| 2472 | // Data should be able to be sent in this state. |
| 2473 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
| 2474 | |
| 2475 | // And now allow the other side to process the pings and send binding |
| 2476 | // responses. |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2477 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2478 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2479 | // Wait long enough for a full timeout (past however long we've already |
| 2480 | // waited). |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2481 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2482 | ch1.Ping(unreliable_timeout_delay + i); |
| 2483 | } |
| 2484 | ch1.conn()->UpdateState(unreliable_timeout_delay + CONNECTION_WRITE_TIMEOUT + |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2485 | kMaxExpectedSimulatedRtt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2486 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2487 | |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 2488 | // Even if the connection has timed out, the Connection shouldn't block |
| 2489 | // the sending of data. |
| 2490 | EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2491 | |
| 2492 | ch1.Stop(); |
| 2493 | ch2.Stop(); |
| 2494 | } |
| 2495 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2496 | // Test writability states using the configured threshold value to replace |
| 2497 | // the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT| and |
| 2498 | // |CONNECTION_WRITE_CONNECT_FAILURES|. |
| 2499 | TEST_F(PortTest, TestWritableStateWithConfiguredThreshold) { |
| 2500 | rtc::ScopedFakeClock clock; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2501 | auto port1 = CreateUdpPort(kLocalAddr1); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2502 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2503 | auto port2 = CreateUdpPort(kLocalAddr2); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2504 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2505 | |
| 2506 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2507 | TestChannel ch1(std::move(port1)); |
| 2508 | TestChannel ch2(std::move(port2)); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2509 | |
| 2510 | // Acquire addresses. |
| 2511 | ch1.Start(); |
| 2512 | ch2.Start(); |
| 2513 | ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock); |
| 2514 | ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock); |
| 2515 | |
| 2516 | // Send a ping from src to dst. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2517 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2518 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2519 | ch1.Ping(); |
| 2520 | SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock); |
| 2521 | |
| 2522 | // Accept the connection to return the binding response, transition to |
| 2523 | // writable, and allow data to be sent. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2524 | ch2.AcceptConnection(GetCandidate(ch1.port())); |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2525 | EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE, |
| 2526 | ch1.conn()->write_state(), kDefaultTimeout, clock); |
| 2527 | |
| 2528 | ch1.conn()->set_unwritable_timeout(1000); |
| 2529 | ch1.conn()->set_unwritable_min_checks(3); |
| 2530 | // Send two checks. |
| 2531 | ch1.Ping(1); |
| 2532 | ch1.Ping(2); |
| 2533 | // We have not reached the timeout nor have we sent the minimum number of |
| 2534 | // checks to change the state to Unreliable. |
| 2535 | ch1.conn()->UpdateState(999); |
| 2536 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2537 | // We have not sent the minimum number of checks without responses. |
| 2538 | ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt); |
| 2539 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2540 | // Last ping after which the candidate pair should become Unreliable after |
| 2541 | // timeout. |
| 2542 | ch1.Ping(3); |
| 2543 | // We have not reached the timeout. |
| 2544 | ch1.conn()->UpdateState(999); |
| 2545 | EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state()); |
| 2546 | // We should be in the state Unreliable now. |
| 2547 | ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt); |
| 2548 | EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state()); |
| 2549 | |
| 2550 | ch1.Stop(); |
| 2551 | ch2.Stop(); |
| 2552 | } |
| 2553 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2554 | TEST_F(PortTest, TestTimeoutForNeverWritable) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2555 | auto port1 = CreateUdpPort(kLocalAddr1); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2556 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2557 | auto port2 = CreateUdpPort(kLocalAddr2); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 2558 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2559 | |
| 2560 | // Set up channels. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2561 | TestChannel ch1(std::move(port1)); |
| 2562 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Acquire addresses. |
| 2565 | ch1.Start(); |
| 2566 | ch2.Start(); |
| 2567 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2568 | ch1.CreateConnection(GetCandidate(ch2.port())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2569 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2570 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2571 | |
| 2572 | // Attempt to go directly to write timeout. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2573 | for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2574 | ch1.Ping(i); |
| 2575 | } |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 2576 | ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + kMaxExpectedSimulatedRtt); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2577 | EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state()); |
| 2578 | } |
| 2579 | |
| 2580 | // This test verifies the connection setup between ICEMODE_FULL |
| 2581 | // and ICEMODE_LITE. |
| 2582 | // In this test |ch1| behaves like FULL mode client and we have created |
| 2583 | // port which responds to the ping message just like LITE client. |
| 2584 | TEST_F(PortTest, TestIceLiteConnectivity) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2585 | auto ice_full_port = |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2586 | CreateTestPort(kLocalAddr1, "lfrag", "lpass", |
| 2587 | cricket::ICEROLE_CONTROLLING, kTiebreaker1); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2588 | auto* ice_full_port_ptr = ice_full_port.get(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2589 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2590 | auto ice_lite_port = CreateTestPort( |
| 2591 | kLocalAddr2, "rfrag", "rpass", cricket::ICEROLE_CONTROLLED, kTiebreaker2); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2592 | // Setup TestChannel. This behaves like FULL mode client. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2593 | TestChannel ch1(std::move(ice_full_port)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2594 | ch1.SetIceMode(ICEMODE_FULL); |
| 2595 | |
| 2596 | // Start gathering candidates. |
| 2597 | ch1.Start(); |
| 2598 | ice_lite_port->PrepareAddress(); |
| 2599 | |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2600 | ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2601 | ASSERT_FALSE(ice_lite_port->Candidates().empty()); |
| 2602 | |
Guo-wei Shieh | 1eb87c7 | 2015-08-25 11:02:55 -0700 | [diff] [blame] | 2603 | ch1.CreateConnection(GetCandidate(ice_lite_port.get())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2604 | ASSERT_TRUE(ch1.conn() != NULL); |
| 2605 | EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state()); |
| 2606 | |
| 2607 | // Send ping from full mode client. |
| 2608 | // This ping must not have USE_CANDIDATE_ATTR. |
| 2609 | ch1.Ping(); |
| 2610 | |
| 2611 | // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly |
| 2612 | // from port. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2613 | ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout); |
| 2614 | IceMessage* msg = ice_full_port_ptr->last_stun_msg(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2615 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL); |
| 2616 | |
| 2617 | // Respond with a BINDING-RESPONSE from litemode client. |
| 2618 | // NOTE: Ideally we should't create connection at this stage from lite |
| 2619 | // port, as it should be done only after receiving ping with USE_CANDIDATE. |
| 2620 | // But we need a connection to send a response message. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2621 | ice_lite_port->CreateConnection(ice_full_port_ptr->Candidates()[0], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2622 | cricket::Port::ORIGIN_MESSAGE); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2623 | std::unique_ptr<IceMessage> request = CopyStunMessage(*msg); |
| 2624 | ice_lite_port->SendBindingResponse( |
| 2625 | request.get(), ice_full_port_ptr->Candidates()[0].address()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Feeding the respone message from litemode to the full mode connection. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 2628 | ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(), |
| 2629 | ice_lite_port->last_stun_buf()->size(), |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 2630 | /* packet_time_us */ -1); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2631 | // Verifying full mode connection becomes writable from the response. |
| 2632 | EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(), |
Honghai Zhang | 161a586 | 2016-10-20 11:47:02 -0700 | [diff] [blame] | 2633 | kDefaultTimeout); |
| 2634 | EXPECT_TRUE_WAIT(ch1.nominated(), kDefaultTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2635 | |
| 2636 | // Clear existing stun messsages. Otherwise we will process old stun |
| 2637 | // message right after we send ping. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2638 | ice_full_port_ptr->Reset(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2639 | // Send ping. This must have USE_CANDIDATE_ATTR. |
| 2640 | ch1.Ping(); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2641 | ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout); |
| 2642 | msg = ice_full_port_ptr->last_stun_msg(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2643 | EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); |
| 2644 | ch1.Stop(); |
| 2645 | } |
| 2646 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2647 | // This test case verifies that both the controlling port and the controlled |
| 2648 | // port will time out after connectivity is lost, if they are not marked as |
| 2649 | // "keep alive until pruned." |
| 2650 | TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) { |
| 2651 | rtc::ScopedFakeClock clock; |
| 2652 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2653 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 2654 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2655 | port1->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2656 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2657 | port1->SetIceTiebreaker(kTiebreaker1); |
| 2658 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2659 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 2660 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2661 | port2->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2662 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2663 | port2->SetIceTiebreaker(kTiebreaker2); |
| 2664 | |
| 2665 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2666 | TestChannel ch1(std::move(port1)); |
| 2667 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 2670 | StartConnectAndStopChannels(&ch1, &ch2); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2671 | // After the connection is destroyed, the port will be destroyed because |
| 2672 | // none of them is marked as "keep alive until pruned. |
| 2673 | EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), 110, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2676 | // Test that if after all connection are destroyed, new connections are created |
| 2677 | // and destroyed again, ports won't be destroyed until a timeout period passes |
| 2678 | // after the last set of connections are all destroyed. |
| 2679 | TEST_F(PortTest, TestPortTimeoutAfterNewConnectionCreatedAndDestroyed) { |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 2680 | rtc::ScopedFakeClock clock; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2681 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2682 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 2683 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2684 | port1->set_timeout_delay(timeout_delay); // milliseconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2685 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2686 | port1->SetIceTiebreaker(kTiebreaker1); |
| 2687 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2688 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 2689 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2690 | port2->set_timeout_delay(timeout_delay); // milliseconds |
| 2691 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2692 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2693 | port2->SetIceTiebreaker(kTiebreaker2); |
| 2694 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2695 | // Set up channels and ensure both ports will be deleted. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2696 | TestChannel ch1(std::move(port1)); |
| 2697 | TestChannel ch2(std::move(port2)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2698 | |
| 2699 | // Simulate a connection that succeeds, and then is destroyed. |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 2700 | StartConnectAndStopChannels(&ch1, &ch2); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2701 | SIMULATED_WAIT(ports_destroyed() > 0, 80, clock); |
| 2702 | EXPECT_EQ(0, ports_destroyed()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2703 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2704 | // Start the second set of connection and destroy them. |
| 2705 | ch1.CreateConnection(GetCandidate(ch2.port())); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 2706 | ch2.CreateConnection(GetCandidate(ch1.port())); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2707 | ch1.Stop(); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 2708 | ch2.Stop(); |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 2709 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2710 | SIMULATED_WAIT(ports_destroyed() > 0, 80, clock); |
| 2711 | EXPECT_EQ(0, ports_destroyed()); |
| 2712 | |
| 2713 | // The ports on both sides should be destroyed after timeout. |
| 2714 | EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 30, clock); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2715 | } |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2716 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2717 | // This test case verifies that neither the controlling port nor the controlled |
| 2718 | // port will time out after connectivity is lost if they are marked as "keep |
| 2719 | // alive until pruned". They will time out after they are pruned. |
| 2720 | TEST_F(PortTest, TestPortNotTimeoutUntilPruned) { |
| 2721 | rtc::ScopedFakeClock clock; |
| 2722 | int timeout_delay = 100; |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2723 | auto port1 = CreateUdpPort(kLocalAddr1); |
| 2724 | ConnectToSignalDestroyed(port1.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2725 | port1->set_timeout_delay(timeout_delay); // milliseconds |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2726 | port1->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 2727 | port1->SetIceTiebreaker(kTiebreaker1); |
| 2728 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2729 | auto port2 = CreateUdpPort(kLocalAddr2); |
| 2730 | ConnectToSignalDestroyed(port2.get()); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2731 | port2->set_timeout_delay(timeout_delay); // milliseconds |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2732 | port2->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 2733 | port2->SetIceTiebreaker(kTiebreaker2); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2734 | // The connection must not be destroyed before a connection is attempted. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2735 | EXPECT_EQ(0, ports_destroyed()); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2736 | |
| 2737 | port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 2738 | port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); |
| 2739 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2740 | // Set up channels and keep the port alive. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2741 | TestChannel ch1(std::move(port1)); |
| 2742 | TestChannel ch2(std::move(port2)); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2743 | // Simulate a connection that succeeds, and then is destroyed. But ports |
| 2744 | // are kept alive. Ports won't be destroyed. |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2745 | StartConnectAndStopChannels(&ch1, &ch2); |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2746 | ch1.port()->KeepAliveUntilPruned(); |
| 2747 | ch2.port()->KeepAliveUntilPruned(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2748 | SIMULATED_WAIT(ports_destroyed() > 0, 150, clock); |
| 2749 | EXPECT_EQ(0, ports_destroyed()); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2750 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2751 | // If they are pruned now, they will be destroyed right away. |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2752 | ch1.port()->Prune(); |
| 2753 | ch2.port()->Prune(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 2754 | // The ports on both sides should be destroyed after timeout. |
| 2755 | EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 1, clock); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 2756 | } |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2757 | |
| 2758 | TEST_F(PortTest, TestSupportsProtocol) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2759 | auto udp_port = CreateUdpPort(kLocalAddr1); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2760 | EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2761 | EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2762 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2763 | auto stun_port = CreateStunPort(kLocalAddr1, nat_socket_factory1()); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2764 | EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2765 | EXPECT_FALSE(stun_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2766 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2767 | auto tcp_port = CreateTcpPort(kLocalAddr1); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2768 | EXPECT_TRUE(tcp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2769 | EXPECT_TRUE(tcp_port->SupportsProtocol(SSLTCP_PROTOCOL_NAME)); |
| 2770 | EXPECT_FALSE(tcp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2771 | |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2772 | auto turn_port = |
| 2773 | CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 2774 | EXPECT_TRUE(turn_port->SupportsProtocol(UDP_PROTOCOL_NAME)); |
| 2775 | EXPECT_FALSE(turn_port->SupportsProtocol(TCP_PROTOCOL_NAME)); |
| 2776 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2777 | |
| 2778 | // Test that SetIceParameters updates the component, ufrag and password |
| 2779 | // on both the port itself and its candidates. |
| 2780 | TEST_F(PortTest, TestSetIceParameters) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2781 | auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1"); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2782 | port->PrepareAddress(); |
| 2783 | EXPECT_EQ(1UL, port->Candidates().size()); |
| 2784 | port->SetIceParameters(1, "ufrag2", "password2"); |
| 2785 | EXPECT_EQ(1, port->component()); |
| 2786 | EXPECT_EQ("ufrag2", port->username_fragment()); |
| 2787 | EXPECT_EQ("password2", port->password()); |
| 2788 | const Candidate& candidate = port->Candidates()[0]; |
| 2789 | EXPECT_EQ(1, candidate.component()); |
| 2790 | EXPECT_EQ("ufrag2", candidate.username()); |
| 2791 | EXPECT_EQ("password2", candidate.password()); |
| 2792 | } |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 2793 | |
| 2794 | TEST_F(PortTest, TestAddConnectionWithSameAddress) { |
Steve Anton | 11358fe | 2018-10-09 15:39:19 -0700 | [diff] [blame] | 2795 | auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1"); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 2796 | port->PrepareAddress(); |
| 2797 | EXPECT_EQ(1u, port->Candidates().size()); |
| 2798 | rtc::SocketAddress address("1.1.1.1", 5000); |
| 2799 | cricket::Candidate candidate(1, "udp", address, 0, "", "", "relay", 0, ""); |
| 2800 | cricket::Connection* conn1 = |
| 2801 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 2802 | cricket::Connection* conn_in_use = port->GetConnection(address); |
| 2803 | EXPECT_EQ(conn1, conn_in_use); |
| 2804 | EXPECT_EQ(0u, conn_in_use->remote_candidate().generation()); |
| 2805 | |
| 2806 | // Creating with a candidate with the same address again will get us a |
| 2807 | // different connection with the new candidate. |
| 2808 | candidate.set_generation(2); |
| 2809 | cricket::Connection* conn2 = |
| 2810 | port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); |
| 2811 | EXPECT_NE(conn1, conn2); |
| 2812 | conn_in_use = port->GetConnection(address); |
| 2813 | EXPECT_EQ(conn2, conn_in_use); |
| 2814 | EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); |
| 2815 | |
| 2816 | // Make sure the new connection was not deleted. |
| 2817 | rtc::Thread::Current()->ProcessMessages(300); |
| 2818 | EXPECT_TRUE(port->GetConnection(address) != nullptr); |
| 2819 | } |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 2820 | |
| 2821 | } // namespace cricket |