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