| 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> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/aligned_memory.h" | 12 #include "base/memory/aligned_memory.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 TEST(WebSocketFrameHeaderTest, FrameLengths) { | 20 TEST(WebSocketFrameHeaderTest, FrameLengths) { |
| 21 struct TestCase { | 21 struct TestCase { |
| 22 const char* frame_header; | 22 const char* frame_header; |
| 23 size_t frame_header_length; | 23 size_t frame_header_length; |
| 24 uint64 frame_length; | 24 uint64_t frame_length; |
| 25 }; | 25 }; |
| 26 static const TestCase kTests[] = { | 26 static const TestCase kTests[] = { |
| 27 { "\x81\x00", 2, UINT64_C(0) }, | 27 { "\x81\x00", 2, UINT64_C(0) }, |
| 28 { "\x81\x7D", 2, UINT64_C(125) }, | 28 { "\x81\x7D", 2, UINT64_C(125) }, |
| 29 { "\x81\x7E\x00\x7E", 4, UINT64_C(126) }, | 29 { "\x81\x7E\x00\x7E", 4, UINT64_C(126) }, |
| 30 { "\x81\x7E\xFF\xFF", 4, UINT64_C(0xFFFF) }, | 30 { "\x81\x7E\xFF\xFF", 4, UINT64_C(0xFFFF) }, |
| 31 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, UINT64_C(0x10000) }, | 31 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, UINT64_C(0x10000) }, |
| 32 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, | 32 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, |
| 33 UINT64_C(0x7FFFFFFFFFFFFFFF) } | 33 UINT64_C(0x7FFFFFFFFFFFFFFF) } |
| 34 }; | 34 }; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 52 | 52 |
| 53 TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) { | 53 TEST(WebSocketFrameHeaderTest, FrameLengthsWithMasking) { |
| 54 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF"; | 54 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF"; |
| 55 static_assert( | 55 static_assert( |
| 56 arraysize(kMaskingKey) - 1 == WebSocketFrameHeader::kMaskingKeyLength, | 56 arraysize(kMaskingKey) - 1 == WebSocketFrameHeader::kMaskingKeyLength, |
| 57 "incorrect masking key size"); | 57 "incorrect masking key size"); |
| 58 | 58 |
| 59 struct TestCase { | 59 struct TestCase { |
| 60 const char* frame_header; | 60 const char* frame_header; |
| 61 size_t frame_header_length; | 61 size_t frame_header_length; |
| 62 uint64 frame_length; | 62 uint64_t frame_length; |
| 63 }; | 63 }; |
| 64 static const TestCase kTests[] = { | 64 static const TestCase kTests[] = { |
| 65 { "\x81\x80\xDE\xAD\xBE\xEF", 6, UINT64_C(0) }, | 65 { "\x81\x80\xDE\xAD\xBE\xEF", 6, UINT64_C(0) }, |
| 66 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, UINT64_C(125) }, | 66 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, UINT64_C(125) }, |
| 67 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, UINT64_C(126) }, | 67 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, UINT64_C(126) }, |
| 68 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, UINT64_C(0xFFFF) }, | 68 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, UINT64_C(0xFFFF) }, |
| 69 { "\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, |
| 70 UINT64_C(0x10000) }, | 70 UINT64_C(0x10000) }, |
| 71 { "\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, |
| 72 UINT64_C(0x7FFFFFFFFFFFFFFF) } | 72 UINT64_C(0x7FFFFFFFFFFFFFFF) } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 std::vector<char> output(expected_output.size()); | 172 std::vector<char> output(expected_output.size()); |
| 173 EXPECT_EQ(static_cast<int>(expected_output.size()), | 173 EXPECT_EQ(static_cast<int>(expected_output.size()), |
| 174 WriteWebSocketFrameHeader( | 174 WriteWebSocketFrameHeader( |
| 175 header, NULL, &output.front(), output.size())); | 175 header, NULL, &output.front(), output.size())); |
| 176 EXPECT_EQ(expected_output, output); | 176 EXPECT_EQ(expected_output, output); |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { | 180 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { |
| 181 struct TestCase { | 181 struct TestCase { |
| 182 uint64 payload_length; | 182 uint64_t payload_length; |
| 183 bool masked; | 183 bool masked; |
| 184 size_t expected_header_size; | 184 size_t expected_header_size; |
| 185 }; | 185 }; |
| 186 static const TestCase kTests[] = { | 186 static const TestCase kTests[] = { |
| 187 { UINT64_C(0), false, 2u }, | 187 { UINT64_C(0), false, 2u }, |
| 188 { UINT64_C(125), false, 2u }, | 188 { UINT64_C(125), false, 2u }, |
| 189 { UINT64_C(126), false, 4u }, | 189 { UINT64_C(126), false, 4u }, |
| 190 { UINT64_C(0xFFFF), false, 4u }, | 190 { UINT64_C(0xFFFF), false, 4u }, |
| 191 { UINT64_C(0x10000), false, 10u }, | 191 { UINT64_C(0x10000), false, 10u }, |
| 192 { UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, | 192 { UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 211 EXPECT_EQ( | 211 EXPECT_EQ( |
| 212 ERR_INVALID_ARGUMENT, | 212 ERR_INVALID_ARGUMENT, |
| 213 WriteWebSocketFrameHeader( | 213 WriteWebSocketFrameHeader( |
| 214 header, NULL, dummy_buffer, kTests[i].expected_header_size - 1)); | 214 header, NULL, dummy_buffer, kTests[i].expected_header_size - 1)); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 TEST(WebSocketFrameTest, MaskPayload) { | 218 TEST(WebSocketFrameTest, MaskPayload) { |
| 219 struct TestCase { | 219 struct TestCase { |
| 220 const char* masking_key; | 220 const char* masking_key; |
| 221 uint64 frame_offset; | 221 uint64_t frame_offset; |
| 222 const char* input; | 222 const char* input; |
| 223 const char* output; | 223 const char* output; |
| 224 size_t data_length; | 224 size_t data_length; |
| 225 }; | 225 }; |
| 226 static const TestCase kTests[] = { | 226 static const TestCase kTests[] = { |
| 227 { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, | 227 { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, |
| 228 { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 }, | 228 { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 }, |
| 229 { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, | 229 { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, |
| 230 { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 }, | 230 { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 }, |
| 231 { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, | 231 { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); | 386 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
| 387 | 387 |
| 388 // Check that out-of-range opcodes return false | 388 // Check that out-of-range opcodes return false |
| 389 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); | 389 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
| 390 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); | 390 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
| 391 } | 391 } |
| 392 | 392 |
| 393 } // namespace | 393 } // namespace |
| 394 | 394 |
| 395 } // namespace net | 395 } // namespace net |
| OLD | NEW |