blob: c72a8812183f810b2da24f82017ea9169ad790db [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
kwiberg3ec46792016-04-27 07:22:53 -070011#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012#include <string>
13
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000014#include "webrtc/base/gunit.h"
15#include "webrtc/base/logging.h"
16#include "webrtc/base/physicalsocketserver.h"
nisse32f25052017-05-08 01:57:18 -070017#include "webrtc/base/ptr_util.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include "webrtc/base/testclient.h"
19#include "webrtc/base/thread.h"
20#include "webrtc/base/virtualsocketserver.h"
nisse32f25052017-05-08 01:57:18 -070021#include "webrtc/p2p/base/stunserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022
23using namespace cricket;
24
25static const rtc::SocketAddress server_addr("99.99.99.1", 3478);
26static const rtc::SocketAddress client_addr("1.2.3.4", 1234);
27
28class StunServerTest : public testing::Test {
29 public:
30 StunServerTest()
31 : pss_(new rtc::PhysicalSocketServer),
32 ss_(new rtc::VirtualSocketServer(pss_.get())),
johan27c3d5b2016-10-17 00:54:57 -070033 network_(ss_.get()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000034 }
35 virtual void SetUp() {
36 server_.reset(new StunServer(
37 rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
38 client_.reset(new rtc::TestClient(
nisse32f25052017-05-08 01:57:18 -070039 WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040
johan27c3d5b2016-10-17 00:54:57 -070041 network_.Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 }
43 void Send(const StunMessage& msg) {
jbauchf1f87202016-03-30 06:43:37 -070044 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045 msg.Write(&buf);
46 Send(buf.Data(), static_cast<int>(buf.Length()));
47 }
48 void Send(const char* buf, int len) {
49 client_->SendTo(buf, len, server_addr);
50 }
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000051 bool ReceiveFails() {
52 return(client_->CheckNoPacket());
53 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054 StunMessage* Receive() {
55 StunMessage* msg = NULL;
nisse32f25052017-05-08 01:57:18 -070056 std::unique_ptr<rtc::TestClient::Packet> packet =
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000057 client_->NextPacket(rtc::TestClient::kTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058 if (packet) {
jbauchf1f87202016-03-30 06:43:37 -070059 rtc::ByteBufferReader buf(packet->buf, packet->size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 msg = new StunMessage();
61 msg->Read(&buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062 }
63 return msg;
64 }
65 private:
kwiberg3ec46792016-04-27 07:22:53 -070066 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
67 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 00:54:57 -070068 rtc::Thread network_;
kwiberg3ec46792016-04-27 07:22:53 -070069 std::unique_ptr<StunServer> server_;
70 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071};
72
73// Disable for TSan v2, see
74// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
75#if !defined(THREAD_SANITIZER)
76
77TEST_F(StunServerTest, TestGood) {
78 StunMessage req;
79 std::string transaction_id = "0123456789ab";
80 req.SetType(STUN_BINDING_REQUEST);
81 req.SetTransactionID(transaction_id);
82 Send(req);
83
84 StunMessage* msg = Receive();
85 ASSERT_TRUE(msg != NULL);
86 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
87 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
88
89 const StunAddressAttribute* mapped_addr =
90 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
91 EXPECT_TRUE(mapped_addr != NULL);
92 EXPECT_EQ(1, mapped_addr->family());
93 EXPECT_EQ(client_addr.port(), mapped_addr->port());
94 if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
95 LOG(LS_WARNING) << "Warning: mapped IP ("
96 << mapped_addr->ipaddr()
97 << ") != local IP (" << client_addr.ipaddr()
98 << ")";
99 }
100
101 delete msg;
102}
103
104#endif // if !defined(THREAD_SANITIZER)
105
106TEST_F(StunServerTest, TestBad) {
107 const char* bad = "this is a completely nonsensical message whose only "
108 "purpose is to make the parser go 'ack'. it doesn't "
109 "look anything like a normal stun message";
110 Send(bad, static_cast<int>(strlen(bad)));
111
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000112 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113}