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