Skip to content

Commit fd5c84a

Browse files
committed
Convert regression tests for XPACK into Catch based unit tests
1 parent 70ebf86 commit fd5c84a

File tree

4 files changed

+138
-105
lines changed

4 files changed

+138
-105
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ proxy/hdrs/test_mime
102102
proxy/hdrs/test_proxy_hdrs
103103
proxy/hdrs/test_hdr_heap
104104
proxy/hdrs/test_Huffmancode
105+
proxy/hdrs/test_XPACK
105106
proxy/http/test_proxy_http
106107
proxy/http2/test_Http2DependencyTree
107108
proxy/http2/test_HPACK

proxy/hdrs/Makefile.am

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ check_PROGRAMS = \
6969
test_mime \
7070
test_proxy_hdrs \
7171
test_hdr_heap \
72-
test_Huffmancode
72+
test_Huffmancode \
73+
test_XPACK
7374

7475
TESTS = $(check_PROGRAMS)
7576

@@ -133,6 +134,18 @@ test_Huffmancode_SOURCES = \
133134
HuffmanCodec.cc \
134135
HuffmanCodec.h
135136

137+
test_XPACK_CPPFLAGS = \
138+
$(AM_CPPFLAGS) \
139+
-I$(abs_top_srcdir)/tests/include
140+
141+
test_XPACK_LDADD = \
142+
$(top_builddir)/src/tscore/libtscore.la \
143+
$(top_builddir)/src/tscpp/util/libtscpputil.la
144+
145+
test_XPACK_SOURCES = \
146+
unit_tests/test_XPACK.cc \
147+
HuffmanCodec.cc \
148+
XPACK.cc
136149

137150
#test_UNUSED_SOURCES = \
138151
# test_urlhash.cc
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/** @file
2+
3+
Catch based unit tests for XPACK
4+
5+
@section license License
6+
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing, software
18+
distributed under the License is distributed on an "AS IS" BASIS,
19+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
See the License for the specific language governing permissions and
21+
limitations under the License.
22+
*/
23+
24+
#define CATCH_CONFIG_MAIN
25+
26+
#include "catch.hpp"
27+
28+
#include "XPACK.h"
29+
#include "HuffmanCodec.h"
30+
31+
static constexpr int BUFSIZE_FOR_REGRESSION_TEST = 128;
32+
33+
TEST_CASE("XPACK_Integer", "[xpack]")
34+
{
35+
// [RFC 7541] C.1. Integer Representation Examples
36+
static const struct {
37+
uint32_t raw_integer;
38+
uint8_t *encoded_field;
39+
int encoded_field_len;
40+
int prefix;
41+
} integer_test_case[] = {{10, (uint8_t *)"\x0a", 1, 5}, {1337, (uint8_t *)"\x1F\x9A\x0A", 3, 5}, {42, (uint8_t *)R"(*)", 1, 8}};
42+
43+
SECTION("Encoding")
44+
{
45+
for (const auto &i : integer_test_case) {
46+
uint8_t buf[BUFSIZE_FOR_REGRESSION_TEST] = {0};
47+
48+
int len = xpack_encode_integer(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, i.raw_integer, i.prefix);
49+
50+
REQUIRE(len > 0);
51+
REQUIRE(len == i.encoded_field_len);
52+
REQUIRE(memcmp(buf, i.encoded_field, len) == 0);
53+
}
54+
}
55+
56+
SECTION("Decoding")
57+
{
58+
for (const auto &i : integer_test_case) {
59+
uint64_t actual = 0;
60+
int len = xpack_decode_integer(actual, i.encoded_field, i.encoded_field + i.encoded_field_len, i.prefix);
61+
62+
REQUIRE(len == i.encoded_field_len);
63+
REQUIRE(actual == i.raw_integer);
64+
}
65+
}
66+
}
67+
68+
TEST_CASE("XPACK_String", "[xpack]")
69+
{
70+
// Example: custom-key: custom-header
71+
const static struct {
72+
char *raw_string;
73+
uint32_t raw_string_len;
74+
uint8_t *encoded_field;
75+
int encoded_field_len;
76+
} string_test_case[] = {{(char *)"", 0,
77+
(uint8_t *)"\x0"
78+
"",
79+
1},
80+
{(char *)"custom-key", 10,
81+
(uint8_t *)"\xA"
82+
"custom-key",
83+
11},
84+
{(char *)"", 0,
85+
(uint8_t *)"\x80"
86+
"",
87+
1},
88+
{(char *)"custom-key", 10,
89+
(uint8_t *)"\x88"
90+
"\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f",
91+
9}};
92+
93+
SECTION("Encoding")
94+
{
95+
// FIXME Current encoder support only huffman conding.
96+
for (unsigned int i = 2; i < sizeof(string_test_case) / sizeof(string_test_case[0]); i++) {
97+
uint8_t buf[BUFSIZE_FOR_REGRESSION_TEST] = {0};
98+
int len = xpack_encode_string(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, string_test_case[i].raw_string,
99+
string_test_case[i].raw_string_len);
100+
101+
REQUIRE(len > 0);
102+
REQUIRE(len == string_test_case[i].encoded_field_len);
103+
REQUIRE(memcmp(buf, string_test_case[i].encoded_field, len) == 0);
104+
}
105+
}
106+
107+
SECTION("Decoding")
108+
{
109+
// Decoding string needs huffman tree
110+
hpack_huffman_init();
111+
112+
for (const auto &i : string_test_case) {
113+
Arena arena;
114+
char *actual = nullptr;
115+
uint64_t actual_len = 0;
116+
int len = xpack_decode_string(arena, &actual, actual_len, i.encoded_field, i.encoded_field + i.encoded_field_len);
117+
118+
REQUIRE(len == i.encoded_field_len);
119+
REQUIRE(actual_len == i.raw_string_len);
120+
REQUIRE(memcmp(actual, i.raw_string, actual_len) == 0);
121+
}
122+
}
123+
}

proxy/http2/RegressionHPACK.cc

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,6 @@ const static int MAX_TABLE_SIZE = 4096;
4141
* *
4242
***********************************************************************************/
4343

44-
// [RFC 7541] C.1. Integer Representation Examples
45-
const static struct {
46-
uint32_t raw_integer;
47-
uint8_t *encoded_field;
48-
int encoded_field_len;
49-
int prefix;
50-
} integer_test_case[] = {{10, (uint8_t *)"\x0A", 1, 5}, {1337, (uint8_t *)"\x1F\x9A\x0A", 3, 5}, {42, (uint8_t *)R"(*)", 1, 8}};
51-
52-
// Example: custom-key: custom-header
53-
const static struct {
54-
char *raw_string;
55-
uint32_t raw_string_len;
56-
uint8_t *encoded_field;
57-
int encoded_field_len;
58-
} string_test_case[] = {{(char *)"", 0,
59-
(uint8_t *)"\x0"
60-
"",
61-
1},
62-
{(char *)"custom-key", 10,
63-
(uint8_t *)"\xA"
64-
"custom-key",
65-
11},
66-
{(char *)"", 0,
67-
(uint8_t *)"\x80"
68-
"",
69-
1},
70-
{(char *)"custom-key", 10,
71-
(uint8_t *)"\x88"
72-
"\x25\xa8\x49\xe9\x5b\xa9\x7d\x7f",
73-
9}};
74-
7544
// [RFC 7541] C.2.4. Indexed Header Field
7645
const static struct {
7746
int index;
@@ -317,43 +286,6 @@ const static struct {
317286
* *
318287
***********************************************************************************/
319288

320-
REGRESSION_TEST(HPACK_EncodeInteger)(RegressionTest *t, int, int *pstatus)
321-
{
322-
TestBox box(t, pstatus);
323-
box = REGRESSION_TEST_PASSED;
324-
uint8_t buf[BUFSIZE_FOR_REGRESSION_TEST];
325-
326-
for (const auto &i : integer_test_case) {
327-
memset(buf, 0, BUFSIZE_FOR_REGRESSION_TEST);
328-
329-
int len = xpack_encode_integer(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, i.raw_integer, i.prefix);
330-
331-
box.check(len == i.encoded_field_len, "encoded length was %d, expecting %d", len, i.encoded_field_len);
332-
box.check(len > 0 && memcmp(buf, i.encoded_field, len) == 0, "encoded value was invalid");
333-
}
334-
}
335-
336-
REGRESSION_TEST(HPACK_EncodeString)(RegressionTest *t, int, int *pstatus)
337-
{
338-
TestBox box(t, pstatus);
339-
box = REGRESSION_TEST_PASSED;
340-
341-
uint8_t buf[BUFSIZE_FOR_REGRESSION_TEST];
342-
int len;
343-
344-
// FIXME Current encoder support only huffman conding.
345-
for (unsigned int i = 2; i < sizeof(string_test_case) / sizeof(string_test_case[0]); i++) {
346-
memset(buf, 0, BUFSIZE_FOR_REGRESSION_TEST);
347-
348-
len = xpack_encode_string(buf, buf + BUFSIZE_FOR_REGRESSION_TEST, string_test_case[i].raw_string,
349-
string_test_case[i].raw_string_len);
350-
351-
box.check(len == string_test_case[i].encoded_field_len, "encoded length was %d, expecting %d", len,
352-
string_test_case[i].encoded_field_len);
353-
box.check(len > 0 && memcmp(buf, string_test_case[i].encoded_field, len) == 0, "encoded string was invalid");
354-
}
355-
}
356-
357289
REGRESSION_TEST(HPACK_EncodeIndexedHeaderField)(RegressionTest *t, int, int *pstatus)
358290
{
359291
TestBox box(t, pstatus);
@@ -467,42 +399,6 @@ REGRESSION_TEST(HPACK_Encode)(RegressionTest *t, int, int *pstatus)
467399
}
468400
}
469401

470-
REGRESSION_TEST(HPACK_DecodeInteger)(RegressionTest *t, int, int *pstatus)
471-
{
472-
TestBox box(t, pstatus);
473-
box = REGRESSION_TEST_PASSED;
474-
475-
uint64_t actual;
476-
477-
for (const auto &i : integer_test_case) {
478-
int len = xpack_decode_integer(actual, i.encoded_field, i.encoded_field + i.encoded_field_len, i.prefix);
479-
480-
box.check(len == i.encoded_field_len, "decoded length was %d, expecting %d", len, i.encoded_field_len);
481-
box.check(actual == i.raw_integer, "decoded value was %" PRIu64 ", expected %d", actual, i.raw_integer);
482-
}
483-
}
484-
485-
REGRESSION_TEST(HPACK_DecodeString)(RegressionTest *t, int, int *pstatus)
486-
{
487-
TestBox box(t, pstatus);
488-
box = REGRESSION_TEST_PASSED;
489-
490-
Arena arena;
491-
char *actual = nullptr;
492-
uint64_t actual_len = 0;
493-
494-
hpack_huffman_init();
495-
496-
for (const auto &i : string_test_case) {
497-
int len = xpack_decode_string(arena, &actual, actual_len, i.encoded_field, i.encoded_field + i.encoded_field_len);
498-
499-
box.check(len == i.encoded_field_len, "decoded length was %d, expecting %d", len, i.encoded_field_len);
500-
box.check(actual_len == i.raw_string_len, "length of decoded string was %" PRIu64 ", expecting %d", actual_len,
501-
i.raw_string_len);
502-
box.check(memcmp(actual, i.raw_string, actual_len) == 0, "decoded string was invalid");
503-
}
504-
}
505-
506402
REGRESSION_TEST(HPACK_DecodeIndexedHeaderField)(RegressionTest *t, int, int *pstatus)
507403
{
508404
TestBox box(t, pstatus);

0 commit comments

Comments
 (0)