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