blob: d729d0ec138264b21d9c4daafae012d28d54b7c1 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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 teamfd50a402018-12-07 22:54:05 -05007#include "testing/gtest/include/gtest/gtest.h"
QUICHE teamfd50a402018-12-07 22:54:05 -05008#include "net/third_party/quiche/src/http2/platform/api/http2_string_utils.h"
bncf0d86a12019-12-13 12:58:44 -08009#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050010
11namespace http2 {
12namespace {
13
14TEST(HuffmanEncoderTest, SpecRequestExamples) {
bnc47904002019-08-16 11:49:48 -070015 std::string test_table[] = {
QUICHE teamfd50a402018-12-07 22:54:05 -050016 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 };
bncf0d86a12019-12-13 12:58:44 -080025 for (size_t i = 0; i != QUICHE_ARRAYSIZE(test_table); i += 2) {
bnc47904002019-08-16 11:49:48 -070026 const std::string& huffman_encoded(test_table[i]);
27 const std::string& plain_string(test_table[i + 1]);
QUICHE teamfd50a402018-12-07 22:54:05 -050028 EXPECT_EQ(ExactHuffmanSize(plain_string), huffman_encoded.size());
29 EXPECT_EQ(BoundedHuffmanSize(plain_string), huffman_encoded.size());
bnc47904002019-08-16 11:49:48 -070030 std::string buffer;
QUICHE teamfd50a402018-12-07 22:54:05 -050031 buffer.reserve();
32 HuffmanEncode(plain_string, &buffer);
33 EXPECT_EQ(buffer, huffman_encoded) << "Error encoding " << plain_string;
34 }
35}
36
37TEST(HuffmanEncoderTest, SpecResponseExamples) {
38 // clang-format off
bnc47904002019-08-16 11:49:48 -070039 std::string test_table[] = {
QUICHE teamfd50a402018-12-07 22:54:05 -050040 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
bncf0d86a12019-12-13 12:58:44 -080056 for (size_t i = 0; i != QUICHE_ARRAYSIZE(test_table); i += 2) {
bnc47904002019-08-16 11:49:48 -070057 const std::string& huffman_encoded(test_table[i]);
58 const std::string& plain_string(test_table[i + 1]);
QUICHE teamfd50a402018-12-07 22:54:05 -050059 EXPECT_EQ(ExactHuffmanSize(plain_string), huffman_encoded.size());
60 EXPECT_EQ(BoundedHuffmanSize(plain_string), huffman_encoded.size());
bnc47904002019-08-16 11:49:48 -070061 std::string buffer;
QUICHE teamfd50a402018-12-07 22:54:05 -050062 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
70TEST(HuffmanEncoderTest, EncodedSizeAgreesWithEncodeString) {
bnc47904002019-08-16 11:49:48 -070071 std::string test_table[] = {
QUICHE teamfd50a402018-12-07 22:54:05 -050072 "",
73 "Mon, 21 Oct 2013 20:13:21 GMT",
74 "https://www.example.com",
75 "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
bnc47904002019-08-16 11:49:48 -070076 std::string(1, '\0'),
77 std::string("foo\0bar", 7),
78 std::string(256, '\0'),
QUICHE teamfd50a402018-12-07 22:54:05 -050079 };
80 // Modify last |test_table| entry to cover all codes.
81 for (size_t i = 0; i != 256; ++i) {
bncf0d86a12019-12-13 12:58:44 -080082 test_table[QUICHE_ARRAYSIZE(test_table) - 1][i] = static_cast<char>(i);
QUICHE teamfd50a402018-12-07 22:54:05 -050083 }
84
bncf0d86a12019-12-13 12:58:44 -080085 for (size_t i = 0; i != QUICHE_ARRAYSIZE(test_table); ++i) {
bnc47904002019-08-16 11:49:48 -070086 const std::string& plain_string = test_table[i];
87 std::string huffman_encoded;
QUICHE teamfd50a402018-12-07 22:54:05 -050088 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