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"
11 #include "base/strings/string_piece.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 using base::StringPiece
;
21 // Utility function to decode an assumed-valid uint32 with an N-bit
23 uint32
DecodeValidUint32(uint8 N
, StringPiece str
) {
26 HpackInputStream
input_stream(kuint32max
, str
);
27 input_stream
.SetBitOffsetForTest(8 - N
);
29 EXPECT_TRUE(input_stream
.DecodeNextUint32ForTest(&I
));
33 // Utility function to decode an assumed-invalid uint32 with an N-bit
35 void ExpectDecodeUint32Invalid(uint8 N
, StringPiece str
) {
38 HpackInputStream
input_stream(kuint32max
, str
);
39 input_stream
.SetBitOffsetForTest(8 - N
);
41 EXPECT_FALSE(input_stream
.DecodeNextUint32ForTest(&I
));
44 uint32
bits32(const string
& bitstring
) {
45 return std::bitset
<32>(bitstring
).to_ulong();
48 // The {Number}ByteIntegersEightBitPrefix tests below test that
49 // certain integers are decoded correctly with an 8-bit prefix in
50 // exactly {Number} bytes.
52 TEST(HpackInputStreamTest
, OneByteIntegersEightBitPrefix
) {
54 EXPECT_EQ(0x00u
, DecodeValidUint32(8, string("\x00", 1)));
55 EXPECT_EQ(0x7fu
, DecodeValidUint32(8, "\x7f"));
57 EXPECT_EQ(0xfeu
, DecodeValidUint32(8, "\xfe"));
59 ExpectDecodeUint32Invalid(8, "\xff");
62 TEST(HpackInputStreamTest
, TwoByteIntegersEightBitPrefix
) {
64 EXPECT_EQ(0xffu
, DecodeValidUint32(8, string("\xff\x00", 2)));
65 EXPECT_EQ(0x0100u
, DecodeValidUint32(8, "\xff\x01"));
67 EXPECT_EQ(0x017eu
, DecodeValidUint32(8, "\xff\x7f"));
69 ExpectDecodeUint32Invalid(8, "\xff\x80");
70 ExpectDecodeUint32Invalid(8, "\xff\xff");
73 TEST(HpackInputStreamTest
, ThreeByteIntegersEightBitPrefix
) {
75 EXPECT_EQ(0x017fu
, DecodeValidUint32(8, "\xff\x80\x01"));
76 EXPECT_EQ(0x0fffu
, DecodeValidUint32(8, "\xff\x80\x1e"));
78 EXPECT_EQ(0x40feu
, DecodeValidUint32(8, "\xff\xff\x7f"));
80 ExpectDecodeUint32Invalid(8, "\xff\x80\x00");
81 ExpectDecodeUint32Invalid(8, "\xff\xff\x00");
82 ExpectDecodeUint32Invalid(8, "\xff\xff\x80");
83 ExpectDecodeUint32Invalid(8, "\xff\xff\xff");
86 TEST(HpackInputStreamTest
, FourByteIntegersEightBitPrefix
) {
88 EXPECT_EQ(0x40ffu
, DecodeValidUint32(8, "\xff\x80\x80\x01"));
89 EXPECT_EQ(0xffffu
, DecodeValidUint32(8, "\xff\x80\xfe\x03"));
91 EXPECT_EQ(0x002000feu
, DecodeValidUint32(8, "\xff\xff\xff\x7f"));
93 ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00");
94 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00");
95 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80");
96 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff");
99 TEST(HpackInputStreamTest
, FiveByteIntegersEightBitPrefix
) {
101 EXPECT_EQ(0x002000ffu
, DecodeValidUint32(8, "\xff\x80\x80\x80\x01"));
102 EXPECT_EQ(0x00ffffffu
, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07"));
104 EXPECT_EQ(0x100000feu
, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f"));
106 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00");
107 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00");
108 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80");
109 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff");
112 TEST(HpackInputStreamTest
, SixByteIntegersEightBitPrefix
) {
114 EXPECT_EQ(0x100000ffu
, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01"));
116 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f"));
118 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00");
119 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10");
120 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff");
123 // There are no valid uint32 encodings that are greater than six
125 TEST(HpackInputStreamTest
, SevenByteIntegersEightBitPrefix
) {
126 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00");
127 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01");
128 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff");
131 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that
132 // certain integers are encoded correctly with an N-bit prefix in
133 // exactly {Number} bytes for N in {1, 2, ..., 7}.
135 TEST(HpackInputStreamTest
, OneByteIntegersOneToSevenBitPrefixes
) {
137 EXPECT_EQ(0x00u
, DecodeValidUint32(7, string("\x00", 1)));
138 EXPECT_EQ(0x00u
, DecodeValidUint32(7, string("\x80", 1)));
139 EXPECT_EQ(0x00u
, DecodeValidUint32(6, string("\x00", 1)));
140 EXPECT_EQ(0x00u
, DecodeValidUint32(6, string("\xc0", 1)));
141 EXPECT_EQ(0x00u
, DecodeValidUint32(5, string("\x00", 1)));
142 EXPECT_EQ(0x00u
, DecodeValidUint32(5, string("\xe0", 1)));
143 EXPECT_EQ(0x00u
, DecodeValidUint32(4, string("\x00", 1)));
144 EXPECT_EQ(0x00u
, DecodeValidUint32(4, string("\xf0", 1)));
145 EXPECT_EQ(0x00u
, DecodeValidUint32(3, string("\x00", 1)));
146 EXPECT_EQ(0x00u
, DecodeValidUint32(3, string("\xf8", 1)));
147 EXPECT_EQ(0x00u
, DecodeValidUint32(2, string("\x00", 1)));
148 EXPECT_EQ(0x00u
, DecodeValidUint32(2, string("\xfc", 1)));
149 EXPECT_EQ(0x00u
, DecodeValidUint32(1, string("\x00", 1)));
150 EXPECT_EQ(0x00u
, DecodeValidUint32(1, string("\xfe", 1)));
153 EXPECT_EQ(0x7eu
, DecodeValidUint32(7, "\x7e"));
154 EXPECT_EQ(0x7eu
, DecodeValidUint32(7, "\xfe"));
155 EXPECT_EQ(0x3eu
, DecodeValidUint32(6, "\x3e"));
156 EXPECT_EQ(0x3eu
, DecodeValidUint32(6, "\xfe"));
157 EXPECT_EQ(0x1eu
, DecodeValidUint32(5, "\x1e"));
158 EXPECT_EQ(0x1eu
, DecodeValidUint32(5, "\xfe"));
159 EXPECT_EQ(0x0eu
, DecodeValidUint32(4, "\x0e"));
160 EXPECT_EQ(0x0eu
, DecodeValidUint32(4, "\xfe"));
161 EXPECT_EQ(0x06u
, DecodeValidUint32(3, "\x06"));
162 EXPECT_EQ(0x06u
, DecodeValidUint32(3, "\xfe"));
163 EXPECT_EQ(0x02u
, DecodeValidUint32(2, "\x02"));
164 EXPECT_EQ(0x02u
, DecodeValidUint32(2, "\xfe"));
165 EXPECT_EQ(0x00u
, DecodeValidUint32(1, string("\x00", 1)));
166 EXPECT_EQ(0x00u
, DecodeValidUint32(1, string("\xfe", 1)));
169 ExpectDecodeUint32Invalid(7, "\x7f");
170 ExpectDecodeUint32Invalid(7, "\xff");
171 ExpectDecodeUint32Invalid(6, "\x3f");
172 ExpectDecodeUint32Invalid(6, "\xff");
173 ExpectDecodeUint32Invalid(5, "\x1f");
174 ExpectDecodeUint32Invalid(5, "\xff");
175 ExpectDecodeUint32Invalid(4, "\x0f");
176 ExpectDecodeUint32Invalid(4, "\xff");
177 ExpectDecodeUint32Invalid(3, "\x07");
178 ExpectDecodeUint32Invalid(3, "\xff");
179 ExpectDecodeUint32Invalid(2, "\x03");
180 ExpectDecodeUint32Invalid(2, "\xff");
181 ExpectDecodeUint32Invalid(1, "\x01");
182 ExpectDecodeUint32Invalid(1, "\xff");
185 TEST(HpackInputStreamTest
, TwoByteIntegersOneToSevenBitPrefixes
) {
187 EXPECT_EQ(0x7fu
, DecodeValidUint32(7, string("\x7f\x00", 2)));
188 EXPECT_EQ(0x7fu
, DecodeValidUint32(7, string("\xff\x00", 2)));
189 EXPECT_EQ(0x3fu
, DecodeValidUint32(6, string("\x3f\x00", 2)));
190 EXPECT_EQ(0x3fu
, DecodeValidUint32(6, string("\xff\x00", 2)));
191 EXPECT_EQ(0x1fu
, DecodeValidUint32(5, string("\x1f\x00", 2)));
192 EXPECT_EQ(0x1fu
, DecodeValidUint32(5, string("\xff\x00", 2)));
193 EXPECT_EQ(0x0fu
, DecodeValidUint32(4, string("\x0f\x00", 2)));
194 EXPECT_EQ(0x0fu
, DecodeValidUint32(4, string("\xff\x00", 2)));
195 EXPECT_EQ(0x07u
, DecodeValidUint32(3, string("\x07\x00", 2)));
196 EXPECT_EQ(0x07u
, DecodeValidUint32(3, string("\xff\x00", 2)));
197 EXPECT_EQ(0x03u
, DecodeValidUint32(2, string("\x03\x00", 2)));
198 EXPECT_EQ(0x03u
, DecodeValidUint32(2, string("\xff\x00", 2)));
199 EXPECT_EQ(0x01u
, DecodeValidUint32(1, string("\x01\x00", 2)));
200 EXPECT_EQ(0x01u
, DecodeValidUint32(1, string("\xff\x00", 2)));
203 EXPECT_EQ(0xfeu
, DecodeValidUint32(7, "\x7f\x7f"));
204 EXPECT_EQ(0xfeu
, DecodeValidUint32(7, "\xff\x7f"));
205 EXPECT_EQ(0xbeu
, DecodeValidUint32(6, "\x3f\x7f"));
206 EXPECT_EQ(0xbeu
, DecodeValidUint32(6, "\xff\x7f"));
207 EXPECT_EQ(0x9eu
, DecodeValidUint32(5, "\x1f\x7f"));
208 EXPECT_EQ(0x9eu
, DecodeValidUint32(5, "\xff\x7f"));
209 EXPECT_EQ(0x8eu
, DecodeValidUint32(4, "\x0f\x7f"));
210 EXPECT_EQ(0x8eu
, DecodeValidUint32(4, "\xff\x7f"));
211 EXPECT_EQ(0x86u
, DecodeValidUint32(3, "\x07\x7f"));
212 EXPECT_EQ(0x86u
, DecodeValidUint32(3, "\xff\x7f"));
213 EXPECT_EQ(0x82u
, DecodeValidUint32(2, "\x03\x7f"));
214 EXPECT_EQ(0x82u
, DecodeValidUint32(2, "\xff\x7f"));
215 EXPECT_EQ(0x80u
, DecodeValidUint32(1, "\x01\x7f"));
216 EXPECT_EQ(0x80u
, DecodeValidUint32(1, "\xff\x7f"));
219 ExpectDecodeUint32Invalid(7, "\x7f\x80");
220 ExpectDecodeUint32Invalid(7, "\xff\xff");
221 ExpectDecodeUint32Invalid(6, "\x3f\x80");
222 ExpectDecodeUint32Invalid(6, "\xff\xff");
223 ExpectDecodeUint32Invalid(5, "\x1f\x80");
224 ExpectDecodeUint32Invalid(5, "\xff\xff");
225 ExpectDecodeUint32Invalid(4, "\x0f\x80");
226 ExpectDecodeUint32Invalid(4, "\xff\xff");
227 ExpectDecodeUint32Invalid(3, "\x07\x80");
228 ExpectDecodeUint32Invalid(3, "\xff\xff");
229 ExpectDecodeUint32Invalid(2, "\x03\x80");
230 ExpectDecodeUint32Invalid(2, "\xff\xff");
231 ExpectDecodeUint32Invalid(1, "\x01\x80");
232 ExpectDecodeUint32Invalid(1, "\xff\xff");
235 TEST(HpackInputStreamTest
, ThreeByteIntegersOneToSevenBitPrefixes
) {
237 EXPECT_EQ(0xffu
, DecodeValidUint32(7, "\x7f\x80\x01"));
238 EXPECT_EQ(0xffu
, DecodeValidUint32(7, "\xff\x80\x01"));
239 EXPECT_EQ(0xbfu
, DecodeValidUint32(6, "\x3f\x80\x01"));
240 EXPECT_EQ(0xbfu
, DecodeValidUint32(6, "\xff\x80\x01"));
241 EXPECT_EQ(0x9fu
, DecodeValidUint32(5, "\x1f\x80\x01"));
242 EXPECT_EQ(0x9fu
, DecodeValidUint32(5, "\xff\x80\x01"));
243 EXPECT_EQ(0x8fu
, DecodeValidUint32(4, "\x0f\x80\x01"));
244 EXPECT_EQ(0x8fu
, DecodeValidUint32(4, "\xff\x80\x01"));
245 EXPECT_EQ(0x87u
, DecodeValidUint32(3, "\x07\x80\x01"));
246 EXPECT_EQ(0x87u
, DecodeValidUint32(3, "\xff\x80\x01"));
247 EXPECT_EQ(0x83u
, DecodeValidUint32(2, "\x03\x80\x01"));
248 EXPECT_EQ(0x83u
, DecodeValidUint32(2, "\xff\x80\x01"));
249 EXPECT_EQ(0x81u
, DecodeValidUint32(1, "\x01\x80\x01"));
250 EXPECT_EQ(0x81u
, DecodeValidUint32(1, "\xff\x80\x01"));
253 EXPECT_EQ(0x407eu
, DecodeValidUint32(7, "\x7f\xff\x7f"));
254 EXPECT_EQ(0x407eu
, DecodeValidUint32(7, "\xff\xff\x7f"));
255 EXPECT_EQ(0x403eu
, DecodeValidUint32(6, "\x3f\xff\x7f"));
256 EXPECT_EQ(0x403eu
, DecodeValidUint32(6, "\xff\xff\x7f"));
257 EXPECT_EQ(0x401eu
, DecodeValidUint32(5, "\x1f\xff\x7f"));
258 EXPECT_EQ(0x401eu
, DecodeValidUint32(5, "\xff\xff\x7f"));
259 EXPECT_EQ(0x400eu
, DecodeValidUint32(4, "\x0f\xff\x7f"));
260 EXPECT_EQ(0x400eu
, DecodeValidUint32(4, "\xff\xff\x7f"));
261 EXPECT_EQ(0x4006u
, DecodeValidUint32(3, "\x07\xff\x7f"));
262 EXPECT_EQ(0x4006u
, DecodeValidUint32(3, "\xff\xff\x7f"));
263 EXPECT_EQ(0x4002u
, DecodeValidUint32(2, "\x03\xff\x7f"));
264 EXPECT_EQ(0x4002u
, DecodeValidUint32(2, "\xff\xff\x7f"));
265 EXPECT_EQ(0x4000u
, DecodeValidUint32(1, "\x01\xff\x7f"));
266 EXPECT_EQ(0x4000u
, DecodeValidUint32(1, "\xff\xff\x7f"));
269 ExpectDecodeUint32Invalid(7, "\x7f\xff\x80");
270 ExpectDecodeUint32Invalid(7, "\xff\xff\xff");
271 ExpectDecodeUint32Invalid(6, "\x3f\xff\x80");
272 ExpectDecodeUint32Invalid(6, "\xff\xff\xff");
273 ExpectDecodeUint32Invalid(5, "\x1f\xff\x80");
274 ExpectDecodeUint32Invalid(5, "\xff\xff\xff");
275 ExpectDecodeUint32Invalid(4, "\x0f\xff\x80");
276 ExpectDecodeUint32Invalid(4, "\xff\xff\xff");
277 ExpectDecodeUint32Invalid(3, "\x07\xff\x80");
278 ExpectDecodeUint32Invalid(3, "\xff\xff\xff");
279 ExpectDecodeUint32Invalid(2, "\x03\xff\x80");
280 ExpectDecodeUint32Invalid(2, "\xff\xff\xff");
281 ExpectDecodeUint32Invalid(1, "\x01\xff\x80");
282 ExpectDecodeUint32Invalid(1, "\xff\xff\xff");
285 TEST(HpackInputStreamTest
, FourByteIntegersOneToSevenBitPrefixes
) {
287 EXPECT_EQ(0x407fu
, DecodeValidUint32(7, "\x7f\x80\x80\x01"));
288 EXPECT_EQ(0x407fu
, DecodeValidUint32(7, "\xff\x80\x80\x01"));
289 EXPECT_EQ(0x403fu
, DecodeValidUint32(6, "\x3f\x80\x80\x01"));
290 EXPECT_EQ(0x403fu
, DecodeValidUint32(6, "\xff\x80\x80\x01"));
291 EXPECT_EQ(0x401fu
, DecodeValidUint32(5, "\x1f\x80\x80\x01"));
292 EXPECT_EQ(0x401fu
, DecodeValidUint32(5, "\xff\x80\x80\x01"));
293 EXPECT_EQ(0x400fu
, DecodeValidUint32(4, "\x0f\x80\x80\x01"));
294 EXPECT_EQ(0x400fu
, DecodeValidUint32(4, "\xff\x80\x80\x01"));
295 EXPECT_EQ(0x4007u
, DecodeValidUint32(3, "\x07\x80\x80\x01"));
296 EXPECT_EQ(0x4007u
, DecodeValidUint32(3, "\xff\x80\x80\x01"));
297 EXPECT_EQ(0x4003u
, DecodeValidUint32(2, "\x03\x80\x80\x01"));
298 EXPECT_EQ(0x4003u
, DecodeValidUint32(2, "\xff\x80\x80\x01"));
299 EXPECT_EQ(0x4001u
, DecodeValidUint32(1, "\x01\x80\x80\x01"));
300 EXPECT_EQ(0x4001u
, DecodeValidUint32(1, "\xff\x80\x80\x01"));
303 EXPECT_EQ(0x20007eu
, DecodeValidUint32(7, "\x7f\xff\xff\x7f"));
304 EXPECT_EQ(0x20007eu
, DecodeValidUint32(7, "\xff\xff\xff\x7f"));
305 EXPECT_EQ(0x20003eu
, DecodeValidUint32(6, "\x3f\xff\xff\x7f"));
306 EXPECT_EQ(0x20003eu
, DecodeValidUint32(6, "\xff\xff\xff\x7f"));
307 EXPECT_EQ(0x20001eu
, DecodeValidUint32(5, "\x1f\xff\xff\x7f"));
308 EXPECT_EQ(0x20001eu
, DecodeValidUint32(5, "\xff\xff\xff\x7f"));
309 EXPECT_EQ(0x20000eu
, DecodeValidUint32(4, "\x0f\xff\xff\x7f"));
310 EXPECT_EQ(0x20000eu
, DecodeValidUint32(4, "\xff\xff\xff\x7f"));
311 EXPECT_EQ(0x200006u
, DecodeValidUint32(3, "\x07\xff\xff\x7f"));
312 EXPECT_EQ(0x200006u
, DecodeValidUint32(3, "\xff\xff\xff\x7f"));
313 EXPECT_EQ(0x200002u
, DecodeValidUint32(2, "\x03\xff\xff\x7f"));
314 EXPECT_EQ(0x200002u
, DecodeValidUint32(2, "\xff\xff\xff\x7f"));
315 EXPECT_EQ(0x200000u
, DecodeValidUint32(1, "\x01\xff\xff\x7f"));
316 EXPECT_EQ(0x200000u
, DecodeValidUint32(1, "\xff\xff\xff\x7f"));
319 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\x80");
320 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff");
321 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\x80");
322 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff");
323 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\x80");
324 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff");
325 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80");
326 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff");
327 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80");
328 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff");
329 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80");
330 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff");
331 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80");
332 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff");
335 TEST(HpackInputStreamTest
, FiveByteIntegersOneToSevenBitPrefixes
) {
337 EXPECT_EQ(0x20007fu
, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01"));
338 EXPECT_EQ(0x20007fu
, DecodeValidUint32(7, "\xff\x80\x80\x80\x01"));
339 EXPECT_EQ(0x20003fu
, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01"));
340 EXPECT_EQ(0x20003fu
, DecodeValidUint32(6, "\xff\x80\x80\x80\x01"));
341 EXPECT_EQ(0x20001fu
, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01"));
342 EXPECT_EQ(0x20001fu
, DecodeValidUint32(5, "\xff\x80\x80\x80\x01"));
343 EXPECT_EQ(0x20000fu
, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01"));
344 EXPECT_EQ(0x20000fu
, DecodeValidUint32(4, "\xff\x80\x80\x80\x01"));
345 EXPECT_EQ(0x200007u
, DecodeValidUint32(3, "\x07\x80\x80\x80\x01"));
346 EXPECT_EQ(0x200007u
, DecodeValidUint32(3, "\xff\x80\x80\x80\x01"));
347 EXPECT_EQ(0x200003u
, DecodeValidUint32(2, "\x03\x80\x80\x80\x01"));
348 EXPECT_EQ(0x200003u
, DecodeValidUint32(2, "\xff\x80\x80\x80\x01"));
349 EXPECT_EQ(0x200001u
, DecodeValidUint32(1, "\x01\x80\x80\x80\x01"));
350 EXPECT_EQ(0x200001u
, DecodeValidUint32(1, "\xff\x80\x80\x80\x01"));
353 EXPECT_EQ(0x1000007eu
, DecodeValidUint32(7, "\x7f\xff\xff\xff\x7f"));
354 EXPECT_EQ(0x1000007eu
, DecodeValidUint32(7, "\xff\xff\xff\xff\x7f"));
355 EXPECT_EQ(0x1000003eu
, DecodeValidUint32(6, "\x3f\xff\xff\xff\x7f"));
356 EXPECT_EQ(0x1000003eu
, DecodeValidUint32(6, "\xff\xff\xff\xff\x7f"));
357 EXPECT_EQ(0x1000001eu
, DecodeValidUint32(5, "\x1f\xff\xff\xff\x7f"));
358 EXPECT_EQ(0x1000001eu
, DecodeValidUint32(5, "\xff\xff\xff\xff\x7f"));
359 EXPECT_EQ(0x1000000eu
, DecodeValidUint32(4, "\x0f\xff\xff\xff\x7f"));
360 EXPECT_EQ(0x1000000eu
, DecodeValidUint32(4, "\xff\xff\xff\xff\x7f"));
361 EXPECT_EQ(0x10000006u
, DecodeValidUint32(3, "\x07\xff\xff\xff\x7f"));
362 EXPECT_EQ(0x10000006u
, DecodeValidUint32(3, "\xff\xff\xff\xff\x7f"));
363 EXPECT_EQ(0x10000002u
, DecodeValidUint32(2, "\x03\xff\xff\xff\x7f"));
364 EXPECT_EQ(0x10000002u
, DecodeValidUint32(2, "\xff\xff\xff\xff\x7f"));
365 EXPECT_EQ(0x10000000u
, DecodeValidUint32(1, "\x01\xff\xff\xff\x7f"));
366 EXPECT_EQ(0x10000000u
, DecodeValidUint32(1, "\xff\xff\xff\xff\x7f"));
369 ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\xff\x80");
370 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff");
371 ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\xff\x80");
372 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff");
373 ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\xff\x80");
374 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff");
375 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80");
376 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff");
377 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80");
378 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff");
379 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80");
380 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff");
381 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80");
382 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff");
385 TEST(HpackInputStreamTest
, SixByteIntegersOneToSevenBitPrefixes
) {
387 EXPECT_EQ(0x1000007fu
, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01"));
388 EXPECT_EQ(0x1000007fu
, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01"));
389 EXPECT_EQ(0x1000003fu
, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01"));
390 EXPECT_EQ(0x1000003fu
, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01"));
391 EXPECT_EQ(0x1000001fu
, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01"));
392 EXPECT_EQ(0x1000001fu
, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01"));
393 EXPECT_EQ(0x1000000fu
, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01"));
394 EXPECT_EQ(0x1000000fu
, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01"));
395 EXPECT_EQ(0x10000007u
, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01"));
396 EXPECT_EQ(0x10000007u
, DecodeValidUint32(3, "\xff\x80\x80\x80\x80\x01"));
397 EXPECT_EQ(0x10000003u
, DecodeValidUint32(2, "\x03\x80\x80\x80\x80\x01"));
398 EXPECT_EQ(0x10000003u
, DecodeValidUint32(2, "\xff\x80\x80\x80\x80\x01"));
399 EXPECT_EQ(0x10000001u
, DecodeValidUint32(1, "\x01\x80\x80\x80\x80\x01"));
400 EXPECT_EQ(0x10000001u
, DecodeValidUint32(1, "\xff\x80\x80\x80\x80\x01"));
403 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(7, "\x7f\x80\xff\xff\xff\x0f"));
404 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(7, "\xff\x80\xff\xff\xff\x0f"));
405 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(6, "\x3f\xc0\xff\xff\xff\x0f"));
406 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(6, "\xff\xc0\xff\xff\xff\x0f"));
407 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(5, "\x1f\xe0\xff\xff\xff\x0f"));
408 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(5, "\xff\xe0\xff\xff\xff\x0f"));
409 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(4, "\x0f\xf0\xff\xff\xff\x0f"));
410 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(4, "\xff\xf0\xff\xff\xff\x0f"));
411 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(3, "\x07\xf8\xff\xff\xff\x0f"));
412 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(3, "\xff\xf8\xff\xff\xff\x0f"));
413 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(2, "\x03\xfc\xff\xff\xff\x0f"));
414 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(2, "\xff\xfc\xff\xff\xff\x0f"));
415 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(1, "\x01\xfe\xff\xff\xff\x0f"));
416 EXPECT_EQ(0xffffffffu
, DecodeValidUint32(1, "\xff\xfe\xff\xff\xff\x0f"));
419 ExpectDecodeUint32Invalid(7, "\x7f\x80\xff\xff\xff\x10");
420 ExpectDecodeUint32Invalid(7, "\xff\x80\xff\xff\xff\xff");
421 ExpectDecodeUint32Invalid(6, "\x3f\xc0\xff\xff\xff\x10");
422 ExpectDecodeUint32Invalid(6, "\xff\xc0\xff\xff\xff\xff");
423 ExpectDecodeUint32Invalid(5, "\x1f\xe0\xff\xff\xff\x10");
424 ExpectDecodeUint32Invalid(5, "\xff\xe0\xff\xff\xff\xff");
425 ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10");
426 ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff");
427 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10");
428 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff");
429 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10");
430 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff");
431 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10");
432 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff");
435 // There are no valid uint32 encodings that are greater than six
437 TEST(HpackInputStreamTest
, SevenByteIntegersOneToSevenBitPrefixes
) {
438 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00");
439 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01");
440 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff");
441 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00");
442 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01");
443 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff");
444 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00");
445 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01");
446 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff");
447 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00");
448 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01");
449 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff");
450 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00");
451 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01");
452 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff");
453 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00");
454 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01");
455 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff");
456 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00");
457 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01");
458 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff");
461 // Decoding a valid encoded string literal should work.
462 TEST(HpackInputStreamTest
, DecodeNextStringLiteral
) {
463 HpackInputStream
input_stream(kuint32max
, "\x0estring literal");
465 EXPECT_TRUE(input_stream
.HasMoreData());
466 StringPiece string_piece
;
467 EXPECT_TRUE(input_stream
.DecodeNextStringLiteralForTest(&string_piece
));
468 EXPECT_EQ("string literal", string_piece
);
469 EXPECT_FALSE(input_stream
.HasMoreData());
472 // Decoding an encoded string literal with size larger than
473 // |max_string_literal_size_| should fail.
474 TEST(HpackInputStreamTest
, DecodeNextStringLiteralSizeLimit
) {
475 HpackInputStream
input_stream(13, "\x0estring literal");
477 EXPECT_TRUE(input_stream
.HasMoreData());
478 StringPiece string_piece
;
479 EXPECT_FALSE(input_stream
.DecodeNextStringLiteralForTest(&string_piece
));
482 // Decoding an encoded string literal with size larger than the
483 // remainder of the buffer should fail.
484 TEST(HpackInputStreamTest
, DecodeNextStringLiteralInvalidSize
) {
485 // Set the length to be one more than it should be.
486 HpackInputStream
input_stream(kuint32max
, "\x0fstring literal");
488 EXPECT_TRUE(input_stream
.HasMoreData());
489 StringPiece string_piece
;
490 EXPECT_FALSE(input_stream
.DecodeNextStringLiteralForTest(&string_piece
));
493 TEST(HpackInputStreamTest
, PeekBitsAndConsume
) {
494 HpackInputStream
input_stream(kuint32max
, "\xad\xab\xad\xab\xad");
497 size_t peeked_count
= 0;
500 EXPECT_TRUE(input_stream
.PeekBits(&peeked_count
, &bits
));
501 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits
);
502 EXPECT_EQ(8u, peeked_count
);
505 EXPECT_TRUE(input_stream
.PeekBits(&peeked_count
, &bits
));
506 EXPECT_EQ(bits32("10101101101010110000000000000000"), bits
);
507 EXPECT_EQ(16u, peeked_count
);
509 input_stream
.ConsumeBits(5);
512 EXPECT_EQ(bits32("10110101011000000000000000000000"), bits
);
513 EXPECT_EQ(11u, peeked_count
);
516 EXPECT_TRUE(input_stream
.PeekBits(&peeked_count
, &bits
));
517 EXPECT_EQ(bits32("10110101011101011010000000000000"), bits
);
518 EXPECT_EQ(19u, peeked_count
);
521 EXPECT_TRUE(input_stream
.PeekBits(&peeked_count
, &bits
));
522 EXPECT_EQ(bits32("10110101011101011011010101100000"), bits
);
523 EXPECT_EQ(27u, peeked_count
);
525 // Read 0xa, and 1 bit of 0xd
526 EXPECT_TRUE(input_stream
.PeekBits(&peeked_count
, &bits
));
527 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits
);
528 EXPECT_EQ(32u, peeked_count
);
530 // |bits| is full, and doesn't change.
531 EXPECT_FALSE(input_stream
.PeekBits(&peeked_count
, &bits
));
532 EXPECT_EQ(bits32("10110101011101011011010101110101"), bits
);
533 EXPECT_EQ(32u, peeked_count
);
535 input_stream
.ConsumeBits(27);
538 EXPECT_EQ(bits32("10101000000000000000000000000000"), bits
);
539 EXPECT_EQ(5u, peeked_count
);
541 // Read remaining 3 bits of 0xd.
542 EXPECT_TRUE(input_stream
.PeekBits(&peeked_count
, &bits
));
543 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits
);
544 EXPECT_EQ(8u, peeked_count
);
547 EXPECT_FALSE(input_stream
.PeekBits(&peeked_count
, &bits
));
548 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits
);
549 EXPECT_EQ(8u, peeked_count
);
551 input_stream
.ConsumeBits(8);
552 EXPECT_FALSE(input_stream
.HasMoreData());
555 TEST(HpackInputStreamTest
, ConsumeByteRemainder
) {
556 HpackInputStream
input_stream(kuint32max
, "\xad\xab");
558 input_stream
.ConsumeByteRemainder();
560 // Consumes one byte.
561 input_stream
.ConsumeBits(3);
562 input_stream
.ConsumeByteRemainder();
563 EXPECT_TRUE(input_stream
.HasMoreData());
565 input_stream
.ConsumeBits(6);
566 EXPECT_TRUE(input_stream
.HasMoreData());
567 input_stream
.ConsumeByteRemainder();
568 EXPECT_FALSE(input_stream
.HasMoreData());