henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <string> |
| 29 | |
| 30 | #include "talk/base/gunit.h" |
| 31 | #include "talk/base/logging.h" |
| 32 | #include "talk/base/physicalsocketserver.h" |
| 33 | #include "talk/base/virtualsocketserver.h" |
| 34 | #include "talk/base/testclient.h" |
| 35 | #include "talk/base/thread.h" |
| 36 | #include "talk/p2p/base/stunserver.h" |
| 37 | |
| 38 | using namespace cricket; |
| 39 | |
| 40 | static const talk_base::SocketAddress server_addr("99.99.99.1", 3478); |
| 41 | static const talk_base::SocketAddress client_addr("1.2.3.4", 1234); |
| 42 | |
| 43 | class StunServerTest : public testing::Test { |
| 44 | public: |
| 45 | StunServerTest() |
| 46 | : pss_(new talk_base::PhysicalSocketServer), |
| 47 | ss_(new talk_base::VirtualSocketServer(pss_.get())), |
| 48 | worker_(ss_.get()) { |
| 49 | } |
| 50 | virtual void SetUp() { |
| 51 | server_.reset(new StunServer( |
| 52 | talk_base::AsyncUDPSocket::Create(ss_.get(), server_addr))); |
| 53 | client_.reset(new talk_base::TestClient( |
| 54 | talk_base::AsyncUDPSocket::Create(ss_.get(), client_addr))); |
| 55 | |
| 56 | worker_.Start(); |
| 57 | } |
| 58 | void Send(const StunMessage& msg) { |
| 59 | talk_base::ByteBuffer buf; |
| 60 | msg.Write(&buf); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 61 | Send(buf.Data(), static_cast<int>(buf.Length())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | } |
| 63 | void Send(const char* buf, int len) { |
| 64 | client_->SendTo(buf, len, server_addr); |
| 65 | } |
| 66 | StunMessage* Receive() { |
| 67 | StunMessage* msg = NULL; |
| 68 | talk_base::TestClient::Packet* packet = client_->NextPacket(); |
| 69 | if (packet) { |
| 70 | talk_base::ByteBuffer buf(packet->buf, packet->size); |
| 71 | msg = new StunMessage(); |
| 72 | msg->Read(&buf); |
| 73 | delete packet; |
| 74 | } |
| 75 | return msg; |
| 76 | } |
| 77 | private: |
| 78 | talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_; |
| 79 | talk_base::scoped_ptr<talk_base::VirtualSocketServer> ss_; |
| 80 | talk_base::Thread worker_; |
| 81 | talk_base::scoped_ptr<StunServer> server_; |
| 82 | talk_base::scoped_ptr<talk_base::TestClient> client_; |
| 83 | }; |
| 84 | |
kjellander@webrtc.org | d1cfa71 | 2013-10-16 16:51:52 +0000 | [diff] [blame] | 85 | // Disable for TSan v2, see |
| 86 | // https://code.google.com/p/webrtc/issues/detail?id=2517 for details. |
| 87 | #if !defined(THREAD_SANITIZER) |
| 88 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | TEST_F(StunServerTest, TestGood) { |
| 90 | StunMessage req; |
| 91 | std::string transaction_id = "0123456789ab"; |
| 92 | req.SetType(STUN_BINDING_REQUEST); |
| 93 | req.SetTransactionID(transaction_id); |
| 94 | Send(req); |
| 95 | |
| 96 | StunMessage* msg = Receive(); |
| 97 | ASSERT_TRUE(msg != NULL); |
| 98 | EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type()); |
| 99 | EXPECT_EQ(req.transaction_id(), msg->transaction_id()); |
| 100 | |
| 101 | const StunAddressAttribute* mapped_addr = |
| 102 | msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 103 | EXPECT_TRUE(mapped_addr != NULL); |
| 104 | EXPECT_EQ(1, mapped_addr->family()); |
| 105 | EXPECT_EQ(client_addr.port(), mapped_addr->port()); |
| 106 | if (mapped_addr->ipaddr() != client_addr.ipaddr()) { |
| 107 | LOG(LS_WARNING) << "Warning: mapped IP (" |
| 108 | << mapped_addr->ipaddr() |
| 109 | << ") != local IP (" << client_addr.ipaddr() |
| 110 | << ")"; |
| 111 | } |
| 112 | |
| 113 | delete msg; |
| 114 | } |
| 115 | |
kjellander@webrtc.org | d1cfa71 | 2013-10-16 16:51:52 +0000 | [diff] [blame] | 116 | #endif // if !defined(THREAD_SANITIZER) |
| 117 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | TEST_F(StunServerTest, TestBad) { |
| 119 | const char* bad = "this is a completely nonsensical message whose only " |
| 120 | "purpose is to make the parser go 'ack'. it doesn't " |
| 121 | "look anything like a normal stun message"; |
pbos@webrtc.org | 371243d | 2014-03-07 15:22:04 +0000 | [diff] [blame^] | 122 | Send(bad, static_cast<int>(strlen(bad))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | |
| 124 | StunMessage* msg = Receive(); |
| 125 | ASSERT_TRUE(msg == NULL); |
| 126 | } |