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