blob: eb94e720c12aa806194ae59fc99e03e1e2f6ff95 [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
kwiberg3ec46792016-04-27 07:22:53 -070011#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012#include <string>
13
Karl Wiberg918f50c2018-07-05 11:40:33 +020014#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "p2p/base/stunserver.h"
16#include "rtc_base/gunit.h"
17#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/testclient.h"
19#include "rtc_base/thread.h"
20#include "rtc_base/virtualsocketserver.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021
Steve Anton6c38cc72017-11-29 10:25:58 -080022namespace cricket {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000023
Steve Anton6c38cc72017-11-29 10:25:58 -080024namespace {
25const rtc::SocketAddress server_addr("99.99.99.1", 3478);
26const rtc::SocketAddress client_addr("1.2.3.4", 1234);
27} // namespace
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028
29class StunServerTest : public testing::Test {
30 public:
deadbeef98e186c2017-05-16 18:00:06 -070031 StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032 virtual void SetUp() {
Yves Gerey665174f2018-06-19 15:03:05 +020033 server_.reset(
34 new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035 client_.reset(new rtc::TestClient(
Karl Wiberg918f50c2018-07-05 11:40:33 +020036 absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037
johan27c3d5b2016-10-17 00:54:57 -070038 network_.Start();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 }
40 void Send(const StunMessage& msg) {
jbauchf1f87202016-03-30 06:43:37 -070041 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 msg.Write(&buf);
43 Send(buf.Data(), static_cast<int>(buf.Length()));
44 }
45 void Send(const char* buf, int len) {
46 client_->SendTo(buf, len, server_addr);
47 }
Yves Gerey665174f2018-06-19 15:03:05 +020048 bool ReceiveFails() { return (client_->CheckNoPacket()); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049 StunMessage* Receive() {
50 StunMessage* msg = NULL;
nisse32f25052017-05-08 01:57:18 -070051 std::unique_ptr<rtc::TestClient::Packet> packet =
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000052 client_->NextPacket(rtc::TestClient::kTimeoutMs);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053 if (packet) {
jbauchf1f87202016-03-30 06:43:37 -070054 rtc::ByteBufferReader buf(packet->buf, packet->size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 msg = new StunMessage();
56 msg->Read(&buf);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 }
58 return msg;
59 }
Steve Anton6c38cc72017-11-29 10:25:58 -080060
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061 private:
kwiberg3ec46792016-04-27 07:22:53 -070062 std::unique_ptr<rtc::VirtualSocketServer> ss_;
johan27c3d5b2016-10-17 00:54:57 -070063 rtc::Thread network_;
kwiberg3ec46792016-04-27 07:22:53 -070064 std::unique_ptr<StunServer> server_;
65 std::unique_ptr<rtc::TestClient> client_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066};
67
68// Disable for TSan v2, see
69// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
70#if !defined(THREAD_SANITIZER)
71
72TEST_F(StunServerTest, TestGood) {
73 StunMessage req;
74 std::string transaction_id = "0123456789ab";
75 req.SetType(STUN_BINDING_REQUEST);
76 req.SetTransactionID(transaction_id);
77 Send(req);
78
79 StunMessage* msg = Receive();
80 ASSERT_TRUE(msg != NULL);
81 EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
82 EXPECT_EQ(req.transaction_id(), msg->transaction_id());
83
84 const StunAddressAttribute* mapped_addr =
85 msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
86 EXPECT_TRUE(mapped_addr != NULL);
87 EXPECT_EQ(1, mapped_addr->family());
88 EXPECT_EQ(client_addr.port(), mapped_addr->port());
89 if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
Jonas Olssonabbe8412018-04-03 13:40:05 +020090 RTC_LOG(LS_WARNING) << "Warning: mapped IP ("
91 << mapped_addr->ipaddr().ToString() << ") != local IP ("
92 << client_addr.ipaddr().ToString() << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 }
94
95 delete msg;
96}
97
Steve Anton6c38cc72017-11-29 10:25:58 -080098#endif // if !defined(THREAD_SANITIZER)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099
100TEST_F(StunServerTest, TestBad) {
Yves Gerey665174f2018-06-19 15:03:05 +0200101 const char* bad =
102 "this is a completely nonsensical message whose only "
103 "purpose is to make the parser go 'ack'. it doesn't "
104 "look anything like a normal stun message";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000105 Send(bad, static_cast<int>(strlen(bad)));
106
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +0000107 ASSERT_TRUE(ReceiveFails());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108}
Steve Anton6c38cc72017-11-29 10:25:58 -0800109
110} // namespace cricket