Ada 4.1.1
Fast spec-compliant URL parser
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"
15// detail::bswap_inplace is exercised for BE host conversion regression.
16
17namespace {
18
19// Raw DEFLATE of "hello" (zlib.compressobj(wbits=-15)).
20constexpr uint8_t kHelloRaw[] = {203, 72, 205, 201, 201, 7, 0};
21
22} // namespace
23
24TEST(RawInflate, FixedHuffmanHello) {
25 uint8_t out[16]{};
27 sizeof(out));
28 ASSERT_EQ(n, 5u);
29 EXPECT_EQ(std::string(reinterpret_cast<char*>(out), n), "hello");
30}
31
32TEST(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
39TEST(RawInflate, RejectsTinyOutputBuffer) {
40 uint8_t out[2]{};
41 EXPECT_EQ(
43 0u);
44}
45
46TEST(RawInflate, HuffBuildFixedLitLen) {
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
60TEST(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.
73TEST(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));
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);
86 EXPECT_EQ(w32, 0x12345678u);
87}
88
89TEST(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}
size_t inflate_raw(const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_cap)
Definition ada_idna.cpp:395
void bswap_inplace(T *data, size_t count) noexcept
bool tables_are_ready() noexcept
std::string to_ascii(std::string_view ut8_string)
bool ensure_tables() noexcept
bool valid_name_code_point(char32_t code_point, bool first)
TEST(RawInflate, FixedHuffmanHello)
bool build(const uint8_t *lens, int n)
Definition ada_idna.cpp:300
int counts[MAXBITS+1]
Definition ada_idna.cpp:291