2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2009 Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#include "talk/p2p/base/basicpacketsocketfactory.h"
|
|
|
|
#include "talk/p2p/base/stunport.h"
|
|
|
|
#include "talk/p2p/base/teststunserver.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/gunit.h"
|
|
|
|
#include "webrtc/base/helpers.h"
|
|
|
|
#include "webrtc/base/physicalsocketserver.h"
|
|
|
|
#include "webrtc/base/scoped_ptr.h"
|
|
|
|
#include "webrtc/base/socketaddress.h"
|
|
|
|
#include "webrtc/base/ssladapter.h"
|
|
|
|
#include "webrtc/base/virtualsocketserver.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
using cricket::ServerAddresses;
|
2014-07-29 19:36:52 +02:00
|
|
|
using rtc::SocketAddress;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
static const SocketAddress kLocalAddr("127.0.0.1", 0);
|
2014-07-29 00:26:15 +02:00
|
|
|
static const SocketAddress kStunAddr1("127.0.0.1", 5000);
|
|
|
|
static const SocketAddress kStunAddr2("127.0.0.1", 4000);
|
2013-07-10 02:45:36 +02:00
|
|
|
static const SocketAddress kBadAddr("0.0.0.1", 5000);
|
|
|
|
static const SocketAddress kStunHostnameAddr("localhost", 5000);
|
|
|
|
static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000);
|
|
|
|
static const int kTimeoutMs = 10000;
|
2014-04-14 18:06:21 +02:00
|
|
|
// stun prio = 100 << 24 | 30 (IPV4) << 8 | 256 - 0
|
|
|
|
static const uint32 kStunCandidatePriority = 1677729535;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Tests connecting a StunPort to a fake STUN server (cricket::StunServer)
|
|
|
|
// TODO: Use a VirtualSocketServer here. We have to use a
|
|
|
|
// PhysicalSocketServer right now since DNS is not part of SocketServer yet.
|
|
|
|
class StunPortTest : public testing::Test,
|
|
|
|
public sigslot::has_slots<> {
|
|
|
|
public:
|
|
|
|
StunPortTest()
|
2014-07-29 19:36:52 +02:00
|
|
|
: pss_(new rtc::PhysicalSocketServer),
|
|
|
|
ss_(new rtc::VirtualSocketServer(pss_.get())),
|
2013-11-13 23:48:52 +01:00
|
|
|
ss_scope_(ss_.get()),
|
2014-07-29 19:36:52 +02:00
|
|
|
network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32),
|
|
|
|
socket_factory_(rtc::Thread::Current()),
|
2014-09-27 01:01:11 +02:00
|
|
|
stun_server_1_(cricket::TestStunServer::Create(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread::Current(), kStunAddr1)),
|
2014-09-27 01:01:11 +02:00
|
|
|
stun_server_2_(cricket::TestStunServer::Create(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread::Current(), kStunAddr2)),
|
2013-07-10 02:45:36 +02:00
|
|
|
done_(false), error_(false), stun_keepalive_delay_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const cricket::Port* port() const { return stun_port_.get(); }
|
|
|
|
bool done() const { return done_; }
|
|
|
|
bool error() const { return error_; }
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void CreateStunPort(const rtc::SocketAddress& server_addr) {
|
2014-07-29 00:26:15 +02:00
|
|
|
ServerAddresses stun_servers;
|
|
|
|
stun_servers.insert(server_addr);
|
|
|
|
CreateStunPort(stun_servers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateStunPort(const ServerAddresses& stun_servers) {
|
2013-07-10 02:45:36 +02:00
|
|
|
stun_port_.reset(cricket::StunPort::Create(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread::Current(), &socket_factory_, &network_,
|
|
|
|
kLocalAddr.ipaddr(), 0, 0, rtc::CreateRandomString(16),
|
|
|
|
rtc::CreateRandomString(22), stun_servers));
|
2013-07-10 02:45:36 +02:00
|
|
|
stun_port_->set_stun_keepalive_delay(stun_keepalive_delay_);
|
|
|
|
stun_port_->SignalPortComplete.connect(this,
|
|
|
|
&StunPortTest::OnPortComplete);
|
|
|
|
stun_port_->SignalPortError.connect(this,
|
|
|
|
&StunPortTest::OnPortError);
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void CreateSharedStunPort(const rtc::SocketAddress& server_addr) {
|
2013-08-24 01:21:25 +02:00
|
|
|
socket_.reset(socket_factory_.CreateUdpSocket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress(kLocalAddr.ipaddr(), 0), 0, 0));
|
2013-08-24 01:21:25 +02:00
|
|
|
ASSERT_TRUE(socket_ != NULL);
|
|
|
|
socket_->SignalReadPacket.connect(this, &StunPortTest::OnReadPacket);
|
|
|
|
stun_port_.reset(cricket::UDPPort::Create(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread::Current(), &socket_factory_,
|
2013-11-13 23:48:52 +01:00
|
|
|
&network_, socket_.get(),
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::CreateRandomString(16), rtc::CreateRandomString(22)));
|
2013-08-24 01:21:25 +02:00
|
|
|
ASSERT_TRUE(stun_port_ != NULL);
|
2014-07-29 00:26:15 +02:00
|
|
|
ServerAddresses stun_servers;
|
|
|
|
stun_servers.insert(server_addr);
|
|
|
|
stun_port_->set_server_addresses(stun_servers);
|
2013-08-24 01:21:25 +02:00
|
|
|
stun_port_->SignalPortComplete.connect(this,
|
|
|
|
&StunPortTest::OnPortComplete);
|
|
|
|
stun_port_->SignalPortError.connect(this,
|
|
|
|
&StunPortTest::OnPortError);
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
void PrepareAddress() {
|
|
|
|
stun_port_->PrepareAddress();
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnReadPacket(rtc::AsyncPacketSocket* socket, const char* data,
|
|
|
|
size_t size, const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time) {
|
2013-12-13 01:21:03 +01:00
|
|
|
stun_port_->HandleIncomingPacket(
|
2014-07-29 19:36:52 +02:00
|
|
|
socket, data, size, remote_addr, rtc::PacketTime());
|
2013-08-24 01:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SendData(const char* data, size_t len) {
|
|
|
|
stun_port_->HandleIncomingPacket(
|
2014-07-29 19:36:52 +02:00
|
|
|
socket_.get(), data, len, rtc::SocketAddress("22.22.22.22", 0),
|
|
|
|
rtc::PacketTime());
|
2013-08-24 01:21:25 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
protected:
|
|
|
|
static void SetUpTestCase() {
|
|
|
|
// Ensure the RNG is inited.
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::InitRandom(NULL, 0);
|
2014-07-29 00:26:15 +02:00
|
|
|
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
void OnPortComplete(cricket::Port* port) {
|
2014-07-29 00:26:15 +02:00
|
|
|
ASSERT_FALSE(done_);
|
2013-07-10 02:45:36 +02:00
|
|
|
done_ = true;
|
|
|
|
error_ = false;
|
|
|
|
}
|
|
|
|
void OnPortError(cricket::Port* port) {
|
|
|
|
done_ = true;
|
|
|
|
error_ = true;
|
|
|
|
}
|
|
|
|
void SetKeepaliveDelay(int delay) {
|
|
|
|
stun_keepalive_delay_ = delay;
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:01:11 +02:00
|
|
|
cricket::TestStunServer* stun_server_1() {
|
|
|
|
return stun_server_1_.get();
|
|
|
|
}
|
|
|
|
cricket::TestStunServer* stun_server_2() {
|
|
|
|
return stun_server_2_.get();
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
private:
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
|
|
|
|
rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
|
|
|
|
rtc::SocketServerScope ss_scope_;
|
|
|
|
rtc::Network network_;
|
|
|
|
rtc::BasicPacketSocketFactory socket_factory_;
|
|
|
|
rtc::scoped_ptr<cricket::UDPPort> stun_port_;
|
|
|
|
rtc::scoped_ptr<cricket::TestStunServer> stun_server_1_;
|
|
|
|
rtc::scoped_ptr<cricket::TestStunServer> stun_server_2_;
|
|
|
|
rtc::scoped_ptr<rtc::AsyncPacketSocket> socket_;
|
2013-07-10 02:45:36 +02:00
|
|
|
bool done_;
|
|
|
|
bool error_;
|
|
|
|
int stun_keepalive_delay_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test that we can create a STUN port
|
|
|
|
TEST_F(StunPortTest, TestBasic) {
|
2014-07-29 00:26:15 +02:00
|
|
|
CreateStunPort(kStunAddr1);
|
2013-07-10 02:45:36 +02:00
|
|
|
EXPECT_EQ("stun", port()->Type());
|
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can get an address from a STUN server.
|
|
|
|
TEST_F(StunPortTest, TestPrepareAddress) {
|
2014-07-29 00:26:15 +02:00
|
|
|
CreateStunPort(kStunAddr1);
|
2013-07-10 02:45:36 +02:00
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
|
|
|
// TODO: Add IPv6 tests here, once either physicalsocketserver supports
|
|
|
|
// IPv6, or this test is changed to use VirtualSocketServer.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we fail properly if we can't get an address.
|
|
|
|
TEST_F(StunPortTest, TestPrepareAddressFail) {
|
|
|
|
CreateStunPort(kBadAddr);
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
EXPECT_TRUE(error());
|
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we can get an address from a STUN server specified by a hostname.
|
|
|
|
TEST_F(StunPortTest, TestPrepareAddressHostname) {
|
|
|
|
CreateStunPort(kStunHostnameAddr);
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
2014-04-14 18:06:21 +02:00
|
|
|
EXPECT_EQ(kStunCandidatePriority, port()->Candidates()[0].priority());
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we handle hostname lookup failures properly.
|
|
|
|
TEST_F(StunPortTest, TestPrepareAddressHostnameFail) {
|
|
|
|
CreateStunPort(kBadHostnameAddr);
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
EXPECT_TRUE(error());
|
|
|
|
EXPECT_EQ(0U, port()->Candidates().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test verifies keepalive response messages don't result in
|
|
|
|
// additional candidate generation.
|
|
|
|
TEST_F(StunPortTest, TestKeepAliveResponse) {
|
|
|
|
SetKeepaliveDelay(500); // 500ms of keepalive delay.
|
|
|
|
CreateStunPort(kStunHostnameAddr);
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
// Waiting for 1 seond, which will allow us to process
|
|
|
|
// response for keepalive binding request. 500 ms is the keepalive delay.
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread::Current()->ProcessMessages(1000);
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
}
|
|
|
|
|
2013-08-24 01:21:25 +02:00
|
|
|
// Test that a local candidate can be generated using a shared socket.
|
|
|
|
TEST_F(StunPortTest, TestSharedSocketPrepareAddress) {
|
2014-07-29 00:26:15 +02:00
|
|
|
CreateSharedStunPort(kStunAddr1);
|
2013-08-24 01:21:25 +02:00
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that we still a get a local candidate with invalid stun server hostname.
|
|
|
|
// Also verifing that UDPPort can receive packets when stun address can't be
|
|
|
|
// resolved.
|
|
|
|
TEST_F(StunPortTest, TestSharedSocketPrepareAddressInvalidHostname) {
|
|
|
|
CreateSharedStunPort(kBadHostnameAddr);
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
ASSERT_EQ(1U, port()->Candidates().size());
|
|
|
|
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->Candidates()[0].address()));
|
|
|
|
|
|
|
|
// Send data to port after it's ready. This is to make sure, UDP port can
|
|
|
|
// handle data with unresolved stun server address.
|
|
|
|
std::string data = "some random data, sending to cricket::Port.";
|
|
|
|
SendData(data.c_str(), data.length());
|
|
|
|
// No crash is success.
|
|
|
|
}
|
2014-07-29 00:26:15 +02:00
|
|
|
|
2014-09-27 01:01:11 +02:00
|
|
|
// Test that the same address is added only once if two STUN servers are in use.
|
|
|
|
TEST_F(StunPortTest, TestNoDuplicatedAddressWithTwoStunServers) {
|
2014-07-29 00:26:15 +02:00
|
|
|
ServerAddresses stun_servers;
|
|
|
|
stun_servers.insert(kStunAddr1);
|
|
|
|
stun_servers.insert(kStunAddr2);
|
|
|
|
CreateStunPort(stun_servers);
|
|
|
|
EXPECT_EQ("stun", port()->Type());
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
2014-09-27 01:01:11 +02:00
|
|
|
EXPECT_EQ(1U, port()->Candidates().size());
|
2014-07-29 00:26:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that candidates can be allocated for multiple STUN servers, one of which
|
|
|
|
// is not reachable.
|
|
|
|
TEST_F(StunPortTest, TestMultipleStunServersWithBadServer) {
|
|
|
|
ServerAddresses stun_servers;
|
|
|
|
stun_servers.insert(kStunAddr1);
|
|
|
|
stun_servers.insert(kBadAddr);
|
|
|
|
CreateStunPort(stun_servers);
|
|
|
|
EXPECT_EQ("stun", port()->Type());
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
EXPECT_EQ(1U, port()->Candidates().size());
|
|
|
|
}
|
2014-09-27 01:01:11 +02:00
|
|
|
|
|
|
|
// Test that two candidates are allocated if the two STUN servers return
|
|
|
|
// different mapped addresses.
|
|
|
|
TEST_F(StunPortTest, TestTwoCandidatesWithTwoStunServersAcrossNat) {
|
|
|
|
const SocketAddress kStunMappedAddr1("77.77.77.77", 0);
|
|
|
|
const SocketAddress kStunMappedAddr2("88.77.77.77", 0);
|
|
|
|
stun_server_1()->set_fake_stun_addr(kStunMappedAddr1);
|
|
|
|
stun_server_2()->set_fake_stun_addr(kStunMappedAddr2);
|
|
|
|
|
|
|
|
ServerAddresses stun_servers;
|
|
|
|
stun_servers.insert(kStunAddr1);
|
|
|
|
stun_servers.insert(kStunAddr2);
|
|
|
|
CreateStunPort(stun_servers);
|
|
|
|
EXPECT_EQ("stun", port()->Type());
|
|
|
|
PrepareAddress();
|
|
|
|
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
|
|
|
|
EXPECT_EQ(2U, port()->Candidates().size());
|
|
|
|
}
|