1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_grpc_private/hpack.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_bytes/array.h"
19
20 namespace pw::grpc {
21 namespace {
22
TestIntegerDecode(ConstByteSpan input,int bits,int expected)23 void TestIntegerDecode(ConstByteSpan input, int bits, int expected) {
24 auto result = HpackIntegerDecode(input, bits);
25 ASSERT_TRUE(result.ok());
26 EXPECT_EQ(*result, expected);
27 EXPECT_TRUE(input.empty()); // input has advanced past the integer
28 }
29
TestHuffmanDecode(ConstByteSpan input,std::string_view expected)30 void TestHuffmanDecode(ConstByteSpan input, std::string_view expected) {
31 auto result = HpackHuffmanDecode(input);
32 ASSERT_TRUE(result.ok());
33 EXPECT_EQ(*result, expected);
34 }
35
36 // Integer test cases from RFC 7541 Appendix C.1.
TEST(HpackTest,HpackIntegerDecodeC11)37 TEST(HpackTest, HpackIntegerDecodeC11) {
38 const auto kInput = bytes::Array<0b11101010>();
39 TestIntegerDecode(kInput, /*bits_in_first_byte=*/5, /*expected=*/10);
40 }
TEST(HpackTest,HpackIntegerDecodeC12)41 TEST(HpackTest, HpackIntegerDecodeC12) {
42 const auto kInput = bytes::Array<0b11111111, 0b10011010, 0b00001010>();
43 TestIntegerDecode(kInput, /*bits_in_first_byte=*/5, /*expected=*/1337);
44 }
TEST(HpackTest,HpackIntegerDecodeC13)45 TEST(HpackTest, HpackIntegerDecodeC13) {
46 const auto kInput = bytes::Array<0b00101010>();
47 TestIntegerDecode(kInput, /*bits_in_first_byte=*/8, /*expected=*/42);
48 }
49
50 // Huffman test cases from RFC 7541 Appendix C.4.
51 // clang-format off
52 const auto kHuffmanC41 = bytes::Array<0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff>();
53 const auto kHuffmanC42 = bytes::Array<0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf>();
54 const auto kHuffmanC43a = bytes::Array<0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f>();
55 const auto kHuffmanC43b = bytes::Array<0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf>();
56 // clang-format on
57
TEST(HpackTest,HpackHuffmanDecodeC41)58 TEST(HpackTest, HpackHuffmanDecodeC41) {
59 TestHuffmanDecode(kHuffmanC41, "www.example.com");
60 }
TEST(HpackTest,HpackHuffmanDecodeC42)61 TEST(HpackTest, HpackHuffmanDecodeC42) {
62 TestHuffmanDecode(kHuffmanC42, "no-cache");
63 }
TEST(HpackTest,HpackHuffmanDecodeC43a)64 TEST(HpackTest, HpackHuffmanDecodeC43a) {
65 TestHuffmanDecode(kHuffmanC43a, "custom-key");
66 }
TEST(HpackTest,HpackHuffmanDecodeC43b)67 TEST(HpackTest, HpackHuffmanDecodeC43b) {
68 TestHuffmanDecode(kHuffmanC43b, "custom-value");
69 }
70
71 // Header field test cases from RFC 7541 Appendix C.
TEST(HpackTest,HpackParseRequestHeadersFoundIndexedSlash)72 TEST(HpackTest, HpackParseRequestHeadersFoundIndexedSlash) {
73 // Appendix C.3.1.
74 const auto kInput = bytes::Array<0x84>();
75 auto result = HpackParseRequestHeaders(kInput);
76 ASSERT_TRUE(result.ok());
77 EXPECT_EQ(*result, "/");
78 }
TEST(HpackTest,HpackParseRequestHeadersFoundIndexedHtml)79 TEST(HpackTest, HpackParseRequestHeadersFoundIndexedHtml) {
80 // Appendix C.3.3.
81 const auto kInput = bytes::Array<0x85>();
82 auto result = HpackParseRequestHeaders(kInput);
83 ASSERT_TRUE(result.ok());
84 EXPECT_EQ(*result, "/index.html");
85 }
TEST(HpackTest,HpackParseRequestHeadersFoundNotIndexed)86 TEST(HpackTest, HpackParseRequestHeadersFoundNotIndexed) {
87 // clang-format off
88 const auto kInput = bytes::Array<
89 // Appendix C.2.1.
90 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0d, 0x63,
91 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72,
92 // Appendix C.2.3.
93 0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x06, 0x73, 0x65, 0x63,
94 0x72, 0x65, 0x74,
95 // Appendix C.2.2.
96 0x04, 0x0c, 0x2f, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x61, 0x74, 0x68
97 >();
98 // clang-format on
99 auto result = HpackParseRequestHeaders(kInput);
100 ASSERT_TRUE(result.ok());
101 EXPECT_EQ(*result, "/sample/path");
102 }
TEST(HpackTest,HpackParseRequestHeadersNotFound)103 TEST(HpackTest, HpackParseRequestHeadersNotFound) {
104 // clang-format off
105 const auto kInput = bytes::Array<
106 // Appendix C.2.1.
107 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0d, 0x63,
108 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72,
109 // Appendix C.2.3.
110 0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x06, 0x73, 0x65, 0x63,
111 0x72, 0x65, 0x74
112 >();
113 // clang-format on
114 auto result = HpackParseRequestHeaders(kInput);
115 ASSERT_FALSE(result.ok());
116 EXPECT_EQ(result.status().code(), PW_STATUS_NOT_FOUND);
117 }
118
119 } // namespace
120 } // namespace pw::grpc
121