utf8-selftest.c 8.1 KB

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