OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "net/websockets/websocket_frame.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace net { | |
13 | |
14 TEST(WebSocketFrameTest, FrameLengths) { | |
mmenke
2012/05/21 15:08:37
nit: Suggest adding "Header" as the prefix of all
Yuta Kitamura
2012/05/22 10:29:35
Done.
| |
15 struct TestCase { | |
16 const char* frame_header; | |
17 size_t frame_header_length; | |
18 uint64 frame_length; | |
19 }; | |
20 static const TestCase kTests[] = { | |
21 { "\x81\x00", 2, GG_UINT64_C(0) }, | |
22 { "\x81\x7D", 2, GG_UINT64_C(125) }, | |
23 { "\x81\x7E\x00\x7E", 4, GG_UINT64_C(126) }, | |
24 { "\x81\x7E\xFF\xFF", 4, GG_UINT64_C(0xFFFF) }, | |
25 { "\x81\x7F\x00\x00\x00\x00\x00\x01\x00\x00", 10, GG_UINT64_C(0x10000) }, | |
26 { "\x81\x7F\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10, | |
27 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | |
28 }; | |
29 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | |
30 | |
31 for (int i = 0; i < kNumTests; ++i) { | |
32 WebSocketFrameHeader header; | |
33 header.final = true; | |
34 header.reserved1 = false; | |
35 header.reserved2 = false; | |
36 header.reserved3 = false; | |
37 header.opcode = WebSocketFrameHeader::kOpCodeText; | |
38 header.masked = false; | |
39 header.payload_length = kTests[i].frame_length; | |
40 | |
41 std::vector<char> expected_output( | |
42 kTests[i].frame_header, | |
43 kTests[i].frame_header + kTests[i].frame_header_length); | |
44 std::vector<char> output; | |
45 WriteWebSocketFrameHeader(&header, NULL, &output); | |
46 | |
47 EXPECT_EQ(expected_output, output); | |
48 } | |
49 } | |
50 | |
51 TEST(WebSocketFrameTest, FrameLengthsWithMasking) { | |
52 static const char kMaskingKey[] = "\xDE\xAD\xBE\xEF"; | |
53 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(kMaskingKey) - 1 == | |
54 WebSocketFrameHeader::kMaskingKeyLength, | |
55 incorrect_masking_key_size); | |
56 | |
57 struct TestCase { | |
58 const char* frame_header; | |
59 size_t frame_header_length; | |
60 uint64 frame_length; | |
61 }; | |
62 static const TestCase kTests[] = { | |
63 { "\x81\x80\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(0) }, | |
64 { "\x81\xFD\xDE\xAD\xBE\xEF", 6, GG_UINT64_C(125) }, | |
65 { "\x81\xFE\x00\x7E\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(126) }, | |
66 { "\x81\xFE\xFF\xFF\xDE\xAD\xBE\xEF", 8, GG_UINT64_C(0xFFFF) }, | |
67 { "\x81\xFF\x00\x00\x00\x00\x00\x01\x00\x00\xDE\xAD\xBE\xEF", 14, | |
68 GG_UINT64_C(0x10000) }, | |
69 { "\x81\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xDE\xAD\xBE\xEF", 14, | |
70 GG_UINT64_C(0x7FFFFFFFFFFFFFFF) } | |
71 }; | |
72 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | |
73 | |
74 for (int i = 0; i < kNumTests; ++i) { | |
75 WebSocketFrameHeader header; | |
76 header.final = true; | |
77 header.reserved1 = false; | |
78 header.reserved2 = false; | |
79 header.reserved3 = false; | |
80 header.opcode = WebSocketFrameHeader::kOpCodeText; | |
81 header.masked = true; | |
82 header.payload_length = kTests[i].frame_length; | |
83 | |
84 std::vector<char> expected_output( | |
85 kTests[i].frame_header, | |
86 kTests[i].frame_header + kTests[i].frame_header_length); | |
87 std::vector<char> output; | |
88 WriteWebSocketFrameHeader(&header, kMaskingKey, &output); | |
89 | |
90 EXPECT_EQ(expected_output, output); | |
91 } | |
92 } | |
93 | |
94 TEST(WebSocketFrameTest, FrameOpCodes) { | |
95 struct TestCase { | |
96 const char* frame_header; | |
97 size_t frame_header_length; | |
98 WebSocketFrameHeader::OpCode opcode; | |
99 }; | |
100 static const TestCase kTests[] = { | |
101 { "\x80\x00", 2, WebSocketFrameHeader::kOpCodeContinuation }, | |
102 { "\x81\x00", 2, WebSocketFrameHeader::kOpCodeText }, | |
103 { "\x82\x00", 2, WebSocketFrameHeader::kOpCodeBinary }, | |
104 { "\x88\x00", 2, WebSocketFrameHeader::kOpCodeClose }, | |
105 { "\x89\x00", 2, WebSocketFrameHeader::kOpCodePing }, | |
106 { "\x8A\x00", 2, WebSocketFrameHeader::kOpCodePong }, | |
107 // These are undefined opcodes, but the builder should accept them anyway. | |
108 { "\x83\x00", 2, 0x3 }, | |
109 { "\x84\x00", 2, 0x4 }, | |
110 { "\x85\x00", 2, 0x5 }, | |
111 { "\x86\x00", 2, 0x6 }, | |
112 { "\x87\x00", 2, 0x7 }, | |
113 { "\x8B\x00", 2, 0xB }, | |
114 { "\x8C\x00", 2, 0xC }, | |
115 { "\x8D\x00", 2, 0xD }, | |
116 { "\x8E\x00", 2, 0xE }, | |
117 { "\x8F\x00", 2, 0xF } | |
118 }; | |
119 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | |
120 | |
121 for (int i = 0; i < kNumTests; ++i) { | |
122 WebSocketFrameHeader header; | |
123 header.final = true; | |
124 header.reserved1 = false; | |
125 header.reserved2 = false; | |
126 header.reserved3 = false; | |
127 header.opcode = kTests[i].opcode; | |
128 header.masked = false; | |
129 header.payload_length = 0; | |
130 | |
131 std::vector<char> expected_output( | |
132 kTests[i].frame_header, | |
133 kTests[i].frame_header + kTests[i].frame_header_length); | |
134 std::vector<char> output; | |
135 WriteWebSocketFrameHeader(&header, NULL, &output); | |
136 | |
137 EXPECT_EQ(expected_output, output); | |
138 } | |
139 } | |
140 | |
141 TEST(WebSocketFrameTest, FinalBitAndReservedBits) { | |
142 struct TestCase { | |
143 const char* frame_header; | |
144 size_t frame_header_length; | |
145 bool final; | |
146 bool reserved1; | |
147 bool reserved2; | |
148 bool reserved3; | |
149 }; | |
150 static const TestCase kTests[] = { | |
151 { "\x81\x00", 2, true, false, false, false }, | |
152 { "\x01\x00", 2, false, false, false, false }, | |
153 { "\xC1\x00", 2, true, true, false, false }, | |
154 { "\xA1\x00", 2, true, false, true, false }, | |
155 { "\x91\x00", 2, true, false, false, true }, | |
156 { "\x71\x00", 2, false, true, true, true }, | |
157 { "\xF1\x00", 2, true, true, true, true } | |
158 }; | |
159 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | |
160 | |
161 for (int i = 0; i < kNumTests; ++i) { | |
162 WebSocketFrameHeader header; | |
163 header.final = kTests[i].final; | |
164 header.reserved1 = kTests[i].reserved1; | |
165 header.reserved2 = kTests[i].reserved2; | |
166 header.reserved3 = kTests[i].reserved3; | |
167 header.opcode = WebSocketFrameHeader::kOpCodeText; | |
168 header.masked = false; | |
169 header.payload_length = 0; | |
170 | |
171 std::vector<char> expected_output( | |
172 kTests[i].frame_header, | |
173 kTests[i].frame_header + kTests[i].frame_header_length); | |
174 std::vector<char> output; | |
175 WriteWebSocketFrameHeader(&header, NULL, &output); | |
176 | |
177 EXPECT_EQ(expected_output, output); | |
178 } | |
179 } | |
180 | |
181 TEST(WebSocketFrameTest, MaskPayload) { | |
182 struct TestCase { | |
183 const char* masking_key; | |
184 uint64 frame_offset; | |
185 const char* input; | |
186 const char* output; | |
187 size_t data_length; | |
188 }; | |
189 static const TestCase kTests[] = { | |
190 { "\xDE\xAD\xBE\xEF", 0, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, | |
191 { "\xDE\xAD\xBE\xEF", 1, "FooBar", "\xEB\xD1\x80\x9C\xCC\xCC", 6 }, | |
192 { "\xDE\xAD\xBE\xEF", 2, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, | |
193 { "\xDE\xAD\xBE\xEF", 3, "FooBar", "\xA9\xB1\xC2\xFC\x8E\xAC", 6 }, | |
194 { "\xDE\xAD\xBE\xEF", 4, "FooBar", "\x98\xC2\xD1\xAD\xBF\xDF", 6 }, | |
195 { "\xDE\xAD\xBE\xEF", 42, "FooBar", "\xF8\x80\xB1\xEF\xDF\x9D", 6 }, | |
196 { "\xDE\xAD\xBE\xEF", 0, "", "", 0 }, | |
197 { "\xDE\xAD\xBE\xEF", 0, "\xDE\xAD\xBE\xEF", "\x00\x00\x00\x00", 4 }, | |
198 { "\xDE\xAD\xBE\xEF", 0, "\x00\x00\x00\x00", "\xDE\xAD\xBE\xEF", 4 }, | |
199 { "\x00\x00\x00\x00", 0, "FooBar", "FooBar", 6 }, | |
200 { "\xFF\xFF\xFF\xFF", 0, "FooBar", "\xB9\x90\x90\xBD\x9E\x8D", 6 }, | |
201 }; | |
202 static const int kNumTests = ARRAYSIZE_UNSAFE(kTests); | |
203 | |
204 for (int i = 0; i < kNumTests; ++i) { | |
205 std::vector<char> frame_data(kTests[i].input, | |
206 kTests[i].input + kTests[i].data_length); | |
207 std::vector<char> expected_output(kTests[i].output, | |
208 kTests[i].output + kTests[i].data_length); | |
209 | |
210 MaskWebSocketFramePayload(kTests[i].masking_key, | |
211 kTests[i].frame_offset, | |
212 &frame_data); | |
213 EXPECT_EQ(expected_output, frame_data); | |
214 } | |
215 } | |
216 | |
217 } // namespace net | |
OLD | NEW |