| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 namespace net { | 29 namespace net { |
| 30 | 30 |
| 31 TEST(WebSocketFrameHeaderTest, FrameLengths) { | 31 TEST(WebSocketFrameHeaderTest, FrameLengths) { |
| 32 struct TestCase { | 32 struct TestCase { |
| 33 const char* frame_header; | 33 const char* frame_header; |
| 34 size_t frame_header_length; | 34 size_t frame_header_length; |
| 35 uint64 frame_length; | 35 uint64 frame_length; |
| 36 }; | 36 }; |
| 37 static const TestCase kTests[] = { | 37 static const TestCase kTests[] = { |
| 38 { "\x81\x00", 2, GG_UINT64_C(0) }, | 38 {"\x81\x00", 2, GG_UINT64_C(0)}, |
| 39 { "\x81\x7D", 2, GG_UINT64_C(125) }, | 39 {"\x81\x7D", 2, GG_UINT64_C(125)}, |
| 40 { "\x81\x7E\x00\x7E", 4, GG_UINT64_C(126) }, | 40 {"\x81\x7E\x00\x7E", 4, GG_UINT64_C(126)}, |
| 41 { "\x81\x7E\xFF\xFF", 4, GG_UINT64_C(0xFFFF) }, | 41 {"\x81\x7E\xFF\xFF", 4, GG_UINT64_C(0xFFFF)}, |
| 42 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, GG_UINT64_C(0x10000) }, | 42 {"\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, GG_UINT64_C(0x10000)}, |
| 43 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, | 43 {"\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, |
| 44 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | 44 GG_UINT64_C(0x7FFFFFFFFFFFFFFF)}}; |
| 45 }; | |
| 46 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 45 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 47 | 46 |
| 48 for (int i = 0; i < kNumTests; ++i) { | 47 for (int i = 0; i < kNumTests; ++i) { |
| 49 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 48 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 50 header.final = true; | 49 header.final = true; |
| 51 header.payload_length = kTests[i].frame_length; | 50 header.payload_length = kTests[i].frame_length; |
| 52 | 51 |
| 53 std::vector<char> expected_output( | 52 std::vector<char> expected_output( |
| 54 kTests[i].frame_header, | 53 kTests[i].frame_header, |
| 55 kTests[i].frame_header + kTests[i].frame_header_length); | 54 kTests[i].frame_header + kTests[i].frame_header_length); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 66 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 == | 65 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 == |
| 67 WebSocketFrameHeader::kMaskingKeyLength, | 66 WebSocketFrameHeader::kMaskingKeyLength, |
| 68 incorrect_masking_key_size); | 67 incorrect_masking_key_size); |
| 69 | 68 |
| 70 struct TestCase { | 69 struct TestCase { |
| 71 const char* frame_header; | 70 const char* frame_header; |
| 72 size_t frame_header_length; | 71 size_t frame_header_length; |
| 73 uint64 frame_length; | 72 uint64 frame_length; |
| 74 }; | 73 }; |
| 75 static const TestCase kTests[] = { | 74 static const TestCase kTests[] = { |
| 76 { "\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0) }, | 75 {"\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0)}, |
| 77 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(125) }, | 76 {"\x81\xFD\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(125)}, |
| 78 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(126) }, | 77 {"\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(126)}, |
| 79 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(0xFFFF) }, | 78 {"\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(0xFFFF)}, |
| 80 { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, | 79 {"\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, |
| 81 GG_UINT64_C(0x10000) }, | 80 GG_UINT64_C(0x10000)}, |
| 82 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, | 81 {"\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, |
| 83 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | 82 GG_UINT64_C(0x7FFFFFFFFFFFFFFF)}}; |
| 84 }; | |
| 85 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 83 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 86 | 84 |
| 87 WebSocketMaskingKey masking_key; | 85 WebSocketMaskingKey masking_key; |
| 88 std::copy(kMaskingKey, | 86 std::copy(kMaskingKey, |
| 89 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, | 87 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
| 90 masking_key.key); | 88 masking_key.key); |
| 91 | 89 |
| 92 for (int i = 0; i < kNumTests; ++i) { | 90 for (int i = 0; i < kNumTests; ++i) { |
| 93 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 91 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 94 header.final = true; | 92 header.final = true; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 106 } | 104 } |
| 107 } | 105 } |
| 108 | 106 |
| 109 TEST(WebSocketFrameHeaderTest, FrameOpCodes) { | 107 TEST(WebSocketFrameHeaderTest, FrameOpCodes) { |
| 110 struct TestCase { | 108 struct TestCase { |
| 111 const char* frame_header; | 109 const char* frame_header; |
| 112 size_t frame_header_length; | 110 size_t frame_header_length; |
| 113 WebSocketFrameHeader::OpCode opcode; | 111 WebSocketFrameHeader::OpCode opcode; |
| 114 }; | 112 }; |
| 115 static const TestCase kTests[] = { | 113 static const TestCase kTests[] = { |
| 116 { "\x80\x00", 2, WebSocketFrameHeader::kOpCodeContinuation }, | 114 {"\x80\x00", 2, WebSocketFrameHeader::kOpCodeContinuation}, |
| 117 { "\x81\x00", 2, WebSocketFrameHeader::kOpCodeText }, | 115 {"\x81\x00", 2, WebSocketFrameHeader::kOpCodeText}, |
| 118 { "\x82\x00", 2, WebSocketFrameHeader::kOpCodeBinary }, | 116 {"\x82\x00", 2, WebSocketFrameHeader::kOpCodeBinary}, |
| 119 { "\x88\x00", 2, WebSocketFrameHeader::kOpCodeClose }, | 117 {"\x88\x00", 2, WebSocketFrameHeader::kOpCodeClose}, |
| 120 { "\x89\x00", 2, WebSocketFrameHeader::kOpCodePing }, | 118 {"\x89\x00", 2, WebSocketFrameHeader::kOpCodePing}, |
| 121 { "\x8A\x00", 2, WebSocketFrameHeader::kOpCodePong }, | 119 {"\x8A\x00", 2, WebSocketFrameHeader::kOpCodePong}, |
| 122 // These are undefined opcodes, but the builder should accept them anyway. | 120 // These are undefined opcodes, but the builder should accept them anyway. |
| 123 { "\x83\x00", 2, 0x3 }, | 121 {"\x83\x00", 2, 0x3}, |
| 124 { "\x84\x00", 2, 0x4 }, | 122 {"\x84\x00", 2, 0x4}, |
| 125 { "\x85\x00", 2, 0x5 }, | 123 {"\x85\x00", 2, 0x5}, |
| 126 { "\x86\x00", 2, 0x6 }, | 124 {"\x86\x00", 2, 0x6}, |
| 127 { "\x87\x00", 2, 0x7 }, | 125 {"\x87\x00", 2, 0x7}, |
| 128 { "\x8B\x00", 2, 0xB }, | 126 {"\x8B\x00", 2, 0xB}, |
| 129 { "\x8C\x00", 2, 0xC }, | 127 {"\x8C\x00", 2, 0xC}, |
| 130 { "\x8D\x00", 2, 0xD }, | 128 {"\x8D\x00", 2, 0xD}, |
| 131 { "\x8E\x00", 2, 0xE }, | 129 {"\x8E\x00", 2, 0xE}, |
| 132 { "\x8F\x00", 2, 0xF } | 130 {"\x8F\x00", 2, 0xF}}; |
| 133 }; | |
| 134 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 131 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 135 | 132 |
| 136 for (int i = 0; i < kNumTests; ++i) { | 133 for (int i = 0; i < kNumTests; ++i) { |
| 137 WebSocketFrameHeader header(kTests[i].opcode); | 134 WebSocketFrameHeader header(kTests[i].opcode); |
| 138 header.final = true; | 135 header.final = true; |
| 139 header.payload_length = 0; | 136 header.payload_length = 0; |
| 140 | 137 |
| 141 std::vector<char> expected_output( | 138 std::vector<char> expected_output( |
| 142 kTests[i].frame_header, | 139 kTests[i].frame_header, |
| 143 kTests[i].frame_header + kTests[i].frame_header_length); | 140 kTests[i].frame_header + kTests[i].frame_header_length); |
| 144 std::vector<char> output(expected_output.size()); | 141 std::vector<char> output(expected_output.size()); |
| 145 EXPECT_EQ(static_cast<int>(expected_output.size()), | 142 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 146 WriteWebSocketFrameHeader( | 143 WriteWebSocketFrameHeader( |
| 147 header, NULL, &output.front(), output.size())); | 144 header, NULL, &output.front(), output.size())); |
| 148 EXPECT_EQ(expected_output, output); | 145 EXPECT_EQ(expected_output, output); |
| 149 } | 146 } |
| 150 } | 147 } |
| 151 | 148 |
| 152 TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) { | 149 TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) { |
| 153 struct TestCase { | 150 struct TestCase { |
| 154 const char* frame_header; | 151 const char* frame_header; |
| 155 size_t frame_header_length; | 152 size_t frame_header_length; |
| 156 bool final; | 153 bool final; |
| 157 bool reserved1; | 154 bool reserved1; |
| 158 bool reserved2; | 155 bool reserved2; |
| 159 bool reserved3; | 156 bool reserved3; |
| 160 }; | 157 }; |
| 161 static const TestCase kTests[] = { | 158 static const TestCase kTests[] = {{"\x81\x00", 2, true, false, false, false}, |
| 162 { "\x81\x00", 2, true, false, false, false }, | 159 {"\x01\x00", 2, false, false, false, false}, |
| 163 { "\x01\x00", 2, false, false, false, false }, | 160 {"\xC1\x00", 2, true, true, false, false}, |
| 164 { "\xC1\x00", 2, true, true, false, false }, | 161 {"\xA1\x00", 2, true, false, true, false}, |
| 165 { "\xA1\x00", 2, true, false, true, false }, | 162 {"\x91\x00", 2, true, false, false, true}, |
| 166 { "\x91\x00", 2, true, false, false, true }, | 163 {"\x71\x00", 2, false, true, true, true}, |
| 167 { "\x71\x00", 2, false, true, true, true }, | 164 {"\xF1\x00", 2, true, true, true, true}}; |
| 168 { "\xF1\x00", 2, true, true, true, true } | |
| 169 }; | |
| 170 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 165 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 171 | 166 |
| 172 for (int i = 0; i < kNumTests; ++i) { | 167 for (int i = 0; i < kNumTests; ++i) { |
| 173 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 168 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 174 header.final = kTests[i].final; | 169 header.final = kTests[i].final; |
| 175 header.reserved1 = kTests[i].reserved1; | 170 header.reserved1 = kTests[i].reserved1; |
| 176 header.reserved2 = kTests[i].reserved2; | 171 header.reserved2 = kTests[i].reserved2; |
| 177 header.reserved3 = kTests[i].reserved3; | 172 header.reserved3 = kTests[i].reserved3; |
| 178 header.payload_length = 0; | 173 header.payload_length = 0; |
| 179 | 174 |
| 180 std::vector<char> expected_output( | 175 std::vector<char> expected_output( |
| 181 kTests[i].frame_header, | 176 kTests[i].frame_header, |
| 182 kTests[i].frame_header + kTests[i].frame_header_length); | 177 kTests[i].frame_header + kTests[i].frame_header_length); |
| 183 std::vector<char> output(expected_output.size()); | 178 std::vector<char> output(expected_output.size()); |
| 184 EXPECT_EQ(static_cast<int>(expected_output.size()), | 179 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 185 WriteWebSocketFrameHeader( | 180 WriteWebSocketFrameHeader( |
| 186 header, NULL, &output.front(), output.size())); | 181 header, NULL, &output.front(), output.size())); |
| 187 EXPECT_EQ(expected_output, output); | 182 EXPECT_EQ(expected_output, output); |
| 188 } | 183 } |
| 189 } | 184 } |
| 190 | 185 |
| 191 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { | 186 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { |
| 192 struct TestCase { | 187 struct TestCase { |
| 193 uint64 payload_length; | 188 uint64 payload_length; |
| 194 bool masked; | 189 bool masked; |
| 195 size_t expected_header_size; | 190 size_t expected_header_size; |
| 196 }; | 191 }; |
| 197 static const TestCase kTests[] = { | 192 static const TestCase kTests[] = { |
| 198 { GG_UINT64_C(0), false, 2u }, | 193 {GG_UINT64_C(0), false, 2u}, |
| 199 { GG_UINT64_C(125), false, 2u }, | 194 {GG_UINT64_C(125), false, 2u}, |
| 200 { GG_UINT64_C(126), false, 4u }, | 195 {GG_UINT64_C(126), false, 4u}, |
| 201 { GG_UINT64_C(0xFFFF), false, 4u }, | 196 {GG_UINT64_C(0xFFFF), false, 4u}, |
| 202 { GG_UINT64_C(0x10000), false, 10u }, | 197 {GG_UINT64_C(0x10000), false, 10u}, |
| 203 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, | 198 {GG_UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u}, |
| 204 { GG_UINT64_C(0), true, 6u }, | 199 {GG_UINT64_C(0), true, 6u}, |
| 205 { GG_UINT64_C(125), true, 6u }, | 200 {GG_UINT64_C(125), true, 6u}, |
| 206 { GG_UINT64_C(126), true, 8u }, | 201 {GG_UINT64_C(126), true, 8u}, |
| 207 { GG_UINT64_C(0xFFFF), true, 8u }, | 202 {GG_UINT64_C(0xFFFF), true, 8u}, |
| 208 { GG_UINT64_C(0x10000), true, 14u }, | 203 {GG_UINT64_C(0x10000), true, 14u}, |
| 209 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u } | 204 {GG_UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u}}; |
| 210 }; | |
| 211 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 205 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 212 | 206 |
| 213 for (int i = 0; i < kNumTests; ++i) { | 207 for (int i = 0; i < kNumTests; ++i) { |
| 214 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 208 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 215 header.final = true; | 209 header.final = true; |
| 216 header.opcode = WebSocketFrameHeader::kOpCodeText; | 210 header.opcode = WebSocketFrameHeader::kOpCodeText; |
| 217 header.masked = kTests[i].masked; | 211 header.masked = kTests[i].masked; |
| 218 header.payload_length = kTests[i].payload_length; | 212 header.payload_length = kTests[i].payload_length; |
| 219 | 213 |
| 220 char dummy_buffer[14]; | 214 char dummy_buffer[14]; |
| 221 // Set an insufficient size to |buffer_size|. | 215 // Set an insufficient size to |buffer_size|. |
| 222 EXPECT_EQ( | 216 EXPECT_EQ( |
| 223 ERR_INVALID_ARGUMENT, | 217 ERR_INVALID_ARGUMENT, |
| 224 WriteWebSocketFrameHeader( | 218 WriteWebSocketFrameHeader( |
| 225 header, NULL, dummy_buffer, kTests[i].expected_header_size - 1)); | 219 header, NULL, dummy_buffer, kTests[i].expected_header_size - 1)); |
| 226 } | 220 } |
| 227 } | 221 } |
| 228 | 222 |
| 229 TEST(WebSocketFrameTest, MaskPayload) { | 223 TEST(WebSocketFrameTest, MaskPayload) { |
| 230 struct TestCase { | 224 struct TestCase { |
| 231 const char* masking_key; | 225 const char* masking_key; |
| 232 uint64 frame_offset; | 226 uint64 frame_offset; |
| 233 const char* input; | 227 const char* input; |
| 234 const char* output; | 228 const char* output; |
| 235 size_t data_length; | 229 size_t data_length; |
| 236 }; | 230 }; |
| 237 static const TestCase kTests[] = { | 231 static const TestCase kTests[] = { |
| 238 { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, | 232 {"\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6}, |
| 239 { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 }, | 233 {"\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6}, |
| 240 { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, | 234 {"\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6}, |
| 241 { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 }, | 235 {"\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6}, |
| 242 { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, | 236 {"\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6}, |
| 243 { "\xDE\xAD\xBE\xEF", 42, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, | 237 {"\xDE\xAD\xBE\xEF", 42, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6}, |
| 244 { "\xDE\xAD\xBE\xEF", 0, "", "", 0 }, | 238 {"\xDE\xAD\xBE\xEF", 0, "", "", 0}, |
| 245 { "\xDE\xAD\xBE\xEF", 0, "\xDE\xAD\xBE\xEF", "\x00\x00\x00\x00", 4 }, | 239 {"\xDE\xAD\xBE\xEF", 0, "\xDE\xAD\xBE\xEF", "\x00\x00\x00\x00", 4}, |
| 246 { "\xDE\xAD\xBE\xEF", 0, "\x00\x00\x00\x00", "\xDE\xAD\xBE\xEF", 4 }, | 240 {"\xDE\xAD\xBE\xEF", 0, "\x00\x00\x00\x00", "\xDE\xAD\xBE\xEF", 4}, |
| 247 { "\x00\x00\x00\x00", 0, "FooBar", "FooBar", 6 }, | 241 {"\x00\x00\x00\x00", 0, "FooBar", "FooBar", 6}, |
| 248 { "\xFF\xFF\xFF\xFF", 0, "FooBar", "\xB9\x90\x90\xBD\x9E\x8D", 6 }, | 242 {"\xFF\xFF\xFF\xFF", 0, "FooBar", "\xB9\x90\x90\xBD\x9E\x8D", 6}, |
| 249 }; | 243 }; |
| 250 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | 244 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); |
| 251 | 245 |
| 252 for (int i = 0; i < kNumTests; ++i) { | 246 for (int i = 0; i < kNumTests; ++i) { |
| 253 WebSocketMaskingKey masking_key; | 247 WebSocketMaskingKey masking_key; |
| 254 std::copy(kTests[i].masking_key, | 248 std::copy(kTests[i].masking_key, |
| 255 kTests[i].masking_key + WebSocketFrameHeader::kMaskingKeyLength, | 249 kTests[i].masking_key + WebSocketFrameHeader::kMaskingKeyLength, |
| 256 masking_key.key); | 250 masking_key.key); |
| 257 std::vector<char> frame_data(kTests[i].input, | 251 std::vector<char> frame_data(kTests[i].input, |
| 258 kTests[i].input + kTests[i].data_length); | 252 kTests[i].input + kTests[i].data_length); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 283 static const size_t kMaxVectorSizeInBits = 256; | 277 static const size_t kMaxVectorSizeInBits = 256; |
| 284 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8; | 278 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8; |
| 285 static const size_t kMaxVectorAlignment = kMaxVectorSize; | 279 static const size_t kMaxVectorAlignment = kMaxVectorSize; |
| 286 static const size_t kMaskingKeyLength = | 280 static const size_t kMaskingKeyLength = |
| 287 WebSocketFrameHeader::kMaskingKeyLength; | 281 WebSocketFrameHeader::kMaskingKeyLength; |
| 288 static const size_t kScratchBufferSize = | 282 static const size_t kScratchBufferSize = |
| 289 kMaxVectorAlignment + kMaxVectorSize * 2; | 283 kMaxVectorAlignment + kMaxVectorSize * 2; |
| 290 static const char kTestMask[] = "\xd2\xba\x5a\xbe"; | 284 static const char kTestMask[] = "\xd2\xba\x5a\xbe"; |
| 291 // We use 786 bits of random input to reduce the risk of correlated errors. | 285 // We use 786 bits of random input to reduce the risk of correlated errors. |
| 292 static const char kTestInput[] = { | 286 static const char kTestInput[] = { |
| 293 "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b" | 287 "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b" |
| 294 "\x69\x8c\xda\x4b\xfc\xac\x2c\xd3\x49\x96\x6e\x8a\x7b\x5a\x32\x76" | 288 "\x69\x8c\xda\x4b\xfc\xac\x2c\xd3\x49\x96\x6e\x8a\x7b\x5a\x32\x76" |
| 295 "\xd0\x11\x43\xa0\x89\xfc\x76\x2b\x10\x2f\x4c\x7b\x4f\xa6\xdd\xe4" | 289 "\xd0\x11\x43\xa0\x89\xfc\x76\x2b\x10\x2f\x4c\x7b\x4f\xa6\xdd\xe4" |
| 296 "\xfc\x8e\xd8\x72\xcf\x7e\x37\xcd\x31\xcd\xc1\xc0\x89\x0c\xa7\x4c" | 290 "\xfc\x8e\xd8\x72\xcf\x7e\x37\xcd\x31\xcd\xc1\xc0\x89\x0c\xa7\x4c" |
| 297 "\xda\xa8\x4b\x75\xa1\xcb\xa9\x77\x19\x4d\x6e\xdf\xc8\x08\x1c\xb6" | 291 "\xda\xa8\x4b\x75\xa1\xcb\xa9\x77\x19\x4d\x6e\xdf\xc8\x08\x1c\xb6" |
| 298 "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8" | 292 "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8"}; |
| 299 }; | |
| 300 static const size_t kTestInputSize = arraysize(kTestInput) - 1; | 293 static const size_t kTestInputSize = arraysize(kTestInput) - 1; |
| 301 static const char kTestOutput[] = { | 294 static const char kTestOutput[] = { |
| 302 "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5" | 295 "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5" |
| 303 "\xbb\x36\x80\xf5\x2e\x16\x76\x6d\x9b\x2c\x34\x34\xa9\xe0\x68\xc8" | 296 "\xbb\x36\x80\xf5\x2e\x16\x76\x6d\x9b\x2c\x34\x34\xa9\xe0\x68\xc8" |
| 304 "\x02\xab\x19\x1e\x5b\x46\x2c\x95\xc2\x95\x16\xc5\x9d\x1c\x87\x5a" | 297 "\x02\xab\x19\x1e\x5b\x46\x2c\x95\xc2\x95\x16\xc5\x9d\x1c\x87\x5a" |
| 305 "\x2e\x34\x82\xcc\x1d\xc4\x6d\x73\xe3\x77\x9b\x7e\x5b\xb6\xfd\xf2" | 298 "\x2e\x34\x82\xcc\x1d\xc4\x6d\x73\xe3\x77\x9b\x7e\x5b\xb6\xfd\xf2" |
| 306 "\x08\x12\x11\xcb\x73\x71\xf3\xc9\xcb\xf7\x34\x61\x1a\xb2\x46\x08" | 299 "\x08\x12\x11\xcb\x73\x71\xf3\xc9\xcb\xf7\x34\x61\x1a\xb2\x46\x08" |
| 307 "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16" | 300 "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16"}; |
| 308 }; | |
| 309 COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput), | 301 COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput), |
| 310 output_and_input_arrays_have_the_same_length); | 302 output_and_input_arrays_have_the_same_length); |
| 311 scoped_ptr<char, base::AlignedFreeDeleter> scratch( | 303 scoped_ptr<char, base::AlignedFreeDeleter> scratch(static_cast<char*>( |
| 312 static_cast<char*>( | 304 base::AlignedAlloc(kScratchBufferSize, kMaxVectorAlignment))); |
| 313 base::AlignedAlloc(kScratchBufferSize, kMaxVectorAlignment))); | |
| 314 WebSocketMaskingKey masking_key; | 305 WebSocketMaskingKey masking_key; |
| 315 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key); | 306 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key); |
| 316 for (size_t frame_offset = 0; frame_offset < kMaskingKeyLength; | 307 for (size_t frame_offset = 0; frame_offset < kMaskingKeyLength; |
| 317 ++frame_offset) { | 308 ++frame_offset) { |
| 318 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) { | 309 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) { |
| 319 char* const aligned_scratch = scratch.get() + alignment; | 310 char* const aligned_scratch = scratch.get() + alignment; |
| 320 const size_t aligned_len = std::min(kScratchBufferSize - alignment, | 311 const size_t aligned_len = std::min(kScratchBufferSize - alignment, |
| 321 kTestInputSize - frame_offset); | 312 kTestInputSize - frame_offset); |
| 322 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) { | 313 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) { |
| 323 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len); | 314 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, | 359 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
| 369 masking_key.key); | 360 masking_key.key); |
| 370 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " << iterations_ | 361 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for " << iterations_ |
| 371 << " iterations"; | 362 << " iterations"; |
| 372 using base::TimeTicks; | 363 using base::TimeTicks; |
| 373 TimeTicks start = TimeTicks::HighResNow(); | 364 TimeTicks start = TimeTicks::HighResNow(); |
| 374 for (int x = 0; x < iterations_; ++x) { | 365 for (int x = 0; x < iterations_; ++x) { |
| 375 MaskWebSocketFramePayload( | 366 MaskWebSocketFramePayload( |
| 376 masking_key, x % size, &scratch.front(), scratch.size()); | 367 masking_key, x % size, &scratch.front(), scratch.size()); |
| 377 } | 368 } |
| 378 double total_time_ms = | 369 double total_time_ms = 1000 * |
| 379 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() / | 370 (TimeTicks::HighResNow() - start).InMillisecondsF() / |
| 380 iterations_; | 371 iterations_; |
| 381 LOG(INFO) << "Payload size " << size | 372 LOG(INFO) << "Payload size " << size |
| 382 << base::StringPrintf(" took %.03f microseconds per iteration", | 373 << base::StringPrintf(" took %.03f microseconds per iteration", |
| 383 total_time_ms); | 374 total_time_ms); |
| 384 } | 375 } |
| 385 | 376 |
| 386 private: | 377 private: |
| 387 int iterations_; | 378 int iterations_; |
| 388 | 379 |
| 389 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark); | 380 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark); |
| 390 }; | 381 }; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 // Check that opcodes with the 4 bit set return false | 444 // Check that opcodes with the 4 bit set return false |
| 454 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); | 445 EXPECT_FALSE(Frame::IsKnownControlOpCode(0x6)); |
| 455 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); | 446 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
| 456 | 447 |
| 457 // Check that out-of-range opcodes return false | 448 // Check that out-of-range opcodes return false |
| 458 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); | 449 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
| 459 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); | 450 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
| 460 } | 451 } |
| 461 | 452 |
| 462 } // namespace net | 453 } // namespace net |
| OLD | NEW |