blob: 154d5b857ca78cc0abb401bca7c1f14f293ad18e [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <string.h>
kwiberg3ec46792016-04-27 07:22:53 -070012#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013#include <string>
14
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "p2p/base/stunserver.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "rtc_base/bytebuffer.h"
18#include "rtc_base/ipaddress.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/testclient.h"
21#include "rtc_base/thread.h"
22#include "rtc_base/virtualsocketserver.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "test/gtest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
Steve Anton6c38cc72017-11-29 10:25:58 -080025namespace cricket {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026
Steve Anton6c38cc72017-11-29 10:25:58 -080027namespace {
28const rtc::SocketAddress server_addr("99.99.99.1", 3478);
29const rtc::SocketAddress client_addr("1.2.3.4", 1234);
30} // namespace
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031
32class StunServerTest : public testing::Test {
33 public:
deadbeef98e186c2017-05-16 18:00:06 -070034 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035 virtual void SetUp() {
Yves Gerey665174f2018-06-19 15:03:05 +020036 server_.reset(
37 new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038 client_.reset(new rtc::TestClient(
Karl Wiberg918f50c2018-07-05 11:40:33 +020039 absl::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 }
Yves Gerey665174f2018-06-19 15:03:05 +020051 bool ReceiveFails() { return (client_->CheckNoPacket()); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 StunMessage* Receive() {
53 StunMessage* msg = NULL;
nisse32f25052017-05-08 01:57:18 -070054 std::unique_ptr<rtc::TestClient::Packet> packet =
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000055 client_->NextPacket(rtc::TestClient::kTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000056 if (packet) {
jbauchf1f87202016-03-30 06:43:37 -070057 rtc::ByteBufferReader buf(packet->buf, packet->size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000058 msg = new StunMessage();
59 msg->Read(&buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060 }
61 return msg;
62 }
Steve Anton6c38cc72017-11-29 10:25:58 -080063
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064 private:
kwiberg3ec46792016-04-27 07:22:53 -070065 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 00:54:57 -070066 rtc::Thread network_;
kwiberg3ec46792016-04-27 07:22:53 -070067 std::unique_ptr<StunServer> server_;
68 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069};
70
71// Disable for TSan v2, see
72// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
73#if !defined(THREAD_SANITIZER)
74
75TEST_F(StunServerTest, TestGood) {
76 StunMessage req;
77 std::string transaction_id = "0123456789ab";
78 req.SetType(STUN_BINDING_REQUEST);
79 req.SetTransactionID(transaction_id);
80 Send(req);
81
82 StunMessage* msg = Receive();
83 ASSERT_TRUE(msg != NULL);
84 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
85 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
86
87 const StunAddressAttribute* mapped_addr =
88 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
89 EXPECT_TRUE(mapped_addr != NULL);
90 EXPECT_EQ(1, mapped_addr->family());
91 EXPECT_EQ(client_addr.port(), mapped_addr->port());
92 if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +020093 RTC_LOG(LS_WARNING) << "Warning: mapped IP ("
94 << mapped_addr->ipaddr().ToString() << ") != local IP ("
95 << client_addr.ipaddr().ToString() << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096 }
97
98 delete msg;
99}
100
Steve Anton6c38cc72017-11-29 10:25:58 -0800101#endif // if !defined(THREAD_SANITIZER)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
103TEST_F(StunServerTest, TestBad) {
Yves Gerey665174f2018-06-19 15:03:05 +0200104 const char* bad =
105 "this is a completely nonsensical message whose only "
106 "purpose is to make the parser go 'ack'. it doesn't "
107 "look anything like a normal stun message";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 Send(bad, static_cast<int>(strlen(bad)));
109
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000110 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111}
Steve Anton6c38cc72017-11-29 10:25:58 -0800112
113} // namespace cricket