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