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