1 unicode: introduce test module for normalized utf8 implementation
3 From: Gabriel Krisman Bertazi <krisman@collabora.co.uk>
5 This implements a in-kernel sanity test module for the utf8
6 normalization core. At probe time, it will run basic sequences through
7 the utf8n core, to identify problems will equivalent sequences and
8 normalization/casefold code. This is supposed to be useful for
9 regression testing when adding support for a new version of utf8 to
12 Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.co.uk>
13 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
16 - integrate with fs/unicode
19 - Include comparison tests for matching strings with different lengths.
20 - Include tests for characters included in unicode 8.0.0, 9.0.0 and 10.0.0.
22 fs/unicode/Kconfig | 5 +
23 fs/unicode/Makefile | 1 +
24 fs/unicode/utf8-selftest.c | 320 +++++++++++++++++++++++++++++++++++++
25 3 files changed, 326 insertions(+)
26 create mode 100644 fs/unicode/utf8-selftest.c
28 diff --git a/fs/unicode/Kconfig b/fs/unicode/Kconfig
29 index f41520f57dff..b560a879edf7 100644
30 --- a/fs/unicode/Kconfig
31 +++ b/fs/unicode/Kconfig
32 @@ -6,3 +6,8 @@ config UNICODE
34 Say Y here to enable UTF-8 NFD normalization and NFD+CF casefolding
37 +config UNICODE_NORMALIZATION_SELFTEST
38 + tristate "Test UTF-8 normalization support"
41 diff --git a/fs/unicode/Makefile b/fs/unicode/Makefile
42 index bfb0360687df..671d31f83006 100644
43 --- a/fs/unicode/Makefile
44 +++ b/fs/unicode/Makefile
46 # SPDX-License-Identifier: GPL-2.0
48 obj-$(CONFIG_UNICODE) += unicode.o
49 +obj-$(CONFIG_UNICODE_NORMALIZATION_SELFTEST) += utf8-selftest.o
51 unicode-y := utf8-norm.o utf8-core.o
53 diff --git a/fs/unicode/utf8-selftest.c b/fs/unicode/utf8-selftest.c
55 index 000000000000..492d934d5c1c
57 +++ b/fs/unicode/utf8-selftest.c
60 + * Kernel module for testing utf-8 support.
62 + * Copyright 2017 Collabora Ltd.
64 + * This software is licensed under the terms of the GNU General Public
65 + * License version 2, as published by the Free Software Foundation, and
66 + * may be copied, distributed, and modified under those terms.
68 + * This program is distributed in the hope that it will be useful,
69 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 + * GNU General Public License for more details.
74 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
76 +#include <linux/module.h>
77 +#include <linux/printk.h>
78 +#include <linux/unicode.h>
79 +#include <linux/dcache.h>
83 +unsigned int failed_tests;
84 +unsigned int total_tests;
86 +/* Tests will be based on this version. */
87 +#define latest_maj 11
91 +#define _test(cond, func, line, fmt, ...) do { \
95 + pr_err("test %s:%d Failed: %s%s", \
96 + func, line, #cond, (fmt?":":".")); \
98 + pr_err(fmt, ##__VA_ARGS__); \
101 +#define test_f(cond, fmt, ...) _test(cond, __func__, __LINE__, fmt, ##__VA_ARGS__)
102 +#define test(cond) _test(cond, __func__, __LINE__, "")
104 +const static struct {
105 + /* UTF-8 strings in this vector _must_ be NULL-terminated. */
106 + unsigned char str[10];
107 + unsigned char dec[10];
108 +} nfdi_test_data[] = {
109 + /* Trivial sequence */
111 + /* "ABba" decomposes to itself */
115 + /* Simple equivalent sequences */
117 + /* 'VULGAR FRACTION ONE QUARTER' cannot decompose to
118 + 'NUMBER 1' + 'FRACTION SLASH' + 'NUMBER 4' on
119 + canonical decomposition */
120 + .str = {0xc2, 0xbc, 0x00},
121 + .dec = {0xc2, 0xbc, 0x00},
124 + /* 'LATIN SMALL LETTER A WITH DIAERESIS' decomposes to
125 + 'LETTER A' + 'COMBINING DIAERESIS' */
126 + .str = {0xc3, 0xa4, 0x00},
127 + .dec = {0x61, 0xcc, 0x88, 0x00},
130 + /* 'LATIN SMALL LETTER LJ' can't decompose to
131 + 'LETTER L' + 'LETTER J' on canonical decomposition */
132 + .str = {0xC7, 0x89, 0x00},
133 + .dec = {0xC7, 0x89, 0x00},
136 + /* GREEK ANO TELEIA decomposes to MIDDLE DOT */
137 + .str = {0xCE, 0x87, 0x00},
138 + .dec = {0xC2, 0xB7, 0x00}
140 + /* Canonical ordering */
142 + /* A + 'COMBINING ACUTE ACCENT' + 'COMBINING OGONEK' decomposes
143 + to A + 'COMBINING OGONEK' + 'COMBINING ACUTE ACCENT' */
144 + .str = {0x41, 0xcc, 0x81, 0xcc, 0xa8, 0x0},
145 + .dec = {0x41, 0xcc, 0xa8, 0xcc, 0x81, 0x0},
148 + /* 'LATIN SMALL LETTER A WITH DIAERESIS' + 'COMBINING OGONEK'
150 + 'LETTER A' + 'COMBINING OGONEK' + 'COMBINING DIAERESIS' */
151 + .str = {0xc3, 0xa4, 0xCC, 0xA8, 0x00},
153 + .dec = {0x61, 0xCC, 0xA8, 0xcc, 0x88, 0x00},
158 +const static struct {
159 + /* UTF-8 strings in this vector _must_ be NULL-terminated. */
160 + unsigned char str[30];
161 + unsigned char ncf[30];
162 +} nfdicf_test_data[] = {
163 + /* Trivial sequences */
165 + /* "ABba" folds to lowercase */
166 + .str = {0x41, 0x42, 0x62, 0x61, 0x00},
167 + .ncf = {0x61, 0x62, 0x62, 0x61, 0x00},
170 + /* All ASCII folds to lower-case */
171 + .str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0.1",
172 + .ncf = "abcdefghijklmnopqrstuvwxyz0.1",
175 + /* LATIN SMALL LETTER SHARP S folds to
176 + LATIN SMALL LETTER S + LATIN SMALL LETTER S */
177 + .str = {0xc3, 0x9f, 0x00},
178 + .ncf = {0x73, 0x73, 0x00},
181 + /* LATIN CAPITAL LETTER A WITH RING ABOVE folds to
182 + LATIN SMALL LETTER A + COMBINING RING ABOVE */
183 + .str = {0xC3, 0x85, 0x00},
184 + .ncf = {0x61, 0xcc, 0x8a, 0x00},
186 + /* Introduced by UTF-8.0.0. */
187 + /* Cherokee letters are interesting test-cases because they fold
188 + to upper-case. Before 8.0.0, Cherokee lowercase were
189 + undefined, thus, the folding from LC is not stable between
190 + 7.0.0 -> 8.0.0, but it is from UC. */
192 + /* CHEROKEE SMALL LETTER A folds to CHEROKEE LETTER A */
193 + .str = {0xea, 0xad, 0xb0, 0x00},
194 + .ncf = {0xe1, 0x8e, 0xa0, 0x00},
197 + /* CHEROKEE SMALL LETTER YE folds to CHEROKEE LETTER YE */
198 + .str = {0xe1, 0x8f, 0xb8, 0x00},
199 + .ncf = {0xe1, 0x8f, 0xb0, 0x00},
202 + /* OLD HUNGARIAN CAPITAL LETTER AMB folds to
203 + OLD HUNGARIAN SMALL LETTER AMB */
204 + .str = {0xf0, 0x90, 0xb2, 0x83, 0x00},
205 + .ncf = {0xf0, 0x90, 0xb3, 0x83, 0x00},
207 + /* Introduced by UTF-9.0.0. */
209 + /* OSAGE CAPITAL LETTER CHA folds to
210 + OSAGE SMALL LETTER CHA */
211 + .str = {0xf0, 0x90, 0x92, 0xb5, 0x00},
212 + .ncf = {0xf0, 0x90, 0x93, 0x9d, 0x00},
215 + /* LATIN CAPITAL LETTER SMALL CAPITAL I folds to
216 + LATIN LETTER SMALL CAPITAL I */
217 + .str = {0xea, 0x9e, 0xae, 0x00},
218 + .ncf = {0xc9, 0xaa, 0x00},
220 + /* Introduced by UTF-11.0.0. */
222 + /* GEORGIAN SMALL LETTER AN folds to GEORGIAN MTAVRULI
223 + CAPITAL LETTER AN */
224 + .str = {0xe1, 0xb2, 0x90, 0x00},
225 + .ncf = {0xe1, 0x83, 0x90, 0x00},
229 +static void check_utf8_nfdi(void)
232 + struct utf8cursor u8c;
233 + const struct utf8data *data;
235 + data = utf8nfdi(UNICODE_AGE(latest_maj, latest_min, latest_rev));
237 + pr_err("%s: Unable to load utf8-%d.%d.%d. Skipping.\n",
238 + __func__, latest_maj, latest_min, latest_rev);
242 + for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
243 + int len = strlen(nfdi_test_data[i].str);
244 + int nlen = strlen(nfdi_test_data[i].dec);
248 + test((utf8len(data, nfdi_test_data[i].str) == nlen));
249 + test((utf8nlen(data, nfdi_test_data[i].str, len) == nlen));
251 + if (utf8cursor(&u8c, data, nfdi_test_data[i].str) < 0)
252 + pr_err("can't create cursor\n");
254 + while ((c = utf8byte(&u8c)) > 0) {
255 + test_f((c == nfdi_test_data[i].dec[j]),
256 + "Unexpected byte 0x%x should be 0x%x\n",
257 + c, nfdi_test_data[i].dec[j]);
265 +static void check_utf8_nfdicf(void)
268 + struct utf8cursor u8c;
269 + const struct utf8data *data;
271 + data = utf8nfdicf(UNICODE_AGE(latest_maj, latest_min, latest_rev));
273 + pr_err("%s: Unable to load utf8-%d.%d.%d. Skipping.\n",
274 + __func__, latest_maj, latest_min, latest_rev);
278 + for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) {
279 + int len = strlen(nfdicf_test_data[i].str);
280 + int nlen = strlen(nfdicf_test_data[i].ncf);
284 + test((utf8len(data, nfdicf_test_data[i].str) == nlen));
285 + test((utf8nlen(data, nfdicf_test_data[i].str, len) == nlen));
287 + if (utf8cursor(&u8c, data, nfdicf_test_data[i].str) < 0)
288 + pr_err("can't create cursor\n");
290 + while ((c = utf8byte(&u8c)) > 0) {
291 + test_f((c == nfdicf_test_data[i].ncf[j]),
292 + "Unexpected byte 0x%x should be 0x%x\n",
293 + c, nfdicf_test_data[i].ncf[j]);
301 +static void check_utf8_comparisons(void)
304 + struct unicode_map *table = utf8_load("11.0.0");
306 + if (IS_ERR(table)) {
307 + pr_err("%s: Unable to load utf8 %d.%d.%d. Skipping.\n",
308 + __func__, latest_maj, latest_min, latest_rev);
312 + for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
313 + const struct qstr s1 = {.name = nfdi_test_data[i].str,
314 + .len = sizeof(nfdi_test_data[i].str)};
315 + const struct qstr s2 = {.name = nfdi_test_data[i].dec,
316 + .len = sizeof(nfdi_test_data[i].dec)};
318 + test_f(!utf8_strncmp(table, &s1, &s2),
319 + "%s %s comparison mismatch\n", s1.name, s2.name);
322 + for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) {
323 + const struct qstr s1 = {.name = nfdicf_test_data[i].str,
324 + .len = sizeof(nfdicf_test_data[i].str)};
325 + const struct qstr s2 = {.name = nfdicf_test_data[i].ncf,
326 + .len = sizeof(nfdicf_test_data[i].ncf)};
328 + test_f(!utf8_strncasecmp(table, &s1, &s2),
329 + "%s %s comparison mismatch\n", s1.name, s2.name);
332 + utf8_unload(table);
335 +static void check_supported_versions(void)
337 + /* Unicode 7.0.0 should be supported. */
338 + test(utf8version_is_supported(7, 0, 0));
340 + /* Unicode 9.0.0 should be supported. */
341 + test(utf8version_is_supported(9, 0, 0));
343 + /* Unicode 1x.0.0 (the latest version) should be supported. */
344 + test(utf8version_is_supported(latest_maj, latest_min, latest_rev));
346 + /* Next versions don't exist. */
347 + test(!utf8version_is_supported(12, 0, 0));
348 + test(!utf8version_is_supported(0, 0, 0));
349 + test(!utf8version_is_supported(-1, -1, -1));
352 +static int __init init_test_ucd(void)
357 + check_supported_versions();
359 + check_utf8_nfdicf();
360 + check_utf8_comparisons();
363 + pr_info("All %u tests passed\n", total_tests);
365 + pr_err("%u out of %u tests failed\n", failed_tests,
370 +static void __exit exit_test_ucd(void)
374 +module_init(init_test_ucd);
375 +module_exit(exit_test_ucd);
377 +MODULE_AUTHOR("Gabriel Krisman Bertazi <krisman@collabora.co.uk>");
378 +MODULE_LICENSE("GPL");