QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright (c) 2018 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/huffman/hpack_huffman_encoder.h" |
| 6 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 7 | #include "testing/gtest/include/gtest/gtest.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/http2/platform/api/http2_string_utils.h" |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 10 | |
| 11 | namespace http2 { |
| 12 | namespace { |
| 13 | |
| 14 | TEST(HuffmanEncoderTest, SpecRequestExamples) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 15 | std::string test_table[] = { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 16 | Http2HexDecode("f1e3c2e5f23a6ba0ab90f4ff"), |
| 17 | "www.example.com", |
| 18 | Http2HexDecode("a8eb10649cbf"), |
| 19 | "no-cache", |
| 20 | Http2HexDecode("25a849e95ba97d7f"), |
| 21 | "custom-key", |
| 22 | Http2HexDecode("25a849e95bb8e8b4bf"), |
| 23 | "custom-value", |
| 24 | }; |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 25 | for (size_t i = 0; i != QUICHE_ARRAYSIZE(test_table); i += 2) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 26 | const std::string& huffman_encoded(test_table[i]); |
| 27 | const std::string& plain_string(test_table[i + 1]); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 28 | EXPECT_EQ(ExactHuffmanSize(plain_string), huffman_encoded.size()); |
| 29 | EXPECT_EQ(BoundedHuffmanSize(plain_string), huffman_encoded.size()); |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 30 | std::string buffer; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 31 | buffer.reserve(); |
| 32 | HuffmanEncode(plain_string, &buffer); |
| 33 | EXPECT_EQ(buffer, huffman_encoded) << "Error encoding " << plain_string; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | TEST(HuffmanEncoderTest, SpecResponseExamples) { |
| 38 | // clang-format off |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 39 | std::string test_table[] = { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 40 | Http2HexDecode("6402"), |
| 41 | "302", |
| 42 | Http2HexDecode("aec3771a4b"), |
| 43 | "private", |
| 44 | Http2HexDecode("d07abe941054d444a8200595040b8166" |
| 45 | "e082a62d1bff"), |
| 46 | "Mon, 21 Oct 2013 20:13:21 GMT", |
| 47 | Http2HexDecode("9d29ad171863c78f0b97c8e9ae82ae43" |
| 48 | "d3"), |
| 49 | "https://www.example.com", |
| 50 | Http2HexDecode("94e7821dd7f2e6c7b335dfdfcd5b3960" |
| 51 | "d5af27087f3672c1ab270fb5291f9587" |
| 52 | "316065c003ed4ee5b1063d5007"), |
| 53 | "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
| 54 | }; |
| 55 | // clang-format on |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 56 | for (size_t i = 0; i != QUICHE_ARRAYSIZE(test_table); i += 2) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 57 | const std::string& huffman_encoded(test_table[i]); |
| 58 | const std::string& plain_string(test_table[i + 1]); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 59 | EXPECT_EQ(ExactHuffmanSize(plain_string), huffman_encoded.size()); |
| 60 | EXPECT_EQ(BoundedHuffmanSize(plain_string), huffman_encoded.size()); |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 61 | std::string buffer; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 62 | buffer.reserve(huffman_encoded.size()); |
| 63 | const size_t capacity = buffer.capacity(); |
| 64 | HuffmanEncode(plain_string, &buffer); |
| 65 | EXPECT_EQ(buffer, huffman_encoded) << "Error encoding " << plain_string; |
| 66 | EXPECT_EQ(capacity, buffer.capacity()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | TEST(HuffmanEncoderTest, EncodedSizeAgreesWithEncodeString) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 71 | std::string test_table[] = { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 72 | "", |
| 73 | "Mon, 21 Oct 2013 20:13:21 GMT", |
| 74 | "https://www.example.com", |
| 75 | "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 76 | std::string(1, '\0'), |
| 77 | std::string("foo\0bar", 7), |
| 78 | std::string(256, '\0'), |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 79 | }; |
| 80 | // Modify last |test_table| entry to cover all codes. |
| 81 | for (size_t i = 0; i != 256; ++i) { |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 82 | test_table[QUICHE_ARRAYSIZE(test_table) - 1][i] = static_cast<char>(i); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 83 | } |
| 84 | |
bnc | f0d86a1 | 2019-12-13 12:58:44 -0800 | [diff] [blame] | 85 | for (size_t i = 0; i != QUICHE_ARRAYSIZE(test_table); ++i) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 86 | const std::string& plain_string = test_table[i]; |
| 87 | std::string huffman_encoded; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 88 | HuffmanEncode(plain_string, &huffman_encoded); |
| 89 | EXPECT_EQ(huffman_encoded.size(), ExactHuffmanSize(plain_string)); |
| 90 | EXPECT_LE(BoundedHuffmanSize(plain_string), plain_string.size()); |
| 91 | EXPECT_LE(BoundedHuffmanSize(plain_string), ExactHuffmanSize(plain_string)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } // namespace |
| 96 | } // namespace http2 |