OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/hpack/hpack_input_stream.h" | 5 #include "net/spdy/hpack/hpack_input_stream.h" |
6 | 6 |
7 #include <bitset> | 7 #include <bitset> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "net/spdy/hpack/hpack_constants.h" | 13 #include "net/spdy/hpack/hpack_constants.h" |
14 #include "net/spdy/spdy_test_utils.h" | 14 #include "net/spdy/spdy_test_utils.h" |
| 15 #include "net/test/gtest_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 using base::StringPiece; | 22 using base::StringPiece; |
22 using std::string; | 23 using std::string; |
23 using test::a2b_hex; | 24 using test::a2b_hex; |
24 | 25 |
25 const size_t kLiteralBound = 1024; | 26 const size_t kLiteralBound = 1024; |
26 | 27 |
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())); | |
32 } | |
33 | |
34 protected: | |
35 HpackHuffmanTable huffman_table_; | |
36 }; | |
37 | |
38 // Hex representation of encoded length and Huffman string. | 28 // Hex representation of encoded length and Huffman string. |
39 const char kEncodedHuffmanFixture[] = | 29 const char kEncodedHuffmanFixture[] = |
40 "2d" // Length prefix. | 30 "2d" // Length prefix. |
41 "94e7821dd7f2e6c7b335dfdfcd5b3960" | 31 "94e7821dd7f2e6c7b335dfdfcd5b3960" |
42 "d5af27087f3672c1ab270fb5291f9587" | 32 "d5af27087f3672c1ab270fb5291f9587" |
43 "316065c003ed4ee5b1063d5007"; | 33 "316065c003ed4ee5b1063d5007"; |
44 | 34 |
45 const char kDecodedHuffmanFixture[] = | 35 const char kDecodedHuffmanFixture[] = |
46 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; | 36 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"; |
47 | 37 |
(...skipping 21 matching lines...) Expand all Loading... |
69 } | 59 } |
70 | 60 |
71 uint32_t bits32(const string& bitstring) { | 61 uint32_t bits32(const string& bitstring) { |
72 return std::bitset<32>(bitstring).to_ulong(); | 62 return std::bitset<32>(bitstring).to_ulong(); |
73 } | 63 } |
74 | 64 |
75 // The {Number}ByteIntegersEightBitPrefix tests below test that | 65 // The {Number}ByteIntegersEightBitPrefix tests below test that |
76 // certain integers are decoded correctly with an 8-bit prefix in | 66 // certain integers are decoded correctly with an 8-bit prefix in |
77 // exactly {Number} bytes. | 67 // exactly {Number} bytes. |
78 | 68 |
79 TEST_F(HpackInputStreamTest, OneByteIntegersEightBitPrefix) { | 69 TEST(HpackInputStreamTest, OneByteIntegersEightBitPrefix) { |
80 // Minimum. | 70 // Minimum. |
81 EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1))); | 71 EXPECT_EQ(0x00u, DecodeValidUint32(8, string("\x00", 1))); |
82 EXPECT_EQ(0x7fu, DecodeValidUint32(8, "\x7f")); | 72 EXPECT_EQ(0x7fu, DecodeValidUint32(8, "\x7f")); |
83 // Maximum. | 73 // Maximum. |
84 EXPECT_EQ(0xfeu, DecodeValidUint32(8, "\xfe")); | 74 EXPECT_EQ(0xfeu, DecodeValidUint32(8, "\xfe")); |
85 // Invalid. | 75 // Invalid. |
86 ExpectDecodeUint32Invalid(8, "\xff"); | 76 ExpectDecodeUint32Invalid(8, "\xff"); |
87 } | 77 } |
88 | 78 |
89 TEST_F(HpackInputStreamTest, TwoByteIntegersEightBitPrefix) { | 79 TEST(HpackInputStreamTest, TwoByteIntegersEightBitPrefix) { |
90 // Minimum. | 80 // Minimum. |
91 EXPECT_EQ(0xffu, DecodeValidUint32(8, string("\xff\x00", 2))); | 81 EXPECT_EQ(0xffu, DecodeValidUint32(8, string("\xff\x00", 2))); |
92 EXPECT_EQ(0x0100u, DecodeValidUint32(8, "\xff\x01")); | 82 EXPECT_EQ(0x0100u, DecodeValidUint32(8, "\xff\x01")); |
93 // Maximum. | 83 // Maximum. |
94 EXPECT_EQ(0x017eu, DecodeValidUint32(8, "\xff\x7f")); | 84 EXPECT_EQ(0x017eu, DecodeValidUint32(8, "\xff\x7f")); |
95 // Invalid. | 85 // Invalid. |
96 ExpectDecodeUint32Invalid(8, "\xff\x80"); | 86 ExpectDecodeUint32Invalid(8, "\xff\x80"); |
97 ExpectDecodeUint32Invalid(8, "\xff\xff"); | 87 ExpectDecodeUint32Invalid(8, "\xff\xff"); |
98 } | 88 } |
99 | 89 |
100 TEST_F(HpackInputStreamTest, ThreeByteIntegersEightBitPrefix) { | 90 TEST(HpackInputStreamTest, ThreeByteIntegersEightBitPrefix) { |
101 // Minimum. | 91 // Minimum. |
102 EXPECT_EQ(0x017fu, DecodeValidUint32(8, "\xff\x80\x01")); | 92 EXPECT_EQ(0x017fu, DecodeValidUint32(8, "\xff\x80\x01")); |
103 EXPECT_EQ(0x0fffu, DecodeValidUint32(8, "\xff\x80\x1e")); | 93 EXPECT_EQ(0x0fffu, DecodeValidUint32(8, "\xff\x80\x1e")); |
104 // Maximum. | 94 // Maximum. |
105 EXPECT_EQ(0x40feu, DecodeValidUint32(8, "\xff\xff\x7f")); | 95 EXPECT_EQ(0x40feu, DecodeValidUint32(8, "\xff\xff\x7f")); |
106 // Invalid. | 96 // Invalid. |
107 ExpectDecodeUint32Invalid(8, "\xff\x80\x00"); | 97 ExpectDecodeUint32Invalid(8, "\xff\x80\x00"); |
108 ExpectDecodeUint32Invalid(8, "\xff\xff\x00"); | 98 ExpectDecodeUint32Invalid(8, "\xff\xff\x00"); |
109 ExpectDecodeUint32Invalid(8, "\xff\xff\x80"); | 99 ExpectDecodeUint32Invalid(8, "\xff\xff\x80"); |
110 ExpectDecodeUint32Invalid(8, "\xff\xff\xff"); | 100 ExpectDecodeUint32Invalid(8, "\xff\xff\xff"); |
111 } | 101 } |
112 | 102 |
113 TEST_F(HpackInputStreamTest, FourByteIntegersEightBitPrefix) { | 103 TEST(HpackInputStreamTest, FourByteIntegersEightBitPrefix) { |
114 // Minimum. | 104 // Minimum. |
115 EXPECT_EQ(0x40ffu, DecodeValidUint32(8, "\xff\x80\x80\x01")); | 105 EXPECT_EQ(0x40ffu, DecodeValidUint32(8, "\xff\x80\x80\x01")); |
116 EXPECT_EQ(0xffffu, DecodeValidUint32(8, "\xff\x80\xfe\x03")); | 106 EXPECT_EQ(0xffffu, DecodeValidUint32(8, "\xff\x80\xfe\x03")); |
117 // Maximum. | 107 // Maximum. |
118 EXPECT_EQ(0x002000feu, DecodeValidUint32(8, "\xff\xff\xff\x7f")); | 108 EXPECT_EQ(0x002000feu, DecodeValidUint32(8, "\xff\xff\xff\x7f")); |
119 // Invalid. | 109 // Invalid. |
120 ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00"); | 110 ExpectDecodeUint32Invalid(8, "\xff\xff\x80\x00"); |
121 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00"); | 111 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x00"); |
122 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80"); | 112 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80"); |
123 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff"); | 113 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff"); |
124 } | 114 } |
125 | 115 |
126 TEST_F(HpackInputStreamTest, FiveByteIntegersEightBitPrefix) { | 116 TEST(HpackInputStreamTest, FiveByteIntegersEightBitPrefix) { |
127 // Minimum. | 117 // Minimum. |
128 EXPECT_EQ(0x002000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x01")); | 118 EXPECT_EQ(0x002000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x01")); |
129 EXPECT_EQ(0x00ffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07")); | 119 EXPECT_EQ(0x00ffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\x07")); |
130 // Maximum. | 120 // Maximum. |
131 EXPECT_EQ(0x100000feu, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f")); | 121 EXPECT_EQ(0x100000feu, DecodeValidUint32(8, "\xff\xff\xff\xff\x7f")); |
132 // Invalid. | 122 // Invalid. |
133 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00"); | 123 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\x80\x00"); |
134 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00"); | 124 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x00"); |
135 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80"); | 125 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\x80"); |
136 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff"); | 126 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff"); |
137 } | 127 } |
138 | 128 |
139 TEST_F(HpackInputStreamTest, SixByteIntegersEightBitPrefix) { | 129 TEST(HpackInputStreamTest, SixByteIntegersEightBitPrefix) { |
140 // Minimum. | 130 // Minimum. |
141 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01")); | 131 EXPECT_EQ(0x100000ffu, DecodeValidUint32(8, "\xff\x80\x80\x80\x80\x01")); |
142 // Maximum. | 132 // Maximum. |
143 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f")); | 133 EXPECT_EQ(0xffffffffu, DecodeValidUint32(8, "\xff\x80\xfe\xff\xff\x0f")); |
144 // Invalid. | 134 // Invalid. |
145 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00"); | 135 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x00"); |
146 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10"); | 136 ExpectDecodeUint32Invalid(8, "\xff\x80\xfe\xff\xff\x10"); |
147 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff"); | 137 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff"); |
148 } | 138 } |
149 | 139 |
150 // There are no valid uint32_t encodings that are greater than six | 140 // There are no valid uint32_t encodings that are greater than six |
151 // bytes. | 141 // bytes. |
152 TEST_F(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) { | 142 TEST(HpackInputStreamTest, SevenByteIntegersEightBitPrefix) { |
153 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00"); | 143 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x00"); |
154 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01"); | 144 ExpectDecodeUint32Invalid(8, "\xff\x80\x80\x80\x80\x80\x01"); |
155 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff"); | 145 ExpectDecodeUint32Invalid(8, "\xff\xff\xff\xff\xff\xff\xff"); |
156 } | 146 } |
157 | 147 |
158 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that | 148 // The {Number}ByteIntegersOneToSevenBitPrefix tests below test that |
159 // certain integers are encoded correctly with an N-bit prefix in | 149 // certain integers are encoded correctly with an N-bit prefix in |
160 // exactly {Number} bytes for N in {1, 2, ..., 7}. | 150 // exactly {Number} bytes for N in {1, 2, ..., 7}. |
161 | 151 |
162 TEST_F(HpackInputStreamTest, OneByteIntegersOneToSevenBitPrefixes) { | 152 TEST(HpackInputStreamTest, OneByteIntegersOneToSevenBitPrefixes) { |
163 // Minimums. | 153 // Minimums. |
164 EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x00", 1))); | 154 EXPECT_EQ(0x00u, DecodeValidUint32(7, string("\x00", 1))); |
165 EXPECT_EQ(0x00u, DecodeValidUint32(7, "\x80")); | 155 EXPECT_EQ(0x00u, DecodeValidUint32(7, "\x80")); |
166 EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\x00", 1))); | 156 EXPECT_EQ(0x00u, DecodeValidUint32(6, string("\x00", 1))); |
167 EXPECT_EQ(0x00u, DecodeValidUint32(6, "\xc0")); | 157 EXPECT_EQ(0x00u, DecodeValidUint32(6, "\xc0")); |
168 EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\x00", 1))); | 158 EXPECT_EQ(0x00u, DecodeValidUint32(5, string("\x00", 1))); |
169 EXPECT_EQ(0x00u, DecodeValidUint32(5, "\xe0")); | 159 EXPECT_EQ(0x00u, DecodeValidUint32(5, "\xe0")); |
170 EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\x00", 1))); | 160 EXPECT_EQ(0x00u, DecodeValidUint32(4, string("\x00", 1))); |
171 EXPECT_EQ(0x00u, DecodeValidUint32(4, "\xf0")); | 161 EXPECT_EQ(0x00u, DecodeValidUint32(4, "\xf0")); |
172 EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\x00", 1))); | 162 EXPECT_EQ(0x00u, DecodeValidUint32(3, string("\x00", 1))); |
(...skipping 29 matching lines...) Expand all Loading... |
202 ExpectDecodeUint32Invalid(4, "\x0f"); | 192 ExpectDecodeUint32Invalid(4, "\x0f"); |
203 ExpectDecodeUint32Invalid(4, "\xff"); | 193 ExpectDecodeUint32Invalid(4, "\xff"); |
204 ExpectDecodeUint32Invalid(3, "\x07"); | 194 ExpectDecodeUint32Invalid(3, "\x07"); |
205 ExpectDecodeUint32Invalid(3, "\xff"); | 195 ExpectDecodeUint32Invalid(3, "\xff"); |
206 ExpectDecodeUint32Invalid(2, "\x03"); | 196 ExpectDecodeUint32Invalid(2, "\x03"); |
207 ExpectDecodeUint32Invalid(2, "\xff"); | 197 ExpectDecodeUint32Invalid(2, "\xff"); |
208 ExpectDecodeUint32Invalid(1, "\x01"); | 198 ExpectDecodeUint32Invalid(1, "\x01"); |
209 ExpectDecodeUint32Invalid(1, "\xff"); | 199 ExpectDecodeUint32Invalid(1, "\xff"); |
210 } | 200 } |
211 | 201 |
212 TEST_F(HpackInputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) { | 202 TEST(HpackInputStreamTest, TwoByteIntegersOneToSevenBitPrefixes) { |
213 // Minimums. | 203 // Minimums. |
214 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\x7f\x00", 2))); | 204 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\x7f\x00", 2))); |
215 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\xff\x00", 2))); | 205 EXPECT_EQ(0x7fu, DecodeValidUint32(7, string("\xff\x00", 2))); |
216 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\x3f\x00", 2))); | 206 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\x3f\x00", 2))); |
217 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\xff\x00", 2))); | 207 EXPECT_EQ(0x3fu, DecodeValidUint32(6, string("\xff\x00", 2))); |
218 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\x1f\x00", 2))); | 208 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\x1f\x00", 2))); |
219 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\xff\x00", 2))); | 209 EXPECT_EQ(0x1fu, DecodeValidUint32(5, string("\xff\x00", 2))); |
220 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\x0f\x00", 2))); | 210 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\x0f\x00", 2))); |
221 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\xff\x00", 2))); | 211 EXPECT_EQ(0x0fu, DecodeValidUint32(4, string("\xff\x00", 2))); |
222 EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\x07\x00", 2))); | 212 EXPECT_EQ(0x07u, DecodeValidUint32(3, string("\x07\x00", 2))); |
(...skipping 29 matching lines...) Expand all Loading... |
252 ExpectDecodeUint32Invalid(4, "\x0f\x80"); | 242 ExpectDecodeUint32Invalid(4, "\x0f\x80"); |
253 ExpectDecodeUint32Invalid(4, "\xff\xff"); | 243 ExpectDecodeUint32Invalid(4, "\xff\xff"); |
254 ExpectDecodeUint32Invalid(3, "\x07\x80"); | 244 ExpectDecodeUint32Invalid(3, "\x07\x80"); |
255 ExpectDecodeUint32Invalid(3, "\xff\xff"); | 245 ExpectDecodeUint32Invalid(3, "\xff\xff"); |
256 ExpectDecodeUint32Invalid(2, "\x03\x80"); | 246 ExpectDecodeUint32Invalid(2, "\x03\x80"); |
257 ExpectDecodeUint32Invalid(2, "\xff\xff"); | 247 ExpectDecodeUint32Invalid(2, "\xff\xff"); |
258 ExpectDecodeUint32Invalid(1, "\x01\x80"); | 248 ExpectDecodeUint32Invalid(1, "\x01\x80"); |
259 ExpectDecodeUint32Invalid(1, "\xff\xff"); | 249 ExpectDecodeUint32Invalid(1, "\xff\xff"); |
260 } | 250 } |
261 | 251 |
262 TEST_F(HpackInputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) { | 252 TEST(HpackInputStreamTest, ThreeByteIntegersOneToSevenBitPrefixes) { |
263 // Minimums. | 253 // Minimums. |
264 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\x7f\x80\x01")); | 254 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\x7f\x80\x01")); |
265 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\xff\x80\x01")); | 255 EXPECT_EQ(0xffu, DecodeValidUint32(7, "\xff\x80\x01")); |
266 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\x3f\x80\x01")); | 256 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\x3f\x80\x01")); |
267 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\xff\x80\x01")); | 257 EXPECT_EQ(0xbfu, DecodeValidUint32(6, "\xff\x80\x01")); |
268 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\x1f\x80\x01")); | 258 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\x1f\x80\x01")); |
269 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\xff\x80\x01")); | 259 EXPECT_EQ(0x9fu, DecodeValidUint32(5, "\xff\x80\x01")); |
270 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\x0f\x80\x01")); | 260 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\x0f\x80\x01")); |
271 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\xff\x80\x01")); | 261 EXPECT_EQ(0x8fu, DecodeValidUint32(4, "\xff\x80\x01")); |
272 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\x07\x80\x01")); | 262 EXPECT_EQ(0x87u, DecodeValidUint32(3, "\x07\x80\x01")); |
(...skipping 29 matching lines...) Expand all Loading... |
302 ExpectDecodeUint32Invalid(4, "\x0f\xff\x80"); | 292 ExpectDecodeUint32Invalid(4, "\x0f\xff\x80"); |
303 ExpectDecodeUint32Invalid(4, "\xff\xff\xff"); | 293 ExpectDecodeUint32Invalid(4, "\xff\xff\xff"); |
304 ExpectDecodeUint32Invalid(3, "\x07\xff\x80"); | 294 ExpectDecodeUint32Invalid(3, "\x07\xff\x80"); |
305 ExpectDecodeUint32Invalid(3, "\xff\xff\xff"); | 295 ExpectDecodeUint32Invalid(3, "\xff\xff\xff"); |
306 ExpectDecodeUint32Invalid(2, "\x03\xff\x80"); | 296 ExpectDecodeUint32Invalid(2, "\x03\xff\x80"); |
307 ExpectDecodeUint32Invalid(2, "\xff\xff\xff"); | 297 ExpectDecodeUint32Invalid(2, "\xff\xff\xff"); |
308 ExpectDecodeUint32Invalid(1, "\x01\xff\x80"); | 298 ExpectDecodeUint32Invalid(1, "\x01\xff\x80"); |
309 ExpectDecodeUint32Invalid(1, "\xff\xff\xff"); | 299 ExpectDecodeUint32Invalid(1, "\xff\xff\xff"); |
310 } | 300 } |
311 | 301 |
312 TEST_F(HpackInputStreamTest, FourByteIntegersOneToSevenBitPrefixes) { | 302 TEST(HpackInputStreamTest, FourByteIntegersOneToSevenBitPrefixes) { |
313 // Minimums. | 303 // Minimums. |
314 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\x7f\x80\x80\x01")); | 304 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\x7f\x80\x80\x01")); |
315 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\xff\x80\x80\x01")); | 305 EXPECT_EQ(0x407fu, DecodeValidUint32(7, "\xff\x80\x80\x01")); |
316 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\x3f\x80\x80\x01")); | 306 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\x3f\x80\x80\x01")); |
317 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\xff\x80\x80\x01")); | 307 EXPECT_EQ(0x403fu, DecodeValidUint32(6, "\xff\x80\x80\x01")); |
318 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\x1f\x80\x80\x01")); | 308 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\x1f\x80\x80\x01")); |
319 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\xff\x80\x80\x01")); | 309 EXPECT_EQ(0x401fu, DecodeValidUint32(5, "\xff\x80\x80\x01")); |
320 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\x0f\x80\x80\x01")); | 310 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\x0f\x80\x80\x01")); |
321 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\xff\x80\x80\x01")); | 311 EXPECT_EQ(0x400fu, DecodeValidUint32(4, "\xff\x80\x80\x01")); |
322 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\x07\x80\x80\x01")); | 312 EXPECT_EQ(0x4007u, DecodeValidUint32(3, "\x07\x80\x80\x01")); |
(...skipping 29 matching lines...) Expand all Loading... |
352 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80"); | 342 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\x80"); |
353 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff"); | 343 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff"); |
354 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80"); | 344 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\x80"); |
355 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff"); | 345 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff"); |
356 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80"); | 346 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\x80"); |
357 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff"); | 347 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff"); |
358 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80"); | 348 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\x80"); |
359 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff"); | 349 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff"); |
360 } | 350 } |
361 | 351 |
362 TEST_F(HpackInputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) { | 352 TEST(HpackInputStreamTest, FiveByteIntegersOneToSevenBitPrefixes) { |
363 // Minimums. | 353 // Minimums. |
364 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01")); | 354 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x01")); |
365 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x01")); | 355 EXPECT_EQ(0x20007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x01")); |
366 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01")); | 356 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x01")); |
367 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x01")); | 357 EXPECT_EQ(0x20003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x01")); |
368 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01")); | 358 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x01")); |
369 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x01")); | 359 EXPECT_EQ(0x20001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x01")); |
370 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01")); | 360 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x01")); |
371 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x01")); | 361 EXPECT_EQ(0x20000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x01")); |
372 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x01")); | 362 EXPECT_EQ(0x200007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x01")); |
(...skipping 29 matching lines...) Expand all Loading... |
402 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80"); | 392 ExpectDecodeUint32Invalid(4, "\x0f\xff\xff\xff\x80"); |
403 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff"); | 393 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff"); |
404 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80"); | 394 ExpectDecodeUint32Invalid(3, "\x07\xff\xff\xff\x80"); |
405 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff"); | 395 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff"); |
406 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80"); | 396 ExpectDecodeUint32Invalid(2, "\x03\xff\xff\xff\x80"); |
407 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff"); | 397 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff"); |
408 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80"); | 398 ExpectDecodeUint32Invalid(1, "\x01\xff\xff\xff\x80"); |
409 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff"); | 399 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff"); |
410 } | 400 } |
411 | 401 |
412 TEST_F(HpackInputStreamTest, SixByteIntegersOneToSevenBitPrefixes) { | 402 TEST(HpackInputStreamTest, SixByteIntegersOneToSevenBitPrefixes) { |
413 // Minimums. | 403 // Minimums. |
414 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01")); | 404 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\x7f\x80\x80\x80\x80\x01")); |
415 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01")); | 405 EXPECT_EQ(0x1000007fu, DecodeValidUint32(7, "\xff\x80\x80\x80\x80\x01")); |
416 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01")); | 406 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\x3f\x80\x80\x80\x80\x01")); |
417 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01")); | 407 EXPECT_EQ(0x1000003fu, DecodeValidUint32(6, "\xff\x80\x80\x80\x80\x01")); |
418 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01")); | 408 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\x1f\x80\x80\x80\x80\x01")); |
419 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01")); | 409 EXPECT_EQ(0x1000001fu, DecodeValidUint32(5, "\xff\x80\x80\x80\x80\x01")); |
420 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01")); | 410 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\x0f\x80\x80\x80\x80\x01")); |
421 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01")); | 411 EXPECT_EQ(0x1000000fu, DecodeValidUint32(4, "\xff\x80\x80\x80\x80\x01")); |
422 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01")); | 412 EXPECT_EQ(0x10000007u, DecodeValidUint32(3, "\x07\x80\x80\x80\x80\x01")); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10"); | 444 ExpectDecodeUint32Invalid(3, "\x07\xf8\xff\xff\xff\x10"); |
455 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff"); | 445 ExpectDecodeUint32Invalid(3, "\xff\xf8\xff\xff\xff\xff"); |
456 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10"); | 446 ExpectDecodeUint32Invalid(2, "\x03\xfc\xff\xff\xff\x10"); |
457 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff"); | 447 ExpectDecodeUint32Invalid(2, "\xff\xfc\xff\xff\xff\xff"); |
458 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10"); | 448 ExpectDecodeUint32Invalid(1, "\x01\xfe\xff\xff\xff\x10"); |
459 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff"); | 449 ExpectDecodeUint32Invalid(1, "\xff\xfe\xff\xff\xff\xff"); |
460 } | 450 } |
461 | 451 |
462 // There are no valid uint32_t encodings that are greater than six | 452 // There are no valid uint32_t encodings that are greater than six |
463 // bytes. | 453 // bytes. |
464 TEST_F(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) { | 454 TEST(HpackInputStreamTest, SevenByteIntegersOneToSevenBitPrefixes) { |
465 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00"); | 455 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x00"); |
466 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01"); | 456 ExpectDecodeUint32Invalid(7, "\x7f\x80\x80\x80\x80\x80\x01"); |
467 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff"); | 457 ExpectDecodeUint32Invalid(7, "\xff\xff\xff\xff\xff\xff\xff"); |
468 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00"); | 458 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x00"); |
469 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01"); | 459 ExpectDecodeUint32Invalid(6, "\x3f\x80\x80\x80\x80\x80\x01"); |
470 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff"); | 460 ExpectDecodeUint32Invalid(6, "\xff\xff\xff\xff\xff\xff\xff"); |
471 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00"); | 461 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x00"); |
472 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01"); | 462 ExpectDecodeUint32Invalid(5, "\x1f\x80\x80\x80\x80\x80\x01"); |
473 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff"); | 463 ExpectDecodeUint32Invalid(5, "\xff\xff\xff\xff\xff\xff\xff"); |
474 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00"); | 464 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x00"); |
475 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01"); | 465 ExpectDecodeUint32Invalid(4, "\x0f\x80\x80\x80\x80\x80\x01"); |
476 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff"); | 466 ExpectDecodeUint32Invalid(4, "\xff\xff\xff\xff\xff\xff\xff"); |
477 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00"); | 467 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x00"); |
478 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01"); | 468 ExpectDecodeUint32Invalid(3, "\x07\x80\x80\x80\x80\x80\x01"); |
479 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff"); | 469 ExpectDecodeUint32Invalid(3, "\xff\xff\xff\xff\xff\xff\xff"); |
480 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00"); | 470 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x00"); |
481 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01"); | 471 ExpectDecodeUint32Invalid(2, "\x03\x80\x80\x80\x80\x80\x01"); |
482 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff"); | 472 ExpectDecodeUint32Invalid(2, "\xff\xff\xff\xff\xff\xff\xff"); |
483 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00"); | 473 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x00"); |
484 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01"); | 474 ExpectDecodeUint32Invalid(1, "\x01\x80\x80\x80\x80\x80\x01"); |
485 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff"); | 475 ExpectDecodeUint32Invalid(1, "\xff\xff\xff\xff\xff\xff\xff"); |
486 } | 476 } |
487 | 477 |
488 // Decoding a valid encoded string literal should work. | 478 // Decoding a valid encoded string literal should work. |
489 TEST_F(HpackInputStreamTest, DecodeNextIdentityString) { | 479 TEST(HpackInputStreamTest, DecodeNextIdentityString) { |
490 HpackInputStream input_stream(kLiteralBound, "\x0estring literal"); | 480 HpackInputStream input_stream(kLiteralBound, "\x0estring literal"); |
491 | 481 |
492 EXPECT_TRUE(input_stream.HasMoreData()); | 482 EXPECT_TRUE(input_stream.HasMoreData()); |
493 StringPiece string_piece; | 483 StringPiece string_piece; |
494 EXPECT_TRUE(input_stream.DecodeNextIdentityString(&string_piece)); | 484 EXPECT_TRUE(input_stream.DecodeNextIdentityString(&string_piece)); |
495 EXPECT_EQ("string literal", string_piece); | 485 EXPECT_EQ("string literal", string_piece); |
496 EXPECT_FALSE(input_stream.HasMoreData()); | 486 EXPECT_FALSE(input_stream.HasMoreData()); |
497 } | 487 } |
498 | 488 |
499 // Decoding an encoded string literal with size larger than | 489 // Decoding an encoded string literal with size larger than |
500 // |max_string_literal_size_| should fail. | 490 // |max_string_literal_size_| should fail. |
501 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringSizeLimit) { | 491 TEST(HpackInputStreamTest, DecodeNextIdentityStringSizeLimit) { |
502 HpackInputStream input_stream(13, "\x0estring literal"); | 492 HpackInputStream input_stream(13, "\x0estring literal"); |
503 | 493 |
504 EXPECT_TRUE(input_stream.HasMoreData()); | 494 EXPECT_TRUE(input_stream.HasMoreData()); |
505 StringPiece string_piece; | 495 StringPiece string_piece; |
506 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); | 496 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); |
507 } | 497 } |
508 | 498 |
509 // Decoding an encoded string literal with size larger than the | 499 // Decoding an encoded string literal with size larger than the |
510 // remainder of the buffer should fail. | 500 // remainder of the buffer should fail. |
511 TEST_F(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) { | 501 TEST(HpackInputStreamTest, DecodeNextIdentityStringNotEnoughInput) { |
512 // Set the length to be one more than it should be. | 502 // Set the length to be one more than it should be. |
513 HpackInputStream input_stream(kLiteralBound, "\x0fstring literal"); | 503 HpackInputStream input_stream(kLiteralBound, "\x0fstring literal"); |
514 | 504 |
515 EXPECT_TRUE(input_stream.HasMoreData()); | 505 EXPECT_TRUE(input_stream.HasMoreData()); |
516 StringPiece string_piece; | 506 StringPiece string_piece; |
517 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); | 507 EXPECT_FALSE(input_stream.DecodeNextIdentityString(&string_piece)); |
518 } | 508 } |
519 | 509 |
520 TEST_F(HpackInputStreamTest, DecodeNextHuffmanString) { | 510 TEST(HpackInputStreamTest, DecodeNextHuffmanString) { |
521 string output, input(a2b_hex(kEncodedHuffmanFixture)); | 511 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
522 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 1, input); | 512 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 1, input); |
523 | 513 |
524 EXPECT_TRUE(input_stream.HasMoreData()); | 514 EXPECT_TRUE(input_stream.HasMoreData()); |
525 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); | 515 EXPECT_TRUE(input_stream.DecodeNextHuffmanString(&output)); |
526 EXPECT_EQ(kDecodedHuffmanFixture, output); | 516 EXPECT_EQ(kDecodedHuffmanFixture, output); |
527 EXPECT_FALSE(input_stream.HasMoreData()); | 517 EXPECT_FALSE(input_stream.HasMoreData()); |
528 } | 518 } |
529 | 519 |
530 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) { | 520 TEST(HpackInputStreamTest, DecodeNextHuffmanStringSizeLimit) { |
531 string output, input(a2b_hex(kEncodedHuffmanFixture)); | 521 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
532 // Max string literal is one byte shorter than the decoded fixture. | 522 // Max string literal is one byte shorter than the decoded fixture. |
533 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 2, input); | 523 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 2, input); |
534 | 524 |
535 // Decoded string overflows the max string literal. | 525 // Decoded string overflows the max string literal. |
536 EXPECT_TRUE(input_stream.HasMoreData()); | 526 EXPECT_TRUE(input_stream.HasMoreData()); |
537 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); | 527 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(&output)); |
538 } | 528 } |
539 | 529 |
540 TEST_F(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { | 530 TEST(HpackInputStreamTest, DecodeNextHuffmanStringNotEnoughInput) { |
541 string output, input(a2b_hex(kEncodedHuffmanFixture)); | 531 string output, input(a2b_hex(kEncodedHuffmanFixture)); |
542 input[0]++; // Input prefix is one byte larger than available input. | 532 input[0]++; // Input prefix is one byte larger than available input. |
543 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 1, input); | 533 HpackInputStream input_stream(arraysize(kDecodedHuffmanFixture) - 1, input); |
544 | 534 |
545 // Not enough buffer for declared encoded length. | 535 // Not enough buffer for declared encoded length. |
546 EXPECT_TRUE(input_stream.HasMoreData()); | 536 EXPECT_TRUE(input_stream.HasMoreData()); |
547 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(huffman_table_, &output)); | 537 EXPECT_FALSE(input_stream.DecodeNextHuffmanString(&output)); |
548 } | 538 } |
549 | 539 |
550 TEST_F(HpackInputStreamTest, PeekBitsAndConsume) { | 540 TEST(HpackInputStreamTest, PeekBitsAndConsume) { |
551 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); | 541 HpackInputStream input_stream(kLiteralBound, "\xad\xab\xad\xab\xad"); |
552 | 542 |
553 uint32_t bits = 0; | 543 uint32_t bits = 0; |
554 size_t peeked_count = 0; | 544 size_t peeked_count = 0; |
555 | 545 |
556 // Read 0xad. | 546 // Read 0xad. |
557 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); | 547 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); |
558 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); | 548 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); |
559 EXPECT_EQ(8u, peeked_count); | 549 EXPECT_EQ(8u, peeked_count); |
560 | 550 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 | 592 |
603 // EOF. | 593 // EOF. |
604 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits)); | 594 EXPECT_FALSE(input_stream.PeekBits(&peeked_count, &bits)); |
605 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); | 595 EXPECT_EQ(bits32("10101101000000000000000000000000"), bits); |
606 EXPECT_EQ(8u, peeked_count); | 596 EXPECT_EQ(8u, peeked_count); |
607 | 597 |
608 input_stream.ConsumeBits(8); | 598 input_stream.ConsumeBits(8); |
609 EXPECT_FALSE(input_stream.HasMoreData()); | 599 EXPECT_FALSE(input_stream.HasMoreData()); |
610 } | 600 } |
611 | 601 |
612 TEST_F(HpackInputStreamTest, ConsumeByteRemainder) { | 602 TEST(HpackInputStreamTest, InitializePeekBits) { |
| 603 { |
| 604 // Empty input, peeked_count == 0 and bits == 0. |
| 605 HpackInputStream input_stream(kLiteralBound, ""); |
| 606 auto peeked_count_and_bits = input_stream.InitializePeekBits(); |
| 607 size_t peeked_count = peeked_count_and_bits.first; |
| 608 uint32_t bits = peeked_count_and_bits.second; |
| 609 EXPECT_EQ(0u, peeked_count); |
| 610 EXPECT_EQ(0u, bits); |
| 611 } |
| 612 { |
| 613 // One input byte, returns peeked_count == 8 and bits |
| 614 // has the input byte in its high order bits. |
| 615 HpackInputStream input_stream(kLiteralBound, "\xfe"); |
| 616 auto peeked_count_and_bits = input_stream.InitializePeekBits(); |
| 617 size_t peeked_count = peeked_count_and_bits.first; |
| 618 uint32_t bits = peeked_count_and_bits.second; |
| 619 EXPECT_EQ(8u, peeked_count); |
| 620 EXPECT_EQ(0xfe000000, bits); |
| 621 input_stream.ConsumeBits(8); |
| 622 EXPECT_FALSE(input_stream.HasMoreData()); |
| 623 } |
| 624 { |
| 625 // Two input bytes, returns peeked_count == 16 and bits |
| 626 // has the two input bytes in its high order bits. |
| 627 HpackInputStream input_stream(kLiteralBound, "\xfe\xdc"); |
| 628 auto peeked_count_and_bits = input_stream.InitializePeekBits(); |
| 629 size_t peeked_count = peeked_count_and_bits.first; |
| 630 uint32_t bits = peeked_count_and_bits.second; |
| 631 EXPECT_EQ(16u, peeked_count); |
| 632 EXPECT_EQ(0xfedc0000, bits); |
| 633 input_stream.ConsumeBits(16); |
| 634 EXPECT_FALSE(input_stream.HasMoreData()); |
| 635 } |
| 636 { |
| 637 // Three input bytes, returns peeked_count == 24 and bits |
| 638 // has the three input bytes in its high order bits. |
| 639 HpackInputStream input_stream(kLiteralBound, "\xab\xcd\xef"); |
| 640 auto peeked_count_and_bits = input_stream.InitializePeekBits(); |
| 641 size_t peeked_count = peeked_count_and_bits.first; |
| 642 uint32_t bits = peeked_count_and_bits.second; |
| 643 EXPECT_EQ(24u, peeked_count); |
| 644 EXPECT_EQ(0xabcdef00, bits); |
| 645 input_stream.ConsumeBits(24); |
| 646 EXPECT_FALSE(input_stream.HasMoreData()); |
| 647 } |
| 648 { |
| 649 // Four input bytes, returns peeked_count == 32 and bits |
| 650 // contains the four input bytes. |
| 651 HpackInputStream input_stream(kLiteralBound, "\xfe\xed\xdc\xcb"); |
| 652 auto peeked_count_and_bits = input_stream.InitializePeekBits(); |
| 653 size_t peeked_count = peeked_count_and_bits.first; |
| 654 uint32_t bits = peeked_count_and_bits.second; |
| 655 EXPECT_EQ(32u, peeked_count); |
| 656 EXPECT_EQ(0xfeeddccb, bits); |
| 657 input_stream.ConsumeBits(32); |
| 658 EXPECT_FALSE(input_stream.HasMoreData()); |
| 659 } |
| 660 { |
| 661 // Five input bytes, returns peeked_count == 32 and bits |
| 662 // contains the first four input bytes. |
| 663 HpackInputStream input_stream(kLiteralBound, "\xfe\xed\xdc\xcb\xba"); |
| 664 auto peeked_count_and_bits = input_stream.InitializePeekBits(); |
| 665 size_t peeked_count = peeked_count_and_bits.first; |
| 666 uint32_t bits = peeked_count_and_bits.second; |
| 667 EXPECT_EQ(32u, peeked_count); |
| 668 EXPECT_EQ(0xfeeddccb, bits); |
| 669 EXPECT_TRUE(input_stream.HasMoreData()); |
| 670 |
| 671 // If we consume some bits, then InitializePeekBits will return no bits. |
| 672 input_stream.ConsumeBits(28); |
| 673 peeked_count -= 28; |
| 674 bits <<= 28; |
| 675 EXPECT_EQ(0xb0000000, bits); |
| 676 |
| 677 EXPECT_DFATAL(peeked_count_and_bits = input_stream.InitializePeekBits(), |
| 678 "bit_offset_"); |
| 679 EXPECT_EQ(0u, peeked_count_and_bits.first); |
| 680 EXPECT_EQ(0u, peeked_count_and_bits.second); |
| 681 EXPECT_TRUE(input_stream.HasMoreData()); |
| 682 |
| 683 // Can PeekBits, which will get us the last byte's bits. |
| 684 EXPECT_TRUE(input_stream.PeekBits(&peeked_count, &bits)); |
| 685 EXPECT_EQ(12u, peeked_count); |
| 686 EXPECT_EQ(0xbba00000, bits); |
| 687 input_stream.ConsumeBits(12); |
| 688 EXPECT_FALSE(input_stream.HasMoreData()); |
| 689 } |
| 690 } |
| 691 |
| 692 TEST(HpackInputStreamTest, ConsumeByteRemainder) { |
613 HpackInputStream input_stream(kLiteralBound, "\xad\xab"); | 693 HpackInputStream input_stream(kLiteralBound, "\xad\xab"); |
614 // Does nothing. | 694 // Does nothing. |
615 input_stream.ConsumeByteRemainder(); | 695 input_stream.ConsumeByteRemainder(); |
616 | 696 |
617 // Consumes one byte. | 697 // Consumes one byte. |
618 input_stream.ConsumeBits(3); | 698 input_stream.ConsumeBits(3); |
619 input_stream.ConsumeByteRemainder(); | 699 input_stream.ConsumeByteRemainder(); |
620 EXPECT_TRUE(input_stream.HasMoreData()); | 700 EXPECT_TRUE(input_stream.HasMoreData()); |
621 | 701 |
622 input_stream.ConsumeBits(6); | 702 input_stream.ConsumeBits(6); |
623 EXPECT_TRUE(input_stream.HasMoreData()); | 703 EXPECT_TRUE(input_stream.HasMoreData()); |
624 input_stream.ConsumeByteRemainder(); | 704 input_stream.ConsumeByteRemainder(); |
625 EXPECT_FALSE(input_stream.HasMoreData()); | 705 EXPECT_FALSE(input_stream.HasMoreData()); |
626 } | 706 } |
627 | 707 |
628 } // namespace | 708 } // namespace |
629 | 709 |
630 } // namespace net | 710 } // namespace net |
OLD | NEW |