[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 1 | // Copyright (c) 2012 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. |
| 4 | |
| 5 | #include "net/websockets/websocket_frame.h" |
| 6 | |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 11 | #include "base/command_line.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/memory/aligned_memory.h" |
| 14 | #include "base/string_number_conversions.h" |
| 15 | #include "base/stringprintf.h" |
| 16 | #include "base/time.h" |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 17 | #include "net/base/net_errors.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 20 | // Run |
| 21 | // out/Release/net_unittests --websocket-mask-iterations=100000 |
| 22 | // --gtest_filter='WebSocketFrameTestMaskBenchmark.*' |
| 23 | // to benchmark the MaskWebSocketFramePayload() function. |
| 24 | static const char kBenchmarkIterations[] = "websocket-mask-iterations"; |
| 25 | static const int kDefaultIterations = 10; |
| 26 | static const int kLongPayloadSize = 1 << 16; |
| 27 | |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 28 | namespace net { |
| 29 | |
| 30 | TEST(WebSocketFrameHeaderTest, FrameLengths) { |
| 31 | struct TestCase { |
| 32 | const char* frame_header; |
| 33 | size_t frame_header_length; |
| 34 | uint64 frame_length; |
| 35 | }; |
| 36 | static const TestCase kTests[] = { |
| 37 | { "\x81\x00", 2, GG_UINT64_C(0) }, |
| 38 | { "\x81\x7D", 2, GG_UINT64_C(125) }, |
| 39 | { "\x81\x7E\x00\x7E", 4, GG_UINT64_C(126) }, |
| 40 | { "\x81\x7E\xFF\xFF", 4, GG_UINT64_C(0xFFFF) }, |
| 41 | { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, GG_UINT64_C(0x10000) }, |
| 42 | { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, |
| 43 | GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } |
| 44 | }; |
| 45 | static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 46 | |
| 47 | for (int i = 0; i < kNumTests; ++i) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame^] | 48 | WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 49 | header.final = true; |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 50 | header.payload_length = kTests[i].frame_length; |
| 51 | |
| 52 | std::vector<char> expected_output( |
| 53 | kTests[i].frame_header, |
| 54 | kTests[i].frame_header + kTests[i].frame_header_length); |
| 55 | std::vector<char> output(expected_output.size()); |
| 56 | EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 57 | WriteWebSocketFrameHeader(header, NULL, &output.front(), |
| 58 | output.size())); |
| 59 | EXPECT_EQ(expected_output, output); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) { |
| 64 | static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF"; |
| 65 | COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 == |
| 66 | WebSocketFrameHeader::kMaskingKeyLength, |
| 67 | incorrect_masking_key_size); |
| 68 | |
| 69 | struct TestCase { |
| 70 | const char* frame_header; |
| 71 | size_t frame_header_length; |
| 72 | uint64 frame_length; |
| 73 | }; |
| 74 | static const TestCase kTests[] = { |
| 75 | { "\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0) }, |
| 76 | { "\x81\xFD\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(125) }, |
| 77 | { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(126) }, |
| 78 | { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(0xFFFF) }, |
| 79 | { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, |
| 80 | GG_UINT64_C(0x10000) }, |
| 81 | { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, |
| 82 | GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } |
| 83 | }; |
| 84 | static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 85 | |
| 86 | WebSocketMaskingKey masking_key; |
| 87 | std::copy(kMaskingKey, kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
| 88 | masking_key.key); |
| 89 | |
| 90 | for (int i = 0; i < kNumTests; ++i) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame^] | 91 | WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 92 | header.final = true; |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 93 | header.masked = true; |
| 94 | header.payload_length = kTests[i].frame_length; |
| 95 | |
| 96 | std::vector<char> expected_output( |
| 97 | kTests[i].frame_header, |
| 98 | kTests[i].frame_header + kTests[i].frame_header_length); |
| 99 | std::vector<char> output(expected_output.size()); |
| 100 | EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 101 | WriteWebSocketFrameHeader(header, &masking_key, |
| 102 | &output.front(), output.size())); |
| 103 | EXPECT_EQ(expected_output, output); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | TEST(WebSocketFrameHeaderTest, FrameOpCodes) { |
| 108 | struct TestCase { |
| 109 | const char* frame_header; |
| 110 | size_t frame_header_length; |
| 111 | WebSocketFrameHeader::OpCode opcode; |
| 112 | }; |
| 113 | static const TestCase kTests[] = { |
| 114 | { "\x80\x00", 2, WebSocketFrameHeader::kOpCodeContinuation }, |
| 115 | { "\x81\x00", 2, WebSocketFrameHeader::kOpCodeText }, |
| 116 | { "\x82\x00", 2, WebSocketFrameHeader::kOpCodeBinary }, |
| 117 | { "\x88\x00", 2, WebSocketFrameHeader::kOpCodeClose }, |
| 118 | { "\x89\x00", 2, WebSocketFrameHeader::kOpCodePing }, |
| 119 | { "\x8A\x00", 2, WebSocketFrameHeader::kOpCodePong }, |
| 120 | // These are undefined opcodes, but the builder should accept them anyway. |
| 121 | { "\x83\x00", 2, 0x3 }, |
| 122 | { "\x84\x00", 2, 0x4 }, |
| 123 | { "\x85\x00", 2, 0x5 }, |
| 124 | { "\x86\x00", 2, 0x6 }, |
| 125 | { "\x87\x00", 2, 0x7 }, |
| 126 | { "\x8B\x00", 2, 0xB }, |
| 127 | { "\x8C\x00", 2, 0xC }, |
| 128 | { "\x8D\x00", 2, 0xD }, |
| 129 | { "\x8E\x00", 2, 0xE }, |
| 130 | { "\x8F\x00", 2, 0xF } |
| 131 | }; |
| 132 | static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 133 | |
| 134 | for (int i = 0; i < kNumTests; ++i) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame^] | 135 | WebSocketFrameHeader header(kTests[i].opcode); |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 136 | header.final = true; |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 137 | header.payload_length = 0; |
| 138 | |
| 139 | std::vector<char> expected_output( |
| 140 | kTests[i].frame_header, |
| 141 | kTests[i].frame_header + kTests[i].frame_header_length); |
| 142 | std::vector<char> output(expected_output.size()); |
| 143 | EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 144 | WriteWebSocketFrameHeader(header, NULL, |
| 145 | &output.front(), output.size())); |
| 146 | EXPECT_EQ(expected_output, output); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) { |
| 151 | struct TestCase { |
| 152 | const char* frame_header; |
| 153 | size_t frame_header_length; |
| 154 | bool final; |
| 155 | bool reserved1; |
| 156 | bool reserved2; |
| 157 | bool reserved3; |
| 158 | }; |
| 159 | static const TestCase kTests[] = { |
| 160 | { "\x81\x00", 2, true, false, false, false }, |
| 161 | { "\x01\x00", 2, false, false, false, false }, |
| 162 | { "\xC1\x00", 2, true, true, false, false }, |
| 163 | { "\xA1\x00", 2, true, false, true, false }, |
| 164 | { "\x91\x00", 2, true, false, false, true }, |
| 165 | { "\x71\x00", 2, false, true, true, true }, |
| 166 | { "\xF1\x00", 2, true, true, true, true } |
| 167 | }; |
| 168 | static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 169 | |
| 170 | for (int i = 0; i < kNumTests; ++i) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame^] | 171 | WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 172 | header.final = kTests[i].final; |
| 173 | header.reserved1 = kTests[i].reserved1; |
| 174 | header.reserved2 = kTests[i].reserved2; |
| 175 | header.reserved3 = kTests[i].reserved3; |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 176 | header.payload_length = 0; |
| 177 | |
| 178 | std::vector<char> expected_output( |
| 179 | kTests[i].frame_header, |
| 180 | kTests[i].frame_header + kTests[i].frame_header_length); |
| 181 | std::vector<char> output(expected_output.size()); |
| 182 | EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 183 | WriteWebSocketFrameHeader(header, NULL, |
| 184 | &output.front(), output.size())); |
| 185 | EXPECT_EQ(expected_output, output); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { |
| 190 | struct TestCase { |
| 191 | uint64 payload_length; |
| 192 | bool masked; |
| 193 | size_t expected_header_size; |
| 194 | }; |
| 195 | static const TestCase kTests[] = { |
| 196 | { GG_UINT64_C(0), false, 2u }, |
| 197 | { GG_UINT64_C(125), false, 2u }, |
| 198 | { GG_UINT64_C(126), false, 4u }, |
| 199 | { GG_UINT64_C(0xFFFF), false, 4u }, |
| 200 | { GG_UINT64_C(0x10000), false, 10u }, |
| 201 | { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, |
| 202 | { GG_UINT64_C(0), true, 6u }, |
| 203 | { GG_UINT64_C(125), true, 6u }, |
| 204 | { GG_UINT64_C(126), true, 8u }, |
| 205 | { GG_UINT64_C(0xFFFF), true, 8u }, |
| 206 | { GG_UINT64_C(0x10000), true, 14u }, |
| 207 | { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u } |
| 208 | }; |
| 209 | static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 210 | |
| 211 | for (int i = 0; i < kNumTests; ++i) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame^] | 212 | WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 213 | header.final = true; |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 214 | header.opcode = WebSocketFrameHeader::kOpCodeText; |
| 215 | header.masked = kTests[i].masked; |
| 216 | header.payload_length = kTests[i].payload_length; |
| 217 | |
| 218 | char dummy_buffer[14]; |
| 219 | // Set an insufficient size to |buffer_size|. |
| 220 | EXPECT_EQ(ERR_INVALID_ARGUMENT, |
| 221 | WriteWebSocketFrameHeader(header, NULL, dummy_buffer, |
| 222 | kTests[i].expected_header_size - 1)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | TEST(WebSocketFrameTest, MaskPayload) { |
| 227 | struct TestCase { |
| 228 | const char* masking_key; |
| 229 | uint64 frame_offset; |
| 230 | const char* input; |
| 231 | const char* output; |
| 232 | size_t data_length; |
| 233 | }; |
| 234 | static const TestCase kTests[] = { |
| 235 | { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, |
| 236 | { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 }, |
| 237 | { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, |
| 238 | { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 }, |
| 239 | { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, |
| 240 | { "\xDE\xAD\xBE\xEF", 42, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, |
| 241 | { "\xDE\xAD\xBE\xEF", 0, "", "", 0 }, |
| 242 | { "\xDE\xAD\xBE\xEF", 0, "\xDE\xAD\xBE\xEF", "\x00\x00\x00\x00", 4 }, |
| 243 | { "\xDE\xAD\xBE\xEF", 0, "\x00\x00\x00\x00", "\xDE\xAD\xBE\xEF", 4 }, |
| 244 | { "\x00\x00\x00\x00", 0, "FooBar", "FooBar", 6 }, |
| 245 | { "\xFF\xFF\xFF\xFF", 0, "FooBar", "\xB9\x90\x90\xBD\x9E\x8D", 6 }, |
| 246 | }; |
| 247 | static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 248 | |
| 249 | for (int i = 0; i < kNumTests; ++i) { |
| 250 | WebSocketMaskingKey masking_key; |
| 251 | std::copy(kTests[i].masking_key, |
| 252 | kTests[i].masking_key + WebSocketFrameHeader::kMaskingKeyLength, |
| 253 | masking_key.key); |
| 254 | std::vector<char> frame_data(kTests[i].input, |
| 255 | kTests[i].input + kTests[i].data_length); |
| 256 | std::vector<char> expected_output(kTests[i].output, |
| 257 | kTests[i].output + kTests[i].data_length); |
| 258 | MaskWebSocketFramePayload(masking_key, |
| 259 | kTests[i].frame_offset, |
| 260 | frame_data.empty() ? NULL : &frame_data.front(), |
| 261 | frame_data.size()); |
| 262 | EXPECT_EQ(expected_output, frame_data); |
| 263 | } |
| 264 | } |
| 265 | |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 266 | // Check that all combinations of alignment, frame offset and chunk size work |
| 267 | // correctly for MaskWebSocketFramePayload(). This is mainly used to ensure that |
| 268 | // vectorisation optimisations don't break anything. We could take a "white box" |
| 269 | // approach and only test the edge cases, but since the exhaustive "black box" |
| 270 | // approach runs in acceptable time, we don't have to take the risk of being |
| 271 | // clever. |
| 272 | // |
| 273 | // This brute-force approach runs in O(N^3) time where N is the size of the |
| 274 | // maximum vector size we want to test again. This might need reconsidering if |
| 275 | // MaskWebSocketFramePayload() is ever optimised for a dedicated vector |
| 276 | // architecture. |
| 277 | TEST(WebSocketFrameTest, MaskPayloadAlignment) { |
| 278 | // This reflects what might be implemented in the future, rather than |
| 279 | // the current implementation. FMA3 and FMA4 support 256-bit vector ops. |
| 280 | static const size_t kMaxVectorSizeInBits = 256; |
| 281 | static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8; |
| 282 | static const size_t kMaxVectorAlignment = kMaxVectorSize; |
| 283 | static const size_t kMaskingKeyLength = |
| 284 | WebSocketFrameHeader::kMaskingKeyLength; |
| 285 | static const size_t kScratchBufferSize = |
| 286 | kMaxVectorAlignment + kMaxVectorSize * 2; |
| 287 | static const char kTestMask[] = "\xd2\xba\x5a\xbe"; |
| 288 | // We use 786 bits of random input to reduce the risk of correlated errors. |
| 289 | static const char kTestInput[] = |
| 290 | { "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b" |
| 291 | "\x69\x8c\xda\x4b\xfc\xac\x2c\xd3\x49\x96\x6e\x8a\x7b\x5a\x32\x76" |
| 292 | "\xd0\x11\x43\xa0\x89\xfc\x76\x2b\x10\x2f\x4c\x7b\x4f\xa6\xdd\xe4" |
| 293 | "\xfc\x8e\xd8\x72\xcf\x7e\x37\xcd\x31\xcd\xc1\xc0\x89\x0c\xa7\x4c" |
| 294 | "\xda\xa8\x4b\x75\xa1\xcb\xa9\x77\x19\x4d\x6e\xdf\xc8\x08\x1c\xb6" |
| 295 | "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8" |
| 296 | }; |
| 297 | static const size_t kTestInputSize = arraysize(kTestInput) - 1; |
| 298 | static const char kTestOutput[] = |
| 299 | { "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5" |
| 300 | "\xbb\x36\x80\xf5\x2e\x16\x76\x6d\x9b\x2c\x34\x34\xa9\xe0\x68\xc8" |
| 301 | "\x02\xab\x19\x1e\x5b\x46\x2c\x95\xc2\x95\x16\xc5\x9d\x1c\x87\x5a" |
| 302 | "\x2e\x34\x82\xcc\x1d\xc4\x6d\x73\xe3\x77\x9b\x7e\x5b\xb6\xfd\xf2" |
| 303 | "\x08\x12\x11\xcb\x73\x71\xf3\xc9\xcb\xf7\x34\x61\x1a\xb2\x46\x08" |
| 304 | "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16" |
| 305 | }; |
| 306 | COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput), |
| 307 | output_and_input_arrays_have_the_same_length); |
| 308 | scoped_ptr_malloc<char, base::ScopedPtrAlignedFree> scratch( |
| 309 | static_cast<char*>(base::AlignedAlloc(kScratchBufferSize, |
| 310 | kMaxVectorAlignment))); |
| 311 | WebSocketMaskingKey masking_key; |
| 312 | std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key); |
| 313 | for (size_t frame_offset = 0; |
| 314 | frame_offset < kMaskingKeyLength; |
| 315 | ++frame_offset) { |
| 316 | for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) { |
| 317 | char* const aligned_scratch = scratch.get() + alignment; |
| 318 | const size_t aligned_len = |
| 319 | std::min(kScratchBufferSize - alignment, |
| 320 | kTestInputSize - frame_offset); |
| 321 | for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) { |
| 322 | memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len); |
| 323 | for (size_t chunk_start = 0; |
| 324 | chunk_start < aligned_len; |
| 325 | chunk_start += chunk_size) { |
| 326 | const size_t this_chunk_size = std::min(chunk_size, |
| 327 | aligned_len - chunk_start); |
| 328 | MaskWebSocketFramePayload(masking_key, |
| 329 | frame_offset + chunk_start, |
| 330 | aligned_scratch + chunk_start, |
| 331 | this_chunk_size); |
| 332 | } |
| 333 | // Stop the test if it fails, since we don't want to spew thousands of |
| 334 | // failures. |
| 335 | ASSERT_TRUE(std::equal(aligned_scratch, aligned_scratch + aligned_len, |
| 336 | kTestOutput + frame_offset)) |
| 337 | << "Output failed to match for frame_offset=" |
| 338 | << frame_offset |
| 339 | << ", alignment=" |
| 340 | << alignment |
| 341 | << ", chunk_size=" |
| 342 | << chunk_size; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | class WebSocketFrameTestMaskBenchmark : public testing::Test { |
| 349 | public: |
| 350 | WebSocketFrameTestMaskBenchmark() |
| 351 | : iterations_(kDefaultIterations) {} |
| 352 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 353 | virtual void SetUp() { |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 354 | std::string iterations( |
| 355 | CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 356 | kBenchmarkIterations)); |
| 357 | int benchmark_iterations = 0; |
| 358 | if (!iterations.empty() && base::StringToInt(iterations, |
| 359 | &benchmark_iterations)) { |
| 360 | iterations_ = benchmark_iterations; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void Benchmark(const char* const payload, size_t size) { |
| 365 | std::vector<char> scratch(payload, payload + size); |
| 366 | static const char kMaskingKey[] = "\xFE\xED\xBE\xEF"; |
| 367 | COMPILE_ASSERT(arraysize(kMaskingKey) == |
| 368 | WebSocketFrameHeader::kMaskingKeyLength + 1, |
| 369 | incorrect_masking_key_size); |
| 370 | WebSocketMaskingKey masking_key; |
| 371 | std::copy(kMaskingKey, kMaskingKey + |
| 372 | WebSocketFrameHeader::kMaskingKeyLength, |
| 373 | masking_key.key); |
| 374 | LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " |
| 375 | << iterations_ << " iterations"; |
| 376 | using base::TimeTicks; |
| 377 | TimeTicks start = TimeTicks::HighResNow(); |
| 378 | for (int x = 0; x < iterations_; ++x) { |
| 379 | MaskWebSocketFramePayload(masking_key, x % size, &scratch.front(), |
| 380 | scratch.size()); |
| 381 | } |
| 382 | double total_time_ms = |
| 383 | 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() / |
| 384 | iterations_; |
| 385 | LOG(INFO) << "Payload size " << size |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 386 | << base::StringPrintf(" took %.03f microseconds per iteration", |
| 387 | total_time_ms); |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | private: |
| 391 | int iterations_; |
| 392 | |
| 393 | DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark); |
| 394 | }; |
| 395 | |
| 396 | TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) { |
| 397 | static const char kShortPayload[] = "Short Payload"; |
| 398 | Benchmark(kShortPayload, arraysize(kShortPayload)); |
| 399 | } |
| 400 | |
| 401 | TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) { |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 402 | scoped_ptr<char[]> payload(new char[kLongPayloadSize]); |
[email protected] | d632a9a | 2012-12-17 12:00:58 | [diff] [blame] | 403 | std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a'); |
| 404 | Benchmark(payload.get(), kLongPayloadSize); |
| 405 | } |
| 406 | |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame^] | 407 | // "IsKnownDataOpCode" is implemented using bit-mangling for efficiency, so we |
| 408 | // need to check that the results match the actual op-codes defined. |
| 409 | TEST(WebSocketFrameHeaderTest, IsKnownDataOpCode) { |
| 410 | // Make the test less verbose. |
| 411 | typedef WebSocketFrameHeader Frame; |
| 412 | |
| 413 | // Known opcode, is used for data frames |
| 414 | EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeContinuation)); |
| 415 | EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeText)); |
| 416 | EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeBinary)); |
| 417 | |
| 418 | // Known opcode, is used for control frames |
| 419 | EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeClose)); |
| 420 | EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePing)); |
| 421 | EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePong)); |
| 422 | |
| 423 | // Check that unused opcodes return false |
| 424 | EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeDataUnused)); |
| 425 | EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeControlUnused)); |
| 426 | |
| 427 | // Check that opcodes with the 4 bit set return false |
| 428 | EXPECT_FALSE(Frame::IsKnownDataOpCode(0x6)); |
| 429 | EXPECT_FALSE(Frame::IsKnownDataOpCode(0xF)); |
| 430 | |
| 431 | // Check that out-of-range opcodes return false |
| 432 | EXPECT_FALSE(Frame::IsKnownDataOpCode(-1)); |
| 433 | EXPECT_FALSE(Frame::IsKnownDataOpCode(0xFF)); |
| 434 | } |
| 435 | |
| 436 | // "IsKnownControlOpCode" is implemented using bit-mangling as with |
| 437 | // "IsKnownDataOpCode". |
| 438 | TEST(WebSocketFrameHeaderTest, IsKnownControlOpCode) { |
| 439 | // Make the test less verbose. |
| 440 | typedef WebSocketFrameHeader Frame; |
| 441 | |
| 442 | // Known opcode, is used for data frames |
| 443 | EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeContinuation)); |
| 444 | EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeText)); |
| 445 | EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeBinary)); |
| 446 | |
| 447 | // Known opcode, is used for control frames |
| 448 | EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodeClose)); |
| 449 | EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePing)); |
| 450 | EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePong)); |
| 451 | |
| 452 | // Check that unused opcodes return false |
| 453 | EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeDataUnused)); |
| 454 | EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeControlUnused)); |
| 455 | |
| 456 | // Check that opcodes with the 4 bit set return false |
| 457 | EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); |
| 458 | EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
| 459 | |
| 460 | // Check that out-of-range opcodes return false |
| 461 | EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
| 462 | EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
| 463 | } |
| 464 | |
[email protected] | bbe30db7 | 2012-06-12 08:35:30 | [diff] [blame] | 465 | } // namespace net |