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