QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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 "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 | |
| 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. |
| 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 |
| 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 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 99 | const std::string expected = |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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 |
| 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, |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame^] | 123 | quiche::QuicheStringPiece(kHuffmanWwwExampleCom, |
| 124 | sizeof kHuffmanWwwExampleCom)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 125 | EXPECT_EQ(17u, b.size()); |
| 126 | |
| 127 | // Hex dump of encoded data (copied from RFC): |
| 128 | // 0x0000: 8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ...A......:k.... |
| 129 | // 0x0010: ff . |
| 130 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 131 | const std::string expected = |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 132 | Http2HexDecode("828684418cf1e3c2e5f23a6ba0ab90f4ff"); |
| 133 | EXPECT_EQ(expected, b.buffer()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | TEST(HpackBlockBuilderTest, DynamicTableSizeUpdate) { |
| 138 | { |
| 139 | HpackBlockBuilder b; |
| 140 | b.AppendDynamicTableSizeUpdate(0); |
| 141 | EXPECT_EQ(1u, b.size()); |
| 142 | |
| 143 | const char kData[] = {'\x20'}; |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame^] | 144 | quiche::QuicheStringPiece expected(kData, sizeof kData); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 145 | EXPECT_EQ(expected, b.buffer()); |
| 146 | } |
| 147 | { |
| 148 | HpackBlockBuilder b; |
| 149 | b.AppendDynamicTableSizeUpdate(4096); // The default size. |
| 150 | EXPECT_EQ(3u, b.size()); |
| 151 | |
| 152 | const char kData[] = {'\x3f', '\xe1', '\x1f'}; |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame^] | 153 | quiche::QuicheStringPiece expected(kData, sizeof kData); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 154 | EXPECT_EQ(expected, b.buffer()); |
| 155 | } |
| 156 | { |
| 157 | HpackBlockBuilder b; |
| 158 | b.AppendDynamicTableSizeUpdate(1000000000000); // A very large value. |
| 159 | EXPECT_EQ(7u, b.size()); |
| 160 | |
| 161 | const char kData[] = {'\x3f', '\xe1', '\x9f', '\x94', |
| 162 | '\xa5', '\x8d', '\x1d'}; |
bnc | 74646d1 | 2019-12-13 09:21:19 -0800 | [diff] [blame^] | 163 | quiche::QuicheStringPiece expected(kData, sizeof kData); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 164 | EXPECT_EQ(expected, b.buffer()); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } // namespace |
| 169 | } // namespace test |
| 170 | } // namespace http2 |