henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 14 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "p2p/base/stunserver.h" |
| 16 | #include "rtc_base/gunit.h" |
| 17 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/testclient.h" |
| 19 | #include "rtc_base/thread.h" |
| 20 | #include "rtc_base/virtualsocketserver.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 21 | |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 22 | namespace cricket { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 23 | |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 24 | namespace { |
| 25 | const rtc::SocketAddress server_addr("99.99.99.1", 3478); |
| 26 | const rtc::SocketAddress client_addr("1.2.3.4", 1234); |
| 27 | } // namespace |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 28 | |
| 29 | class StunServerTest : public testing::Test { |
| 30 | public: |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 31 | StunServerTest() : ss_(new rtc::VirtualSocketServer()), network_(ss_.get()) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 32 | virtual void SetUp() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | server_.reset( |
| 34 | new StunServer(rtc::AsyncUDPSocket::Create(ss_.get(), server_addr))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 35 | client_.reset(new rtc::TestClient( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 36 | absl::WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr)))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 37 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 38 | network_.Start(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | } |
| 40 | void Send(const StunMessage& msg) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 41 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | bool ReceiveFails() { return (client_->CheckNoPacket()); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | StunMessage* Receive() { |
| 50 | StunMessage* msg = NULL; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 51 | std::unique_ptr<rtc::TestClient::Packet> packet = |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 52 | client_->NextPacket(rtc::TestClient::kTimeoutMs); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | if (packet) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 54 | rtc::ByteBufferReader buf(packet->buf, packet->size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 55 | msg = new StunMessage(); |
| 56 | msg->Read(&buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 57 | } |
| 58 | return msg; |
| 59 | } |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 60 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 62 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 63 | rtc::Thread network_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 64 | std::unique_ptr<StunServer> server_; |
| 65 | std::unique_ptr<rtc::TestClient> client_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 66 | }; |
| 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 | |
| 72 | TEST_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 Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 90 | RTC_LOG(LS_WARNING) << "Warning: mapped IP (" |
| 91 | << mapped_addr->ipaddr().ToString() << ") != local IP (" |
| 92 | << client_addr.ipaddr().ToString() << ")"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | delete msg; |
| 96 | } |
| 97 | |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 98 | #endif // if !defined(THREAD_SANITIZER) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | |
| 100 | TEST_F(StunServerTest, TestBad) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 105 | Send(bad, static_cast<int>(strlen(bad))); |
| 106 | |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 107 | ASSERT_TRUE(ReceiveFails()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 108 | } |
Steve Anton | 6c38cc7 | 2017-11-29 10:25:58 -0800 | [diff] [blame] | 109 | |
| 110 | } // namespace cricket |