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