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 <algorithm> | 8 #include <algorithm> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/memory/aligned_memory.h" | 12 #include "base/memory/aligned_memory.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 TEST(WebSocketFrameHeaderTest, FrameLengths) { | 20 TEST(WebSocketFrameHeaderTest, FrameLengths) { |
20 struct TestCase { | 21 struct TestCase { |
21 const char* frame_header; | 22 const char* frame_header; |
22 size_t frame_header_length; | 23 size_t frame_header_length; |
23 uint64 frame_length; | 24 uint64 frame_length; |
24 }; | 25 }; |
25 static const TestCase kTests[] = { | 26 static const TestCase kTests[] = { |
26 { "\x81\x00", 2, GG_UINT64_C(0) }, | 27 { "\x81\x00", 2, UINT64_C(0) }, |
27 { "\x81\x7D", 2, GG_UINT64_C(125) }, | 28 { "\x81\x7D", 2, UINT64_C(125) }, |
28 { "\x81\x7E\x00\x7E", 4, GG_UINT64_C(126) }, | 29 { "\x81\x7E\x00\x7E", 4, UINT64_C(126) }, |
29 { "\x81\x7E\xFF\xFF", 4, GG_UINT64_C(0xFFFF) }, | 30 { "\x81\x7E\xFF\xFF", 4, UINT64_C(0xFFFF) }, |
30 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, GG_UINT64_C(0x10000) }, | 31 { "\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, | 32 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, |
32 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | 33 UINT64_C(0x7FFFFFFFFFFFFFFF) } |
33 }; | 34 }; |
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, |
(...skipping 11 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 }; |
73 static const int kNumTests = arraysize(kTests); | 74 static const int kNumTests = arraysize(kTests); |
74 | 75 |
75 WebSocketMaskingKey masking_key; | 76 WebSocketMaskingKey masking_key; |
76 std::copy(kMaskingKey, | 77 std::copy(kMaskingKey, |
77 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, | 78 kMaskingKey + WebSocketFrameHeader::kMaskingKeyLength, |
78 masking_key.key); | 79 masking_key.key); |
79 | 80 |
80 for (int i = 0; i < kNumTests; ++i) { | 81 for (int i = 0; i < kNumTests; ++i) { |
81 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 82 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 } | 177 } |
177 } | 178 } |
178 | 179 |
179 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { | 180 TEST(WebSocketFrameHeaderTest, InsufficientBufferSize) { |
180 struct TestCase { | 181 struct TestCase { |
181 uint64 payload_length; | 182 uint64 payload_length; |
182 bool masked; | 183 bool masked; |
183 size_t expected_header_size; | 184 size_t expected_header_size; |
184 }; | 185 }; |
185 static const TestCase kTests[] = { | 186 static const TestCase kTests[] = { |
186 { GG_UINT64_C(0), false, 2u }, | 187 { UINT64_C(0), false, 2u }, |
187 { GG_UINT64_C(125), false, 2u }, | 188 { UINT64_C(125), false, 2u }, |
188 { GG_UINT64_C(126), false, 4u }, | 189 { UINT64_C(126), false, 4u }, |
189 { GG_UINT64_C(0xFFFF), false, 4u }, | 190 { UINT64_C(0xFFFF), false, 4u }, |
190 { GG_UINT64_C(0x10000), false, 10u }, | 191 { UINT64_C(0x10000), false, 10u }, |
191 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, | 192 { UINT64_C(0x7FFFFFFFFFFFFFFF), false, 10u }, |
192 { GG_UINT64_C(0), true, 6u }, | 193 { UINT64_C(0), true, 6u }, |
193 { GG_UINT64_C(125), true, 6u }, | 194 { UINT64_C(125), true, 6u }, |
194 { GG_UINT64_C(126), true, 8u }, | 195 { UINT64_C(126), true, 8u }, |
195 { GG_UINT64_C(0xFFFF), true, 8u }, | 196 { UINT64_C(0xFFFF), true, 8u }, |
196 { GG_UINT64_C(0x10000), true, 14u }, | 197 { UINT64_C(0x10000), true, 14u }, |
197 { GG_UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u } | 198 { UINT64_C(0x7FFFFFFFFFFFFFFF), true, 14u } |
198 }; | 199 }; |
199 static const int kNumTests = arraysize(kTests); | 200 static const int kNumTests = arraysize(kTests); |
200 | 201 |
201 for (int i = 0; i < kNumTests; ++i) { | 202 for (int i = 0; i < kNumTests; ++i) { |
202 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); | 203 WebSocketFrameHeader header(WebSocketFrameHeader::kOpCodeText); |
203 header.final = true; | 204 header.final = true; |
204 header.opcode = WebSocketFrameHeader::kOpCodeText; | 205 header.opcode = WebSocketFrameHeader::kOpCodeText; |
205 header.masked = kTests[i].masked; | 206 header.masked = kTests[i].masked; |
206 header.payload_length = kTests[i].payload_length; | 207 header.payload_length = kTests[i].payload_length; |
207 | 208 |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); | 386 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xF)); |
386 | 387 |
387 // Check that out-of-range opcodes return false | 388 // Check that out-of-range opcodes return false |
388 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); | 389 EXPECT_FALSE(Frame::IsKnownControlOpCode(-1)); |
389 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); | 390 EXPECT_FALSE(Frame::IsKnownControlOpCode(0xFF)); |
390 } | 391 } |
391 | 392 |
392 } // namespace | 393 } // namespace |
393 | 394 |
394 } // namespace net | 395 } // namespace net |
OLD | NEW |