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