blob: dcbff8a19382564b6819312780e02421e08b662c [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// Copyright 2016 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/third_party/quiche/src/http2/hpack/varint/hpack_varint_encoder.h"
6
QUICHE teamfd50a402018-12-07 22:54:05 -05007#include "net/third_party/quiche/src/http2/platform/api/http2_string_utils.h"
bncf0d86a12019-12-13 12:58:44 -08008#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
QUICHE teamfd50a402018-12-07 22:54:05 -05009
bnc6744c062020-09-30 10:45:28 -070010#include "net/third_party/quiche/src/common/platform/api/quiche_test.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050011
12namespace http2 {
13namespace test {
14namespace {
15
QUICHE teamfd50a402018-12-07 22:54:05 -050016struct {
17 uint8_t high_bits;
18 uint8_t prefix_length;
19 uint64_t value;
20 uint8_t expected_encoding;
21} kShortTestData[] = {{0b10110010, 1, 0, 0b10110010},
22 {0b10101100, 2, 2, 0b10101110},
23 {0b10100000, 3, 6, 0b10100110},
24 {0b10110000, 4, 13, 0b10111101},
25 {0b10100000, 5, 8, 0b10101000},
26 {0b11000000, 6, 48, 0b11110000},
27 {0b10000000, 7, 99, 0b11100011},
28 // Example from RFC7541 C.1.
29 {0b00000000, 5, 10, 0b00001010}};
30
31// Encode integers that fit in the prefix.
32TEST(HpackVarintEncoderTest, Short) {
bncf0d86a12019-12-13 12:58:44 -080033 for (size_t i = 0; i < QUICHE_ARRAYSIZE(kShortTestData); ++i) {
bnc47904002019-08-16 11:49:48 -070034 std::string output;
bncd0f61e72019-06-24 11:53:31 -070035 HpackVarintEncoder::Encode(kShortTestData[i].high_bits,
36 kShortTestData[i].prefix_length,
37 kShortTestData[i].value, &output);
bnce6640c82019-06-24 11:25:56 -070038 ASSERT_EQ(1u, output.size());
bnc8da8f712019-06-25 06:14:59 -070039 EXPECT_EQ(kShortTestData[i].expected_encoding,
40 static_cast<uint8_t>(output[0]));
QUICHE teamfd50a402018-12-07 22:54:05 -050041 }
42}
43
44struct {
45 uint8_t high_bits;
46 uint8_t prefix_length;
47 uint64_t value;
48 const char* expected_encoding;
49} kLongTestData[] = {
50 // One extension byte.
51 {0b10011000, 3, 103, "9f60"},
52 {0b10010000, 4, 57, "9f2a"},
53 {0b11000000, 5, 158, "df7f"},
54 {0b01000000, 6, 65, "7f02"},
55 {0b00000000, 7, 200, "7f49"},
56 // Two extension bytes.
57 {0b10011000, 3, 12345, "9fb260"},
58 {0b10010000, 4, 5401, "9f8a2a"},
59 {0b11000000, 5, 16327, "dfa87f"},
60 {0b01000000, 6, 399, "7fd002"},
61 {0b00000000, 7, 9598, "7fff49"},
62 // Three extension bytes.
63 {0b10011000, 3, 1579281, "9f8ab260"},
64 {0b10010000, 4, 689488, "9fc18a2a"},
65 {0b11000000, 5, 2085964, "dfada87f"},
66 {0b01000000, 6, 43103, "7fa0d002"},
67 {0b00000000, 7, 1212541, "7ffeff49"},
68 // Four extension bytes.
69 {0b10011000, 3, 202147110, "9f9f8ab260"},
70 {0b10010000, 4, 88252593, "9fa2c18a2a"},
71 {0b11000000, 5, 266999535, "dfd0ada87f"},
72 {0b01000000, 6, 5509304, "7ff9a0d002"},
73 {0b00000000, 7, 155189149, "7f9efeff49"},
74 // Six extension bytes.
75 {0b10011000, 3, 3311978140938, "9f83aa9f8ab260"},
76 {0b10010000, 4, 1445930244223, "9ff0b0a2c18a2a"},
77 {0b11000000, 5, 4374519874169, "dfda84d0ada87f"},
78 {0b01000000, 6, 90263420404, "7fb5fbf9a0d002"},
79 {0b00000000, 7, 2542616951118, "7fcff19efeff49"},
80 // Eight extension bytes.
81 {0b10011000, 3, 54263449861016696, "9ff19883aa9f8ab260"},
82 {0b10010000, 4, 23690121121119891, "9f84fdf0b0a2c18a2a"},
83 {0b11000000, 5, 71672133617889215, "dfa0dfda84d0ada87f"},
84 {0b01000000, 6, 1478875878881374, "7f9ff0b5fbf9a0d002"},
85 {0b00000000, 7, 41658236125045114, "7ffbc1cff19efeff49"},
86 // Ten extension bytes.
87 {0b10011000, 3, 12832019021693745307u, "9f94f1f19883aa9f8ab201"},
88 {0b10010000, 4, 9980690937382242223u, "9fa08f84fdf0b0a2c18a01"},
89 {0b11000000, 5, 12131360551794650846u, "dfbfdda0dfda84d0ada801"},
90 {0b01000000, 6, 15006530362736632796u, "7f9dc79ff0b5fbf9a0d001"},
91 {0b00000000, 7, 18445754019193211014u, "7f8790fbc1cff19efeff01"},
92 // Maximum value: 2^64-1.
93 {0b10011000, 3, 18446744073709551615u, "9ff8ffffffffffffffff01"},
94 {0b10010000, 4, 18446744073709551615u, "9ff0ffffffffffffffff01"},
95 {0b11000000, 5, 18446744073709551615u, "dfe0ffffffffffffffff01"},
96 {0b01000000, 6, 18446744073709551615u, "7fc0ffffffffffffffff01"},
97 {0b00000000, 7, 18446744073709551615u, "7f80ffffffffffffffff01"},
98 // Example from RFC7541 C.1.
99 {0b00000000, 5, 1337, "1f9a0a"},
100};
101
102// Encode integers that do not fit in the prefix.
103TEST(HpackVarintEncoderTest, Long) {
QUICHE teamfd50a402018-12-07 22:54:05 -0500104 // Test encoding byte by byte, also test encoding in
105 // a single ResumeEncoding() call.
bncf0d86a12019-12-13 12:58:44 -0800106 for (size_t i = 0; i < QUICHE_ARRAYSIZE(kLongTestData); ++i) {
107 std::string expected_encoding =
108 Http2HexDecode(kLongTestData[i].expected_encoding);
QUICHE teamfd50a402018-12-07 22:54:05 -0500109
bncf0d86a12019-12-13 12:58:44 -0800110 std::string output;
111 HpackVarintEncoder::Encode(kLongTestData[i].high_bits,
112 kLongTestData[i].prefix_length,
113 kLongTestData[i].value, &output);
bnce6640c82019-06-24 11:25:56 -0700114
bncf0d86a12019-12-13 12:58:44 -0800115 EXPECT_EQ(expected_encoding, output);
116 }
QUICHE teamfd50a402018-12-07 22:54:05 -0500117}
118
119struct {
120 uint8_t high_bits;
121 uint8_t prefix_length;
122 uint64_t value;
123 uint8_t expected_encoding_first_byte;
124} kLastByteIsZeroTestData[] = {
QUICHE teamcc663702018-12-17 15:51:59 -0500125 {0b10110010, 1, 1, 0b10110011}, {0b10101100, 2, 3, 0b10101111},
126 {0b10101000, 3, 7, 0b10101111}, {0b10110000, 4, 15, 0b10111111},
127 {0b10100000, 5, 31, 0b10111111}, {0b11000000, 6, 63, 0b11111111},
128 {0b10000000, 7, 127, 0b11111111}, {0b00000000, 8, 255, 0b11111111}};
QUICHE teamfd50a402018-12-07 22:54:05 -0500129
130// Make sure that the encoder outputs the last byte even when it is zero. This
131// happens exactly when encoding the value 2^prefix_length - 1.
132TEST(HpackVarintEncoderTest, LastByteIsZero) {
bncf0d86a12019-12-13 12:58:44 -0800133 for (size_t i = 0; i < QUICHE_ARRAYSIZE(kLastByteIsZeroTestData); ++i) {
bnc47904002019-08-16 11:49:48 -0700134 std::string output;
bncd0f61e72019-06-24 11:53:31 -0700135 HpackVarintEncoder::Encode(kLastByteIsZeroTestData[i].high_bits,
136 kLastByteIsZeroTestData[i].prefix_length,
137 kLastByteIsZeroTestData[i].value, &output);
bnce6640c82019-06-24 11:25:56 -0700138 ASSERT_EQ(2u, output.size());
139 EXPECT_EQ(kLastByteIsZeroTestData[i].expected_encoding_first_byte,
bnc8da8f712019-06-25 06:14:59 -0700140 static_cast<uint8_t>(output[0]));
bnce6640c82019-06-24 11:25:56 -0700141 EXPECT_EQ(0b00000000, output[1]);
QUICHE teamfd50a402018-12-07 22:54:05 -0500142 }
143}
144
bnce6640c82019-06-24 11:25:56 -0700145// Test that encoder appends correctly to non-empty string.
146TEST(HpackVarintEncoderTest, Append) {
bnc47904002019-08-16 11:49:48 -0700147 std::string output("foo");
bnce6640c82019-06-24 11:25:56 -0700148 EXPECT_EQ(Http2HexDecode("666f6f"), output);
149
bncd0f61e72019-06-24 11:53:31 -0700150 HpackVarintEncoder::Encode(0b10011000, 3, 103, &output);
bnce6640c82019-06-24 11:25:56 -0700151 EXPECT_EQ(Http2HexDecode("666f6f9f60"), output);
152
bncd0f61e72019-06-24 11:53:31 -0700153 HpackVarintEncoder::Encode(0b10100000, 5, 8, &output);
bnce6640c82019-06-24 11:25:56 -0700154 EXPECT_EQ(Http2HexDecode("666f6f9f60a8"), output);
155
bncd0f61e72019-06-24 11:53:31 -0700156 HpackVarintEncoder::Encode(0b10011000, 3, 202147110, &output);
bnce6640c82019-06-24 11:25:56 -0700157 EXPECT_EQ(Http2HexDecode("666f6f9f60a89f9f8ab260"), output);
158}
159
QUICHE teamfd50a402018-12-07 22:54:05 -0500160} // namespace
161} // namespace test
162} // namespace http2