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