henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [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 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "p2p/base/async_stun_tcp_socket.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Steve Anton | 6c38cc7 | 2017-11-29 18:25:58 | [diff] [blame] | 16 | #include <list> |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 17 | #include <memory> |
Steve Anton | 6c38cc7 | 2017-11-29 18:25:58 | [diff] [blame] | 18 | #include <string> |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 19 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/async_socket.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include "rtc_base/network/sent_packet.h" |
| 22 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 23 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "rtc_base/virtual_socket_server.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 25 | #include "test/gtest.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 26 | |
| 27 | namespace cricket { |
| 28 | |
| 29 | static unsigned char kStunMessageWithZeroLength[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 30 | 0x00, 0x01, 0x00, 0x00, // length of 0 (last 2 bytes) |
| 31 | 0x21, 0x12, 0xA4, 0x42, '0', '1', '2', '3', |
| 32 | '4', '5', '6', '7', '8', '9', 'a', 'b', |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 33 | }; |
| 34 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 35 | static unsigned char kTurnChannelDataMessageWithZeroLength[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 36 | 0x40, 0x00, 0x00, 0x00, // length of 0 (last 2 bytes) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static unsigned char kTurnChannelDataMessage[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 40 | 0x40, 0x00, 0x00, 0x10, 0x21, 0x12, 0xA4, 0x42, '0', '1', |
| 41 | '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static unsigned char kStunMessageWithInvalidLength[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 45 | 0x00, 0x01, 0x00, 0x10, 0x21, 0x12, 0xA4, 0x42, '0', '1', |
| 46 | '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static unsigned char kTurnChannelDataMessageWithInvalidLength[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 50 | 0x80, 0x00, 0x00, 0x20, 0x21, 0x12, 0xA4, 0x42, '0', '1', |
| 51 | '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static unsigned char kTurnChannelDataMessageWithOddLength[] = { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 55 | 0x40, 0x00, 0x00, 0x05, 0x21, 0x12, 0xA4, 0x42, '0', |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 56 | }; |
| 57 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 58 | static const rtc::SocketAddress kClientAddr("11.11.11.11", 0); |
| 59 | static const rtc::SocketAddress kServerAddr("22.22.22.22", 0); |
| 60 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 61 | class AsyncStunTCPSocketTest : public ::testing::Test, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 62 | public sigslot::has_slots<> { |
| 63 | protected: |
| 64 | AsyncStunTCPSocketTest() |
deadbeef | 98e186c | 2017-05-17 01:00:06 | [diff] [blame] | 65 | : vss_(new rtc::VirtualSocketServer()), thread_(vss_.get()) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 66 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 67 | virtual void SetUp() { CreateSockets(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 68 | |
| 69 | void CreateSockets() { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 70 | rtc::AsyncSocket* server = |
| 71 | vss_->CreateAsyncSocket(kServerAddr.family(), SOCK_STREAM); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 72 | server->Bind(kServerAddr); |
| 73 | recv_socket_.reset(new AsyncStunTCPSocket(server, true)); |
| 74 | recv_socket_->SignalNewConnection.connect( |
| 75 | this, &AsyncStunTCPSocketTest::OnNewConnection); |
| 76 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 77 | rtc::AsyncSocket* client = |
| 78 | vss_->CreateAsyncSocket(kClientAddr.family(), SOCK_STREAM); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 79 | send_socket_.reset(AsyncStunTCPSocket::Create( |
| 80 | client, kClientAddr, recv_socket_->GetLocalAddress())); |
deadbeef | b56671e | 2017-05-27 01:40:05 | [diff] [blame] | 81 | send_socket_->SignalSentPacket.connect( |
| 82 | this, &AsyncStunTCPSocketTest::OnSentPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 83 | ASSERT_TRUE(send_socket_.get() != NULL); |
| 84 | vss_->ProcessMessagesUntilIdle(); |
| 85 | } |
| 86 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 87 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 88 | const char* data, |
| 89 | size_t len, |
| 90 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 91 | const int64_t& /* packet_time_us */) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 92 | recv_packets_.push_back(std::string(data, len)); |
| 93 | } |
| 94 | |
deadbeef | b56671e | 2017-05-27 01:40:05 | [diff] [blame] | 95 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 96 | const rtc::SentPacket& packet) { |
| 97 | ++sent_packets_; |
| 98 | } |
| 99 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 100 | void OnNewConnection(rtc::AsyncPacketSocket* server, |
| 101 | rtc::AsyncPacketSocket* new_socket) { |
| 102 | listen_socket_.reset(new_socket); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 103 | new_socket->SignalReadPacket.connect(this, |
| 104 | &AsyncStunTCPSocketTest::OnReadPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool Send(const void* data, size_t len) { |
| 108 | rtc::PacketOptions options; |
Steve Anton | 3fa2b80 | 2020-01-31 18:31:53 | [diff] [blame] | 109 | int ret = |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 110 | send_socket_->Send(reinterpret_cast<const char*>(data), len, options); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 111 | vss_->ProcessMessagesUntilIdle(); |
Steve Anton | 3fa2b80 | 2020-01-31 18:31:53 | [diff] [blame] | 112 | return (ret == static_cast<int>(len)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool CheckData(const void* data, int len) { |
| 116 | bool ret = false; |
| 117 | if (recv_packets_.size()) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 118 | std::string packet = recv_packets_.front(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 119 | recv_packets_.pop_front(); |
| 120 | ret = (memcmp(data, packet.c_str(), len) == 0); |
| 121 | } |
| 122 | return ret; |
| 123 | } |
| 124 | |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 125 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 126 | rtc::AutoSocketServerThread thread_; |
kwiberg | 3ec4679 | 2016-04-27 14:22:53 | [diff] [blame] | 127 | std::unique_ptr<AsyncStunTCPSocket> send_socket_; |
| 128 | std::unique_ptr<AsyncStunTCPSocket> recv_socket_; |
| 129 | std::unique_ptr<rtc::AsyncPacketSocket> listen_socket_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 130 | std::list<std::string> recv_packets_; |
deadbeef | b56671e | 2017-05-27 01:40:05 | [diff] [blame] | 131 | int sent_packets_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | // Testing a stun packet sent/recv properly. |
| 135 | TEST_F(AsyncStunTCPSocketTest, TestSingleStunPacket) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 136 | EXPECT_TRUE( |
| 137 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 138 | EXPECT_EQ(1u, recv_packets_.size()); |
| 139 | EXPECT_TRUE(CheckData(kStunMessageWithZeroLength, |
| 140 | sizeof(kStunMessageWithZeroLength))); |
| 141 | } |
| 142 | |
| 143 | // Verify sending multiple packets. |
| 144 | TEST_F(AsyncStunTCPSocketTest, TestMultipleStunPackets) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 145 | EXPECT_TRUE( |
| 146 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
| 147 | EXPECT_TRUE( |
| 148 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
| 149 | EXPECT_TRUE( |
| 150 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
| 151 | EXPECT_TRUE( |
| 152 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 153 | EXPECT_EQ(4u, recv_packets_.size()); |
| 154 | } |
| 155 | |
| 156 | // Verifying TURN channel data message with zero length. |
| 157 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataWithZeroLength) { |
| 158 | EXPECT_TRUE(Send(kTurnChannelDataMessageWithZeroLength, |
| 159 | sizeof(kTurnChannelDataMessageWithZeroLength))); |
| 160 | EXPECT_EQ(1u, recv_packets_.size()); |
| 161 | EXPECT_TRUE(CheckData(kTurnChannelDataMessageWithZeroLength, |
| 162 | sizeof(kTurnChannelDataMessageWithZeroLength))); |
| 163 | } |
| 164 | |
| 165 | // Verifying TURN channel data message. |
| 166 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelData) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 167 | EXPECT_TRUE(Send(kTurnChannelDataMessage, sizeof(kTurnChannelDataMessage))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 168 | EXPECT_EQ(1u, recv_packets_.size()); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 169 | EXPECT_TRUE( |
| 170 | CheckData(kTurnChannelDataMessage, sizeof(kTurnChannelDataMessage))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Verifying TURN channel messages which needs padding handled properly. |
| 174 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataPadding) { |
| 175 | EXPECT_TRUE(Send(kTurnChannelDataMessageWithOddLength, |
| 176 | sizeof(kTurnChannelDataMessageWithOddLength))); |
| 177 | EXPECT_EQ(1u, recv_packets_.size()); |
| 178 | EXPECT_TRUE(CheckData(kTurnChannelDataMessageWithOddLength, |
| 179 | sizeof(kTurnChannelDataMessageWithOddLength))); |
| 180 | } |
| 181 | |
| 182 | // Verifying stun message with invalid length. |
| 183 | TEST_F(AsyncStunTCPSocketTest, TestStunInvalidLength) { |
| 184 | EXPECT_FALSE(Send(kStunMessageWithInvalidLength, |
| 185 | sizeof(kStunMessageWithInvalidLength))); |
| 186 | EXPECT_EQ(0u, recv_packets_.size()); |
| 187 | |
| 188 | // Modify the message length to larger value. |
| 189 | kStunMessageWithInvalidLength[2] = 0xFF; |
| 190 | kStunMessageWithInvalidLength[3] = 0xFF; |
| 191 | EXPECT_FALSE(Send(kStunMessageWithInvalidLength, |
| 192 | sizeof(kStunMessageWithInvalidLength))); |
| 193 | |
| 194 | // Modify the message length to smaller value. |
| 195 | kStunMessageWithInvalidLength[2] = 0x00; |
| 196 | kStunMessageWithInvalidLength[3] = 0x01; |
| 197 | EXPECT_FALSE(Send(kStunMessageWithInvalidLength, |
| 198 | sizeof(kStunMessageWithInvalidLength))); |
| 199 | } |
| 200 | |
| 201 | // Verifying TURN channel data message with invalid length. |
| 202 | TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataWithInvalidLength) { |
| 203 | EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 204 | sizeof(kTurnChannelDataMessageWithInvalidLength))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 205 | // Modify the length to larger value. |
| 206 | kTurnChannelDataMessageWithInvalidLength[2] = 0xFF; |
| 207 | kTurnChannelDataMessageWithInvalidLength[3] = 0xF0; |
| 208 | EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 209 | sizeof(kTurnChannelDataMessageWithInvalidLength))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 210 | |
| 211 | // Modify the length to smaller value. |
| 212 | kTurnChannelDataMessageWithInvalidLength[2] = 0x00; |
| 213 | kTurnChannelDataMessageWithInvalidLength[3] = 0x00; |
| 214 | EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 215 | sizeof(kTurnChannelDataMessageWithInvalidLength))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Verifying a small buffer handled (dropped) properly. This will be |
| 219 | // a common one for both stun and turn. |
| 220 | TEST_F(AsyncStunTCPSocketTest, TestTooSmallMessageBuffer) { |
| 221 | char data[1]; |
| 222 | EXPECT_FALSE(Send(data, sizeof(data))); |
| 223 | } |
| 224 | |
| 225 | // Verifying a legal large turn message. |
| 226 | TEST_F(AsyncStunTCPSocketTest, TestMaximumSizeTurnPacket) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 227 | unsigned char packet[65539]; |
| 228 | packet[0] = 0x40; |
| 229 | packet[1] = 0x00; |
| 230 | packet[2] = 0xFF; |
| 231 | packet[3] = 0xFF; |
| 232 | EXPECT_TRUE(Send(packet, sizeof(packet))); |
| 233 | } |
| 234 | |
| 235 | // Verifying a legal large stun message. |
| 236 | TEST_F(AsyncStunTCPSocketTest, TestMaximumSizeStunPacket) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 237 | unsigned char packet[65552]; |
| 238 | packet[0] = 0x00; |
| 239 | packet[1] = 0x01; |
| 240 | packet[2] = 0xFF; |
| 241 | packet[3] = 0xFC; |
| 242 | EXPECT_TRUE(Send(packet, sizeof(packet))); |
| 243 | } |
| 244 | |
Steve Anton | 3fa2b80 | 2020-01-31 18:31:53 | [diff] [blame] | 245 | // Test that a turn message is sent completely even if it exceeds the socket |
| 246 | // send buffer capacity. |
| 247 | TEST_F(AsyncStunTCPSocketTest, TestWithSmallSendBuffer) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 248 | vss_->set_send_buffer_capacity(1); |
| 249 | Send(kTurnChannelDataMessageWithOddLength, |
| 250 | sizeof(kTurnChannelDataMessageWithOddLength)); |
| 251 | EXPECT_EQ(1u, recv_packets_.size()); |
| 252 | EXPECT_TRUE(CheckData(kTurnChannelDataMessageWithOddLength, |
| 253 | sizeof(kTurnChannelDataMessageWithOddLength))); |
| 254 | } |
| 255 | |
deadbeef | b56671e | 2017-05-27 01:40:05 | [diff] [blame] | 256 | // Test that SignalSentPacket is fired when a packet is sent. |
| 257 | TEST_F(AsyncStunTCPSocketTest, SignalSentPacketFiredWhenPacketSent) { |
| 258 | ASSERT_TRUE( |
| 259 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
| 260 | EXPECT_EQ(1, sent_packets_); |
| 261 | // Send another packet for good measure. |
| 262 | ASSERT_TRUE( |
| 263 | Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength))); |
| 264 | EXPECT_EQ(2, sent_packets_); |
| 265 | } |
| 266 | |
| 267 | // Test that SignalSentPacket isn't fired when a packet isn't sent (for |
| 268 | // example, because it's invalid). |
| 269 | TEST_F(AsyncStunTCPSocketTest, SignalSentPacketNotFiredWhenPacketNotSent) { |
| 270 | // Attempt to send a packet that's too small; since it isn't sent, |
| 271 | // SignalSentPacket shouldn't fire. |
| 272 | char data[1]; |
| 273 | ASSERT_FALSE(Send(data, sizeof(data))); |
| 274 | EXPECT_EQ(0, sent_packets_); |
| 275 | } |
| 276 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 277 | } // namespace cricket |