| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/websockets/websocket_frame.h" | 5 #include "net/websockets/websocket_frame.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/aligned_memory.h" | 14 #include "base/memory/aligned_memory.h" |
| 14 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 15 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 16 #include "base/time.h" | 17 #include "base/time.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 47 for (int i = 0; i < kNumTests; ++i) { | 48 for (int i = 0; i < kNumTests; ++i) { |
| 48 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 49 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 49 header.final = true; | 50 header.final = true; |
| 50 header.payload_length = kTests[i].frame_length; | 51 header.payload_length = kTests[i].frame_length; |
| 51 | 52 |
| 52 std::vector<char> expected_output( | 53 std::vector<char> expected_output( |
| 53 kTests[i].frame_header, | 54 kTests[i].frame_header, |
| 54 kTests[i].frame_header + kTests[i].frame_header_length); | 55 kTests[i].frame_header + kTests[i].frame_header_length); |
| 55 std::vector<char> output(expected_output.size()); | 56 std::vector<char> output(expected_output.size()); |
| 56 EXPECT_EQ(static_cast<int>(expected_output.size()), | 57 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 57 WriteWebSocketFrameHeader(header, NULL, &output.front(), | 58 WriteWebSocketFrameHeader( |
| 58 output.size())); | 59 header, NULL, &output.front(), output.size())); |
| 59 EXPECT_EQ(expected_output, output); | 60 EXPECT_EQ(expected_output, output); |
| 60 } | 61 } |
| 61 } | 62 } |
| 62 | 63 |
| 63 TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) { | 64 TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) { |
| 64 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF"; | 65 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF"; |
| 65 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 == | 66 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 == |
| 66 WebSocketFrameHeader::kMaskingKeyLength, | 67 WebSocketFrameHeader::kMaskingKeyLength, |
| 67 incorrect_masking_key_size); | 68 incorrect_masking_key_size); |
| 68 | 69 |
| 69 struct TestCase { | 70 struct TestCase { |
| 70 const char* frame_header; | 71 const char* frame_header; |
| 71 size_t frame_header_length; | 72 size_t frame_header_length; |
| 72 uint64 frame_length; | 73 uint64 frame_length; |
| 73 }; | 74 }; |
| 74 static const TestCase kTests[] = { | 75 static const TestCase kTests[] = { |
| 75 { "\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0) }, | 76 { "\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\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\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\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 { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, |
| 80 GG_UINT64_C(0x10000) }, | 81 GG_UINT64_C(0x10000) }, |
| 81 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, | 82 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, |
| 82 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | 83 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } |
| 83 }; | 84 }; |
| 84 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 85 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 85 | 86 |
| 86 WebSocketMaskingKey masking_key; | 87 WebSocketMaskingKey masking_key; |
| 87 std::copy(kMaskingKey, kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, | 88 std::copy(kMaskingKey, |
| 89 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
| 88 masking_key.key); | 90 masking_key.key); |
| 89 | 91 |
| 90 for (int i = 0; i < kNumTests; ++i) { | 92 for (int i = 0; i < kNumTests; ++i) { |
| 91 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 93 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 92 header.final = true; | 94 header.final = true; |
| 93 header.masked = true; | 95 header.masked = true; |
| 94 header.payload_length = kTests[i].frame_length; | 96 header.payload_length = kTests[i].frame_length; |
| 95 | 97 |
| 96 std::vector<char> expected_output( | 98 std::vector<char> expected_output( |
| 97 kTests[i].frame_header, | 99 kTests[i].frame_header, |
| 98 kTests[i].frame_header + kTests[i].frame_header_length); | 100 kTests[i].frame_header + kTests[i].frame_header_length); |
| 99 std::vector<char> output(expected_output.size()); | 101 std::vector<char> output(expected_output.size()); |
| 100 EXPECT_EQ(static_cast<int>(expected_output.size()), | 102 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 101 WriteWebSocketFrameHeader(header, &masking_key, | 103 WriteWebSocketFrameHeader( |
| 102 &output.front(), output.size())); | 104 header, &masking_key, &output.front(), output.size())); |
| 103 EXPECT_EQ(expected_output, output); | 105 EXPECT_EQ(expected_output, output); |
| 104 } | 106 } |
| 105 } | 107 } |
| 106 | 108 |
| 107 TEST(WebSocketFrameHeaderTest, FrameOpCodes) { | 109 TEST(WebSocketFrameHeaderTest, FrameOpCodes) { |
| 108 struct TestCase { | 110 struct TestCase { |
| 109 const char* frame_header; | 111 const char* frame_header; |
| 110 size_t frame_header_length; | 112 size_t frame_header_length; |
| 111 WebSocketFrameHeader::OpCode opcode; | 113 WebSocketFrameHeader::OpCode opcode; |
| 112 }; | 114 }; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 134 for (int i = 0; i < kNumTests; ++i) { | 136 for (int i = 0; i < kNumTests; ++i) { |
| 135 WebSocketFrameHeader header(kTests[i].opcode); | 137 WebSocketFrameHeader header(kTests[i].opcode); |
| 136 header.final = true; | 138 header.final = true; |
| 137 header.payload_length = 0; | 139 header.payload_length = 0; |
| 138 | 140 |
| 139 std::vector<char> expected_output( | 141 std::vector<char> expected_output( |
| 140 kTests[i].frame_header, | 142 kTests[i].frame_header, |
| 141 kTests[i].frame_header + kTests[i].frame_header_length); | 143 kTests[i].frame_header + kTests[i].frame_header_length); |
| 142 std::vector<char> output(expected_output.size()); | 144 std::vector<char> output(expected_output.size()); |
| 143 EXPECT_EQ(static_cast<int>(expected_output.size()), | 145 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 144 WriteWebSocketFrameHeader(header, NULL, | 146 WriteWebSocketFrameHeader( |
| 145 &output.front(), output.size())); | 147 header, NULL, &output.front(), output.size())); |
| 146 EXPECT_EQ(expected_output, output); | 148 EXPECT_EQ(expected_output, output); |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 | 151 |
| 150 TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) { | 152 TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) { |
| 151 struct TestCase { | 153 struct TestCase { |
| 152 const char* frame_header; | 154 const char* frame_header; |
| 153 size_t frame_header_length; | 155 size_t frame_header_length; |
| 154 bool final; | 156 bool final; |
| 155 bool reserved1; | 157 bool reserved1; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 173 header.reserved1 = kTests[i].reserved1; | 175 header.reserved1 = kTests[i].reserved1; |
| 174 header.reserved2 = kTests[i].reserved2; | 176 header.reserved2 = kTests[i].reserved2; |
| 175 header.reserved3 = kTests[i].reserved3; | 177 header.reserved3 = kTests[i].reserved3; |
| 176 header.payload_length = 0; | 178 header.payload_length = 0; |
| 177 | 179 |
| 178 std::vector<char> expected_output( | 180 std::vector<char> expected_output( |
| 179 kTests[i].frame_header, | 181 kTests[i].frame_header, |
| 180 kTests[i].frame_header + kTests[i].frame_header_length); | 182 kTests[i].frame_header + kTests[i].frame_header_length); |
| 181 std::vector<char> output(expected_output.size()); | 183 std::vector<char> output(expected_output.size()); |
| 182 EXPECT_EQ(static_cast<int>(expected_output.size()), | 184 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 183 WriteWebSocketFrameHeader(header, NULL, | 185 WriteWebSocketFrameHeader( |
| 184 &output.front(), output.size())); | 186 header, NULL, &output.front(), output.size())); |
| 185 EXPECT_EQ(expected_output, output); | 187 EXPECT_EQ(expected_output, output); |
| 186 } | 188 } |
| 187 } | 189 } |
| 188 | 190 |
| 189 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { | 191 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { |
| 190 struct TestCase { | 192 struct TestCase { |
| 191 uint64 payload_length; | 193 uint64 payload_length; |
| 192 bool masked; | 194 bool masked; |
| 193 size_t expected_header_size; | 195 size_t expected_header_size; |
| 194 }; | 196 }; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 210 | 212 |
| 211 for (int i = 0; i < kNumTests; ++i) { | 213 for (int i = 0; i < kNumTests; ++i) { |
| 212 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 214 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 213 header.final = true; | 215 header.final = true; |
| 214 header.opcode = WebSocketFrameHeader::kOpCodeText; | 216 header.opcode = WebSocketFrameHeader::kOpCodeText; |
| 215 header.masked = kTests[i].masked; | 217 header.masked = kTests[i].masked; |
| 216 header.payload_length = kTests[i].payload_length; | 218 header.payload_length = kTests[i].payload_length; |
| 217 | 219 |
| 218 char dummy_buffer[14]; | 220 char dummy_buffer[14]; |
| 219 // Set an insufficient size to |buffer_size|. | 221 // Set an insufficient size to |buffer_size|. |
| 220 EXPECT_EQ(ERR_INVALID_ARGUMENT, | 222 EXPECT_EQ( |
| 221 WriteWebSocketFrameHeader(header, NULL, dummy_buffer, | 223 ERR_INVALID_ARGUMENT, |
| 222 kTests[i].expected_header_size - 1)); | 224 WriteWebSocketFrameHeader( |
| 225 header, NULL, dummy_buffer, kTests[i].expected_header_size - 1)); |
| 223 } | 226 } |
| 224 } | 227 } |
| 225 | 228 |
| 226 TEST(WebSocketFrameTest, MaskPayload) { | 229 TEST(WebSocketFrameTest, MaskPayload) { |
| 227 struct TestCase { | 230 struct TestCase { |
| 228 const char* masking_key; | 231 const char* masking_key; |
| 229 uint64 frame_offset; | 232 uint64 frame_offset; |
| 230 const char* input; | 233 const char* input; |
| 231 const char* output; | 234 const char* output; |
| 232 size_t data_length; | 235 size_t data_length; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 // the current implementation. FMA3 and FMA4 support 256-bit vector ops. | 282 // the current implementation. FMA3 and FMA4 support 256-bit vector ops. |
| 280 static const size_t kMaxVectorSizeInBits = 256; | 283 static const size_t kMaxVectorSizeInBits = 256; |
| 281 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8; | 284 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8; |
| 282 static const size_t kMaxVectorAlignment = kMaxVectorSize; | 285 static const size_t kMaxVectorAlignment = kMaxVectorSize; |
| 283 static const size_t kMaskingKeyLength = | 286 static const size_t kMaskingKeyLength = |
| 284 WebSocketFrameHeader::kMaskingKeyLength; | 287 WebSocketFrameHeader::kMaskingKeyLength; |
| 285 static const size_t kScratchBufferSize = | 288 static const size_t kScratchBufferSize = |
| 286 kMaxVectorAlignment + kMaxVectorSize * 2; | 289 kMaxVectorAlignment + kMaxVectorSize * 2; |
| 287 static const char kTestMask[] = "\xd2\xba\x5a\xbe"; | 290 static const char kTestMask[] = "\xd2\xba\x5a\xbe"; |
| 288 // We use 786 bits of random input to reduce the risk of correlated errors. | 291 // We use 786 bits of random input to reduce the risk of correlated errors. |
| 289 static const char kTestInput[] = | 292 static const char kTestInput[] = { |
| 290 { "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b" | 293 "\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" | 294 "\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" | 295 "\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" | 296 "\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" | 297 "\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" | 298 "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8" |
| 296 }; | 299 }; |
| 297 static const size_t kTestInputSize = arraysize(kTestInput) - 1; | 300 static const size_t kTestInputSize = arraysize(kTestInput) - 1; |
| 298 static const char kTestOutput[] = | 301 static const char kTestOutput[] = { |
| 299 { "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5" | 302 "\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" | 303 "\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" | 304 "\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" | 305 "\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" | 306 "\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" | 307 "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16" |
| 305 }; | 308 }; |
| 306 COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput), | 309 COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput), |
| 307 output_and_input_arrays_have_the_same_length); | 310 output_and_input_arrays_have_the_same_length); |
| 308 scoped_ptr_malloc<char, base::ScopedPtrAlignedFree> scratch( | 311 scoped_ptr_malloc<char, base::ScopedPtrAlignedFree> scratch( |
| 309 static_cast<char*>(base::AlignedAlloc(kScratchBufferSize, | 312 static_cast<char*>( |
| 310 kMaxVectorAlignment))); | 313 base::AlignedAlloc(kScratchBufferSize, kMaxVectorAlignment))); |
| 311 WebSocketMaskingKey masking_key; | 314 WebSocketMaskingKey masking_key; |
| 312 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key); | 315 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key); |
| 313 for (size_t frame_offset = 0; | 316 for (size_t frame_offset = 0; frame_offset < kMaskingKeyLength; |
| 314 frame_offset < kMaskingKeyLength; | |
| 315 ++frame_offset) { | 317 ++frame_offset) { |
| 316 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) { | 318 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) { |
| 317 char* const aligned_scratch = scratch.get() + alignment; | 319 char* const aligned_scratch = scratch.get() + alignment; |
| 318 const size_t aligned_len = | 320 const size_t aligned_len = std::min(kScratchBufferSize - alignment, |
| 319 std::min(kScratchBufferSize - alignment, | 321 kTestInputSize - frame_offset); |
| 320 kTestInputSize - frame_offset); | |
| 321 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) { | 322 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) { |
| 322 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len); | 323 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len); |
| 323 for (size_t chunk_start = 0; | 324 for (size_t chunk_start = 0; chunk_start < aligned_len; |
| 324 chunk_start < aligned_len; | |
| 325 chunk_start += chunk_size) { | 325 chunk_start += chunk_size) { |
| 326 const size_t this_chunk_size = std::min(chunk_size, | 326 const size_t this_chunk_size = |
| 327 aligned_len - chunk_start); | 327 std::min(chunk_size, aligned_len - chunk_start); |
| 328 MaskWebSocketFramePayload(masking_key, | 328 MaskWebSocketFramePayload(masking_key, |
| 329 frame_offset + chunk_start, | 329 frame_offset + chunk_start, |
| 330 aligned_scratch + chunk_start, | 330 aligned_scratch + chunk_start, |
| 331 this_chunk_size); | 331 this_chunk_size); |
| 332 } | 332 } |
| 333 // Stop the test if it fails, since we don't want to spew thousands of | 333 // Stop the test if it fails, since we don't want to spew thousands of |
| 334 // failures. | 334 // failures. |
| 335 ASSERT_TRUE(std::equal(aligned_scratch, aligned_scratch + aligned_len, | 335 ASSERT_TRUE(std::equal(aligned_scratch, |
| 336 aligned_scratch + aligned_len, |
| 336 kTestOutput + frame_offset)) | 337 kTestOutput + frame_offset)) |
| 337 << "Output failed to match for frame_offset=" | 338 << "Output failed to match for frame_offset=" << frame_offset |
| 338 << frame_offset | 339 << ", alignment=" << alignment << ", chunk_size=" << chunk_size; |
| 339 << ", alignment=" | |
| 340 << alignment | |
| 341 << ", chunk_size=" | |
| 342 << chunk_size; | |
| 343 } | 340 } |
| 344 } | 341 } |
| 345 } | 342 } |
| 346 } | 343 } |
| 347 | 344 |
| 348 class WebSocketFrameTestMaskBenchmark : public testing::Test { | 345 class WebSocketFrameTestMaskBenchmark : public testing::Test { |
| 349 public: | 346 public: |
| 350 WebSocketFrameTestMaskBenchmark() | 347 WebSocketFrameTestMaskBenchmark() : iterations_(kDefaultIterations) {} |
| 351 : iterations_(kDefaultIterations) {} | |
| 352 | 348 |
| 353 virtual void SetUp() { | 349 virtual void SetUp() { |
| 354 std::string iterations( | 350 std::string iterations( |
| 355 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 351 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 356 kBenchmarkIterations)); | 352 kBenchmarkIterations)); |
| 357 int benchmark_iterations = 0; | 353 int benchmark_iterations = 0; |
| 358 if (!iterations.empty() && base::StringToInt(iterations, | 354 if (!iterations.empty() && |
| 359 &benchmark_iterations)) { | 355 base::StringToInt(iterations, &benchmark_iterations)) { |
| 360 iterations_ = benchmark_iterations; | 356 iterations_ = benchmark_iterations; |
| 361 } | 357 } |
| 362 } | 358 } |
| 363 | 359 |
| 364 void Benchmark(const char* const payload, size_t size) { | 360 void Benchmark(const char* const payload, size_t size) { |
| 365 std::vector<char> scratch(payload, payload + size); | 361 std::vector<char> scratch(payload, payload + size); |
| 366 static const char kMaskingKey[] = "\xFE\xED\xBE\xEF"; | 362 static const char kMaskingKey[] = "\xFE\xED\xBE\xEF"; |
| 367 COMPILE_ASSERT(arraysize(kMaskingKey) == | 363 COMPILE_ASSERT( |
| 368 WebSocketFrameHeader::kMaskingKeyLength + 1, | 364 arraysize(kMaskingKey) == WebSocketFrameHeader::kMaskingKeyLength + 1, |
| 369 incorrect_masking_key_size); | 365 incorrect_masking_key_size); |
| 370 WebSocketMaskingKey masking_key; | 366 WebSocketMaskingKey masking_key; |
| 371 std::copy(kMaskingKey, kMaskingKey + | 367 std::copy(kMaskingKey, |
| 372 WebSocketFrameHeader::kMaskingKeyLength, | 368 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
| 373 masking_key.key); | 369 masking_key.key); |
| 374 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " | 370 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " << iterations_ |
| 375 << iterations_ << " iterations"; | 371 << " iterations"; |
| 376 using base::TimeTicks; | 372 using base::TimeTicks; |
| 377 TimeTicks start = TimeTicks::HighResNow(); | 373 TimeTicks start = TimeTicks::HighResNow(); |
| 378 for (int x = 0; x < iterations_; ++x) { | 374 for (int x = 0; x < iterations_; ++x) { |
| 379 MaskWebSocketFramePayload(masking_key, x % size, &scratch.front(), | 375 MaskWebSocketFramePayload( |
| 380 scratch.size()); | 376 masking_key, x % size, &scratch.front(), scratch.size()); |
| 381 } | 377 } |
| 382 double total_time_ms = | 378 double total_time_ms = |
| 383 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() / | 379 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() / |
| 384 iterations_; | 380 iterations_; |
| 385 LOG(INFO) << "Payload size " << size | 381 LOG(INFO) << "Payload size " << size |
| 386 << base::StringPrintf(" took %.03f microseconds per iteration", | 382 << base::StringPrintf(" took %.03f microseconds per iteration", |
| 387 total_time_ms); | 383 total_time_ms); |
| 388 } | 384 } |
| 389 | 385 |
| 390 private: | 386 private: |
| 391 int iterations_; | 387 int iterations_; |
| 392 | 388 |
| 393 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark); | 389 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark); |
| 394 }; | 390 }; |
| 395 | 391 |
| 396 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) { | 392 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) { |
| 397 static const char kShortPayload[] = "Short Payload"; | 393 static const char kShortPayload[] = "Short Payload"; |
| 398 Benchmark(kShortPayload, arraysize(kShortPayload)); | 394 Benchmark(kShortPayload, arraysize(kShortPayload)); |
| 399 } | 395 } |
| 400 | 396 |
| 401 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) { | 397 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) { |
| 402 scoped_ptr<char[]> payload(new char[kLongPayloadSize]); | 398 scoped_ptr<char[]> payload(new char[kLongPayloadSize]); |
| 403 std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a'); | 399 std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a'); |
| 404 Benchmark(payload.get(), kLongPayloadSize); | 400 Benchmark(payload.get(), kLongPayloadSize); |
| 405 } | 401 } |
| 406 | 402 |
| 407 // "IsKnownDataOpCode" is implemented using bit-mangling for efficiency, so we | 403 // "IsKnownDataOpCode" is currently implemented in an "obviously correct" |
| 408 // need to check that the results match the actual op-codes defined. | 404 // manner, but we test is anyway in case it changes to a more complex |
| 405 // implementation in future. |
| 409 TEST(WebSocketFrameHeaderTest, IsKnownDataOpCode) { | 406 TEST(WebSocketFrameHeaderTest, IsKnownDataOpCode) { |
| 410 // Make the test less verbose. | 407 // Make the test less verbose. |
| 411 typedef WebSocketFrameHeader Frame; | 408 typedef WebSocketFrameHeader Frame; |
| 412 | 409 |
| 413 // Known opcode, is used for data frames | 410 // Known opcode, is used for data frames |
| 414 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeContinuation)); | 411 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeContinuation)); |
| 415 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeText)); | 412 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeText)); |
| 416 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeBinary)); | 413 EXPECT_TRUE(Frame::IsKnownDataOpCode(Frame::kOpCodeBinary)); |
| 417 | 414 |
| 418 // Known opcode, is used for control frames | 415 // Known opcode, is used for control frames |
| 419 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeClose)); | 416 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeClose)); |
| 420 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePing)); | 417 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePing)); |
| 421 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePong)); | 418 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodePong)); |
| 422 | 419 |
| 423 // Check that unused opcodes return false | 420 // Check that unused opcodes return false |
| 424 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeDataUnused)); | 421 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeDataUnused)); |
| 425 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeControlUnused)); | 422 EXPECT_FALSE(Frame::IsKnownDataOpCode(Frame::kOpCodeControlUnused)); |
| 426 | 423 |
| 427 // Check that opcodes with the 4 bit set return false | 424 // Check that opcodes with the 4 bit set return false |
| 428 EXPECT_FALSE(Frame::IsKnownDataOpCode(0x6)); | 425 EXPECT_FALSE(Frame::IsKnownDataOpCode(0x6)); |
| 429 EXPECT_FALSE(Frame::IsKnownDataOpCode(0xF)); | 426 EXPECT_FALSE(Frame::IsKnownDataOpCode(0xF)); |
| 430 | 427 |
| 431 // Check that out-of-range opcodes return false | 428 // Check that out-of-range opcodes return false |
| 432 EXPECT_FALSE(Frame::IsKnownDataOpCode(-1)); | 429 EXPECT_FALSE(Frame::IsKnownDataOpCode(-1)); |
| 433 EXPECT_FALSE(Frame::IsKnownDataOpCode(0xFF)); | 430 EXPECT_FALSE(Frame::IsKnownDataOpCode(0xFF)); |
| 434 } | 431 } |
| 435 | 432 |
| 436 // "IsKnownControlOpCode" is implemented using bit-mangling as with | 433 // "IsKnownControlOpCode" is implemented in an "obviously correct" manner but |
| 437 // "IsKnownDataOpCode". | 434 // might be optimised in future. |
| 438 TEST(WebSocketFrameHeaderTest, IsKnownControlOpCode) { | 435 TEST(WebSocketFrameHeaderTest, IsKnownControlOpCode) { |
| 439 // Make the test less verbose. | 436 // Make the test less verbose. |
| 440 typedef WebSocketFrameHeader Frame; | 437 typedef WebSocketFrameHeader Frame; |
| 441 | 438 |
| 442 // Known opcode, is used for data frames | 439 // Known opcode, is used for data frames |
| 443 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeContinuation)); | 440 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeContinuation)); |
| 444 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeText)); | 441 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeText)); |
| 445 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeBinary)); | 442 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeBinary)); |
| 446 | 443 |
| 447 // Known opcode, is used for control frames | 444 // Known opcode, is used for control frames |
| 448 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodeClose)); | 445 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodeClose)); |
| 449 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePing)); | 446 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePing)); |
| 450 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePong)); | 447 EXPECT_TRUE(Frame::IsKnownControlOpCode(Frame::kOpCodePong)); |
| 451 | 448 |
| 452 // Check that unused opcodes return false | 449 // Check that unused opcodes return false |
| 453 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeDataUnused)); | 450 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeDataUnused)); |
| 454 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeControlUnused)); | 451 EXPECT_FALSE(Frame::IsKnownControlOpCode(Frame::kOpCodeControlUnused)); |
| 455 | 452 |
| 456 // Check that opcodes with the 4 bit set return false | 453 // Check that opcodes with the 4 bit set return false |
| 457 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); | 454 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); |
| 458 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); | 455 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
| 459 | 456 |
| 460 // Check that out-of-range opcodes return false | 457 // Check that out-of-range opcodes return false |
| 461 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); | 458 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
| 462 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); | 459 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
| 463 } | 460 } |
| 464 | 461 |
| 465 } // namespace net | 462 } // namespace net |
| OLD | NEW |