3
* Copyright 2004 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.
30
#include "talk/base/gunit.h"
31
#include "talk/base/logging.h"
32
#include "talk/base/physicalsocketserver.h"
33
#include "talk/base/virtualsocketserver.h"
34
#include "talk/base/testclient.h"
35
#include "talk/base/thread.h"
36
#include "talk/p2p/base/stunserver.h"
38
using namespace cricket;
40
static const talk_base::SocketAddress server_addr("99.99.99.1", 3478);
41
static const talk_base::SocketAddress client_addr("1.2.3.4", 1234);
43
class StunServerTest : public testing::Test {
46
: pss_(new talk_base::PhysicalSocketServer),
47
ss_(new talk_base::VirtualSocketServer(pss_.get())),
50
virtual void SetUp() {
51
server_.reset(new StunServer(
52
talk_base::AsyncUDPSocket::Create(ss_.get(), server_addr)));
53
client_.reset(new talk_base::TestClient(
54
talk_base::AsyncUDPSocket::Create(ss_.get(), client_addr)));
58
void Send(const StunMessage& msg) {
59
talk_base::ByteBuffer buf;
61
Send(buf.Data(), buf.Length());
63
void Send(const char* buf, int len) {
64
client_->SendTo(buf, len, server_addr);
66
StunMessage* Receive() {
67
StunMessage* msg = NULL;
68
talk_base::TestClient::Packet* packet = client_->NextPacket();
70
talk_base::ByteBuffer buf(packet->buf, packet->size);
71
msg = new StunMessage();
78
talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
79
talk_base::scoped_ptr<talk_base::VirtualSocketServer> ss_;
80
talk_base::Thread worker_;
81
talk_base::scoped_ptr<StunServer> server_;
82
talk_base::scoped_ptr<talk_base::TestClient> client_;
85
TEST_F(StunServerTest, TestGood) {
87
std::string transaction_id = "0123456789ab";
88
req.SetType(STUN_BINDING_REQUEST);
89
req.SetTransactionID(transaction_id);
92
StunMessage* msg = Receive();
93
ASSERT_TRUE(msg != NULL);
94
EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
95
EXPECT_EQ(req.transaction_id(), msg->transaction_id());
97
const StunAddressAttribute* mapped_addr =
98
msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
99
EXPECT_TRUE(mapped_addr != NULL);
100
EXPECT_EQ(1, mapped_addr->family());
101
EXPECT_EQ(client_addr.port(), mapped_addr->port());
102
if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
103
LOG(LS_WARNING) << "Warning: mapped IP ("
104
<< mapped_addr->ipaddr()
105
<< ") != local IP (" << client_addr.ipaddr()
112
TEST_F(StunServerTest, TestBad) {
113
const char* bad = "this is a completely nonsensical message whose only "
114
"purpose is to make the parser go 'ack'. it doesn't "
115
"look anything like a normal stun message";
116
Send(bad, std::strlen(bad));
118
StunMessage* msg = Receive();
119
ASSERT_TRUE(msg != NULL);
120
EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
122
const StunErrorCodeAttribute* err = msg->GetErrorCode();
123
EXPECT_TRUE(err != NULL);
124
EXPECT_EQ(4, err->error_class());
125
EXPECT_EQ(0, err->number());
126
EXPECT_EQ("Bad Request", err->reason());