Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "alloc-util.h"
4 : #include "string-util.h"
5 : #include "strv.h"
6 : #include "utf8.h"
7 : #include "util.h"
8 :
9 1 : static void test_utf8_is_printable(void) {
10 1 : log_info("/* %s */", __func__);
11 :
12 1 : assert_se(utf8_is_printable("ascii is valid\tunicode", 22));
13 1 : assert_se(utf8_is_printable("\342\204\242", 3));
14 1 : assert_se(!utf8_is_printable("\341\204", 2));
15 1 : assert_se(utf8_is_printable("ąę", 4));
16 1 : }
17 :
18 1 : static void test_utf8_is_valid(void) {
19 1 : log_info("/* %s */", __func__);
20 :
21 1 : assert_se(utf8_is_valid("ascii is valid unicode"));
22 1 : assert_se(utf8_is_valid("\342\204\242"));
23 1 : assert_se(!utf8_is_valid("\341\204"));
24 1 : }
25 :
26 1 : static void test_ascii_is_valid(void) {
27 1 : log_info("/* %s */", __func__);
28 :
29 1 : assert_se( ascii_is_valid("alsdjf\t\vbarr\nba z"));
30 1 : assert_se(!ascii_is_valid("\342\204\242"));
31 1 : assert_se(!ascii_is_valid("\341\204"));
32 1 : }
33 :
34 1 : static void test_ascii_is_valid_n(void) {
35 1 : log_info("/* %s */", __func__);
36 :
37 1 : assert_se( ascii_is_valid_n("alsdjf\t\vbarr\nba z", 17));
38 1 : assert_se( ascii_is_valid_n("alsdjf\t\vbarr\nba z", 16));
39 1 : assert_se(!ascii_is_valid_n("alsdjf\t\vbarr\nba z", 18));
40 1 : assert_se(!ascii_is_valid_n("\342\204\242", 3));
41 1 : assert_se(!ascii_is_valid_n("\342\204\242", 2));
42 1 : assert_se(!ascii_is_valid_n("\342\204\242", 1));
43 1 : assert_se( ascii_is_valid_n("\342\204\242", 0));
44 1 : }
45 :
46 1 : static void test_utf8_encoded_valid_unichar(void) {
47 1 : log_info("/* %s */", __func__);
48 :
49 1 : assert_se(utf8_encoded_valid_unichar("\342\204\242", 1) == -EINVAL); /* truncated */
50 1 : assert_se(utf8_encoded_valid_unichar("\342\204\242", 2) == -EINVAL); /* truncated */
51 1 : assert_se(utf8_encoded_valid_unichar("\342\204\242", 3) == 3);
52 1 : assert_se(utf8_encoded_valid_unichar("\342\204\242", 4) == 3);
53 1 : assert_se(utf8_encoded_valid_unichar("\302\256", 1) == -EINVAL); /* truncated */
54 1 : assert_se(utf8_encoded_valid_unichar("\302\256", 2) == 2);
55 1 : assert_se(utf8_encoded_valid_unichar("\302\256", 3) == 2);
56 1 : assert_se(utf8_encoded_valid_unichar("\302\256", (size_t) -1) == 2);
57 1 : assert_se(utf8_encoded_valid_unichar("a", 1) == 1);
58 1 : assert_se(utf8_encoded_valid_unichar("a", 2) == 1);
59 1 : assert_se(utf8_encoded_valid_unichar("\341\204", 1) == -EINVAL); /* truncated, potentially valid */
60 1 : assert_se(utf8_encoded_valid_unichar("\341\204", 2) == -EINVAL); /* truncated, potentially valid */
61 1 : assert_se(utf8_encoded_valid_unichar("\341\204", 3) == -EINVAL);
62 1 : assert_se(utf8_encoded_valid_unichar("\341\204\341\204", 4) == -EINVAL);
63 1 : assert_se(utf8_encoded_valid_unichar("\341\204\341\204", 5) == -EINVAL);
64 1 : }
65 :
66 1 : static void test_utf8_escape_invalid(void) {
67 1 : _cleanup_free_ char *p1, *p2, *p3;
68 :
69 1 : log_info("/* %s */", __func__);
70 :
71 1 : p1 = utf8_escape_invalid("goo goo goo");
72 1 : puts(p1);
73 1 : assert_se(utf8_is_valid(p1));
74 :
75 1 : p2 = utf8_escape_invalid("\341\204\341\204");
76 1 : puts(p2);
77 1 : assert_se(utf8_is_valid(p2));
78 :
79 1 : p3 = utf8_escape_invalid("\341\204");
80 1 : puts(p3);
81 1 : assert_se(utf8_is_valid(p3));
82 1 : }
83 :
84 1 : static void test_utf8_escape_non_printable(void) {
85 1 : _cleanup_free_ char *p1, *p2, *p3, *p4, *p5, *p6;
86 :
87 1 : log_info("/* %s */", __func__);
88 :
89 1 : p1 = utf8_escape_non_printable("goo goo goo");
90 1 : puts(p1);
91 1 : assert_se(utf8_is_valid(p1));
92 :
93 1 : p2 = utf8_escape_non_printable("\341\204\341\204");
94 1 : puts(p2);
95 1 : assert_se(utf8_is_valid(p2));
96 :
97 1 : p3 = utf8_escape_non_printable("\341\204");
98 1 : puts(p3);
99 1 : assert_se(utf8_is_valid(p3));
100 :
101 1 : p4 = utf8_escape_non_printable("ąę\n가너도루\n1234\n\341\204\341\204\n\001 \019\20\a");
102 1 : puts(p4);
103 1 : assert_se(utf8_is_valid(p4));
104 :
105 1 : p5 = utf8_escape_non_printable("\001 \019\20\a");
106 1 : puts(p5);
107 1 : assert_se(utf8_is_valid(p5));
108 :
109 1 : p6 = utf8_escape_non_printable("\xef\xbf\x30\x13");
110 1 : puts(p6);
111 1 : assert_se(utf8_is_valid(p6));
112 1 : }
113 :
114 1 : static void test_utf8_escape_non_printable_full(void) {
115 1 : log_info("/* %s */", __func__);
116 :
117 21 : for (size_t i = 0; i < 20; i++) {
118 20 : _cleanup_free_ char *p;
119 :
120 20 : p = utf8_escape_non_printable_full("goo goo goo", i);
121 20 : puts(p);
122 20 : assert_se(utf8_is_valid(p));
123 20 : assert_se(utf8_console_width(p) <= i);
124 : }
125 :
126 21 : for (size_t i = 0; i < 20; i++) {
127 20 : _cleanup_free_ char *p;
128 :
129 20 : p = utf8_escape_non_printable_full("\001 \019\20\a", i);
130 20 : puts(p);
131 20 : assert_se(utf8_is_valid(p));
132 20 : assert_se(utf8_console_width(p) <= i);
133 : }
134 :
135 21 : for (size_t i = 0; i < 20; i++) {
136 20 : _cleanup_free_ char *p;
137 :
138 20 : p = utf8_escape_non_printable_full("\xef\xbf\x30\x13", i);
139 20 : puts(p);
140 20 : assert_se(utf8_is_valid(p));
141 20 : assert_se(utf8_console_width(p) <= i);
142 : }
143 1 : }
144 :
145 1 : static void test_utf16_to_utf8(void) {
146 1 : const char16_t utf16[] = { htole16('a'), htole16(0xd800), htole16('b'), htole16(0xdc00), htole16('c'), htole16(0xd801), htole16(0xdc37) };
147 : static const char utf8[] = { 'a', 'b', 'c', 0xf0, 0x90, 0x90, 0xb7 };
148 1 : _cleanup_free_ char16_t *b = NULL;
149 1 : _cleanup_free_ char *a = NULL;
150 :
151 1 : log_info("/* %s */", __func__);
152 :
153 : /* Convert UTF-16 to UTF-8, filtering embedded bad chars */
154 1 : a = utf16_to_utf8(utf16, sizeof(utf16));
155 1 : assert_se(a);
156 1 : assert_se(memcmp(a, utf8, sizeof(utf8)) == 0);
157 :
158 : /* Convert UTF-8 to UTF-16, and back */
159 1 : b = utf8_to_utf16(utf8, sizeof(utf8));
160 1 : assert_se(b);
161 :
162 1 : free(a);
163 1 : a = utf16_to_utf8(b, char16_strlen(b) * 2);
164 1 : assert_se(a);
165 1 : assert_se(strlen(a) == sizeof(utf8));
166 1 : assert_se(memcmp(a, utf8, sizeof(utf8)) == 0);
167 1 : }
168 :
169 1 : static void test_utf8_n_codepoints(void) {
170 1 : log_info("/* %s */", __func__);
171 :
172 1 : assert_se(utf8_n_codepoints("abc") == 3);
173 1 : assert_se(utf8_n_codepoints("zażółcić gęślą jaźń") == 19);
174 1 : assert_se(utf8_n_codepoints("串") == 1);
175 1 : assert_se(utf8_n_codepoints("") == 0);
176 1 : assert_se(utf8_n_codepoints("…👊🔪💐…") == 5);
177 1 : assert_se(utf8_n_codepoints("\xF1") == (size_t) -1);
178 1 : }
179 :
180 1 : static void test_utf8_console_width(void) {
181 1 : log_info("/* %s */", __func__);
182 :
183 1 : assert_se(utf8_console_width("abc") == 3);
184 1 : assert_se(utf8_console_width("zażółcić gęślą jaźń") == 19);
185 1 : assert_se(utf8_console_width("串") == 2);
186 1 : assert_se(utf8_console_width("") == 0);
187 1 : assert_se(utf8_console_width("…👊🔪💐…") == 8);
188 1 : assert_se(utf8_console_width("\xF1") == (size_t) -1);
189 1 : }
190 :
191 1 : static void test_utf8_to_utf16(void) {
192 : const char *p;
193 :
194 1 : log_info("/* %s */", __func__);
195 :
196 6 : FOREACH_STRING(p,
197 : "abc",
198 : "zażółcić gęślą jaźń",
199 : "串",
200 : "",
201 : "…👊🔪💐…") {
202 :
203 5 : _cleanup_free_ char16_t *a = NULL;
204 5 : _cleanup_free_ char *b = NULL;
205 :
206 5 : a = utf8_to_utf16(p, strlen(p));
207 5 : assert_se(a);
208 :
209 5 : b = utf16_to_utf8(a, char16_strlen(a) * 2);
210 5 : assert_se(b);
211 5 : assert_se(streq(p, b));
212 : }
213 1 : }
214 :
215 1 : int main(int argc, char *argv[]) {
216 1 : test_utf8_is_valid();
217 1 : test_utf8_is_printable();
218 1 : test_ascii_is_valid();
219 1 : test_ascii_is_valid_n();
220 1 : test_utf8_encoded_valid_unichar();
221 1 : test_utf8_escape_invalid();
222 1 : test_utf8_escape_non_printable();
223 1 : test_utf8_escape_non_printable_full();
224 1 : test_utf16_to_utf8();
225 1 : test_utf8_n_codepoints();
226 1 : test_utf8_console_width();
227 1 : test_utf8_to_utf16();
228 :
229 1 : return 0;
230 : }
|