OLD | NEW |
(Empty) | |
| 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/http2/hpack/tools/hpack_block_builder.h" |
| 6 |
| 7 #include "net/spdy/spdy_test_utils.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 using base::StringPiece; |
| 11 |
| 12 namespace net { |
| 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. |
| 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 |
| 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 const std::string expected = |
| 102 a2b_hex("828684410f7777772e6578616d706c652e636f6d"); |
| 103 EXPECT_EQ(expected, b.buffer()); |
| 104 } |
| 105 } |
| 106 |
| 107 // Tests of encoding per the RFC. See: |
| 108 // http://httpwg.org/specs/rfc7541.html#request.examples.with.huffman.coding |
| 109 TEST(HpackBlockBuilderTest, ExamplesFromSpecC4) { |
| 110 { |
| 111 // Header block to encode: |
| 112 // :method: GET |
| 113 // :scheme: http |
| 114 // :path: / |
| 115 // :authority: www.example.com (Huffman encoded) |
| 116 HpackBlockBuilder b; |
| 117 b.AppendIndexedHeader(kStaticTableMethodGET); |
| 118 b.AppendIndexedHeader(kStaticTableSchemeHttp); |
| 119 b.AppendIndexedHeader(kStaticTablePathSlash); |
| 120 const char kHuffmanWwwExampleCom[] = {0xf1u, 0xe3u, 0xc2u, 0xe5u, |
| 121 0xf2u, 0x3au, 0x6bu, 0xa0u, |
| 122 0xabu, 0x90u, 0xf4u, 0xffu}; |
| 123 b.AppendNameIndexAndLiteralValue( |
| 124 HpackEntryType::kIndexedLiteralHeader, 1, kCompressed, |
| 125 StringPiece(kHuffmanWwwExampleCom, sizeof kHuffmanWwwExampleCom)); |
| 126 EXPECT_EQ(17u, b.size()); |
| 127 |
| 128 // Hex dump of encoded data (copied from RFC): |
| 129 // 0x0000: 8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ...A......:k.... |
| 130 // 0x0010: ff . |
| 131 |
| 132 const std::string expected = a2b_hex("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[] = {0x20}; |
| 144 StringPiece expected(kData, sizeof kData); |
| 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[] = {0x3f, 0xe1u, 0x1f}; |
| 153 StringPiece expected(kData, sizeof kData); |
| 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[] = {0x3fu, 0xe1u, 0x9fu, 0x94u, 0xa5u, 0x8du, 0x1du}; |
| 162 StringPiece expected(kData, sizeof kData); |
| 163 EXPECT_EQ(expected, b.buffer()); |
| 164 } |
| 165 } |
| 166 |
| 167 } // namespace |
| 168 } // namespace test |
| 169 } // namespace net |
OLD | NEW |