• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()) {
37     server_.reset(
38         new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
39     client_.reset(new rtc::TestClient(
40         absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
41 
42     network_.Start();
43   }
~StunServerTest()44   ~StunServerTest() override { network_.Stop(); }
45 
Send(const StunMessage & msg)46   void Send(const StunMessage& msg) {
47     rtc::ByteBufferWriter buf;
48     msg.Write(&buf);
49     Send(buf.Data(), static_cast<int>(buf.Length()));
50   }
Send(const char * buf,int len)51   void Send(const char* buf, int len) {
52     client_->SendTo(buf, len, server_addr);
53   }
ReceiveFails()54   bool ReceiveFails() { return (client_->CheckNoPacket()); }
Receive()55   StunMessage* Receive() {
56     StunMessage* msg = NULL;
57     std::unique_ptr<rtc::TestClient::Packet> packet =
58         client_->NextPacket(rtc::TestClient::kTimeoutMs);
59     if (packet) {
60       rtc::ByteBufferReader buf(packet->buf, packet->size);
61       msg = new StunMessage();
62       msg->Read(&buf);
63     }
64     return msg;
65   }
66 
67  private:
68   rtc::AutoThread main_thread;
69   std::unique_ptr<rtc::VirtualSocketServer> ss_;
70   rtc::Thread network_;
71   std::unique_ptr<StunServer> server_;
72   std::unique_ptr<rtc::TestClient> client_;
73 };
74 
TEST_F(StunServerTest,TestGood)75 TEST_F(StunServerTest, TestGood) {
76   // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
77   std::string transaction_id = "0123456789abcdef";
78   StunMessage req(STUN_BINDING_REQUEST, transaction_id);
79   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());
91 
92   delete msg;
93 }
94 
TEST_F(StunServerTest,TestGoodXorMappedAddr)95 TEST_F(StunServerTest, TestGoodXorMappedAddr) {
96   // kStunTransactionIdLength = 12 for RFC 5389 request
97   // StunMessage::Write will automatically insert magic cookie (0x2112A442)
98   std::string transaction_id = "0123456789ab";
99   StunMessage req(STUN_BINDING_REQUEST, transaction_id);
100   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
TEST_F(StunServerTest,TestNoXorMappedAddr)117 TEST_F(StunServerTest, TestNoXorMappedAddr) {
118   // kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
119   std::string transaction_id = "0123456789abcdef";
120   StunMessage req(STUN_BINDING_REQUEST, transaction_id);
121   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);
131 
132   delete msg;
133 }
134 
TEST_F(StunServerTest,TestBad)135 TEST_F(StunServerTest, TestBad) {
136   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";
140   Send(bad, static_cast<int>(strlen(bad)));
141 
142   ASSERT_TRUE(ReceiveFails());
143 }
144 
145 }  // namespace cricket
146