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