utf8-selftest.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/unicode.h>
  11. #include <linux/dcache.h>
  12. #include "utf8n.h"
  13. unsigned int failed_tests;
  14. unsigned int total_tests;
  15. /* Tests will be based on this version. */
  16. #define UTF8_LATEST UNICODE_AGE(12, 1, 0)
  17. #define _test(cond, func, line, fmt, ...) do { \
  18. total_tests++; \
  19. if (!cond) { \
  20. failed_tests++; \
  21. pr_err("test %s:%d Failed: %s%s", \
  22. func, line, #cond, (fmt?":":".")); \
  23. if (fmt) \
  24. pr_err(fmt, ##__VA_ARGS__); \
  25. } \
  26. } while (0)
  27. #define test_f(cond, fmt, ...) _test(cond, __func__, __LINE__, fmt, ##__VA_ARGS__)
  28. #define test(cond) _test(cond, __func__, __LINE__, "")
  29. static const struct {
  30. /* UTF-8 strings in this vector _must_ be NULL-terminated. */
  31. unsigned char str[10];
  32. unsigned char dec[10];
  33. } nfdi_test_data[] = {
  34. /* Trivial sequence */
  35. {
  36. /* "ABba" decomposes to itself */
  37. .str = "aBba",
  38. .dec = "aBba",
  39. },
  40. /* Simple equivalent sequences */
  41. {
  42. /* 'VULGAR FRACTION ONE QUARTER' cannot decompose to
  43. 'NUMBER 1' + 'FRACTION SLASH' + 'NUMBER 4' on
  44. canonical decomposition */
  45. .str = {0xc2, 0xbc, 0x00},
  46. .dec = {0xc2, 0xbc, 0x00},
  47. },
  48. {
  49. /* 'LATIN SMALL LETTER A WITH DIAERESIS' decomposes to
  50. 'LETTER A' + 'COMBINING DIAERESIS' */
  51. .str = {0xc3, 0xa4, 0x00},
  52. .dec = {0x61, 0xcc, 0x88, 0x00},
  53. },
  54. {
  55. /* 'LATIN SMALL LETTER LJ' can't decompose to
  56. 'LETTER L' + 'LETTER J' on canonical decomposition */
  57. .str = {0xC7, 0x89, 0x00},
  58. .dec = {0xC7, 0x89, 0x00},
  59. },
  60. {
  61. /* GREEK ANO TELEIA decomposes to MIDDLE DOT */
  62. .str = {0xCE, 0x87, 0x00},
  63. .dec = {0xC2, 0xB7, 0x00}
  64. },
  65. /* Canonical ordering */
  66. {
  67. /* A + 'COMBINING ACUTE ACCENT' + 'COMBINING OGONEK' decomposes
  68. to A + 'COMBINING OGONEK' + 'COMBINING ACUTE ACCENT' */
  69. .str = {0x41, 0xcc, 0x81, 0xcc, 0xa8, 0x0},
  70. .dec = {0x41, 0xcc, 0xa8, 0xcc, 0x81, 0x0},
  71. },
  72. {
  73. /* 'LATIN SMALL LETTER A WITH DIAERESIS' + 'COMBINING OGONEK'
  74. decomposes to
  75. 'LETTER A' + 'COMBINING OGONEK' + 'COMBINING DIAERESIS' */
  76. .str = {0xc3, 0xa4, 0xCC, 0xA8, 0x00},
  77. .dec = {0x61, 0xCC, 0xA8, 0xcc, 0x88, 0x00},
  78. },
  79. };
  80. static const struct {
  81. /* UTF-8 strings in this vector _must_ be NULL-terminated. */
  82. unsigned char str[30];
  83. unsigned char ncf[30];
  84. } nfdicf_test_data[] = {
  85. /* Trivial sequences */
  86. {
  87. /* "ABba" folds to lowercase */
  88. .str = {0x41, 0x42, 0x62, 0x61, 0x00},
  89. .ncf = {0x61, 0x62, 0x62, 0x61, 0x00},
  90. },
  91. {
  92. /* All ASCII folds to lower-case */
  93. .str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0.1",
  94. .ncf = "abcdefghijklmnopqrstuvwxyz0.1",
  95. },
  96. {
  97. /* LATIN SMALL LETTER SHARP S folds to
  98. LATIN SMALL LETTER S + LATIN SMALL LETTER S */
  99. .str = {0xc3, 0x9f, 0x00},
  100. .ncf = {0x73, 0x73, 0x00},
  101. },
  102. {
  103. /* LATIN CAPITAL LETTER A WITH RING ABOVE folds to
  104. LATIN SMALL LETTER A + COMBINING RING ABOVE */
  105. .str = {0xC3, 0x85, 0x00},
  106. .ncf = {0x61, 0xcc, 0x8a, 0x00},
  107. },
  108. /* Introduced by UTF-8.0.0. */
  109. /* Cherokee letters are interesting test-cases because they fold
  110. to upper-case. Before 8.0.0, Cherokee lowercase were
  111. undefined, thus, the folding from LC is not stable between
  112. 7.0.0 -> 8.0.0, but it is from UC. */
  113. {
  114. /* CHEROKEE SMALL LETTER A folds to CHEROKEE LETTER A */
  115. .str = {0xea, 0xad, 0xb0, 0x00},
  116. .ncf = {0xe1, 0x8e, 0xa0, 0x00},
  117. },
  118. {
  119. /* CHEROKEE SMALL LETTER YE folds to CHEROKEE LETTER YE */
  120. .str = {0xe1, 0x8f, 0xb8, 0x00},
  121. .ncf = {0xe1, 0x8f, 0xb0, 0x00},
  122. },
  123. {
  124. /* OLD HUNGARIAN CAPITAL LETTER AMB folds to
  125. OLD HUNGARIAN SMALL LETTER AMB */
  126. .str = {0xf0, 0x90, 0xb2, 0x83, 0x00},
  127. .ncf = {0xf0, 0x90, 0xb3, 0x83, 0x00},
  128. },
  129. /* Introduced by UTF-9.0.0. */
  130. {
  131. /* OSAGE CAPITAL LETTER CHA folds to
  132. OSAGE SMALL LETTER CHA */
  133. .str = {0xf0, 0x90, 0x92, 0xb5, 0x00},
  134. .ncf = {0xf0, 0x90, 0x93, 0x9d, 0x00},
  135. },
  136. {
  137. /* LATIN CAPITAL LETTER SMALL CAPITAL I folds to
  138. LATIN LETTER SMALL CAPITAL I */
  139. .str = {0xea, 0x9e, 0xae, 0x00},
  140. .ncf = {0xc9, 0xaa, 0x00},
  141. },
  142. /* Introduced by UTF-11.0.0. */
  143. {
  144. /* GEORGIAN SMALL LETTER AN folds to GEORGIAN MTAVRULI
  145. CAPITAL LETTER AN */
  146. .str = {0xe1, 0xb2, 0x90, 0x00},
  147. .ncf = {0xe1, 0x83, 0x90, 0x00},
  148. }
  149. };
  150. static ssize_t utf8len(const struct unicode_map *um, enum utf8_normalization n,
  151. const char *s)
  152. {
  153. return utf8nlen(um, n, s, (size_t)-1);
  154. }
  155. static int utf8cursor(struct utf8cursor *u8c, const struct unicode_map *um,
  156. enum utf8_normalization n, const char *s)
  157. {
  158. return utf8ncursor(u8c, um, n, s, (unsigned int)-1);
  159. }
  160. static void check_utf8_nfdi(struct unicode_map *um)
  161. {
  162. int i;
  163. struct utf8cursor u8c;
  164. for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
  165. int len = strlen(nfdi_test_data[i].str);
  166. int nlen = strlen(nfdi_test_data[i].dec);
  167. int j = 0;
  168. unsigned char c;
  169. test((utf8len(um, UTF8_NFDI, nfdi_test_data[i].str) == nlen));
  170. test((utf8nlen(um, UTF8_NFDI, nfdi_test_data[i].str, len) ==
  171. nlen));
  172. if (utf8cursor(&u8c, um, UTF8_NFDI, nfdi_test_data[i].str) < 0)
  173. pr_err("can't create cursor\n");
  174. while ((c = utf8byte(&u8c)) > 0) {
  175. test_f((c == nfdi_test_data[i].dec[j]),
  176. "Unexpected byte 0x%x should be 0x%x\n",
  177. c, nfdi_test_data[i].dec[j]);
  178. j++;
  179. }
  180. test((j == nlen));
  181. }
  182. }
  183. static void check_utf8_nfdicf(struct unicode_map *um)
  184. {
  185. int i;
  186. struct utf8cursor u8c;
  187. for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) {
  188. int len = strlen(nfdicf_test_data[i].str);
  189. int nlen = strlen(nfdicf_test_data[i].ncf);
  190. int j = 0;
  191. unsigned char c;
  192. test((utf8len(um, UTF8_NFDICF, nfdicf_test_data[i].str) ==
  193. nlen));
  194. test((utf8nlen(um, UTF8_NFDICF, nfdicf_test_data[i].str, len) ==
  195. nlen));
  196. if (utf8cursor(&u8c, um, UTF8_NFDICF,
  197. nfdicf_test_data[i].str) < 0)
  198. pr_err("can't create cursor\n");
  199. while ((c = utf8byte(&u8c)) > 0) {
  200. test_f((c == nfdicf_test_data[i].ncf[j]),
  201. "Unexpected byte 0x%x should be 0x%x\n",
  202. c, nfdicf_test_data[i].ncf[j]);
  203. j++;
  204. }
  205. test((j == nlen));
  206. }
  207. }
  208. static void check_utf8_comparisons(struct unicode_map *table)
  209. {
  210. int i;
  211. for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) {
  212. const struct qstr s1 = {.name = nfdi_test_data[i].str,
  213. .len = sizeof(nfdi_test_data[i].str)};
  214. const struct qstr s2 = {.name = nfdi_test_data[i].dec,
  215. .len = sizeof(nfdi_test_data[i].dec)};
  216. test_f(!utf8_strncmp(table, &s1, &s2),
  217. "%s %s comparison mismatch\n", s1.name, s2.name);
  218. }
  219. for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) {
  220. const struct qstr s1 = {.name = nfdicf_test_data[i].str,
  221. .len = sizeof(nfdicf_test_data[i].str)};
  222. const struct qstr s2 = {.name = nfdicf_test_data[i].ncf,
  223. .len = sizeof(nfdicf_test_data[i].ncf)};
  224. test_f(!utf8_strncasecmp(table, &s1, &s2),
  225. "%s %s comparison mismatch\n", s1.name, s2.name);
  226. }
  227. }
  228. static void check_supported_versions(struct unicode_map *um)
  229. {
  230. /* Unicode 7.0.0 should be supported. */
  231. test(utf8version_is_supported(um, UNICODE_AGE(7, 0, 0)));
  232. /* Unicode 9.0.0 should be supported. */
  233. test(utf8version_is_supported(um, UNICODE_AGE(9, 0, 0)));
  234. /* Unicode 1x.0.0 (the latest version) should be supported. */
  235. test(utf8version_is_supported(um, UTF8_LATEST));
  236. /* Next versions don't exist. */
  237. test(!utf8version_is_supported(um, UNICODE_AGE(13, 0, 0)));
  238. test(!utf8version_is_supported(um, UNICODE_AGE(0, 0, 0)));
  239. test(!utf8version_is_supported(um, UNICODE_AGE(-1, -1, -1)));
  240. }
  241. static int __init init_test_ucd(void)
  242. {
  243. struct unicode_map *um;
  244. failed_tests = 0;
  245. total_tests = 0;
  246. um = utf8_load(UTF8_LATEST);
  247. if (IS_ERR(um)) {
  248. pr_err("%s: Unable to load utf8 table.\n", __func__);
  249. return PTR_ERR(um);
  250. }
  251. check_supported_versions(um);
  252. check_utf8_nfdi(um);
  253. check_utf8_nfdicf(um);
  254. check_utf8_comparisons(um);
  255. if (!failed_tests)
  256. pr_info("All %u tests passed\n", total_tests);
  257. else
  258. pr_err("%u out of %u tests failed\n", failed_tests,
  259. total_tests);
  260. utf8_unload(um);
  261. return 0;
  262. }
  263. static void __exit exit_test_ucd(void)
  264. {
  265. }
  266. module_init(init_test_ucd);
  267. module_exit(exit_test_ucd);
  268. MODULE_AUTHOR("Gabriel Krisman Bertazi <[email protected]>");
  269. MODULE_LICENSE("GPL");