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