blob: a7b80647979ec582491140f106475615ee564b67 [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/tools/hpack_block_builder.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8#include "net/third_party/quiche/src/http2/platform/api/http2_string_utils.h"
9
10namespace http2 {
11namespace test {
12namespace {
13const bool kUncompressed = false;
14const bool kCompressed = true;
15
16// TODO(jamessynge): Once static table code is checked in, switch to using
17// constants from there.
18const uint32_t kStaticTableMethodGET = 2;
19const uint32_t kStaticTablePathSlash = 4;
20const 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.
25TEST(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
80TEST(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 Http2String expected =
100 Http2HexDecode("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
107TEST(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 Http2StringPiece(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 Http2String expected =
131 Http2HexDecode("828684418cf1e3c2e5f23a6ba0ab90f4ff");
132 EXPECT_EQ(expected, b.buffer());
133 }
134}
135
136TEST(HpackBlockBuilderTest, DynamicTableSizeUpdate) {
137 {
138 HpackBlockBuilder b;
139 b.AppendDynamicTableSizeUpdate(0);
140 EXPECT_EQ(1u, b.size());
141
142 const char kData[] = {'\x20'};
143 Http2StringPiece 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 Http2StringPiece 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 Http2StringPiece expected(kData, sizeof kData);
163 EXPECT_EQ(expected, b.buffer());
164 }
165}
166
167} // namespace
168} // namespace test
169} // namespace http2