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
11 #include "p2p/base/async_stun_tcp_socket.h"
12
13 #include <stdint.h>
14 #include <string.h>
15
16 #include <list>
17 #include <memory>
18 #include <string>
19
20 #include "rtc_base/async_socket.h"
21 #include "rtc_base/network/sent_packet.h"
22 #include "rtc_base/third_party/sigslot/sigslot.h"
23 #include "rtc_base/thread.h"
24 #include "rtc_base/virtual_socket_server.h"
25 #include "test/gtest.h"
26
27 namespace cricket {
28
29 static unsigned char kStunMessageWithZeroLength[] = {
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',
33 };
34
35 static unsigned char kTurnChannelDataMessageWithZeroLength[] = {
36 0x40, 0x00, 0x00, 0x00, // length of 0 (last 2 bytes)
37 };
38
39 static unsigned char kTurnChannelDataMessage[] = {
40 0x40, 0x00, 0x00, 0x10, 0x21, 0x12, 0xA4, 0x42, '0', '1',
41 '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
42 };
43
44 static unsigned char kStunMessageWithInvalidLength[] = {
45 0x00, 0x01, 0x00, 0x10, 0x21, 0x12, 0xA4, 0x42, '0', '1',
46 '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
47 };
48
49 static unsigned char kTurnChannelDataMessageWithInvalidLength[] = {
50 0x80, 0x00, 0x00, 0x20, 0x21, 0x12, 0xA4, 0x42, '0', '1',
51 '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
52 };
53
54 static unsigned char kTurnChannelDataMessageWithOddLength[] = {
55 0x40, 0x00, 0x00, 0x05, 0x21, 0x12, 0xA4, 0x42, '0',
56 };
57
58 static const rtc::SocketAddress kClientAddr("11.11.11.11", 0);
59 static const rtc::SocketAddress kServerAddr("22.22.22.22", 0);
60
61 class AsyncStunTCPSocketTest : public ::testing::Test,
62 public sigslot::has_slots<> {
63 protected:
AsyncStunTCPSocketTest()64 AsyncStunTCPSocketTest()
65 : vss_(new rtc::VirtualSocketServer()), thread_(vss_.get()) {}
66
SetUp()67 virtual void SetUp() { CreateSockets(); }
68
CreateSockets()69 void CreateSockets() {
70 rtc::AsyncSocket* server =
71 vss_->CreateAsyncSocket(kServerAddr.family(), SOCK_STREAM);
72 server->Bind(kServerAddr);
73 recv_socket_.reset(new AsyncStunTCPSocket(server, true));
74 recv_socket_->SignalNewConnection.connect(
75 this, &AsyncStunTCPSocketTest::OnNewConnection);
76
77 rtc::AsyncSocket* client =
78 vss_->CreateAsyncSocket(kClientAddr.family(), SOCK_STREAM);
79 send_socket_.reset(AsyncStunTCPSocket::Create(
80 client, kClientAddr, recv_socket_->GetLocalAddress()));
81 send_socket_->SignalSentPacket.connect(
82 this, &AsyncStunTCPSocketTest::OnSentPacket);
83 ASSERT_TRUE(send_socket_.get() != NULL);
84 vss_->ProcessMessagesUntilIdle();
85 }
86
OnReadPacket(rtc::AsyncPacketSocket * socket,const char * data,size_t len,const rtc::SocketAddress & remote_addr,const int64_t &)87 void OnReadPacket(rtc::AsyncPacketSocket* socket,
88 const char* data,
89 size_t len,
90 const rtc::SocketAddress& remote_addr,
91 const int64_t& /* packet_time_us */) {
92 recv_packets_.push_back(std::string(data, len));
93 }
94
OnSentPacket(rtc::AsyncPacketSocket * socket,const rtc::SentPacket & packet)95 void OnSentPacket(rtc::AsyncPacketSocket* socket,
96 const rtc::SentPacket& packet) {
97 ++sent_packets_;
98 }
99
OnNewConnection(rtc::AsyncPacketSocket * server,rtc::AsyncPacketSocket * new_socket)100 void OnNewConnection(rtc::AsyncPacketSocket* server,
101 rtc::AsyncPacketSocket* new_socket) {
102 listen_socket_.reset(new_socket);
103 new_socket->SignalReadPacket.connect(this,
104 &AsyncStunTCPSocketTest::OnReadPacket);
105 }
106
Send(const void * data,size_t len)107 bool Send(const void* data, size_t len) {
108 rtc::PacketOptions options;
109 int ret =
110 send_socket_->Send(reinterpret_cast<const char*>(data), len, options);
111 vss_->ProcessMessagesUntilIdle();
112 return (ret == static_cast<int>(len));
113 }
114
CheckData(const void * data,int len)115 bool CheckData(const void* data, int len) {
116 bool ret = false;
117 if (recv_packets_.size()) {
118 std::string packet = recv_packets_.front();
119 recv_packets_.pop_front();
120 ret = (memcmp(data, packet.c_str(), len) == 0);
121 }
122 return ret;
123 }
124
125 std::unique_ptr<rtc::VirtualSocketServer> vss_;
126 rtc::AutoSocketServerThread thread_;
127 std::unique_ptr<AsyncStunTCPSocket> send_socket_;
128 std::unique_ptr<AsyncStunTCPSocket> recv_socket_;
129 std::unique_ptr<rtc::AsyncPacketSocket> listen_socket_;
130 std::list<std::string> recv_packets_;
131 int sent_packets_ = 0;
132 };
133
134 // Testing a stun packet sent/recv properly.
TEST_F(AsyncStunTCPSocketTest,TestSingleStunPacket)135 TEST_F(AsyncStunTCPSocketTest, TestSingleStunPacket) {
136 EXPECT_TRUE(
137 Send(kStunMessageWithZeroLength, sizeof(kStunMessageWithZeroLength)));
138 EXPECT_EQ(1u, recv_packets_.size());
139 EXPECT_TRUE(CheckData(kStunMessageWithZeroLength,
140 sizeof(kStunMessageWithZeroLength)));
141 }
142
143 // Verify sending multiple packets.
TEST_F(AsyncStunTCPSocketTest,TestMultipleStunPackets)144 TEST_F(AsyncStunTCPSocketTest, TestMultipleStunPackets) {
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)));
153 EXPECT_EQ(4u, recv_packets_.size());
154 }
155
156 // Verifying TURN channel data message with zero length.
TEST_F(AsyncStunTCPSocketTest,TestTurnChannelDataWithZeroLength)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.
TEST_F(AsyncStunTCPSocketTest,TestTurnChannelData)166 TEST_F(AsyncStunTCPSocketTest, TestTurnChannelData) {
167 EXPECT_TRUE(Send(kTurnChannelDataMessage, sizeof(kTurnChannelDataMessage)));
168 EXPECT_EQ(1u, recv_packets_.size());
169 EXPECT_TRUE(
170 CheckData(kTurnChannelDataMessage, sizeof(kTurnChannelDataMessage)));
171 }
172
173 // Verifying TURN channel messages which needs padding handled properly.
TEST_F(AsyncStunTCPSocketTest,TestTurnChannelDataPadding)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.
TEST_F(AsyncStunTCPSocketTest,TestStunInvalidLength)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.
TEST_F(AsyncStunTCPSocketTest,TestTurnChannelDataWithInvalidLength)202 TEST_F(AsyncStunTCPSocketTest, TestTurnChannelDataWithInvalidLength) {
203 EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength,
204 sizeof(kTurnChannelDataMessageWithInvalidLength)));
205 // Modify the length to larger value.
206 kTurnChannelDataMessageWithInvalidLength[2] = 0xFF;
207 kTurnChannelDataMessageWithInvalidLength[3] = 0xF0;
208 EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength,
209 sizeof(kTurnChannelDataMessageWithInvalidLength)));
210
211 // Modify the length to smaller value.
212 kTurnChannelDataMessageWithInvalidLength[2] = 0x00;
213 kTurnChannelDataMessageWithInvalidLength[3] = 0x00;
214 EXPECT_FALSE(Send(kTurnChannelDataMessageWithInvalidLength,
215 sizeof(kTurnChannelDataMessageWithInvalidLength)));
216 }
217
218 // Verifying a small buffer handled (dropped) properly. This will be
219 // a common one for both stun and turn.
TEST_F(AsyncStunTCPSocketTest,TestTooSmallMessageBuffer)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.
TEST_F(AsyncStunTCPSocketTest,TestMaximumSizeTurnPacket)226 TEST_F(AsyncStunTCPSocketTest, TestMaximumSizeTurnPacket) {
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.
TEST_F(AsyncStunTCPSocketTest,TestMaximumSizeStunPacket)236 TEST_F(AsyncStunTCPSocketTest, TestMaximumSizeStunPacket) {
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
245 // Test that a turn message is sent completely even if it exceeds the socket
246 // send buffer capacity.
TEST_F(AsyncStunTCPSocketTest,TestWithSmallSendBuffer)247 TEST_F(AsyncStunTCPSocketTest, TestWithSmallSendBuffer) {
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
256 // Test that SignalSentPacket is fired when a packet is sent.
TEST_F(AsyncStunTCPSocketTest,SignalSentPacketFiredWhenPacketSent)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).
TEST_F(AsyncStunTCPSocketTest,SignalSentPacketNotFiredWhenPacketNotSent)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
277 } // namespace cricket
278