blob: 5d3f31fb982526134de52daba8eeab0cd9a3b06e [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:
Danil Chapovalova01e7272022-09-14 17:26:58 +020036 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {
Yves Gerey665174f2018-06-19 15:03:05 +020037 server_.reset(
38 new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 client_.reset(new rtc::TestClient(
Karl Wiberg918f50c2018-07-05 11:40:33 +020040 absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041
johan27c3d5b2016-10-17 00:54:57 -070042 network_.Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000043 }
Danil Chapovalova01e7272022-09-14 17:26:58 +020044 ~StunServerTest() override { network_.Stop(); }
45
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046 void Send(const StunMessage& msg) {
jbauchf1f87202016-03-30 06:43:37 -070047 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048 msg.Write(&buf);
49 Send(buf.Data(), static_cast<int>(buf.Length()));
50 }
51 void Send(const char* buf, int len) {
52 client_->SendTo(buf, len, server_addr);
53 }
Yves Gerey665174f2018-06-19 15:03:05 +020054 bool ReceiveFails() { return (client_->CheckNoPacket()); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 StunMessage* Receive() {
56 StunMessage* msg = NULL;
nisse32f25052017-05-08 01:57:18 -070057 std::unique_ptr<rtc::TestClient::Packet> packet =
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000058 client_->NextPacket(rtc::TestClient::kTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 if (packet) {
jbauchf1f87202016-03-30 06:43:37 -070060 rtc::ByteBufferReader buf(packet->buf, packet->size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061 msg = new StunMessage();
62 msg->Read(&buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063 }
64 return msg;
65 }
Steve Anton6c38cc72017-11-29 10:25:58 -080066
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067 private:
Niels Möller83830f32022-05-20 09:12:57 +020068 rtc::AutoThread main_thread;
kwiberg3ec46792016-04-27 07:22:53 -070069 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 00:54:57 -070070 rtc::Thread network_;
kwiberg3ec46792016-04-27 07:22:53 -070071 std::unique_ptr<StunServer> server_;
72 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000073};
74
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000075TEST_F(StunServerTest, TestGood) {
Min Wang1e00dbc2019-06-26 13:08:29 -050076 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
77 std::string transaction_id = "0123456789abcdef";
Tommi408143d2022-06-01 15:29:31 +020078 StunMessage req(STUN_BINDING_REQUEST, transaction_id);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079 Send(req);
80
81 StunMessage* msg = Receive();
82 ASSERT_TRUE(msg != NULL);
83 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
84 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
85
86 const StunAddressAttribute* mapped_addr =
87 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
88 EXPECT_TRUE(mapped_addr != NULL);
89 EXPECT_EQ(1, mapped_addr->family());
90 EXPECT_EQ(client_addr.port(), mapped_addr->port());
Min Wang1e00dbc2019-06-26 13:08:29 -050091
92 delete msg;
93}
94
95TEST_F(StunServerTest, TestGoodXorMappedAddr) {
Min Wang1e00dbc2019-06-26 13:08:29 -050096 // kStunTransactionIdLength = 12 for RFC 5389 request
97 // StunMessage::Write will automatically insert magic cookie (0x2112A442)
98 std::string transaction_id = "0123456789ab";
Tommi408143d2022-06-01 15:29:31 +020099 StunMessage req(STUN_BINDING_REQUEST, transaction_id);
Min Wang1e00dbc2019-06-26 13:08:29 -0500100 Send(req);
101
102 StunMessage* msg = Receive();
103 ASSERT_TRUE(msg != NULL);
104 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
105 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
106
107 const StunAddressAttribute* mapped_addr =
108 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
109 EXPECT_TRUE(mapped_addr != NULL);
110 EXPECT_EQ(1, mapped_addr->family());
111 EXPECT_EQ(client_addr.port(), mapped_addr->port());
112
113 delete msg;
114}
115
116// Send legacy RFC 3489 request, should not get xor mapped addr
117TEST_F(StunServerTest, TestNoXorMappedAddr) {
Min Wang1e00dbc2019-06-26 13:08:29 -0500118 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
119 std::string transaction_id = "0123456789abcdef";
Tommi408143d2022-06-01 15:29:31 +0200120 StunMessage req(STUN_BINDING_REQUEST, transaction_id);
Min Wang1e00dbc2019-06-26 13:08:29 -0500121 Send(req);
122
123 StunMessage* msg = Receive();
124 ASSERT_TRUE(msg != NULL);
125 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
126 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
127
128 const StunAddressAttribute* mapped_addr =
129 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
130 EXPECT_TRUE(mapped_addr == NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131
132 delete msg;
133}
134
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000135TEST_F(StunServerTest, TestBad) {
Yves Gerey665174f2018-06-19 15:03:05 +0200136 const char* bad =
137 "this is a completely nonsensical message whose only "
138 "purpose is to make the parser go 'ack'. it doesn't "
139 "look anything like a normal stun message";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 Send(bad, static_cast<int>(strlen(bad)));
141
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000142 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143}
Steve Anton6c38cc72017-11-29 10:25:58 -0800144
145} // namespace cricket