2014-10-28 22:20:11 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "p2p/base/stun_server.h"
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <string.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2016-04-27 07:22:53 -07:00
|
|
|
#include <memory>
|
2014-10-28 22:20:11 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
Use absl::make_unique and absl::WrapUnique directly
Instead of going through our wrappers in ptr_util.h.
This CL was generated by the following script:
git grep -l ptr_util | xargs perl -pi -e 's,#include "rtc_base/ptr_util.h",#include "absl/memory/memory.h",'
git grep -l MakeUnique | xargs perl -pi -e 's,\b(rtc::)?MakeUnique\b,absl::make_unique,g'
git grep -l WrapUnique | xargs perl -pi -e 's,\b(rtc::)?WrapUnique\b,absl::WrapUnique,g'
git checkout -- rtc_base/ptr_util{.h,_unittest.cc}
git cl format
Followed by manually adding dependencies on
//third_party/abseil-cpp/absl/memory until `gn check` stopped
complaining.
Bug: webrtc:9473
Change-Id: I89ccd363f070479b8c431eb2c3d404a46eaacc1c
Reviewed-on: https://webrtc-review.googlesource.com/86600
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23850}
2018-07-05 11:40:33 +02:00
|
|
|
#include "absl/memory/memory.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/byte_buffer.h"
|
|
|
|
|
#include "rtc_base/ip_address.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/test_client.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/thread.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/virtual_socket_server.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "test/gtest.h"
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2017-11-29 10:25:58 -08:00
|
|
|
namespace cricket {
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2017-11-29 10:25:58 -08:00
|
|
|
namespace {
|
|
|
|
|
const rtc::SocketAddress server_addr("99.99.99.1", 3478);
|
|
|
|
|
const rtc::SocketAddress client_addr("1.2.3.4", 1234);
|
|
|
|
|
} // namespace
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class StunServerTest : public ::testing::Test {
|
2014-10-28 22:20:11 +00:00
|
|
|
public:
|
2017-05-16 18:00:06 -07:00
|
|
|
StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {}
|
2014-10-28 22:20:11 +00:00
|
|
|
virtual void SetUp() {
|
|
|
|
|
server_.reset(
|
|
|
|
|
new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr)));
|
|
|
|
|
client_.reset(new rtc::TestClient(
|
Use absl::make_unique and absl::WrapUnique directly
Instead of going through our wrappers in ptr_util.h.
This CL was generated by the following script:
git grep -l ptr_util | xargs perl -pi -e 's,#include "rtc_base/ptr_util.h",#include "absl/memory/memory.h",'
git grep -l MakeUnique | xargs perl -pi -e 's,\b(rtc::)?MakeUnique\b,absl::make_unique,g'
git grep -l WrapUnique | xargs perl -pi -e 's,\b(rtc::)?WrapUnique\b,absl::WrapUnique,g'
git checkout -- rtc_base/ptr_util{.h,_unittest.cc}
git cl format
Followed by manually adding dependencies on
//third_party/abseil-cpp/absl/memory until `gn check` stopped
complaining.
Bug: webrtc:9473
Change-Id: I89ccd363f070479b8c431eb2c3d404a46eaacc1c
Reviewed-on: https://webrtc-review.googlesource.com/86600
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23850}
2018-07-05 11:40:33 +02:00
|
|
|
absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))));
|
2014-10-28 22:20:11 +00:00
|
|
|
|
2016-10-17 00:54:57 -07:00
|
|
|
network_.Start();
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
|
|
|
|
void Send(const StunMessage& msg) {
|
2016-03-30 06:43:37 -07:00
|
|
|
rtc::ByteBufferWriter buf;
|
2014-10-28 22:20:11 +00:00
|
|
|
msg.Write(&buf);
|
|
|
|
|
Send(buf.Data(), static_cast<int>(buf.Length()));
|
|
|
|
|
}
|
|
|
|
|
void Send(const char* buf, int len) {
|
|
|
|
|
client_->SendTo(buf, len, server_addr);
|
|
|
|
|
}
|
2015-02-07 22:37:59 +00:00
|
|
|
bool ReceiveFails() { return (client_->CheckNoPacket()); }
|
2014-10-28 22:20:11 +00:00
|
|
|
StunMessage* Receive() {
|
|
|
|
|
StunMessage* msg = NULL;
|
2017-05-08 01:57:18 -07:00
|
|
|
std::unique_ptr<rtc::TestClient::Packet> packet =
|
2015-02-07 22:37:59 +00:00
|
|
|
client_->NextPacket(rtc::TestClient::kTimeoutMs);
|
2014-10-28 22:20:11 +00:00
|
|
|
if (packet) {
|
2016-03-30 06:43:37 -07:00
|
|
|
rtc::ByteBufferReader buf(packet->buf, packet->size);
|
2014-10-28 22:20:11 +00:00
|
|
|
msg = new StunMessage();
|
|
|
|
|
msg->Read(&buf);
|
|
|
|
|
}
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
2017-11-29 10:25:58 -08:00
|
|
|
|
2014-10-28 22:20:11 +00:00
|
|
|
private:
|
2016-04-27 07:22:53 -07:00
|
|
|
std::unique_ptr<rtc::VirtualSocketServer> ss_;
|
2016-10-17 00:54:57 -07:00
|
|
|
rtc::Thread network_;
|
2016-04-27 07:22:53 -07:00
|
|
|
std::unique_ptr<StunServer> server_;
|
|
|
|
|
std::unique_ptr<rtc::TestClient> client_;
|
2014-10-28 22:20:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Disable for TSan v2, see
|
|
|
|
|
// https://code.google.com/p/webrtc/issues/detail?id=2517 for details.
|
|
|
|
|
#if !defined(THREAD_SANITIZER)
|
|
|
|
|
|
|
|
|
|
TEST_F(StunServerTest, TestGood) {
|
|
|
|
|
StunMessage req;
|
2019-06-26 13:08:29 -05:00
|
|
|
// kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
|
|
|
|
|
std::string transaction_id = "0123456789abcdef";
|
2014-10-28 22:20:11 +00:00
|
|
|
req.SetType(STUN_BINDING_REQUEST);
|
|
|
|
|
req.SetTransactionID(transaction_id);
|
|
|
|
|
Send(req);
|
|
|
|
|
|
|
|
|
|
StunMessage* msg = Receive();
|
|
|
|
|
ASSERT_TRUE(msg != NULL);
|
|
|
|
|
EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
|
|
|
|
|
EXPECT_EQ(req.transaction_id(), msg->transaction_id());
|
|
|
|
|
|
|
|
|
|
const StunAddressAttribute* mapped_addr =
|
|
|
|
|
msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
|
|
|
|
|
EXPECT_TRUE(mapped_addr != NULL);
|
|
|
|
|
EXPECT_EQ(1, mapped_addr->family());
|
|
|
|
|
EXPECT_EQ(client_addr.port(), mapped_addr->port());
|
2019-06-26 13:08:29 -05:00
|
|
|
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(StunServerTest, TestGoodXorMappedAddr) {
|
|
|
|
|
StunMessage req;
|
|
|
|
|
// kStunTransactionIdLength = 12 for RFC 5389 request
|
|
|
|
|
// StunMessage::Write will automatically insert magic cookie (0x2112A442)
|
|
|
|
|
std::string transaction_id = "0123456789ab";
|
|
|
|
|
req.SetType(STUN_BINDING_REQUEST);
|
|
|
|
|
req.SetTransactionID(transaction_id);
|
|
|
|
|
Send(req);
|
|
|
|
|
|
|
|
|
|
StunMessage* msg = Receive();
|
|
|
|
|
ASSERT_TRUE(msg != NULL);
|
|
|
|
|
EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
|
|
|
|
|
EXPECT_EQ(req.transaction_id(), msg->transaction_id());
|
|
|
|
|
|
|
|
|
|
const StunAddressAttribute* mapped_addr =
|
|
|
|
|
msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
|
|
|
|
|
EXPECT_TRUE(mapped_addr != NULL);
|
|
|
|
|
EXPECT_EQ(1, mapped_addr->family());
|
|
|
|
|
EXPECT_EQ(client_addr.port(), mapped_addr->port());
|
|
|
|
|
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send legacy RFC 3489 request, should not get xor mapped addr
|
|
|
|
|
TEST_F(StunServerTest, TestNoXorMappedAddr) {
|
|
|
|
|
StunMessage req;
|
|
|
|
|
// kStunLegacyTransactionIdLength = 16 for legacy RFC 3489 request
|
|
|
|
|
std::string transaction_id = "0123456789abcdef";
|
|
|
|
|
req.SetType(STUN_BINDING_REQUEST);
|
|
|
|
|
req.SetTransactionID(transaction_id);
|
|
|
|
|
Send(req);
|
|
|
|
|
|
|
|
|
|
StunMessage* msg = Receive();
|
|
|
|
|
ASSERT_TRUE(msg != NULL);
|
|
|
|
|
EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
|
|
|
|
|
EXPECT_EQ(req.transaction_id(), msg->transaction_id());
|
|
|
|
|
|
|
|
|
|
const StunAddressAttribute* mapped_addr =
|
|
|
|
|
msg->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
|
|
|
|
|
EXPECT_TRUE(mapped_addr == NULL);
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 10:25:58 -08:00
|
|
|
#endif // if !defined(THREAD_SANITIZER)
|
2014-10-28 22:20:11 +00:00
|
|
|
|
|
|
|
|
TEST_F(StunServerTest, TestBad) {
|
|
|
|
|
const char* bad =
|
|
|
|
|
"this is a completely nonsensical message whose only "
|
|
|
|
|
"purpose is to make the parser go 'ack'. it doesn't "
|
|
|
|
|
"look anything like a normal stun message";
|
|
|
|
|
Send(bad, static_cast<int>(strlen(bad)));
|
|
|
|
|
|
2015-02-07 22:37:59 +00:00
|
|
|
ASSERT_TRUE(ReceiveFails());
|
2014-10-28 22:20:11 +00:00
|
|
|
}
|
2017-11-29 10:25:58 -08:00
|
|
|
|
|
|
|
|
} // namespace cricket
|