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