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