| 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 <stdint.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 11 #include "base/memory/aligned_memory.h" | 13 #include "base/memory/aligned_memory.h" |
| 12 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 namespace net { | 17 namespace net { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 TEST(WebSocketFrameHeaderTest, FrameLengths) { | 21 TEST(WebSocketFrameHeaderTest, FrameLengths) { |
| 20 struct TestCase { | 22 struct TestCase { |
| 21 const char* frame_header; | 23 const char* frame_header; |
| 22 size_t frame_header_length; | 24 size_t frame_header_length; |
| 23 uint64 frame_length; | 25 uint64 frame_length; |
| 24 }; | 26 }; |
| 25 static const TestCase kTests[] = { | 27 static const TestCase kTests[] = { |
| 26 { "\x81\x00", 2, GG_UINT64_C(0) }, | 28 {"\x81\x00", 2, UINT64_C(0)}, |
| 27 { "\x81\x7D", 2, GG_UINT64_C(125) }, | 29 {"\x81\x7D", 2, UINT64_C(125)}, |
| 28 { "\x81\x7E\x00\x7E", 4, GG_UINT64_C(126) }, | 30 {"\x81\x7E\x00\x7E", 4, UINT64_C(126)}, |
| 29 { "\x81\x7E\xFF\xFF", 4, GG_UINT64_C(0xFFFF) }, | 31 {"\x81\x7E\xFF\xFF", 4, UINT64_C(0xFFFF)}, |
| 30 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, GG_UINT64_C(0x10000) }, | 32 {"\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, UINT64_C(0x10000)}, |
| 31 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, | 33 {"\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, |
| 32 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | 34 UINT64_C(0x7FFFFFFFFFFFFFFF)}}; |
| 33 }; | |
| 34 static const int kNumTests = arraysize(kTests); | 35 static const int kNumTests = arraysize(kTests); |
| 35 | 36 |
| 36 for (int i = 0; i < kNumTests; ++i) { | 37 for (int i = 0; i < kNumTests; ++i) { |
| 37 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 38 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 38 header.final = true; | 39 header.final = true; |
| 39 header.payload_length = kTests[i].frame_length; | 40 header.payload_length = kTests[i].frame_length; |
| 40 | 41 |
| 41 std::vector<char> expected_output( | 42 std::vector<char> expected_output( |
| 42 kTests[i].frame_header, | 43 kTests[i].frame_header, |
| 43 kTests[i].frame_header + kTests[i].frame_header_length); | 44 kTests[i].frame_header + kTests[i].frame_header_length); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 54 static_assert( | 55 static_assert( |
| 55 arraysize(kMaskingKey) - 1 == WebSocketFrameHeader::kMaskingKeyLength, | 56 arraysize(kMaskingKey) - 1 == WebSocketFrameHeader::kMaskingKeyLength, |
| 56 "incorrect masking key size"); | 57 "incorrect masking key size"); |
| 57 | 58 |
| 58 struct TestCase { | 59 struct TestCase { |
| 59 const char* frame_header; | 60 const char* frame_header; |
| 60 size_t frame_header_length; | 61 size_t frame_header_length; |
| 61 uint64 frame_length; | 62 uint64 frame_length; |
| 62 }; | 63 }; |
| 63 static const TestCase kTests[] = { | 64 static const TestCase kTests[] = { |
| 64 { "\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0) }, | 65 {"\x81\x80\xDE\xAD\xBE\xEF", 6, UINT64_C(0)}, |
| 65 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(125) }, | 66 {"\x81\xFD\xDE\xAD\xBE\xEF", 6, UINT64_C(125)}, |
| 66 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(126) }, | 67 {"\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, UINT64_C(126)}, |
| 67 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(0xFFFF) }, | 68 {"\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, UINT64_C(0xFFFF)}, |
| 68 { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, | 69 {"\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, |
| 69 GG_UINT64_C(0x10000) }, | 70 UINT64_C(0x10000)}, |
| 70 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, | 71 {"\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, |
| 71 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | 72 UINT64_C(0x7FFFFFFFFFFFFFFF)}}; |
| 72 }; | |
| 73 static const int kNumTests = arraysize(kTests); | 73 static const int kNumTests = arraysize(kTests); |
| 74 | 74 |
| 75 WebSocketMaskingKey masking_key; | 75 WebSocketMaskingKey masking_key; |
| 76 std::copy(kMaskingKey, | 76 std::copy(kMaskingKey, |
| 77 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, | 77 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
| 78 masking_key.key); | 78 masking_key.key); |
| 79 | 79 |
| 80 for (int i = 0; i < kNumTests; ++i) { | 80 for (int i = 0; i < kNumTests; ++i) { |
| 81 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 81 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 82 header.final = true; | 82 header.final = true; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 EXPECT_EQ(expected_output, output); | 175 EXPECT_EQ(expected_output, output); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { | 179 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { |
| 180 struct TestCase { | 180 struct TestCase { |
| 181 uint64 payload_length; | 181 uint64 payload_length; |
| 182 bool masked; | 182 bool masked; |
| 183 size_t expected_header_size; | 183 size_t expected_header_size; |
| 184 }; | 184 }; |
| 185 static const TestCase kTests[] = { | 185 static const TestCase kTests[] = {{UINT64_C(0), false, 2u}, |
| 186 { GG_UINT64_C(0), false, 2u }, | 186 {UINT64_C(125), false, 2u}, |
| 187 { GG_UINT64_C(125), false, 2u }, | 187 {UINT64_C(126), false, 4u}, |
| 188 { GG_UINT64_C(126), false, 4u }, | 188 {UINT64_C(0xFFFF), false, 4u}, |
| 189 { GG_UINT64_C(0xFFFF), false, 4u }, | 189 {UINT64_C(0x10000), false, 10u}, |
| 190 { GG_UINT64_C(0x10000), false, 10u }, | 190 {UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u}, |
| 191 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, | 191 {UINT64_C(0), true, 6u}, |
| 192 { GG_UINT64_C(0), true, 6u }, | 192 {UINT64_C(125), true, 6u}, |
| 193 { GG_UINT64_C(125), true, 6u }, | 193 {UINT64_C(126), true, 8u}, |
| 194 { GG_UINT64_C(126), true, 8u }, | 194 {UINT64_C(0xFFFF), true, 8u}, |
| 195 { GG_UINT64_C(0xFFFF), true, 8u }, | 195 {UINT64_C(0x10000), true, 14u}, |
| 196 { GG_UINT64_C(0x10000), true, 14u }, | 196 {UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u}}; |
| 197 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u } | |
| 198 }; | |
| 199 static const int kNumTests = arraysize(kTests); | 197 static const int kNumTests = arraysize(kTests); |
| 200 | 198 |
| 201 for (int i = 0; i < kNumTests; ++i) { | 199 for (int i = 0; i < kNumTests; ++i) { |
| 202 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 200 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
| 203 header.final = true; | 201 header.final = true; |
| 204 header.opcode = WebSocketFrameHeader::kOpCodeText; | 202 header.opcode = WebSocketFrameHeader::kOpCodeText; |
| 205 header.masked = kTests[i].masked; | 203 header.masked = kTests[i].masked; |
| 206 header.payload_length = kTests[i].payload_length; | 204 header.payload_length = kTests[i].payload_length; |
| 207 | 205 |
| 208 char dummy_buffer[14]; | 206 char dummy_buffer[14]; |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); | 383 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
| 386 | 384 |
| 387 // Check that out-of-range opcodes return false | 385 // Check that out-of-range opcodes return false |
| 388 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); | 386 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
| 389 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); | 387 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
| 390 } | 388 } |
| 391 | 389 |
| 392 } // namespace | 390 } // namespace |
| 393 | 391 |
| 394 } // namespace net | 392 } // namespace net |
| OLD | NEW |