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