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 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 14 | #include "webrtc/base/gunit.h" |
| 15 | #include "webrtc/base/logging.h" |
| 16 | #include "webrtc/base/physicalsocketserver.h" |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 17 | #include "webrtc/base/ptr_util.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 18 | #include "webrtc/base/testclient.h" |
| 19 | #include "webrtc/base/thread.h" |
| 20 | #include "webrtc/base/virtualsocketserver.h" |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 21 | #include "webrtc/p2p/base/stunserver.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace cricket; |
| 24 | |
| 25 | static const rtc::SocketAddress server_addr("99.99.99.1", 3478); |
| 26 | static const rtc::SocketAddress client_addr("1.2.3.4", 1234); |
| 27 | |
| 28 | class StunServerTest : public testing::Test { |
| 29 | public: |
| 30 | StunServerTest() |
| 31 | : pss_(new rtc::PhysicalSocketServer), |
| 32 | ss_(new rtc::VirtualSocketServer(pss_.get())), |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 33 | network_(ss_.get()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 34 | } |
| 35 | virtual void SetUp() { |
| 36 | server_.reset(new StunServer( |
| 37 | rtc::AsyncUDPSocket::Create(ss_.get(), server_addr))); |
| 38 | client_.reset(new rtc::TestClient( |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 39 | WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr)))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 40 | |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 41 | network_.Start(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | } |
| 43 | void Send(const StunMessage& msg) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 44 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 45 | msg.Write(&buf); |
| 46 | Send(buf.Data(), static_cast<int>(buf.Length())); |
| 47 | } |
| 48 | void Send(const char* buf, int len) { |
| 49 | client_->SendTo(buf, len, server_addr); |
| 50 | } |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 51 | bool ReceiveFails() { |
| 52 | return(client_->CheckNoPacket()); |
| 53 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | StunMessage* Receive() { |
| 55 | StunMessage* msg = NULL; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame^] | 56 | std::unique_ptr<rtc::TestClient::Packet> packet = |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 57 | client_->NextPacket(rtc::TestClient::kTimeoutMs); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | if (packet) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 59 | rtc::ByteBufferReader buf(packet->buf, packet->size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | msg = new StunMessage(); |
| 61 | msg->Read(&buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | } |
| 63 | return msg; |
| 64 | } |
| 65 | private: |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 66 | std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
| 67 | std::unique_ptr<rtc::VirtualSocketServer> ss_; |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 68 | rtc::Thread network_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 69 | std::unique_ptr<StunServer> server_; |
| 70 | std::unique_ptr<rtc::TestClient> client_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
| 77 | TEST_F(StunServerTest, TestGood) { |
| 78 | StunMessage req; |
| 79 | std::string transaction_id = "0123456789ab"; |
| 80 | req.SetType(STUN_BINDING_REQUEST); |
| 81 | req.SetTransactionID(transaction_id); |
| 82 | Send(req); |
| 83 | |
| 84 | StunMessage* msg = Receive(); |
| 85 | ASSERT_TRUE(msg != NULL); |
| 86 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
| 87 | EXPECT_EQ(req.transaction_id(), msg->transaction_id()); |
| 88 | |
| 89 | const StunAddressAttribute* mapped_addr = |
| 90 | msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 91 | EXPECT_TRUE(mapped_addr != NULL); |
| 92 | EXPECT_EQ(1, mapped_addr->family()); |
| 93 | EXPECT_EQ(client_addr.port(), mapped_addr->port()); |
| 94 | if (mapped_addr->ipaddr() != client_addr.ipaddr()) { |
| 95 | LOG(LS_WARNING) << "Warning: mapped IP (" |
| 96 | << mapped_addr->ipaddr() |
| 97 | << ") != local IP (" << client_addr.ipaddr() |
| 98 | << ")"; |
| 99 | } |
| 100 | |
| 101 | delete msg; |
| 102 | } |
| 103 | |
| 104 | #endif // if !defined(THREAD_SANITIZER) |
| 105 | |
| 106 | TEST_F(StunServerTest, TestBad) { |
| 107 | const char* bad = "this is a completely nonsensical message whose only " |
| 108 | "purpose is to make the parser go 'ack'. it doesn't " |
| 109 | "look anything like a normal stun message"; |
| 110 | Send(bad, static_cast<int>(strlen(bad))); |
| 111 | |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 112 | ASSERT_TRUE(ReceiveFails()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 113 | } |