blob: 3206c5621e41ef381986a1f63538a8968b8f3d98 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "p2p/base/stun_server.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
kwiberg3ec46792016-04-27 07:22:53 -070015#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <string>
17
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/byte_buffer.h"
20#include "rtc_base/ip_address.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/test_client.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/virtual_socket_server.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "test/gtest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026
Steve Anton6c38cc72017-11-29 10:25:58 -080027namespace cricket {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
Steve Anton6c38cc72017-11-29 10:25:58 -080029namespace {
30const rtc::SocketAddress server_addr("99.99.99.1", 3478);
31const rtc::SocketAddress client_addr("1.2.3.4", 1234);
32} // namespace
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033
Mirko Bonadei6a489f22019-04-09 15:11:12 +020034class StunServerTest : public ::testing::Test {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035 public:
deadbeef98e186c2017-05-16 18:00:06 -070036 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037 virtual void SetUp() {
Yves Gerey665174f2018-06-19 15:03:05 +020038 server_.reset(
39 new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000040 client_.reset(new rtc::TestClient(
Karl Wiberg918f50c2018-07-05 11:40:33 +020041 absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042
johan27c3d5b2016-10-17 00:54:57 -070043 network_.Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044 }
45 void Send(const StunMessage& msg) {
jbauchf1f87202016-03-30 06:43:37 -070046 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 msg.Write(&buf);
48 Send(buf.Data(), static_cast<int>(buf.Length()));
49 }
50 void Send(const char* buf, int len) {
51 client_->SendTo(buf, len, server_addr);
52 }
Yves Gerey665174f2018-06-19 15:03:05 +020053 bool ReceiveFails() { return (client_->CheckNoPacket()); }
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 }
Steve Anton6c38cc72017-11-29 10:25:58 -080065
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066 private:
Niels Möller83830f32022-05-20 09:12:57 +020067 rtc::AutoThread main_thread;
kwiberg3ec46792016-04-27 07:22:53 -070068 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 00:54:57 -070069 rtc::Thread network_;
kwiberg3ec46792016-04-27 07:22:53 -070070 std::unique_ptr<StunServer> server_;
71 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072};
73
74// Disable for TSan v2, see
75// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
76#if !defined(THREAD_SANITIZER)
77
78TEST_F(StunServerTest, TestGood) {
Min Wang1e00dbc2019-06-26 13:08:29 -050079 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
80 std::string transaction_id = "0123456789abcdef";
Tommi408143d2022-06-01 15:29:31 +020081 StunMessage req(STUN_BINDING_REQUEST, transaction_id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 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());
Min Wang1e00dbc2019-06-26 13:08:29 -050094
95 delete msg;
96}
97
98TEST_F(StunServerTest, TestGoodXorMappedAddr) {
Min Wang1e00dbc2019-06-26 13:08:29 -050099 // kStunTransactionIdLength = 12 for RFC 5389 request
100 // StunMessage::Write will automatically insert magic cookie (0x2112A442)
101 std::string transaction_id = "0123456789ab";
Tommi408143d2022-06-01 15:29:31 +0200102 StunMessage req(STUN_BINDING_REQUEST, transaction_id);
Min Wang1e00dbc2019-06-26 13:08:29 -0500103 Send(req);
104
105 StunMessage* msg = Receive();
106 ASSERT_TRUE(msg != NULL);
107 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
108 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
109
110 const StunAddressAttribute* mapped_addr =
111 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
112 EXPECT_TRUE(mapped_addr != NULL);
113 EXPECT_EQ(1, mapped_addr->family());
114 EXPECT_EQ(client_addr.port(), mapped_addr->port());
115
116 delete msg;
117}
118
119// Send legacy RFC 3489 request, should not get xor mapped addr
120TEST_F(StunServerTest, TestNoXorMappedAddr) {
Min Wang1e00dbc2019-06-26 13:08:29 -0500121 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
122 std::string transaction_id = "0123456789abcdef";
Tommi408143d2022-06-01 15:29:31 +0200123 StunMessage req(STUN_BINDING_REQUEST, transaction_id);
Min Wang1e00dbc2019-06-26 13:08:29 -0500124 Send(req);
125
126 StunMessage* msg = Receive();
127 ASSERT_TRUE(msg != NULL);
128 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
129 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
130
131 const StunAddressAttribute* mapped_addr =
132 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
133 EXPECT_TRUE(mapped_addr == NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134
135 delete msg;
136}
137
Steve Anton6c38cc72017-11-29 10:25:58 -0800138#endif // if !defined(THREAD_SANITIZER)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139
140TEST_F(StunServerTest, TestBad) {
Yves Gerey665174f2018-06-19 15:03:05 +0200141 const char* bad =
142 "this is a completely nonsensical message whose only "
143 "purpose is to make the parser go 'ack'. it doesn't "
144 "look anything like a normal stun message";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 Send(bad, static_cast<int>(strlen(bad)));
146
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000147 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148}
Steve Anton6c38cc72017-11-29 10:25:58 -0800149
150} // namespace cricket