OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "webrtc/p2p/base/stunserver.h" | |
15 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
16 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
17 #include "webrtc/base/physicalsocketserver.h" | 16 #include "webrtc/base/physicalsocketserver.h" |
| 17 #include "webrtc/base/ptr_util.h" |
18 #include "webrtc/base/testclient.h" | 18 #include "webrtc/base/testclient.h" |
19 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
20 #include "webrtc/base/virtualsocketserver.h" | 20 #include "webrtc/base/virtualsocketserver.h" |
| 21 #include "webrtc/p2p/base/stunserver.h" |
21 | 22 |
22 using namespace cricket; | 23 using namespace cricket; |
23 | 24 |
24 static const rtc::SocketAddress server_addr("99.99.99.1", 3478); | 25 static const rtc::SocketAddress server_addr("99.99.99.1", 3478); |
25 static const rtc::SocketAddress client_addr("1.2.3.4", 1234); | 26 static const rtc::SocketAddress client_addr("1.2.3.4", 1234); |
26 | 27 |
27 class StunServerTest : public testing::Test { | 28 class StunServerTest : public testing::Test { |
28 public: | 29 public: |
29 StunServerTest() | 30 StunServerTest() |
30 : pss_(new rtc::PhysicalSocketServer), | 31 : pss_(new rtc::PhysicalSocketServer), |
31 ss_(new rtc::VirtualSocketServer(pss_.get())), | 32 ss_(new rtc::VirtualSocketServer(pss_.get())), |
32 network_(ss_.get()) { | 33 network_(ss_.get()) { |
33 } | 34 } |
34 virtual void SetUp() { | 35 virtual void SetUp() { |
35 server_.reset(new StunServer( | 36 server_.reset(new StunServer( |
36 rtc::AsyncUDPSocket::Create(ss_.get(), server_addr))); | 37 rtc::AsyncUDPSocket::Create(ss_.get(), server_addr))); |
37 client_.reset(new rtc::TestClient( | 38 client_.reset(new rtc::TestClient( |
38 rtc::AsyncUDPSocket::Create(ss_.get(), client_addr))); | 39 WrapUnique(rtc::AsyncUDPSocket::Create(ss_.get(), client_addr)))); |
39 | 40 |
40 network_.Start(); | 41 network_.Start(); |
41 } | 42 } |
42 void Send(const StunMessage& msg) { | 43 void Send(const StunMessage& msg) { |
43 rtc::ByteBufferWriter buf; | 44 rtc::ByteBufferWriter buf; |
44 msg.Write(&buf); | 45 msg.Write(&buf); |
45 Send(buf.Data(), static_cast<int>(buf.Length())); | 46 Send(buf.Data(), static_cast<int>(buf.Length())); |
46 } | 47 } |
47 void Send(const char* buf, int len) { | 48 void Send(const char* buf, int len) { |
48 client_->SendTo(buf, len, server_addr); | 49 client_->SendTo(buf, len, server_addr); |
49 } | 50 } |
50 bool ReceiveFails() { | 51 bool ReceiveFails() { |
51 return(client_->CheckNoPacket()); | 52 return(client_->CheckNoPacket()); |
52 } | 53 } |
53 StunMessage* Receive() { | 54 StunMessage* Receive() { |
54 StunMessage* msg = NULL; | 55 StunMessage* msg = NULL; |
55 rtc::TestClient::Packet* packet = | 56 std::unique_ptr<rtc::TestClient::Packet> packet = |
56 client_->NextPacket(rtc::TestClient::kTimeoutMs); | 57 client_->NextPacket(rtc::TestClient::kTimeoutMs); |
57 if (packet) { | 58 if (packet) { |
58 rtc::ByteBufferReader buf(packet->buf, packet->size); | 59 rtc::ByteBufferReader buf(packet->buf, packet->size); |
59 msg = new StunMessage(); | 60 msg = new StunMessage(); |
60 msg->Read(&buf); | 61 msg->Read(&buf); |
61 delete packet; | |
62 } | 62 } |
63 return msg; | 63 return msg; |
64 } | 64 } |
65 private: | 65 private: |
66 std::unique_ptr<rtc::PhysicalSocketServer> pss_; | 66 std::unique_ptr<rtc::PhysicalSocketServer> pss_; |
67 std::unique_ptr<rtc::VirtualSocketServer> ss_; | 67 std::unique_ptr<rtc::VirtualSocketServer> ss_; |
68 rtc::Thread network_; | 68 rtc::Thread network_; |
69 std::unique_ptr<StunServer> server_; | 69 std::unique_ptr<StunServer> server_; |
70 std::unique_ptr<rtc::TestClient> client_; | 70 std::unique_ptr<rtc::TestClient> client_; |
71 }; | 71 }; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 #endif // if !defined(THREAD_SANITIZER) | 104 #endif // if !defined(THREAD_SANITIZER) |
105 | 105 |
106 TEST_F(StunServerTest, TestBad) { | 106 TEST_F(StunServerTest, TestBad) { |
107 const char* bad = "this is a completely nonsensical message whose only " | 107 const char* bad = "this is a completely nonsensical message whose only " |
108 "purpose is to make the parser go 'ack'. it doesn't " | 108 "purpose is to make the parser go 'ack'. it doesn't " |
109 "look anything like a normal stun message"; | 109 "look anything like a normal stun message"; |
110 Send(bad, static_cast<int>(strlen(bad))); | 110 Send(bad, static_cast<int>(strlen(bad))); |
111 | 111 |
112 ASSERT_TRUE(ReceiveFails()); | 112 ASSERT_TRUE(ReceiveFails()); |
113 } | 113 } |
OLD | NEW |