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/varint/hpack_varint_encoder.h" |
| 6 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/http2/platform/api/http2_string_utils.h" |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 9 | |
bnc | 6744c06 | 2020-09-30 10:45:28 -0700 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/common/platform/api/quiche_test.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 11 | |
| 12 | namespace http2 { |
| 13 | namespace test { |
| 14 | namespace { |
| 15 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 16 | struct { |
| 17 | uint8_t high_bits; |
| 18 | uint8_t prefix_length; |
| 19 | uint64_t value; |
| 20 | uint8_t expected_encoding; |
| 21 | } kShortTestData[] = {{0b10110010, 1, 0, 0b10110010}, |
| 22 | {0b10101100, 2, 2, 0b10101110}, |
| 23 | {0b10100000, 3, 6, 0b10100110}, |
| 24 | {0b10110000, 4, 13, 0b10111101}, |
| 25 | {0b10100000, 5, 8, 0b10101000}, |
| 26 | {0b11000000, 6, 48, 0b11110000}, |
| 27 | {0b10000000, 7, 99, 0b11100011}, |
| 28 | // Example from RFC7541 C.1. |
| 29 | {0b00000000, 5, 10, 0b00001010}}; |
| 30 | |
| 31 | // Encode integers that fit in the prefix. |
| 32 | TEST(HpackVarintEncoderTest, Short) { |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 33 | for (size_t i = 0; i < QUICHE_ARRAYSIZE(kShortTestData); ++i) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 34 | std::string output; |
bnc | d0f61e7 | 2019-06-24 11:53:31 -0700 | [diff] [blame] | 35 | HpackVarintEncoder::Encode(kShortTestData[i].high_bits, |
| 36 | kShortTestData[i].prefix_length, |
| 37 | kShortTestData[i].value, &output); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 38 | ASSERT_EQ(1u, output.size()); |
bnc | 8da8f71 | 2019-06-25 06:14:59 -0700 | [diff] [blame] | 39 | EXPECT_EQ(kShortTestData[i].expected_encoding, |
| 40 | static_cast<uint8_t>(output[0])); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | struct { |
| 45 | uint8_t high_bits; |
| 46 | uint8_t prefix_length; |
| 47 | uint64_t value; |
| 48 | const char* expected_encoding; |
| 49 | } kLongTestData[] = { |
| 50 | // One extension byte. |
| 51 | {0b10011000, 3, 103, "9f60"}, |
| 52 | {0b10010000, 4, 57, "9f2a"}, |
| 53 | {0b11000000, 5, 158, "df7f"}, |
| 54 | {0b01000000, 6, 65, "7f02"}, |
| 55 | {0b00000000, 7, 200, "7f49"}, |
| 56 | // Two extension bytes. |
| 57 | {0b10011000, 3, 12345, "9fb260"}, |
| 58 | {0b10010000, 4, 5401, "9f8a2a"}, |
| 59 | {0b11000000, 5, 16327, "dfa87f"}, |
| 60 | {0b01000000, 6, 399, "7fd002"}, |
| 61 | {0b00000000, 7, 9598, "7fff49"}, |
| 62 | // Three extension bytes. |
| 63 | {0b10011000, 3, 1579281, "9f8ab260"}, |
| 64 | {0b10010000, 4, 689488, "9fc18a2a"}, |
| 65 | {0b11000000, 5, 2085964, "dfada87f"}, |
| 66 | {0b01000000, 6, 43103, "7fa0d002"}, |
| 67 | {0b00000000, 7, 1212541, "7ffeff49"}, |
| 68 | // Four extension bytes. |
| 69 | {0b10011000, 3, 202147110, "9f9f8ab260"}, |
| 70 | {0b10010000, 4, 88252593, "9fa2c18a2a"}, |
| 71 | {0b11000000, 5, 266999535, "dfd0ada87f"}, |
| 72 | {0b01000000, 6, 5509304, "7ff9a0d002"}, |
| 73 | {0b00000000, 7, 155189149, "7f9efeff49"}, |
| 74 | // Six extension bytes. |
| 75 | {0b10011000, 3, 3311978140938, "9f83aa9f8ab260"}, |
| 76 | {0b10010000, 4, 1445930244223, "9ff0b0a2c18a2a"}, |
| 77 | {0b11000000, 5, 4374519874169, "dfda84d0ada87f"}, |
| 78 | {0b01000000, 6, 90263420404, "7fb5fbf9a0d002"}, |
| 79 | {0b00000000, 7, 2542616951118, "7fcff19efeff49"}, |
| 80 | // Eight extension bytes. |
| 81 | {0b10011000, 3, 54263449861016696, "9ff19883aa9f8ab260"}, |
| 82 | {0b10010000, 4, 23690121121119891, "9f84fdf0b0a2c18a2a"}, |
| 83 | {0b11000000, 5, 71672133617889215, "dfa0dfda84d0ada87f"}, |
| 84 | {0b01000000, 6, 1478875878881374, "7f9ff0b5fbf9a0d002"}, |
| 85 | {0b00000000, 7, 41658236125045114, "7ffbc1cff19efeff49"}, |
| 86 | // Ten extension bytes. |
| 87 | {0b10011000, 3, 12832019021693745307u, "9f94f1f19883aa9f8ab201"}, |
| 88 | {0b10010000, 4, 9980690937382242223u, "9fa08f84fdf0b0a2c18a01"}, |
| 89 | {0b11000000, 5, 12131360551794650846u, "dfbfdda0dfda84d0ada801"}, |
| 90 | {0b01000000, 6, 15006530362736632796u, "7f9dc79ff0b5fbf9a0d001"}, |
| 91 | {0b00000000, 7, 18445754019193211014u, "7f8790fbc1cff19efeff01"}, |
| 92 | // Maximum value: 2^64-1. |
| 93 | {0b10011000, 3, 18446744073709551615u, "9ff8ffffffffffffffff01"}, |
| 94 | {0b10010000, 4, 18446744073709551615u, "9ff0ffffffffffffffff01"}, |
| 95 | {0b11000000, 5, 18446744073709551615u, "dfe0ffffffffffffffff01"}, |
| 96 | {0b01000000, 6, 18446744073709551615u, "7fc0ffffffffffffffff01"}, |
| 97 | {0b00000000, 7, 18446744073709551615u, "7f80ffffffffffffffff01"}, |
| 98 | // Example from RFC7541 C.1. |
| 99 | {0b00000000, 5, 1337, "1f9a0a"}, |
| 100 | }; |
| 101 | |
| 102 | // Encode integers that do not fit in the prefix. |
| 103 | TEST(HpackVarintEncoderTest, Long) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 104 | // Test encoding byte by byte, also test encoding in |
| 105 | // a single ResumeEncoding() call. |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 106 | for (size_t i = 0; i < QUICHE_ARRAYSIZE(kLongTestData); ++i) { |
| 107 | std::string expected_encoding = |
| 108 | Http2HexDecode(kLongTestData[i].expected_encoding); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 109 | |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 110 | std::string output; |
| 111 | HpackVarintEncoder::Encode(kLongTestData[i].high_bits, |
| 112 | kLongTestData[i].prefix_length, |
| 113 | kLongTestData[i].value, &output); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 114 | |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 115 | EXPECT_EQ(expected_encoding, output); |
| 116 | } |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | struct { |
| 120 | uint8_t high_bits; |
| 121 | uint8_t prefix_length; |
| 122 | uint64_t value; |
| 123 | uint8_t expected_encoding_first_byte; |
| 124 | } kLastByteIsZeroTestData[] = { |
QUICHE team | cc66370 | 2018-12-17 15:51:59 -0500 | [diff] [blame] | 125 | {0b10110010, 1, 1, 0b10110011}, {0b10101100, 2, 3, 0b10101111}, |
| 126 | {0b10101000, 3, 7, 0b10101111}, {0b10110000, 4, 15, 0b10111111}, |
| 127 | {0b10100000, 5, 31, 0b10111111}, {0b11000000, 6, 63, 0b11111111}, |
| 128 | {0b10000000, 7, 127, 0b11111111}, {0b00000000, 8, 255, 0b11111111}}; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 129 | |
| 130 | // Make sure that the encoder outputs the last byte even when it is zero. This |
| 131 | // happens exactly when encoding the value 2^prefix_length - 1. |
| 132 | TEST(HpackVarintEncoderTest, LastByteIsZero) { |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 133 | for (size_t i = 0; i < QUICHE_ARRAYSIZE(kLastByteIsZeroTestData); ++i) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 134 | std::string output; |
bnc | d0f61e7 | 2019-06-24 11:53:31 -0700 | [diff] [blame] | 135 | HpackVarintEncoder::Encode(kLastByteIsZeroTestData[i].high_bits, |
| 136 | kLastByteIsZeroTestData[i].prefix_length, |
| 137 | kLastByteIsZeroTestData[i].value, &output); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 138 | ASSERT_EQ(2u, output.size()); |
| 139 | EXPECT_EQ(kLastByteIsZeroTestData[i].expected_encoding_first_byte, |
bnc | 8da8f71 | 2019-06-25 06:14:59 -0700 | [diff] [blame] | 140 | static_cast<uint8_t>(output[0])); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 141 | EXPECT_EQ(0b00000000, output[1]); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 145 | // Test that encoder appends correctly to non-empty string. |
| 146 | TEST(HpackVarintEncoderTest, Append) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 147 | std::string output("foo"); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 148 | EXPECT_EQ(Http2HexDecode("666f6f"), output); |
| 149 | |
bnc | d0f61e7 | 2019-06-24 11:53:31 -0700 | [diff] [blame] | 150 | HpackVarintEncoder::Encode(0b10011000, 3, 103, &output); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 151 | EXPECT_EQ(Http2HexDecode("666f6f9f60"), output); |
| 152 | |
bnc | d0f61e7 | 2019-06-24 11:53:31 -0700 | [diff] [blame] | 153 | HpackVarintEncoder::Encode(0b10100000, 5, 8, &output); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 154 | EXPECT_EQ(Http2HexDecode("666f6f9f60a8"), output); |
| 155 | |
bnc | d0f61e7 | 2019-06-24 11:53:31 -0700 | [diff] [blame] | 156 | HpackVarintEncoder::Encode(0b10011000, 3, 202147110, &output); |
bnc | e6640c8 | 2019-06-24 11:25:56 -0700 | [diff] [blame] | 157 | EXPECT_EQ(Http2HexDecode("666f6f9f60a89f9f8ab260"), output); |
| 158 | } |
| 159 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 160 | } // namespace |
| 161 | } // namespace test |
| 162 | } // namespace http2 |