Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / spdy / hpack_input_stream_test.cc
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.
4
5 #include "net/spdy/hpack_input_stream.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/strings/string_piece.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace net {
14
15 namespace {
16
17 using base::StringPiece;
18 using std::string;
19
20 // Utility function to decode an assumed-valid uint32 with an N-bit
21 // prefix.
22 uint32 DecodeValidUint32(uint8 N, StringPiece str) {
23   EXPECT_GT(N, 0);
24   EXPECT_LE(N, 8);
25   HpackInputStream input_stream(kuint32max, str);
26   input_stream.SetBitOffsetForTest(8 - N);
27   uint32 I;
28   EXPECT_TRUE(input_stream.DecodeNextUint32ForTest(&I));
29   return I;
30 }
31
32 // Utility function to decode an assumed-invalid uint32 with an N-bit
33 // prefix.
34 void ExpectDecodeUint32Invalid(uint8 N, StringPiece str) {
35   EXPECT_GT(N, 0);
36   EXPECT_LE(N, 8);
37   HpackInputStream input_stream(kuint32max, str);
38   input_stream.SetBitOffsetForTest(8 - N);
39   uint32 I;
40   EXPECT_FALSE(input_stream.DecodeNextUint32ForTest(&I));
41 }
42
43 // The {Number}ByteIntegersEightBitPrefix tests below test that
44 // certain integers are decoded correctly with an 8-bit prefix in
45 // exactly {Number} bytes.
46
47 TEST(HpackInputStreamTest, OneByteIntegersEightBitPrefix) {
48   // Minimum.
49   EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1)));
50   EXPECT_EQ(0x7fu, DecodeValidUint32(8, "\x7f"));
51   // Maximum.
52   EXPECT_EQ(0xfeu, DecodeValidUint32(8, "\xfe"));
53   // Invalid.
54   ExpectDecodeUint32Invalid(8, "\xff");
55 }
56
57 TEST(HpackInputStreamTest, TwoByteIntegersEightBitPrefix) {
58   // Minimum.
59   EXPECT_EQ(0xffu, DecodeValidUint32(8, string("\xff\x00", 2)));
60   EXPECT_EQ(0x0100u, DecodeValidUint32(8, "\xff\x01"));
61   // Maximum.
62   EXPECT_EQ(0x017eu, DecodeValidUint32(8, "\xff\x7f"));
63   // Invalid.
64   ExpectDecodeUint32Invalid(8, "\xff\x80");
65   ExpectDecodeUint32Invalid(8, "\xff\xff");
66 }
67
68 TEST(HpackInputStreamTest, ThreeByteIntegersEightBitPrefix) {
69   // Minimum.
70   EXPECT_EQ(0x017fu, DecodeValidUint32(8, "\xff\x80\x01"));
71   EXPECT_EQ(0x0fffu, DecodeValidUint32(8, "\xff\x80\x1e"));
72   // Maximum.
73   EXPECT_EQ(0x40feu, DecodeValidUint32(8, "\xff\xff\x7f"));
74   // Invalid.
75   ExpectDecodeUint32Invalid(8, "\xff\x80\x00");
76   ExpectDecodeUint32Invalid(8, "\xff\xff\x00");
77   ExpectDecodeUint32Invalid(8, "\xff\xff\x80");
78   ExpectDecodeUint32Invalid(8, "\xff\xff\xff");
79 }
80
81 TEST(HpackInputStreamTest, FourByteIntegersEightBitPrefix) {
82   // Minimum.
83   EXPECT_EQ(0x40ffu, DecodeValidUint32(8, "\xff\x80\x80\x01"));
84   EXPECT_EQ(0xffffu, DecodeValidUint32(8, "\xff\x80\xfe\x03"));
85   // Maximum.
86   EXPECT_EQ(0x002000feu, DecodeValidUint32(8, "\xff\xff\xff\x7f"));
87   // Invalid.
88   ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00");
89   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00");
90   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80");
91   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff");
92 }
93
94 TEST(HpackInputStreamTest, FiveByteIntegersEightBitPrefix) {
95   // Minimum.
96   EXPECT_EQ(0x002000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x01"));
97   EXPECT_EQ(0x00ffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07"));
98   // Maximum.
99   EXPECT_EQ(0x100000feu, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f"));
100   // Invalid.
101   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00");
102   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00");
103   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80");
104   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff");
105 }
106
107 TEST(HpackInputStreamTest, SixByteIntegersEightBitPrefix) {
108   // Minimum.
109   EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01"));
110   // Maximum.
111   EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f"));
112   // Invalid.
113   ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00");
114   ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10");
115   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff");
116 }
117
118 // There are no valid uint32 encodings that are greater than six
119 // bytes.
120 TEST(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) {
121   ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00");
122   ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01");
123   ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff");
124 }
125
126 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that
127 // certain integers are encoded correctly with an N-bit prefix in
128 // exactly {Number} bytes for N in {1, 2, ..., 7}.
129
130 TEST(HpackInputStreamTest, OneByteIntegersOneToSevenBitPrefixes) {
131   // Minimums.
132   EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x00", 1)));
133   EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x80", 1)));
134   EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\x00", 1)));
135   EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\xc0", 1)));
136   EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\x00", 1)));
137   EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\xe0", 1)));
138   EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\x00", 1)));
139   EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\xf0", 1)));
140   EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\x00", 1)));
141   EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\xf8", 1)));
142   EXPECT_EQ(0x00u, DecodeValidUint32(2, string("\x00", 1)));
143   EXPECT_EQ(0x00u, DecodeValidUint32(2, string("\xfc", 1)));
144   EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\x00", 1)));
145   EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\xfe", 1)));
146
147   // Maximums.
148   EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\x7e"));
149   EXPECT_EQ(0x7eu, DecodeValidUint32(7, "\xfe"));
150   EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\x3e"));
151   EXPECT_EQ(0x3eu, DecodeValidUint32(6, "\xfe"));
152   EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\x1e"));
153   EXPECT_EQ(0x1eu, DecodeValidUint32(5, "\xfe"));
154   EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\x0e"));
155   EXPECT_EQ(0x0eu, DecodeValidUint32(4, "\xfe"));
156   EXPECT_EQ(0x06u, DecodeValidUint32(3, "\x06"));
157   EXPECT_EQ(0x06u, DecodeValidUint32(3, "\xfe"));
158   EXPECT_EQ(0x02u, DecodeValidUint32(2, "\x02"));
159   EXPECT_EQ(0x02u, DecodeValidUint32(2, "\xfe"));
160   EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\x00", 1)));
161   EXPECT_EQ(0x00u, DecodeValidUint32(1, string("\xfe", 1)));
162
163   // Invalid.
164   ExpectDecodeUint32Invalid(7, "\x7f");
165   ExpectDecodeUint32Invalid(7, "\xff");
166   ExpectDecodeUint32Invalid(6, "\x3f");
167   ExpectDecodeUint32Invalid(6, "\xff");
168   ExpectDecodeUint32Invalid(5, "\x1f");
169   ExpectDecodeUint32Invalid(5, "\xff");
170   ExpectDecodeUint32Invalid(4, "\x0f");
171   ExpectDecodeUint32Invalid(4, "\xff");
172   ExpectDecodeUint32Invalid(3, "\x07");
173   ExpectDecodeUint32Invalid(3, "\xff");
174   ExpectDecodeUint32Invalid(2, "\x03");
175   ExpectDecodeUint32Invalid(2, "\xff");
176   ExpectDecodeUint32Invalid(1, "\x01");
177   ExpectDecodeUint32Invalid(1, "\xff");
178 }
179
180 TEST(HpackInputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) {
181   // Minimums.
182   EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\x7f\x00", 2)));
183   EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\xff\x00", 2)));
184   EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\x3f\x00", 2)));
185   EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\xff\x00", 2)));
186   EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\x1f\x00", 2)));
187   EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\xff\x00", 2)));
188   EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\x0f\x00", 2)));
189   EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\xff\x00", 2)));
190   EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\x07\x00", 2)));
191   EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\xff\x00", 2)));
192   EXPECT_EQ(0x03u, DecodeValidUint32(2, string("\x03\x00", 2)));
193   EXPECT_EQ(0x03u, DecodeValidUint32(2, string("\xff\x00", 2)));
194   EXPECT_EQ(0x01u, DecodeValidUint32(1, string("\x01\x00", 2)));
195   EXPECT_EQ(0x01u, DecodeValidUint32(1, string("\xff\x00", 2)));
196
197   // Maximums.
198   EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\x7f\x7f"));
199   EXPECT_EQ(0xfeu, DecodeValidUint32(7, "\xff\x7f"));
200   EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\x3f\x7f"));
201   EXPECT_EQ(0xbeu, DecodeValidUint32(6, "\xff\x7f"));
202   EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\x1f\x7f"));
203   EXPECT_EQ(0x9eu, DecodeValidUint32(5, "\xff\x7f"));
204   EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\x0f\x7f"));
205   EXPECT_EQ(0x8eu, DecodeValidUint32(4, "\xff\x7f"));
206   EXPECT_EQ(0x86u, DecodeValidUint32(3, "\x07\x7f"));
207   EXPECT_EQ(0x86u, DecodeValidUint32(3, "\xff\x7f"));
208   EXPECT_EQ(0x82u, DecodeValidUint32(2, "\x03\x7f"));
209   EXPECT_EQ(0x82u, DecodeValidUint32(2, "\xff\x7f"));
210   EXPECT_EQ(0x80u, DecodeValidUint32(1, "\x01\x7f"));
211   EXPECT_EQ(0x80u, DecodeValidUint32(1, "\xff\x7f"));
212
213   // Invalid.
214   ExpectDecodeUint32Invalid(7, "\x7f\x80");
215   ExpectDecodeUint32Invalid(7, "\xff\xff");
216   ExpectDecodeUint32Invalid(6, "\x3f\x80");
217   ExpectDecodeUint32Invalid(6, "\xff\xff");
218   ExpectDecodeUint32Invalid(5, "\x1f\x80");
219   ExpectDecodeUint32Invalid(5, "\xff\xff");
220   ExpectDecodeUint32Invalid(4, "\x0f\x80");
221   ExpectDecodeUint32Invalid(4, "\xff\xff");
222   ExpectDecodeUint32Invalid(3, "\x07\x80");
223   ExpectDecodeUint32Invalid(3, "\xff\xff");
224   ExpectDecodeUint32Invalid(2, "\x03\x80");
225   ExpectDecodeUint32Invalid(2, "\xff\xff");
226   ExpectDecodeUint32Invalid(1, "\x01\x80");
227   ExpectDecodeUint32Invalid(1, "\xff\xff");
228 }
229
230 TEST(HpackInputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) {
231   // Minimums.
232   EXPECT_EQ(0xffu, DecodeValidUint32(7, "\x7f\x80\x01"));
233   EXPECT_EQ(0xffu, DecodeValidUint32(7, "\xff\x80\x01"));
234   EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\x3f\x80\x01"));
235   EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\xff\x80\x01"));
236   EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\x1f\x80\x01"));
237   EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\xff\x80\x01"));
238   EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\x0f\x80\x01"));
239   EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\xff\x80\x01"));
240   EXPECT_EQ(0x87u, DecodeValidUint32(3, "\x07\x80\x01"));
241   EXPECT_EQ(0x87u, DecodeValidUint32(3, "\xff\x80\x01"));
242   EXPECT_EQ(0x83u, DecodeValidUint32(2, "\x03\x80\x01"));
243   EXPECT_EQ(0x83u, DecodeValidUint32(2, "\xff\x80\x01"));
244   EXPECT_EQ(0x81u, DecodeValidUint32(1, "\x01\x80\x01"));
245   EXPECT_EQ(0x81u, DecodeValidUint32(1, "\xff\x80\x01"));
246
247   // Maximums.
248   EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\x7f\xff\x7f"));
249   EXPECT_EQ(0x407eu, DecodeValidUint32(7, "\xff\xff\x7f"));
250   EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\x3f\xff\x7f"));
251   EXPECT_EQ(0x403eu, DecodeValidUint32(6, "\xff\xff\x7f"));
252   EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\x1f\xff\x7f"));
253   EXPECT_EQ(0x401eu, DecodeValidUint32(5, "\xff\xff\x7f"));
254   EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\x0f\xff\x7f"));
255   EXPECT_EQ(0x400eu, DecodeValidUint32(4, "\xff\xff\x7f"));
256   EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\x07\xff\x7f"));
257   EXPECT_EQ(0x4006u, DecodeValidUint32(3, "\xff\xff\x7f"));
258   EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\x03\xff\x7f"));
259   EXPECT_EQ(0x4002u, DecodeValidUint32(2, "\xff\xff\x7f"));
260   EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\x01\xff\x7f"));
261   EXPECT_EQ(0x4000u, DecodeValidUint32(1, "\xff\xff\x7f"));
262
263   // Invalid.
264   ExpectDecodeUint32Invalid(7, "\x7f\xff\x80");
265   ExpectDecodeUint32Invalid(7, "\xff\xff\xff");
266   ExpectDecodeUint32Invalid(6, "\x3f\xff\x80");
267   ExpectDecodeUint32Invalid(6, "\xff\xff\xff");
268   ExpectDecodeUint32Invalid(5, "\x1f\xff\x80");
269   ExpectDecodeUint32Invalid(5, "\xff\xff\xff");
270   ExpectDecodeUint32Invalid(4, "\x0f\xff\x80");
271   ExpectDecodeUint32Invalid(4, "\xff\xff\xff");
272   ExpectDecodeUint32Invalid(3, "\x07\xff\x80");
273   ExpectDecodeUint32Invalid(3, "\xff\xff\xff");
274   ExpectDecodeUint32Invalid(2, "\x03\xff\x80");
275   ExpectDecodeUint32Invalid(2, "\xff\xff\xff");
276   ExpectDecodeUint32Invalid(1, "\x01\xff\x80");
277   ExpectDecodeUint32Invalid(1, "\xff\xff\xff");
278 }
279
280 TEST(HpackInputStreamTest, FourByteIntegersOneToSevenBitPrefixes) {
281   // Minimums.
282   EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\x7f\x80\x80\x01"));
283   EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\xff\x80\x80\x01"));
284   EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\x3f\x80\x80\x01"));
285   EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\xff\x80\x80\x01"));
286   EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\x1f\x80\x80\x01"));
287   EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\xff\x80\x80\x01"));
288   EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\x0f\x80\x80\x01"));
289   EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\xff\x80\x80\x01"));
290   EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\x07\x80\x80\x01"));
291   EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\xff\x80\x80\x01"));
292   EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\x03\x80\x80\x01"));
293   EXPECT_EQ(0x4003u, DecodeValidUint32(2, "\xff\x80\x80\x01"));
294   EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\x01\x80\x80\x01"));
295   EXPECT_EQ(0x4001u, DecodeValidUint32(1, "\xff\x80\x80\x01"));
296
297   // Maximums.
298   EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\x7f\xff\xff\x7f"));
299   EXPECT_EQ(0x20007eu, DecodeValidUint32(7, "\xff\xff\xff\x7f"));
300   EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\x3f\xff\xff\x7f"));
301   EXPECT_EQ(0x20003eu, DecodeValidUint32(6, "\xff\xff\xff\x7f"));
302   EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\x1f\xff\xff\x7f"));
303   EXPECT_EQ(0x20001eu, DecodeValidUint32(5, "\xff\xff\xff\x7f"));
304   EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\x0f\xff\xff\x7f"));
305   EXPECT_EQ(0x20000eu, DecodeValidUint32(4, "\xff\xff\xff\x7f"));
306   EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\x07\xff\xff\x7f"));
307   EXPECT_EQ(0x200006u, DecodeValidUint32(3, "\xff\xff\xff\x7f"));
308   EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\x03\xff\xff\x7f"));
309   EXPECT_EQ(0x200002u, DecodeValidUint32(2, "\xff\xff\xff\x7f"));
310   EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\x01\xff\xff\x7f"));
311   EXPECT_EQ(0x200000u, DecodeValidUint32(1, "\xff\xff\xff\x7f"));
312
313   // Invalid.
314   ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\x80");
315   ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff");
316   ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\x80");
317   ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff");
318   ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\x80");
319   ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff");
320   ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80");
321   ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff");
322   ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80");
323   ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff");
324   ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80");
325   ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff");
326   ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80");
327   ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff");
328 }
329
330 TEST(HpackInputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) {
331   // Minimums.
332   EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01"));
333   EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x01"));
334   EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01"));
335   EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x01"));
336   EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01"));
337   EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x01"));
338   EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01"));
339   EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x01"));
340   EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x01"));
341   EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x01"));
342   EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x01"));
343   EXPECT_EQ(0x200003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x01"));
344   EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x01"));
345   EXPECT_EQ(0x200001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x01"));
346
347   // Maximums.
348   EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\x7f\xff\xff\xff\x7f"));
349   EXPECT_EQ(0x1000007eu, DecodeValidUint32(7, "\xff\xff\xff\xff\x7f"));
350   EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\x3f\xff\xff\xff\x7f"));
351   EXPECT_EQ(0x1000003eu, DecodeValidUint32(6, "\xff\xff\xff\xff\x7f"));
352   EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\x1f\xff\xff\xff\x7f"));
353   EXPECT_EQ(0x1000001eu, DecodeValidUint32(5, "\xff\xff\xff\xff\x7f"));
354   EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\x0f\xff\xff\xff\x7f"));
355   EXPECT_EQ(0x1000000eu, DecodeValidUint32(4, "\xff\xff\xff\xff\x7f"));
356   EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\x07\xff\xff\xff\x7f"));
357   EXPECT_EQ(0x10000006u, DecodeValidUint32(3, "\xff\xff\xff\xff\x7f"));
358   EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\x03\xff\xff\xff\x7f"));
359   EXPECT_EQ(0x10000002u, DecodeValidUint32(2, "\xff\xff\xff\xff\x7f"));
360   EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\x01\xff\xff\xff\x7f"));
361   EXPECT_EQ(0x10000000u, DecodeValidUint32(1, "\xff\xff\xff\xff\x7f"));
362
363   // Invalid.
364   ExpectDecodeUint32Invalid(7, "\x7f\xff\xff\xff\x80");
365   ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff");
366   ExpectDecodeUint32Invalid(6, "\x3f\xff\xff\xff\x80");
367   ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff");
368   ExpectDecodeUint32Invalid(5, "\x1f\xff\xff\xff\x80");
369   ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff");
370   ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80");
371   ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff");
372   ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80");
373   ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff");
374   ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80");
375   ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff");
376   ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80");
377   ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff");
378 }
379
380 TEST(HpackInputStreamTest, SixByteIntegersOneToSevenBitPrefixes) {
381   // Minimums.
382   EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01"));
383   EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01"));
384   EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01"));
385   EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01"));
386   EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01"));
387   EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01"));
388   EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01"));
389   EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01"));
390   EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01"));
391   EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\xff\x80\x80\x80\x80\x01"));
392   EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\x03\x80\x80\x80\x80\x01"));
393   EXPECT_EQ(0x10000003u, DecodeValidUint32(2, "\xff\x80\x80\x80\x80\x01"));
394   EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\x01\x80\x80\x80\x80\x01"));
395   EXPECT_EQ(0x10000001u, DecodeValidUint32(1, "\xff\x80\x80\x80\x80\x01"));
396
397   // Maximums.
398   EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\x7f\x80\xff\xff\xff\x0f"));
399   EXPECT_EQ(0xffffffffu, DecodeValidUint32(7, "\xff\x80\xff\xff\xff\x0f"));
400   EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\x3f\xc0\xff\xff\xff\x0f"));
401   EXPECT_EQ(0xffffffffu, DecodeValidUint32(6, "\xff\xc0\xff\xff\xff\x0f"));
402   EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\x1f\xe0\xff\xff\xff\x0f"));
403   EXPECT_EQ(0xffffffffu, DecodeValidUint32(5, "\xff\xe0\xff\xff\xff\x0f"));
404   EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\x0f\xf0\xff\xff\xff\x0f"));
405   EXPECT_EQ(0xffffffffu, DecodeValidUint32(4, "\xff\xf0\xff\xff\xff\x0f"));
406   EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\x07\xf8\xff\xff\xff\x0f"));
407   EXPECT_EQ(0xffffffffu, DecodeValidUint32(3, "\xff\xf8\xff\xff\xff\x0f"));
408   EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\x03\xfc\xff\xff\xff\x0f"));
409   EXPECT_EQ(0xffffffffu, DecodeValidUint32(2, "\xff\xfc\xff\xff\xff\x0f"));
410   EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\x01\xfe\xff\xff\xff\x0f"));
411   EXPECT_EQ(0xffffffffu, DecodeValidUint32(1, "\xff\xfe\xff\xff\xff\x0f"));
412
413   // Invalid.
414   ExpectDecodeUint32Invalid(7, "\x7f\x80\xff\xff\xff\x10");
415   ExpectDecodeUint32Invalid(7, "\xff\x80\xff\xff\xff\xff");
416   ExpectDecodeUint32Invalid(6, "\x3f\xc0\xff\xff\xff\x10");
417   ExpectDecodeUint32Invalid(6, "\xff\xc0\xff\xff\xff\xff");
418   ExpectDecodeUint32Invalid(5, "\x1f\xe0\xff\xff\xff\x10");
419   ExpectDecodeUint32Invalid(5, "\xff\xe0\xff\xff\xff\xff");
420   ExpectDecodeUint32Invalid(4, "\x0f\xf0\xff\xff\xff\x10");
421   ExpectDecodeUint32Invalid(4, "\xff\xf0\xff\xff\xff\xff");
422   ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10");
423   ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff");
424   ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10");
425   ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff");
426   ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10");
427   ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff");
428 }
429
430 // There are no valid uint32 encodings that are greater than six
431 // bytes.
432 TEST(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) {
433   ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00");
434   ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01");
435   ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff");
436   ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00");
437   ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01");
438   ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff");
439   ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00");
440   ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01");
441   ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff");
442   ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00");
443   ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01");
444   ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff");
445   ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00");
446   ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01");
447   ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff");
448   ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00");
449   ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01");
450   ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff");
451   ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00");
452   ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01");
453   ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff");
454 }
455
456 // Decoding a valid encoded string literal should work.
457 TEST(HpackInputStreamTest, DecodeNextStringLiteral) {
458   HpackInputStream input_stream(kuint32max, "\x0estring literal");
459
460   EXPECT_TRUE(input_stream.HasMoreData());
461   StringPiece string_piece;
462   EXPECT_TRUE(input_stream.DecodeNextStringLiteralForTest(&string_piece));
463   EXPECT_EQ("string literal", string_piece);
464   EXPECT_FALSE(input_stream.HasMoreData());
465 }
466
467 // Decoding an encoded string literal with size larger than
468 // |max_string_literal_size_| should fail.
469 TEST(HpackInputStreamTest, DecodeNextStringLiteralSizeLimit) {
470   HpackInputStream input_stream(13, "\x0estring literal");
471
472   EXPECT_TRUE(input_stream.HasMoreData());
473   StringPiece string_piece;
474   EXPECT_FALSE(input_stream.DecodeNextStringLiteralForTest(&string_piece));
475 }
476
477 // Decoding an encoded string literal with size larger than the
478 // remainder of the buffer should fail.
479 TEST(HpackInputStreamTest, DecodeNextStringLiteralInvalidSize) {
480   // Set the length to be one more than it should be.
481   HpackInputStream input_stream(kuint32max, "\x0fstring literal");
482
483   EXPECT_TRUE(input_stream.HasMoreData());
484   StringPiece string_piece;
485   EXPECT_FALSE(input_stream.DecodeNextStringLiteralForTest(&string_piece));
486 }
487
488 }  // namespace
489
490 }  // namespace net