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