blob: 060305e6b35466409ac56d03c2b13c37c09d8396 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/stunserver.h"
15#include "rtc_base/gunit.h"
16#include "rtc_base/logging.h"
17#include "rtc_base/ptr_util.h"
18#include "rtc_base/testclient.h"
19#include "rtc_base/thread.h"
20#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021
22using namespace cricket;
23
24static const rtc::SocketAddress server_addr("99.99.99.1", 3478);
25static const rtc::SocketAddress client_addr("1.2.3.4", 1234);
26
27class StunServerTest : public testing::Test {
28 public:
deadbeef98e186c2017-05-16 18:00:06 -070029 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030 virtual void SetUp() {
31 server_.reset(new StunServer(
32 rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
33 client_.reset(new rtc::TestClient(
nisse32f25052017-05-08 01:57:18 -070034 WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035
johan27c3d5b2016-10-17 00:54:57 -070036 network_.Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037 }
38 void Send(const StunMessage& msg) {
jbauchf1f87202016-03-30 06:43:37 -070039 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040 msg.Write(&buf);
41 Send(buf.Data(), static_cast<int>(buf.Length()));
42 }
43 void Send(const char* buf, int len) {
44 client_->SendTo(buf, len, server_addr);
45 }
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000046 bool ReceiveFails() {
47 return(client_->CheckNoPacket());
48 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049 StunMessage* Receive() {
50 StunMessage* msg = NULL;
nisse32f25052017-05-08 01:57:18 -070051 std::unique_ptr<rtc::TestClient::Packet> packet =
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000052 client_->NextPacket(rtc::TestClient::kTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053 if (packet) {
jbauchf1f87202016-03-30 06:43:37 -070054 rtc::ByteBufferReader buf(packet->buf, packet->size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 msg = new StunMessage();
56 msg->Read(&buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 }
58 return msg;
59 }
60 private:
kwiberg3ec46792016-04-27 07:22:53 -070061 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 00:54:57 -070062 rtc::Thread network_;
kwiberg3ec46792016-04-27 07:22:53 -070063 std::unique_ptr<StunServer> server_;
64 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065};
66
67// Disable for TSan v2, see
68// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
69#if !defined(THREAD_SANITIZER)
70
71TEST_F(StunServerTest, TestGood) {
72 StunMessage req;
73 std::string transaction_id = "0123456789ab";
74 req.SetType(STUN_BINDING_REQUEST);
75 req.SetTransactionID(transaction_id);
76 Send(req);
77
78 StunMessage* msg = Receive();
79 ASSERT_TRUE(msg != NULL);
80 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
81 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
82
83 const StunAddressAttribute* mapped_addr =
84 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
85 EXPECT_TRUE(mapped_addr != NULL);
86 EXPECT_EQ(1, mapped_addr->family());
87 EXPECT_EQ(client_addr.port(), mapped_addr->port());
88 if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
89 LOG(LS_WARNING) << "Warning: mapped IP ("
90 << mapped_addr->ipaddr()
91 << ") != local IP (" << client_addr.ipaddr()
92 << ")";
93 }
94
95 delete msg;
96}
97
98#endif // if !defined(THREAD_SANITIZER)
99
100TEST_F(StunServerTest, TestBad) {
101 const char* bad = "this is a completely nonsensical message whose only "
102 "purpose is to make the parser go 'ack'. it doesn't "
103 "look anything like a normal stun message";
104 Send(bad, static_cast<int>(strlen(bad)));
105
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000106 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000107}