Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / net / websockets / websocket_frame_unittest.cc
blobe0972a78043dc7e75bb0aafdd9c58729609dcfa3
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.
5 #include "net/websockets/websocket_frame.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/basictypes.h"
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"
17 #include "net/base/net_errors.h"
18 #include "testing/gtest/include/gtest/gtest.h"
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;
28 namespace net {
30 TEST(WebSocketFrameHeaderTest, FrameLengths) {
31 struct TestCase {
32 const char* frame_header;
33 size_t frame_header_length;
34 uint64 frame_length;
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) }
45 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests);
47 for (int i = 0; i < kNumTests; ++i) {
48 WebSocketFrameHeader header;
49 header.final = true;
50 header.reserved1 = false;
51 header.reserved2 = false;
52 header.reserved3 = false;
53 header.opcode = WebSocketFrameHeader::kOpCodeText;
54 header.masked = false;
55 header.payload_length = kTests[i].frame_length;
57 std::vector<char> expected_output(
58 kTests[i].frame_header,
59 kTests[i].frame_header + kTests[i].frame_header_length);
60 std::vector<char> output(expected_output.size());
61 EXPECT_EQ(static_cast<int>(expected_output.size()),
62 WriteWebSocketFrameHeader(header, NULL, &output.front(),
63 output.size()));
64 EXPECT_EQ(expected_output, output);
68 TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) {
69 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF";
70 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 ==
71 WebSocketFrameHeader::kMaskingKeyLength,
72 incorrect_masking_key_size);
74 struct TestCase {
75 const char* frame_header;
76 size_t frame_header_length;
77 uint64 frame_length;
79 static const TestCase kTests[] = {
80 { "\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0) },
81 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(125) },
82 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(126) },
83 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(0xFFFF) },
84 { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14,
85 GG_UINT64_C(0x10000) },
86 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14,
87 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) }
89 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests);
91 WebSocketMaskingKey masking_key;
92 std::copy(kMaskingKey, kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength,
93 masking_key.key);
95 for (int i = 0; i < kNumTests; ++i) {
96 WebSocketFrameHeader header;
97 header.final = true;
98 header.reserved1 = false;
99 header.reserved2 = false;
100 header.reserved3 = false;
101 header.opcode = WebSocketFrameHeader::kOpCodeText;
102 header.masked = true;
103 header.payload_length = kTests[i].frame_length;
105 std::vector<char> expected_output(
106 kTests[i].frame_header,
107 kTests[i].frame_header + kTests[i].frame_header_length);
108 std::vector<char> output(expected_output.size());
109 EXPECT_EQ(static_cast<int>(expected_output.size()),
110 WriteWebSocketFrameHeader(header, &masking_key,
111 &output.front(), output.size()));
112 EXPECT_EQ(expected_output, output);
116 TEST(WebSocketFrameHeaderTest, FrameOpCodes) {
117 struct TestCase {
118 const char* frame_header;
119 size_t frame_header_length;
120 WebSocketFrameHeader::OpCode opcode;
122 static const TestCase kTests[] = {
123 { "\x80\x00", 2, WebSocketFrameHeader::kOpCodeContinuation },
124 { "\x81\x00", 2, WebSocketFrameHeader::kOpCodeText },
125 { "\x82\x00", 2, WebSocketFrameHeader::kOpCodeBinary },
126 { "\x88\x00", 2, WebSocketFrameHeader::kOpCodeClose },
127 { "\x89\x00", 2, WebSocketFrameHeader::kOpCodePing },
128 { "\x8A\x00", 2, WebSocketFrameHeader::kOpCodePong },
129 // These are undefined opcodes, but the builder should accept them anyway.
130 { "\x83\x00", 2, 0x3 },
131 { "\x84\x00", 2, 0x4 },
132 { "\x85\x00", 2, 0x5 },
133 { "\x86\x00", 2, 0x6 },
134 { "\x87\x00", 2, 0x7 },
135 { "\x8B\x00", 2, 0xB },
136 { "\x8C\x00", 2, 0xC },
137 { "\x8D\x00", 2, 0xD },
138 { "\x8E\x00", 2, 0xE },
139 { "\x8F\x00", 2, 0xF }
141 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests);
143 for (int i = 0; i < kNumTests; ++i) {
144 WebSocketFrameHeader header;
145 header.final = true;
146 header.reserved1 = false;
147 header.reserved2 = false;
148 header.reserved3 = false;
149 header.opcode = kTests[i].opcode;
150 header.masked = false;
151 header.payload_length = 0;
153 std::vector<char> expected_output(
154 kTests[i].frame_header,
155 kTests[i].frame_header + kTests[i].frame_header_length);
156 std::vector<char> output(expected_output.size());
157 EXPECT_EQ(static_cast<int>(expected_output.size()),
158 WriteWebSocketFrameHeader(header, NULL,
159 &output.front(), output.size()));
160 EXPECT_EQ(expected_output, output);
164 TEST(WebSocketFrameHeaderTest, FinalBitAndReservedBits) {
165 struct TestCase {
166 const char* frame_header;
167 size_t frame_header_length;
168 bool final;
169 bool reserved1;
170 bool reserved2;
171 bool reserved3;
173 static const TestCase kTests[] = {
174 { "\x81\x00", 2, true, false, false, false },
175 { "\x01\x00", 2, false, false, false, false },
176 { "\xC1\x00", 2, true, true, false, false },
177 { "\xA1\x00", 2, true, false, true, false },
178 { "\x91\x00", 2, true, false, false, true },
179 { "\x71\x00", 2, false, true, true, true },
180 { "\xF1\x00", 2, true, true, true, true }
182 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests);
184 for (int i = 0; i < kNumTests; ++i) {
185 WebSocketFrameHeader header;
186 header.final = kTests[i].final;
187 header.reserved1 = kTests[i].reserved1;
188 header.reserved2 = kTests[i].reserved2;
189 header.reserved3 = kTests[i].reserved3;
190 header.opcode = WebSocketFrameHeader::kOpCodeText;
191 header.masked = false;
192 header.payload_length = 0;
194 std::vector<char> expected_output(
195 kTests[i].frame_header,
196 kTests[i].frame_header + kTests[i].frame_header_length);
197 std::vector<char> output(expected_output.size());
198 EXPECT_EQ(static_cast<int>(expected_output.size()),
199 WriteWebSocketFrameHeader(header, NULL,
200 &output.front(), output.size()));
201 EXPECT_EQ(expected_output, output);
205 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) {
206 struct TestCase {
207 uint64 payload_length;
208 bool masked;
209 size_t expected_header_size;
211 static const TestCase kTests[] = {
212 { GG_UINT64_C(0), false, 2u },
213 { GG_UINT64_C(125), false, 2u },
214 { GG_UINT64_C(126), false, 4u },
215 { GG_UINT64_C(0xFFFF), false, 4u },
216 { GG_UINT64_C(0x10000), false, 10u },
217 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u },
218 { GG_UINT64_C(0), true, 6u },
219 { GG_UINT64_C(125), true, 6u },
220 { GG_UINT64_C(126), true, 8u },
221 { GG_UINT64_C(0xFFFF), true, 8u },
222 { GG_UINT64_C(0x10000), true, 14u },
223 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u }
225 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests);
227 for (int i = 0; i < kNumTests; ++i) {
228 WebSocketFrameHeader header;
229 header.final = true;
230 header.reserved1 = false;
231 header.reserved2 = false;
232 header.reserved3 = false;
233 header.opcode = WebSocketFrameHeader::kOpCodeText;
234 header.masked = kTests[i].masked;
235 header.payload_length = kTests[i].payload_length;
237 char dummy_buffer[14];
238 // Set an insufficient size to |buffer_size|.
239 EXPECT_EQ(ERR_INVALID_ARGUMENT,
240 WriteWebSocketFrameHeader(header, NULL, dummy_buffer,
241 kTests[i].expected_header_size - 1));
245 TEST(WebSocketFrameTest, MaskPayload) {
246 struct TestCase {
247 const char* masking_key;
248 uint64 frame_offset;
249 const char* input;
250 const char* output;
251 size_t data_length;
253 static const TestCase kTests[] = {
254 { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 },
255 { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 },
256 { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 },
257 { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 },
258 { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 },
259 { "\xDE\xAD\xBE\xEF", 42, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 },
260 { "\xDE\xAD\xBE\xEF", 0, "", "", 0 },
261 { "\xDE\xAD\xBE\xEF", 0, "\xDE\xAD\xBE\xEF", "\x00\x00\x00\x00", 4 },
262 { "\xDE\xAD\xBE\xEF", 0, "\x00\x00\x00\x00", "\xDE\xAD\xBE\xEF", 4 },
263 { "\x00\x00\x00\x00", 0, "FooBar", "FooBar", 6 },
264 { "\xFF\xFF\xFF\xFF", 0, "FooBar", "\xB9\x90\x90\xBD\x9E\x8D", 6 },
266 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests);
268 for (int i = 0; i < kNumTests; ++i) {
269 WebSocketMaskingKey masking_key;
270 std::copy(kTests[i].masking_key,
271 kTests[i].masking_key + WebSocketFrameHeader::kMaskingKeyLength,
272 masking_key.key);
273 std::vector<char> frame_data(kTests[i].input,
274 kTests[i].input + kTests[i].data_length);
275 std::vector<char> expected_output(kTests[i].output,
276 kTests[i].output + kTests[i].data_length);
277 MaskWebSocketFramePayload(masking_key,
278 kTests[i].frame_offset,
279 frame_data.empty() ? NULL : &frame_data.front(),
280 frame_data.size());
281 EXPECT_EQ(expected_output, frame_data);
285 // Check that all combinations of alignment, frame offset and chunk size work
286 // correctly for MaskWebSocketFramePayload(). This is mainly used to ensure that
287 // vectorisation optimisations don't break anything. We could take a "white box"
288 // approach and only test the edge cases, but since the exhaustive "black box"
289 // approach runs in acceptable time, we don't have to take the risk of being
290 // clever.
292 // This brute-force approach runs in O(N^3) time where N is the size of the
293 // maximum vector size we want to test again. This might need reconsidering if
294 // MaskWebSocketFramePayload() is ever optimised for a dedicated vector
295 // architecture.
296 TEST(WebSocketFrameTest, MaskPayloadAlignment) {
297 // This reflects what might be implemented in the future, rather than
298 // the current implementation. FMA3 and FMA4 support 256-bit vector ops.
299 static const size_t kMaxVectorSizeInBits = 256;
300 static const size_t kMaxVectorSize = kMaxVectorSizeInBits / 8;
301 static const size_t kMaxVectorAlignment = kMaxVectorSize;
302 static const size_t kMaskingKeyLength =
303 WebSocketFrameHeader::kMaskingKeyLength;
304 static const size_t kScratchBufferSize =
305 kMaxVectorAlignment + kMaxVectorSize * 2;
306 static const char kTestMask[] = "\xd2\xba\x5a\xbe";
307 // We use 786 bits of random input to reduce the risk of correlated errors.
308 static const char kTestInput[] =
309 { "\x3d\x77\x1d\x1b\x19\x8c\x48\xa3\x19\x6d\xf7\xcc\x39\xe7\x57\x0b"
310 "\x69\x8c\xda\x4b\xfc\xac\x2c\xd3\x49\x96\x6e\x8a\x7b\x5a\x32\x76"
311 "\xd0\x11\x43\xa0\x89\xfc\x76\x2b\x10\x2f\x4c\x7b\x4f\xa6\xdd\xe4"
312 "\xfc\x8e\xd8\x72\xcf\x7e\x37\xcd\x31\xcd\xc1\xc0\x89\x0c\xa7\x4c"
313 "\xda\xa8\x4b\x75\xa1\xcb\xa9\x77\x19\x4d\x6e\xdf\xc8\x08\x1c\xb6"
314 "\x6d\xfb\x38\x04\x44\xd5\xba\x57\x9f\x76\xb0\x2e\x07\x91\xe6\xa8"
316 static const size_t kTestInputSize = arraysize(kTestInput) - 1;
317 static const char kTestOutput[] =
318 { "\xef\xcd\x47\xa5\xcb\x36\x12\x1d\xcb\xd7\xad\x72\xeb\x5d\x0d\xb5"
319 "\xbb\x36\x80\xf5\x2e\x16\x76\x6d\x9b\x2c\x34\x34\xa9\xe0\x68\xc8"
320 "\x02\xab\x19\x1e\x5b\x46\x2c\x95\xc2\x95\x16\xc5\x9d\x1c\x87\x5a"
321 "\x2e\x34\x82\xcc\x1d\xc4\x6d\x73\xe3\x77\x9b\x7e\x5b\xb6\xfd\xf2"
322 "\x08\x12\x11\xcb\x73\x71\xf3\xc9\xcb\xf7\x34\x61\x1a\xb2\x46\x08"
323 "\xbf\x41\x62\xba\x96\x6f\xe0\xe9\x4d\xcc\xea\x90\xd5\x2b\xbc\x16"
325 COMPILE_ASSERT(arraysize(kTestInput) == arraysize(kTestOutput),
326 output_and_input_arrays_have_the_same_length);
327 scoped_ptr_malloc<char, base::ScopedPtrAlignedFree> scratch(
328 static_cast<char*>(base::AlignedAlloc(kScratchBufferSize,
329 kMaxVectorAlignment)));
330 WebSocketMaskingKey masking_key;
331 std::copy(kTestMask, kTestMask + kMaskingKeyLength, masking_key.key);
332 for (size_t frame_offset = 0;
333 frame_offset < kMaskingKeyLength;
334 ++frame_offset) {
335 for (size_t alignment = 0; alignment < kMaxVectorAlignment; ++alignment) {
336 char* const aligned_scratch = scratch.get() + alignment;
337 const size_t aligned_len =
338 std::min(kScratchBufferSize - alignment,
339 kTestInputSize - frame_offset);
340 for (size_t chunk_size = 1; chunk_size < kMaxVectorSize; ++chunk_size) {
341 memcpy(aligned_scratch, kTestInput + frame_offset, aligned_len);
342 for (size_t chunk_start = 0;
343 chunk_start < aligned_len;
344 chunk_start += chunk_size) {
345 const size_t this_chunk_size = std::min(chunk_size,
346 aligned_len - chunk_start);
347 MaskWebSocketFramePayload(masking_key,
348 frame_offset + chunk_start,
349 aligned_scratch + chunk_start,
350 this_chunk_size);
352 // Stop the test if it fails, since we don't want to spew thousands of
353 // failures.
354 ASSERT_TRUE(std::equal(aligned_scratch, aligned_scratch + aligned_len,
355 kTestOutput + frame_offset))
356 << "Output failed to match for frame_offset="
357 << frame_offset
358 << ", alignment="
359 << alignment
360 << ", chunk_size="
361 << chunk_size;
367 class WebSocketFrameTestMaskBenchmark : public testing::Test {
368 public:
369 WebSocketFrameTestMaskBenchmark()
370 : iterations_(kDefaultIterations) {}
372 void SetUp() {
373 std::string iterations(
374 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
375 kBenchmarkIterations));
376 int benchmark_iterations = 0;
377 if (!iterations.empty() && base::StringToInt(iterations,
378 &benchmark_iterations)) {
379 iterations_ = benchmark_iterations;
383 void Benchmark(const char* const payload, size_t size) {
384 std::vector<char> scratch(payload, payload + size);
385 static const char kMaskingKey[] = "\xFE\xED\xBE\xEF";
386 COMPILE_ASSERT(arraysize(kMaskingKey) ==
387 WebSocketFrameHeader::kMaskingKeyLength + 1,
388 incorrect_masking_key_size);
389 WebSocketMaskingKey masking_key;
390 std::copy(kMaskingKey, kMaskingKey +
391 WebSocketFrameHeader::kMaskingKeyLength,
392 masking_key.key);
393 LOG(INFO) << "Benchmarking MaskWebSocketFramePayload() for "
394 << iterations_ << " iterations";
395 using base::TimeTicks;
396 TimeTicks start = TimeTicks::HighResNow();
397 for (int x = 0; x < iterations_; ++x) {
398 MaskWebSocketFramePayload(masking_key, x % size, &scratch.front(),
399 scratch.size());
401 double total_time_ms =
402 1000 * (TimeTicks::HighResNow() - start).InMillisecondsF() /
403 iterations_;
404 LOG(INFO) << "Payload size " << size
405 << StringPrintf(" took %.03f microseconds per iteration",
406 total_time_ms);
409 private:
410 int iterations_;
412 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameTestMaskBenchmark);
415 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskShortPayload) {
416 static const char kShortPayload[] = "Short Payload";
417 Benchmark(kShortPayload, arraysize(kShortPayload));
420 TEST_F(WebSocketFrameTestMaskBenchmark, BenchmarkMaskLongPayload) {
421 scoped_array<char> payload(new char[kLongPayloadSize]);
422 std::fill(payload.get(), payload.get() + kLongPayloadSize, 'a');
423 Benchmark(payload.get(), kLongPayloadSize);
426 } // namespace net