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