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 <string>
29
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"
37
38 using namespace cricket;
39
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);
42
43 class StunServerTest : public testing::Test {
44 public:
StunServerTest()45 StunServerTest()
46 : pss_(new talk_base::PhysicalSocketServer),
47 ss_(new talk_base::VirtualSocketServer(pss_.get())),
48 worker_(ss_.get()) {
49 }
SetUp()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)));
55
56 worker_.Start();
57 }
Send(const StunMessage & msg)58 void Send(const StunMessage& msg) {
59 talk_base::ByteBuffer buf;
60 msg.Write(&buf);
61 Send(buf.Data(), static_cast<int>(buf.Length()));
62 }
Send(const char * buf,int len)63 void Send(const char* buf, int len) {
64 client_->SendTo(buf, len, server_addr);
65 }
Receive()66 StunMessage* Receive() {
67 StunMessage* msg = NULL;
68 talk_base::TestClient::Packet* packet = client_->NextPacket();
69 if (packet) {
70 talk_base::ByteBuffer buf(packet->buf, packet->size);
71 msg = new StunMessage();
72 msg->Read(&buf);
73 delete packet;
74 }
75 return msg;
76 }
77 private:
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_;
83 };
84
85 // Disable for TSan v2, see
86 // https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
87 #if !defined(THREAD_SANITIZER)
88
TEST_F(StunServerTest,TestGood)89 TEST_F(StunServerTest, TestGood) {
90 StunMessage req;
91 std::string transaction_id = "0123456789ab";
92 req.SetType(STUN_BINDING_REQUEST);
93 req.SetTransactionID(transaction_id);
94 Send(req);
95
96 StunMessage* msg = Receive();
97 ASSERT_TRUE(msg != NULL);
98 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
99 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
100
101 const StunAddressAttribute* mapped_addr =
102 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
103 EXPECT_TRUE(mapped_addr != NULL);
104 EXPECT_EQ(1, mapped_addr->family());
105 EXPECT_EQ(client_addr.port(), mapped_addr->port());
106 if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
107 LOG(LS_WARNING) << "Warning: mapped IP ("
108 << mapped_addr->ipaddr()
109 << ") != local IP (" << client_addr.ipaddr()
110 << ")";
111 }
112
113 delete msg;
114 }
115
116 #endif // if !defined(THREAD_SANITIZER)
117
TEST_F(StunServerTest,TestBad)118 TEST_F(StunServerTest, TestBad) {
119 const char* bad = "this is a completely nonsensical message whose only "
120 "purpose is to make the parser go 'ack'. it doesn't "
121 "look anything like a normal stun message";
122 Send(bad, static_cast<int>(std::strlen(bad)));
123
124 StunMessage* msg = Receive();
125 ASSERT_TRUE(msg == NULL);
126 }
127