Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / net / spdy / hpack_input_stream_test.cc
blobb54174565c6a7b430abfd31057642e44e274293b
1 // Copyright 2014 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/spdy/hpack_input_stream.h"
7 #include <bitset>
8 #include <string>
9 #include <vector>
11 #include "base/logging.h"
12 #include "base/strings/string_piece.h"
13 #include "net/spdy/hpack_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace net {
18 namespace {
20 using base::StringPiece;
21 using std::string;
23 const size_t kLiteralBound = 1024;
25 class HpackInputStreamTest : public ::testing::Test {
26 virtual void SetUp() {
27 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
28 EXPECT_TRUE(huffman_table.Initialize(&code[0], code.size()));
31 protected:
32 HpackHuffmanTable huffman_table;
35 const char kEncodedFixture[] = "\x33" // Length prefix.
36 "\xc5\xad\xb7\x7f\x87\x6f\xc7\xfb\xf7\xfd\xbf\xbe\xbf\xf3\xf7\xf4"
37 "\xfb\x7e\xbb\xbe\x9f\x5f\x87\xe3\x7f\xef\xed\xfa\xee\xfa\x7c\x3f"
38 "\x1d\x5d\x1a\x23\xce\x54\x64\x36\xcd\x49\x4b\xd5\xd1\xcc\x5f\x05"
39 "\x35\x96\x9b";
41 const char kDecodedFixture[] =
42 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1";
44 // Utility function to decode an assumed-valid uint32 with an N-bit
45 // prefix.
46 uint32 DecodeValidUint32(uint8 N, StringPiece str) {
47 EXPECT_GT(N, 0);
48 EXPECT_LE(N, 8);
49 HpackInputStream input_stream(kLiteralBound, str);
50 input_stream.SetBitOffsetForTest(8 - N);
51 uint32 I;
52 EXPECT_TRUE(input_stream.DecodeNextUint32ForTest(&I));
53 return I;
56 // Utility function to decode an assumed-invalid uint32 with an N-bit
57 // prefix.
58 void ExpectDecodeUint32Invalid(uint8 N, StringPiece str) {
59 EXPECT_GT(N, 0);
60 EXPECT_LE(N, 8);
61 HpackInputStream input_stream(kLiteralBound, str);
62 input_stream.SetBitOffsetForTest(8 - N);
63 uint32 I;
64 EXPECT_FALSE(input_stream.DecodeNextUint32ForTest(&I));
67 uint32 bits32(const string& bitstring) {
68 return std::bitset<32>(bitstring).to_ulong();
71 // The {Number}ByteIntegersEightBitPrefix tests below test that
72 // certain integers are decoded correctly with an 8-bit prefix in
73 // exactly {Number} bytes.
75 TEST_F(HpackInputStreamTest, OneByteIntegersEightBitPrefix) {
76 // Minimum.
77 EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1)));
78 EXPECT_EQ(0x7fu, DecodeValidUint32(8, "\x7f"));
79 // Maximum.
80 EXPECT_EQ(0xfeu, DecodeValidUint32(8, "\xfe"));
81 // Invalid.
82 ExpectDecodeUint32Invalid(8, "\xff");
85 TEST_F(HpackInputStreamTest, TwoByteIntegersEightBitPrefix) {
86 // Minimum.
87 EXPECT_EQ(0xffu, DecodeValidUint32(8, string("\xff\x00", 2)));
88 EXPECT_EQ(0x0100u, DecodeValidUint32(8, "\xff\x01"));
89 // Maximum.
90 EXPECT_EQ(0x017eu, DecodeValidUint32(8, "\xff\x7f"));
91 // Invalid.
92 ExpectDecodeUint32Invalid(8, "\xff\x80");
93 ExpectDecodeUint32Invalid(8, "\xff\xff");
96 TEST_F(HpackInputStreamTest, ThreeByteIntegersEightBitPrefix) {
97 // Minimum.
98 EXPECT_EQ(0x017fu, DecodeValidUint32(8, "\xff\x80\x01"));
99 EXPECT_EQ(0x0fffu, DecodeValidUint32(8, "\xff\x80\x1e"));
100 // Maximum.
101 EXPECT_EQ(0x40feu, DecodeValidUint32(8, "\xff\xff\x7f"));
102 // Invalid.
103 ExpectDecodeUint32Invalid(8, "\xff\x80\x00");
104 ExpectDecodeUint32Invalid(8, "\xff\xff\x00");
105 ExpectDecodeUint32Invalid(8, "\xff\xff\x80");
106 ExpectDecodeUint32Invalid(8, "\xff\xff\xff");
109 TEST_F(HpackInputStreamTest, FourByteIntegersEightBitPrefix) {
110 // Minimum.
111 EXPECT_EQ(0x40ffu, DecodeValidUint32(8, "\xff\x80\x80\x01"));
112 EXPECT_EQ(0xffffu, DecodeValidUint32(8, "\xff\x80\xfe\x03"));
113 // Maximum.
114 EXPECT_EQ(0x002000feu, DecodeValidUint32(8, "\xff\xff\xff\x7f"));
115 // Invalid.
116 ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00");
117 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00");
118 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80");
119 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff");
122 TEST_F(HpackInputStreamTest, FiveByteIntegersEightBitPrefix) {
123 // Minimum.
124 EXPECT_EQ(0x002000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x01"));
125 EXPECT_EQ(0x00ffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07"));
126 // Maximum.
127 EXPECT_EQ(0x100000feu, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f"));
128 // Invalid.
129 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00");
130 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00");
131 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80");
132 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff");
135 TEST_F(HpackInputStreamTest, SixByteIntegersEightBitPrefix) {
136 // Minimum.
137 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01"));
138 // Maximum.
139 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f"));
140 // Invalid.
141 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00");
142 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10");
143 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff");
146 // There are no valid uint32 encodings that are greater than six
147 // bytes.
148 TEST_F(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) {
149 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00");
150 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01");
151 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff");
154 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that
155 // certain integers are encoded correctly with an N-bit prefix in
156 // exactly {Number} bytes for N in {1, 2, ..., 7}.
158 TEST_F(HpackInputStreamTest, OneByteIntegersOneToSevenBitPrefixes) {
159 // Minimums.
160 EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x00", 1)));
161 EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x80", 1)));
162 EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\x00", 1)));
163 EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\xc0", 1)));
164 EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\x00", 1)));
165 EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\xe0", 1)));
166 EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\x00", 1)));
167 EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\xf0", 1)));
168 EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\x00", 1)));
169 EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\xf8", 1)));
170 EXPECT_EQ(0x00u, DecodeValidUint32(2, string("\x00", 1)));
171 EXPECT_EQ(0x00u, DecodeValidUint32(2, string("\xfc", 1)));
172 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\x00", 1)));
173 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\xfe", 1)));
175 // Maximums.
176 EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\x7e"));
177 EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\xfe"));
178 EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\x3e"));
179 EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\xfe"));
180 EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\x1e"));
181 EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\xfe"));
182 EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\x0e"));
183 EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\xfe"));
184 EXPECT_EQ(0x06u, DecodeValidUint32(3, "\x06"));
185 EXPECT_EQ(0x06u, DecodeValidUint32(3, "\xfe"));
186 EXPECT_EQ(0x02u, DecodeValidUint32(2, "\x02"));
187 EXPECT_EQ(0x02u, DecodeValidUint32(2, "\xfe"));
188 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\x00", 1)));
189 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\xfe", 1)));
191 // Invalid.
192 ExpectDecodeUint32Invalid(7, "\x7f");
193 ExpectDecodeUint32Invalid(7, "\xff");
194 ExpectDecodeUint32Invalid(6, "\x3f");
195 ExpectDecodeUint32Invalid(6, "\xff");
196 ExpectDecodeUint32Invalid(5, "\x1f");
197 ExpectDecodeUint32Invalid(5, "\xff");
198 ExpectDecodeUint32Invalid(4, "\x0f");
199 ExpectDecodeUint32Invalid(4, "\xff");
200 ExpectDecodeUint32Invalid(3, "\x07");
201 ExpectDecodeUint32Invalid(3, "\xff");
202 ExpectDecodeUint32Invalid(2, "\x03");
203 ExpectDecodeUint32Invalid(2, "\xff");
204 ExpectDecodeUint32Invalid(1, "\x01");
205 ExpectDecodeUint32Invalid(1, "\xff");
208 TEST_F(HpackInputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) {
209 // Minimums.
210 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\x7f\x00", 2)));
211 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\xff\x00", 2)));
212 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\x3f\x00", 2)));
213 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\xff\x00", 2)));
214 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\x1f\x00", 2)));
215 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\xff\x00", 2)));
216 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\x0f\x00", 2)));
217 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\xff\x00", 2)));
218 EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\x07\x00", 2)));
219 EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\xff\x00", 2)));
220 EXPECT_EQ(0x03u, DecodeValidUint32(2, string("\x03\x00", 2)));
221 EXPECT_EQ(0x03u, DecodeValidUint32(2, string("\xff\x00", 2)));
222 EXPECT_EQ(0x01u, DecodeValidUint32(1, string("\x01\x00", 2)));
223 EXPECT_EQ(0x01u, DecodeValidUint32(1, string("\xff\x00", 2)));
225 // Maximums.
226 EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\x7f\x7f"));
227 EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\xff\x7f"));
228 EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\x3f\x7f"));
229 EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\xff\x7f"));
230 EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\x1f\x7f"));
231 EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\xff\x7f"));
232 EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\x0f\x7f"));
233 EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\xff\x7f"));
234 EXPECT_EQ(0x86u, DecodeValidUint32(3, "\x07\x7f"));
235 EXPECT_EQ(0x86u, DecodeValidUint32(3, "\xff\x7f"));
236 EXPECT_EQ(0x82u, DecodeValidUint32(2, "\x03\x7f"));
237 EXPECT_EQ(0x82u, DecodeValidUint32(2, "\xff\x7f"));
238 EXPECT_EQ(0x80u, DecodeValidUint32(1, "\x01\x7f"));
239 EXPECT_EQ(0x80u, DecodeValidUint32(1, "\xff\x7f"));
241 // Invalid.
242 ExpectDecodeUint32Invalid(7, "\x7f\x80");
243 ExpectDecodeUint32Invalid(7, "\xff\xff");
244 ExpectDecodeUint32Invalid(6, "\x3f\x80");
245 ExpectDecodeUint32Invalid(6, "\xff\xff");
246 ExpectDecodeUint32Invalid(5, "\x1f\x80");
247 ExpectDecodeUint32Invalid(5, "\xff\xff");
248 ExpectDecodeUint32Invalid(4, "\x0f\x80");
249 ExpectDecodeUint32Invalid(4, "\xff\xff");
250 ExpectDecodeUint32Invalid(3, "\x07\x80");
251 ExpectDecodeUint32Invalid(3, "\xff\xff");
252 ExpectDecodeUint32Invalid(2, "\x03\x80");
253 ExpectDecodeUint32Invalid(2, "\xff\xff");
254 ExpectDecodeUint32Invalid(1, "\x01\x80");
255 ExpectDecodeUint32Invalid(1, "\xff\xff");
258 TEST_F(HpackInputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) {
259 // Minimums.
260 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\x7f\x80\x01"));
261 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\xff\x80\x01"));
262 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\x3f\x80\x01"));
263 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\xff\x80\x01"));
264 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\x1f\x80\x01"));
265 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\xff\x80\x01"));
266 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\x0f\x80\x01"));
267 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\xff\x80\x01"));
268 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\x07\x80\x01"));
269 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\xff\x80\x01"));
270 EXPECT_EQ(0x83u, DecodeValidUint32(2, "\x03\x80\x01"));
271 EXPECT_EQ(0x83u, DecodeValidUint32(2, "\xff\x80\x01"));
272 EXPECT_EQ(0x81u, DecodeValidUint32(1, "\x01\x80\x01"));
273 EXPECT_EQ(0x81u, DecodeValidUint32(1, "\xff\x80\x01"));
275 // Maximums.
276 EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\x7f\xff\x7f"));
277 EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\xff\xff\x7f"));
278 EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\x3f\xff\x7f"));
279 EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\xff\xff\x7f"));
280 EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\x1f\xff\x7f"));
281 EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\xff\xff\x7f"));
282 EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\x0f\xff\x7f"));
283 EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\xff\xff\x7f"));
284 EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\x07\xff\x7f"));
285 EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\xff\xff\x7f"));
286 EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\x03\xff\x7f"));
287 EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\xff\xff\x7f"));
288 EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\x01\xff\x7f"));
289 EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\xff\xff\x7f"));
291 // Invalid.
292 ExpectDecodeUint32Invalid(7, "\x7f\xff\x80");
293 ExpectDecodeUint32Invalid(7, "\xff\xff\xff");
294 ExpectDecodeUint32Invalid(6, "\x3f\xff\x80");
295 ExpectDecodeUint32Invalid(6, "\xff\xff\xff");
296 ExpectDecodeUint32Invalid(5, "\x1f\xff\x80");
297 ExpectDecodeUint32Invalid(5, "\xff\xff\xff");
298 ExpectDecodeUint32Invalid(4, "\x0f\xff\x80");
299 ExpectDecodeUint32Invalid(4, "\xff\xff\xff");
300 ExpectDecodeUint32Invalid(3, "\x07\xff\x80");
301 ExpectDecodeUint32Invalid(3, "\xff\xff\xff");
302 ExpectDecodeUint32Invalid(2, "\x03\xff\x80");
303 ExpectDecodeUint32Invalid(2, "\xff\xff\xff");
304 ExpectDecodeUint32Invalid(1, "\x01\xff\x80");
305 ExpectDecodeUint32Invalid(1, "\xff\xff\xff");
308 TEST_F(HpackInputStreamTest, FourByteIntegersOneToSevenBitPrefixes) {
309 // Minimums.
310 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\x7f\x80\x80\x01"));
311 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\xff\x80\x80\x01"));
312 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\x3f\x80\x80\x01"));
313 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\xff\x80\x80\x01"));
314 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\x1f\x80\x80\x01"));
315 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\xff\x80\x80\x01"));
316 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\x0f\x80\x80\x01"));
317 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\xff\x80\x80\x01"));
318 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\x07\x80\x80\x01"));
319 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\xff\x80\x80\x01"));
320 EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\x03\x80\x80\x01"));
321 EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\xff\x80\x80\x01"));
322 EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\x01\x80\x80\x01"));
323 EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\xff\x80\x80\x01"));
325 // Maximums.
326 EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\x7f\xff\xff\x7f"));
327 EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\xff\xff\xff\x7f"));
328 EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\x3f\xff\xff\x7f"));
329 EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\xff\xff\xff\x7f"));
330 EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\x1f\xff\xff\x7f"));
331 EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\xff\xff\xff\x7f"));
332 EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\x0f\xff\xff\x7f"));
333 EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\xff\xff\xff\x7f"));
334 EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\x07\xff\xff\x7f"));
335 EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\xff\xff\xff\x7f"));
336 EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\x03\xff\xff\x7f"));
337 EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\xff\xff\xff\x7f"));
338 EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\x01\xff\xff\x7f"));
339 EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\xff\xff\xff\x7f"));
341 // Invalid.
342 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\x80");
343 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff");
344 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\x80");
345 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff");
346 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\x80");
347 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff");
348 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80");
349 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff");
350 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80");
351 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff");
352 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80");
353 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff");
354 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80");
355 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff");
358 TEST_F(HpackInputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) {
359 // Minimums.
360 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01"));
361 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x01"));
362 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01"));
363 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x01"));
364 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01"));
365 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x01"));
366 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01"));
367 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x01"));
368 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x01"));
369 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x01"));
370 EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x01"));
371 EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x01"));
372 EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x01"));
373 EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x01"));
375 // Maximums.
376 EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\x7f\xff\xff\xff\x7f"));
377 EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\xff\xff\xff\xff\x7f"));
378 EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\x3f\xff\xff\xff\x7f"));
379 EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\xff\xff\xff\xff\x7f"));
380 EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\x1f\xff\xff\xff\x7f"));
381 EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\xff\xff\xff\xff\x7f"));
382 EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\x0f\xff\xff\xff\x7f"));
383 EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\xff\xff\xff\xff\x7f"));
384 EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\x07\xff\xff\xff\x7f"));
385 EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\xff\xff\xff\xff\x7f"));
386 EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\x03\xff\xff\xff\x7f"));
387 EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\xff\xff\xff\xff\x7f"));
388 EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\x01\xff\xff\xff\x7f"));
389 EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\xff\xff\xff\xff\x7f"));
391 // Invalid.
392 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\xff\x80");
393 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff");
394 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\xff\x80");
395 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff");
396 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\xff\x80");
397 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff");
398 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80");
399 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff");
400 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80");
401 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff");
402 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80");
403 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff");
404 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80");
405 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff");
408 TEST_F(HpackInputStreamTest, SixByteIntegersOneToSevenBitPrefixes) {
409 // Minimums.
410 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01"));
411 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01"));
412 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01"));
413 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01"));
414 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01"));
415 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01"));
416 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01"));
417 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01"));
418 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01"));
419 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x80\x01"));
420 EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x80\x01"));
421 EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x80\x01"));
422 EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x80\x01"));
423 EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x80\x01"));
425 // Maximums.
426 EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\x7f\x80\xff\xff\xff\x0f"));
427 EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\xff\x80\xff\xff\xff\x0f"));
428 EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\x3f\xc0\xff\xff\xff\x0f"));
429 EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\xff\xc0\xff\xff\xff\x0f"));
430 EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\x1f\xe0\xff\xff\xff\x0f"));
431 EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\xff\xe0\xff\xff\xff\x0f"));
432 EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\x0f\xf0\xff\xff\xff\x0f"));
433 EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\xff\xf0\xff\xff\xff\x0f"));
434 EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\x07\xf8\xff\xff\xff\x0f"));
435 EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\xff\xf8\xff\xff\xff\x0f"));
436 EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\x03\xfc\xff\xff\xff\x0f"));
437 EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\xff\xfc\xff\xff\xff\x0f"));
438 EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\x01\xfe\xff\xff\xff\x0f"));
439 EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\xff\xfe\xff\xff\xff\x0f"));
441 // Invalid.
442 ExpectDecodeUint32Invalid(7, "\x7f\x80\xff\xff\xff\x10");
443 ExpectDecodeUint32Invalid(7, "\xff\x80\xff\xff\xff\xff");
444 ExpectDecodeUint32Invalid(6, "\x3f\xc0\xff\xff\xff\x10");
445 ExpectDecodeUint32Invalid(6, "\xff\xc0\xff\xff\xff\xff");
446 ExpectDecodeUint32Invalid(5, "\x1f\xe0\xff\xff\xff\x10");
447 ExpectDecodeUint32Invalid(5, "\xff\xe0\xff\xff\xff\xff");
448 ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10");
449 ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff");
450 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10");
451 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff");
452 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10");
453 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff");
454 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10");
455 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff");
458 // There are no valid uint32 encodings that are greater than six
459 // bytes.
460 TEST_F(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) {
461 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00");
462 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01");
463 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff");
464 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00");
465 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01");
466 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff");
467 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00");
468 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01");
469 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff");
470 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00");
471 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01");
472 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff");
473 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00");
474 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01");
475 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff");
476 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00");
477 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01");
478 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff");
479 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00");
480 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01");
481 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff");
484 // Decoding a valid encoded string literal should work.
485 TEST_F(HpackInputStreamTest, DecodeNextIdentityString) {
486 HpackInputStream input_stream(kLiteralBound, "\x0estring literal");
488 EXPECT_TRUE(input_stream.HasMoreData());
489 StringPiece string_piece;
490 EXPECT_TRUE(input_stream.DecodeNextIdentityString(&string_piece));
491 EXPECT_EQ("string literal", string_piece);
492 EXPECT_FALSE(input_stream.HasMoreData());
495 // Decoding an encoded string literal with size larger than
496 // |max_string_literal_size_| should fail.
497 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringSizeLimit) {
498 HpackInputStream input_stream(13, "\x0estring literal");
500 EXPECT_TRUE(input_stream.HasMoreData());
501 StringPiece string_piece;
502 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece));
505 // Decoding an encoded string literal with size larger than the
506 // remainder of the buffer should fail.
507 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) {
508 // Set the length to be one more than it should be.
509 HpackInputStream input_stream(kLiteralBound, "\x0fstring literal");
511 EXPECT_TRUE(input_stream.HasMoreData());
512 StringPiece string_piece;
513 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece));
516 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) {
517 string output, input(kEncodedFixture, arraysize(kEncodedFixture)-1);
518 HpackInputStream input_stream(arraysize(kDecodedFixture)-1, input);
520 EXPECT_TRUE(input_stream.HasMoreData());
521 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table, &output));
522 EXPECT_EQ(kDecodedFixture, output);
523 EXPECT_FALSE(input_stream.HasMoreData());
526 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) {
527 string output, input(kEncodedFixture, arraysize(kEncodedFixture)-1);
528 // Max string literal is one byte shorter than the decoded fixture.
529 HpackInputStream input_stream(arraysize(kDecodedFixture)-2, input);
531 // Decoded string overflows the max string literal.
532 EXPECT_TRUE(input_stream.HasMoreData());
533 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output));
536 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) {
537 string output, input(kEncodedFixture, arraysize(kEncodedFixture)-1);
538 input[0]++; // Input prefix is one byte larger than available input.
539 HpackInputStream input_stream(arraysize(kDecodedFixture)-1, input);
541 // Not enough buffer for declared encoded length.
542 EXPECT_TRUE(input_stream.HasMoreData());
543 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table, &output));
546 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) {
547 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad");
549 uint32 bits = 0;
550 size_t peeked_count = 0;
552 // Read 0xad.
553 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
554 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits);
555 EXPECT_EQ(8u, peeked_count);
557 // Read 0xab.
558 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
559 EXPECT_EQ(bits32("10101101101010110000000000000000"), bits);
560 EXPECT_EQ(16u, peeked_count);
562 input_stream.ConsumeBits(5);
563 bits = bits << 5;
564 peeked_count -= 5;
565 EXPECT_EQ(bits32("10110101011000000000000000000000"), bits);
566 EXPECT_EQ(11u, peeked_count);
568 // Read 0xad.
569 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
570 EXPECT_EQ(bits32("10110101011101011010000000000000"), bits);
571 EXPECT_EQ(19u, peeked_count);
573 // Read 0xab.
574 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
575 EXPECT_EQ(bits32("10110101011101011011010101100000"), bits);
576 EXPECT_EQ(27u, peeked_count);
578 // Read 0xa, and 1 bit of 0xd
579 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
580 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits);
581 EXPECT_EQ(32u, peeked_count);
583 // |bits| is full, and doesn't change.
584 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits));
585 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits);
586 EXPECT_EQ(32u, peeked_count);
588 input_stream.ConsumeBits(27);
589 bits = bits << 27;
590 peeked_count -= 27;
591 EXPECT_EQ(bits32("10101000000000000000000000000000"), bits);
592 EXPECT_EQ(5u, peeked_count);
594 // Read remaining 3 bits of 0xd.
595 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
596 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits);
597 EXPECT_EQ(8u, peeked_count);
599 // EOF.
600 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits));
601 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits);
602 EXPECT_EQ(8u, peeked_count);
604 input_stream.ConsumeBits(8);
605 EXPECT_FALSE(input_stream.HasMoreData());
608 TEST_F(HpackInputStreamTest, ConsumeByteRemainder) {
609 HpackInputStream input_stream(kLiteralBound, "\xad\xab");
610 // Does nothing.
611 input_stream.ConsumeByteRemainder();
613 // Consumes one byte.
614 input_stream.ConsumeBits(3);
615 input_stream.ConsumeByteRemainder();
616 EXPECT_TRUE(input_stream.HasMoreData());
618 input_stream.ConsumeBits(6);
619 EXPECT_TRUE(input_stream.HasMoreData());
620 input_stream.ConsumeByteRemainder();
621 EXPECT_FALSE(input_stream.HasMoreData());
624 } // namespace
626 } // namespace net