blob: b1142f7a801ffd96065cc6f2e9cb19b6b790be9f [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
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 17:08:3311#include "p2p/base/stun_server.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <string.h>
Jonas Olssona4d87372019-07-05 17:08:3314
kwiberg3ec46792016-04-27 14:22:5315#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:1116#include <string>
17
Karl Wiberg918f50c2018-07-05 09:40:3318#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 17:11:0019#include "rtc_base/byte_buffer.h"
20#include "rtc_base/ip_address.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0022#include "rtc_base/test_client.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/virtual_socket_server.h"
Yves Gerey3e707812018-11-28 15:47:4925#include "test/gtest.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1126
Steve Anton6c38cc72017-11-29 18:25:5827namespace cricket {
henrike@webrtc.org269fb4b2014-10-28 22:20:1128
Steve Anton6c38cc72017-11-29 18:25:5829namespace {
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:1133
Mirko Bonadei6a489f22019-04-09 13:11:1234class StunServerTest : public ::testing::Test {
henrike@webrtc.org269fb4b2014-10-28 22:20:1135 public:
deadbeef98e186c2017-05-17 01:00:0636 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:1137 virtual void SetUp() {
Yves Gerey665174f2018-06-19 13:03:0538 server_.reset(
39 new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
henrike@webrtc.org269fb4b2014-10-28 22:20:1140 client_.reset(new rtc::TestClient(
Karl Wiberg918f50c2018-07-05 09:40:3341 absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
henrike@webrtc.org269fb4b2014-10-28 22:20:1142
johan27c3d5b2016-10-17 07:54:5743 network_.Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:1144 }
45 void Send(const StunMessage& msg) {
jbauchf1f87202016-03-30 13:43:3746 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:1147 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 13:03:0553 bool ReceiveFails() { return (client_->CheckNoPacket()); }
henrike@webrtc.org269fb4b2014-10-28 22:20:1154 StunMessage* Receive() {
55 StunMessage* msg = NULL;
nisse32f25052017-05-08 08:57:1856 std::unique_ptr<rtc::TestClient::Packet> packet =
jlmiller@webrtc.orgec499be2015-02-07 22:37:5957 client_->NextPacket(rtc::TestClient::kTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:1158 if (packet) {
jbauchf1f87202016-03-30 13:43:3759 rtc::ByteBufferReader buf(packet->buf, packet->size);
henrike@webrtc.org269fb4b2014-10-28 22:20:1160 msg = new StunMessage();
61 msg->Read(&buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:1162 }
63 return msg;
64 }
Steve Anton6c38cc72017-11-29 18:25:5865
henrike@webrtc.org269fb4b2014-10-28 22:20:1166 private:
kwiberg3ec46792016-04-27 14:22:5367 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 07:54:5768 rtc::Thread network_;
kwiberg3ec46792016-04-27 14:22:5369 std::unique_ptr<StunServer> server_;
70 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:1171};
72
73// Disable for TSan v2, see
74// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
75#if !defined(THREAD_SANITIZER)
76
77TEST_F(StunServerTest, TestGood) {
78 StunMessage req;
Min Wang1e00dbc2019-06-26 18:08:2979 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
80 std::string transaction_id = "0123456789abcdef";
henrike@webrtc.org269fb4b2014-10-28 22:20:1181 req.SetType(STUN_BINDING_REQUEST);
82 req.SetTransactionID(transaction_id);
83 Send(req);
84
85 StunMessage* msg = Receive();
86 ASSERT_TRUE(msg != NULL);
87 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
88 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
89
90 const StunAddressAttribute* mapped_addr =
91 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
92 EXPECT_TRUE(mapped_addr != NULL);
93 EXPECT_EQ(1, mapped_addr->family());
94 EXPECT_EQ(client_addr.port(), mapped_addr->port());
Min Wang1e00dbc2019-06-26 18:08:2995
96 delete msg;
97}
98
99TEST_F(StunServerTest, TestGoodXorMappedAddr) {
100 StunMessage req;
101 // kStunTransactionIdLength = 12 for RFC 5389 request
102 // StunMessage::Write will automatically insert magic cookie (0x2112A442)
103 std::string transaction_id = "0123456789ab";
104 req.SetType(STUN_BINDING_REQUEST);
105 req.SetTransactionID(transaction_id);
106 Send(req);
107
108 StunMessage* msg = Receive();
109 ASSERT_TRUE(msg != NULL);
110 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
111 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
112
113 const StunAddressAttribute* mapped_addr =
114 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
115 EXPECT_TRUE(mapped_addr != NULL);
116 EXPECT_EQ(1, mapped_addr->family());
117 EXPECT_EQ(client_addr.port(), mapped_addr->port());
118
119 delete msg;
120}
121
122// Send legacy RFC 3489 request, should not get xor mapped addr
123TEST_F(StunServerTest, TestNoXorMappedAddr) {
124 StunMessage req;
125 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
126 std::string transaction_id = "0123456789abcdef";
127 req.SetType(STUN_BINDING_REQUEST);
128 req.SetTransactionID(transaction_id);
129 Send(req);
130
131 StunMessage* msg = Receive();
132 ASSERT_TRUE(msg != NULL);
133 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
134 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
135
136 const StunAddressAttribute* mapped_addr =
137 msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
138 EXPECT_TRUE(mapped_addr == NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11139
140 delete msg;
141}
142
Steve Anton6c38cc72017-11-29 18:25:58143#endif // if !defined(THREAD_SANITIZER)
henrike@webrtc.org269fb4b2014-10-28 22:20:11144
145TEST_F(StunServerTest, TestBad) {
Yves Gerey665174f2018-06-19 13:03:05146 const char* bad =
147 "this is a completely nonsensical message whose only "
148 "purpose is to make the parser go 'ack'. it doesn't "
149 "look anything like a normal stun message";
henrike@webrtc.org269fb4b2014-10-28 22:20:11150 Send(bad, static_cast<int>(strlen(bad)));
151
jlmiller@webrtc.orgec499be2015-02-07 22:37:59152 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11153}
Steve Anton6c38cc72017-11-29 18:25:58154
155} // namespace cricket