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