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