1 /* 2 * libjingle 3 * Copyright 2012, 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 #if defined(POSIX) 28 #include <dirent.h> 29 #endif 30 31 #include "talk/p2p/base/basicpacketsocketfactory.h" 32 #include "talk/p2p/base/constants.h" 33 #include "talk/p2p/base/tcpport.h" 34 #include "talk/p2p/base/testturnserver.h" 35 #include "talk/p2p/base/turnport.h" 36 #include "talk/p2p/base/udpport.h" 37 #include "webrtc/base/asynctcpsocket.h" 38 #include "webrtc/base/buffer.h" 39 #include "webrtc/base/dscp.h" 40 #include "webrtc/base/firewallsocketserver.h" 41 #include "webrtc/base/gunit.h" 42 #include "webrtc/base/helpers.h" 43 #include "webrtc/base/logging.h" 44 #include "webrtc/base/physicalsocketserver.h" 45 #include "webrtc/base/scoped_ptr.h" 46 #include "webrtc/base/socketaddress.h" 47 #include "webrtc/base/ssladapter.h" 48 #include "webrtc/base/thread.h" 49 #include "webrtc/base/virtualsocketserver.h" 50 51 using rtc::SocketAddress; 52 using cricket::Connection; 53 using cricket::Port; 54 using cricket::PortInterface; 55 using cricket::TurnPort; 56 using cricket::UDPPort; 57 58 static const SocketAddress kLocalAddr1("11.11.11.11", 0); 59 static const SocketAddress kLocalAddr2("22.22.22.22", 0); 60 static const SocketAddress kLocalIPv6Addr( 61 "2401:fa00:4:1000:be30:5bff:fee5:c3", 0); 62 static const SocketAddress kTurnUdpIntAddr("99.99.99.3", 63 cricket::TURN_SERVER_PORT); 64 static const SocketAddress kTurnTcpIntAddr("99.99.99.4", 65 cricket::TURN_SERVER_PORT); 66 static const SocketAddress kTurnUdpExtAddr("99.99.99.5", 0); 67 static const SocketAddress kTurnAlternateUdpIntAddr( 68 "99.99.99.6", cricket::TURN_SERVER_PORT); 69 static const SocketAddress kTurnUdpIPv6IntAddr( 70 "2400:4030:1:2c00:be30:abcd:efab:cdef", cricket::TURN_SERVER_PORT); 71 static const SocketAddress kTurnUdpIPv6ExtAddr( 72 "2620:0:1000:1b03:2e41:38ff:fea6:f2a4", 0); 73 74 static const char kIceUfrag1[] = "TESTICEUFRAG0001"; 75 static const char kIceUfrag2[] = "TESTICEUFRAG0002"; 76 static const char kIcePwd1[] = "TESTICEPWD00000000000001"; 77 static const char kIcePwd2[] = "TESTICEPWD00000000000002"; 78 static const char kTurnUsername[] = "test"; 79 static const char kTurnPassword[] = "test"; 80 static const unsigned int kTimeout = 1000; 81 82 static const cricket::ProtocolAddress kTurnUdpProtoAddr( 83 kTurnUdpIntAddr, cricket::PROTO_UDP); 84 static const cricket::ProtocolAddress kTurnTcpProtoAddr( 85 kTurnTcpIntAddr, cricket::PROTO_TCP); 86 static const cricket::ProtocolAddress kTurnUdpIPv6ProtoAddr( 87 kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 88 89 static const unsigned int MSG_TESTFINISH = 0; 90 91 #if defined(LINUX) 92 static int GetFDCount() { 93 struct dirent *dp; 94 int fd_count = 0; 95 DIR *dir = opendir("/proc/self/fd/"); 96 while ((dp = readdir(dir)) != NULL) { 97 if (dp->d_name[0] == '.') 98 continue; 99 ++fd_count; 100 } 101 closedir(dir); 102 return fd_count; 103 } 104 #endif 105 106 class TurnPortTest : public testing::Test, 107 public sigslot::has_slots<>, 108 public rtc::MessageHandler { 109 public: 110 TurnPortTest() 111 : main_(rtc::Thread::Current()), 112 pss_(new rtc::PhysicalSocketServer), 113 ss_(new rtc::VirtualSocketServer(pss_.get())), 114 ss_scope_(ss_.get()), 115 network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), 116 socket_factory_(rtc::Thread::Current()), 117 turn_server_(main_, kTurnUdpIntAddr, kTurnUdpExtAddr), 118 turn_ready_(false), 119 turn_error_(false), 120 turn_unknown_address_(false), 121 turn_create_permission_success_(false), 122 udp_ready_(false), 123 test_finish_(false) { 124 network_.AddIP(rtc::IPAddress(INADDR_ANY)); 125 } 126 127 static void SetUpTestCase() { 128 rtc::InitializeSSL(); 129 } 130 131 static void TearDownTestCase() { 132 rtc::CleanupSSL(); 133 } 134 135 virtual void OnMessage(rtc::Message* msg) { 136 ASSERT(msg->message_id == MSG_TESTFINISH); 137 if (msg->message_id == MSG_TESTFINISH) 138 test_finish_ = true; 139 } 140 141 void OnTurnPortComplete(Port* port) { 142 turn_ready_ = true; 143 } 144 void OnTurnPortError(Port* port) { 145 turn_error_ = true; 146 } 147 void OnTurnUnknownAddress(PortInterface* port, const SocketAddress& addr, 148 cricket::ProtocolType proto, 149 cricket::IceMessage* msg, const std::string& rf, 150 bool /*port_muxed*/) { 151 turn_unknown_address_ = true; 152 } 153 void OnTurnCreatePermissionResult(TurnPort* port, const SocketAddress& addr, 154 int code) { 155 // Ignoring the address. 156 if (code == 0) { 157 turn_create_permission_success_ = true; 158 } 159 } 160 void OnTurnReadPacket(Connection* conn, const char* data, size_t size, 161 const rtc::PacketTime& packet_time) { 162 turn_packets_.push_back(rtc::Buffer(data, size)); 163 } 164 void OnUdpPortComplete(Port* port) { 165 udp_ready_ = true; 166 } 167 void OnUdpReadPacket(Connection* conn, const char* data, size_t size, 168 const rtc::PacketTime& packet_time) { 169 udp_packets_.push_back(rtc::Buffer(data, size)); 170 } 171 void OnSocketReadPacket(rtc::AsyncPacketSocket* socket, 172 const char* data, size_t size, 173 const rtc::SocketAddress& remote_addr, 174 const rtc::PacketTime& packet_time) { 175 turn_port_->HandleIncomingPacket(socket, data, size, remote_addr, 176 packet_time); 177 } 178 rtc::AsyncSocket* CreateServerSocket(const SocketAddress addr) { 179 rtc::AsyncSocket* socket = ss_->CreateAsyncSocket(SOCK_STREAM); 180 EXPECT_GE(socket->Bind(addr), 0); 181 EXPECT_GE(socket->Listen(5), 0); 182 return socket; 183 } 184 185 void CreateTurnPort(const std::string& username, 186 const std::string& password, 187 const cricket::ProtocolAddress& server_address) { 188 CreateTurnPort(kLocalAddr1, username, password, server_address); 189 } 190 void CreateTurnPort(const rtc::SocketAddress& local_address, 191 const std::string& username, 192 const std::string& password, 193 const cricket::ProtocolAddress& server_address) { 194 cricket::RelayCredentials credentials(username, password); 195 turn_port_.reset(TurnPort::Create(main_, &socket_factory_, &network_, 196 local_address.ipaddr(), 0, 0, 197 kIceUfrag1, kIcePwd1, 198 server_address, credentials, 0)); 199 // Set ICE protocol type to ICEPROTO_RFC5245, as port by default will be 200 // in Hybrid mode. Protocol type is necessary to send correct type STUN ping 201 // messages. 202 // This TURN port will be the controlling. 203 turn_port_->SetIceProtocolType(cricket::ICEPROTO_RFC5245); 204 turn_port_->SetIceRole(cricket::ICEROLE_CONTROLLING); 205 ConnectSignals(); 206 } 207 208 void CreateSharedTurnPort(const std::string& username, 209 const std::string& password, 210 const cricket::ProtocolAddress& server_address) { 211 ASSERT(server_address.proto == cricket::PROTO_UDP); 212 213 if (!socket_) { 214 socket_.reset(socket_factory_.CreateUdpSocket( 215 rtc::SocketAddress(kLocalAddr1.ipaddr(), 0), 0, 0)); 216 ASSERT_TRUE(socket_ != NULL); 217 socket_->SignalReadPacket.connect( 218 this, &TurnPortTest::OnSocketReadPacket); 219 } 220 221 cricket::RelayCredentials credentials(username, password); 222 turn_port_.reset(cricket::TurnPort::Create( 223 main_, &socket_factory_, &network_, socket_.get(), 224 kIceUfrag1, kIcePwd1, server_address, credentials, 0)); 225 // Set ICE protocol type to ICEPROTO_RFC5245, as port by default will be 226 // in Hybrid mode. Protocol type is necessary to send correct type STUN ping 227 // messages. 228 // This TURN port will be the controlling. 229 turn_port_->SetIceProtocolType(cricket::ICEPROTO_RFC5245); 230 turn_port_->SetIceRole(cricket::ICEROLE_CONTROLLING); 231 ConnectSignals(); 232 } 233 234 void ConnectSignals() { 235 turn_port_->SignalPortComplete.connect(this, 236 &TurnPortTest::OnTurnPortComplete); 237 turn_port_->SignalPortError.connect(this, 238 &TurnPortTest::OnTurnPortError); 239 turn_port_->SignalUnknownAddress.connect(this, 240 &TurnPortTest::OnTurnUnknownAddress); 241 turn_port_->SignalCreatePermissionResult.connect(this, 242 &TurnPortTest::OnTurnCreatePermissionResult); 243 } 244 void CreateUdpPort() { 245 udp_port_.reset(UDPPort::Create(main_, &socket_factory_, &network_, 246 kLocalAddr2.ipaddr(), 0, 0, 247 kIceUfrag2, kIcePwd2)); 248 // Set protocol type to RFC5245, as turn port is also in same mode. 249 // UDP port will be controlled. 250 udp_port_->SetIceProtocolType(cricket::ICEPROTO_RFC5245); 251 udp_port_->SetIceRole(cricket::ICEROLE_CONTROLLED); 252 udp_port_->SignalPortComplete.connect( 253 this, &TurnPortTest::OnUdpPortComplete); 254 } 255 256 void TestTurnConnection() { 257 // Create ports and prepare addresses. 258 ASSERT_TRUE(turn_port_ != NULL); 259 turn_port_->PrepareAddress(); 260 ASSERT_TRUE_WAIT(turn_ready_, kTimeout); 261 CreateUdpPort(); 262 udp_port_->PrepareAddress(); 263 ASSERT_TRUE_WAIT(udp_ready_, kTimeout); 264 265 // Send ping from UDP to TURN. 266 Connection* conn1 = udp_port_->CreateConnection( 267 turn_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 268 ASSERT_TRUE(conn1 != NULL); 269 conn1->Ping(0); 270 WAIT(!turn_unknown_address_, kTimeout); 271 EXPECT_FALSE(turn_unknown_address_); 272 EXPECT_EQ(Connection::STATE_READ_INIT, conn1->read_state()); 273 EXPECT_EQ(Connection::STATE_WRITE_INIT, conn1->write_state()); 274 275 // Send ping from TURN to UDP. 276 Connection* conn2 = turn_port_->CreateConnection( 277 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 278 ASSERT_TRUE(conn2 != NULL); 279 ASSERT_TRUE_WAIT(turn_create_permission_success_, kTimeout); 280 conn2->Ping(0); 281 282 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn2->write_state(), kTimeout); 283 EXPECT_EQ(Connection::STATE_READABLE, conn1->read_state()); 284 EXPECT_EQ(Connection::STATE_READ_INIT, conn2->read_state()); 285 EXPECT_EQ(Connection::STATE_WRITE_INIT, conn1->write_state()); 286 287 // Send another ping from UDP to TURN. 288 conn1->Ping(0); 289 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn1->write_state(), kTimeout); 290 EXPECT_EQ(Connection::STATE_READABLE, conn2->read_state()); 291 } 292 293 void TestTurnSendData() { 294 turn_port_->PrepareAddress(); 295 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 296 CreateUdpPort(); 297 udp_port_->PrepareAddress(); 298 EXPECT_TRUE_WAIT(udp_ready_, kTimeout); 299 // Create connections and send pings. 300 Connection* conn1 = turn_port_->CreateConnection( 301 udp_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 302 Connection* conn2 = udp_port_->CreateConnection( 303 turn_port_->Candidates()[0], Port::ORIGIN_MESSAGE); 304 ASSERT_TRUE(conn1 != NULL); 305 ASSERT_TRUE(conn2 != NULL); 306 conn1->SignalReadPacket.connect(static_cast<TurnPortTest*>(this), 307 &TurnPortTest::OnTurnReadPacket); 308 conn2->SignalReadPacket.connect(static_cast<TurnPortTest*>(this), 309 &TurnPortTest::OnUdpReadPacket); 310 conn1->Ping(0); 311 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn1->write_state(), kTimeout); 312 conn2->Ping(0); 313 EXPECT_EQ_WAIT(Connection::STATE_WRITABLE, conn2->write_state(), kTimeout); 314 315 // Send some data. 316 size_t num_packets = 256; 317 for (size_t i = 0; i < num_packets; ++i) { 318 unsigned char buf[256] = { 0 }; 319 for (size_t j = 0; j < i + 1; ++j) { 320 buf[j] = 0xFF - static_cast<unsigned char>(j); 321 } 322 conn1->Send(buf, i + 1, options); 323 conn2->Send(buf, i + 1, options); 324 main_->ProcessMessages(0); 325 } 326 327 // Check the data. 328 ASSERT_EQ_WAIT(num_packets, turn_packets_.size(), kTimeout); 329 ASSERT_EQ_WAIT(num_packets, udp_packets_.size(), kTimeout); 330 for (size_t i = 0; i < num_packets; ++i) { 331 EXPECT_EQ(i + 1, turn_packets_[i].length()); 332 EXPECT_EQ(i + 1, udp_packets_[i].length()); 333 EXPECT_EQ(turn_packets_[i], udp_packets_[i]); 334 } 335 } 336 337 protected: 338 rtc::Thread* main_; 339 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; 340 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_; 341 rtc::SocketServerScope ss_scope_; 342 rtc::Network network_; 343 rtc::BasicPacketSocketFactory socket_factory_; 344 rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_; 345 cricket::TestTurnServer turn_server_; 346 rtc::scoped_ptr<TurnPort> turn_port_; 347 rtc::scoped_ptr<UDPPort> udp_port_; 348 bool turn_ready_; 349 bool turn_error_; 350 bool turn_unknown_address_; 351 bool turn_create_permission_success_; 352 bool udp_ready_; 353 bool test_finish_; 354 std::vector<rtc::Buffer> turn_packets_; 355 std::vector<rtc::Buffer> udp_packets_; 356 rtc::PacketOptions options; 357 }; 358 359 // Do a normal TURN allocation. 360 TEST_F(TurnPortTest, TestTurnAllocate) { 361 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 362 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10*1024)); 363 turn_port_->PrepareAddress(); 364 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 365 ASSERT_EQ(1U, turn_port_->Candidates().size()); 366 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 367 turn_port_->Candidates()[0].address().ipaddr()); 368 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 369 } 370 371 // Testing a normal UDP allocation using TCP connection. 372 TEST_F(TurnPortTest, TestTurnTcpAllocate) { 373 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 374 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 375 EXPECT_EQ(0, turn_port_->SetOption(rtc::Socket::OPT_SNDBUF, 10*1024)); 376 turn_port_->PrepareAddress(); 377 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 378 ASSERT_EQ(1U, turn_port_->Candidates().size()); 379 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 380 turn_port_->Candidates()[0].address().ipaddr()); 381 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 382 } 383 384 // Testing turn port will attempt to create TCP socket on address resolution 385 // failure. 386 TEST_F(TurnPortTest, DISABLED_TestTurnTcpOnAddressResolveFailure) { 387 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 388 CreateTurnPort(kTurnUsername, kTurnPassword, cricket::ProtocolAddress( 389 rtc::SocketAddress("www.webrtc-blah-blah.com", 3478), 390 cricket::PROTO_TCP)); 391 turn_port_->PrepareAddress(); 392 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 393 // As VSS doesn't provide a DNS resolution, name resolve will fail. TurnPort 394 // will proceed in creating a TCP socket which will fail as there is no 395 // server on the above domain and error will be set to SOCKET_ERROR. 396 EXPECT_EQ(SOCKET_ERROR, turn_port_->error()); 397 } 398 399 // In case of UDP on address resolve failure, TurnPort will not create socket 400 // and return allocate failure. 401 TEST_F(TurnPortTest, DISABLED_TestTurnUdpOnAdressResolveFailure) { 402 CreateTurnPort(kTurnUsername, kTurnPassword, cricket::ProtocolAddress( 403 rtc::SocketAddress("www.webrtc-blah-blah.com", 3478), 404 cricket::PROTO_UDP)); 405 turn_port_->PrepareAddress(); 406 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 407 // Error from turn port will not be socket error. 408 EXPECT_NE(SOCKET_ERROR, turn_port_->error()); 409 } 410 411 // Try to do a TURN allocation with an invalid password. 412 TEST_F(TurnPortTest, TestTurnAllocateBadPassword) { 413 CreateTurnPort(kTurnUsername, "bad", kTurnUdpProtoAddr); 414 turn_port_->PrepareAddress(); 415 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 416 ASSERT_EQ(0U, turn_port_->Candidates().size()); 417 } 418 419 // Tests that a new local address is created after 420 // STUN_ERROR_ALLOCATION_MISMATCH. 421 TEST_F(TurnPortTest, TestTurnAllocateMismatch) { 422 // Do a normal allocation first. 423 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 424 turn_port_->PrepareAddress(); 425 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 426 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 427 428 // Forces the socket server to assign the same port. 429 ss_->SetNextPortForTesting(first_addr.port()); 430 431 turn_ready_ = false; 432 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 433 turn_port_->PrepareAddress(); 434 435 // Verifies that the new port has the same address. 436 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress()); 437 438 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 439 440 // Verifies that the new port has a different address now. 441 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress()); 442 } 443 444 // Tests that a shared-socket-TurnPort creates its own socket after 445 // STUN_ERROR_ALLOCATION_MISMATCH. 446 TEST_F(TurnPortTest, TestSharedSocketAllocateMismatch) { 447 // Do a normal allocation first. 448 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 449 turn_port_->PrepareAddress(); 450 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 451 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 452 453 turn_ready_ = false; 454 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 455 456 // Verifies that the new port has the same address. 457 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress()); 458 EXPECT_TRUE(turn_port_->SharedSocket()); 459 460 turn_port_->PrepareAddress(); 461 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 462 463 // Verifies that the new port has a different address now. 464 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress()); 465 EXPECT_FALSE(turn_port_->SharedSocket()); 466 } 467 468 TEST_F(TurnPortTest, TestTurnTcpAllocateMismatch) { 469 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 470 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 471 472 // Do a normal allocation first. 473 turn_port_->PrepareAddress(); 474 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 475 rtc::SocketAddress first_addr(turn_port_->socket()->GetLocalAddress()); 476 477 // Forces the socket server to assign the same port. 478 ss_->SetNextPortForTesting(first_addr.port()); 479 480 turn_ready_ = false; 481 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 482 turn_port_->PrepareAddress(); 483 484 // Verifies that the new port has the same address. 485 EXPECT_EQ(first_addr, turn_port_->socket()->GetLocalAddress()); 486 487 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 488 489 // Verifies that the new port has a different address now. 490 EXPECT_NE(first_addr, turn_port_->socket()->GetLocalAddress()); 491 } 492 493 // Do a TURN allocation and try to send a packet to it from the outside. 494 // The packet should be dropped. Then, try to send a packet from TURN to the 495 // outside. It should reach its destination. Finally, try again from the 496 // outside. It should now work as well. 497 TEST_F(TurnPortTest, TestTurnConnection) { 498 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 499 TestTurnConnection(); 500 } 501 502 // Similar to above, except that this test will use the shared socket. 503 TEST_F(TurnPortTest, TestTurnConnectionUsingSharedSocket) { 504 CreateSharedTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 505 TestTurnConnection(); 506 } 507 508 // Test that we can establish a TCP connection with TURN server. 509 TEST_F(TurnPortTest, TestTurnTcpConnection) { 510 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 511 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 512 TestTurnConnection(); 513 } 514 515 // Test that we fail to create a connection when we want to use TLS over TCP. 516 // This test should be removed once we have TLS support. 517 TEST_F(TurnPortTest, TestTurnTlsTcpConnectionFails) { 518 cricket::ProtocolAddress secure_addr(kTurnTcpProtoAddr.address, 519 kTurnTcpProtoAddr.proto, 520 true); 521 CreateTurnPort(kTurnUsername, kTurnPassword, secure_addr); 522 turn_port_->PrepareAddress(); 523 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 524 ASSERT_EQ(0U, turn_port_->Candidates().size()); 525 } 526 527 // Test try-alternate-server feature. 528 TEST_F(TurnPortTest, TestTurnAlternateServer) { 529 std::vector<rtc::SocketAddress> redirect_addresses; 530 redirect_addresses.push_back(kTurnAlternateUdpIntAddr); 531 532 cricket::TestTurnRedirector redirector(redirect_addresses); 533 turn_server_.AddInternalSocket(kTurnAlternateUdpIntAddr, 534 cricket::PROTO_UDP); 535 turn_server_.set_redirect_hook(&redirector); 536 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 537 538 // Retrieve the address before we run the state machine. 539 const SocketAddress old_addr = turn_port_->server_address().address; 540 541 turn_port_->PrepareAddress(); 542 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 543 // Retrieve the address again, the turn port's address should be 544 // changed. 545 const SocketAddress new_addr = turn_port_->server_address().address; 546 EXPECT_NE(old_addr, new_addr); 547 ASSERT_EQ(1U, turn_port_->Candidates().size()); 548 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 549 turn_port_->Candidates()[0].address().ipaddr()); 550 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 551 } 552 553 // Test that we fail when we redirect to an address different from 554 // current IP family. 555 TEST_F(TurnPortTest, TestTurnAlternateServerV4toV6) { 556 std::vector<rtc::SocketAddress> redirect_addresses; 557 redirect_addresses.push_back(kTurnUdpIPv6IntAddr); 558 559 cricket::TestTurnRedirector redirector(redirect_addresses); 560 turn_server_.AddInternalSocket(kTurnAlternateUdpIntAddr, 561 cricket::PROTO_UDP); 562 turn_server_.set_redirect_hook(&redirector); 563 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 564 turn_port_->PrepareAddress(); 565 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 566 } 567 568 // Test that we fail to handle alternate-server response over TCP protocol. 569 TEST_F(TurnPortTest, TestTurnAlternateServerTcp) { 570 std::vector<rtc::SocketAddress> redirect_addresses; 571 redirect_addresses.push_back(kTurnAlternateUdpIntAddr); 572 573 cricket::TestTurnRedirector redirector(redirect_addresses); 574 turn_server_.set_redirect_hook(&redirector); 575 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 576 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 577 578 turn_server_.AddInternalSocket(kTurnAlternateUdpIntAddr, cricket::PROTO_TCP); 579 turn_port_->PrepareAddress(); 580 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 581 } 582 583 // Test try-alternate-server catches the case of pingpong. 584 TEST_F(TurnPortTest, TestTurnAlternateServerPingPong) { 585 std::vector<rtc::SocketAddress> redirect_addresses; 586 redirect_addresses.push_back(kTurnAlternateUdpIntAddr); 587 redirect_addresses.push_back(kTurnUdpIntAddr); 588 589 cricket::TestTurnRedirector redirector(redirect_addresses); 590 591 turn_server_.AddInternalSocket(kTurnAlternateUdpIntAddr, 592 cricket::PROTO_UDP); 593 turn_server_.set_redirect_hook(&redirector); 594 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 595 596 turn_port_->PrepareAddress(); 597 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 598 ASSERT_EQ(0U, turn_port_->Candidates().size()); 599 rtc::SocketAddress address; 600 // Verify that we have exhausted all alternate servers instead of 601 // failure caused by other errors. 602 EXPECT_FALSE(redirector.ShouldRedirect(address, &address)); 603 } 604 605 // Test try-alternate-server catch the case of repeated server. 606 TEST_F(TurnPortTest, TestTurnAlternateServerDetectRepetition) { 607 std::vector<rtc::SocketAddress> redirect_addresses; 608 redirect_addresses.push_back(kTurnAlternateUdpIntAddr); 609 redirect_addresses.push_back(kTurnAlternateUdpIntAddr); 610 611 cricket::TestTurnRedirector redirector(redirect_addresses); 612 613 turn_server_.AddInternalSocket(kTurnAlternateUdpIntAddr, 614 cricket::PROTO_UDP); 615 turn_server_.set_redirect_hook(&redirector); 616 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 617 618 turn_port_->PrepareAddress(); 619 EXPECT_TRUE_WAIT(turn_error_, kTimeout); 620 ASSERT_EQ(0U, turn_port_->Candidates().size()); 621 } 622 623 624 // Run TurnConnectionTest with one-time-use nonce feature. 625 // Here server will send a 438 STALE_NONCE error message for 626 // every TURN transaction. 627 TEST_F(TurnPortTest, TestTurnConnectionUsingOTUNonce) { 628 turn_server_.set_enable_otu_nonce(true); 629 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 630 TestTurnConnection(); 631 } 632 633 // Do a TURN allocation, establish a UDP connection, and send some data. 634 TEST_F(TurnPortTest, TestTurnSendDataTurnUdpToUdp) { 635 // Create ports and prepare addresses. 636 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnUdpProtoAddr); 637 TestTurnSendData(); 638 } 639 640 // Do a TURN allocation, establish a TCP connection, and send some data. 641 TEST_F(TurnPortTest, TestTurnSendDataTurnTcpToUdp) { 642 turn_server_.AddInternalSocket(kTurnTcpIntAddr, cricket::PROTO_TCP); 643 // Create ports and prepare addresses. 644 CreateTurnPort(kTurnUsername, kTurnPassword, kTurnTcpProtoAddr); 645 TestTurnSendData(); 646 } 647 648 // Test TURN fails to make a connection from IPv6 address to a server which has 649 // IPv4 address. 650 TEST_F(TurnPortTest, TestTurnLocalIPv6AddressServerIPv4) { 651 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 652 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 653 kTurnUdpProtoAddr); 654 turn_port_->PrepareAddress(); 655 ASSERT_TRUE_WAIT(turn_error_, kTimeout); 656 EXPECT_TRUE(turn_port_->Candidates().empty()); 657 } 658 659 // Test TURN make a connection from IPv6 address to a server which has 660 // IPv6 intenal address. But in this test external address is a IPv4 address, 661 // hence allocated address will be a IPv4 address. 662 TEST_F(TurnPortTest, TestTurnLocalIPv6AddressServerIPv6ExtenalIPv4) { 663 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 664 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 665 kTurnUdpIPv6ProtoAddr); 666 turn_port_->PrepareAddress(); 667 EXPECT_TRUE_WAIT(turn_ready_, kTimeout); 668 ASSERT_EQ(1U, turn_port_->Candidates().size()); 669 EXPECT_EQ(kTurnUdpExtAddr.ipaddr(), 670 turn_port_->Candidates()[0].address().ipaddr()); 671 EXPECT_NE(0, turn_port_->Candidates()[0].address().port()); 672 } 673 674 // This test verifies any FD's are not leaked after TurnPort is destroyed. 675 // https://code.google.com/p/webrtc/issues/detail?id=2651 676 #if defined(LINUX) 677 TEST_F(TurnPortTest, TestResolverShutdown) { 678 turn_server_.AddInternalSocket(kTurnUdpIPv6IntAddr, cricket::PROTO_UDP); 679 int last_fd_count = GetFDCount(); 680 // Need to supply unresolved address to kick off resolver. 681 CreateTurnPort(kLocalIPv6Addr, kTurnUsername, kTurnPassword, 682 cricket::ProtocolAddress(rtc::SocketAddress( 683 "stun.l.google.com", 3478), cricket::PROTO_UDP)); 684 turn_port_->PrepareAddress(); 685 ASSERT_TRUE_WAIT(turn_error_, kTimeout); 686 EXPECT_TRUE(turn_port_->Candidates().empty()); 687 turn_port_.reset(); 688 rtc::Thread::Current()->Post(this, MSG_TESTFINISH); 689 // Waiting for above message to be processed. 690 ASSERT_TRUE_WAIT(test_finish_, kTimeout); 691 EXPECT_EQ(last_fd_count, GetFDCount()); 692 } 693 #endif 694