1 // Copyright 2018 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 "quiche/http2/hpack/varint/hpack_varint_decoder.h"
6
7 // Test HpackVarintDecoder against hardcoded data.
8
9 #include <stddef.h>
10
11 #include <utility>
12
13 #include "absl/base/macros.h"
14 #include "absl/strings/escaping.h"
15 #include "absl/strings/string_view.h"
16 #include "quiche/http2/test_tools/random_decoder_test_base.h"
17 #include "quiche/http2/test_tools/verify_macros.h"
18 #include "quiche/common/platform/api/quiche_logging.h"
19 #include "quiche/common/platform/api/quiche_test.h"
20
21 using ::testing::AssertionSuccess;
22
23 namespace http2 {
24 namespace test {
25 namespace {
26
27 class HpackVarintDecoderTest
28 : public RandomDecoderTest,
29 public ::testing::WithParamInterface<std::tuple<uint8_t, const char*>> {
30 protected:
HpackVarintDecoderTest()31 HpackVarintDecoderTest()
32 : high_bits_(std::get<0>(GetParam())),
33 suffix_(absl::HexStringToBytes(std::get<1>(GetParam()))),
34 prefix_length_(0) {}
35
DecodeExpectSuccess(absl::string_view data,uint32_t prefix_length,uint64_t expected_value)36 void DecodeExpectSuccess(absl::string_view data, uint32_t prefix_length,
37 uint64_t expected_value) {
38 Validator validator = [expected_value, this](
39 const DecodeBuffer& /*db*/,
40 DecodeStatus /*status*/) -> AssertionResult {
41 HTTP2_VERIFY_EQ(expected_value, decoder_.value())
42 << "Value doesn't match expected: " << decoder_.value()
43 << " != " << expected_value;
44 return AssertionSuccess();
45 };
46
47 // First validate that decoding is done and that we've advanced the cursor
48 // the expected amount.
49 validator =
50 ValidateDoneAndOffset(/* offset = */ data.size(), std::move(validator));
51
52 EXPECT_TRUE(Decode(data, prefix_length, std::move(validator)));
53
54 EXPECT_EQ(expected_value, decoder_.value());
55 }
56
DecodeExpectError(absl::string_view data,uint32_t prefix_length)57 void DecodeExpectError(absl::string_view data, uint32_t prefix_length) {
58 Validator validator = [](const DecodeBuffer& /*db*/,
59 DecodeStatus status) -> AssertionResult {
60 HTTP2_VERIFY_EQ(DecodeStatus::kDecodeError, status);
61 return AssertionSuccess();
62 };
63
64 EXPECT_TRUE(Decode(data, prefix_length, std::move(validator)));
65 }
66
67 private:
Decode(absl::string_view data,uint32_t prefix_length,const Validator validator)68 AssertionResult Decode(absl::string_view data, uint32_t prefix_length,
69 const Validator validator) {
70 prefix_length_ = prefix_length;
71
72 // Copy |data| so that it can be modified.
73 std::string data_copy(data);
74
75 // Bits of the first byte not part of the prefix should be ignored.
76 uint8_t high_bits_mask = 0b11111111 << prefix_length_;
77 data_copy[0] |= (high_bits_mask & high_bits_);
78
79 // Extra bytes appended to the input should be ignored.
80 data_copy.append(suffix_);
81
82 DecodeBuffer b(data_copy);
83
84 // StartDecoding, above, requires the DecodeBuffer be non-empty so that it
85 // can call Start with the prefix byte.
86 bool return_non_zero_on_first = true;
87
88 return DecodeAndValidateSeveralWays(&b, return_non_zero_on_first,
89 validator);
90 }
91
StartDecoding(DecodeBuffer * b)92 DecodeStatus StartDecoding(DecodeBuffer* b) override {
93 QUICHE_CHECK_LT(0u, b->Remaining());
94 uint8_t prefix = b->DecodeUInt8();
95 return decoder_.Start(prefix, prefix_length_, b);
96 }
97
ResumeDecoding(DecodeBuffer * b)98 DecodeStatus ResumeDecoding(DecodeBuffer* b) override {
99 return decoder_.Resume(b);
100 }
101
102 // Bits of the first byte not part of the prefix.
103 const uint8_t high_bits_;
104 // Extra bytes appended to the input.
105 const std::string suffix_;
106
107 HpackVarintDecoder decoder_;
108 uint8_t prefix_length_;
109 };
110
111 INSTANTIATE_TEST_SUITE_P(
112 HpackVarintDecoderTest, HpackVarintDecoderTest,
113 ::testing::Combine(
114 // Bits of the first byte not part of the prefix should be ignored.
115 ::testing::Values(0b00000000, 0b11111111, 0b10101010),
116 // Extra bytes appended to the input should be ignored.
117 ::testing::Values("", "00", "666f6f")));
118
119 struct {
120 const char* data;
121 uint32_t prefix_length;
122 uint64_t expected_value;
123 } kSuccessTestData[] = {
124 // Zero value with different prefix lengths.
125 {"00", 3, 0},
126 {"00", 4, 0},
127 {"00", 5, 0},
128 {"00", 6, 0},
129 {"00", 7, 0},
130 {"00", 8, 0},
131 // Small values that fit in the prefix.
132 {"06", 3, 6},
133 {"0d", 4, 13},
134 {"10", 5, 16},
135 {"29", 6, 41},
136 {"56", 7, 86},
137 {"bf", 8, 191},
138 // Values of 2^n-1, which have an all-zero extension byte.
139 {"0700", 3, 7},
140 {"0f00", 4, 15},
141 {"1f00", 5, 31},
142 {"3f00", 6, 63},
143 {"7f00", 7, 127},
144 {"ff00", 8, 255},
145 // Values of 2^n-1, plus one extra byte of padding.
146 {"078000", 3, 7},
147 {"0f8000", 4, 15},
148 {"1f8000", 5, 31},
149 {"3f8000", 6, 63},
150 {"7f8000", 7, 127},
151 {"ff8000", 8, 255},
152 // Values requiring one extension byte.
153 {"0760", 3, 103},
154 {"0f2a", 4, 57},
155 {"1f7f", 5, 158},
156 {"3f02", 6, 65},
157 {"7f49", 7, 200},
158 {"ff6f", 8, 366},
159 // Values requiring one extension byte, plus one byte of padding.
160 {"07e000", 3, 103},
161 {"0faa00", 4, 57},
162 {"1fff00", 5, 158},
163 {"3f8200", 6, 65},
164 {"7fc900", 7, 200},
165 {"ffef00", 8, 366},
166 // Values requiring one extension byte, plus two bytes of padding.
167 {"07e08000", 3, 103},
168 {"0faa8000", 4, 57},
169 {"1fff8000", 5, 158},
170 {"3f828000", 6, 65},
171 {"7fc98000", 7, 200},
172 {"ffef8000", 8, 366},
173 // Values requiring one extension byte, plus the maximum amount of padding.
174 {"07e0808080808080808000", 3, 103},
175 {"0faa808080808080808000", 4, 57},
176 {"1fff808080808080808000", 5, 158},
177 {"3f82808080808080808000", 6, 65},
178 {"7fc9808080808080808000", 7, 200},
179 {"ffef808080808080808000", 8, 366},
180 // Values requiring two extension bytes.
181 {"07b260", 3, 12345},
182 {"0f8a2a", 4, 5401},
183 {"1fa87f", 5, 16327},
184 {"3fd002", 6, 399},
185 {"7fff49", 7, 9598},
186 {"ffe32f", 8, 6370},
187 // Values requiring two extension bytes, plus one byte of padding.
188 {"07b2e000", 3, 12345},
189 {"0f8aaa00", 4, 5401},
190 {"1fa8ff00", 5, 16327},
191 {"3fd08200", 6, 399},
192 {"7fffc900", 7, 9598},
193 {"ffe3af00", 8, 6370},
194 // Values requiring two extension bytes, plus the maximum amount of padding.
195 {"07b2e080808080808000", 3, 12345},
196 {"0f8aaa80808080808000", 4, 5401},
197 {"1fa8ff80808080808000", 5, 16327},
198 {"3fd08280808080808000", 6, 399},
199 {"7fffc980808080808000", 7, 9598},
200 {"ffe3af80808080808000", 8, 6370},
201 // Values requiring three extension bytes.
202 {"078ab260", 3, 1579281},
203 {"0fc18a2a", 4, 689488},
204 {"1fada87f", 5, 2085964},
205 {"3fa0d002", 6, 43103},
206 {"7ffeff49", 7, 1212541},
207 {"ff93de23", 8, 585746},
208 // Values requiring three extension bytes, plus one byte of padding.
209 {"078ab2e000", 3, 1579281},
210 {"0fc18aaa00", 4, 689488},
211 {"1fada8ff00", 5, 2085964},
212 {"3fa0d08200", 6, 43103},
213 {"7ffeffc900", 7, 1212541},
214 {"ff93dea300", 8, 585746},
215 // Values requiring four extension bytes.
216 {"079f8ab260", 3, 202147110},
217 {"0fa2c18a2a", 4, 88252593},
218 {"1fd0ada87f", 5, 266999535},
219 {"3ff9a0d002", 6, 5509304},
220 {"7f9efeff49", 7, 155189149},
221 {"ffaa82f404", 8, 10289705},
222 // Values requiring four extension bytes, plus one byte of padding.
223 {"079f8ab2e000", 3, 202147110},
224 {"0fa2c18aaa00", 4, 88252593},
225 {"1fd0ada8ff00", 5, 266999535},
226 {"3ff9a0d08200", 6, 5509304},
227 {"7f9efeffc900", 7, 155189149},
228 {"ffaa82f48400", 8, 10289705},
229 // Values requiring six extension bytes.
230 {"0783aa9f8ab260", 3, 3311978140938},
231 {"0ff0b0a2c18a2a", 4, 1445930244223},
232 {"1fda84d0ada87f", 5, 4374519874169},
233 {"3fb5fbf9a0d002", 6, 90263420404},
234 {"7fcff19efeff49", 7, 2542616951118},
235 {"ff9fa486bbc327", 8, 1358138807070},
236 // Values requiring eight extension bytes.
237 {"07f19883aa9f8ab260", 3, 54263449861016696},
238 {"0f84fdf0b0a2c18a2a", 4, 23690121121119891},
239 {"1fa0dfda84d0ada87f", 5, 71672133617889215},
240 {"3f9ff0b5fbf9a0d002", 6, 1478875878881374},
241 {"7ffbc1cff19efeff49", 7, 41658236125045114},
242 {"ff91b6fb85af99c342", 8, 37450237664484368},
243 // Values requiring ten extension bytes.
244 {"0794f1f19883aa9f8ab201", 3, 12832019021693745307u},
245 {"0fa08f84fdf0b0a2c18a01", 4, 9980690937382242223u},
246 {"1fbfdda0dfda84d0ada801", 5, 12131360551794650846u},
247 {"3f9dc79ff0b5fbf9a0d001", 6, 15006530362736632796u},
248 {"7f8790fbc1cff19efeff01", 7, 18445754019193211014u},
249 {"fffba8c5b8d3fe9f8c8401", 8, 9518498503615141242u},
250 // Maximum value: 2^64-1.
251 {"07f8ffffffffffffffff01", 3, 18446744073709551615u},
252 {"0ff0ffffffffffffffff01", 4, 18446744073709551615u},
253 {"1fe0ffffffffffffffff01", 5, 18446744073709551615u},
254 {"3fc0ffffffffffffffff01", 6, 18446744073709551615u},
255 {"7f80ffffffffffffffff01", 7, 18446744073709551615u},
256 {"ff80feffffffffffffff01", 8, 18446744073709551615u},
257 // Examples from RFC7541 C.1.
258 {"0a", 5, 10},
259 {"1f9a0a", 5, 1337},
260 };
261
TEST_P(HpackVarintDecoderTest,Success)262 TEST_P(HpackVarintDecoderTest, Success) {
263 for (size_t i = 0; i < ABSL_ARRAYSIZE(kSuccessTestData); ++i) {
264 DecodeExpectSuccess(absl::HexStringToBytes(kSuccessTestData[i].data),
265 kSuccessTestData[i].prefix_length,
266 kSuccessTestData[i].expected_value);
267 }
268 }
269
270 struct {
271 const char* data;
272 uint32_t prefix_length;
273 } kErrorTestData[] = {
274 // Too many extension bytes, all 0s (except for extension bit in each byte).
275 {"0780808080808080808080", 3},
276 {"0f80808080808080808080", 4},
277 {"1f80808080808080808080", 5},
278 {"3f80808080808080808080", 6},
279 {"7f80808080808080808080", 7},
280 {"ff80808080808080808080", 8},
281 // Too many extension bytes, all 1s.
282 {"07ffffffffffffffffffff", 3},
283 {"0fffffffffffffffffffff", 4},
284 {"1fffffffffffffffffffff", 5},
285 {"3fffffffffffffffffffff", 6},
286 {"7fffffffffffffffffffff", 7},
287 {"ffffffffffffffffffffff", 8},
288 // Value of 2^64, one higher than maximum of 2^64-1.
289 {"07f9ffffffffffffffff01", 3},
290 {"0ff1ffffffffffffffff01", 4},
291 {"1fe1ffffffffffffffff01", 5},
292 {"3fc1ffffffffffffffff01", 6},
293 {"7f81ffffffffffffffff01", 7},
294 {"ff81feffffffffffffff01", 8},
295 // Maximum value: 2^64-1, with one byte of padding.
296 {"07f8ffffffffffffffff8100", 3},
297 {"0ff0ffffffffffffffff8100", 4},
298 {"1fe0ffffffffffffffff8100", 5},
299 {"3fc0ffffffffffffffff8100", 6},
300 {"7f80ffffffffffffffff8100", 7},
301 {"ff80feffffffffffffff8100", 8}};
302
TEST_P(HpackVarintDecoderTest,Error)303 TEST_P(HpackVarintDecoderTest, Error) {
304 for (size_t i = 0; i < ABSL_ARRAYSIZE(kErrorTestData); ++i) {
305 DecodeExpectError(absl::HexStringToBytes(kErrorTestData[i].data),
306 kErrorTestData[i].prefix_length);
307 }
308 }
309
310 } // namespace
311 } // namespace test
312 } // namespace http2
313