blob: 3556e3102a4a235c4406737dc66b83bda41d258f [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "p2p/base/port.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
deadbeef5c3c1042017-08-04 15:01:57 -070016#include <list>
kwiberg3ec46792016-04-27 07:22:53 -070017#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010018#include <string>
19#include <utility>
20#include <vector>
kwiberg3ec46792016-04-27 07:22:53 -070021
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "absl/types/optional.h"
23#include "api/candidate.h"
Patrik Höglund7d003422019-09-17 12:16:35 +020024#include "api/packet_socket_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "api/units/time_delta.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "p2p/base/basic_packet_socket_factory.h"
27#include "p2p/base/p2p_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "p2p/base/port_allocator.h"
29#include "p2p/base/port_interface.h"
30#include "p2p/base/relay_port.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "p2p/base/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "p2p/base/stun_port.h"
33#include "p2p/base/stun_server.h"
34#include "p2p/base/tcp_port.h"
35#include "p2p/base/test_relay_server.h"
36#include "p2p/base/test_stun_server.h"
37#include "p2p/base/test_turn_server.h"
38#include "p2p/base/transport_description.h"
39#include "p2p/base/turn_port.h"
40#include "p2p/base/turn_server.h"
41#include "p2p/client/relay_port_factory_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/arraysize.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "rtc_base/async_packet_socket.h"
44#include "rtc_base/async_socket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/byte_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010047#include "rtc_base/checks.h"
48#include "rtc_base/dscp.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/gunit.h"
51#include "rtc_base/helpers.h"
52#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080053#include "rtc_base/nat_server.h"
54#include "rtc_base/nat_socket_factory.h"
55#include "rtc_base/nat_types.h"
56#include "rtc_base/net_helper.h"
Yves Gerey3e707812018-11-28 16:47:49 +010057#include "rtc_base/network.h"
58#include "rtc_base/network/sent_packet.h"
59#include "rtc_base/network_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080060#include "rtc_base/proxy_info.h"
Yves Gerey3e707812018-11-28 16:47:49 +010061#include "rtc_base/socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080062#include "rtc_base/socket_adapters.h"
63#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010064#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080066#include "rtc_base/time_utils.h"
67#include "rtc_base/virtual_socket_server.h"
Yves Gerey3e707812018-11-28 16:47:49 +010068#include "test/gtest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069
70using rtc::AsyncPacketSocket;
jbauchf1f87202016-03-30 06:43:37 -070071using rtc::ByteBufferReader;
72using rtc::ByteBufferWriter;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073using rtc::NAT_ADDR_RESTRICTED;
Jonas Olssona4d87372019-07-05 19:08:33 +020074using rtc::NAT_OPEN_CONE;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075using rtc::NAT_PORT_RESTRICTED;
76using rtc::NAT_SYMMETRIC;
Jonas Olssona4d87372019-07-05 19:08:33 +020077using rtc::NATType;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078using rtc::PacketSocketFactory;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079using rtc::Socket;
80using rtc::SocketAddress;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081
Steve Antonbabf9172017-11-29 10:19:02 -080082namespace cricket {
83namespace {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084
Steve Antonbabf9172017-11-29 10:19:02 -080085constexpr int kDefaultTimeout = 3000;
86constexpr int kShortTimeout = 1000;
Qingsi Wang22e623a2018-03-13 10:53:57 -070087constexpr int kMaxExpectedSimulatedRtt = 200;
Steve Antonbabf9172017-11-29 10:19:02 -080088const SocketAddress kLocalAddr1("192.168.1.2", 0);
89const SocketAddress kLocalAddr2("192.168.1.3", 0);
90const SocketAddress kNatAddr1("77.77.77.77", rtc::NAT_SERVER_UDP_PORT);
91const SocketAddress kNatAddr2("88.88.88.88", rtc::NAT_SERVER_UDP_PORT);
92const SocketAddress kStunAddr("99.99.99.1", STUN_SERVER_PORT);
93const SocketAddress kRelayUdpIntAddr("99.99.99.2", 5000);
94const SocketAddress kRelayUdpExtAddr("99.99.99.3", 5001);
95const SocketAddress kRelayTcpIntAddr("99.99.99.2", 5002);
96const SocketAddress kRelayTcpExtAddr("99.99.99.3", 5003);
97const SocketAddress kRelaySslTcpIntAddr("99.99.99.2", 5004);
98const SocketAddress kRelaySslTcpExtAddr("99.99.99.3", 5005);
99const SocketAddress kTurnUdpIntAddr("99.99.99.4", STUN_SERVER_PORT);
100const SocketAddress kTurnTcpIntAddr("99.99.99.4", 5010);
101const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0);
102const RelayCredentials kRelayCredentials("test", "test");
103
104// TODO(?): Update these when RFC5245 is completely supported.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105// Magic value of 30 is from RFC3484, for IPv4 addresses.
Steve Antonbabf9172017-11-29 10:19:02 -0800106const uint32_t kDefaultPrflxPriority = ICE_TYPE_PREFERENCE_PRFLX << 24 |
107 30 << 8 |
108 (256 - ICE_CANDIDATE_COMPONENT_DEFAULT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000109
Steve Antonbabf9172017-11-29 10:19:02 -0800110constexpr int kTiebreaker1 = 11111;
111constexpr int kTiebreaker2 = 22222;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112
Steve Antonbabf9172017-11-29 10:19:02 -0800113const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700114
Steve Antonbabf9172017-11-29 10:19:02 -0800115constexpr int kGturnUserNameLength = 16;
zhihuang6d0d4bf2016-05-24 10:13:32 -0700116
Steve Antonbabf9172017-11-29 10:19:02 -0800117Candidate GetCandidate(Port* port) {
118 RTC_DCHECK_GE(port->Candidates().size(), 1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 return port->Candidates()[0];
120}
121
Steve Antonbabf9172017-11-29 10:19:02 -0800122SocketAddress GetAddress(Port* port) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123 return GetCandidate(port).address();
124}
125
Steve Anton11358fe2018-10-09 15:39:19 -0700126std::unique_ptr<IceMessage> CopyStunMessage(const IceMessage& src) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200127 auto dst = std::make_unique<IceMessage>();
jbauchf1f87202016-03-30 06:43:37 -0700128 ByteBufferWriter buf;
Steve Anton11358fe2018-10-09 15:39:19 -0700129 src.Write(&buf);
jbauchf1f87202016-03-30 06:43:37 -0700130 ByteBufferReader read_buf(buf);
131 dst->Read(&read_buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 return dst;
133}
134
Steve Anton11358fe2018-10-09 15:39:19 -0700135bool WriteStunMessage(const StunMessage& msg, ByteBufferWriter* buf) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136 buf->Resize(0); // clear out any existing buffer contents
Steve Anton11358fe2018-10-09 15:39:19 -0700137 return msg.Write(buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138}
139
Steve Antonbabf9172017-11-29 10:19:02 -0800140} // namespace
141
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142// Stub port class for testing STUN generation and processing.
143class TestPort : public Port {
144 public:
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000145 TestPort(rtc::Thread* thread,
146 const std::string& type,
147 rtc::PacketSocketFactory* factory,
148 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 uint16_t min_port,
150 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000151 const std::string& username_fragment,
152 const std::string& password)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200153 : Port(thread,
154 type,
155 factory,
156 network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200157 min_port,
158 max_port,
159 username_fragment,
160 password) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 ~TestPort() {}
162
163 // Expose GetStunMessage so that we can test it.
164 using cricket::Port::GetStunMessage;
165
166 // The last StunMessage that was sent on this Port.
Steve Antonbabf9172017-11-29 10:19:02 -0800167 // TODO(?): Make these const; requires changes to SendXXXXResponse.
Karl Wiberg4b6c2ec2018-10-03 15:17:14 +0200168 rtc::BufferT<uint8_t>* last_stun_buf() { return last_stun_buf_.get(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 IceMessage* last_stun_msg() { return last_stun_msg_.get(); }
170 int last_stun_error_code() {
171 int code = 0;
172 if (last_stun_msg_) {
173 const StunErrorCodeAttribute* error_attr = last_stun_msg_->GetErrorCode();
174 if (error_attr) {
175 code = error_attr->code();
176 }
177 }
178 return code;
179 }
180
181 virtual void PrepareAddress() {
deadbeef5c3c1042017-08-04 15:01:57 -0700182 // Act as if the socket was bound to the best IP on the network, to the
183 // first port in the allowed range.
184 rtc::SocketAddress addr(Network()->GetBestIP(), min_port());
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700185 AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(),
zhihuang26d99c22017-02-13 12:47:27 -0800186 ICE_TYPE_PREFERENCE_HOST, 0, "", true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000187 }
188
Honghai Zhangf9945b22015-12-15 12:20:13 -0800189 virtual bool SupportsProtocol(const std::string& protocol) const {
190 return true;
191 }
192
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -0700193 virtual ProtocolType GetProtocol() const { return PROTO_UDP; }
194
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000195 // Exposed for testing candidate building.
196 void AddCandidateAddress(const rtc::SocketAddress& addr) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700197 AddAddress(addr, addr, rtc::SocketAddress(), "udp", "", "", Type(),
zhihuang26d99c22017-02-13 12:47:27 -0800198 type_preference_, 0, "", false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 }
200 void AddCandidateAddress(const rtc::SocketAddress& addr,
201 const rtc::SocketAddress& base_address,
202 const std::string& type,
203 int type_preference,
204 bool final) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700205 AddAddress(addr, base_address, rtc::SocketAddress(), "udp", "", "", type,
zhihuang26d99c22017-02-13 12:47:27 -0800206 type_preference, 0, "", final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 }
208
209 virtual Connection* CreateConnection(const Candidate& remote_candidate,
210 CandidateOrigin origin) {
211 Connection* conn = new ProxyConnection(this, 0, remote_candidate);
honghaiz36f50e82016-06-01 15:57:03 -0700212 AddOrReplaceConnection(conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 // Set use-candidate attribute flag as this will add USE-CANDIDATE attribute
214 // in STUN binding requests.
215 conn->set_use_candidate_attr(true);
216 return conn;
217 }
Yves Gerey665174f2018-06-19 15:03:05 +0200218 virtual int SendTo(const void* data,
219 size_t size,
220 const rtc::SocketAddress& addr,
221 const rtc::PacketOptions& options,
222 bool payload) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000223 if (!payload) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200224 auto msg = std::make_unique<IceMessage>();
225 auto buf = std::make_unique<rtc::BufferT<uint8_t>>(
Steve Anton11358fe2018-10-09 15:39:19 -0700226 static_cast<const char*>(data), size);
jbauchf1f87202016-03-30 06:43:37 -0700227 ByteBufferReader read_buf(*buf);
228 if (!msg->Read(&read_buf)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000229 return -1;
230 }
Steve Anton11358fe2018-10-09 15:39:19 -0700231 last_stun_buf_ = std::move(buf);
232 last_stun_msg_ = std::move(msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000233 }
234 return static_cast<int>(size);
235 }
Yves Gerey665174f2018-06-19 15:03:05 +0200236 virtual int SetOption(rtc::Socket::Option opt, int value) { return 0; }
237 virtual int GetOption(rtc::Socket::Option opt, int* value) { return -1; }
238 virtual int GetError() { return 0; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000239 void Reset() {
240 last_stun_buf_.reset();
241 last_stun_msg_.reset();
242 }
243 void set_type_preference(int type_preference) {
244 type_preference_ = type_preference;
245 }
246
247 private:
Stefan Holmer55674ff2016-01-14 15:49:16 +0100248 void OnSentPacket(rtc::AsyncPacketSocket* socket,
249 const rtc::SentPacket& sent_packet) {
250 PortInterface::SignalSentPacket(sent_packet);
251 }
Karl Wiberg4b6c2ec2018-10-03 15:17:14 +0200252 std::unique_ptr<rtc::BufferT<uint8_t>> last_stun_buf_;
kwiberg3ec46792016-04-27 07:22:53 -0700253 std::unique_ptr<IceMessage> last_stun_msg_;
pbos7640ffa2015-11-30 09:16:59 -0800254 int type_preference_ = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255};
256
Yves Gerey665174f2018-06-19 15:03:05 +0200257static void SendPingAndReceiveResponse(Connection* lconn,
258 TestPort* lport,
259 Connection* rconn,
260 TestPort* rport,
261 rtc::ScopedFakeClock* clock,
262 int64_t ms) {
hbosbf8d3e52017-02-28 06:34:47 -0800263 lconn->Ping(rtc::TimeMillis());
264 ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout);
265 ASSERT_TRUE(lport->last_stun_buf());
266 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +0100267 lport->last_stun_buf()->size(), /* packet_time_us */ -1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200268 clock->AdvanceTime(webrtc::TimeDelta::ms(ms));
hbosbf8d3e52017-02-28 06:34:47 -0800269 ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout);
270 ASSERT_TRUE(rport->last_stun_buf());
271 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +0100272 rport->last_stun_buf()->size(), /* packet_time_us */ -1);
hbosbf8d3e52017-02-28 06:34:47 -0800273}
274
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275class TestChannel : public sigslot::has_slots<> {
276 public:
277 // Takes ownership of |p1| (but not |p2|).
Steve Anton11358fe2018-10-09 15:39:19 -0700278 explicit TestChannel(std::unique_ptr<Port> p1) : port_(std::move(p1)) {
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700279 port_->SignalPortComplete.connect(this, &TestChannel::OnPortComplete);
280 port_->SignalUnknownAddress.connect(this, &TestChannel::OnUnknownAddress);
281 port_->SignalDestroyed.connect(this, &TestChannel::OnSrcPortDestroyed);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000282 }
283
284 int complete_count() { return complete_count_; }
285 Connection* conn() { return conn_; }
286 const SocketAddress& remote_address() { return remote_address_; }
287 const std::string remote_fragment() { return remote_frag_; }
288
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700289 void Start() { port_->PrepareAddress(); }
290 void CreateConnection(const Candidate& remote_candidate) {
291 conn_ = port_->CreateConnection(remote_candidate, Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000292 IceMode remote_ice_mode =
293 (ice_mode_ == ICEMODE_FULL) ? ICEMODE_LITE : ICEMODE_FULL;
294 conn_->set_remote_ice_mode(remote_ice_mode);
295 conn_->set_use_candidate_attr(remote_ice_mode == ICEMODE_FULL);
Yves Gerey665174f2018-06-19 15:03:05 +0200296 conn_->SignalStateChange.connect(this,
297 &TestChannel::OnConnectionStateChange);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700298 conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700299 conn_->SignalReadyToSend.connect(this,
300 &TestChannel::OnConnectionReadyToSend);
301 connection_ready_to_send_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000302 }
303 void OnConnectionStateChange(Connection* conn) {
304 if (conn->write_state() == Connection::STATE_WRITABLE) {
305 conn->set_use_candidate_attr(true);
306 nominated_ = true;
307 }
308 }
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700309 void AcceptConnection(const Candidate& remote_candidate) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000310 ASSERT_TRUE(remote_request_.get() != NULL);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700311 Candidate c = remote_candidate;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000312 c.set_address(remote_address_);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700313 conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700314 conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700315 port_->SendBindingResponse(remote_request_.get(), remote_address_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000316 remote_request_.reset();
317 }
Yves Gerey665174f2018-06-19 15:03:05 +0200318 void Ping() { Ping(0); }
honghaiz34b11eb2016-03-16 08:55:44 -0700319 void Ping(int64_t now) { conn_->Ping(now); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000320 void Stop() {
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700321 if (conn_) {
322 conn_->Destroy();
323 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000324 }
325
Yves Gerey665174f2018-06-19 15:03:05 +0200326 void OnPortComplete(Port* port) { complete_count_++; }
327 void SetIceMode(IceMode ice_mode) { ice_mode_ = ice_mode; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000328
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700329 int SendData(const char* data, size_t len) {
330 rtc::PacketOptions options;
331 return conn_->Send(data, len, options);
332 }
333
Yves Gerey665174f2018-06-19 15:03:05 +0200334 void OnUnknownAddress(PortInterface* port,
335 const SocketAddress& addr,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000336 ProtocolType proto,
Yves Gerey665174f2018-06-19 15:03:05 +0200337 IceMessage* msg,
338 const std::string& rf,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000339 bool /*port_muxed*/) {
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700340 ASSERT_EQ(port_.get(), port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341 if (!remote_address_.IsNil()) {
342 ASSERT_EQ(remote_address_, addr);
343 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 const cricket::StunUInt32Attribute* priority_attr =
345 msg->GetUInt32(STUN_ATTR_PRIORITY);
346 const cricket::StunByteStringAttribute* mi_attr =
347 msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY);
348 const cricket::StunUInt32Attribute* fingerprint_attr =
349 msg->GetUInt32(STUN_ATTR_FINGERPRINT);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700350 EXPECT_TRUE(priority_attr != NULL);
351 EXPECT_TRUE(mi_attr != NULL);
352 EXPECT_TRUE(fingerprint_attr != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000353 remote_address_ = addr;
Steve Anton11358fe2018-10-09 15:39:19 -0700354 remote_request_ = CopyStunMessage(*msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000355 remote_frag_ = rf;
356 }
357
358 void OnDestroyed(Connection* conn) {
359 ASSERT_EQ(conn_, conn);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 RTC_LOG(INFO) << "OnDestroy connection " << conn << " deleted";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000361 conn_ = NULL;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700362 // When the connection is destroyed, also clear these fields so future
363 // connections are possible.
364 remote_request_.reset();
365 remote_address_.Clear();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000366 }
367
368 void OnSrcPortDestroyed(PortInterface* port) {
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700369 Port* destroyed_src = port_.release();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000370 ASSERT_EQ(destroyed_src, port);
371 }
372
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700373 Port* port() { return port_.get(); }
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700374
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000375 bool nominated() const { return nominated_; }
376
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700377 void set_connection_ready_to_send(bool ready) {
378 connection_ready_to_send_ = ready;
379 }
Yves Gerey665174f2018-06-19 15:03:05 +0200380 bool connection_ready_to_send() const { return connection_ready_to_send_; }
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700381
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000382 private:
skvladc309e0e2016-07-28 17:15:20 -0700383 // ReadyToSend will only issue after a Connection recovers from ENOTCONN
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700384 void OnConnectionReadyToSend(Connection* conn) {
385 ASSERT_EQ(conn, conn_);
386 connection_ready_to_send_ = true;
387 }
388
Steve Anton11358fe2018-10-09 15:39:19 -0700389 IceMode ice_mode_ = ICEMODE_FULL;
kwiberg3ec46792016-04-27 07:22:53 -0700390 std::unique_ptr<Port> port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000391
Steve Anton11358fe2018-10-09 15:39:19 -0700392 int complete_count_ = 0;
393 Connection* conn_ = nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 SocketAddress remote_address_;
kwiberg3ec46792016-04-27 07:22:53 -0700395 std::unique_ptr<StunMessage> remote_request_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000396 std::string remote_frag_;
Steve Anton11358fe2018-10-09 15:39:19 -0700397 bool nominated_ = false;
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700398 bool connection_ready_to_send_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000399};
400
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200401class PortTest : public ::testing::Test, public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000402 public:
403 PortTest()
deadbeef98e186c2017-05-16 18:00:06 -0700404 : ss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 05:25:41 -0700405 main_(ss_.get()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000406 socket_factory_(rtc::Thread::Current()),
deadbeefc5d0d952015-07-16 10:22:21 -0700407 nat_factory1_(ss_.get(), kNatAddr1, SocketAddress()),
408 nat_factory2_(ss_.get(), kNatAddr2, SocketAddress()),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409 nat_socket_factory1_(&nat_factory1_),
410 nat_socket_factory2_(&nat_factory2_),
nisse7eaa4ea2017-05-08 05:25:41 -0700411 stun_server_(TestStunServer::Create(&main_, kStunAddr)),
412 turn_server_(&main_, kTurnUdpIntAddr, kTurnUdpExtAddr),
413 relay_server_(&main_,
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700414 kRelayUdpIntAddr,
415 kRelayUdpExtAddr,
416 kRelayTcpIntAddr,
417 kRelayTcpExtAddr,
418 kRelaySslTcpIntAddr,
419 kRelaySslTcpExtAddr),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000420 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)),
421 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000422 role_conflict_(false),
Yves Gerey665174f2018-06-19 15:03:05 +0200423 ports_destroyed_(0) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000424
425 protected:
426 void TestLocalToLocal() {
Steve Anton11358fe2018-10-09 15:39:19 -0700427 auto port1 = CreateUdpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700428 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700429 auto port2 = CreateUdpPort(kLocalAddr2);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700430 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700431 TestConnectivity("udp", std::move(port1), "udp", std::move(port2), true,
432 true, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000433 }
434 void TestLocalToStun(NATType ntype) {
Steve Anton11358fe2018-10-09 15:39:19 -0700435 auto port1 = CreateUdpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700436 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700437 nat_server2_ = CreateNatServer(kNatAddr2, ntype);
438 auto port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700439 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700440 TestConnectivity("udp", std::move(port1), StunName(ntype), std::move(port2),
Yves Gerey665174f2018-06-19 15:03:05 +0200441 ntype == NAT_OPEN_CONE, true, ntype != NAT_SYMMETRIC,
442 true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000443 }
444 void TestLocalToRelay(RelayType rtype, ProtocolType proto) {
Steve Anton11358fe2018-10-09 15:39:19 -0700445 auto port1 = CreateUdpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700446 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700447 auto port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_UDP);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700448 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700449 TestConnectivity("udp", std::move(port1), RelayName(rtype, proto),
450 std::move(port2), rtype == RELAY_GTURN, true, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000451 }
452 void TestStunToLocal(NATType ntype) {
Steve Anton11358fe2018-10-09 15:39:19 -0700453 nat_server1_ = CreateNatServer(kNatAddr1, ntype);
454 auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700455 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700456 auto port2 = CreateUdpPort(kLocalAddr2);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700457 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700458 TestConnectivity(StunName(ntype), std::move(port1), "udp", std::move(port2),
459 true, ntype != NAT_SYMMETRIC, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 }
461 void TestStunToStun(NATType ntype1, NATType ntype2) {
Steve Anton11358fe2018-10-09 15:39:19 -0700462 nat_server1_ = CreateNatServer(kNatAddr1, ntype1);
463 auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700464 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700465 nat_server2_ = CreateNatServer(kNatAddr2, ntype2);
466 auto port2 = CreateStunPort(kLocalAddr2, &nat_socket_factory2_);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700467 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700468 TestConnectivity(StunName(ntype1), std::move(port1), StunName(ntype2),
469 std::move(port2), ntype2 == NAT_OPEN_CONE,
470 ntype1 != NAT_SYMMETRIC, ntype2 != NAT_SYMMETRIC,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000471 ntype1 + ntype2 < (NAT_PORT_RESTRICTED + NAT_SYMMETRIC));
472 }
473 void TestStunToRelay(NATType ntype, RelayType rtype, ProtocolType proto) {
Steve Anton11358fe2018-10-09 15:39:19 -0700474 nat_server1_ = CreateNatServer(kNatAddr1, ntype);
475 auto port1 = CreateStunPort(kLocalAddr1, &nat_socket_factory1_);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700476 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700477 auto port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_UDP);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700478 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700479 TestConnectivity(StunName(ntype), std::move(port1), RelayName(rtype, proto),
480 std::move(port2), rtype == RELAY_GTURN,
481 ntype != NAT_SYMMETRIC, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000482 }
483 void TestTcpToTcp() {
Steve Anton11358fe2018-10-09 15:39:19 -0700484 auto port1 = CreateTcpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700485 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700486 auto port2 = CreateTcpPort(kLocalAddr2);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700487 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700488 TestConnectivity("tcp", std::move(port1), "tcp", std::move(port2), true,
489 false, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000490 }
491 void TestTcpToRelay(RelayType rtype, ProtocolType proto) {
Steve Anton11358fe2018-10-09 15:39:19 -0700492 auto port1 = CreateTcpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700493 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700494 auto port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_TCP);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700495 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700496 TestConnectivity("tcp", std::move(port1), RelayName(rtype, proto),
497 std::move(port2), rtype == RELAY_GTURN, false, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000498 }
499 void TestSslTcpToRelay(RelayType rtype, ProtocolType proto) {
Steve Anton11358fe2018-10-09 15:39:19 -0700500 auto port1 = CreateTcpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700501 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700502 auto port2 = CreateRelayPort(kLocalAddr2, rtype, proto, PROTO_SSLTCP);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700503 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Steve Anton11358fe2018-10-09 15:39:19 -0700504 TestConnectivity("ssltcp", std::move(port1), RelayName(rtype, proto),
505 std::move(port2), rtype == RELAY_GTURN, false, true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000506 }
deadbeef5c3c1042017-08-04 15:01:57 -0700507
508 rtc::Network* MakeNetwork(const SocketAddress& addr) {
509 networks_.emplace_back("unittest", "unittest", addr.ipaddr(), 32);
510 networks_.back().AddIP(addr.ipaddr());
511 return &networks_.back();
512 }
513
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000514 // helpers for above functions
Steve Anton11358fe2018-10-09 15:39:19 -0700515 std::unique_ptr<UDPPort> CreateUdpPort(const SocketAddress& addr) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000516 return CreateUdpPort(addr, &socket_factory_);
517 }
Steve Anton11358fe2018-10-09 15:39:19 -0700518 std::unique_ptr<UDPPort> CreateUdpPort(const SocketAddress& addr,
519 PacketSocketFactory* socket_factory) {
Steve Antona8f1e562018-10-10 11:29:44 -0700520 return UDPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0,
521 username_, password_, std::string(), true,
522 absl::nullopt);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000523 }
Steve Anton11358fe2018-10-09 15:39:19 -0700524 std::unique_ptr<TCPPort> CreateTcpPort(const SocketAddress& addr) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700525 return CreateTcpPort(addr, &socket_factory_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000526 }
Steve Anton11358fe2018-10-09 15:39:19 -0700527 std::unique_ptr<TCPPort> CreateTcpPort(const SocketAddress& addr,
528 PacketSocketFactory* socket_factory) {
Steve Antona8f1e562018-10-10 11:29:44 -0700529 return TCPPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0,
530 username_, password_, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000531 }
Steve Anton11358fe2018-10-09 15:39:19 -0700532 std::unique_ptr<StunPort> CreateStunPort(const SocketAddress& addr,
533 rtc::PacketSocketFactory* factory) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000534 ServerAddresses stun_servers;
535 stun_servers.insert(kStunAddr);
Steve Antona8f1e562018-10-10 11:29:44 -0700536 return StunPort::Create(&main_, factory, MakeNetwork(addr), 0, 0, username_,
537 password_, stun_servers, std::string(),
538 absl::nullopt);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000539 }
Steve Anton11358fe2018-10-09 15:39:19 -0700540 std::unique_ptr<Port> CreateRelayPort(const SocketAddress& addr,
541 RelayType rtype,
542 ProtocolType int_proto,
543 ProtocolType ext_proto) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000544 if (rtype == RELAY_TURN) {
545 return CreateTurnPort(addr, &socket_factory_, int_proto, ext_proto);
546 } else {
547 return CreateGturnPort(addr, int_proto, ext_proto);
548 }
549 }
Steve Anton11358fe2018-10-09 15:39:19 -0700550 std::unique_ptr<TurnPort> CreateTurnPort(const SocketAddress& addr,
551 PacketSocketFactory* socket_factory,
552 ProtocolType int_proto,
553 ProtocolType ext_proto) {
Honghai Zhang80f1db92016-01-27 11:54:45 -0800554 SocketAddress server_addr =
555 int_proto == PROTO_TCP ? kTurnTcpIntAddr : kTurnUdpIntAddr;
556 return CreateTurnPort(addr, socket_factory, int_proto, ext_proto,
557 server_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000558 }
Steve Anton11358fe2018-10-09 15:39:19 -0700559 std::unique_ptr<TurnPort> CreateTurnPort(
560 const SocketAddress& addr,
561 PacketSocketFactory* socket_factory,
562 ProtocolType int_proto,
563 ProtocolType ext_proto,
564 const rtc::SocketAddress& server_addr) {
Steve Antonf7dd9df2018-10-10 15:06:28 -0700565 return TurnPort::Create(&main_, socket_factory, MakeNetwork(addr), 0, 0,
566 username_, password_,
567 ProtocolAddress(server_addr, int_proto),
568 kRelayCredentials, 0, "", {}, {}, nullptr, nullptr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569 }
Steve Anton11358fe2018-10-09 15:39:19 -0700570 std::unique_ptr<RelayPort> CreateGturnPort(const SocketAddress& addr,
571 ProtocolType int_proto,
572 ProtocolType ext_proto) {
573 std::unique_ptr<RelayPort> port = CreateGturnPort(addr);
Yves Gerey665174f2018-06-19 15:03:05 +0200574 SocketAddress addrs[] = {kRelayUdpIntAddr, kRelayTcpIntAddr,
575 kRelaySslTcpIntAddr};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000576 port->AddServerAddress(ProtocolAddress(addrs[int_proto], int_proto));
577 return port;
578 }
Steve Anton11358fe2018-10-09 15:39:19 -0700579 std::unique_ptr<RelayPort> CreateGturnPort(const SocketAddress& addr) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700580 // TODO(pthatcher): Remove GTURN.
zhihuang6d0d4bf2016-05-24 10:13:32 -0700581 // Generate a username with length of 16 for Gturn only.
582 std::string username = rtc::CreateRandomString(kGturnUserNameLength);
Steve Antona8f1e562018-10-10 11:29:44 -0700583 return RelayPort::Create(&main_, &socket_factory_, MakeNetwork(addr), 0, 0,
584 username, password_);
Steve Antonbabf9172017-11-29 10:19:02 -0800585 // TODO(?): Add an external address for ext_proto, so that the
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000586 // other side can connect to this port using a non-UDP protocol.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000587 }
Steve Anton11358fe2018-10-09 15:39:19 -0700588 std::unique_ptr<rtc::NATServer> CreateNatServer(const SocketAddress& addr,
589 rtc::NATType type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200590 return std::make_unique<rtc::NATServer>(type, ss_.get(), addr, addr,
591 ss_.get(), addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000592 }
593 static const char* StunName(NATType type) {
594 switch (type) {
hnsl277b2502016-12-13 05:17:23 -0800595 case NAT_OPEN_CONE:
596 return "stun(open cone)";
597 case NAT_ADDR_RESTRICTED:
598 return "stun(addr restricted)";
599 case NAT_PORT_RESTRICTED:
600 return "stun(port restricted)";
601 case NAT_SYMMETRIC:
602 return "stun(symmetric)";
603 default:
604 return "stun(?)";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000605 }
606 }
607 static const char* RelayName(RelayType type, ProtocolType proto) {
608 if (type == RELAY_TURN) {
609 switch (proto) {
hnsl277b2502016-12-13 05:17:23 -0800610 case PROTO_UDP:
611 return "turn(udp)";
612 case PROTO_TCP:
613 return "turn(tcp)";
614 case PROTO_SSLTCP:
615 return "turn(ssltcp)";
616 case PROTO_TLS:
617 return "turn(tls)";
618 default:
619 return "turn(?)";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000620 }
621 } else {
622 switch (proto) {
hnsl277b2502016-12-13 05:17:23 -0800623 case PROTO_UDP:
624 return "gturn(udp)";
625 case PROTO_TCP:
626 return "gturn(tcp)";
627 case PROTO_SSLTCP:
628 return "gturn(ssltcp)";
629 case PROTO_TLS:
630 return "gturn(tls)";
631 default:
632 return "gturn(?)";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000633 }
634 }
635 }
636
637 void TestCrossFamilyPorts(int type);
638
Peter Thatcherb8b01432015-07-07 16:45:53 -0700639 void ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2);
640
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000641 // This does all the work and then deletes |port1| and |port2|.
Yves Gerey665174f2018-06-19 15:03:05 +0200642 void TestConnectivity(const char* name1,
Steve Anton11358fe2018-10-09 15:39:19 -0700643 std::unique_ptr<Port> port1,
Yves Gerey665174f2018-06-19 15:03:05 +0200644 const char* name2,
Steve Anton11358fe2018-10-09 15:39:19 -0700645 std::unique_ptr<Port> port2,
Yves Gerey665174f2018-06-19 15:03:05 +0200646 bool accept,
647 bool same_addr1,
648 bool same_addr2,
649 bool possible);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000650
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700651 // This connects the provided channels which have already started. |ch1|
652 // should have its Connection created (either through CreateConnection() or
653 // TCP reconnecting mechanism before entering this function.
654 void ConnectStartedChannels(TestChannel* ch1, TestChannel* ch2) {
655 ASSERT_TRUE(ch1->conn());
Honghai Zhang161a5862016-10-20 11:47:02 -0700656 EXPECT_TRUE_WAIT(ch1->conn()->connected(),
657 kDefaultTimeout); // for TCP connect
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700658 ch1->Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700659 WAIT(!ch2->remote_address().IsNil(), kShortTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700660
661 // Send a ping from dst to src.
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700662 ch2->AcceptConnection(GetCandidate(ch1->port()));
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700663 ch2->Ping();
664 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch2->conn()->write_state(),
Honghai Zhang161a5862016-10-20 11:47:02 -0700665 kDefaultTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700666 }
667
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000668 // This connects and disconnects the provided channels in the same sequence as
669 // TestConnectivity with all options set to |true|. It does not delete either
670 // channel.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700671 void StartConnectAndStopChannels(TestChannel* ch1, TestChannel* ch2) {
672 // Acquire addresses.
673 ch1->Start();
674 ch2->Start();
675
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700676 ch1->CreateConnection(GetCandidate(ch2->port()));
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700677 ConnectStartedChannels(ch1, ch2);
678
679 // Destroy the connections.
680 ch1->Stop();
681 ch2->Stop();
682 }
683
684 // This disconnects both end's Connection and make sure ch2 ready for new
685 // connection.
686 void DisconnectTcpTestChannels(TestChannel* ch1, TestChannel* ch2) {
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700687 TCPConnection* tcp_conn1 = static_cast<TCPConnection*>(ch1->conn());
688 TCPConnection* tcp_conn2 = static_cast<TCPConnection*>(ch2->conn());
689 ASSERT_TRUE(
690 ss_->CloseTcpConnections(tcp_conn1->socket()->GetLocalAddress(),
691 tcp_conn2->socket()->GetLocalAddress()));
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700692
693 // Wait for both OnClose are delivered.
Honghai Zhang161a5862016-10-20 11:47:02 -0700694 EXPECT_TRUE_WAIT(!ch1->conn()->connected(), kDefaultTimeout);
695 EXPECT_TRUE_WAIT(!ch2->conn()->connected(), kDefaultTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700696
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700697 // Ensure redundant SignalClose events on TcpConnection won't break tcp
698 // reconnection. Chromium will fire SignalClose for all outstanding IPC
699 // packets during reconnection.
700 tcp_conn1->socket()->SignalClose(tcp_conn1->socket(), 0);
701 tcp_conn2->socket()->SignalClose(tcp_conn2->socket(), 0);
702
703 // Speed up destroying ch2's connection such that the test is ready to
704 // accept a new connection from ch1 before ch1's connection destroys itself.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700705 ch2->conn()->Destroy();
Honghai Zhang161a5862016-10-20 11:47:02 -0700706 EXPECT_TRUE_WAIT(ch2->conn() == NULL, kDefaultTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700707 }
708
709 void TestTcpReconnect(bool ping_after_disconnected,
710 bool send_after_disconnected) {
Steve Anton11358fe2018-10-09 15:39:19 -0700711 auto port1 = CreateTcpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700712 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -0700713 auto port2 = CreateTcpPort(kLocalAddr2);
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700714 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700715
716 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
717 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
718
719 // Set up channels and ensure both ports will be deleted.
Steve Anton11358fe2018-10-09 15:39:19 -0700720 TestChannel ch1(std::move(port1));
721 TestChannel ch2(std::move(port2));
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700722 EXPECT_EQ(0, ch1.complete_count());
723 EXPECT_EQ(0, ch2.complete_count());
724
725 ch1.Start();
726 ch2.Start();
Honghai Zhang161a5862016-10-20 11:47:02 -0700727 ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout);
728 ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700729
730 // Initial connecting the channel, create connection on channel1.
Steve Anton11358fe2018-10-09 15:39:19 -0700731 ch1.CreateConnection(GetCandidate(ch2.port()));
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700732 ConnectStartedChannels(&ch1, &ch2);
733
734 // Shorten the timeout period.
Honghai Zhang161a5862016-10-20 11:47:02 -0700735 const int kTcpReconnectTimeout = kDefaultTimeout;
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700736 static_cast<TCPConnection*>(ch1.conn())
737 ->set_reconnection_timeout(kTcpReconnectTimeout);
738 static_cast<TCPConnection*>(ch2.conn())
739 ->set_reconnection_timeout(kTcpReconnectTimeout);
740
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700741 EXPECT_FALSE(ch1.connection_ready_to_send());
742 EXPECT_FALSE(ch2.connection_ready_to_send());
743
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700744 // Once connected, disconnect them.
745 DisconnectTcpTestChannels(&ch1, &ch2);
746
747 if (send_after_disconnected || ping_after_disconnected) {
748 if (send_after_disconnected) {
749 // First SendData after disconnect should fail but will trigger
750 // reconnect.
751 EXPECT_EQ(-1, ch1.SendData(data, static_cast<int>(strlen(data))));
752 }
753
754 if (ping_after_disconnected) {
755 // Ping should trigger reconnect.
756 ch1.Ping();
757 }
758
759 // Wait for channel's outgoing TCPConnection connected.
Honghai Zhang161a5862016-10-20 11:47:02 -0700760 EXPECT_TRUE_WAIT(ch1.conn()->connected(), kDefaultTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700761
762 // Verify that we could still connect channels.
763 ConnectStartedChannels(&ch1, &ch2);
Yves Gerey665174f2018-06-19 15:03:05 +0200764 EXPECT_TRUE_WAIT(ch1.connection_ready_to_send(), kTcpReconnectTimeout);
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700765 // Channel2 is the passive one so a new connection is created during
skvladc309e0e2016-07-28 17:15:20 -0700766 // reconnect. This new connection should never have issued ENOTCONN
Guo-wei Shiehb5940412015-08-24 11:58:03 -0700767 // hence the connection_ready_to_send() should be false.
768 EXPECT_FALSE(ch2.connection_ready_to_send());
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700769 } else {
770 EXPECT_EQ(ch1.conn()->write_state(), Connection::STATE_WRITABLE);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700771 // Since the reconnection never happens, the connections should have been
772 // destroyed after the timeout.
Honghai Zhang161a5862016-10-20 11:47:02 -0700773 EXPECT_TRUE_WAIT(!ch1.conn(), kTcpReconnectTimeout + kDefaultTimeout);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700774 EXPECT_TRUE(!ch2.conn());
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700775 }
776
777 // Tear down and ensure that goes smoothly.
778 ch1.Stop();
779 ch2.Stop();
Honghai Zhang161a5862016-10-20 11:47:02 -0700780 EXPECT_TRUE_WAIT(ch1.conn() == NULL, kDefaultTimeout);
781 EXPECT_TRUE_WAIT(ch2.conn() == NULL, kDefaultTimeout);
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700782 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000783
Steve Anton11358fe2018-10-09 15:39:19 -0700784 std::unique_ptr<IceMessage> CreateStunMessage(int type) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200785 auto msg = std::make_unique<IceMessage>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000786 msg->SetType(type);
787 msg->SetTransactionID("TESTTESTTEST");
788 return msg;
789 }
Steve Anton11358fe2018-10-09 15:39:19 -0700790 std::unique_ptr<IceMessage> CreateStunMessageWithUsername(
791 int type,
792 const std::string& username) {
793 std::unique_ptr<IceMessage> msg = CreateStunMessage(type);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200794 msg->AddAttribute(std::make_unique<StunByteStringAttribute>(
Karl Wiberg918f50c2018-07-05 11:40:33 +0200795 STUN_ATTR_USERNAME, username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000796 return msg;
797 }
Steve Anton11358fe2018-10-09 15:39:19 -0700798 std::unique_ptr<TestPort> CreateTestPort(const rtc::SocketAddress& addr,
799 const std::string& username,
800 const std::string& password) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200801 auto port =
802 std::make_unique<TestPort>(&main_, "test", &socket_factory_,
803 MakeNetwork(addr), 0, 0, username, password);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000804 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict);
805 return port;
806 }
Steve Anton11358fe2018-10-09 15:39:19 -0700807 std::unique_ptr<TestPort> CreateTestPort(const rtc::SocketAddress& addr,
808 const std::string& username,
809 const std::string& password,
810 cricket::IceRole role,
811 int tiebreaker) {
812 auto port = CreateTestPort(addr, username, password);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000813 port->SetIceRole(role);
814 port->SetIceTiebreaker(tiebreaker);
815 return port;
816 }
deadbeef5c3c1042017-08-04 15:01:57 -0700817 // Overload to create a test port given an rtc::Network directly.
Steve Anton11358fe2018-10-09 15:39:19 -0700818 std::unique_ptr<TestPort> CreateTestPort(rtc::Network* network,
819 const std::string& username,
820 const std::string& password) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200821 auto port = std::make_unique<TestPort>(&main_, "test", &socket_factory_,
822 network, 0, 0, username, password);
deadbeef5c3c1042017-08-04 15:01:57 -0700823 port->SignalRoleConflict.connect(this, &PortTest::OnRoleConflict);
824 return port;
825 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000826
Yves Gerey665174f2018-06-19 15:03:05 +0200827 void OnRoleConflict(PortInterface* port) { role_conflict_ = true; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000828 bool role_conflict() const { return role_conflict_; }
829
830 void ConnectToSignalDestroyed(PortInterface* port) {
831 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed);
832 }
833
Honghai Zhanga74363c2016-07-28 18:06:15 -0700834 void OnDestroyed(PortInterface* port) { ++ports_destroyed_; }
835 int ports_destroyed() const { return ports_destroyed_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000836
837 rtc::BasicPacketSocketFactory* nat_socket_factory1() {
838 return &nat_socket_factory1_;
839 }
840
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -0700841 rtc::VirtualSocketServer* vss() { return ss_.get(); }
842
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000843 private:
deadbeef5c3c1042017-08-04 15:01:57 -0700844 // When a "create port" helper method is called with an IP, we create a
845 // Network with that IP and add it to this list. Using a list instead of a
846 // vector so that when it grows, pointers aren't invalidated.
847 std::list<rtc::Network> networks_;
kwiberg3ec46792016-04-27 07:22:53 -0700848 std::unique_ptr<rtc::VirtualSocketServer> ss_;
nisse7eaa4ea2017-05-08 05:25:41 -0700849 rtc::AutoSocketServerThread main_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000850 rtc::BasicPacketSocketFactory socket_factory_;
kwiberg3ec46792016-04-27 07:22:53 -0700851 std::unique_ptr<rtc::NATServer> nat_server1_;
852 std::unique_ptr<rtc::NATServer> nat_server2_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000853 rtc::NATSocketFactory nat_factory1_;
854 rtc::NATSocketFactory nat_factory2_;
855 rtc::BasicPacketSocketFactory nat_socket_factory1_;
856 rtc::BasicPacketSocketFactory nat_socket_factory2_;
kwiberg3ec46792016-04-27 07:22:53 -0700857 std::unique_ptr<TestStunServer> stun_server_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000858 TestTurnServer turn_server_;
859 TestRelayServer relay_server_;
860 std::string username_;
861 std::string password_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000862 bool role_conflict_;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700863 int ports_destroyed_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000864};
865
Yves Gerey665174f2018-06-19 15:03:05 +0200866void PortTest::TestConnectivity(const char* name1,
Steve Anton11358fe2018-10-09 15:39:19 -0700867 std::unique_ptr<Port> port1,
Yves Gerey665174f2018-06-19 15:03:05 +0200868 const char* name2,
Steve Anton11358fe2018-10-09 15:39:19 -0700869 std::unique_ptr<Port> port2,
Yves Gerey665174f2018-06-19 15:03:05 +0200870 bool accept,
871 bool same_addr1,
872 bool same_addr2,
873 bool possible) {
Honghai Zhang161a5862016-10-20 11:47:02 -0700874 rtc::ScopedFakeClock clock;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100875 RTC_LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": ";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000876 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
877 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
878
879 // Set up channels and ensure both ports will be deleted.
Steve Anton11358fe2018-10-09 15:39:19 -0700880 TestChannel ch1(std::move(port1));
881 TestChannel ch2(std::move(port2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000882 EXPECT_EQ(0, ch1.complete_count());
883 EXPECT_EQ(0, ch2.complete_count());
884
885 // Acquire addresses.
886 ch1.Start();
887 ch2.Start();
Honghai Zhang161a5862016-10-20 11:47:02 -0700888 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock);
889 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000890
891 // Send a ping from src to dst. This may or may not make it.
Steve Anton11358fe2018-10-09 15:39:19 -0700892 ch1.CreateConnection(GetCandidate(ch2.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000893 ASSERT_TRUE(ch1.conn() != NULL);
Honghai Zhang161a5862016-10-20 11:47:02 -0700894 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout,
895 clock); // for TCP connect
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000896 ch1.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700897 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898
899 if (accept) {
900 // We are able to send a ping from src to dst. This is the case when
901 // sending to UDP ports and cone NATs.
902 EXPECT_TRUE(ch1.remote_address().IsNil());
Steve Anton11358fe2018-10-09 15:39:19 -0700903 EXPECT_EQ(ch2.remote_fragment(), ch1.port()->username_fragment());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000904
905 // Ensure the ping came from the same address used for src.
906 // This is the case unless the source NAT was symmetric.
Yves Gerey665174f2018-06-19 15:03:05 +0200907 if (same_addr1)
Steve Anton11358fe2018-10-09 15:39:19 -0700908 EXPECT_EQ(ch2.remote_address(), GetAddress(ch1.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000909 EXPECT_TRUE(same_addr2);
910
911 // Send a ping from dst to src.
Steve Anton11358fe2018-10-09 15:39:19 -0700912 ch2.AcceptConnection(GetCandidate(ch1.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000913 ASSERT_TRUE(ch2.conn() != NULL);
914 ch2.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700915 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
916 ch2.conn()->write_state(), kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000917 } else {
918 // We can't send a ping from src to dst, so flip it around. This will happen
919 // when the destination NAT is addr/port restricted or symmetric.
920 EXPECT_TRUE(ch1.remote_address().IsNil());
921 EXPECT_TRUE(ch2.remote_address().IsNil());
922
923 // Send a ping from dst to src. Again, this may or may not make it.
Steve Anton11358fe2018-10-09 15:39:19 -0700924 ch2.CreateConnection(GetCandidate(ch1.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000925 ASSERT_TRUE(ch2.conn() != NULL);
926 ch2.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700927 SIMULATED_WAIT(ch2.conn()->write_state() == Connection::STATE_WRITABLE,
928 kShortTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000929
930 if (same_addr1 && same_addr2) {
931 // The new ping got back to the source.
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700932 EXPECT_TRUE(ch1.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000933 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
934
935 // First connection may not be writable if the first ping did not get
936 // through. So we will have to do another.
937 if (ch1.conn()->write_state() == Connection::STATE_WRITE_INIT) {
938 ch1.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700939 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
940 ch1.conn()->write_state(), kDefaultTimeout,
941 clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000942 }
943 } else if (!same_addr1 && possible) {
944 // The new ping went to the candidate address, but that address was bad.
945 // This will happen when the source NAT is symmetric.
946 EXPECT_TRUE(ch1.remote_address().IsNil());
947 EXPECT_TRUE(ch2.remote_address().IsNil());
948
949 // However, since we have now sent a ping to the source IP, we should be
950 // able to get a ping from it. This gives us the real source address.
951 ch1.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700952 EXPECT_TRUE_SIMULATED_WAIT(!ch2.remote_address().IsNil(), kDefaultTimeout,
953 clock);
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700954 EXPECT_FALSE(ch2.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000955 EXPECT_TRUE(ch1.remote_address().IsNil());
956
957 // Pick up the actual address and establish the connection.
Steve Anton11358fe2018-10-09 15:39:19 -0700958 ch2.AcceptConnection(GetCandidate(ch1.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000959 ASSERT_TRUE(ch2.conn() != NULL);
960 ch2.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700961 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
962 ch2.conn()->write_state(), kDefaultTimeout,
963 clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000964 } else if (!same_addr2 && possible) {
965 // The new ping came in, but from an unexpected address. This will happen
966 // when the destination NAT is symmetric.
967 EXPECT_FALSE(ch1.remote_address().IsNil());
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700968 EXPECT_FALSE(ch1.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000969
970 // Update our address and complete the connection.
Steve Anton11358fe2018-10-09 15:39:19 -0700971 ch1.AcceptConnection(GetCandidate(ch2.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000972 ch1.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700973 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
974 ch1.conn()->write_state(), kDefaultTimeout,
975 clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000976 } else { // (!possible)
977 // There should be s no way for the pings to reach each other. Check it.
978 EXPECT_TRUE(ch1.remote_address().IsNil());
979 EXPECT_TRUE(ch2.remote_address().IsNil());
980 ch1.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -0700981 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000982 EXPECT_TRUE(ch1.remote_address().IsNil());
983 EXPECT_TRUE(ch2.remote_address().IsNil());
984 }
985 }
986
987 // Everything should be good, unless we know the situation is impossible.
988 ASSERT_TRUE(ch1.conn() != NULL);
989 ASSERT_TRUE(ch2.conn() != NULL);
990 if (possible) {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700991 EXPECT_TRUE(ch1.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000992 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700993 EXPECT_TRUE(ch2.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000994 EXPECT_EQ(Connection::STATE_WRITABLE, ch2.conn()->write_state());
995 } else {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700996 EXPECT_FALSE(ch1.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000997 EXPECT_NE(Connection::STATE_WRITABLE, ch1.conn()->write_state());
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700998 EXPECT_FALSE(ch2.conn()->receiving());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000999 EXPECT_NE(Connection::STATE_WRITABLE, ch2.conn()->write_state());
1000 }
1001
1002 // Tear down and ensure that goes smoothly.
1003 ch1.Stop();
1004 ch2.Stop();
Honghai Zhang161a5862016-10-20 11:47:02 -07001005 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn() == NULL, kDefaultTimeout, clock);
1006 EXPECT_TRUE_SIMULATED_WAIT(ch2.conn() == NULL, kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001007}
1008
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001009class FakePacketSocketFactory : public rtc::PacketSocketFactory {
1010 public:
1011 FakePacketSocketFactory()
1012 : next_udp_socket_(NULL),
1013 next_server_tcp_socket_(NULL),
Yves Gerey665174f2018-06-19 15:03:05 +02001014 next_client_tcp_socket_(NULL) {}
1015 ~FakePacketSocketFactory() override {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001016
pkasting@chromium.org332331f2014-11-06 20:19:22 +00001017 AsyncPacketSocket* CreateUdpSocket(const SocketAddress& address,
Peter Boström0c4e06b2015-10-07 12:23:21 +02001018 uint16_t min_port,
1019 uint16_t max_port) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001020 EXPECT_TRUE(next_udp_socket_ != NULL);
1021 AsyncPacketSocket* result = next_udp_socket_;
1022 next_udp_socket_ = NULL;
1023 return result;
1024 }
1025
pkasting@chromium.org332331f2014-11-06 20:19:22 +00001026 AsyncPacketSocket* CreateServerTcpSocket(const SocketAddress& local_address,
Peter Boström0c4e06b2015-10-07 12:23:21 +02001027 uint16_t min_port,
1028 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +00001029 int opts) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001030 EXPECT_TRUE(next_server_tcp_socket_ != NULL);
1031 AsyncPacketSocket* result = next_server_tcp_socket_;
1032 next_server_tcp_socket_ = NULL;
1033 return result;
1034 }
1035
Patrik Höglund662e31f2019-09-05 14:35:04 +02001036 AsyncPacketSocket* CreateClientTcpSocket(
1037 const SocketAddress& local_address,
1038 const SocketAddress& remote_address,
1039 const rtc::ProxyInfo& proxy_info,
1040 const std::string& user_agent,
1041 const rtc::PacketSocketTcpOptions& opts) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001042 EXPECT_TRUE(next_client_tcp_socket_ != NULL);
1043 AsyncPacketSocket* result = next_client_tcp_socket_;
1044 next_client_tcp_socket_ = NULL;
1045 return result;
1046 }
1047
1048 void set_next_udp_socket(AsyncPacketSocket* next_udp_socket) {
1049 next_udp_socket_ = next_udp_socket;
1050 }
1051 void set_next_server_tcp_socket(AsyncPacketSocket* next_server_tcp_socket) {
1052 next_server_tcp_socket_ = next_server_tcp_socket;
1053 }
1054 void set_next_client_tcp_socket(AsyncPacketSocket* next_client_tcp_socket) {
1055 next_client_tcp_socket_ = next_client_tcp_socket;
1056 }
Yves Gerey665174f2018-06-19 15:03:05 +02001057 rtc::AsyncResolverInterface* CreateAsyncResolver() override { return NULL; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001058
1059 private:
1060 AsyncPacketSocket* next_udp_socket_;
1061 AsyncPacketSocket* next_server_tcp_socket_;
1062 AsyncPacketSocket* next_client_tcp_socket_;
1063};
1064
1065class FakeAsyncPacketSocket : public AsyncPacketSocket {
1066 public:
1067 // Returns current local address. Address may be set to NULL if the
1068 // socket is not bound yet (GetState() returns STATE_BINDING).
Yves Gerey665174f2018-06-19 15:03:05 +02001069 virtual SocketAddress GetLocalAddress() const { return SocketAddress(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001070
1071 // Returns remote address. Returns zeroes if this is not a client TCP socket.
Yves Gerey665174f2018-06-19 15:03:05 +02001072 virtual SocketAddress GetRemoteAddress() const { return SocketAddress(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001073
1074 // Send a packet.
Yves Gerey665174f2018-06-19 15:03:05 +02001075 virtual int Send(const void* pv,
1076 size_t cb,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001077 const rtc::PacketOptions& options) {
1078 return static_cast<int>(cb);
1079 }
Yves Gerey665174f2018-06-19 15:03:05 +02001080 virtual int SendTo(const void* pv,
1081 size_t cb,
1082 const SocketAddress& addr,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001083 const rtc::PacketOptions& options) {
1084 return static_cast<int>(cb);
1085 }
Yves Gerey665174f2018-06-19 15:03:05 +02001086 virtual int Close() { return 0; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001087
1088 virtual State GetState() const { return state_; }
1089 virtual int GetOption(Socket::Option opt, int* value) { return 0; }
1090 virtual int SetOption(Socket::Option opt, int value) { return 0; }
1091 virtual int GetError() const { return 0; }
Yves Gerey665174f2018-06-19 15:03:05 +02001092 virtual void SetError(int error) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001093
1094 void set_state(State state) { state_ = state; }
1095
1096 private:
1097 State state_;
1098};
1099
1100// Local -> XXXX
1101TEST_F(PortTest, TestLocalToLocal) {
1102 TestLocalToLocal();
1103}
1104
1105TEST_F(PortTest, TestLocalToConeNat) {
1106 TestLocalToStun(NAT_OPEN_CONE);
1107}
1108
1109TEST_F(PortTest, TestLocalToARNat) {
1110 TestLocalToStun(NAT_ADDR_RESTRICTED);
1111}
1112
1113TEST_F(PortTest, TestLocalToPRNat) {
1114 TestLocalToStun(NAT_PORT_RESTRICTED);
1115}
1116
1117TEST_F(PortTest, TestLocalToSymNat) {
1118 TestLocalToStun(NAT_SYMMETRIC);
1119}
1120
1121// Flaky: https://code.google.com/p/webrtc/issues/detail?id=3316.
1122TEST_F(PortTest, DISABLED_TestLocalToTurn) {
1123 TestLocalToRelay(RELAY_TURN, PROTO_UDP);
1124}
1125
1126TEST_F(PortTest, TestLocalToGturn) {
1127 TestLocalToRelay(RELAY_GTURN, PROTO_UDP);
1128}
1129
1130TEST_F(PortTest, TestLocalToTcpGturn) {
1131 TestLocalToRelay(RELAY_GTURN, PROTO_TCP);
1132}
1133
1134TEST_F(PortTest, TestLocalToSslTcpGturn) {
1135 TestLocalToRelay(RELAY_GTURN, PROTO_SSLTCP);
1136}
1137
1138// Cone NAT -> XXXX
1139TEST_F(PortTest, TestConeNatToLocal) {
1140 TestStunToLocal(NAT_OPEN_CONE);
1141}
1142
1143TEST_F(PortTest, TestConeNatToConeNat) {
1144 TestStunToStun(NAT_OPEN_CONE, NAT_OPEN_CONE);
1145}
1146
1147TEST_F(PortTest, TestConeNatToARNat) {
1148 TestStunToStun(NAT_OPEN_CONE, NAT_ADDR_RESTRICTED);
1149}
1150
1151TEST_F(PortTest, TestConeNatToPRNat) {
1152 TestStunToStun(NAT_OPEN_CONE, NAT_PORT_RESTRICTED);
1153}
1154
1155TEST_F(PortTest, TestConeNatToSymNat) {
1156 TestStunToStun(NAT_OPEN_CONE, NAT_SYMMETRIC);
1157}
1158
1159TEST_F(PortTest, TestConeNatToTurn) {
1160 TestStunToRelay(NAT_OPEN_CONE, RELAY_TURN, PROTO_UDP);
1161}
1162
1163TEST_F(PortTest, TestConeNatToGturn) {
1164 TestStunToRelay(NAT_OPEN_CONE, RELAY_GTURN, PROTO_UDP);
1165}
1166
1167TEST_F(PortTest, TestConeNatToTcpGturn) {
1168 TestStunToRelay(NAT_OPEN_CONE, RELAY_GTURN, PROTO_TCP);
1169}
1170
1171// Address-restricted NAT -> XXXX
1172TEST_F(PortTest, TestARNatToLocal) {
1173 TestStunToLocal(NAT_ADDR_RESTRICTED);
1174}
1175
1176TEST_F(PortTest, TestARNatToConeNat) {
1177 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_OPEN_CONE);
1178}
1179
1180TEST_F(PortTest, TestARNatToARNat) {
1181 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_ADDR_RESTRICTED);
1182}
1183
1184TEST_F(PortTest, TestARNatToPRNat) {
1185 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_PORT_RESTRICTED);
1186}
1187
1188TEST_F(PortTest, TestARNatToSymNat) {
1189 TestStunToStun(NAT_ADDR_RESTRICTED, NAT_SYMMETRIC);
1190}
1191
1192TEST_F(PortTest, TestARNatToTurn) {
1193 TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_TURN, PROTO_UDP);
1194}
1195
1196TEST_F(PortTest, TestARNatToGturn) {
1197 TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_GTURN, PROTO_UDP);
1198}
1199
1200TEST_F(PortTest, TestARNATNatToTcpGturn) {
1201 TestStunToRelay(NAT_ADDR_RESTRICTED, RELAY_GTURN, PROTO_TCP);
1202}
1203
1204// Port-restricted NAT -> XXXX
1205TEST_F(PortTest, TestPRNatToLocal) {
1206 TestStunToLocal(NAT_PORT_RESTRICTED);
1207}
1208
1209TEST_F(PortTest, TestPRNatToConeNat) {
1210 TestStunToStun(NAT_PORT_RESTRICTED, NAT_OPEN_CONE);
1211}
1212
1213TEST_F(PortTest, TestPRNatToARNat) {
1214 TestStunToStun(NAT_PORT_RESTRICTED, NAT_ADDR_RESTRICTED);
1215}
1216
1217TEST_F(PortTest, TestPRNatToPRNat) {
1218 TestStunToStun(NAT_PORT_RESTRICTED, NAT_PORT_RESTRICTED);
1219}
1220
1221TEST_F(PortTest, TestPRNatToSymNat) {
1222 // Will "fail"
1223 TestStunToStun(NAT_PORT_RESTRICTED, NAT_SYMMETRIC);
1224}
1225
1226TEST_F(PortTest, TestPRNatToTurn) {
1227 TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_TURN, PROTO_UDP);
1228}
1229
1230TEST_F(PortTest, TestPRNatToGturn) {
1231 TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_GTURN, PROTO_UDP);
1232}
1233
1234TEST_F(PortTest, TestPRNatToTcpGturn) {
1235 TestStunToRelay(NAT_PORT_RESTRICTED, RELAY_GTURN, PROTO_TCP);
1236}
1237
1238// Symmetric NAT -> XXXX
1239TEST_F(PortTest, TestSymNatToLocal) {
1240 TestStunToLocal(NAT_SYMMETRIC);
1241}
1242
1243TEST_F(PortTest, TestSymNatToConeNat) {
1244 TestStunToStun(NAT_SYMMETRIC, NAT_OPEN_CONE);
1245}
1246
1247TEST_F(PortTest, TestSymNatToARNat) {
1248 TestStunToStun(NAT_SYMMETRIC, NAT_ADDR_RESTRICTED);
1249}
1250
1251TEST_F(PortTest, TestSymNatToPRNat) {
1252 // Will "fail"
1253 TestStunToStun(NAT_SYMMETRIC, NAT_PORT_RESTRICTED);
1254}
1255
1256TEST_F(PortTest, TestSymNatToSymNat) {
1257 // Will "fail"
1258 TestStunToStun(NAT_SYMMETRIC, NAT_SYMMETRIC);
1259}
1260
1261TEST_F(PortTest, TestSymNatToTurn) {
1262 TestStunToRelay(NAT_SYMMETRIC, RELAY_TURN, PROTO_UDP);
1263}
1264
1265TEST_F(PortTest, TestSymNatToGturn) {
1266 TestStunToRelay(NAT_SYMMETRIC, RELAY_GTURN, PROTO_UDP);
1267}
1268
1269TEST_F(PortTest, TestSymNatToTcpGturn) {
1270 TestStunToRelay(NAT_SYMMETRIC, RELAY_GTURN, PROTO_TCP);
1271}
1272
1273// Outbound TCP -> XXXX
1274TEST_F(PortTest, TestTcpToTcp) {
1275 TestTcpToTcp();
1276}
1277
Guo-wei Shiehbe508a12015-04-06 12:48:47 -07001278TEST_F(PortTest, TestTcpReconnectOnSendPacket) {
1279 TestTcpReconnect(false /* ping */, true /* send */);
1280}
1281
1282TEST_F(PortTest, TestTcpReconnectOnPing) {
1283 TestTcpReconnect(true /* ping */, false /* send */);
1284}
1285
1286TEST_F(PortTest, TestTcpReconnectTimeout) {
1287 TestTcpReconnect(false /* ping */, false /* send */);
1288}
1289
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001290// Test when TcpConnection never connects, the OnClose() will be called to
1291// destroy the connection.
1292TEST_F(PortTest, TestTcpNeverConnect) {
Steve Anton11358fe2018-10-09 15:39:19 -07001293 auto port1 = CreateTcpPort(kLocalAddr1);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001294 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
1295 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
1296
1297 // Set up a channel and ensure the port will be deleted.
Steve Anton11358fe2018-10-09 15:39:19 -07001298 TestChannel ch1(std::move(port1));
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001299 EXPECT_EQ(0, ch1.complete_count());
1300
1301 ch1.Start();
Honghai Zhang161a5862016-10-20 11:47:02 -07001302 ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout);
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001303
kwiberg3ec46792016-04-27 07:22:53 -07001304 std::unique_ptr<rtc::AsyncSocket> server(
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001305 vss()->CreateAsyncSocket(kLocalAddr2.family(), SOCK_STREAM));
1306 // Bind but not listen.
1307 EXPECT_EQ(0, server->Bind(kLocalAddr2));
1308
Steve Anton11358fe2018-10-09 15:39:19 -07001309 Candidate c = GetCandidate(ch1.port());
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001310 c.set_address(server->GetLocalAddress());
1311
1312 ch1.CreateConnection(c);
1313 EXPECT_TRUE(ch1.conn());
Honghai Zhang161a5862016-10-20 11:47:02 -07001314 EXPECT_TRUE_WAIT(!ch1.conn(), kDefaultTimeout); // for TCP connect
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07001315}
1316
Steve Antonbabf9172017-11-29 10:19:02 -08001317/* TODO(?): Enable these once testrelayserver can accept external TCP.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001318TEST_F(PortTest, TestTcpToTcpRelay) {
1319 TestTcpToRelay(PROTO_TCP);
1320}
1321
1322TEST_F(PortTest, TestTcpToSslTcpRelay) {
1323 TestTcpToRelay(PROTO_SSLTCP);
1324}
1325*/
1326
1327// Outbound SSLTCP -> XXXX
Steve Antonbabf9172017-11-29 10:19:02 -08001328/* TODO(?): Enable these once testrelayserver can accept external SSL.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001329TEST_F(PortTest, TestSslTcpToTcpRelay) {
1330 TestSslTcpToRelay(PROTO_TCP);
1331}
1332
1333TEST_F(PortTest, TestSslTcpToSslTcpRelay) {
1334 TestSslTcpToRelay(PROTO_SSLTCP);
1335}
1336*/
1337
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001338// Test that a connection will be dead and deleted if
1339// i) it has never received anything for MIN_CONNECTION_LIFETIME milliseconds
1340// since it was created, or
1341// ii) it has not received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT
1342// milliseconds since last receiving.
1343TEST_F(PortTest, TestConnectionDead) {
Steve Anton11358fe2018-10-09 15:39:19 -07001344 TestChannel ch1(CreateUdpPort(kLocalAddr1));
1345 TestChannel ch2(CreateUdpPort(kLocalAddr2));
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001346 // Acquire address.
1347 ch1.Start();
1348 ch2.Start();
Honghai Zhang161a5862016-10-20 11:47:02 -07001349 ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout);
1350 ASSERT_EQ_WAIT(1, ch2.complete_count(), kDefaultTimeout);
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001351
honghaiz37389b42016-01-04 21:57:33 -08001352 // Test case that the connection has never received anything.
nisse1bffc1d2016-05-02 08:18:55 -07001353 int64_t before_created = rtc::TimeMillis();
Steve Anton11358fe2018-10-09 15:39:19 -07001354 ch1.CreateConnection(GetCandidate(ch2.port()));
nisse1bffc1d2016-05-02 08:18:55 -07001355 int64_t after_created = rtc::TimeMillis();
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001356 Connection* conn = ch1.conn();
nissec8ee8822017-01-18 07:20:55 -08001357 ASSERT_NE(conn, nullptr);
honghaiz37389b42016-01-04 21:57:33 -08001358 // It is not dead if it is after MIN_CONNECTION_LIFETIME but not pruned.
1359 conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1);
1360 rtc::Thread::Current()->ProcessMessages(0);
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001361 EXPECT_TRUE(ch1.conn() != nullptr);
honghaiz37389b42016-01-04 21:57:33 -08001362 // It is not dead if it is before MIN_CONNECTION_LIFETIME and pruned.
1363 conn->UpdateState(before_created + MIN_CONNECTION_LIFETIME - 1);
1364 conn->Prune();
1365 rtc::Thread::Current()->ProcessMessages(0);
1366 EXPECT_TRUE(ch1.conn() != nullptr);
1367 // It will be dead after MIN_CONNECTION_LIFETIME and pruned.
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001368 conn->UpdateState(after_created + MIN_CONNECTION_LIFETIME + 1);
Honghai Zhang161a5862016-10-20 11:47:02 -07001369 EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout);
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001370
honghaiz37389b42016-01-04 21:57:33 -08001371 // Test case that the connection has received something.
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001372 // Create a connection again and receive a ping.
Steve Anton11358fe2018-10-09 15:39:19 -07001373 ch1.CreateConnection(GetCandidate(ch2.port()));
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001374 conn = ch1.conn();
nissec8ee8822017-01-18 07:20:55 -08001375 ASSERT_NE(conn, nullptr);
nisse1bffc1d2016-05-02 08:18:55 -07001376 int64_t before_last_receiving = rtc::TimeMillis();
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001377 conn->ReceivedPing();
nisse1bffc1d2016-05-02 08:18:55 -07001378 int64_t after_last_receiving = rtc::TimeMillis();
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001379 // The connection will be dead after DEAD_CONNECTION_RECEIVE_TIMEOUT
Yves Gerey665174f2018-06-19 15:03:05 +02001380 conn->UpdateState(before_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT -
1381 1);
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001382 rtc::Thread::Current()->ProcessMessages(100);
1383 EXPECT_TRUE(ch1.conn() != nullptr);
1384 conn->UpdateState(after_last_receiving + DEAD_CONNECTION_RECEIVE_TIMEOUT + 1);
Honghai Zhang161a5862016-10-20 11:47:02 -07001385 EXPECT_TRUE_WAIT(ch1.conn() == nullptr, kDefaultTimeout);
Honghai Zhang2cd7afe2015-11-12 11:14:33 -08001386}
1387
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001388// This test case verifies standard ICE features in STUN messages. Currently it
1389// verifies Message Integrity attribute in STUN messages and username in STUN
1390// binding request will have colon (":") between remote and local username.
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001391TEST_F(PortTest, TestLocalToLocalStandard) {
Steve Anton11358fe2018-10-09 15:39:19 -07001392 auto port1 = CreateUdpPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001393 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
1394 port1->SetIceTiebreaker(kTiebreaker1);
Steve Anton11358fe2018-10-09 15:39:19 -07001395 auto port2 = CreateUdpPort(kLocalAddr2);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001396 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
1397 port2->SetIceTiebreaker(kTiebreaker2);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001398 // Same parameters as TestLocalToLocal above.
Steve Anton11358fe2018-10-09 15:39:19 -07001399 TestConnectivity("udp", std::move(port1), "udp", std::move(port2), true, true,
1400 true, true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001401}
1402
1403// This test is trying to validate a successful and failure scenario in a
1404// loopback test when protocol is RFC5245. For success IceTiebreaker, username
1405// should remain equal to the request generated by the port and role of port
1406// must be in controlling.
Honghai Zhang161a5862016-10-20 11:47:02 -07001407TEST_F(PortTest, TestLoopbackCall) {
Steve Anton11358fe2018-10-09 15:39:19 -07001408 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001409 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1410 lport->SetIceTiebreaker(kTiebreaker1);
1411 lport->PrepareAddress();
1412 ASSERT_FALSE(lport->Candidates().empty());
Yves Gerey665174f2018-06-19 15:03:05 +02001413 Connection* conn =
1414 lport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001415 conn->Ping(0);
1416
Honghai Zhang161a5862016-10-20 11:47:02 -07001417 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001418 IceMessage* msg = lport->last_stun_msg();
1419 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
jbauchf1f87202016-03-30 06:43:37 -07001420 conn->OnReadPacket(lport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001421 lport->last_stun_buf()->size(), /* packet_time_us */ -1);
Honghai Zhang161a5862016-10-20 11:47:02 -07001422 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001423 msg = lport->last_stun_msg();
1424 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
1425
1426 // If the tiebreaker value is different from port, we expect a error
1427 // response.
1428 lport->Reset();
1429 lport->AddCandidateAddress(kLocalAddr2);
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001430 // Creating a different connection as |conn| is receiving.
Yves Gerey665174f2018-06-19 15:03:05 +02001431 Connection* conn1 =
1432 lport->CreateConnection(lport->Candidates()[1], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001433 conn1->Ping(0);
1434
Honghai Zhang161a5862016-10-20 11:47:02 -07001435 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001436 msg = lport->last_stun_msg();
1437 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
kwiberg3ec46792016-04-27 07:22:53 -07001438 std::unique_ptr<IceMessage> modified_req(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001439 CreateStunMessage(STUN_BINDING_REQUEST));
Yves Gerey665174f2018-06-19 15:03:05 +02001440 const StunByteStringAttribute* username_attr =
1441 msg->GetByteString(STUN_ATTR_USERNAME);
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001442 modified_req->AddAttribute(std::make_unique<StunByteStringAttribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001443 STUN_ATTR_USERNAME, username_attr->GetString()));
1444 // To make sure we receive error response, adding tiebreaker less than
1445 // what's present in request.
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001446 modified_req->AddAttribute(std::make_unique<StunUInt64Attribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001447 STUN_ATTR_ICE_CONTROLLING, kTiebreaker1 - 1));
1448 modified_req->AddMessageIntegrity("lpass");
1449 modified_req->AddFingerprint();
1450
1451 lport->Reset();
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001452 auto buf = std::make_unique<ByteBufferWriter>();
Steve Anton11358fe2018-10-09 15:39:19 -07001453 WriteStunMessage(*modified_req, buf.get());
Niels Möllere6933812018-11-05 13:01:41 +01001454 conn1->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1);
Honghai Zhang161a5862016-10-20 11:47:02 -07001455 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001456 msg = lport->last_stun_msg();
1457 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1458}
1459
1460// This test verifies role conflict signal is received when there is
1461// conflict in the role. In this case both ports are in controlling and
1462// |rport| has higher tiebreaker value than |lport|. Since |lport| has lower
1463// value of tiebreaker, when it receives ping request from |rport| it will
1464// send role conflict signal.
1465TEST_F(PortTest, TestIceRoleConflict) {
Steve Anton11358fe2018-10-09 15:39:19 -07001466 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001467 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1468 lport->SetIceTiebreaker(kTiebreaker1);
Steve Anton11358fe2018-10-09 15:39:19 -07001469 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470 rport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1471 rport->SetIceTiebreaker(kTiebreaker2);
1472
1473 lport->PrepareAddress();
1474 rport->PrepareAddress();
1475 ASSERT_FALSE(lport->Candidates().empty());
1476 ASSERT_FALSE(rport->Candidates().empty());
Yves Gerey665174f2018-06-19 15:03:05 +02001477 Connection* lconn =
1478 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1479 Connection* rconn =
1480 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001481 rconn->Ping(0);
1482
Honghai Zhang161a5862016-10-20 11:47:02 -07001483 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001484 IceMessage* msg = rport->last_stun_msg();
1485 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1486 // Send rport binding request to lport.
jbauchf1f87202016-03-30 06:43:37 -07001487 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001488 rport->last_stun_buf()->size(), /* packet_time_us */ -1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001489
Honghai Zhang161a5862016-10-20 11:47:02 -07001490 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001491 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type());
1492 EXPECT_TRUE(role_conflict());
1493}
1494
1495TEST_F(PortTest, TestTcpNoDelay) {
Steve Anton11358fe2018-10-09 15:39:19 -07001496 auto port1 = CreateTcpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001497 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001498 int option_value = -1;
Yves Gerey665174f2018-06-19 15:03:05 +02001499 int success = port1->GetOption(rtc::Socket::OPT_NODELAY, &option_value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001500 ASSERT_EQ(0, success); // GetOption() should complete successfully w/ 0
1501 ASSERT_EQ(1, option_value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001502}
1503
1504TEST_F(PortTest, TestDelayedBindingUdp) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001505 FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001506 FakePacketSocketFactory socket_factory;
1507
1508 socket_factory.set_next_udp_socket(socket);
Steve Anton11358fe2018-10-09 15:39:19 -07001509 auto port = CreateUdpPort(kLocalAddr1, &socket_factory);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001510
1511 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1512 port->PrepareAddress();
1513
1514 EXPECT_EQ(0U, port->Candidates().size());
1515 socket->SignalAddressReady(socket, kLocalAddr2);
1516
1517 EXPECT_EQ(1U, port->Candidates().size());
1518}
1519
1520TEST_F(PortTest, TestDelayedBindingTcp) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001521 FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001522 FakePacketSocketFactory socket_factory;
1523
1524 socket_factory.set_next_server_tcp_socket(socket);
Steve Anton11358fe2018-10-09 15:39:19 -07001525 auto port = CreateTcpPort(kLocalAddr1, &socket_factory);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001526
1527 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1528 port->PrepareAddress();
1529
1530 EXPECT_EQ(0U, port->Candidates().size());
1531 socket->SignalAddressReady(socket, kLocalAddr2);
1532
1533 EXPECT_EQ(1U, port->Candidates().size());
1534}
1535
1536void PortTest::TestCrossFamilyPorts(int type) {
1537 FakePacketSocketFactory factory;
kwiberg3ec46792016-04-27 07:22:53 -07001538 std::unique_ptr<Port> ports[4];
Yves Gerey665174f2018-06-19 15:03:05 +02001539 SocketAddress addresses[4] = {
1540 SocketAddress("192.168.1.3", 0), SocketAddress("192.168.1.4", 0),
1541 SocketAddress("2001:db8::1", 0), SocketAddress("2001:db8::2", 0)};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001542 for (int i = 0; i < 4; i++) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001543 FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001544 if (type == SOCK_DGRAM) {
1545 factory.set_next_udp_socket(socket);
Steve Anton11358fe2018-10-09 15:39:19 -07001546 ports[i] = CreateUdpPort(addresses[i], &factory);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001547 } else if (type == SOCK_STREAM) {
1548 factory.set_next_server_tcp_socket(socket);
Steve Anton11358fe2018-10-09 15:39:19 -07001549 ports[i] = CreateTcpPort(addresses[i], &factory);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001550 }
1551 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1552 socket->SignalAddressReady(socket, addresses[i]);
1553 ports[i]->PrepareAddress();
1554 }
1555
1556 // IPv4 Port, connects to IPv6 candidate and then to IPv4 candidate.
1557 if (type == SOCK_STREAM) {
1558 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket();
1559 factory.set_next_client_tcp_socket(clientsocket);
1560 }
1561 Connection* c = ports[0]->CreateConnection(GetCandidate(ports[2].get()),
1562 Port::ORIGIN_MESSAGE);
1563 EXPECT_TRUE(NULL == c);
1564 EXPECT_EQ(0U, ports[0]->connections().size());
1565 c = ports[0]->CreateConnection(GetCandidate(ports[1].get()),
1566 Port::ORIGIN_MESSAGE);
1567 EXPECT_FALSE(NULL == c);
1568 EXPECT_EQ(1U, ports[0]->connections().size());
1569
1570 // IPv6 Port, connects to IPv4 candidate and to IPv6 candidate.
1571 if (type == SOCK_STREAM) {
1572 FakeAsyncPacketSocket* clientsocket = new FakeAsyncPacketSocket();
1573 factory.set_next_client_tcp_socket(clientsocket);
1574 }
1575 c = ports[2]->CreateConnection(GetCandidate(ports[0].get()),
1576 Port::ORIGIN_MESSAGE);
1577 EXPECT_TRUE(NULL == c);
1578 EXPECT_EQ(0U, ports[2]->connections().size());
1579 c = ports[2]->CreateConnection(GetCandidate(ports[3].get()),
1580 Port::ORIGIN_MESSAGE);
1581 EXPECT_FALSE(NULL == c);
1582 EXPECT_EQ(1U, ports[2]->connections().size());
1583}
1584
1585TEST_F(PortTest, TestSkipCrossFamilyTcp) {
1586 TestCrossFamilyPorts(SOCK_STREAM);
1587}
1588
1589TEST_F(PortTest, TestSkipCrossFamilyUdp) {
1590 TestCrossFamilyPorts(SOCK_DGRAM);
1591}
1592
Peter Thatcherb8b01432015-07-07 16:45:53 -07001593void PortTest::ExpectPortsCanConnect(bool can_connect, Port* p1, Port* p2) {
Yves Gerey665174f2018-06-19 15:03:05 +02001594 Connection* c = p1->CreateConnection(GetCandidate(p2), Port::ORIGIN_MESSAGE);
Peter Thatcherb8b01432015-07-07 16:45:53 -07001595 if (can_connect) {
1596 EXPECT_FALSE(NULL == c);
1597 EXPECT_EQ(1U, p1->connections().size());
1598 } else {
1599 EXPECT_TRUE(NULL == c);
1600 EXPECT_EQ(0U, p1->connections().size());
1601 }
1602}
1603
1604TEST_F(PortTest, TestUdpV6CrossTypePorts) {
1605 FakePacketSocketFactory factory;
kwiberg3ec46792016-04-27 07:22:53 -07001606 std::unique_ptr<Port> ports[4];
Yves Gerey665174f2018-06-19 15:03:05 +02001607 SocketAddress addresses[4] = {
1608 SocketAddress("2001:db8::1", 0), SocketAddress("fe80::1", 0),
1609 SocketAddress("fe80::2", 0), SocketAddress("::1", 0)};
Peter Thatcherb8b01432015-07-07 16:45:53 -07001610 for (int i = 0; i < 4; i++) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001611 FakeAsyncPacketSocket* socket = new FakeAsyncPacketSocket();
Peter Thatcherb8b01432015-07-07 16:45:53 -07001612 factory.set_next_udp_socket(socket);
Steve Anton11358fe2018-10-09 15:39:19 -07001613 ports[i] = CreateUdpPort(addresses[i], &factory);
Peter Thatcherb8b01432015-07-07 16:45:53 -07001614 socket->set_state(AsyncPacketSocket::STATE_BINDING);
1615 socket->SignalAddressReady(socket, addresses[i]);
1616 ports[i]->PrepareAddress();
1617 }
1618
1619 Port* standard = ports[0].get();
1620 Port* link_local1 = ports[1].get();
1621 Port* link_local2 = ports[2].get();
1622 Port* localhost = ports[3].get();
1623
1624 ExpectPortsCanConnect(false, link_local1, standard);
1625 ExpectPortsCanConnect(false, standard, link_local1);
1626 ExpectPortsCanConnect(false, link_local1, localhost);
1627 ExpectPortsCanConnect(false, localhost, link_local1);
1628
1629 ExpectPortsCanConnect(true, link_local1, link_local2);
1630 ExpectPortsCanConnect(true, localhost, standard);
1631 ExpectPortsCanConnect(true, standard, localhost);
1632}
1633
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001634// This test verifies DSCP value set through SetOption interface can be
1635// get through DefaultDscpValue.
1636TEST_F(PortTest, TestDefaultDscpValue) {
1637 int dscp;
Steve Anton11358fe2018-10-09 15:39:19 -07001638 auto udpport = CreateUdpPort(kLocalAddr1);
Yves Gerey665174f2018-06-19 15:03:05 +02001639 EXPECT_EQ(0, udpport->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS6));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001640 EXPECT_EQ(0, udpport->GetOption(rtc::Socket::OPT_DSCP, &dscp));
Steve Anton11358fe2018-10-09 15:39:19 -07001641 auto tcpport = CreateTcpPort(kLocalAddr1);
Yves Gerey665174f2018-06-19 15:03:05 +02001642 EXPECT_EQ(0, tcpport->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF31));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001643 EXPECT_EQ(0, tcpport->GetOption(rtc::Socket::OPT_DSCP, &dscp));
1644 EXPECT_EQ(rtc::DSCP_AF31, dscp);
Steve Anton11358fe2018-10-09 15:39:19 -07001645 auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1());
Yves Gerey665174f2018-06-19 15:03:05 +02001646 EXPECT_EQ(0, stunport->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_AF41));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001647 EXPECT_EQ(0, stunport->GetOption(rtc::Socket::OPT_DSCP, &dscp));
1648 EXPECT_EQ(rtc::DSCP_AF41, dscp);
Steve Anton11358fe2018-10-09 15:39:19 -07001649 auto turnport1 =
1650 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001651 // Socket is created in PrepareAddress.
1652 turnport1->PrepareAddress();
Yves Gerey665174f2018-06-19 15:03:05 +02001653 EXPECT_EQ(0, turnport1->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS7));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001654 EXPECT_EQ(0, turnport1->GetOption(rtc::Socket::OPT_DSCP, &dscp));
1655 EXPECT_EQ(rtc::DSCP_CS7, dscp);
1656 // This will verify correct value returned without the socket.
Steve Anton11358fe2018-10-09 15:39:19 -07001657 auto turnport2 =
1658 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP);
Yves Gerey665174f2018-06-19 15:03:05 +02001659 EXPECT_EQ(0, turnport2->SetOption(rtc::Socket::OPT_DSCP, rtc::DSCP_CS6));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001660 EXPECT_EQ(0, turnport2->GetOption(rtc::Socket::OPT_DSCP, &dscp));
1661 EXPECT_EQ(rtc::DSCP_CS6, dscp);
1662}
1663
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001664// Test sending STUN messages.
1665TEST_F(PortTest, TestSendStunMessage) {
Steve Anton11358fe2018-10-09 15:39:19 -07001666 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
1667 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001668 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1669 lport->SetIceTiebreaker(kTiebreaker1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001670 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1671 rport->SetIceTiebreaker(kTiebreaker2);
1672
1673 // Send a fake ping from lport to rport.
1674 lport->PrepareAddress();
1675 rport->PrepareAddress();
1676 ASSERT_FALSE(rport->Candidates().empty());
Yves Gerey665174f2018-06-19 15:03:05 +02001677 Connection* lconn =
1678 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1679 Connection* rconn =
1680 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001681 lconn->Ping(0);
1682
1683 // Check that it's a proper BINDING-REQUEST.
Honghai Zhang161a5862016-10-20 11:47:02 -07001684 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001685 IceMessage* msg = lport->last_stun_msg();
1686 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1687 EXPECT_FALSE(msg->IsLegacy());
1688 const StunByteStringAttribute* username_attr =
1689 msg->GetByteString(STUN_ATTR_USERNAME);
1690 ASSERT_TRUE(username_attr != NULL);
1691 const StunUInt32Attribute* priority_attr = msg->GetUInt32(STUN_ATTR_PRIORITY);
1692 ASSERT_TRUE(priority_attr != NULL);
1693 EXPECT_EQ(kDefaultPrflxPriority, priority_attr->value());
1694 EXPECT_EQ("rfrag:lfrag", username_attr->GetString());
1695 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL);
1696 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
jbauchf1f87202016-03-30 06:43:37 -07001697 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size(),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001698 "rpass"));
1699 const StunUInt64Attribute* ice_controlling_attr =
1700 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
1701 ASSERT_TRUE(ice_controlling_attr != NULL);
1702 EXPECT_EQ(lport->IceTiebreaker(), ice_controlling_attr->value());
1703 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL);
1704 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL);
1705 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL);
1706 EXPECT_TRUE(StunMessage::ValidateFingerprint(
jbauchf1f87202016-03-30 06:43:37 -07001707 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001708
1709 // Request should not include ping count.
1710 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL);
1711
1712 // Save a copy of the BINDING-REQUEST for use below.
Steve Anton11358fe2018-10-09 15:39:19 -07001713 std::unique_ptr<IceMessage> request = CopyStunMessage(*msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001714
zhihuang5ecf16c2016-06-01 17:09:15 -07001715 // Receive the BINDING-REQUEST and respond with BINDING-RESPONSE.
1716 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001717 lport->last_stun_buf()->size(), /* packet_time_us */ -1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001718 msg = rport->last_stun_msg();
1719 ASSERT_TRUE(msg != NULL);
1720 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
zhihuang5ecf16c2016-06-01 17:09:15 -07001721 // Received a BINDING-RESPONSE.
1722 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001723 rport->last_stun_buf()->size(), /* packet_time_us */ -1);
zhihuang5ecf16c2016-06-01 17:09:15 -07001724 // Verify the STUN Stats.
1725 EXPECT_EQ(1U, lconn->stats().sent_ping_requests_total);
1726 EXPECT_EQ(1U, lconn->stats().sent_ping_requests_before_first_response);
1727 EXPECT_EQ(1U, lconn->stats().recv_ping_responses);
1728 EXPECT_EQ(1U, rconn->stats().recv_ping_requests);
1729 EXPECT_EQ(1U, rconn->stats().sent_ping_responses);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001730
1731 EXPECT_FALSE(msg->IsLegacy());
Yves Gerey665174f2018-06-19 15:03:05 +02001732 const StunAddressAttribute* addr_attr =
1733 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001734 ASSERT_TRUE(addr_attr != NULL);
1735 EXPECT_EQ(lport->Candidates()[0].address(), addr_attr->GetAddress());
1736 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL);
1737 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
jbauchf1f87202016-03-30 06:43:37 -07001738 rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001739 "rpass"));
1740 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL);
1741 EXPECT_TRUE(StunMessage::ValidateFingerprint(
jbauchf1f87202016-03-30 06:43:37 -07001742 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001743 // No USERNAME or PRIORITY in ICE responses.
1744 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL);
1745 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1746 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MAPPED_ADDRESS) == NULL);
1747 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLING) == NULL);
1748 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_ICE_CONTROLLED) == NULL);
1749 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL);
1750
1751 // Response should not include ping count.
1752 ASSERT_TRUE(msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT) == NULL);
1753
1754 // Respond with a BINDING-ERROR-RESPONSE. This wouldn't happen in real life,
1755 // but we can do it here.
Yves Gerey665174f2018-06-19 15:03:05 +02001756 rport->SendBindingErrorResponse(
1757 request.get(), lport->Candidates()[0].address(), STUN_ERROR_SERVER_ERROR,
1758 STUN_ERROR_REASON_SERVER_ERROR);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001759 msg = rport->last_stun_msg();
1760 ASSERT_TRUE(msg != NULL);
1761 EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
1762 EXPECT_FALSE(msg->IsLegacy());
1763 const StunErrorCodeAttribute* error_attr = msg->GetErrorCode();
1764 ASSERT_TRUE(error_attr != NULL);
1765 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, error_attr->code());
1766 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR), error_attr->reason());
1767 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY) != NULL);
1768 EXPECT_TRUE(StunMessage::ValidateMessageIntegrity(
jbauchf1f87202016-03-30 06:43:37 -07001769 rport->last_stun_buf()->data<char>(), rport->last_stun_buf()->size(),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001770 "rpass"));
1771 EXPECT_TRUE(msg->GetUInt32(STUN_ATTR_FINGERPRINT) != NULL);
1772 EXPECT_TRUE(StunMessage::ValidateFingerprint(
jbauchf1f87202016-03-30 06:43:37 -07001773 lport->last_stun_buf()->data<char>(), lport->last_stun_buf()->size()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001774 // No USERNAME with ICE.
1775 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USERNAME) == NULL);
1776 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_PRIORITY) == NULL);
1777
1778 // Testing STUN binding requests from rport --> lport, having ICE_CONTROLLED
1779 // and (incremented) RETRANSMIT_COUNT attributes.
1780 rport->Reset();
1781 rport->set_send_retransmit_count_attribute(true);
1782 rconn->Ping(0);
1783 rconn->Ping(0);
1784 rconn->Ping(0);
Honghai Zhang161a5862016-10-20 11:47:02 -07001785 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001786 msg = rport->last_stun_msg();
1787 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1788 const StunUInt64Attribute* ice_controlled_attr =
1789 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED);
1790 ASSERT_TRUE(ice_controlled_attr != NULL);
1791 EXPECT_EQ(rport->IceTiebreaker(), ice_controlled_attr->value());
1792 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL);
1793
1794 // Request should include ping count.
1795 const StunUInt32Attribute* retransmit_attr =
1796 msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
1797 ASSERT_TRUE(retransmit_attr != NULL);
1798 EXPECT_EQ(2U, retransmit_attr->value());
1799
1800 // Respond with a BINDING-RESPONSE.
Steve Anton11358fe2018-10-09 15:39:19 -07001801 request = CopyStunMessage(*msg);
zhihuang5ecf16c2016-06-01 17:09:15 -07001802 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001803 rport->last_stun_buf()->size(), /* packet_time_us */ -1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001804 msg = lport->last_stun_msg();
zhihuang5ecf16c2016-06-01 17:09:15 -07001805 // Receive the BINDING-RESPONSE.
1806 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001807 lport->last_stun_buf()->size(), /* packet_time_us */ -1);
zhihuang5ecf16c2016-06-01 17:09:15 -07001808
1809 // Verify the Stun ping stats.
1810 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_total);
1811 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response);
1812 EXPECT_EQ(1U, rconn->stats().recv_ping_responses);
1813 EXPECT_EQ(1U, lconn->stats().sent_ping_responses);
1814 EXPECT_EQ(1U, lconn->stats().recv_ping_requests);
1815 // Ping after receiver the first response
1816 rconn->Ping(0);
1817 rconn->Ping(0);
1818 EXPECT_EQ(5U, rconn->stats().sent_ping_requests_total);
1819 EXPECT_EQ(3U, rconn->stats().sent_ping_requests_before_first_response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001820
1821 // Response should include same ping count.
1822 retransmit_attr = msg->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
1823 ASSERT_TRUE(retransmit_attr != NULL);
1824 EXPECT_EQ(2U, retransmit_attr->value());
1825}
1826
hbos92eaec62017-02-27 01:38:08 -08001827TEST_F(PortTest, TestNomination) {
Steve Anton11358fe2018-10-09 15:39:19 -07001828 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
1829 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
hbos92eaec62017-02-27 01:38:08 -08001830 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1831 lport->SetIceTiebreaker(kTiebreaker1);
1832 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1833 rport->SetIceTiebreaker(kTiebreaker2);
1834
1835 lport->PrepareAddress();
1836 rport->PrepareAddress();
1837 ASSERT_FALSE(lport->Candidates().empty());
1838 ASSERT_FALSE(rport->Candidates().empty());
Yves Gerey665174f2018-06-19 15:03:05 +02001839 Connection* lconn =
1840 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1841 Connection* rconn =
1842 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
hbos92eaec62017-02-27 01:38:08 -08001843
1844 // |lconn| is controlling, |rconn| is controlled.
1845 uint32_t nomination = 1234;
1846 lconn->set_nomination(nomination);
1847
1848 EXPECT_FALSE(lconn->nominated());
1849 EXPECT_FALSE(rconn->nominated());
1850 EXPECT_EQ(lconn->nominated(), lconn->stats().nominated);
1851 EXPECT_EQ(rconn->nominated(), rconn->stats().nominated);
1852
1853 // Send ping (including the nomination value) from |lconn| to |rconn|. This
1854 // should set the remote nomination of |rconn|.
1855 lconn->Ping(0);
1856 ASSERT_TRUE_WAIT(lport->last_stun_msg(), kDefaultTimeout);
1857 ASSERT_TRUE(lport->last_stun_buf());
1858 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001859 lport->last_stun_buf()->size(), /* packet_time_us */ -1);
hbos92eaec62017-02-27 01:38:08 -08001860 EXPECT_EQ(nomination, rconn->remote_nomination());
1861 EXPECT_FALSE(lconn->nominated());
1862 EXPECT_TRUE(rconn->nominated());
1863 EXPECT_EQ(lconn->nominated(), lconn->stats().nominated);
1864 EXPECT_EQ(rconn->nominated(), rconn->stats().nominated);
1865
1866 // This should result in an acknowledgment sent back from |rconn| to |lconn|,
1867 // updating the acknowledged nomination of |lconn|.
1868 ASSERT_TRUE_WAIT(rport->last_stun_msg(), kDefaultTimeout);
1869 ASSERT_TRUE(rport->last_stun_buf());
1870 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001871 rport->last_stun_buf()->size(), /* packet_time_us */ -1);
hbos92eaec62017-02-27 01:38:08 -08001872 EXPECT_EQ(nomination, lconn->acked_nomination());
1873 EXPECT_TRUE(lconn->nominated());
1874 EXPECT_TRUE(rconn->nominated());
1875 EXPECT_EQ(lconn->nominated(), lconn->stats().nominated);
1876 EXPECT_EQ(rconn->nominated(), rconn->stats().nominated);
1877}
1878
hbosbf8d3e52017-02-28 06:34:47 -08001879TEST_F(PortTest, TestRoundTripTime) {
1880 rtc::ScopedFakeClock clock;
1881
Steve Anton11358fe2018-10-09 15:39:19 -07001882 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
1883 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
hbosbf8d3e52017-02-28 06:34:47 -08001884 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1885 lport->SetIceTiebreaker(kTiebreaker1);
1886 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1887 rport->SetIceTiebreaker(kTiebreaker2);
1888
1889 lport->PrepareAddress();
1890 rport->PrepareAddress();
1891 ASSERT_FALSE(lport->Candidates().empty());
1892 ASSERT_FALSE(rport->Candidates().empty());
Yves Gerey665174f2018-06-19 15:03:05 +02001893 Connection* lconn =
1894 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1895 Connection* rconn =
1896 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
hbosbf8d3e52017-02-28 06:34:47 -08001897
1898 EXPECT_EQ(0u, lconn->stats().total_round_trip_time_ms);
1899 EXPECT_FALSE(lconn->stats().current_round_trip_time_ms);
1900
Yves Gerey665174f2018-06-19 15:03:05 +02001901 SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock,
1902 10);
hbosbf8d3e52017-02-28 06:34:47 -08001903 EXPECT_EQ(10u, lconn->stats().total_round_trip_time_ms);
1904 ASSERT_TRUE(lconn->stats().current_round_trip_time_ms);
1905 EXPECT_EQ(10u, *lconn->stats().current_round_trip_time_ms);
1906
Yves Gerey665174f2018-06-19 15:03:05 +02001907 SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock,
1908 20);
hbosbf8d3e52017-02-28 06:34:47 -08001909 EXPECT_EQ(30u, lconn->stats().total_round_trip_time_ms);
1910 ASSERT_TRUE(lconn->stats().current_round_trip_time_ms);
1911 EXPECT_EQ(20u, *lconn->stats().current_round_trip_time_ms);
1912
Yves Gerey665174f2018-06-19 15:03:05 +02001913 SendPingAndReceiveResponse(lconn, lport.get(), rconn, rport.get(), &clock,
1914 30);
hbosbf8d3e52017-02-28 06:34:47 -08001915 EXPECT_EQ(60u, lconn->stats().total_round_trip_time_ms);
1916 ASSERT_TRUE(lconn->stats().current_round_trip_time_ms);
1917 EXPECT_EQ(30u, *lconn->stats().current_round_trip_time_ms);
1918}
1919
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001920TEST_F(PortTest, TestUseCandidateAttribute) {
Steve Anton11358fe2018-10-09 15:39:19 -07001921 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
1922 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001923 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1924 lport->SetIceTiebreaker(kTiebreaker1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001925 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1926 rport->SetIceTiebreaker(kTiebreaker2);
1927
1928 // Send a fake ping from lport to rport.
1929 lport->PrepareAddress();
1930 rport->PrepareAddress();
1931 ASSERT_FALSE(rport->Candidates().empty());
Yves Gerey665174f2018-06-19 15:03:05 +02001932 Connection* lconn =
1933 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001934 lconn->Ping(0);
Honghai Zhang161a5862016-10-20 11:47:02 -07001935 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001936 IceMessage* msg = lport->last_stun_msg();
1937 const StunUInt64Attribute* ice_controlling_attr =
1938 msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
1939 ASSERT_TRUE(ice_controlling_attr != NULL);
Yves Gerey665174f2018-06-19 15:03:05 +02001940 const StunByteStringAttribute* use_candidate_attr =
1941 msg->GetByteString(STUN_ATTR_USE_CANDIDATE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001942 ASSERT_TRUE(use_candidate_attr != NULL);
1943}
1944
Honghai Zhang351d77b2016-05-20 15:08:29 -07001945// Tests that when the network type changes, the network cost of the port will
1946// change, the network cost of the local candidates will change. Also tests that
1947// the remote network costs are updated with the stun binding requests.
1948TEST_F(PortTest, TestNetworkCostChange) {
deadbeef5c3c1042017-08-04 15:01:57 -07001949 rtc::Network* test_network = MakeNetwork(kLocalAddr1);
Steve Anton11358fe2018-10-09 15:39:19 -07001950 auto lport = CreateTestPort(test_network, "lfrag", "lpass");
1951 auto rport = CreateTestPort(test_network, "rfrag", "rpass");
Honghai Zhang351d77b2016-05-20 15:08:29 -07001952 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
1953 lport->SetIceTiebreaker(kTiebreaker1);
1954 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
1955 rport->SetIceTiebreaker(kTiebreaker2);
1956 lport->PrepareAddress();
1957 rport->PrepareAddress();
1958
1959 // Default local port cost is rtc::kNetworkCostUnknown.
1960 EXPECT_EQ(rtc::kNetworkCostUnknown, lport->network_cost());
1961 ASSERT_TRUE(!lport->Candidates().empty());
1962 for (const cricket::Candidate& candidate : lport->Candidates()) {
1963 EXPECT_EQ(rtc::kNetworkCostUnknown, candidate.network_cost());
1964 }
1965
1966 // Change the network type to wifi.
deadbeef5c3c1042017-08-04 15:01:57 -07001967 test_network->set_type(rtc::ADAPTER_TYPE_WIFI);
Honghai Zhang351d77b2016-05-20 15:08:29 -07001968 EXPECT_EQ(rtc::kNetworkCostLow, lport->network_cost());
1969 for (const cricket::Candidate& candidate : lport->Candidates()) {
1970 EXPECT_EQ(rtc::kNetworkCostLow, candidate.network_cost());
1971 }
1972
1973 // Add a connection and then change the network type.
1974 Connection* lconn =
1975 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
1976 // Change the network type to cellular.
deadbeef5c3c1042017-08-04 15:01:57 -07001977 test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR);
Honghai Zhang351d77b2016-05-20 15:08:29 -07001978 EXPECT_EQ(rtc::kNetworkCostHigh, lport->network_cost());
1979 for (const cricket::Candidate& candidate : lport->Candidates()) {
1980 EXPECT_EQ(rtc::kNetworkCostHigh, candidate.network_cost());
1981 }
1982
deadbeef5c3c1042017-08-04 15:01:57 -07001983 test_network->set_type(rtc::ADAPTER_TYPE_WIFI);
Honghai Zhang351d77b2016-05-20 15:08:29 -07001984 Connection* rconn =
1985 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
deadbeef5c3c1042017-08-04 15:01:57 -07001986 test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR);
Honghai Zhang351d77b2016-05-20 15:08:29 -07001987 lconn->Ping(0);
1988 // The rconn's remote candidate cost is rtc::kNetworkCostLow, but the ping
1989 // contains an attribute of network cost of rtc::kNetworkCostHigh. Once the
1990 // message is handled in rconn, The rconn's remote candidate will have cost
1991 // rtc::kNetworkCostHigh;
1992 EXPECT_EQ(rtc::kNetworkCostLow, rconn->remote_candidate().network_cost());
Honghai Zhang161a5862016-10-20 11:47:02 -07001993 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
Honghai Zhang351d77b2016-05-20 15:08:29 -07001994 IceMessage* msg = lport->last_stun_msg();
1995 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
1996 // Pass the binding request to rport.
1997 rconn->OnReadPacket(lport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01001998 lport->last_stun_buf()->size(), /* packet_time_us */ -1);
Honghai Zhang351d77b2016-05-20 15:08:29 -07001999 // Wait until rport sends the response and then check the remote network cost.
Honghai Zhang161a5862016-10-20 11:47:02 -07002000 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout);
Honghai Zhang351d77b2016-05-20 15:08:29 -07002001 EXPECT_EQ(rtc::kNetworkCostHigh, rconn->remote_candidate().network_cost());
2002}
2003
honghaiza0c44ea2016-03-23 16:07:48 -07002004TEST_F(PortTest, TestNetworkInfoAttribute) {
deadbeef5c3c1042017-08-04 15:01:57 -07002005 rtc::Network* test_network = MakeNetwork(kLocalAddr1);
Steve Anton11358fe2018-10-09 15:39:19 -07002006 auto lport = CreateTestPort(test_network, "lfrag", "lpass");
2007 auto rport = CreateTestPort(test_network, "rfrag", "rpass");
honghaiza0c44ea2016-03-23 16:07:48 -07002008 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
2009 lport->SetIceTiebreaker(kTiebreaker1);
2010 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2011 rport->SetIceTiebreaker(kTiebreaker2);
2012
2013 uint16_t lnetwork_id = 9;
2014 lport->Network()->set_id(lnetwork_id);
2015 // Send a fake ping from lport to rport.
2016 lport->PrepareAddress();
2017 rport->PrepareAddress();
2018 Connection* lconn =
2019 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
2020 lconn->Ping(0);
Honghai Zhang161a5862016-10-20 11:47:02 -07002021 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
honghaiza0c44ea2016-03-23 16:07:48 -07002022 IceMessage* msg = lport->last_stun_msg();
2023 const StunUInt32Attribute* network_info_attr =
2024 msg->GetUInt32(STUN_ATTR_NETWORK_INFO);
2025 ASSERT_TRUE(network_info_attr != NULL);
2026 uint32_t network_info = network_info_attr->value();
2027 EXPECT_EQ(lnetwork_id, network_info >> 16);
Honghai Zhang351d77b2016-05-20 15:08:29 -07002028 // Default network has unknown type and cost kNetworkCostUnknown.
2029 EXPECT_EQ(rtc::kNetworkCostUnknown, network_info & 0xFFFF);
honghaiza0c44ea2016-03-23 16:07:48 -07002030
Honghai Zhang351d77b2016-05-20 15:08:29 -07002031 // Set the network type to be cellular so its cost will be kNetworkCostHigh.
honghaiza0c44ea2016-03-23 16:07:48 -07002032 // Send a fake ping from rport to lport.
deadbeef5c3c1042017-08-04 15:01:57 -07002033 test_network->set_type(rtc::ADAPTER_TYPE_CELLULAR);
honghaiza0c44ea2016-03-23 16:07:48 -07002034 uint16_t rnetwork_id = 8;
2035 rport->Network()->set_id(rnetwork_id);
2036 Connection* rconn =
2037 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
2038 rconn->Ping(0);
Honghai Zhang161a5862016-10-20 11:47:02 -07002039 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout);
honghaiza0c44ea2016-03-23 16:07:48 -07002040 msg = rport->last_stun_msg();
2041 network_info_attr = msg->GetUInt32(STUN_ATTR_NETWORK_INFO);
2042 ASSERT_TRUE(network_info_attr != NULL);
2043 network_info = network_info_attr->value();
2044 EXPECT_EQ(rnetwork_id, network_info >> 16);
Honghai Zhang351d77b2016-05-20 15:08:29 -07002045 EXPECT_EQ(rtc::kNetworkCostHigh, network_info & 0xFFFF);
honghaiza0c44ea2016-03-23 16:07:48 -07002046}
2047
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002048// Test handling STUN messages.
2049TEST_F(PortTest, TestHandleStunMessage) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002050 // Our port will act as the "remote" port.
Steve Anton11358fe2018-10-09 15:39:19 -07002051 auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002052
kwiberg3ec46792016-04-27 07:22:53 -07002053 std::unique_ptr<IceMessage> in_msg, out_msg;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002054 auto buf = std::make_unique<ByteBufferWriter>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002055 rtc::SocketAddress addr(kLocalAddr1);
2056 std::string username;
2057
2058 // BINDING-REQUEST from local to remote with valid ICE username,
2059 // MESSAGE-INTEGRITY, and FINGERPRINT.
Steve Anton11358fe2018-10-09 15:39:19 -07002060 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002061 in_msg->AddMessageIntegrity("rpass");
2062 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002063 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002064 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2065 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002066 EXPECT_TRUE(out_msg.get() != NULL);
2067 EXPECT_EQ("lfrag", username);
2068
2069 // BINDING-RESPONSE without username, with MESSAGE-INTEGRITY and FINGERPRINT.
Steve Anton11358fe2018-10-09 15:39:19 -07002070 in_msg = CreateStunMessage(STUN_BINDING_RESPONSE);
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002071 in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>(
zsteinf42cc9d2017-03-27 16:17:19 -07002072 STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002073 in_msg->AddMessageIntegrity("rpass");
2074 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002075 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002076 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2077 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002078 EXPECT_TRUE(out_msg.get() != NULL);
2079 EXPECT_EQ("", username);
2080
2081 // BINDING-ERROR-RESPONSE without username, with error, M-I, and FINGERPRINT.
Steve Anton11358fe2018-10-09 15:39:19 -07002082 in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE);
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002083 in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>(
zsteinf42cc9d2017-03-27 16:17:19 -07002084 STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR,
2085 STUN_ERROR_REASON_SERVER_ERROR));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002086 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002087 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002088 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2089 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002090 EXPECT_TRUE(out_msg.get() != NULL);
2091 EXPECT_EQ("", username);
2092 ASSERT_TRUE(out_msg->GetErrorCode() != NULL);
2093 EXPECT_EQ(STUN_ERROR_SERVER_ERROR, out_msg->GetErrorCode()->code());
2094 EXPECT_EQ(std::string(STUN_ERROR_REASON_SERVER_ERROR),
Yves Gerey665174f2018-06-19 15:03:05 +02002095 out_msg->GetErrorCode()->reason());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002096}
2097
guoweisd12140a2015-09-10 13:32:11 -07002098// Tests handling of ICE binding requests with missing or incorrect usernames.
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002099TEST_F(PortTest, TestHandleStunMessageBadUsername) {
Steve Anton11358fe2018-10-09 15:39:19 -07002100 auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002101
kwiberg3ec46792016-04-27 07:22:53 -07002102 std::unique_ptr<IceMessage> in_msg, out_msg;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002103 auto buf = std::make_unique<ByteBufferWriter>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002104 rtc::SocketAddress addr(kLocalAddr1);
2105 std::string username;
2106
2107 // BINDING-REQUEST with no username.
Steve Anton11358fe2018-10-09 15:39:19 -07002108 in_msg = CreateStunMessage(STUN_BINDING_REQUEST);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002109 in_msg->AddMessageIntegrity("rpass");
2110 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002111 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002112 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2113 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002114 EXPECT_TRUE(out_msg.get() == NULL);
2115 EXPECT_EQ("", username);
2116 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code());
2117
2118 // BINDING-REQUEST with empty username.
Steve Anton11358fe2018-10-09 15:39:19 -07002119 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002120 in_msg->AddMessageIntegrity("rpass");
2121 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002122 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002123 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2124 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002125 EXPECT_TRUE(out_msg.get() == NULL);
2126 EXPECT_EQ("", username);
2127 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2128
2129 // BINDING-REQUEST with too-short username.
Steve Anton11358fe2018-10-09 15:39:19 -07002130 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfra");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002131 in_msg->AddMessageIntegrity("rpass");
2132 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002133 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002134 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2135 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002136 EXPECT_TRUE(out_msg.get() == NULL);
2137 EXPECT_EQ("", username);
2138 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2139
2140 // BINDING-REQUEST with reversed username.
Steve Anton11358fe2018-10-09 15:39:19 -07002141 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "lfrag:rfrag");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002142 in_msg->AddMessageIntegrity("rpass");
2143 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002144 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002145 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2146 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002147 EXPECT_TRUE(out_msg.get() == NULL);
2148 EXPECT_EQ("", username);
2149 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2150
2151 // BINDING-REQUEST with garbage username.
Steve Anton11358fe2018-10-09 15:39:19 -07002152 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "abcd:efgh");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002153 in_msg->AddMessageIntegrity("rpass");
2154 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002155 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002156 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2157 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002158 EXPECT_TRUE(out_msg.get() == NULL);
2159 EXPECT_EQ("", username);
2160 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2161}
2162
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002163// Test handling STUN messages with missing or malformed M-I.
2164TEST_F(PortTest, TestHandleStunMessageBadMessageIntegrity) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002165 // Our port will act as the "remote" port.
Steve Anton11358fe2018-10-09 15:39:19 -07002166 auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002167
kwiberg3ec46792016-04-27 07:22:53 -07002168 std::unique_ptr<IceMessage> in_msg, out_msg;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002169 auto buf = std::make_unique<ByteBufferWriter>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002170 rtc::SocketAddress addr(kLocalAddr1);
2171 std::string username;
2172
2173 // BINDING-REQUEST from local to remote with valid ICE username and
2174 // FINGERPRINT, but no MESSAGE-INTEGRITY.
Steve Anton11358fe2018-10-09 15:39:19 -07002175 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002176 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002177 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002178 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2179 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002180 EXPECT_TRUE(out_msg.get() == NULL);
2181 EXPECT_EQ("", username);
2182 EXPECT_EQ(STUN_ERROR_BAD_REQUEST, port->last_stun_error_code());
2183
2184 // BINDING-REQUEST from local to remote with valid ICE username and
2185 // FINGERPRINT, but invalid MESSAGE-INTEGRITY.
Steve Anton11358fe2018-10-09 15:39:19 -07002186 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002187 in_msg->AddMessageIntegrity("invalid");
2188 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002189 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002190 EXPECT_TRUE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2191 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002192 EXPECT_TRUE(out_msg.get() == NULL);
2193 EXPECT_EQ("", username);
2194 EXPECT_EQ(STUN_ERROR_UNAUTHORIZED, port->last_stun_error_code());
2195
Steve Antonbabf9172017-11-29 10:19:02 -08002196 // TODO(?): BINDING-RESPONSES and BINDING-ERROR-RESPONSES are checked
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002197 // by the Connection, not the Port, since they require the remote username.
2198 // Change this test to pass in data via Connection::OnReadPacket instead.
2199}
2200
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002201// Test handling STUN messages with missing or malformed FINGERPRINT.
2202TEST_F(PortTest, TestHandleStunMessageBadFingerprint) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002203 // Our port will act as the "remote" port.
Steve Anton11358fe2018-10-09 15:39:19 -07002204 auto port = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002205
kwiberg3ec46792016-04-27 07:22:53 -07002206 std::unique_ptr<IceMessage> in_msg, out_msg;
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002207 auto buf = std::make_unique<ByteBufferWriter>();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002208 rtc::SocketAddress addr(kLocalAddr1);
2209 std::string username;
2210
2211 // BINDING-REQUEST from local to remote with valid ICE username and
2212 // MESSAGE-INTEGRITY, but no FINGERPRINT; GetStunMessage should fail.
Steve Anton11358fe2018-10-09 15:39:19 -07002213 in_msg = CreateStunMessageWithUsername(STUN_BINDING_REQUEST, "rfrag:lfrag");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002214 in_msg->AddMessageIntegrity("rpass");
Steve Anton11358fe2018-10-09 15:39:19 -07002215 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002216 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2217 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002218 EXPECT_EQ(0, port->last_stun_error_code());
2219
2220 // Now, add a fingerprint, but munge the message so it's not valid.
2221 in_msg->AddFingerprint();
2222 in_msg->SetTransactionID("TESTTESTBADD");
Steve Anton11358fe2018-10-09 15:39:19 -07002223 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002224 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2225 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002226 EXPECT_EQ(0, port->last_stun_error_code());
2227
2228 // Valid BINDING-RESPONSE, except no FINGERPRINT.
Steve Anton11358fe2018-10-09 15:39:19 -07002229 in_msg = CreateStunMessage(STUN_BINDING_RESPONSE);
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002230 in_msg->AddAttribute(std::make_unique<StunXorAddressAttribute>(
zsteinf42cc9d2017-03-27 16:17:19 -07002231 STUN_ATTR_XOR_MAPPED_ADDRESS, kLocalAddr2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002232 in_msg->AddMessageIntegrity("rpass");
Steve Anton11358fe2018-10-09 15:39:19 -07002233 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002234 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2235 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002236 EXPECT_EQ(0, port->last_stun_error_code());
2237
2238 // Now, add a fingerprint, but munge the message so it's not valid.
2239 in_msg->AddFingerprint();
2240 in_msg->SetTransactionID("TESTTESTBADD");
Steve Anton11358fe2018-10-09 15:39:19 -07002241 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002242 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2243 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002244 EXPECT_EQ(0, port->last_stun_error_code());
2245
2246 // Valid BINDING-ERROR-RESPONSE, except no FINGERPRINT.
Steve Anton11358fe2018-10-09 15:39:19 -07002247 in_msg = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE);
Mirko Bonadei317a1f02019-09-17 17:06:18 +02002248 in_msg->AddAttribute(std::make_unique<StunErrorCodeAttribute>(
zsteinf42cc9d2017-03-27 16:17:19 -07002249 STUN_ATTR_ERROR_CODE, STUN_ERROR_SERVER_ERROR,
2250 STUN_ERROR_REASON_SERVER_ERROR));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002251 in_msg->AddMessageIntegrity("rpass");
Steve Anton11358fe2018-10-09 15:39:19 -07002252 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002253 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2254 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002255 EXPECT_EQ(0, port->last_stun_error_code());
2256
2257 // Now, add a fingerprint, but munge the message so it's not valid.
2258 in_msg->AddFingerprint();
2259 in_msg->SetTransactionID("TESTTESTBADD");
Steve Anton11358fe2018-10-09 15:39:19 -07002260 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002261 EXPECT_FALSE(port->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2262 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002263 EXPECT_EQ(0, port->last_stun_error_code());
2264}
2265
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002266// Test handling of STUN binding indication messages . STUN binding
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002267// indications are allowed only to the connection which is in read mode.
2268TEST_F(PortTest, TestHandleStunBindingIndication) {
Steve Anton11358fe2018-10-09 15:39:19 -07002269 auto lport = CreateTestPort(kLocalAddr2, "lfrag", "lpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002270 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
2271 lport->SetIceTiebreaker(kTiebreaker1);
2272
2273 // Verifying encoding and decoding STUN indication message.
kwiberg3ec46792016-04-27 07:22:53 -07002274 std::unique_ptr<IceMessage> in_msg, out_msg;
2275 std::unique_ptr<ByteBufferWriter> buf(new ByteBufferWriter());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002276 rtc::SocketAddress addr(kLocalAddr1);
2277 std::string username;
2278
Steve Anton11358fe2018-10-09 15:39:19 -07002279 in_msg = CreateStunMessage(STUN_BINDING_INDICATION);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002280 in_msg->AddFingerprint();
Steve Anton11358fe2018-10-09 15:39:19 -07002281 WriteStunMessage(*in_msg, buf.get());
kwiberg6baec032016-03-15 11:09:39 -07002282 EXPECT_TRUE(lport->GetStunMessage(buf->Data(), buf->Length(), addr, &out_msg,
2283 &username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002284 EXPECT_TRUE(out_msg.get() != NULL);
2285 EXPECT_EQ(out_msg->type(), STUN_BINDING_INDICATION);
2286 EXPECT_EQ("", username);
2287
2288 // Verify connection can handle STUN indication and updates
2289 // last_ping_received.
Steve Anton11358fe2018-10-09 15:39:19 -07002290 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002291 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2292 rport->SetIceTiebreaker(kTiebreaker2);
2293
2294 lport->PrepareAddress();
2295 rport->PrepareAddress();
2296 ASSERT_FALSE(lport->Candidates().empty());
2297 ASSERT_FALSE(rport->Candidates().empty());
2298
Yves Gerey665174f2018-06-19 15:03:05 +02002299 Connection* lconn =
2300 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
2301 Connection* rconn =
2302 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002303 rconn->Ping(0);
2304
Honghai Zhang161a5862016-10-20 11:47:02 -07002305 ASSERT_TRUE_WAIT(rport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002306 IceMessage* msg = rport->last_stun_msg();
2307 EXPECT_EQ(STUN_BINDING_REQUEST, msg->type());
2308 // Send rport binding request to lport.
jbauchf1f87202016-03-30 06:43:37 -07002309 lconn->OnReadPacket(rport->last_stun_buf()->data<char>(),
Niels Möllere6933812018-11-05 13:01:41 +01002310 rport->last_stun_buf()->size(), /* packet_time_us */ -1);
Honghai Zhang161a5862016-10-20 11:47:02 -07002311 ASSERT_TRUE_WAIT(lport->last_stun_msg() != NULL, kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002312 EXPECT_EQ(STUN_BINDING_RESPONSE, lport->last_stun_msg()->type());
honghaiz34b11eb2016-03-16 08:55:44 -07002313 int64_t last_ping_received1 = lconn->last_ping_received();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002314
2315 // Adding a delay of 100ms.
2316 rtc::Thread::Current()->ProcessMessages(100);
2317 // Pinging lconn using stun indication message.
Niels Möllere6933812018-11-05 13:01:41 +01002318 lconn->OnReadPacket(buf->Data(), buf->Length(), /* packet_time_us */ -1);
honghaiz34b11eb2016-03-16 08:55:44 -07002319 int64_t last_ping_received2 = lconn->last_ping_received();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002320 EXPECT_GT(last_ping_received2, last_ping_received1);
2321}
2322
2323TEST_F(PortTest, TestComputeCandidatePriority) {
Steve Anton11358fe2018-10-09 15:39:19 -07002324 auto port = CreateTestPort(kLocalAddr1, "name", "pass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002325 port->set_type_preference(90);
2326 port->set_component(177);
2327 port->AddCandidateAddress(SocketAddress("192.168.1.4", 1234));
2328 port->AddCandidateAddress(SocketAddress("2001:db8::1234", 1234));
2329 port->AddCandidateAddress(SocketAddress("fc12:3456::1234", 1234));
2330 port->AddCandidateAddress(SocketAddress("::ffff:192.168.1.4", 1234));
2331 port->AddCandidateAddress(SocketAddress("::192.168.1.4", 1234));
2332 port->AddCandidateAddress(SocketAddress("2002::1234:5678", 1234));
2333 port->AddCandidateAddress(SocketAddress("2001::1234:5678", 1234));
2334 port->AddCandidateAddress(SocketAddress("fecf::1234:5678", 1234));
2335 port->AddCandidateAddress(SocketAddress("3ffe::1234:5678", 1234));
2336 // These should all be:
2337 // (90 << 24) | ([rfc3484 pref value] << 8) | (256 - 177)
Peter Boström0c4e06b2015-10-07 12:23:21 +02002338 uint32_t expected_priority_v4 = 1509957199U;
2339 uint32_t expected_priority_v6 = 1509959759U;
2340 uint32_t expected_priority_ula = 1509962319U;
2341 uint32_t expected_priority_v4mapped = expected_priority_v4;
2342 uint32_t expected_priority_v4compat = 1509949775U;
2343 uint32_t expected_priority_6to4 = 1509954639U;
2344 uint32_t expected_priority_teredo = 1509952079U;
2345 uint32_t expected_priority_sitelocal = 1509949775U;
2346 uint32_t expected_priority_6bone = 1509949775U;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002347 ASSERT_EQ(expected_priority_v4, port->Candidates()[0].priority());
2348 ASSERT_EQ(expected_priority_v6, port->Candidates()[1].priority());
2349 ASSERT_EQ(expected_priority_ula, port->Candidates()[2].priority());
2350 ASSERT_EQ(expected_priority_v4mapped, port->Candidates()[3].priority());
2351 ASSERT_EQ(expected_priority_v4compat, port->Candidates()[4].priority());
2352 ASSERT_EQ(expected_priority_6to4, port->Candidates()[5].priority());
2353 ASSERT_EQ(expected_priority_teredo, port->Candidates()[6].priority());
2354 ASSERT_EQ(expected_priority_sitelocal, port->Candidates()[7].priority());
2355 ASSERT_EQ(expected_priority_6bone, port->Candidates()[8].priority());
2356}
2357
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002358// In the case of shared socket, one port may be shared by local and stun.
2359// Test that candidates with different types will have different foundation.
2360TEST_F(PortTest, TestFoundation) {
Steve Anton11358fe2018-10-09 15:39:19 -07002361 auto testport = CreateTestPort(kLocalAddr1, "name", "pass");
Yves Gerey665174f2018-06-19 15:03:05 +02002362 testport->AddCandidateAddress(kLocalAddr1, kLocalAddr1, LOCAL_PORT_TYPE,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002363 cricket::ICE_TYPE_PREFERENCE_HOST, false);
Yves Gerey665174f2018-06-19 15:03:05 +02002364 testport->AddCandidateAddress(kLocalAddr2, kLocalAddr1, STUN_PORT_TYPE,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002365 cricket::ICE_TYPE_PREFERENCE_SRFLX, true);
2366 EXPECT_NE(testport->Candidates()[0].foundation(),
2367 testport->Candidates()[1].foundation());
2368}
2369
2370// This test verifies the foundation of different types of ICE candidates.
2371TEST_F(PortTest, TestCandidateFoundation) {
kwiberg3ec46792016-04-27 07:22:53 -07002372 std::unique_ptr<rtc::NATServer> nat_server(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002373 CreateNatServer(kNatAddr1, NAT_OPEN_CONE));
Steve Anton11358fe2018-10-09 15:39:19 -07002374 auto udpport1 = CreateUdpPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002375 udpport1->PrepareAddress();
Steve Anton11358fe2018-10-09 15:39:19 -07002376 auto udpport2 = CreateUdpPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002377 udpport2->PrepareAddress();
2378 EXPECT_EQ(udpport1->Candidates()[0].foundation(),
2379 udpport2->Candidates()[0].foundation());
Steve Anton11358fe2018-10-09 15:39:19 -07002380 auto tcpport1 = CreateTcpPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002381 tcpport1->PrepareAddress();
Steve Anton11358fe2018-10-09 15:39:19 -07002382 auto tcpport2 = CreateTcpPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002383 tcpport2->PrepareAddress();
2384 EXPECT_EQ(tcpport1->Candidates()[0].foundation(),
2385 tcpport2->Candidates()[0].foundation());
Steve Anton11358fe2018-10-09 15:39:19 -07002386 auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002387 stunport->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002388 ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002389 EXPECT_NE(tcpport1->Candidates()[0].foundation(),
2390 stunport->Candidates()[0].foundation());
2391 EXPECT_NE(tcpport2->Candidates()[0].foundation(),
2392 stunport->Candidates()[0].foundation());
2393 EXPECT_NE(udpport1->Candidates()[0].foundation(),
2394 stunport->Candidates()[0].foundation());
2395 EXPECT_NE(udpport2->Candidates()[0].foundation(),
2396 stunport->Candidates()[0].foundation());
2397 // Verify GTURN candidate foundation.
Steve Anton11358fe2018-10-09 15:39:19 -07002398 auto relayport = CreateGturnPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002399 relayport->AddServerAddress(
2400 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP));
2401 relayport->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002402 ASSERT_EQ_WAIT(1U, relayport->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002403 EXPECT_NE(udpport1->Candidates()[0].foundation(),
2404 relayport->Candidates()[0].foundation());
2405 EXPECT_NE(udpport2->Candidates()[0].foundation(),
2406 relayport->Candidates()[0].foundation());
2407 // Verifying TURN candidate foundation.
Steve Anton11358fe2018-10-09 15:39:19 -07002408 auto turnport1 =
2409 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002410 turnport1->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002411 ASSERT_EQ_WAIT(1U, turnport1->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002412 EXPECT_NE(udpport1->Candidates()[0].foundation(),
2413 turnport1->Candidates()[0].foundation());
2414 EXPECT_NE(udpport2->Candidates()[0].foundation(),
2415 turnport1->Candidates()[0].foundation());
2416 EXPECT_NE(stunport->Candidates()[0].foundation(),
2417 turnport1->Candidates()[0].foundation());
Steve Anton11358fe2018-10-09 15:39:19 -07002418 auto turnport2 =
2419 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002420 turnport2->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002421 ASSERT_EQ_WAIT(1U, turnport2->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002422 EXPECT_EQ(turnport1->Candidates()[0].foundation(),
2423 turnport2->Candidates()[0].foundation());
2424
2425 // Running a second turn server, to get different base IP address.
2426 SocketAddress kTurnUdpIntAddr2("99.99.98.4", STUN_SERVER_PORT);
2427 SocketAddress kTurnUdpExtAddr2("99.99.98.5", 0);
Yves Gerey665174f2018-06-19 15:03:05 +02002428 TestTurnServer turn_server2(rtc::Thread::Current(), kTurnUdpIntAddr2,
2429 kTurnUdpExtAddr2);
Steve Anton11358fe2018-10-09 15:39:19 -07002430 auto turnport3 = CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP,
2431 PROTO_UDP, kTurnUdpIntAddr2);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002432 turnport3->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002433 ASSERT_EQ_WAIT(1U, turnport3->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002434 EXPECT_NE(turnport3->Candidates()[0].foundation(),
2435 turnport2->Candidates()[0].foundation());
Honghai Zhang80f1db92016-01-27 11:54:45 -08002436
2437 // Start a TCP turn server, and check that two turn candidates have
2438 // different foundations if their relay protocols are different.
2439 TestTurnServer turn_server3(rtc::Thread::Current(), kTurnTcpIntAddr,
2440 kTurnUdpExtAddr, PROTO_TCP);
Steve Anton11358fe2018-10-09 15:39:19 -07002441 auto turnport4 =
2442 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_TCP, PROTO_UDP);
Honghai Zhang80f1db92016-01-27 11:54:45 -08002443 turnport4->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002444 ASSERT_EQ_WAIT(1U, turnport4->Candidates().size(), kDefaultTimeout);
Honghai Zhang80f1db92016-01-27 11:54:45 -08002445 EXPECT_NE(turnport2->Candidates()[0].foundation(),
2446 turnport4->Candidates()[0].foundation());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002447}
2448
2449// This test verifies the related addresses of different types of
2450// ICE candiates.
2451TEST_F(PortTest, TestCandidateRelatedAddress) {
Steve Anton11358fe2018-10-09 15:39:19 -07002452 auto nat_server = CreateNatServer(kNatAddr1, NAT_OPEN_CONE);
2453 auto udpport = CreateUdpPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002454 udpport->PrepareAddress();
2455 // For UDPPort, related address will be empty.
2456 EXPECT_TRUE(udpport->Candidates()[0].related_address().IsNil());
2457 // Testing related address for stun candidates.
2458 // For stun candidate related address must be equal to the base
2459 // socket address.
Steve Anton11358fe2018-10-09 15:39:19 -07002460 auto stunport = CreateStunPort(kLocalAddr1, nat_socket_factory1());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002461 stunport->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002462 ASSERT_EQ_WAIT(1U, stunport->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002463 // Check STUN candidate address.
Yves Gerey665174f2018-06-19 15:03:05 +02002464 EXPECT_EQ(stunport->Candidates()[0].address().ipaddr(), kNatAddr1.ipaddr());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002465 // Check STUN candidate related address.
2466 EXPECT_EQ(stunport->Candidates()[0].related_address(),
2467 stunport->GetLocalAddress());
2468 // Verifying the related address for the GTURN candidates.
2469 // NOTE: In case of GTURN related address will be equal to the mapped
2470 // address, but address(mapped) will not be XOR.
Steve Anton11358fe2018-10-09 15:39:19 -07002471 auto relayport = CreateGturnPort(kLocalAddr1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002472 relayport->AddServerAddress(
2473 cricket::ProtocolAddress(kRelayUdpIntAddr, cricket::PROTO_UDP));
2474 relayport->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002475 ASSERT_EQ_WAIT(1U, relayport->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002476 // For Gturn related address is set to "0.0.0.0:0"
Yves Gerey665174f2018-06-19 15:03:05 +02002477 EXPECT_EQ(rtc::SocketAddress(), relayport->Candidates()[0].related_address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002478 // Verifying the related address for TURN candidate.
2479 // For TURN related address must be equal to the mapped address.
Steve Anton11358fe2018-10-09 15:39:19 -07002480 auto turnport =
2481 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002482 turnport->PrepareAddress();
Honghai Zhang161a5862016-10-20 11:47:02 -07002483 ASSERT_EQ_WAIT(1U, turnport->Candidates().size(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002484 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(),
2485 turnport->Candidates()[0].address().ipaddr());
2486 EXPECT_EQ(kNatAddr1.ipaddr(),
2487 turnport->Candidates()[0].related_address().ipaddr());
2488}
2489
2490// Test priority value overflow handling when preference is set to 3.
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002491TEST_F(PortTest, TestCandidatePriority) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002492 cricket::Candidate cand1;
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002493 cand1.set_priority(3);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002494 cricket::Candidate cand2;
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002495 cand2.set_priority(1);
2496 EXPECT_TRUE(cand1.priority() > cand2.priority());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002497}
2498
2499// Test the Connection priority is calculated correctly.
2500TEST_F(PortTest, TestConnectionPriority) {
Steve Anton11358fe2018-10-09 15:39:19 -07002501 auto lport = CreateTestPort(kLocalAddr1, "lfrag", "lpass");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002502 lport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_HOST);
Steve Anton11358fe2018-10-09 15:39:19 -07002503 auto rport = CreateTestPort(kLocalAddr2, "rfrag", "rpass");
hnsl277b2502016-12-13 05:17:23 -08002504 rport->set_type_preference(cricket::ICE_TYPE_PREFERENCE_RELAY_UDP);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002505 lport->set_component(123);
2506 lport->AddCandidateAddress(SocketAddress("192.168.1.4", 1234));
2507 rport->set_component(23);
2508 rport->AddCandidateAddress(SocketAddress("10.1.1.100", 1234));
2509
2510 EXPECT_EQ(0x7E001E85U, lport->Candidates()[0].priority());
2511 EXPECT_EQ(0x2001EE9U, rport->Candidates()[0].priority());
2512
2513 // RFC 5245
2514 // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0)
2515 lport->SetIceRole(cricket::ICEROLE_CONTROLLING);
2516 rport->SetIceRole(cricket::ICEROLE_CONTROLLED);
Yves Gerey665174f2018-06-19 15:03:05 +02002517 Connection* lconn =
2518 lport->CreateConnection(rport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002519#if defined(WEBRTC_WIN)
2520 EXPECT_EQ(0x2001EE9FC003D0BU, lconn->priority());
2521#else
2522 EXPECT_EQ(0x2001EE9FC003D0BLLU, lconn->priority());
2523#endif
2524
2525 lport->SetIceRole(cricket::ICEROLE_CONTROLLED);
2526 rport->SetIceRole(cricket::ICEROLE_CONTROLLING);
Yves Gerey665174f2018-06-19 15:03:05 +02002527 Connection* rconn =
2528 rport->CreateConnection(lport->Candidates()[0], Port::ORIGIN_MESSAGE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002529#if defined(WEBRTC_WIN)
2530 EXPECT_EQ(0x2001EE9FC003D0AU, rconn->priority());
2531#else
2532 EXPECT_EQ(0x2001EE9FC003D0ALLU, rconn->priority());
2533#endif
2534}
2535
Qingsi Wang22e623a2018-03-13 10:53:57 -07002536// Note that UpdateState takes into account the estimated RTT, and the
2537// correctness of using |kMaxExpectedSimulatedRtt| as an upper bound of RTT in
2538// the following tests depends on the link rate and the delay distriubtion
2539// configured in VirtualSocketServer::AddPacketToNetwork. The tests below use
2540// the default setup where the RTT is deterministically one, which generates an
2541// estimate given by |MINIMUM_RTT| = 100.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002542TEST_F(PortTest, TestWritableState) {
Honghai Zhang161a5862016-10-20 11:47:02 -07002543 rtc::ScopedFakeClock clock;
Steve Anton11358fe2018-10-09 15:39:19 -07002544 auto port1 = CreateUdpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002545 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -07002546 auto port2 = CreateUdpPort(kLocalAddr2);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002547 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002548
2549 // Set up channels.
Steve Anton11358fe2018-10-09 15:39:19 -07002550 TestChannel ch1(std::move(port1));
2551 TestChannel ch2(std::move(port2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002552
2553 // Acquire addresses.
2554 ch1.Start();
2555 ch2.Start();
Honghai Zhang161a5862016-10-20 11:47:02 -07002556 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock);
2557 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002558
2559 // Send a ping from src to dst.
Steve Anton11358fe2018-10-09 15:39:19 -07002560 ch1.CreateConnection(GetCandidate(ch2.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002561 ASSERT_TRUE(ch1.conn() != NULL);
2562 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
Honghai Zhang161a5862016-10-20 11:47:02 -07002563 // for TCP connect
2564 EXPECT_TRUE_SIMULATED_WAIT(ch1.conn()->connected(), kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002565 ch1.Ping();
Honghai Zhang161a5862016-10-20 11:47:02 -07002566 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002567
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -07002568 // Data should be sendable before the connection is accepted.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002569 char data[] = "abcd";
tfarina5237aaf2015-11-10 23:44:30 -08002570 int data_size = arraysize(data);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002571 rtc::PacketOptions options;
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -07002572 EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002573
2574 // Accept the connection to return the binding response, transition to
2575 // writable, and allow data to be sent.
Steve Anton11358fe2018-10-09 15:39:19 -07002576 ch2.AcceptConnection(GetCandidate(ch1.port()));
Honghai Zhang161a5862016-10-20 11:47:02 -07002577 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
2578 ch1.conn()->write_state(), kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002579 EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options));
2580
2581 // Ask the connection to update state as if enough time has passed to lose
2582 // full writability and 5 pings went unresponded to. We'll accomplish the
2583 // latter by sending pings but not pumping messages.
Peter Boström0c4e06b2015-10-07 12:23:21 +02002584 for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002585 ch1.Ping(i);
2586 }
Qingsi Wang22e623a2018-03-13 10:53:57 -07002587 int unreliable_timeout_delay =
2588 CONNECTION_WRITE_CONNECT_TIMEOUT + kMaxExpectedSimulatedRtt;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002589 ch1.conn()->UpdateState(unreliable_timeout_delay);
2590 EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state());
2591
2592 // Data should be able to be sent in this state.
2593 EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options));
2594
2595 // And now allow the other side to process the pings and send binding
2596 // responses.
Honghai Zhang161a5862016-10-20 11:47:02 -07002597 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
2598 ch1.conn()->write_state(), kDefaultTimeout, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002599 // Wait long enough for a full timeout (past however long we've already
2600 // waited).
Peter Boström0c4e06b2015-10-07 12:23:21 +02002601 for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002602 ch1.Ping(unreliable_timeout_delay + i);
2603 }
2604 ch1.conn()->UpdateState(unreliable_timeout_delay + CONNECTION_WRITE_TIMEOUT +
Qingsi Wang22e623a2018-03-13 10:53:57 -07002605 kMaxExpectedSimulatedRtt);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002606 EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state());
2607
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -07002608 // Even if the connection has timed out, the Connection shouldn't block
2609 // the sending of data.
2610 EXPECT_EQ(data_size, ch1.conn()->Send(data, data_size, options));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002611
2612 ch1.Stop();
2613 ch2.Stop();
2614}
2615
Qingsi Wang22e623a2018-03-13 10:53:57 -07002616// Test writability states using the configured threshold value to replace
2617// the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT| and
2618// |CONNECTION_WRITE_CONNECT_FAILURES|.
2619TEST_F(PortTest, TestWritableStateWithConfiguredThreshold) {
2620 rtc::ScopedFakeClock clock;
Steve Anton11358fe2018-10-09 15:39:19 -07002621 auto port1 = CreateUdpPort(kLocalAddr1);
Qingsi Wang22e623a2018-03-13 10:53:57 -07002622 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -07002623 auto port2 = CreateUdpPort(kLocalAddr2);
Qingsi Wang22e623a2018-03-13 10:53:57 -07002624 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2625
2626 // Set up channels.
Steve Anton11358fe2018-10-09 15:39:19 -07002627 TestChannel ch1(std::move(port1));
2628 TestChannel ch2(std::move(port2));
Qingsi Wang22e623a2018-03-13 10:53:57 -07002629
2630 // Acquire addresses.
2631 ch1.Start();
2632 ch2.Start();
2633 ASSERT_EQ_SIMULATED_WAIT(1, ch1.complete_count(), kDefaultTimeout, clock);
2634 ASSERT_EQ_SIMULATED_WAIT(1, ch2.complete_count(), kDefaultTimeout, clock);
2635
2636 // Send a ping from src to dst.
Steve Anton11358fe2018-10-09 15:39:19 -07002637 ch1.CreateConnection(GetCandidate(ch2.port()));
Qingsi Wang22e623a2018-03-13 10:53:57 -07002638 ASSERT_TRUE(ch1.conn() != NULL);
2639 ch1.Ping();
2640 SIMULATED_WAIT(!ch2.remote_address().IsNil(), kShortTimeout, clock);
2641
2642 // Accept the connection to return the binding response, transition to
2643 // writable, and allow data to be sent.
Steve Anton11358fe2018-10-09 15:39:19 -07002644 ch2.AcceptConnection(GetCandidate(ch1.port()));
Qingsi Wang22e623a2018-03-13 10:53:57 -07002645 EXPECT_EQ_SIMULATED_WAIT(Connection::STATE_WRITABLE,
2646 ch1.conn()->write_state(), kDefaultTimeout, clock);
2647
2648 ch1.conn()->set_unwritable_timeout(1000);
2649 ch1.conn()->set_unwritable_min_checks(3);
2650 // Send two checks.
2651 ch1.Ping(1);
2652 ch1.Ping(2);
2653 // We have not reached the timeout nor have we sent the minimum number of
2654 // checks to change the state to Unreliable.
2655 ch1.conn()->UpdateState(999);
2656 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
2657 // We have not sent the minimum number of checks without responses.
2658 ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt);
2659 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
2660 // Last ping after which the candidate pair should become Unreliable after
2661 // timeout.
2662 ch1.Ping(3);
2663 // We have not reached the timeout.
2664 ch1.conn()->UpdateState(999);
2665 EXPECT_EQ(Connection::STATE_WRITABLE, ch1.conn()->write_state());
2666 // We should be in the state Unreliable now.
2667 ch1.conn()->UpdateState(1000 + kMaxExpectedSimulatedRtt);
2668 EXPECT_EQ(Connection::STATE_WRITE_UNRELIABLE, ch1.conn()->write_state());
2669
2670 ch1.Stop();
2671 ch2.Stop();
2672}
2673
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002674TEST_F(PortTest, TestTimeoutForNeverWritable) {
Steve Anton11358fe2018-10-09 15:39:19 -07002675 auto port1 = CreateUdpPort(kLocalAddr1);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002676 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
Steve Anton11358fe2018-10-09 15:39:19 -07002677 auto port2 = CreateUdpPort(kLocalAddr2);
Peter Thatcher7cbd1882015-09-17 18:54:52 -07002678 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002679
2680 // Set up channels.
Steve Anton11358fe2018-10-09 15:39:19 -07002681 TestChannel ch1(std::move(port1));
2682 TestChannel ch2(std::move(port2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002683
2684 // Acquire addresses.
2685 ch1.Start();
2686 ch2.Start();
2687
Steve Anton11358fe2018-10-09 15:39:19 -07002688 ch1.CreateConnection(GetCandidate(ch2.port()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002689 ASSERT_TRUE(ch1.conn() != NULL);
2690 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2691
2692 // Attempt to go directly to write timeout.
Peter Boström0c4e06b2015-10-07 12:23:21 +02002693 for (uint32_t i = 1; i <= CONNECTION_WRITE_CONNECT_FAILURES; ++i) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002694 ch1.Ping(i);
2695 }
Qingsi Wang22e623a2018-03-13 10:53:57 -07002696 ch1.conn()->UpdateState(CONNECTION_WRITE_TIMEOUT + kMaxExpectedSimulatedRtt);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002697 EXPECT_EQ(Connection::STATE_WRITE_TIMEOUT, ch1.conn()->write_state());
2698}
2699
2700// This test verifies the connection setup between ICEMODE_FULL
2701// and ICEMODE_LITE.
2702// In this test |ch1| behaves like FULL mode client and we have created
2703// port which responds to the ping message just like LITE client.
2704TEST_F(PortTest, TestIceLiteConnectivity) {
Steve Anton11358fe2018-10-09 15:39:19 -07002705 auto ice_full_port =
Yves Gerey665174f2018-06-19 15:03:05 +02002706 CreateTestPort(kLocalAddr1, "lfrag", "lpass",
2707 cricket::ICEROLE_CONTROLLING, kTiebreaker1);
Steve Anton11358fe2018-10-09 15:39:19 -07002708 auto* ice_full_port_ptr = ice_full_port.get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002709
Steve Anton11358fe2018-10-09 15:39:19 -07002710 auto ice_lite_port = CreateTestPort(
2711 kLocalAddr2, "rfrag", "rpass", cricket::ICEROLE_CONTROLLED, kTiebreaker2);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002712 // Setup TestChannel. This behaves like FULL mode client.
Steve Anton11358fe2018-10-09 15:39:19 -07002713 TestChannel ch1(std::move(ice_full_port));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002714 ch1.SetIceMode(ICEMODE_FULL);
2715
2716 // Start gathering candidates.
2717 ch1.Start();
2718 ice_lite_port->PrepareAddress();
2719
Honghai Zhang161a5862016-10-20 11:47:02 -07002720 ASSERT_EQ_WAIT(1, ch1.complete_count(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002721 ASSERT_FALSE(ice_lite_port->Candidates().empty());
2722
Guo-wei Shieh1eb87c72015-08-25 11:02:55 -07002723 ch1.CreateConnection(GetCandidate(ice_lite_port.get()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002724 ASSERT_TRUE(ch1.conn() != NULL);
2725 EXPECT_EQ(Connection::STATE_WRITE_INIT, ch1.conn()->write_state());
2726
2727 // Send ping from full mode client.
2728 // This ping must not have USE_CANDIDATE_ATTR.
2729 ch1.Ping();
2730
2731 // Verify stun ping is without USE_CANDIDATE_ATTR. Getting message directly
2732 // from port.
Steve Anton11358fe2018-10-09 15:39:19 -07002733 ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout);
2734 IceMessage* msg = ice_full_port_ptr->last_stun_msg();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002735 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) == NULL);
2736
2737 // Respond with a BINDING-RESPONSE from litemode client.
2738 // NOTE: Ideally we should't create connection at this stage from lite
2739 // port, as it should be done only after receiving ping with USE_CANDIDATE.
2740 // But we need a connection to send a response message.
Steve Anton11358fe2018-10-09 15:39:19 -07002741 ice_lite_port->CreateConnection(ice_full_port_ptr->Candidates()[0],
Yves Gerey665174f2018-06-19 15:03:05 +02002742 cricket::Port::ORIGIN_MESSAGE);
Steve Anton11358fe2018-10-09 15:39:19 -07002743 std::unique_ptr<IceMessage> request = CopyStunMessage(*msg);
2744 ice_lite_port->SendBindingResponse(
2745 request.get(), ice_full_port_ptr->Candidates()[0].address());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002746
2747 // Feeding the respone message from litemode to the full mode connection.
jbauchf1f87202016-03-30 06:43:37 -07002748 ch1.conn()->OnReadPacket(ice_lite_port->last_stun_buf()->data<char>(),
2749 ice_lite_port->last_stun_buf()->size(),
Niels Möllere6933812018-11-05 13:01:41 +01002750 /* packet_time_us */ -1);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002751 // Verifying full mode connection becomes writable from the response.
2752 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, ch1.conn()->write_state(),
Honghai Zhang161a5862016-10-20 11:47:02 -07002753 kDefaultTimeout);
2754 EXPECT_TRUE_WAIT(ch1.nominated(), kDefaultTimeout);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002755
2756 // Clear existing stun messsages. Otherwise we will process old stun
2757 // message right after we send ping.
Steve Anton11358fe2018-10-09 15:39:19 -07002758 ice_full_port_ptr->Reset();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002759 // Send ping. This must have USE_CANDIDATE_ATTR.
2760 ch1.Ping();
Steve Anton11358fe2018-10-09 15:39:19 -07002761 ASSERT_TRUE_WAIT(ice_full_port_ptr->last_stun_msg() != NULL, kDefaultTimeout);
2762 msg = ice_full_port_ptr->last_stun_msg();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002763 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL);
2764 ch1.Stop();
2765}
2766
Honghai Zhanga74363c2016-07-28 18:06:15 -07002767// This test case verifies that both the controlling port and the controlled
2768// port will time out after connectivity is lost, if they are not marked as
2769// "keep alive until pruned."
2770TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) {
2771 rtc::ScopedFakeClock clock;
2772 int timeout_delay = 100;
Steve Anton11358fe2018-10-09 15:39:19 -07002773 auto port1 = CreateUdpPort(kLocalAddr1);
2774 ConnectToSignalDestroyed(port1.get());
Honghai Zhanga74363c2016-07-28 18:06:15 -07002775 port1->set_timeout_delay(timeout_delay); // milliseconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002776 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2777 port1->SetIceTiebreaker(kTiebreaker1);
2778
Steve Anton11358fe2018-10-09 15:39:19 -07002779 auto port2 = CreateUdpPort(kLocalAddr2);
2780 ConnectToSignalDestroyed(port2.get());
Honghai Zhanga74363c2016-07-28 18:06:15 -07002781 port2->set_timeout_delay(timeout_delay); // milliseconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002782 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2783 port2->SetIceTiebreaker(kTiebreaker2);
2784
2785 // Set up channels and ensure both ports will be deleted.
Steve Anton11358fe2018-10-09 15:39:19 -07002786 TestChannel ch1(std::move(port1));
2787 TestChannel ch2(std::move(port2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002788
2789 // Simulate a connection that succeeds, and then is destroyed.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -07002790 StartConnectAndStopChannels(&ch1, &ch2);
Honghai Zhanga74363c2016-07-28 18:06:15 -07002791 // After the connection is destroyed, the port will be destroyed because
2792 // none of them is marked as "keep alive until pruned.
2793 EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), 110, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002794}
2795
Honghai Zhanga74363c2016-07-28 18:06:15 -07002796// Test that if after all connection are destroyed, new connections are created
2797// and destroyed again, ports won't be destroyed until a timeout period passes
2798// after the last set of connections are all destroyed.
2799TEST_F(PortTest, TestPortTimeoutAfterNewConnectionCreatedAndDestroyed) {
Honghai Zhangb5db1ec2016-07-28 13:23:05 -07002800 rtc::ScopedFakeClock clock;
Honghai Zhanga74363c2016-07-28 18:06:15 -07002801 int timeout_delay = 100;
Steve Anton11358fe2018-10-09 15:39:19 -07002802 auto port1 = CreateUdpPort(kLocalAddr1);
2803 ConnectToSignalDestroyed(port1.get());
Honghai Zhanga74363c2016-07-28 18:06:15 -07002804 port1->set_timeout_delay(timeout_delay); // milliseconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002805 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2806 port1->SetIceTiebreaker(kTiebreaker1);
2807
Steve Anton11358fe2018-10-09 15:39:19 -07002808 auto port2 = CreateUdpPort(kLocalAddr2);
2809 ConnectToSignalDestroyed(port2.get());
Honghai Zhanga74363c2016-07-28 18:06:15 -07002810 port2->set_timeout_delay(timeout_delay); // milliseconds
2811
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002812 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2813 port2->SetIceTiebreaker(kTiebreaker2);
2814
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002815 // Set up channels and ensure both ports will be deleted.
Steve Anton11358fe2018-10-09 15:39:19 -07002816 TestChannel ch1(std::move(port1));
2817 TestChannel ch2(std::move(port2));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002818
2819 // Simulate a connection that succeeds, and then is destroyed.
Guo-wei Shiehbe508a12015-04-06 12:48:47 -07002820 StartConnectAndStopChannels(&ch1, &ch2);
Honghai Zhanga74363c2016-07-28 18:06:15 -07002821 SIMULATED_WAIT(ports_destroyed() > 0, 80, clock);
2822 EXPECT_EQ(0, ports_destroyed());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002823
Honghai Zhanga74363c2016-07-28 18:06:15 -07002824 // Start the second set of connection and destroy them.
2825 ch1.CreateConnection(GetCandidate(ch2.port()));
Honghai Zhangb5db1ec2016-07-28 13:23:05 -07002826 ch2.CreateConnection(GetCandidate(ch1.port()));
Honghai Zhanga74363c2016-07-28 18:06:15 -07002827 ch1.Stop();
Honghai Zhangb5db1ec2016-07-28 13:23:05 -07002828 ch2.Stop();
Honghai Zhangb5db1ec2016-07-28 13:23:05 -07002829
Honghai Zhanga74363c2016-07-28 18:06:15 -07002830 SIMULATED_WAIT(ports_destroyed() > 0, 80, clock);
2831 EXPECT_EQ(0, ports_destroyed());
2832
2833 // The ports on both sides should be destroyed after timeout.
2834 EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 30, clock);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00002835}
honghaizd0b31432015-09-30 12:42:17 -07002836
Honghai Zhanga74363c2016-07-28 18:06:15 -07002837// This test case verifies that neither the controlling port nor the controlled
2838// port will time out after connectivity is lost if they are marked as "keep
2839// alive until pruned". They will time out after they are pruned.
2840TEST_F(PortTest, TestPortNotTimeoutUntilPruned) {
2841 rtc::ScopedFakeClock clock;
2842 int timeout_delay = 100;
Steve Anton11358fe2018-10-09 15:39:19 -07002843 auto port1 = CreateUdpPort(kLocalAddr1);
2844 ConnectToSignalDestroyed(port1.get());
Honghai Zhanga74363c2016-07-28 18:06:15 -07002845 port1->set_timeout_delay(timeout_delay); // milliseconds
honghaizd0b31432015-09-30 12:42:17 -07002846 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2847 port1->SetIceTiebreaker(kTiebreaker1);
2848
Steve Anton11358fe2018-10-09 15:39:19 -07002849 auto port2 = CreateUdpPort(kLocalAddr2);
2850 ConnectToSignalDestroyed(port2.get());
Honghai Zhanga74363c2016-07-28 18:06:15 -07002851 port2->set_timeout_delay(timeout_delay); // milliseconds
honghaizd0b31432015-09-30 12:42:17 -07002852 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2853 port2->SetIceTiebreaker(kTiebreaker2);
honghaizd0b31432015-09-30 12:42:17 -07002854 // The connection must not be destroyed before a connection is attempted.
Honghai Zhanga74363c2016-07-28 18:06:15 -07002855 EXPECT_EQ(0, ports_destroyed());
honghaizd0b31432015-09-30 12:42:17 -07002856
2857 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2858 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2859
Honghai Zhanga74363c2016-07-28 18:06:15 -07002860 // Set up channels and keep the port alive.
Steve Anton11358fe2018-10-09 15:39:19 -07002861 TestChannel ch1(std::move(port1));
2862 TestChannel ch2(std::move(port2));
Honghai Zhanga74363c2016-07-28 18:06:15 -07002863 // Simulate a connection that succeeds, and then is destroyed. But ports
2864 // are kept alive. Ports won't be destroyed.
honghaizd0b31432015-09-30 12:42:17 -07002865 StartConnectAndStopChannels(&ch1, &ch2);
Steve Anton11358fe2018-10-09 15:39:19 -07002866 ch1.port()->KeepAliveUntilPruned();
2867 ch2.port()->KeepAliveUntilPruned();
Honghai Zhanga74363c2016-07-28 18:06:15 -07002868 SIMULATED_WAIT(ports_destroyed() > 0, 150, clock);
2869 EXPECT_EQ(0, ports_destroyed());
honghaizd0b31432015-09-30 12:42:17 -07002870
Honghai Zhanga74363c2016-07-28 18:06:15 -07002871 // If they are pruned now, they will be destroyed right away.
Steve Anton11358fe2018-10-09 15:39:19 -07002872 ch1.port()->Prune();
2873 ch2.port()->Prune();
Honghai Zhanga74363c2016-07-28 18:06:15 -07002874 // The ports on both sides should be destroyed after timeout.
2875 EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 1, clock);
honghaizd0b31432015-09-30 12:42:17 -07002876}
Honghai Zhangf9945b22015-12-15 12:20:13 -08002877
2878TEST_F(PortTest, TestSupportsProtocol) {
Steve Anton11358fe2018-10-09 15:39:19 -07002879 auto udp_port = CreateUdpPort(kLocalAddr1);
Honghai Zhangf9945b22015-12-15 12:20:13 -08002880 EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME));
2881 EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME));
2882
Steve Anton11358fe2018-10-09 15:39:19 -07002883 auto stun_port = CreateStunPort(kLocalAddr1, nat_socket_factory1());
Honghai Zhangf9945b22015-12-15 12:20:13 -08002884 EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME));
2885 EXPECT_FALSE(stun_port->SupportsProtocol(TCP_PROTOCOL_NAME));
2886
Steve Anton11358fe2018-10-09 15:39:19 -07002887 auto tcp_port = CreateTcpPort(kLocalAddr1);
Honghai Zhangf9945b22015-12-15 12:20:13 -08002888 EXPECT_TRUE(tcp_port->SupportsProtocol(TCP_PROTOCOL_NAME));
2889 EXPECT_TRUE(tcp_port->SupportsProtocol(SSLTCP_PROTOCOL_NAME));
2890 EXPECT_FALSE(tcp_port->SupportsProtocol(UDP_PROTOCOL_NAME));
2891
Steve Anton11358fe2018-10-09 15:39:19 -07002892 auto turn_port =
2893 CreateTurnPort(kLocalAddr1, nat_socket_factory1(), PROTO_UDP, PROTO_UDP);
Honghai Zhangf9945b22015-12-15 12:20:13 -08002894 EXPECT_TRUE(turn_port->SupportsProtocol(UDP_PROTOCOL_NAME));
2895 EXPECT_FALSE(turn_port->SupportsProtocol(TCP_PROTOCOL_NAME));
2896}
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002897
2898// Test that SetIceParameters updates the component, ufrag and password
2899// on both the port itself and its candidates.
2900TEST_F(PortTest, TestSetIceParameters) {
Steve Anton11358fe2018-10-09 15:39:19 -07002901 auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1");
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002902 port->PrepareAddress();
2903 EXPECT_EQ(1UL, port->Candidates().size());
2904 port->SetIceParameters(1, "ufrag2", "password2");
2905 EXPECT_EQ(1, port->component());
2906 EXPECT_EQ("ufrag2", port->username_fragment());
2907 EXPECT_EQ("password2", port->password());
2908 const Candidate& candidate = port->Candidates()[0];
2909 EXPECT_EQ(1, candidate.component());
2910 EXPECT_EQ("ufrag2", candidate.username());
2911 EXPECT_EQ("password2", candidate.password());
2912}
honghaiz36f50e82016-06-01 15:57:03 -07002913
2914TEST_F(PortTest, TestAddConnectionWithSameAddress) {
Steve Anton11358fe2018-10-09 15:39:19 -07002915 auto port = CreateTestPort(kLocalAddr1, "ufrag1", "password1");
honghaiz36f50e82016-06-01 15:57:03 -07002916 port->PrepareAddress();
2917 EXPECT_EQ(1u, port->Candidates().size());
2918 rtc::SocketAddress address("1.1.1.1", 5000);
2919 cricket::Candidate candidate(1, "udp", address, 0, "", "", "relay", 0, "");
2920 cricket::Connection* conn1 =
2921 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE);
2922 cricket::Connection* conn_in_use = port->GetConnection(address);
2923 EXPECT_EQ(conn1, conn_in_use);
2924 EXPECT_EQ(0u, conn_in_use->remote_candidate().generation());
2925
2926 // Creating with a candidate with the same address again will get us a
2927 // different connection with the new candidate.
2928 candidate.set_generation(2);
2929 cricket::Connection* conn2 =
2930 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE);
2931 EXPECT_NE(conn1, conn2);
2932 conn_in_use = port->GetConnection(address);
2933 EXPECT_EQ(conn2, conn_in_use);
2934 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation());
2935
2936 // Make sure the new connection was not deleted.
2937 rtc::Thread::Current()->ProcessMessages(300);
2938 EXPECT_TRUE(port->GetConnection(address) != nullptr);
2939}
Steve Antonbabf9172017-11-29 10:19:02 -08002940
2941} // namespace cricket