xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/http2/test_tools/hpack_block_builder_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // 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 "quiche/http2/test_tools/hpack_block_builder.h"
6 
7 #include <string>
8 
9 #include "absl/strings/escaping.h"
10 #include "quiche/common/platform/api/quiche_test.h"
11 
12 namespace http2 {
13 namespace test {
14 namespace {
15 const bool kUncompressed = false;
16 const bool kCompressed = true;
17 
18 // TODO(jamessynge): Once static table code is checked in, switch to using
19 // constants from there.
20 const uint32_t kStaticTableMethodGET = 2;
21 const uint32_t kStaticTablePathSlash = 4;
22 const uint32_t kStaticTableSchemeHttp = 6;
23 
24 // Tests of encoding per the RFC. See:
25 //   http://httpwg.org/specs/rfc7541.html#header.field.representation.examples
26 // The expected values have been copied from the RFC.
TEST(HpackBlockBuilderTest,ExamplesFromSpecC2)27 TEST(HpackBlockBuilderTest, ExamplesFromSpecC2) {
28   {
29     HpackBlockBuilder b;
30     b.AppendLiteralNameAndValue(HpackEntryType::kIndexedLiteralHeader,
31                                 kUncompressed, "custom-key", kUncompressed,
32                                 "custom-header");
33     EXPECT_EQ(26u, b.size());
34 
35     const char kExpected[] =
36         "\x40"            // == Literal indexed ==
37         "\x0a"            // Name length (10)
38         "custom-key"      // Name
39         "\x0d"            // Value length (13)
40         "custom-header";  // Value
41     EXPECT_EQ(kExpected, b.buffer());
42   }
43   {
44     HpackBlockBuilder b;
45     b.AppendNameIndexAndLiteralValue(HpackEntryType::kUnindexedLiteralHeader, 4,
46                                      kUncompressed, "/sample/path");
47     EXPECT_EQ(14u, b.size());
48 
49     const char kExpected[] =
50         "\x04"           // == Literal unindexed, name index 0x04 ==
51         "\x0c"           // Value length (12)
52         "/sample/path";  // Value
53     EXPECT_EQ(kExpected, b.buffer());
54   }
55   {
56     HpackBlockBuilder b;
57     b.AppendLiteralNameAndValue(HpackEntryType::kNeverIndexedLiteralHeader,
58                                 kUncompressed, "password", kUncompressed,
59                                 "secret");
60     EXPECT_EQ(17u, b.size());
61 
62     const char kExpected[] =
63         "\x10"      // == Literal never indexed ==
64         "\x08"      // Name length (8)
65         "password"  // Name
66         "\x06"      // Value length (6)
67         "secret";   // Value
68     EXPECT_EQ(kExpected, b.buffer());
69   }
70   {
71     HpackBlockBuilder b;
72     b.AppendIndexedHeader(2);
73     EXPECT_EQ(1u, b.size());
74 
75     const char kExpected[] = "\x82";  // == Indexed (2) ==
76     EXPECT_EQ(kExpected, b.buffer());
77   }
78 }
79 
80 // Tests of encoding per the RFC. See:
81 //  http://httpwg.org/specs/rfc7541.html#request.examples.without.huffman.coding
TEST(HpackBlockBuilderTest,ExamplesFromSpecC3)82 TEST(HpackBlockBuilderTest, ExamplesFromSpecC3) {
83   {
84     // Header block to encode:
85     //   :method: GET
86     //   :scheme: http
87     //   :path: /
88     //   :authority: www.example.com
89     HpackBlockBuilder b;
90     b.AppendIndexedHeader(2);  // :method: GET
91     b.AppendIndexedHeader(6);  // :scheme: http
92     b.AppendIndexedHeader(4);  // :path: /
93     b.AppendNameIndexAndLiteralValue(HpackEntryType::kIndexedLiteralHeader, 1,
94                                      kUncompressed, "www.example.com");
95     EXPECT_EQ(20u, b.size());
96 
97     // Hex dump of encoded data (copied from RFC):
98     // 0x0000:  8286 8441 0f77 7777 2e65 7861 6d70 6c65  ...A.www.example
99     // 0x0010:  2e63 6f6d                                .com
100 
101     std::string expected;
102     ASSERT_TRUE(absl::HexStringToBytes(
103         "828684410f7777772e6578616d706c652e636f6d", &expected));
104     EXPECT_EQ(expected, b.buffer());
105   }
106 }
107 
108 // Tests of encoding per the RFC. See:
109 //   http://httpwg.org/specs/rfc7541.html#request.examples.with.huffman.coding
TEST(HpackBlockBuilderTest,ExamplesFromSpecC4)110 TEST(HpackBlockBuilderTest, ExamplesFromSpecC4) {
111   {
112     // Header block to encode:
113     //   :method: GET
114     //   :scheme: http
115     //   :path: /
116     //   :authority: www.example.com  (Huffman encoded)
117     HpackBlockBuilder b;
118     b.AppendIndexedHeader(kStaticTableMethodGET);
119     b.AppendIndexedHeader(kStaticTableSchemeHttp);
120     b.AppendIndexedHeader(kStaticTablePathSlash);
121     const char kHuffmanWwwExampleCom[] = {'\xf1', '\xe3', '\xc2', '\xe5',
122                                           '\xf2', '\x3a', '\x6b', '\xa0',
123                                           '\xab', '\x90', '\xf4', '\xff'};
124     b.AppendNameIndexAndLiteralValue(
125         HpackEntryType::kIndexedLiteralHeader, 1, kCompressed,
126         absl::string_view(kHuffmanWwwExampleCom, sizeof kHuffmanWwwExampleCom));
127     EXPECT_EQ(17u, b.size());
128 
129     // Hex dump of encoded data (copied from RFC):
130     // 0x0000:  8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4  ...A......:k....
131     // 0x0010:  ff                                       .
132 
133     std::string expected;
134     ASSERT_TRUE(absl::HexStringToBytes("828684418cf1e3c2e5f23a6ba0ab90f4ff",
135                                        &expected));
136     EXPECT_EQ(expected, b.buffer());
137   }
138 }
139 
TEST(HpackBlockBuilderTest,DynamicTableSizeUpdate)140 TEST(HpackBlockBuilderTest, DynamicTableSizeUpdate) {
141   {
142     HpackBlockBuilder b;
143     b.AppendDynamicTableSizeUpdate(0);
144     EXPECT_EQ(1u, b.size());
145 
146     const char kData[] = {'\x20'};
147     absl::string_view expected(kData, sizeof kData);
148     EXPECT_EQ(expected, b.buffer());
149   }
150   {
151     HpackBlockBuilder b;
152     b.AppendDynamicTableSizeUpdate(4096);  // The default size.
153     EXPECT_EQ(3u, b.size());
154 
155     const char kData[] = {'\x3f', '\xe1', '\x1f'};
156     absl::string_view expected(kData, sizeof kData);
157     EXPECT_EQ(expected, b.buffer());
158   }
159   {
160     HpackBlockBuilder b;
161     b.AppendDynamicTableSizeUpdate(1000000000000);  // A very large value.
162     EXPECT_EQ(7u, b.size());
163 
164     const char kData[] = {'\x3f', '\xe1', '\x9f', '\x94',
165                           '\xa5', '\x8d', '\x1d'};
166     absl::string_view expected(kData, sizeof kData);
167     EXPECT_EQ(expected, b.buffer());
168   }
169 }
170 
171 }  // namespace
172 }  // namespace test
173 }  // namespace http2
174