Delay audio rendering until the clock reaches the first timestamp.
[chromium-blink-merge.git] / net / spdy / hpack_input_stream_test.cc
bloba88acb2d37c3744ae132a38accf409e38054d175
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 "net/spdy/spdy_test_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace net {
19 namespace {
21 using base::StringPiece;
22 using std::string;
23 using test::a2b_hex;
25 const size_t kLiteralBound = 1024;
27 class HpackInputStreamTest : public ::testing::Test {
28 public:
29 void SetUp() override {
30 std::vector<HpackHuffmanSymbol> code = HpackHuffmanCode();
31 EXPECT_TRUE(huffman_table_.Initialize(&code[0], code.size()));
34 protected:
35 HpackHuffmanTable huffman_table_;
38 // Hex representation of encoded length and Huffman string.
39 const char kEncodedHuffmanFixture[] = "2d" // Length prefix.
40 "94e7821dd7f2e6c7b335dfdfcd5b3960"
41 "d5af27087f3672c1ab270fb5291f9587"
42 "316065c003ed4ee5b1063d5007";
44 const char kDecodedHuffmanFixture[] =
45 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1";
47 // Utility function to decode an assumed-valid uint32 with an N-bit
48 // prefix.
49 uint32 DecodeValidUint32(uint8 N, StringPiece str) {
50 EXPECT_GT(N, 0);
51 EXPECT_LE(N, 8);
52 HpackInputStream input_stream(kLiteralBound, str);
53 input_stream.SetBitOffsetForTest(8 - N);
54 uint32 I;
55 EXPECT_TRUE(input_stream.DecodeNextUint32(&I));
56 return I;
59 // Utility function to decode an assumed-invalid uint32 with an N-bit
60 // prefix.
61 void ExpectDecodeUint32Invalid(uint8 N, StringPiece str) {
62 EXPECT_GT(N, 0);
63 EXPECT_LE(N, 8);
64 HpackInputStream input_stream(kLiteralBound, str);
65 input_stream.SetBitOffsetForTest(8 - N);
66 uint32 I;
67 EXPECT_FALSE(input_stream.DecodeNextUint32(&I));
70 uint32 bits32(const string& bitstring) {
71 return std::bitset<32>(bitstring).to_ulong();
74 // The {Number}ByteIntegersEightBitPrefix tests below test that
75 // certain integers are decoded correctly with an 8-bit prefix in
76 // exactly {Number} bytes.
78 TEST_F(HpackInputStreamTest, OneByteIntegersEightBitPrefix) {
79 // Minimum.
80 EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1)));
81 EXPECT_EQ(0x7fu, DecodeValidUint32(8, "\x7f"));
82 // Maximum.
83 EXPECT_EQ(0xfeu, DecodeValidUint32(8, "\xfe"));
84 // Invalid.
85 ExpectDecodeUint32Invalid(8, "\xff");
88 TEST_F(HpackInputStreamTest, TwoByteIntegersEightBitPrefix) {
89 // Minimum.
90 EXPECT_EQ(0xffu, DecodeValidUint32(8, string("\xff\x00", 2)));
91 EXPECT_EQ(0x0100u, DecodeValidUint32(8, "\xff\x01"));
92 // Maximum.
93 EXPECT_EQ(0x017eu, DecodeValidUint32(8, "\xff\x7f"));
94 // Invalid.
95 ExpectDecodeUint32Invalid(8, "\xff\x80");
96 ExpectDecodeUint32Invalid(8, "\xff\xff");
99 TEST_F(HpackInputStreamTest, ThreeByteIntegersEightBitPrefix) {
100 // Minimum.
101 EXPECT_EQ(0x017fu, DecodeValidUint32(8, "\xff\x80\x01"));
102 EXPECT_EQ(0x0fffu, DecodeValidUint32(8, "\xff\x80\x1e"));
103 // Maximum.
104 EXPECT_EQ(0x40feu, DecodeValidUint32(8, "\xff\xff\x7f"));
105 // Invalid.
106 ExpectDecodeUint32Invalid(8, "\xff\x80\x00");
107 ExpectDecodeUint32Invalid(8, "\xff\xff\x00");
108 ExpectDecodeUint32Invalid(8, "\xff\xff\x80");
109 ExpectDecodeUint32Invalid(8, "\xff\xff\xff");
112 TEST_F(HpackInputStreamTest, FourByteIntegersEightBitPrefix) {
113 // Minimum.
114 EXPECT_EQ(0x40ffu, DecodeValidUint32(8, "\xff\x80\x80\x01"));
115 EXPECT_EQ(0xffffu, DecodeValidUint32(8, "\xff\x80\xfe\x03"));
116 // Maximum.
117 EXPECT_EQ(0x002000feu, DecodeValidUint32(8, "\xff\xff\xff\x7f"));
118 // Invalid.
119 ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00");
120 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00");
121 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80");
122 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff");
125 TEST_F(HpackInputStreamTest, FiveByteIntegersEightBitPrefix) {
126 // Minimum.
127 EXPECT_EQ(0x002000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x01"));
128 EXPECT_EQ(0x00ffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07"));
129 // Maximum.
130 EXPECT_EQ(0x100000feu, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f"));
131 // Invalid.
132 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00");
133 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00");
134 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80");
135 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff");
138 TEST_F(HpackInputStreamTest, SixByteIntegersEightBitPrefix) {
139 // Minimum.
140 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01"));
141 // Maximum.
142 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f"));
143 // Invalid.
144 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00");
145 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10");
146 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff");
149 // There are no valid uint32 encodings that are greater than six
150 // bytes.
151 TEST_F(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) {
152 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00");
153 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01");
154 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff");
157 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that
158 // certain integers are encoded correctly with an N-bit prefix in
159 // exactly {Number} bytes for N in {1, 2, ..., 7}.
161 TEST_F(HpackInputStreamTest, OneByteIntegersOneToSevenBitPrefixes) {
162 // Minimums.
163 EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x00", 1)));
164 EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x80", 1)));
165 EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\x00", 1)));
166 EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\xc0", 1)));
167 EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\x00", 1)));
168 EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\xe0", 1)));
169 EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\x00", 1)));
170 EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\xf0", 1)));
171 EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\x00", 1)));
172 EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\xf8", 1)));
173 EXPECT_EQ(0x00u, DecodeValidUint32(2, string("\x00", 1)));
174 EXPECT_EQ(0x00u, DecodeValidUint32(2, string("\xfc", 1)));
175 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\x00", 1)));
176 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\xfe", 1)));
178 // Maximums.
179 EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\x7e"));
180 EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\xfe"));
181 EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\x3e"));
182 EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\xfe"));
183 EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\x1e"));
184 EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\xfe"));
185 EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\x0e"));
186 EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\xfe"));
187 EXPECT_EQ(0x06u, DecodeValidUint32(3, "\x06"));
188 EXPECT_EQ(0x06u, DecodeValidUint32(3, "\xfe"));
189 EXPECT_EQ(0x02u, DecodeValidUint32(2, "\x02"));
190 EXPECT_EQ(0x02u, DecodeValidUint32(2, "\xfe"));
191 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\x00", 1)));
192 EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\xfe", 1)));
194 // Invalid.
195 ExpectDecodeUint32Invalid(7, "\x7f");
196 ExpectDecodeUint32Invalid(7, "\xff");
197 ExpectDecodeUint32Invalid(6, "\x3f");
198 ExpectDecodeUint32Invalid(6, "\xff");
199 ExpectDecodeUint32Invalid(5, "\x1f");
200 ExpectDecodeUint32Invalid(5, "\xff");
201 ExpectDecodeUint32Invalid(4, "\x0f");
202 ExpectDecodeUint32Invalid(4, "\xff");
203 ExpectDecodeUint32Invalid(3, "\x07");
204 ExpectDecodeUint32Invalid(3, "\xff");
205 ExpectDecodeUint32Invalid(2, "\x03");
206 ExpectDecodeUint32Invalid(2, "\xff");
207 ExpectDecodeUint32Invalid(1, "\x01");
208 ExpectDecodeUint32Invalid(1, "\xff");
211 TEST_F(HpackInputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) {
212 // Minimums.
213 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\x7f\x00", 2)));
214 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\xff\x00", 2)));
215 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\x3f\x00", 2)));
216 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\xff\x00", 2)));
217 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\x1f\x00", 2)));
218 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\xff\x00", 2)));
219 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\x0f\x00", 2)));
220 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\xff\x00", 2)));
221 EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\x07\x00", 2)));
222 EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\xff\x00", 2)));
223 EXPECT_EQ(0x03u, DecodeValidUint32(2, string("\x03\x00", 2)));
224 EXPECT_EQ(0x03u, DecodeValidUint32(2, string("\xff\x00", 2)));
225 EXPECT_EQ(0x01u, DecodeValidUint32(1, string("\x01\x00", 2)));
226 EXPECT_EQ(0x01u, DecodeValidUint32(1, string("\xff\x00", 2)));
228 // Maximums.
229 EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\x7f\x7f"));
230 EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\xff\x7f"));
231 EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\x3f\x7f"));
232 EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\xff\x7f"));
233 EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\x1f\x7f"));
234 EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\xff\x7f"));
235 EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\x0f\x7f"));
236 EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\xff\x7f"));
237 EXPECT_EQ(0x86u, DecodeValidUint32(3, "\x07\x7f"));
238 EXPECT_EQ(0x86u, DecodeValidUint32(3, "\xff\x7f"));
239 EXPECT_EQ(0x82u, DecodeValidUint32(2, "\x03\x7f"));
240 EXPECT_EQ(0x82u, DecodeValidUint32(2, "\xff\x7f"));
241 EXPECT_EQ(0x80u, DecodeValidUint32(1, "\x01\x7f"));
242 EXPECT_EQ(0x80u, DecodeValidUint32(1, "\xff\x7f"));
244 // Invalid.
245 ExpectDecodeUint32Invalid(7, "\x7f\x80");
246 ExpectDecodeUint32Invalid(7, "\xff\xff");
247 ExpectDecodeUint32Invalid(6, "\x3f\x80");
248 ExpectDecodeUint32Invalid(6, "\xff\xff");
249 ExpectDecodeUint32Invalid(5, "\x1f\x80");
250 ExpectDecodeUint32Invalid(5, "\xff\xff");
251 ExpectDecodeUint32Invalid(4, "\x0f\x80");
252 ExpectDecodeUint32Invalid(4, "\xff\xff");
253 ExpectDecodeUint32Invalid(3, "\x07\x80");
254 ExpectDecodeUint32Invalid(3, "\xff\xff");
255 ExpectDecodeUint32Invalid(2, "\x03\x80");
256 ExpectDecodeUint32Invalid(2, "\xff\xff");
257 ExpectDecodeUint32Invalid(1, "\x01\x80");
258 ExpectDecodeUint32Invalid(1, "\xff\xff");
261 TEST_F(HpackInputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) {
262 // Minimums.
263 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\x7f\x80\x01"));
264 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\xff\x80\x01"));
265 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\x3f\x80\x01"));
266 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\xff\x80\x01"));
267 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\x1f\x80\x01"));
268 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\xff\x80\x01"));
269 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\x0f\x80\x01"));
270 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\xff\x80\x01"));
271 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\x07\x80\x01"));
272 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\xff\x80\x01"));
273 EXPECT_EQ(0x83u, DecodeValidUint32(2, "\x03\x80\x01"));
274 EXPECT_EQ(0x83u, DecodeValidUint32(2, "\xff\x80\x01"));
275 EXPECT_EQ(0x81u, DecodeValidUint32(1, "\x01\x80\x01"));
276 EXPECT_EQ(0x81u, DecodeValidUint32(1, "\xff\x80\x01"));
278 // Maximums.
279 EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\x7f\xff\x7f"));
280 EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\xff\xff\x7f"));
281 EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\x3f\xff\x7f"));
282 EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\xff\xff\x7f"));
283 EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\x1f\xff\x7f"));
284 EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\xff\xff\x7f"));
285 EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\x0f\xff\x7f"));
286 EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\xff\xff\x7f"));
287 EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\x07\xff\x7f"));
288 EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\xff\xff\x7f"));
289 EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\x03\xff\x7f"));
290 EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\xff\xff\x7f"));
291 EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\x01\xff\x7f"));
292 EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\xff\xff\x7f"));
294 // Invalid.
295 ExpectDecodeUint32Invalid(7, "\x7f\xff\x80");
296 ExpectDecodeUint32Invalid(7, "\xff\xff\xff");
297 ExpectDecodeUint32Invalid(6, "\x3f\xff\x80");
298 ExpectDecodeUint32Invalid(6, "\xff\xff\xff");
299 ExpectDecodeUint32Invalid(5, "\x1f\xff\x80");
300 ExpectDecodeUint32Invalid(5, "\xff\xff\xff");
301 ExpectDecodeUint32Invalid(4, "\x0f\xff\x80");
302 ExpectDecodeUint32Invalid(4, "\xff\xff\xff");
303 ExpectDecodeUint32Invalid(3, "\x07\xff\x80");
304 ExpectDecodeUint32Invalid(3, "\xff\xff\xff");
305 ExpectDecodeUint32Invalid(2, "\x03\xff\x80");
306 ExpectDecodeUint32Invalid(2, "\xff\xff\xff");
307 ExpectDecodeUint32Invalid(1, "\x01\xff\x80");
308 ExpectDecodeUint32Invalid(1, "\xff\xff\xff");
311 TEST_F(HpackInputStreamTest, FourByteIntegersOneToSevenBitPrefixes) {
312 // Minimums.
313 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\x7f\x80\x80\x01"));
314 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\xff\x80\x80\x01"));
315 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\x3f\x80\x80\x01"));
316 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\xff\x80\x80\x01"));
317 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\x1f\x80\x80\x01"));
318 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\xff\x80\x80\x01"));
319 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\x0f\x80\x80\x01"));
320 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\xff\x80\x80\x01"));
321 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\x07\x80\x80\x01"));
322 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\xff\x80\x80\x01"));
323 EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\x03\x80\x80\x01"));
324 EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\xff\x80\x80\x01"));
325 EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\x01\x80\x80\x01"));
326 EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\xff\x80\x80\x01"));
328 // Maximums.
329 EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\x7f\xff\xff\x7f"));
330 EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\xff\xff\xff\x7f"));
331 EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\x3f\xff\xff\x7f"));
332 EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\xff\xff\xff\x7f"));
333 EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\x1f\xff\xff\x7f"));
334 EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\xff\xff\xff\x7f"));
335 EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\x0f\xff\xff\x7f"));
336 EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\xff\xff\xff\x7f"));
337 EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\x07\xff\xff\x7f"));
338 EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\xff\xff\xff\x7f"));
339 EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\x03\xff\xff\x7f"));
340 EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\xff\xff\xff\x7f"));
341 EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\x01\xff\xff\x7f"));
342 EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\xff\xff\xff\x7f"));
344 // Invalid.
345 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\x80");
346 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff");
347 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\x80");
348 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff");
349 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\x80");
350 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff");
351 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80");
352 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff");
353 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80");
354 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff");
355 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80");
356 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff");
357 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80");
358 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff");
361 TEST_F(HpackInputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) {
362 // Minimums.
363 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01"));
364 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x01"));
365 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01"));
366 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x01"));
367 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01"));
368 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x01"));
369 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01"));
370 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x01"));
371 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x01"));
372 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x01"));
373 EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x01"));
374 EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x01"));
375 EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x01"));
376 EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x01"));
378 // Maximums.
379 EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\x7f\xff\xff\xff\x7f"));
380 EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\xff\xff\xff\xff\x7f"));
381 EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\x3f\xff\xff\xff\x7f"));
382 EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\xff\xff\xff\xff\x7f"));
383 EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\x1f\xff\xff\xff\x7f"));
384 EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\xff\xff\xff\xff\x7f"));
385 EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\x0f\xff\xff\xff\x7f"));
386 EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\xff\xff\xff\xff\x7f"));
387 EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\x07\xff\xff\xff\x7f"));
388 EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\xff\xff\xff\xff\x7f"));
389 EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\x03\xff\xff\xff\x7f"));
390 EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\xff\xff\xff\xff\x7f"));
391 EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\x01\xff\xff\xff\x7f"));
392 EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\xff\xff\xff\xff\x7f"));
394 // Invalid.
395 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\xff\x80");
396 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff");
397 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\xff\x80");
398 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff");
399 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\xff\x80");
400 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff");
401 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80");
402 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff");
403 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80");
404 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff");
405 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80");
406 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff");
407 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80");
408 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff");
411 TEST_F(HpackInputStreamTest, SixByteIntegersOneToSevenBitPrefixes) {
412 // Minimums.
413 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01"));
414 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01"));
415 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01"));
416 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01"));
417 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01"));
418 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01"));
419 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01"));
420 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01"));
421 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01"));
422 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x80\x01"));
423 EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x80\x01"));
424 EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x80\x01"));
425 EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x80\x01"));
426 EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x80\x01"));
428 // Maximums.
429 EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\x7f\x80\xff\xff\xff\x0f"));
430 EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\xff\x80\xff\xff\xff\x0f"));
431 EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\x3f\xc0\xff\xff\xff\x0f"));
432 EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\xff\xc0\xff\xff\xff\x0f"));
433 EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\x1f\xe0\xff\xff\xff\x0f"));
434 EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\xff\xe0\xff\xff\xff\x0f"));
435 EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\x0f\xf0\xff\xff\xff\x0f"));
436 EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\xff\xf0\xff\xff\xff\x0f"));
437 EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\x07\xf8\xff\xff\xff\x0f"));
438 EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\xff\xf8\xff\xff\xff\x0f"));
439 EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\x03\xfc\xff\xff\xff\x0f"));
440 EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\xff\xfc\xff\xff\xff\x0f"));
441 EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\x01\xfe\xff\xff\xff\x0f"));
442 EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\xff\xfe\xff\xff\xff\x0f"));
444 // Invalid.
445 ExpectDecodeUint32Invalid(7, "\x7f\x80\xff\xff\xff\x10");
446 ExpectDecodeUint32Invalid(7, "\xff\x80\xff\xff\xff\xff");
447 ExpectDecodeUint32Invalid(6, "\x3f\xc0\xff\xff\xff\x10");
448 ExpectDecodeUint32Invalid(6, "\xff\xc0\xff\xff\xff\xff");
449 ExpectDecodeUint32Invalid(5, "\x1f\xe0\xff\xff\xff\x10");
450 ExpectDecodeUint32Invalid(5, "\xff\xe0\xff\xff\xff\xff");
451 ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10");
452 ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff");
453 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10");
454 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff");
455 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10");
456 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff");
457 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10");
458 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff");
461 // There are no valid uint32 encodings that are greater than six
462 // bytes.
463 TEST_F(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) {
464 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00");
465 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01");
466 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff");
467 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00");
468 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01");
469 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff");
470 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00");
471 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01");
472 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff");
473 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00");
474 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01");
475 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff");
476 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00");
477 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01");
478 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff");
479 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00");
480 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01");
481 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff");
482 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00");
483 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01");
484 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff");
487 // Decoding a valid encoded string literal should work.
488 TEST_F(HpackInputStreamTest, DecodeNextIdentityString) {
489 HpackInputStream input_stream(kLiteralBound, "\x0estring literal");
491 EXPECT_TRUE(input_stream.HasMoreData());
492 StringPiece string_piece;
493 EXPECT_TRUE(input_stream.DecodeNextIdentityString(&string_piece));
494 EXPECT_EQ("string literal", string_piece);
495 EXPECT_FALSE(input_stream.HasMoreData());
498 // Decoding an encoded string literal with size larger than
499 // |max_string_literal_size_| should fail.
500 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringSizeLimit) {
501 HpackInputStream input_stream(13, "\x0estring literal");
503 EXPECT_TRUE(input_stream.HasMoreData());
504 StringPiece string_piece;
505 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece));
508 // Decoding an encoded string literal with size larger than the
509 // remainder of the buffer should fail.
510 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) {
511 // Set the length to be one more than it should be.
512 HpackInputStream input_stream(kLiteralBound, "\x0fstring literal");
514 EXPECT_TRUE(input_stream.HasMoreData());
515 StringPiece string_piece;
516 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece));
519 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) {
520 string output, input(a2b_hex(kEncodedHuffmanFixture));
521 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input);
523 EXPECT_TRUE(input_stream.HasMoreData());
524 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table_, &output));
525 EXPECT_EQ(kDecodedHuffmanFixture, output);
526 EXPECT_FALSE(input_stream.HasMoreData());
529 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) {
530 string output, input(a2b_hex(kEncodedHuffmanFixture));
531 // Max string literal is one byte shorter than the decoded fixture.
532 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-2, input);
534 // Decoded string overflows the max string literal.
535 EXPECT_TRUE(input_stream.HasMoreData());
536 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output));
539 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) {
540 string output, input(a2b_hex(kEncodedHuffmanFixture));
541 input[0]++; // Input prefix is one byte larger than available input.
542 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture)-1, input);
544 // Not enough buffer for declared encoded length.
545 EXPECT_TRUE(input_stream.HasMoreData());
546 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output));
549 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) {
550 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad");
552 uint32 bits = 0;
553 size_t peeked_count = 0;
555 // Read 0xad.
556 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
557 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits);
558 EXPECT_EQ(8u, peeked_count);
560 // Read 0xab.
561 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
562 EXPECT_EQ(bits32("10101101101010110000000000000000"), bits);
563 EXPECT_EQ(16u, peeked_count);
565 input_stream.ConsumeBits(5);
566 bits = bits << 5;
567 peeked_count -= 5;
568 EXPECT_EQ(bits32("10110101011000000000000000000000"), bits);
569 EXPECT_EQ(11u, peeked_count);
571 // Read 0xad.
572 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
573 EXPECT_EQ(bits32("10110101011101011010000000000000"), bits);
574 EXPECT_EQ(19u, peeked_count);
576 // Read 0xab.
577 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
578 EXPECT_EQ(bits32("10110101011101011011010101100000"), bits);
579 EXPECT_EQ(27u, peeked_count);
581 // Read 0xa, and 1 bit of 0xd
582 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
583 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits);
584 EXPECT_EQ(32u, peeked_count);
586 // |bits| is full, and doesn't change.
587 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits));
588 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits);
589 EXPECT_EQ(32u, peeked_count);
591 input_stream.ConsumeBits(27);
592 bits = bits << 27;
593 peeked_count -= 27;
594 EXPECT_EQ(bits32("10101000000000000000000000000000"), bits);
595 EXPECT_EQ(5u, peeked_count);
597 // Read remaining 3 bits of 0xd.
598 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits));
599 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits);
600 EXPECT_EQ(8u, peeked_count);
602 // EOF.
603 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits));
604 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits);
605 EXPECT_EQ(8u, peeked_count);
607 input_stream.ConsumeBits(8);
608 EXPECT_FALSE(input_stream.HasMoreData());
611 TEST_F(HpackInputStreamTest, ConsumeByteRemainder) {
612 HpackInputStream input_stream(kLiteralBound, "\xad\xab");
613 // Does nothing.
614 input_stream.ConsumeByteRemainder();
616 // Consumes one byte.
617 input_stream.ConsumeBits(3);
618 input_stream.ConsumeByteRemainder();
619 EXPECT_TRUE(input_stream.HasMoreData());
621 input_stream.ConsumeBits(6);
622 EXPECT_TRUE(input_stream.HasMoreData());
623 input_stream.ConsumeByteRemainder();
624 EXPECT_FALSE(input_stream.HasMoreData());
627 } // namespace
629 } // namespace net