henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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> |
| 12 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | #include "webrtc/p2p/base/asyncstuntcpsocket.h" |
| 14 | #include "webrtc/base/asyncsocket.h" |
| 15 | #include "webrtc/base/gunit.h" |
| 16 | #include "webrtc/base/physicalsocketserver.h" |
| 17 | #include "webrtc/base/virtualsocketserver.h" |
| 18 | |
| 19 | namespace cricket { |
| 20 | |
| 21 | static unsigned char kStunMessageWithZeroLength[] = { |
| 22 | 0x00, 0x01, 0x00, 0x00, // length of 0 (last 2 bytes) |
| 23 | 0x21, 0x12, 0xA4, 0x42, |
| 24 | '0', '1', '2', '3', |
| 25 | '4', '5', '6', '7', |
| 26 | '8', '9', 'a', 'b', |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | static unsigned char kTurnChannelDataMessageWithZeroLength[] = { |
| 31 | 0x40, 0x00, 0x00, 0x00, // length of 0 (last 2 bytes) |
| 32 | }; |
| 33 | |
| 34 | static unsigned char kTurnChannelDataMessage[] = { |
| 35 | 0x40, 0x00, 0x00, 0x10, |
| 36 | 0x21, 0x12, 0xA4, 0x42, |
| 37 | '0', '1', '2', '3', |
| 38 | '4', '5', '6', '7', |
| 39 | '8', '9', 'a', 'b', |
| 40 | }; |
| 41 | |
| 42 | static unsigned char kStunMessageWithInvalidLength[] = { |
| 43 | 0x00, 0x01, 0x00, 0x10, |
| 44 | 0x21, 0x12, 0xA4, 0x42, |
| 45 | '0', '1', '2', '3', |
| 46 | '4', '5', '6', '7', |
| 47 | '8', '9', 'a', 'b', |
| 48 | }; |
| 49 | |
| 50 | static unsigned char kTurnChannelDataMessageWithInvalidLength[] = { |
| 51 | 0x80, 0x00, 0x00, 0x20, |
| 52 | 0x21, 0x12, 0xA4, 0x42, |
| 53 | '0', '1', '2', '3', |
| 54 | '4', '5', '6', '7', |
| 55 | '8', '9', 'a', 'b', |
| 56 | }; |
| 57 | |
| 58 | static unsigned char kTurnChannelDataMessageWithOddLength[] = { |
| 59 | 0x40, 0x00, 0x00, 0x05, |
| 60 | 0x21, 0x12, 0xA4, 0x42, |
| 61 | '0', |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | static const rtc::SocketAddress kClientAddr("11.11.11.11", 0); |
| 66 | static const rtc::SocketAddress kServerAddr("22.22.22.22", 0); |
| 67 | |
| 68 | class AsyncStunTCPSocketTest : public testing::Test, |
| 69 | public sigslot::has_slots<> { |
| 70 | protected: |
| 71 | AsyncStunTCPSocketTest() |
| 72 | : vss_(new rtc::VirtualSocketServer(NULL)), |
| 73 | ss_scope_(vss_.get()) { |
| 74 | } |
| 75 | |
| 76 | virtual void SetUp() { |
| 77 | CreateSockets(); |
| 78 | } |
| 79 | |
| 80 | void CreateSockets() { |
| 81 | rtc::AsyncSocket* server = vss_->CreateAsyncSocket( |
| 82 | kServerAddr.family(), SOCK_STREAM); |
| 83 | server->Bind(kServerAddr); |
| 84 | recv_socket_.reset(new AsyncStunTCPSocket(server, true)); |
| 85 | recv_socket_->SignalNewConnection.connect( |
| 86 | this, &AsyncStunTCPSocketTest::OnNewConnection); |
| 87 | |
| 88 | rtc::AsyncSocket* client = vss_->CreateAsyncSocket( |
| 89 | kClientAddr.family(), SOCK_STREAM); |
| 90 | send_socket_.reset(AsyncStunTCPSocket::Create( |
| 91 | client, kClientAddr, recv_socket_->GetLocalAddress())); |
| 92 | ASSERT_TRUE(send_socket_.get() != NULL); |
| 93 | vss_->ProcessMessagesUntilIdle(); |
| 94 | } |
| 95 | |
| 96 | void OnReadPacket(rtc::AsyncPacketSocket* socket, const char* data, |
| 97 | size_t len, const rtc::SocketAddress& remote_addr, |
| 98 | const rtc::PacketTime& packet_time) { |
| 99 | recv_packets_.push_back(std::string(data, len)); |
| 100 | } |
| 101 | |
| 102 | void OnNewConnection(rtc::AsyncPacketSocket* server, |
| 103 | rtc::AsyncPacketSocket* new_socket) { |
| 104 | listen_socket_.reset(new_socket); |
| 105 | new_socket->SignalReadPacket.connect( |
| 106 | this, &AsyncStunTCPSocketTest::OnReadPacket); |
| 107 | } |
| 108 | |
| 109 | bool Send(const void* data, size_t len) { |
| 110 | rtc::PacketOptions options; |
| 111 | size_t ret = send_socket_->Send( |
| 112 | reinterpret_cast<const char*>(data), len, options); |
| 113 | vss_->ProcessMessagesUntilIdle(); |
| 114 | return (ret == len); |
| 115 | } |
| 116 | |
| 117 | bool CheckData(const void* data, int len) { |
| 118 | bool ret = false; |
| 119 | if (recv_packets_.size()) { |
| 120 | std::string packet = recv_packets_.front(); |
| 121 | recv_packets_.pop_front(); |
| 122 | ret = (memcmp(data, packet.c_str(), len) == 0); |
| 123 | } |
| 124 | return ret; |
| 125 | } |
| 126 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame^] | 127 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 128 | rtc::SocketServerScope ss_scope_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame^] | 129 | std::unique_ptr<AsyncStunTCPSocket> send_socket_; |
| 130 | std::unique_ptr<AsyncStunTCPSocket> recv_socket_; |
| 131 | std::unique_ptr<rtc::AsyncPacketSocket> listen_socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 132 | std::list<std::string> recv_packets_; |
| 133 | }; |
| 134 | |
| 135 | // Testing a stun packet sent/recv properly. |
| 136 | TEST_F(AsyncStunTCPSocketTest, TestSingleStunPacket) { |
| 137 | EXPECT_TRUE(Send(kStunMessageWithZeroLength, |
| 138 | sizeof(kStunMessageWithZeroLength))); |
| 139 | EXPECT_EQ(1u, recv_packets_.size()); |
| 140 | EXPECT_TRUE(CheckData(kStunMessageWithZeroLength, |
| 141 | sizeof(kStunMessageWithZeroLength))); |
| 142 | } |
| 143 | |
| 144 | // Verify sending multiple packets. |
| 145 | TEST_F(AsyncStunTCPSocketTest, TestMultipleStunPackets) { |
| 146 | EXPECT_TRUE(Send(kStunMessageWithZeroLength, |
| 147 | sizeof(kStunMessageWithZeroLength))); |
| 148 | EXPECT_TRUE(Send(kStunMessageWithZeroLength, |
| 149 | sizeof(kStunMessageWithZeroLength))); |
| 150 | EXPECT_TRUE(Send(kStunMessageWithZeroLength, |
| 151 | sizeof(kStunMessageWithZeroLength))); |
| 152 | EXPECT_TRUE(Send(kStunMessageWithZeroLength, |
| 153 | sizeof(kStunMessageWithZeroLength))); |
| 154 | EXPECT_EQ(4u, recv_packets_.size()); |
| 155 | } |
| 156 | |
| 157 | // Verifying TURN channel data message with zero length. |
| 158 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataWithZeroLength) { |
| 159 | EXPECT_TRUE(Send(kTurnChannelDataMessageWithZeroLength, |
| 160 | sizeof(kTurnChannelDataMessageWithZeroLength))); |
| 161 | EXPECT_EQ(1u, recv_packets_.size()); |
| 162 | EXPECT_TRUE(CheckData(kTurnChannelDataMessageWithZeroLength, |
| 163 | sizeof(kTurnChannelDataMessageWithZeroLength))); |
| 164 | } |
| 165 | |
| 166 | // Verifying TURN channel data message. |
| 167 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelData) { |
| 168 | EXPECT_TRUE(Send(kTurnChannelDataMessage, |
| 169 | sizeof(kTurnChannelDataMessage))); |
| 170 | EXPECT_EQ(1u, recv_packets_.size()); |
| 171 | EXPECT_TRUE(CheckData(kTurnChannelDataMessage, |
| 172 | sizeof(kTurnChannelDataMessage))); |
| 173 | } |
| 174 | |
| 175 | // Verifying TURN channel messages which needs padding handled properly. |
| 176 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataPadding) { |
| 177 | EXPECT_TRUE(Send(kTurnChannelDataMessageWithOddLength, |
| 178 | sizeof(kTurnChannelDataMessageWithOddLength))); |
| 179 | EXPECT_EQ(1u, recv_packets_.size()); |
| 180 | EXPECT_TRUE(CheckData(kTurnChannelDataMessageWithOddLength, |
| 181 | sizeof(kTurnChannelDataMessageWithOddLength))); |
| 182 | } |
| 183 | |
| 184 | // Verifying stun message with invalid length. |
| 185 | TEST_F(AsyncStunTCPSocketTest, TestStunInvalidLength) { |
| 186 | EXPECT_FALSE(Send(kStunMessageWithInvalidLength, |
| 187 | sizeof(kStunMessageWithInvalidLength))); |
| 188 | EXPECT_EQ(0u, recv_packets_.size()); |
| 189 | |
| 190 | // Modify the message length to larger value. |
| 191 | kStunMessageWithInvalidLength[2] = 0xFF; |
| 192 | kStunMessageWithInvalidLength[3] = 0xFF; |
| 193 | EXPECT_FALSE(Send(kStunMessageWithInvalidLength, |
| 194 | sizeof(kStunMessageWithInvalidLength))); |
| 195 | |
| 196 | // Modify the message length to smaller value. |
| 197 | kStunMessageWithInvalidLength[2] = 0x00; |
| 198 | kStunMessageWithInvalidLength[3] = 0x01; |
| 199 | EXPECT_FALSE(Send(kStunMessageWithInvalidLength, |
| 200 | sizeof(kStunMessageWithInvalidLength))); |
| 201 | } |
| 202 | |
| 203 | // Verifying TURN channel data message with invalid length. |
| 204 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataWithInvalidLength) { |
| 205 | EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength, |
| 206 | sizeof(kTurnChannelDataMessageWithInvalidLength))); |
| 207 | // Modify the length to larger value. |
| 208 | kTurnChannelDataMessageWithInvalidLength[2] = 0xFF; |
| 209 | kTurnChannelDataMessageWithInvalidLength[3] = 0xF0; |
| 210 | EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength, |
| 211 | sizeof(kTurnChannelDataMessageWithInvalidLength))); |
| 212 | |
| 213 | // Modify the length to smaller value. |
| 214 | kTurnChannelDataMessageWithInvalidLength[2] = 0x00; |
| 215 | kTurnChannelDataMessageWithInvalidLength[3] = 0x00; |
| 216 | EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength, |
| 217 | sizeof(kTurnChannelDataMessageWithInvalidLength))); |
| 218 | } |
| 219 | |
| 220 | // Verifying a small buffer handled (dropped) properly. This will be |
| 221 | // a common one for both stun and turn. |
| 222 | TEST_F(AsyncStunTCPSocketTest, TestTooSmallMessageBuffer) { |
| 223 | char data[1]; |
| 224 | EXPECT_FALSE(Send(data, sizeof(data))); |
| 225 | } |
| 226 | |
| 227 | // Verifying a legal large turn message. |
| 228 | TEST_F(AsyncStunTCPSocketTest, TestMaximumSizeTurnPacket) { |
| 229 | // We have problem in getting the SignalWriteEvent from the virtual socket |
| 230 | // server. So increasing the send buffer to 64k. |
| 231 | // TODO(mallinath) - Remove this setting after we fix vss issue. |
| 232 | vss_->set_send_buffer_capacity(64 * 1024); |
| 233 | unsigned char packet[65539]; |
| 234 | packet[0] = 0x40; |
| 235 | packet[1] = 0x00; |
| 236 | packet[2] = 0xFF; |
| 237 | packet[3] = 0xFF; |
| 238 | EXPECT_TRUE(Send(packet, sizeof(packet))); |
| 239 | } |
| 240 | |
| 241 | // Verifying a legal large stun message. |
| 242 | TEST_F(AsyncStunTCPSocketTest, TestMaximumSizeStunPacket) { |
| 243 | // We have problem in getting the SignalWriteEvent from the virtual socket |
| 244 | // server. So increasing the send buffer to 64k. |
| 245 | // TODO(mallinath) - Remove this setting after we fix vss issue. |
| 246 | vss_->set_send_buffer_capacity(64 * 1024); |
| 247 | unsigned char packet[65552]; |
| 248 | packet[0] = 0x00; |
| 249 | packet[1] = 0x01; |
| 250 | packet[2] = 0xFF; |
| 251 | packet[3] = 0xFC; |
| 252 | EXPECT_TRUE(Send(packet, sizeof(packet))); |
| 253 | } |
| 254 | |
| 255 | // Investigate why WriteEvent is not signaled from VSS. |
| 256 | TEST_F(AsyncStunTCPSocketTest, DISABLED_TestWithSmallSendBuffer) { |
| 257 | vss_->set_send_buffer_capacity(1); |
| 258 | Send(kTurnChannelDataMessageWithOddLength, |
| 259 | sizeof(kTurnChannelDataMessageWithOddLength)); |
| 260 | EXPECT_EQ(1u, recv_packets_.size()); |
| 261 | EXPECT_TRUE(CheckData(kTurnChannelDataMessageWithOddLength, |
| 262 | sizeof(kTurnChannelDataMessageWithOddLength))); |
| 263 | } |
| 264 | |
| 265 | } // namespace cricket |