blob: 7b11d6f50c4c9e3cb8edbb1ecb23f3dc9920e498 [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"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "p2p/base/stun_server.h"
17#include "rtc_base/byte_buffer.h"
18#include "rtc_base/ip_address.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/test_client.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/virtual_socket_server.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
Mirko Bonadei6a489f22019-04-09 15:11:12 +020032class StunServerTest : public ::testing::Test {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033 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;
Min Wang1e00dbc2019-06-26 13:08:29 -050077 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
78 std::string transaction_id = "0123456789abcdef";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079 req.SetType(STUN_BINDING_REQUEST);
80 req.SetTransactionID(transaction_id);
81 Send(req);
82
83 StunMessage* msg = Receive();
84 ASSERT_TRUE(msg != NULL);
85 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
86 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
87
88 const StunAddressAttribute* mapped_addr =
89 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
90 EXPECT_TRUE(mapped_addr != NULL);
91 EXPECT_EQ(1, mapped_addr->family());
92 EXPECT_EQ(client_addr.port(), mapped_addr->port());
Min Wang1e00dbc2019-06-26 13:08:29 -050093
94 delete msg;
95}
96
97TEST_F(StunServerTest, TestGoodXorMappedAddr) {
98 StunMessage req;
99 // kStunTransactionIdLength = 12 for RFC 5389 request
100 // StunMessage::Write will automatically insert magic cookie (0x2112A442)
101 std::string transaction_id = "0123456789ab";
102 req.SetType(STUN_BINDING_REQUEST);
103 req.SetTransactionID(transaction_id);
104 Send(req);
105
106 StunMessage* msg = Receive();
107 ASSERT_TRUE(msg != NULL);
108 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
109 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
110
111 const StunAddressAttribute* mapped_addr =
112 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
113 EXPECT_TRUE(mapped_addr != NULL);
114 EXPECT_EQ(1, mapped_addr->family());
115 EXPECT_EQ(client_addr.port(), mapped_addr->port());
116
117 delete msg;
118}
119
120// Send legacy RFC 3489 request, should not get xor mapped addr
121TEST_F(StunServerTest, TestNoXorMappedAddr) {
122 StunMessage req;
123 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
124 std::string transaction_id = "0123456789abcdef";
125 req.SetType(STUN_BINDING_REQUEST);
126 req.SetTransactionID(transaction_id);
127 Send(req);
128
129 StunMessage* msg = Receive();
130 ASSERT_TRUE(msg != NULL);
131 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
132 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
133
134 const StunAddressAttribute* mapped_addr =
135 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
136 EXPECT_TRUE(mapped_addr == NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137
138 delete msg;
139}
140
Steve Anton6c38cc72017-11-29 10:25:58 -0800141#endif // if !defined(THREAD_SANITIZER)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142
143TEST_F(StunServerTest, TestBad) {
Yves Gerey665174f2018-06-19 15:03:05 +0200144 const char* bad =
145 "this is a completely nonsensical message whose only "
146 "purpose is to make the parser go 'ack'. it doesn't "
147 "look anything like a normal stun message";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148 Send(bad, static_cast<int>(strlen(bad)));
149
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000150 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151}
Steve Anton6c38cc72017-11-29 10:25:58 -0800152
153} // namespace cricket