blob: 7266eae862aa4856787967063c2cdc907dfc4edf [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <string>
12
13#include "webrtc/p2p/base/stunserver.h"
14#include "webrtc/base/gunit.h"
15#include "webrtc/base/logging.h"
16#include "webrtc/base/physicalsocketserver.h"
17#include "webrtc/base/testclient.h"
18#include "webrtc/base/thread.h"
19#include "webrtc/base/virtualsocketserver.h"
20
21using namespace cricket;
22
23static const rtc::SocketAddress server_addr("99.99.99.1", 3478);
24static const rtc::SocketAddress client_addr("1.2.3.4", 1234);
25
26class StunServerTest : public testing::Test {
27 public:
28 StunServerTest()
29 : pss_(new rtc::PhysicalSocketServer),
30 ss_(new rtc::VirtualSocketServer(pss_.get())),
31 worker_(ss_.get()) {
32 }
33 virtual void SetUp() {
34 server_.reset(new StunServer(
35 rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
36 client_.reset(new rtc::TestClient(
37 rtc::AsyncUDPSocket::Create(ss_.get(), client_addr)));
38
39 worker_.Start();
40 }
41 void Send(const StunMessage& msg) {
42 rtc::ByteBuffer buf;
43 msg.Write(&buf);
44 Send(buf.Data(), static_cast<int>(buf.Length()));
45 }
46 void Send(const char* buf, int len) {
47 client_->SendTo(buf, len, server_addr);
48 }
49 StunMessage* Receive() {
50 StunMessage* msg = NULL;
51 rtc::TestClient::Packet* packet = client_->NextPacket();
52 if (packet) {
53 rtc::ByteBuffer buf(packet->buf, packet->size);
54 msg = new StunMessage();
55 msg->Read(&buf);
56 delete packet;
57 }
58 return msg;
59 }
60 private:
61 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
62 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
63 rtc::Thread worker_;
64 rtc::scoped_ptr<StunServer> server_;
65 rtc::scoped_ptr<rtc::TestClient> client_;
66};
67
68// Disable for TSan v2, see
69// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
70#if !defined(THREAD_SANITIZER)
71
72TEST_F(StunServerTest, TestGood) {
73 StunMessage req;
74 std::string transaction_id = "0123456789ab";
75 req.SetType(STUN_BINDING_REQUEST);
76 req.SetTransactionID(transaction_id);
77 Send(req);
78
79 StunMessage* msg = Receive();
80 ASSERT_TRUE(msg != NULL);
81 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
82 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
83
84 const StunAddressAttribute* mapped_addr =
85 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
86 EXPECT_TRUE(mapped_addr != NULL);
87 EXPECT_EQ(1, mapped_addr->family());
88 EXPECT_EQ(client_addr.port(), mapped_addr->port());
89 if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
90 LOG(LS_WARNING) << "Warning: mapped IP ("
91 << mapped_addr->ipaddr()
92 << ") != local IP (" << client_addr.ipaddr()
93 << ")";
94 }
95
96 delete msg;
97}
98
99#endif // if !defined(THREAD_SANITIZER)
100
101TEST_F(StunServerTest, TestBad) {
102 const char* bad = "this is a completely nonsensical message whose only "
103 "purpose is to make the parser go 'ack'. it doesn't "
104 "look anything like a normal stun message";
105 Send(bad, static_cast<int>(strlen(bad)));
106
107 StunMessage* msg = Receive();
108 ASSERT_TRUE(msg == NULL);
109}