Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
safety_tests.cpp
Go to the documentation of this file.
1
#include "gtest/gtest.h"
2
3
#include <string>
4
5
#include "
idna.h
"
6
#include "
ada/idna/limits.h
"
7
#include "
../src/table_store.hpp
"
8
9
TEST
(Safety, RejectsOverlongDomainInput) {
10
// Non-ASCII input over the limit is rejected by to_ascii.
11
std::string huge;
12
while
(huge.size() <=
ada::idna::max_domain_input_bytes
) {
13
huge +=
"\xC3\xA0."
;
// "\u00E0."
14
}
15
EXPECT_TRUE(
ada::idna::to_ascii
(huge).empty());
16
17
std::string out;
18
EXPECT_FALSE(
ada::idna::to_ascii
(huge, out));
19
EXPECT_TRUE(out.empty());
20
21
// to_unicode bounds its input regardless (punycode decoding can expand it).
22
std::string huge_ascii(
ada::idna::max_domain_input_bytes
+ 1,
'a'
);
23
huge_ascii[huge_ascii.size() / 2] =
'.'
;
24
std::string uni_out;
25
EXPECT_FALSE(
ada::idna::to_unicode
(huge_ascii, uni_out));
26
EXPECT_TRUE(uni_out.empty());
27
}
28
29
TEST
(Safety, ToAsciiAcceptsLongAsciiInput) {
30
std::string huge(
ada::idna::max_domain_input_bytes
+ 1,
'A'
);
31
huge[huge.size() / 2] =
'.'
;
32
std::string out;
33
ASSERT_TRUE(
ada::idna::to_ascii
(huge, out));
34
EXPECT_EQ(out.size(), huge.size());
35
EXPECT_EQ(out[0],
'a'
);
36
}
37
38
TEST
(Safety, ToAsciiIdempotentAcrossLimit) {
39
// 2049 x "\u00E0." is 6147 bytes in, 16392 bytes out (over the limit).
40
std::string host;
41
for
(
int
i = 0; i < 2049; i++) host +=
"\xC3\xA0."
;
42
std::string ascii;
43
ASSERT_TRUE(
ada::idna::to_ascii
(host, ascii));
44
ASSERT_GT(ascii.size(),
ada::idna::max_domain_input_bytes
);
45
std::string again;
46
ASSERT_TRUE(
ada::idna::to_ascii
(ascii, again));
47
EXPECT_EQ(again, ascii);
48
}
49
50
TEST
(Safety, RejectsInvalidUtf8) {
51
// Overlong / truncated multi-byte sequences.
52
const
char
bad1[] = {
'\xc3'
,
'\x00'
};
// incomplete 2-byte
53
std::string_view s1(bad1, 1);
54
EXPECT_TRUE(
ada::idna::to_ascii
(s1).empty());
55
56
const
char
bad2[] = {
'\xe2'
,
'\x82'
};
// incomplete 3-byte
57
std::string_view s2(bad2, 2);
58
EXPECT_TRUE(
ada::idna::to_ascii
(s2).empty());
59
60
const
char
bad3[] = {
'\xf0'
,
'\x9f'
,
'\x98'
};
// incomplete 4-byte
61
std::string_view s3(bad3, 3);
62
EXPECT_TRUE(
ada::idna::to_ascii
(s3).empty());
63
}
64
65
TEST
(Safety, ToAsciiBoolOverload) {
66
std::string out;
67
ASSERT_TRUE(
ada::idna::to_ascii
(
"Example.COM"
, out));
68
EXPECT_EQ(out,
"example.com"
);
69
70
ASSERT_TRUE(
71
ada::idna::to_ascii
(
"me\xc3\x9f"
72
"agefactory.ca"
,
73
out));
74
EXPECT_EQ(out,
"xn--meagefactory-m9a.ca"
);
75
}
76
77
TEST
(Safety, ToAsciiToUnicodeRoundTripAscii) {
78
const
char
* cases[] = {
79
"example.com"
,
80
"www.google.com"
,
81
"a.b.c.example.org"
,
82
"xn--mgba3gch31f060k"
,
83
};
84
for
(
const
char
* c : cases) {
85
std::string ascii =
ada::idna::to_ascii
(c);
86
ASSERT_FALSE(ascii.empty()) << c;
87
std::string uni =
ada::idna::to_unicode
(ascii);
88
std::string again =
ada::idna::to_ascii
(uni);
89
EXPECT_EQ(again, ascii) <<
"round-trip failed for "
<< c;
90
}
91
}
92
93
TEST
(Safety, ToAsciiToUnicodeRoundTripUnicode) {
94
// UTF-8 meßagefactory.ca / münchen.de / faß.de
95
const
char
* cases[] = {
96
"me\xc3\x9f"
97
"agefactory.ca"
,
98
"m\xc3\xbc"
99
"nchen.de"
,
100
"fa\xc3\x9f.de"
,
101
};
102
for
(
const
char
* c : cases) {
103
std::string ascii =
ada::idna::to_ascii
(c);
104
ASSERT_FALSE(ascii.empty()) << c;
105
std::string uni =
ada::idna::to_unicode
(ascii);
106
std::string again =
ada::idna::to_ascii
(uni);
107
EXPECT_EQ(again, ascii) <<
"round-trip failed for "
<< c;
108
}
109
}
110
111
TEST
(Safety, TablesReadyAfterUse) {
112
(void)
ada::idna::to_ascii
(
"a.com"
);
113
// After a successful call that needs tables, init should be ready.
114
// (ASCII-only path may skip tables; force a non-ASCII domain.)
115
ASSERT_FALSE(
ada::idna::to_ascii
(
"me\xc3\x9f"
116
"agefactory.ca"
)
117
.empty());
118
EXPECT_TRUE(
ada::idna::tables_are_ready
());
119
}
120
121
TEST
(Safety, IsAlreadyNfc) {
122
ASSERT_TRUE(
ada::idna::ensure_tables
());
123
std::u32string ascii = U
"example.com"
;
124
EXPECT_TRUE(
ada::idna::is_already_nfc
(ascii));
125
// Precomposed e-acute is NFC; e + combining acute is not.
126
std::u32string precomposed = {0xE9};
127
std::u32string decomposed = {0x65, 0x301};
128
EXPECT_TRUE(
ada::idna::is_already_nfc
(precomposed));
129
EXPECT_FALSE(
ada::idna::is_already_nfc
(decomposed));
130
}
131
132
TEST
(Safety, IsAlreadyNfcCompositionExclusions) {
133
ASSERT_TRUE(
ada::idna::ensure_tables
());
134
// A canonical decomposition longer than one code point does not make a
135
// character NFC: composition exclusions and non-starter decompositions are
136
// NFC_Quick_Check=No and must normalize.
137
// U+0958 DEVANAGARI LETTER QA -> U+0915 U+093C (excluded, no recompose)
138
std::u32string qa = {0x0958};
139
EXPECT_FALSE(
ada::idna::is_already_nfc
(qa));
140
ASSERT_TRUE(
ada::idna::normalize
(qa));
141
EXPECT_EQ(qa, std::u32string({0x0915, 0x093C}));
142
143
// U+1F71 GREEK SMALL LETTER ALPHA WITH OXIA -> U+03AC: its decomposition
144
// recomposes to a different primary composite, so U+1F71 is not NFC.
145
std::u32string oxia = {0x1F71};
146
EXPECT_FALSE(
ada::idna::is_already_nfc
(oxia));
147
ASSERT_TRUE(
ada::idna::normalize
(oxia));
148
EXPECT_EQ(oxia, std::u32string({0x03AC}));
149
150
// U+0344 COMBINING GREEK DIALYTIKA TONOS -> U+0308 U+0301 (non-starter
151
// decomposition, stays decomposed).
152
std::u32string dialytika = {0x0344};
153
EXPECT_FALSE(
ada::idna::is_already_nfc
(dialytika));
154
ASSERT_TRUE(
ada::idna::normalize
(dialytika));
155
EXPECT_EQ(dialytika, std::u32string({0x0308, 0x0301}));
156
157
// Genuine primary composites stay on the already-NFC fast path.
158
EXPECT_TRUE(
ada::idna::is_already_nfc
(std::u32string({0x00E9})));
159
EXPECT_TRUE(
ada::idna::is_already_nfc
(std::u32string({0x03AC})));
160
}
161
162
TEST
(Safety, ComposesUnicode16Compositions) {
163
ASSERT_TRUE(
ada::idna::ensure_tables
());
164
// U+1611E U+1611E -> U+16121 GURUNG KHEMA VOWEL SIGN U.
165
std::u32string gurung_khema = {0x1611E, 0x1611E};
166
EXPECT_FALSE(
ada::idna::is_already_nfc
(gurung_khema));
167
ASSERT_TRUE(
ada::idna::normalize
(gurung_khema));
168
EXPECT_EQ(gurung_khema, std::u32string({0x16121}));
169
EXPECT_TRUE(
ada::idna::is_already_nfc
(std::u32string({0x16121})));
170
171
// Chained: U+16D63 U+16D67 -> U+16D69, then U+16D69 U+16D67 -> U+16D6A
172
// KIRAT RAI VOWEL SIGN AU.
173
std::u32string kirat_rai = {0x16D63, 0x16D67, 0x16D67};
174
ASSERT_TRUE(
ada::idna::normalize
(kirat_rai));
175
EXPECT_EQ(kirat_rai, std::u32string({0x16D6A}));
176
177
// A starter and a combining mark: U+105D2 U+0307 -> U+105C9 TODHRI LETTER EI.
178
std::u32string todhri = {0x105D2, 0x0307};
179
ASSERT_TRUE(
ada::idna::normalize
(todhri));
180
EXPECT_EQ(todhri, std::u32string({0x105C9}));
181
182
// A decomposition that begins with a code point composing with the starter
183
// before it: U+16121 is U+1611E U+1611E, so U+1611E U+16121 is not NFC.
184
std::u32string recomposed = {0x1611E, 0x16121};
185
EXPECT_FALSE(
ada::idna::is_already_nfc
(recomposed));
186
ASSERT_TRUE(
ada::idna::normalize
(recomposed));
187
EXPECT_EQ(recomposed, std::u32string({0x16121, 0x1611E}));
188
EXPECT_TRUE(
ada::idna::is_already_nfc
(recomposed));
189
190
// U+16D68 is U+16D67 U+16D67: U+16D63 U+16D68 -> U+16D69 U+16D67 -> U+16D6A.
191
std::u32string kirat_rai_ai = {0x16D63, 0x16D68};
192
EXPECT_FALSE(
ada::idna::is_already_nfc
(kirat_rai_ai));
193
ASSERT_TRUE(
ada::idna::normalize
(kirat_rai_ai));
194
EXPECT_EQ(kirat_rai_ai, std::u32string({0x16D6A}));
195
}
196
197
TEST
(Safety, ComposesHangulLvPlusTrailingJamo) {
198
ASSERT_TRUE(
ada::idna::ensure_tables
());
199
// A precomposed LV syllable (SIndex % TCount == 0) followed by a trailing
200
// jamo composes to the LVT syllable under NFC.
201
// U+AC00 (가, LV) + U+11A8 (ᆨ, T) -> U+AC01 (각)
202
std::u32string lv_plus_t = {0xAC00, 0x11A8};
203
EXPECT_FALSE(
ada::idna::is_already_nfc
(lv_plus_t));
204
ASSERT_TRUE(
ada::idna::normalize
(lv_plus_t));
205
EXPECT_EQ(lv_plus_t, std::u32string({0xAC01}));
206
207
// U+C4D4 (LV) + U+11B6 (T) -> U+C4E3
208
std::u32string lv_plus_t2 = {0xC4D4, 0x11B6};
209
ASSERT_TRUE(
ada::idna::normalize
(lv_plus_t2));
210
EXPECT_EQ(lv_plus_t2, std::u32string({0xC4E3}));
211
}
idna.h
limits.h
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::to_unicode
std::string to_unicode(std::string_view input)
Definition
ada_idna.cpp:6924
ada::idna::normalize
bool normalize(std::u32string &input)
Definition
ada_idna.cpp:5744
ada::idna::ensure_tables
bool ensure_tables() noexcept
Definition
ada_idna.cpp:4956
ada::idna::is_already_nfc
bool is_already_nfc(std::u32string_view input) noexcept
Definition
ada_idna.cpp:5623
ada::idna::max_domain_input_bytes
constexpr size_t max_domain_input_bytes
Definition
limits.h:12
TEST
TEST(Safety, RejectsOverlongDomainInput)
Definition
safety_tests.cpp:9
table_store.hpp