utf8-selftest.c (fbc59d65059ecfea8b746715e920c325cc16cede) | utf8-selftest.c (6ca99ce756c27852d1ea1e555045de1c920f30ed) |
---|---|
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Kernel module for testing utf-8 support. 4 * 5 * Copyright 2017 Collabora Ltd. 6 */ 7 8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt --- 4 unchanged lines hidden (view full) --- 13#include <linux/dcache.h> 14 15#include "utf8n.h" 16 17unsigned int failed_tests; 18unsigned int total_tests; 19 20/* Tests will be based on this version. */ | 1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Kernel module for testing utf-8 support. 4 * 5 * Copyright 2017 Collabora Ltd. 6 */ 7 8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt --- 4 unchanged lines hidden (view full) --- 13#include <linux/dcache.h> 14 15#include "utf8n.h" 16 17unsigned int failed_tests; 18unsigned int total_tests; 19 20/* Tests will be based on this version. */ |
21#define latest_maj 12 22#define latest_min 1 23#define latest_rev 0 | 21#define UTF8_LATEST UNICODE_AGE(12, 1, 0) |
24 25#define _test(cond, func, line, fmt, ...) do { \ 26 total_tests++; \ 27 if (!cond) { \ 28 failed_tests++; \ 29 pr_err("test %s:%d Failed: %s%s", \ 30 func, line, #cond, (fmt?":":".")); \ 31 if (fmt) \ --- 123 unchanged lines hidden (view full) --- 155 { 156 /* GEORGIAN SMALL LETTER AN folds to GEORGIAN MTAVRULI 157 CAPITAL LETTER AN */ 158 .str = {0xe1, 0xb2, 0x90, 0x00}, 159 .ncf = {0xe1, 0x83, 0x90, 0x00}, 160 } 161}; 162 | 22 23#define _test(cond, func, line, fmt, ...) do { \ 24 total_tests++; \ 25 if (!cond) { \ 26 failed_tests++; \ 27 pr_err("test %s:%d Failed: %s%s", \ 28 func, line, #cond, (fmt?":":".")); \ 29 if (fmt) \ --- 123 unchanged lines hidden (view full) --- 153 { 154 /* GEORGIAN SMALL LETTER AN folds to GEORGIAN MTAVRULI 155 CAPITAL LETTER AN */ 156 .str = {0xe1, 0xb2, 0x90, 0x00}, 157 .ncf = {0xe1, 0x83, 0x90, 0x00}, 158 } 159}; 160 |
163static ssize_t utf8len(const struct utf8data *data, const char *s) | 161static ssize_t utf8len(const struct unicode_map *um, enum utf8_normalization n, 162 const char *s) |
164{ | 163{ |
165 return utf8nlen(data, s, (size_t)-1); | 164 return utf8nlen(um, n, s, (size_t)-1); |
166} 167 | 165} 166 |
168static int utf8cursor(struct utf8cursor *u8c, const struct utf8data *data, 169 const char *s) | 167static int utf8cursor(struct utf8cursor *u8c, const struct unicode_map *um, 168 enum utf8_normalization n, const char *s) |
170{ | 169{ |
171 return utf8ncursor(u8c, data, s, (unsigned int)-1); | 170 return utf8ncursor(u8c, um, n, s, (unsigned int)-1); |
172} 173 | 171} 172 |
174static void check_utf8_nfdi(void) | 173static void check_utf8_nfdi(struct unicode_map *um) |
175{ 176 int i; 177 struct utf8cursor u8c; | 174{ 175 int i; 176 struct utf8cursor u8c; |
178 const struct utf8data *data; | |
179 | 177 |
180 data = utf8nfdi(UNICODE_AGE(latest_maj, latest_min, latest_rev)); 181 if (!data) { 182 pr_err("%s: Unable to load utf8-%d.%d.%d. Skipping.\n", 183 __func__, latest_maj, latest_min, latest_rev); 184 return; 185 } 186 | |
187 for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) { 188 int len = strlen(nfdi_test_data[i].str); 189 int nlen = strlen(nfdi_test_data[i].dec); 190 int j = 0; 191 unsigned char c; 192 | 178 for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) { 179 int len = strlen(nfdi_test_data[i].str); 180 int nlen = strlen(nfdi_test_data[i].dec); 181 int j = 0; 182 unsigned char c; 183 |
193 test((utf8len(data, nfdi_test_data[i].str) == nlen)); 194 test((utf8nlen(data, nfdi_test_data[i].str, len) == nlen)); | 184 test((utf8len(um, UTF8_NFDI, nfdi_test_data[i].str) == nlen)); 185 test((utf8nlen(um, UTF8_NFDI, nfdi_test_data[i].str, len) == 186 nlen)); |
195 | 187 |
196 if (utf8cursor(&u8c, data, nfdi_test_data[i].str) < 0) | 188 if (utf8cursor(&u8c, um, UTF8_NFDI, nfdi_test_data[i].str) < 0) |
197 pr_err("can't create cursor\n"); 198 199 while ((c = utf8byte(&u8c)) > 0) { 200 test_f((c == nfdi_test_data[i].dec[j]), 201 "Unexpected byte 0x%x should be 0x%x\n", 202 c, nfdi_test_data[i].dec[j]); 203 j++; 204 } 205 206 test((j == nlen)); 207 } 208} 209 | 189 pr_err("can't create cursor\n"); 190 191 while ((c = utf8byte(&u8c)) > 0) { 192 test_f((c == nfdi_test_data[i].dec[j]), 193 "Unexpected byte 0x%x should be 0x%x\n", 194 c, nfdi_test_data[i].dec[j]); 195 j++; 196 } 197 198 test((j == nlen)); 199 } 200} 201 |
210static void check_utf8_nfdicf(void) | 202static void check_utf8_nfdicf(struct unicode_map *um) |
211{ 212 int i; 213 struct utf8cursor u8c; | 203{ 204 int i; 205 struct utf8cursor u8c; |
214 const struct utf8data *data; | |
215 | 206 |
216 data = utf8nfdicf(UNICODE_AGE(latest_maj, latest_min, latest_rev)); 217 if (!data) { 218 pr_err("%s: Unable to load utf8-%d.%d.%d. Skipping.\n", 219 __func__, latest_maj, latest_min, latest_rev); 220 return; 221 } 222 | |
223 for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) { 224 int len = strlen(nfdicf_test_data[i].str); 225 int nlen = strlen(nfdicf_test_data[i].ncf); 226 int j = 0; 227 unsigned char c; 228 | 207 for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) { 208 int len = strlen(nfdicf_test_data[i].str); 209 int nlen = strlen(nfdicf_test_data[i].ncf); 210 int j = 0; 211 unsigned char c; 212 |
229 test((utf8len(data, nfdicf_test_data[i].str) == nlen)); 230 test((utf8nlen(data, nfdicf_test_data[i].str, len) == nlen)); | 213 test((utf8len(um, UTF8_NFDICF, nfdicf_test_data[i].str) == 214 nlen)); 215 test((utf8nlen(um, UTF8_NFDICF, nfdicf_test_data[i].str, len) == 216 nlen)); |
231 | 217 |
232 if (utf8cursor(&u8c, data, nfdicf_test_data[i].str) < 0) | 218 if (utf8cursor(&u8c, um, UTF8_NFDICF, 219 nfdicf_test_data[i].str) < 0) |
233 pr_err("can't create cursor\n"); 234 235 while ((c = utf8byte(&u8c)) > 0) { 236 test_f((c == nfdicf_test_data[i].ncf[j]), 237 "Unexpected byte 0x%x should be 0x%x\n", 238 c, nfdicf_test_data[i].ncf[j]); 239 j++; 240 } 241 242 test((j == nlen)); 243 } 244} 245 | 220 pr_err("can't create cursor\n"); 221 222 while ((c = utf8byte(&u8c)) > 0) { 223 test_f((c == nfdicf_test_data[i].ncf[j]), 224 "Unexpected byte 0x%x should be 0x%x\n", 225 c, nfdicf_test_data[i].ncf[j]); 226 j++; 227 } 228 229 test((j == nlen)); 230 } 231} 232 |
246static void check_utf8_comparisons(void) | 233static void check_utf8_comparisons(struct unicode_map *table) |
247{ 248 int i; | 234{ 235 int i; |
249 struct unicode_map *table = utf8_load(UNICODE_AGE(12, 1, 0)); | |
250 | 236 |
251 if (IS_ERR(table)) { 252 pr_err("%s: Unable to load utf8 %d.%d.%d. Skipping.\n", 253 __func__, latest_maj, latest_min, latest_rev); 254 return; 255 } 256 | |
257 for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) { 258 const struct qstr s1 = {.name = nfdi_test_data[i].str, 259 .len = sizeof(nfdi_test_data[i].str)}; 260 const struct qstr s2 = {.name = nfdi_test_data[i].dec, 261 .len = sizeof(nfdi_test_data[i].dec)}; 262 263 test_f(!utf8_strncmp(table, &s1, &s2), 264 "%s %s comparison mismatch\n", s1.name, s2.name); 265 } 266 267 for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) { 268 const struct qstr s1 = {.name = nfdicf_test_data[i].str, 269 .len = sizeof(nfdicf_test_data[i].str)}; 270 const struct qstr s2 = {.name = nfdicf_test_data[i].ncf, 271 .len = sizeof(nfdicf_test_data[i].ncf)}; 272 273 test_f(!utf8_strncasecmp(table, &s1, &s2), 274 "%s %s comparison mismatch\n", s1.name, s2.name); 275 } | 237 for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) { 238 const struct qstr s1 = {.name = nfdi_test_data[i].str, 239 .len = sizeof(nfdi_test_data[i].str)}; 240 const struct qstr s2 = {.name = nfdi_test_data[i].dec, 241 .len = sizeof(nfdi_test_data[i].dec)}; 242 243 test_f(!utf8_strncmp(table, &s1, &s2), 244 "%s %s comparison mismatch\n", s1.name, s2.name); 245 } 246 247 for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) { 248 const struct qstr s1 = {.name = nfdicf_test_data[i].str, 249 .len = sizeof(nfdicf_test_data[i].str)}; 250 const struct qstr s2 = {.name = nfdicf_test_data[i].ncf, 251 .len = sizeof(nfdicf_test_data[i].ncf)}; 252 253 test_f(!utf8_strncasecmp(table, &s1, &s2), 254 "%s %s comparison mismatch\n", s1.name, s2.name); 255 } |
276 277 utf8_unload(table); | |
278} 279 280static void check_supported_versions(void) 281{ 282 /* Unicode 7.0.0 should be supported. */ 283 test(utf8version_is_supported(UNICODE_AGE(7, 0, 0))); 284 285 /* Unicode 9.0.0 should be supported. */ 286 test(utf8version_is_supported(UNICODE_AGE(9, 0, 0))); 287 288 /* Unicode 1x.0.0 (the latest version) should be supported. */ | 256} 257 258static void check_supported_versions(void) 259{ 260 /* Unicode 7.0.0 should be supported. */ 261 test(utf8version_is_supported(UNICODE_AGE(7, 0, 0))); 262 263 /* Unicode 9.0.0 should be supported. */ 264 test(utf8version_is_supported(UNICODE_AGE(9, 0, 0))); 265 266 /* Unicode 1x.0.0 (the latest version) should be supported. */ |
289 test(utf8version_is_supported( 290 UNICODE_AGE(latest_maj, latest_min, latest_rev))); | 267 test(utf8version_is_supported(UTF8_LATEST)); |
291 292 /* Next versions don't exist. */ 293 test(!utf8version_is_supported(UNICODE_AGE(13, 0, 0))); 294 test(!utf8version_is_supported(UNICODE_AGE(0, 0, 0))); 295 test(!utf8version_is_supported(UNICODE_AGE(-1, -1, -1))); 296} 297 298static int __init init_test_ucd(void) 299{ | 268 269 /* Next versions don't exist. */ 270 test(!utf8version_is_supported(UNICODE_AGE(13, 0, 0))); 271 test(!utf8version_is_supported(UNICODE_AGE(0, 0, 0))); 272 test(!utf8version_is_supported(UNICODE_AGE(-1, -1, -1))); 273} 274 275static int __init init_test_ucd(void) 276{ |
277 struct unicode_map *um; 278 |
|
300 failed_tests = 0; 301 total_tests = 0; 302 | 279 failed_tests = 0; 280 total_tests = 0; 281 |
282 um = utf8_load(UTF8_LATEST); 283 if (IS_ERR(um)) { 284 pr_err("%s: Unable to load utf8 table.\n", __func__); 285 return PTR_ERR(um); 286 } 287 |
|
303 check_supported_versions(); | 288 check_supported_versions(); |
304 check_utf8_nfdi(); 305 check_utf8_nfdicf(); 306 check_utf8_comparisons(); | 289 check_utf8_nfdi(um); 290 check_utf8_nfdicf(um); 291 check_utf8_comparisons(um); |
307 308 if (!failed_tests) 309 pr_info("All %u tests passed\n", total_tests); 310 else 311 pr_err("%u out of %u tests failed\n", failed_tests, 312 total_tests); | 292 293 if (!failed_tests) 294 pr_info("All %u tests passed\n", total_tests); 295 else 296 pr_err("%u out of %u tests failed\n", failed_tests, 297 total_tests); |
298 utf8_unload(um); |
|
313 return 0; 314} 315 316static void __exit exit_test_ucd(void) 317{ 318} 319 320module_init(init_test_ucd); 321module_exit(exit_test_ucd); 322 323MODULE_AUTHOR("Gabriel Krisman Bertazi <krisman@collabora.co.uk>"); 324MODULE_LICENSE("GPL"); | 299 return 0; 300} 301 302static void __exit exit_test_ucd(void) 303{ 304} 305 306module_init(init_test_ucd); 307module_exit(exit_test_ucd); 308 309MODULE_AUTHOR("Gabriel Krisman Bertazi <krisman@collabora.co.uk>"); 310MODULE_LICENSE("GPL"); |