1 /*
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
11 #include "p2p/base/stun_server.h"
12
13 #include <string.h>
14
15 #include <memory>
16 #include <string>
17
18 #include "absl/memory/memory.h"
19 #include "rtc_base/byte_buffer.h"
20 #include "rtc_base/ip_address.h"
21 #include "rtc_base/logging.h"
22 #include "rtc_base/test_client.h"
23 #include "rtc_base/thread.h"
24 #include "rtc_base/virtual_socket_server.h"
25 #include "test/gtest.h"
26
27 namespace cricket {
28
29 namespace {
30 const rtc::SocketAddress server_addr("99.99.99.1", 3478);
31 const rtc::SocketAddress client_addr("1.2.3.4", 1234);
32 } // namespace
33
34 class StunServerTest : public ::testing::Test {
35 public:
StunServerTest()36 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
SetUp()37 virtual void SetUp() {
38 server_.reset(
39 new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
40 client_.reset(new rtc::TestClient(
41 absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
42
43 network_.Start();
44 }
Send(const StunMessage & msg)45 void Send(const StunMessage& msg) {
46 rtc::ByteBufferWriter buf;
47 msg.Write(&buf);
48 Send(buf.Data(), static_cast<int>(buf.Length()));
49 }
Send(const char * buf,int len)50 void Send(const char* buf, int len) {
51 client_->SendTo(buf, len, server_addr);
52 }
ReceiveFails()53 bool ReceiveFails() { return (client_->CheckNoPacket()); }
Receive()54 StunMessage* Receive() {
55 StunMessage* msg = NULL;
56 std::unique_ptr<rtc::TestClient::Packet> packet =
57 client_->NextPacket(rtc::TestClient::kTimeoutMs);
58 if (packet) {
59 rtc::ByteBufferReader buf(packet->buf, packet->size);
60 msg = new StunMessage();
61 msg->Read(&buf);
62 }
63 return msg;
64 }
65
66 private:
67 std::unique_ptr<rtc::VirtualSocketServer> ss_;
68 rtc::Thread network_;
69 std::unique_ptr<StunServer> server_;
70 std::unique_ptr<rtc::TestClient> client_;
71 };
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
TEST_F(StunServerTest,TestGood)77 TEST_F(StunServerTest, TestGood) {
78 StunMessage req;
79 // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
80 std::string transaction_id = "0123456789abcdef";
81 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());
95
96 delete msg;
97 }
98
TEST_F(StunServerTest,TestGoodXorMappedAddr)99 TEST_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
TEST_F(StunServerTest,TestNoXorMappedAddr)123 TEST_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);
139
140 delete msg;
141 }
142
143 #endif // if !defined(THREAD_SANITIZER)
144
TEST_F(StunServerTest,TestBad)145 TEST_F(StunServerTest, TestBad) {
146 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";
150 Send(bad, static_cast<int>(strlen(bad)));
151
152 ASSERT_TRUE(ReceiveFails());
153 }
154
155 } // namespace cricket
156