Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
raw_inflate_tests.cpp
Go to the documentation of this file.
1
// Regression tests for the in-tree raw DEFLATE inflater (RFC 1951).
2
// These TUs are built under the same pedantic flags as ada's g++-12 CI
3
// (-Werror -Wextra -Wno-unused-parameter -Wimplicit-fallthrough) so that
4
// unused-but-set locals in Huffman table construction cannot reappear.
5
#include "gtest/gtest.h"
6
7
#include <cstdint>
8
#include <cstring>
9
#include <string>
10
#include <vector>
11
12
#include "
idna.h
"
13
#include "
../src/raw_inflate.hpp
"
14
#include "
../src/table_store.hpp
"
15
// detail::bswap_inplace is exercised for BE host conversion regression.
16
17
namespace
{
18
19
// Raw DEFLATE of "hello" (zlib.compressobj(wbits=-15)).
20
constexpr
uint8_t
kHelloRaw
[] = {203, 72, 205, 201, 201, 7, 0};
21
22
}
// namespace
23
24
TEST
(RawInflate, FixedHuffmanHello) {
25
uint8_t out[16]{};
26
size_t
n =
ada::idna::deflate::inflate_raw
(
kHelloRaw
,
sizeof
(
kHelloRaw
), out,
27
sizeof
(out));
28
ASSERT_EQ(n, 5u);
29
EXPECT_EQ(std::string(
reinterpret_cast<
char
*
>
(out), n),
"hello"
);
30
}
31
32
TEST
(RawInflate, RejectsTruncatedInput) {
33
uint8_t out[16]{};
34
// Only the first byte of the hello stream.
35
EXPECT_EQ(
ada::idna::deflate::inflate_raw
(
kHelloRaw
, 1, out,
sizeof
(out)),
36
0u);
37
}
38
39
TEST
(RawInflate, RejectsTinyOutputBuffer) {
40
uint8_t out[2]{};
41
EXPECT_EQ(
42
ada::idna::deflate::inflate_raw
(
kHelloRaw
,
sizeof
(
kHelloRaw
), out, 2),
43
0u);
44
}
45
46
TEST
(RawInflate, HuffBuildFixedLitLen) {
47
ada::idna::deflate::Huff
h;
48
uint8_t lens[288];
49
for
(
int
i = 0; i <= 143; i++) lens[i] = 8;
50
for
(
int
i = 144; i <= 255; i++) lens[i] = 9;
51
for
(
int
i = 256; i <= 279; i++) lens[i] = 7;
52
for
(
int
i = 280; i <= 287; i++) lens[i] = 8;
53
ASSERT_TRUE(h.
build
(lens, 288));
54
EXPECT_GT(h.
max_bits
, 0);
55
EXPECT_EQ(h.
counts
[8], 144 + 8);
// 0-143 and 280-287
56
EXPECT_EQ(h.
counts
[9], 112);
// 144-255
57
EXPECT_EQ(h.
counts
[7], 24);
// 256-279
58
}
59
60
TEST
(RawInflate, TableBlobInflatesViaEnsureTables) {
61
ASSERT_TRUE(
ada::idna::ensure_tables
());
62
EXPECT_TRUE(
ada::idna::tables_are_ready
());
63
// Non-ASCII path exercises mapping tables produced by inflate.
64
std::string ascii =
ada::idna::to_ascii
(
65
"me\xc3\x9f"
66
"agefactory.ca"
);
67
ASSERT_FALSE(ascii.empty());
68
EXPECT_EQ(ascii,
"xn--meagefactory-m9a.ca"
);
69
}
70
71
// Regression: table_blob is little-endian. On big-endian hosts (s390x) we must
72
// bswap multi-byte sections after inflate or IDNA lookups fail.
73
TEST
(RawInflate, LittleEndianWordsConvertToHost) {
74
// LE memory image of 0x1234 and 0xABCD.
75
uint8_t le_bytes[4] = {0x34, 0x12, 0xCD, 0xAB};
76
uint16_t words[2];
77
std::memcpy(words, le_bytes,
sizeof
(words));
78
ada::idna::detail::bswap_inplace
(words, 2);
79
EXPECT_EQ(words[0], 0x1234);
80
EXPECT_EQ(words[1], 0xABCD);
81
82
uint8_t le32[4] = {0x78, 0x56, 0x34, 0x12};
83
uint32_t w32;
84
std::memcpy(&w32, le32, 4);
85
ada::idna::detail::bswap_inplace
(&w32, 1);
86
EXPECT_EQ(w32, 0x12345678u);
87
}
88
89
TEST
(RawInflate, HostEndianTablesServeUnicodeIdna) {
90
// Multi-byte table-backed mapping / normalization / identifier ranges.
91
// (UTS #46 non-transitional: U+00DF is not mapped to "ss" alone as a label.)
92
EXPECT_EQ(
ada::idna::to_ascii
(
"\xc3\x9f.com"
),
"xn--zca.com"
);
93
EXPECT_EQ(
ada::idna::to_ascii
(
"m\xc3\xbc"
94
"nchen.de"
),
95
"xn--mnchen-3ya.de"
);
96
EXPECT_EQ(
ada::idna::to_ascii
(
"me\xc3\x9f"
97
"agefactory.ca"
),
98
"xn--meagefactory-m9a.ca"
);
99
EXPECT_TRUE(
ada::idna::valid_name_code_point
(U
'a'
,
true
));
100
EXPECT_TRUE(
ada::idna::valid_name_code_point
(U
'\u03b1'
,
true
));
// alpha
101
}
idna.h
ada::idna::deflate::inflate_raw
size_t inflate_raw(const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_cap)
Definition
ada_idna.cpp:395
ada::idna::detail::bswap_inplace
void bswap_inplace(T *data, size_t count) noexcept
Definition
ada_idna.cpp:4695
ada::idna::tables_are_ready
bool tables_are_ready() noexcept
Definition
ada_idna.cpp:4951
ada::idna::to_ascii
std::string to_ascii(std::string_view ut8_string)
Definition
ada_idna.cpp:6695
ada::idna::ensure_tables
bool ensure_tables() noexcept
Definition
ada_idna.cpp:4956
ada::idna::valid_name_code_point
bool valid_name_code_point(char32_t code_point, bool first)
Definition
ada_idna.cpp:6972
anonymous_namespace{raw_inflate_tests.cpp}::kHelloRaw
constexpr uint8_t kHelloRaw[]
Definition
raw_inflate_tests.cpp:20
raw_inflate.hpp
TEST
TEST(RawInflate, FixedHuffmanHello)
Definition
raw_inflate_tests.cpp:24
ada::idna::deflate::Huff
Definition
ada_idna.cpp:286
ada::idna::deflate::Huff::max_bits
int max_bits
Definition
ada_idna.cpp:295
ada::idna::deflate::Huff::build
bool build(const uint8_t *lens, int n)
Definition
ada_idna.cpp:300
ada::idna::deflate::Huff::counts
int counts[MAXBITS+1]
Definition
ada_idna.cpp:291
table_store.hpp