3
* Copyright 2009 Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/base/basicpacketsocketfactory.h"
29
#include "talk/base/gunit.h"
30
#include "talk/base/helpers.h"
31
#include "talk/base/socketaddress.h"
32
#include "talk/p2p/base/stunport.h"
33
#include "talk/p2p/base/teststunserver.h"
35
using talk_base::SocketAddress;
37
static const SocketAddress kLocalAddr("127.0.0.1", 0);
38
static const SocketAddress kStunAddr("127.0.0.1", 5000);
39
static const SocketAddress kBadAddr("0.0.0.1", 5000);
40
static const SocketAddress kStunHostnameAddr("localhost", 5000);
41
static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000);
42
static const int kTimeoutMs = 10000;
44
// Tests connecting a StunPort to a fake STUN server (cricket::StunServer)
45
// TODO: Use a VirtualSocketServer here. We have to use a
46
// PhysicalSocketServer right now since DNS is not part of SocketServer yet.
47
class StunPortTest : public testing::Test,
48
public sigslot::has_slots<> {
51
: network_("unittest", "unittest", talk_base::IPAddress(INADDR_ANY), 32),
52
socket_factory_(talk_base::Thread::Current()),
53
stun_server_(new cricket::TestStunServer(
54
talk_base::Thread::Current(), kStunAddr)),
55
done_(false), error_(false) {
58
const cricket::Port* port() const { return stun_port_.get(); }
59
bool done() const { return done_; }
60
bool error() const { return error_; }
62
void CreateStunPort(const talk_base::SocketAddress& server_addr) {
63
stun_port_.reset(cricket::StunPort::Create(
64
talk_base::Thread::Current(), &socket_factory_, &network_,
65
kLocalAddr.ipaddr(), 0, 0, server_addr));
66
stun_port_->SignalAddressReady.connect(this,
67
&StunPortTest::OnAddressReady);
68
stun_port_->SignalAddressError.connect(this,
69
&StunPortTest::OnAddressError);
72
void PrepareAddress() {
73
stun_port_->PrepareAddress();
77
static void SetUpTestCase() {
78
// Ensure the RNG is inited.
79
talk_base::InitRandom(NULL, 0);
82
void OnAddressReady(cricket::Port* port) {
86
void OnAddressError(cricket::Port* port) {
92
talk_base::Network network_;
93
talk_base::BasicPacketSocketFactory socket_factory_;
94
talk_base::scoped_ptr<cricket::StunPort> stun_port_;
95
talk_base::scoped_ptr<cricket::TestStunServer> stun_server_;
100
// Test that we can create a STUN port
101
TEST_F(StunPortTest, TestBasic) {
102
CreateStunPort(kStunAddr);
103
EXPECT_EQ("stun", port()->type());
104
EXPECT_EQ(0U, port()->candidates().size());
107
// Test that we can get an address from a STUN server.
108
TEST_F(StunPortTest, TestPrepareAddress) {
109
CreateStunPort(kStunAddr);
111
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
112
ASSERT_EQ(1U, port()->candidates().size());
113
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->candidates()[0].address()));
115
// TODO: Add IPv6 tests here, once either physicalsocketserver supports
116
// IPv6, or this test is changed to use VirtualSocketServer.
119
// Test that we fail properly if we can't get an address.
120
TEST_F(StunPortTest, TestPrepareAddressFail) {
121
CreateStunPort(kBadAddr);
123
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
124
EXPECT_TRUE(error());
125
EXPECT_EQ(0U, port()->candidates().size());
128
// Test that we can get an address from a STUN server specified by a hostname.
129
TEST_F(StunPortTest, TestPrepareAddressHostname) {
130
CreateStunPort(kStunHostnameAddr);
132
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
133
ASSERT_EQ(1U, port()->candidates().size());
134
EXPECT_TRUE(kLocalAddr.EqualIPs(port()->candidates()[0].address()));
137
// Test that we handle hostname lookup failures properly.
138
TEST_F(StunPortTest, TestPrepareAddressHostnameFail) {
139
CreateStunPort(kBadHostnameAddr);
141
EXPECT_TRUE_WAIT(done(), kTimeoutMs);
142
EXPECT_TRUE(error());
143
EXPECT_EQ(0U, port()->candidates().size());