Roll src/third_party/WebKit 3aa9dd9:183b2b0 (svn 201777:201778)
[chromium-blink-merge.git] / net / websockets / websocket_frame_test.cc
blobac35d90342cea20185890b87d6011d13ce0aeea1
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/websockets/websocket_frame.h"
7 #include <stdint.h>
8 #include <algorithm>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/aligned_memory.h"
13 #include "net/base/net_errors.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace net {
18 namespace {
20 TEST(WebSocketFrameHeaderTest, FrameLengths) {
21 struct TestCase {
22 const char* frame_header;
23 size_t frame_header_length;
24 uint64 frame_length;
26 static const TestCase kTests[] = {
27 { "\x81\x00", 2, UINT64_C(0) },
28 { "\x81\x7D", 2, UINT64_C(125) },
29 { "\x81\x7E\x00\x7E", 4, UINT64_C(126) },
30 { "\x81\x7E\xFF\xFF", 4, UINT64_C(0xFFFF) },
31 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, UINT64_C(0x10000) },
32 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10,
33 UINT64_C(0x7FFFFFFFFFFFFFFF) }
35 static const int kNumTests = arraysize(kTests);
37 for (int i = 0; i < kNumTests; ++i) {
38 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText);
39 header.final = true;
40 header.payload_length = kTests[i].frame_length;
42 std::vector<char> expected_output(
43 kTests[i].frame_header,
44 kTests[i].frame_header + kTests[i].frame_header_length);
45 std::vector<char> output(expected_output.size());
46 EXPECT_EQ(static_cast<int>(expected_output.size()),
47 WriteWebSocketFrameHeader(
48 header, NULL, &output.front(), output.size()));
49 EXPECT_EQ(expected_output, output);
53 TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) {
54 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF";
55 static_assert(
56 arraysize(kMaskingKey) - 1 == WebSocketFrameHeader::kMaskingKeyLength,
57 "incorrect masking key size");
59 struct TestCase {
60 const char* frame_header;
61 size_t frame_header_length;
62 uint64 frame_length;
64 static const TestCase kTests[] = {
65 { "\x81\x80\xDE\xAD\xBE\xEF", 6, UINT64_C(0) },
66 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, UINT64_C(125) },
67 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, UINT64_C(126) },
68 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, UINT64_C(0xFFFF) },
69 { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14,
70 UINT64_C(0x10000) },
71 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14,
72 UINT64_C(0x7FFFFFFFFFFFFFFF) }
74 static const int kNumTests = arraysize(kTests);
76 WebSocketMaskingKey masking_key;
77 std::copy(kMaskingKey,
78 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength,
79 masking_key.key);
81 for (int i = 0; i < kNumTests; ++i) {
82 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText);
83 header.final = true;
84 header.masked = true;
85 header.payload_length = kTests[i].frame_length;
87 std::vector<char> expected_output(
88 kTests[i].frame_header,
89 kTests[i].frame_header + kTests[i].frame_header_length);
90 std::vector<char> output(expected_output.size());
91 EXPECT_EQ(static_cast<int>(expected_output.size()),
92 WriteWebSocketFrameHeader(
93 header, &masking_key, &output.front(), output.size()));
94 EXPECT_EQ(expected_output, output);
98 TEST(WebSocketFrameHeaderTest, FrameOpCodes) {
99 struct TestCase {
100 const char* frame_header;
101 size_t frame_header_length;
102 WebSocketFrameHeader::OpCode opcode;
104 static const TestCase kTests[] = {
105 { "\x80\x00", 2, WebSocketFrameHeader::kOpCodeContinuation },
106 { "\x81\x00", 2, WebSocketFrameHeader::kOpCodeText },
107 { "\x82\x00", 2, WebSocketFrameHeader::kOpCodeBinary },
108 { "\x88\x00", 2, WebSocketFrameHeader::kOpCodeClose },
109 { "\x89\x00", 2, WebSocketFrameHeader::kOpCodePing },
110 { "\x8A\x00", 2, WebSocketFrameHeader::kOpCodePong },
111 // These are undefined opcodes, but the builder should accept them anyway.
112 { "\x83\x00", 2, 0x3 },
113 { "\x84\x00", 2, 0x4 },
114 { "\x85\x00", 2, 0x5 },
115 { "\x86\x00", 2, 0x6 },
116 { "\x87\x00", 2, 0x7 },
117 { "\x8B\x00", 2, 0xB },
118 { "\x8C\x00", 2, 0xC },
119 { "\x8D\x00", 2, 0xD },
120 { "\x8E\x00", 2, 0xE },
121 { "\x8F\x00", 2, 0xF }
123 static const int kNumTests = arraysize(kTests);
125 for (int i = 0; i < kNumTests; ++i) {
126 WebSocketFrameHeader header(kTests[i].opcode);
127 header.final = true;
128 header.payload_length = 0;
130 std::vector<char> expected_output(
131 kTests[i].frame_header,
132 kTests[i].frame_header + kTests[i].frame_header_length);
133 std::vector<char> output(expected_output.size());
134 EXPECT_EQ(static_cast<int>(expected_output.size()),
135 WriteWebSocketFrameHeader(
136 header, NULL, &output.front(), output.size()));
137 EXPECT_EQ(expected_output, output);
141 TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) {
142 struct TestCase {
143 const char* frame_header;
144 size_t frame_header_length;
145 bool final;
146 bool reserved1;
147 bool reserved2;
148 bool reserved3;
150 static const TestCase kTests[] = {
151 { "\x81\x00", 2, true, false, false, false },
152 { "\x01\x00", 2, false, false, false, false },
153 { "\xC1\x00", 2, true, true, false, false },
154 { "\xA1\x00", 2, true, false, true, false },
155 { "\x91\x00", 2, true, false, false, true },
156 { "\x71\x00", 2, false, true, true, true },
157 { "\xF1\x00", 2, true, true, true, true }
159 static const int kNumTests = arraysize(kTests);
161 for (int i = 0; i < kNumTests; ++i) {
162 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText);
163 header.final = kTests[i].final;
164 header.reserved1 = kTests[i].reserved1;
165 header.reserved2 = kTests[i].reserved2;
166 header.reserved3 = kTests[i].reserved3;
167 header.payload_length = 0;
169 std::vector<char> expected_output(
170 kTests[i].frame_header,
171 kTests[i].frame_header + kTests[i].frame_header_length);
172 std::vector<char> output(expected_output.size());
173 EXPECT_EQ(static_cast<int>(expected_output.size()),
174 WriteWebSocketFrameHeader(
175 header, NULL, &output.front(), output.size()));
176 EXPECT_EQ(expected_output, output);
180 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) {
181 struct TestCase {
182 uint64 payload_length;
183 bool masked;
184 size_t expected_header_size;
186 static const TestCase kTests[] = {
187 { UINT64_C(0), false, 2u },
188 { UINT64_C(125), false, 2u },
189 { UINT64_C(126), false, 4u },
190 { UINT64_C(0xFFFF), false, 4u },
191 { UINT64_C(0x10000), false, 10u },
192 { UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u },
193 { UINT64_C(0), true, 6u },
194 { UINT64_C(125), true, 6u },
195 { UINT64_C(126), true, 8u },
196 { UINT64_C(0xFFFF), true, 8u },
197 { UINT64_C(0x10000), true, 14u },
198 { UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u }
200 static const int kNumTests = arraysize(kTests);
202 for (int i = 0; i < kNumTests; ++i) {
203 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText);
204 header.final = true;
205 header.opcode = WebSocketFrameHeader::kOpCodeText;
206 header.masked = kTests[i].masked;
207 header.payload_length = kTests[i].payload_length;
209 char dummy_buffer[14];
210 // Set an insufficient size to |buffer_size|.
211 EXPECT_EQ(
212 ERR_INVALID_ARGUMENT,
213 WriteWebSocketFrameHeader(
214 header, NULL, dummy_buffer, kTests[i].expected_header_size - 1));
218 TEST(WebSocketFrameTest, MaskPayload) {
219 struct TestCase {
220 const char* masking_key;
221 uint64 frame_offset;
222 const char* input;
223 const char* output;
224 size_t data_length;
226 static const TestCase kTests[] = {
227 { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 },
228 { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 },
229 { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 },
230 { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 },
231 { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 },
232 { "\xDE\xAD\xBE\xEF", 42, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 },
233 { "\xDE\xAD\xBE\xEF", 0, "", "", 0 },
234 { "\xDE\xAD\xBE\xEF", 0, "\xDE\xAD\xBE\xEF", "\x00\x00\x00\x00", 4 },
235 { "\xDE\xAD\xBE\xEF", 0, "\x00\x00\x00\x00", "\xDE\xAD\xBE\xEF", 4 },
236 { "\x00\x00\x00\x00", 0, "FooBar", "FooBar", 6 },
237 { "\xFF\xFF\xFF\xFF", 0, "FooBar", "\xB9\x90\x90\xBD\x9E\x8D", 6 },
239 static const int kNumTests = arraysize(kTests);
241 for (int i = 0; i < kNumTests; ++i) {
242 WebSocketMaskingKey masking_key;
243 std::copy(kTests[i].masking_key,
244 kTests[i].masking_key + WebSocketFrameHeader::kMaskingKeyLength,
245 masking_key.key);
246 std::vector<char> frame_data(kTests[i].input,
247 kTests[i].input + kTests[i].data_length);
248 std::vector<char> expected_output(kTests[i].output,
249 kTests[i].output + kTests[i].data_length);
250 MaskWebSocketFramePayload(masking_key,
251 kTests[i].frame_offset,
252 frame_data.empty() ? NULL : &frame_data.front(),
253 frame_data.size());
254 EXPECT_EQ(expected_output, frame_data);
258 // Check that all combinations of alignment, frame offset and chunk size work
259 // correctly for MaskWebSocketFramePayload(). This is mainly used to ensure that
260 // vectorisation optimisations don't break anything. We could take a "white box"
261 // approach and only test the edge cases, but since the exhaustive "black box"
262 // approach runs in acceptable time, we don't have to take the risk of being
263 // clever.
265 // This brute-force approach runs in O(N^3) time where N is the size of the
266 // maximum vector size we want to test again. This might need reconsidering if
267 // MaskWebSocketFramePayload() is ever optimised for a dedicated vector
268 // architecture.
269 TEST(WebSocketFrameTest, MaskPayloadAlignment) {
270 // This reflects what might be implemented in the future, rather than
271 // the current implementation. FMA3 and FMA4 support 256-bit vector ops.
272 static const size_t kMaxVectorSizeInBits = 256;
273 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8;
274 static const size_t kMaxVectorAlignment = kMaxVectorSize;
275 static const size_t kMaskingKeyLength =
276 WebSocketFrameHeader::kMaskingKeyLength;
277 static const size_t kScratchBufferSize =
278 kMaxVectorAlignment + kMaxVectorSize * 2;
279 static const char kTestMask[] = "\xd2\xba\x5a\xbe";
280 // We use 786 bits of random input to reduce the risk of correlated errors.
281 static const char kTestInput[] = {
282 "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b"
283 "\x69\x8c\xda\x4b\xfc\xac\x2c\xd3\x49\x96\x6e\x8a\x7b\x5a\x32\x76"
284 "\xd0\x11\x43\xa0\x89\xfc\x76\x2b\x10\x2f\x4c\x7b\x4f\xa6\xdd\xe4"
285 "\xfc\x8e\xd8\x72\xcf\x7e\x37\xcd\x31\xcd\xc1\xc0\x89\x0c\xa7\x4c"
286 "\xda\xa8\x4b\x75\xa1\xcb\xa9\x77\x19\x4d\x6e\xdf\xc8\x08\x1c\xb6"
287 "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8"
289 static const size_t kTestInputSize = arraysize(kTestInput) - 1;
290 static const char kTestOutput[] = {
291 "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5"
292 "\xbb\x36\x80\xf5\x2e\x16\x76\x6d\x9b\x2c\x34\x34\xa9\xe0\x68\xc8"
293 "\x02\xab\x19\x1e\x5b\x46\x2c\x95\xc2\x95\x16\xc5\x9d\x1c\x87\x5a"
294 "\x2e\x34\x82\xcc\x1d\xc4\x6d\x73\xe3\x77\x9b\x7e\x5b\xb6\xfd\xf2"
295 "\x08\x12\x11\xcb\x73\x71\xf3\xc9\xcb\xf7\x34\x61\x1a\xb2\x46\x08"
296 "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16"
298 static_assert(arraysize(kTestInput) == arraysize(kTestOutput),
299 "output and input arrays should have the same length");
300 scoped_ptr<char, base::AlignedFreeDeleter> scratch(
301 static_cast<char*>(
302 base::AlignedAlloc(kScratchBufferSize, kMaxVectorAlignment)));
303 WebSocketMaskingKey masking_key;
304 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key);
305 for (size_t frame_offset = 0; frame_offset < kMaskingKeyLength;
306 ++frame_offset) {
307 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) {
308 char* const aligned_scratch = scratch.get() + alignment;
309 const size_t aligned_len = std::min(kScratchBufferSize - alignment,
310 kTestInputSize - frame_offset);
311 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) {
312 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len);
313 for (size_t chunk_start = 0; chunk_start < aligned_len;
314 chunk_start += chunk_size) {
315 const size_t this_chunk_size =
316 std::min(chunk_size, aligned_len - chunk_start);
317 MaskWebSocketFramePayload(masking_key,
318 frame_offset + chunk_start,
319 aligned_scratch + chunk_start,
320 this_chunk_size);
322 // Stop the test if it fails, since we don't want to spew thousands of
323 // failures.
324 ASSERT_TRUE(std::equal(aligned_scratch,
325 aligned_scratch + aligned_len,
326 kTestOutput + frame_offset))
327 << "Output failed to match for frame_offset=" << frame_offset
328 << ", alignment=" << alignment << ", chunk_size=" << chunk_size;
334 // "IsKnownDataOpCode" is currently implemented in an "obviously correct"
335 // manner, but we test is anyway in case it changes to a more complex
336 // implementation in future.
337 TEST(WebSocketFrameHeaderTest, IsKnownDataOpCode) {
338 // Make the test less verbose.
339 typedef WebSocketFrameHeader Frame;
341 // Known opcode, is used for data frames
342 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeContinuation));
343 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeText));
344 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeBinary));
346 // Known opcode, is used for control frames
347 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeClose));
348 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePing));
349 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePong));
351 // Check that unused opcodes return false
352 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeDataUnused));
353 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeControlUnused));
355 // Check that opcodes with the 4 bit set return false
356 EXPECT_FALSE(Frame::IsKnownDataOpCode(0x6));
357 EXPECT_FALSE(Frame::IsKnownDataOpCode(0xF));
359 // Check that out-of-range opcodes return false
360 EXPECT_FALSE(Frame::IsKnownDataOpCode(-1));
361 EXPECT_FALSE(Frame::IsKnownDataOpCode(0xFF));
364 // "IsKnownControlOpCode" is implemented in an "obviously correct" manner but
365 // might be optimised in future.
366 TEST(WebSocketFrameHeaderTest, IsKnownControlOpCode) {
367 // Make the test less verbose.
368 typedef WebSocketFrameHeader Frame;
370 // Known opcode, is used for data frames
371 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeContinuation));
372 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeText));
373 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeBinary));
375 // Known opcode, is used for control frames
376 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodeClose));
377 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePing));
378 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePong));
380 // Check that unused opcodes return false
381 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeDataUnused));
382 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeControlUnused));
384 // Check that opcodes with the 4 bit set return false
385 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6));
386 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF));
388 // Check that out-of-range opcodes return false
389 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1));
390 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF));
393 } // namespace
395 } // namespace net