-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiconv.ctest
345 lines (289 loc) · 16.3 KB
/
iconv.ctest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Copyright (c) 2019-2025 Griefer@Work *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement (see the following) in the product *
* documentation is required: *
* Portions Copyright (c) 2019-2025 Griefer@Work *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
*/
#ifndef GUARD_LIBICONV_ICONV_CTEST
#define GUARD_LIBICONV_ICONV_CTEST 1
#define _KOS_SOURCE 1
#undef NDEBUG
#include <hybrid/compiler.h>
#include <kos/types.h>
#include <system-test/ctest.h>
#include <assert.h>
#include <dlfcn.h>
#include <format-printer.h>
#include <string.h>
#include <unicode.h>
#include <libiconv/codec.h>
#include <libiconv/iconv.h>
#include <libiconv/transliterate.h>
DECL_BEGIN
PRIVATE _PICONV_TRANSCODE_INIT _iconv_transcode_init = NULL;
PRIVATE PICONV_DECODE_ISSHIFTZERO iconv_decode_isshiftzero = NULL;
PRIVATE PICONV_ENCODE_FLUSH iconv_encode_flush = NULL;
PRIVATE PICONV_CODECBYNAME iconv_codecbyname = NULL;
PRIVATE PICONV_GETCODECNAMES iconv_getcodecnames = NULL;
PRIVATE PICONV_DETECT_CODEC iconv_detect_codec = NULL;
PRIVATE PICONV_TRANSLITERATE iconv_transliterate = NULL;
PRIVATE char *convertz(char const *from_codec, char const *to_codec,
unsigned int error_mode, char const *from_text,
size_t from_len, char *to_buf) {
char *result = to_buf;
struct iconv_transcode tc;
struct iconv_printer in;
int error;
ssize_t status;
tc.it_encode.ice_output.ii_printer = &format_sprintf_printer;
tc.it_encode.ice_output.ii_arg = &to_buf;
tc.it_encode.ice_flags = error_mode;
/* Initialize the transcoder */
EQ(0, (error = (*_iconv_transcode_init)(&tc, &in, from_codec, to_codec)));
/* Print data */
ISpos((status = (*in.ii_printer)(in.ii_arg, from_text, from_len)));
/* Make sure that the decoder is left in a 0-shift state. */
assertf((*iconv_decode_isshiftzero)(&tc.it_decode),
"decoder (codec=%q) not in 0-shift state after pasing %$q",
from_codec, from_len, from_text);
/* As per recommendations, flush data once everything has been printed. */
ISpos((status = (*iconv_encode_flush)(&tc.it_encode)));
/* NUL-terminate. */
*to_buf = 0;
return result;
}
PRIVATE char *convert(char const *from_codec, char const *to_codec,
unsigned int error_mode, char const *from_text,
char *to_buf) {
return convertz(from_codec, to_codec, error_mode, from_text, strlen(from_text), to_buf);
}
PRIVATE bool has_transliteration(char32_t ch, char const *translit) {
char utf8_buf[UNICODE_32TO8_MAXBUF(ICONV_TRANSLITERATE_MAXLEN) + 1];
char32_t buf[ICONV_TRANSLITERATE_MAXLEN];
size_t len, nth;
for (nth = 0;; ++nth) {
len = iconv_transliterate(buf, ch, nth, ICONV_TRANSLITERATE_F_ALL);
if (len == (size_t)-1)
break;
*unicode_32to8(utf8_buf, buf, len) = '\0';
if (strcmp(utf8_buf, translit) == 0)
return true;
}
return false;
}
DEFINE_TEST(iconv) {
char buf[256];
void *libiconv;
ISnonnullf((libiconv = dlopen(LIBICONV_LIBRARY_NAME, RTLD_LOCAL)), "%s", dlerror());
#define DLSYM(x) ISnonnullf((*(void **)&x = dlsym(libiconv, #x)), "%s", dlerror())
DLSYM(_iconv_transcode_init);
DLSYM(iconv_decode_isshiftzero);
DLSYM(iconv_encode_flush);
DLSYM(iconv_codecbyname);
DLSYM(iconv_getcodecnames);
DLSYM(iconv_detect_codec);
DLSYM(iconv_transliterate);
#undef DLSYM
assert(has_transliteration(0x00E4, "a")); /* ä */
assert(has_transliteration(0x00F6, "o")); /* ö */
assert(has_transliteration(0x00FC, "u")); /* ü */
assert(has_transliteration(0x00C4, "A")); /* Ä */
assert(has_transliteration(0x00D6, "O")); /* Ö */
assert(has_transliteration(0x00DC, "U")); /* Ü */
/* Because the next test relies on unicode folding,
* test that folding works as a stand-alone operation! */
{
size_t len;
char32_t foldbuf[UNICODE_FOLDED_MAX];
EQ(2, (len = (size_t)(unicode_fold(0xdf, foldbuf) - foldbuf))); /* ß */
EQ('s', foldbuf[0]);
EQ('s', foldbuf[1]);
}
EQstr("aou: \x7B\x7C\x7D!", convert("latin-1", "de", ICONV_ERR_ERRNO, "aou: \xE4\xF6\xFC!", buf));
EQstr("ss: ss!", convert("de", "ascii", ICONV_ERR_ERRNO | ICONV_ERR_TRANSLIT, "ss: \x7E!", buf));
EQstr("aou: aou!", convert("de", "ascii", ICONV_ERR_ERRNO | ICONV_ERR_TRANSLIT, "aou: \x7B\x7C\x7D!", buf));
EQstr("aou: aou!", convert("latin-1", "ascii", ICONV_ERR_ERRNO | ICONV_ERR_TRANSLIT, "aou: \xE4\xF6\xFC!", buf));
EQstr("Yy: \xDE\xFE!", convert("utf-8", "iso-ir-182", ICONV_ERR_ERRNO, "Yy: \u0176\u0177!", buf));
EQstr("Yy: \u0176\u0177!", convert("iso-ir-182", "utf-8", ICONV_ERR_ERRNO, "Yy: \xDE\xFE!", buf));
{
char buf2[64];
EQstr("\x31\x32\x33\x34", convert("utf-8", "bcdic", ICONV_ERR_ERRNO, "ABCD", buf2));
EQstr("ABCD", convert("bcdic", "utf-8", ICONV_ERR_ERRNO, buf2, buf));
}
/* Also test multi-byte decode functionality. */
EQstr("Hello", convertz("utf-16", "utf-8", ICONV_ERR_ERRNO, (char *)u"Hello", 10, buf));
#define ASSERT_DETECTED_CODEC(name, data) \
ASSERT_DETECTED_CODEC_Z(name, data, sizeof(data) - 1)
#define ASSERT_DETECTED_CODEC_Z(name, data, size) \
do { \
iconv_codec_t _dec, _cd = iconv_codecbyname(name); \
assertf(_cd != ICONV_CODEC_UNKNOWN, "Unknown codec: %q", name); \
_dec = iconv_detect_codec(data, size); \
assertf(_dec != ICONV_CODEC_UNKNOWN, "Failed to detect any codec"); \
assertf(_dec == _cd, "Detected %u:%q instead of %q", \
_dec, iconv_getcodecnames(_dec), name); \
} __WHILE0
/* Test special cases. */
EQ(ICONV_CODEC_UNKNOWN, iconv_detect_codec("", 0));
{
void *volatile null = NULL;
EQ(ICONV_CODEC_UNKNOWN, iconv_detect_codec(null, 0));
}
/* Test simple input data. Anything beyond this would rely too much on the
* internal ASCII-text heuristic to allow for any safety during testing. */
ASSERT_DETECTED_CODEC_Z("utf-8", "Hello World!", 12);
ASSERT_DETECTED_CODEC_Z("utf-16", u"Hello World!", 24);
ASSERT_DETECTED_CODEC_Z("utf-32", U"Hello World!", 48);
/* Test the auto codec detection functionality. */
ASSERT_DETECTED_CODEC("de",
"Text before the codec marker # codec: de\n"
"Pure ascii!\n");
ASSERT_DETECTED_CODEC("de",
"Text before the codec marker codec: de\n"
"Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker /* codec: de */ Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker (* codec: de *) Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker /*codec: de*/ Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker (*codec: de*) Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker /*codec de*/ Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker (*codec de*) Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker /* \t*#@+!$/codec: de \t*#@+!$/*/ Pure ascii!\n");
ASSERT_DETECTED_CODEC("de", "Text before the codec marker (* \t*#@+!$/codec: de \t*#@+!$/*) Pure ascii!\n");
ASSERT_DETECTED_CODEC("latin-1", "<meta charset=\"latin-1\"/>\n"
"<html>\n"
"\tHello World! aou: \xE4\xF6\xFC\n"
"</html>\n");
#define ICONV_DETECT_CODEC(s) iconv_detect_codec(s, sizeof(s) - 1)
EQ(ICONV_CODEC_UNKNOWN, ICONV_DETECT_CODEC("<meta charset=\xE4\"latin-1\"/>\n"
"<html>\n"
"\tHello World! aou: \xE4\xF6\xFC\n"
"</html>\n"));
EQ(ICONV_CODEC_UNKNOWN, ICONV_DETECT_CODEC("\xE4#codec l1\n"
"Hello World! aou: \xE4\xF6\xFC\n"));
ASSERT_DETECTED_CODEC("l1",
"#codec l1\n\xE4"
"Hello World! aou: \xE4\xF6\xFC\n");
/* Test conversion to c-escape (and its variants) */
/* When encoding a unicode character that isn't printable, it will be escaped in output. */
EQstr("Hello\\uFFFF\\nWorld", convert("utf-8", "c-escape", ICONV_ERR_ERRNO, "Hello\uFFFF\nWorld", buf));
EQstr("'Hello\\uFFFF\\nWorld'", convert("utf-8", "c-escape-chr", ICONV_ERR_ERRNO, "Hello\uFFFF\nWorld", buf));
EQstr("\"Hello\\uFFFF\\nWorld\"", convert("utf-8", "c-escape-str", ICONV_ERR_ERRNO, "Hello\uFFFF\nWorld", buf));
EQstr("Hello\\xEF\\xBF\\xBF\\nWorld", convert("utf-8", "c-escape-bytes", ICONV_ERR_ERRNO, "Hello\uFFFF\nWorld", buf));
EQstr("'Hello\\xEF\\xBF\\xBF\\nWorld'", convert("utf-8", "c-escape-bytes-chr", ICONV_ERR_ERRNO, "Hello\uFFFF\nWorld", buf));
EQstr("\"Hello\\xEF\\xBF\\xBF\\nWorld\"", convert("utf-8", "c-escape-bytes-str", ICONV_ERR_ERRNO, "Hello\uFFFF\nWorld", buf));
/* When a unicode character is printable, it's simply re-printed as-is
* HINT: '\u263A' == '☺' (which is considered printable, and should show up as a smiley) */
EQstr("Hello\u263A\\nWorld", convert("utf-8", "c-escape", ICONV_ERR_ERRNO, "Hello\u263A\nWorld", buf));
EQstr("'Hello\u263A\\nWorld'", convert("utf-8", "c-escape-chr", ICONV_ERR_ERRNO, "Hello\u263A\nWorld", buf));
EQstr("\"Hello\u263A\\nWorld\"", convert("utf-8", "c-escape-str", ICONV_ERR_ERRNO, "Hello\u263A\nWorld", buf));
EQstr("Hello\\xE2\\x98\\xBA\\nWorld", convert("utf-8", "c-escape-bytes", ICONV_ERR_ERRNO, "Hello\u263A\nWorld", buf));
EQstr("'Hello\\xE2\\x98\\xBA\\nWorld'", convert("utf-8", "c-escape-bytes-chr", ICONV_ERR_ERRNO, "Hello\u263A\nWorld", buf));
EQstr("\"Hello\\xE2\\x98\\xBA\\nWorld\"", convert("utf-8", "c-escape-bytes-str", ICONV_ERR_ERRNO, "Hello\u263A\nWorld", buf));
EQstr("\\xAB\\x41\\x42", convert("utf-8", "c-escape-bytes", ICONV_ERR_ERRNO, "\xAB" "AB", buf));
EQstr("\"\\xAB\" \"AB\"", convert("utf-8", "c-escape-bytes-str", ICONV_ERR_ERRNO, "\xAB" "AB", buf));
EQstr("\'\\xAB\' \'AB\'", convert("utf-8", "c-escape-bytes-chr", ICONV_ERR_ERRNO, "\xAB" "AB", buf));
EQstr("\\\\", convert("utf-8", "c-escape", ICONV_ERR_ERRNO, "\\", buf));
EQstr("\"\\\\\"", convert("utf-8", "c-escape-str", ICONV_ERR_ERRNO, "\\", buf));
EQstr("\'\\\\\'", convert("utf-8", "c-escape-chr", ICONV_ERR_ERRNO, "\\", buf));
EQstr("\\\\", convert("utf-8", "c-escape-bytes", ICONV_ERR_ERRNO, "\\", buf));
EQstr("\"\\\\\"", convert("utf-8", "c-escape-bytes-str", ICONV_ERR_ERRNO, "\\", buf));
EQstr("\'\\\\\'", convert("utf-8", "c-escape-bytes-chr", ICONV_ERR_ERRNO, "\\", buf));
EQstr("Hello\u263A\nWorld", convert("c-escape", "utf-8", ICONV_ERR_ERRNO, "Hello\\u263A\\nWorld", buf));
EQstr("Hello\\xE2\\x98\\xBA\\nWorld", convert("c-escape", "c-escape-bytes", ICONV_ERR_ERRNO, "Hello\\u263A\\nWorld", buf));
/* The c-escape parser is also able to correctly deal with escaped line-feeds */
EQstr("Hello\nWorld", convert("c-escape", "utf-8", ICONV_ERR_ERRNO, "Hello\\nWorld", buf)); /* Linefeed is not escaped */
EQstr("HelloWorld", convert("c-escape", "utf-8", ICONV_ERR_ERRNO, "Hello\\\nWorld", buf)); /* Linefeed is escaped */
EQstr("HelloWorld", convert("c-escape", "utf-8", ICONV_ERR_ERRNO, "Hello\\\rWorld", buf)); /* Linefeed is escaped */
EQstr("HelloWorld", convert("c-escape", "utf-8", ICONV_ERR_ERRNO, "Hello\\\r\nWorld", buf)); /* Linefeed is escaped */
EQstr("HelloWorld", convert("c-escape", "utf-8", ICONV_ERR_ERRNO, "\"Hello\\\r\nWorld\"", buf)); /* Linefeed is escaped */
/* Test the zero-shift integration for decoding c-escape */
{
struct iconv_transcode tc;
struct iconv_printer in;
int error;
ssize_t status;
char *ptr = buf;
tc.it_encode.ice_output.ii_printer = &format_sprintf_printer;
tc.it_encode.ice_output.ii_arg = &ptr;
tc.it_encode.ice_flags = ICONV_ERR_ERRNO;
error = (*_iconv_transcode_init)(&tc, &in, "c-escape", "utf-8");
assertf(error == 0, "error = %d", error);
#define PRINT(s) ISpos((status = (*in.ii_printer)(in.ii_arg, s, COMPILER_STRLEN(s))))
/* The c-escape decoder is only is 0-shift state while outside of a string literal!
* NOTE: This also tests that feeding the codec its data piece-wise works correctly. */
assert((*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("\"");
assert(!(*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("Hello\\");
assert(!(*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("\r");
assert(!(*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("\n");
assert(!(*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("\"");
assert((*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT(" \t \n \r \r\n \t "); /* Only whitespace is allowed to appear here; anything else causes errors! */
assert((*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("\"");
assert(!(*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("World");
assert(!(*iconv_decode_isshiftzero)(&tc.it_decode));
PRINT("\"");
/* As per recommendations, flush data once everything has been printed. */
ISpos((status = (*iconv_encode_flush)(&tc.it_encode)));
/* NUL-terminate. */
*ptr = 0;
/* Ensure that the correct data was decoded. */
EQstr("HelloWorld", buf);
}
/* Test the xml-escape codec. */
EQstr("Foo\"&\'<>Bar", convert("xml-escape", "utf-8", ICONV_ERR_ERRNO, "Foo"&'<>Bar", buf));
/* Depending on configuration, libiconv may either use
* entity names or (where doing so is shorter), decimals. */
convert("utf-8", "xml-escape", ICONV_ERR_ERRNO, "Foo\"&\'<>Bar", buf);
assertf(strcmp(buf, "Foo"&'<>Bar") == 0 ||
strcmp(buf, "Foo"&'<>Bar") == 0,
"buf = %q", buf);
EQstr("A\u00A0B", convert("xml-escape", "utf-8", ICONV_ERR_ERRNO, "A B", buf)); /* The trailing ';' is optional for ' ' */
EQstr("A\u00A0B", convert("xml-escape", "utf-8", ICONV_ERR_ERRNO, "A B", buf));
EQstr("A\u00A0", convert("xml-escape", "utf-8", ICONV_ERR_ERRNO, "A ", buf));
EQstr("A\u00A0", convert("xml-escape", "utf-8", ICONV_ERR_ERRNO, "A ", buf));
/* Test the uri-escape codec. */
EQstr("Hello%20World", convert("ascii", "uri-escape", ICONV_ERR_ERRNO, "Hello World", buf));
EQstr("Hello World", convert("uri-escape", "ascii", ICONV_ERR_ERRNO, "Hello%20World", buf));
/* Test the hex codec. */
EQstr("48656C6C6F20576F726C64", convert("ascii", "hex", ICONV_ERR_ERRNO, "Hello World", buf));
EQstr("48656C6C6F20576F726C64", convert("ascii", "hex-upper", ICONV_ERR_ERRNO, "Hello World", buf));
EQstr("48656c6c6f20576f726c64", convert("ascii", "hex-lower", ICONV_ERR_ERRNO, "Hello World", buf));
EQstr("Hello World", convert("hex", "ascii", ICONV_ERR_ERRNO, "48656C6C6F20576F726C64", buf));
EQstr("Hello World", convert("hex", "ascii", ICONV_ERR_ERRNO, "48656c6c6f20576f726c64", buf));
/* Test the base64 codec.
* NOTE: Example take from `https://de.wikipedia.org/wiki/Base64' */
EQstr("Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark",
convert("base64", "utf-8", ICONV_ERR_ERRNO,
"UG9seWZvbiB6d2l0c2NoZXJuZCBhw59lbiBNw6R4Y2hlbnMgVsO2Z2VsIFLDvGJl"
"biwgSm9naHVydCB1bmQgUXVhcms=",
buf));
EQstr("UG9seWZvbiB6d2l0c2NoZXJuZCBhw59lbiBNw6R4Y2hlbnMgVsO2Z2VsIFLDvGJl"
"biwgSm9naHVydCB1bmQgUXVhcms=",
convert("utf-8", "base64", ICONV_ERR_ERRNO,
"Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark",
buf));
/* Cleanup */
EQ(0, dlclose(libiconv));
}
DECL_END
#endif /* !GUARD_LIBICONV_ICONV_CTEST */