unicode_ut.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Unit tests for Unicode functions
  4. *
  5. * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <command.h>
  10. #include <efi_loader.h>
  11. #include <errno.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <test/test.h>
  15. #include <test/suites.h>
  16. #include <test/ut.h>
  17. /* Linker list entry for a Unicode test */
  18. #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
  19. /* Constants c1-c4 and d1-d4 encode the same letters */
  20. /* Six characters translating to one utf-8 byte each. */
  21. static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  22. /* One character translating to two utf-8 bytes */
  23. static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
  24. /* Three characters translating to three utf-8 bytes each */
  25. static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
  26. /* Three letters translating to four utf-8 bytes each */
  27. static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
  28. 0x0000};
  29. /* Illegal utf-16 strings */
  30. static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
  31. static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
  32. static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
  33. /* Six characters translating to one utf-16 word each. */
  34. static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
  35. /* Eight characters translating to one utf-16 word each */
  36. static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
  37. 0x72, 0x00};
  38. /* Three characters translating to one utf-16 word each */
  39. static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
  40. 0xa6, 0x00};
  41. /* Three letters translating to two utf-16 word each */
  42. static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
  43. 0xf0, 0x90, 0x92, 0x87, 0x00};
  44. /* Illegal utf-8 strings */
  45. static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
  46. static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
  47. static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
  48. static int unicode_test_u16_strlen(struct unit_test_state *uts)
  49. {
  50. ut_asserteq(6, u16_strlen(c1));
  51. ut_asserteq(8, u16_strlen(c2));
  52. ut_asserteq(3, u16_strlen(c3));
  53. ut_asserteq(6, u16_strlen(c4));
  54. return 0;
  55. }
  56. UNICODE_TEST(unicode_test_u16_strlen);
  57. static int unicode_test_u16_strdup(struct unit_test_state *uts)
  58. {
  59. u16 *copy = u16_strdup(c4);
  60. ut_assert(copy != c4);
  61. ut_asserteq_mem(copy, c4, sizeof(c4));
  62. free(copy);
  63. return 0;
  64. }
  65. UNICODE_TEST(unicode_test_u16_strdup);
  66. static int unicode_test_u16_strcpy(struct unit_test_state *uts)
  67. {
  68. u16 *r;
  69. u16 copy[10];
  70. r = u16_strcpy(copy, c1);
  71. ut_assert(r == copy);
  72. ut_asserteq_mem(copy, c1, sizeof(c1));
  73. return 0;
  74. }
  75. UNICODE_TEST(unicode_test_u16_strcpy);
  76. /* U-Boot uses UTF-16 strings in the EFI context only. */
  77. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  78. static int unicode_test_string16(struct unit_test_state *uts)
  79. {
  80. char buf[20];
  81. /* Test length and precision */
  82. memset(buf, 0xff, sizeof(buf));
  83. sprintf(buf, "%8.6ls", c2);
  84. ut_asserteq(' ', buf[1]);
  85. ut_assert(!strncmp(&buf[2], d2, 7));
  86. ut_assert(!buf[9]);
  87. memset(buf, 0xff, sizeof(buf));
  88. sprintf(buf, "%8.6ls", c4);
  89. ut_asserteq(' ', buf[4]);
  90. ut_assert(!strncmp(&buf[5], d4, 12));
  91. ut_assert(!buf[17]);
  92. memset(buf, 0xff, sizeof(buf));
  93. sprintf(buf, "%-8.2ls", c4);
  94. ut_asserteq(' ', buf[8]);
  95. ut_assert(!strncmp(buf, d4, 8));
  96. ut_assert(!buf[14]);
  97. /* Test handling of illegal utf-16 sequences */
  98. memset(buf, 0xff, sizeof(buf));
  99. sprintf(buf, "%ls", i1);
  100. ut_asserteq_str("i1?l", buf);
  101. memset(buf, 0xff, sizeof(buf));
  102. sprintf(buf, "%ls", i2);
  103. ut_asserteq_str("i2?l", buf);
  104. memset(buf, 0xff, sizeof(buf));
  105. sprintf(buf, "%ls", i3);
  106. ut_asserteq_str("i3?", buf);
  107. return 0;
  108. }
  109. UNICODE_TEST(unicode_test_string16);
  110. #endif
  111. static int unicode_test_utf8_get(struct unit_test_state *uts)
  112. {
  113. const char *s;
  114. s32 code;
  115. int i;
  116. /* Check characters less than 0x800 */
  117. s = d2;
  118. for (i = 0; i < 8; ++i) {
  119. code = utf8_get((const char **)&s);
  120. /* c2 is the utf-8 encoding of d2 */
  121. ut_asserteq(c2[i], code);
  122. if (!code)
  123. break;
  124. }
  125. ut_asserteq_ptr(s, d2 + 9)
  126. /* Check characters less than 0x10000 */
  127. s = d3;
  128. for (i = 0; i < 4; ++i) {
  129. code = utf8_get((const char **)&s);
  130. /* c3 is the utf-8 encoding of d3 */
  131. ut_asserteq(c3[i], code);
  132. if (!code)
  133. break;
  134. }
  135. ut_asserteq_ptr(s, d3 + 9)
  136. /* Check character greater 0xffff */
  137. s = d4;
  138. code = utf8_get((const char **)&s);
  139. ut_asserteq(0x0001048d, code);
  140. ut_asserteq_ptr(s, d4 + 4);
  141. return 0;
  142. }
  143. UNICODE_TEST(unicode_test_utf8_get);
  144. static int unicode_test_utf8_put(struct unit_test_state *uts)
  145. {
  146. char buffer[8] = { 0, };
  147. char *pos;
  148. /* Commercial at, translates to one character */
  149. pos = buffer;
  150. ut_assert(!utf8_put('@', &pos))
  151. ut_asserteq(1, pos - buffer);
  152. ut_asserteq('@', buffer[0]);
  153. ut_assert(!buffer[1]);
  154. /* Latin letter G with acute, translates to two charactes */
  155. pos = buffer;
  156. ut_assert(!utf8_put(0x1f4, &pos));
  157. ut_asserteq(2, pos - buffer);
  158. ut_asserteq_str("\xc7\xb4", buffer);
  159. /* Tagalog letter i, translates to three characters */
  160. pos = buffer;
  161. ut_assert(!utf8_put(0x1701, &pos));
  162. ut_asserteq(3, pos - buffer);
  163. ut_asserteq_str("\xe1\x9c\x81", buffer);
  164. /* Hamster face, translates to four characters */
  165. pos = buffer;
  166. ut_assert(!utf8_put(0x1f439, &pos));
  167. ut_asserteq(4, pos - buffer);
  168. ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
  169. /* Illegal code */
  170. pos = buffer;
  171. ut_asserteq(-1, utf8_put(0xd888, &pos));
  172. return 0;
  173. }
  174. UNICODE_TEST(unicode_test_utf8_put);
  175. static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
  176. {
  177. ut_asserteq(6, utf8_utf16_strlen(d1));
  178. ut_asserteq(8, utf8_utf16_strlen(d2));
  179. ut_asserteq(3, utf8_utf16_strlen(d3));
  180. ut_asserteq(6, utf8_utf16_strlen(d4));
  181. /* illegal utf-8 sequences */
  182. ut_asserteq(4, utf8_utf16_strlen(j1));
  183. ut_asserteq(4, utf8_utf16_strlen(j2));
  184. ut_asserteq(3, utf8_utf16_strlen(j3));
  185. return 0;
  186. }
  187. UNICODE_TEST(unicode_test_utf8_utf16_strlen);
  188. static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
  189. {
  190. ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
  191. ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
  192. ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
  193. ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
  194. ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
  195. ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
  196. /* illegal utf-8 sequences */
  197. ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
  198. ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
  199. ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
  200. return 0;
  201. }
  202. UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
  203. /**
  204. * ut_u16_strcmp() - Compare to u16 strings.
  205. *
  206. * @a1: first string
  207. * @a2: second string
  208. * @count: number of u16 to compare
  209. * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
  210. */
  211. static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
  212. {
  213. for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
  214. if (*a1 < *a2)
  215. return -1;
  216. if (*a1 > *a2)
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
  222. {
  223. u16 buf[16];
  224. u16 *pos;
  225. pos = buf;
  226. utf8_utf16_strcpy(&pos, d1);
  227. ut_asserteq(6, pos - buf);
  228. ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
  229. pos = buf;
  230. utf8_utf16_strcpy(&pos, d2);
  231. ut_asserteq(8, pos - buf);
  232. ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
  233. pos = buf;
  234. utf8_utf16_strcpy(&pos, d3);
  235. ut_asserteq(3, pos - buf);
  236. ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
  237. pos = buf;
  238. utf8_utf16_strcpy(&pos, d4);
  239. ut_asserteq(6, pos - buf);
  240. ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
  241. /* Illegal utf-8 strings */
  242. pos = buf;
  243. utf8_utf16_strcpy(&pos, j1);
  244. ut_asserteq(4, pos - buf);
  245. ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
  246. pos = buf;
  247. utf8_utf16_strcpy(&pos, j2);
  248. ut_asserteq(4, pos - buf);
  249. ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
  250. pos = buf;
  251. utf8_utf16_strcpy(&pos, j3);
  252. ut_asserteq(3, pos - buf);
  253. ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
  254. return 0;
  255. }
  256. UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
  257. static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
  258. {
  259. u16 buf[16];
  260. u16 *pos;
  261. pos = buf;
  262. memset(buf, 0, sizeof(buf));
  263. utf8_utf16_strncpy(&pos, d1, 4);
  264. ut_asserteq(4, pos - buf);
  265. ut_assert(!buf[4]);
  266. ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
  267. pos = buf;
  268. memset(buf, 0, sizeof(buf));
  269. utf8_utf16_strncpy(&pos, d2, 10);
  270. ut_asserteq(8, pos - buf);
  271. ut_assert(buf[4]);
  272. ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
  273. pos = buf;
  274. memset(buf, 0, sizeof(buf));
  275. utf8_utf16_strncpy(&pos, d3, 2);
  276. ut_asserteq(2, pos - buf);
  277. ut_assert(!buf[2]);
  278. ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
  279. pos = buf;
  280. memset(buf, 0, sizeof(buf));
  281. utf8_utf16_strncpy(&pos, d4, 2);
  282. ut_asserteq(4, pos - buf);
  283. ut_assert(!buf[4]);
  284. ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
  285. pos = buf;
  286. memset(buf, 0, sizeof(buf));
  287. utf8_utf16_strncpy(&pos, d4, 10);
  288. ut_asserteq(6, pos - buf);
  289. ut_assert(buf[5]);
  290. ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
  291. return 0;
  292. }
  293. UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
  294. static int unicode_test_utf16_get(struct unit_test_state *uts)
  295. {
  296. const u16 *s;
  297. s32 code;
  298. int i;
  299. /* Check characters less than 0x10000 */
  300. s = c2;
  301. for (i = 0; i < 9; ++i) {
  302. code = utf16_get((const u16 **)&s);
  303. ut_asserteq(c2[i], code);
  304. if (!code)
  305. break;
  306. }
  307. ut_asserteq_ptr(c2 + 8, s);
  308. /* Check character greater 0xffff */
  309. s = c4;
  310. code = utf16_get((const u16 **)&s);
  311. ut_asserteq(0x0001048d, code);
  312. ut_asserteq_ptr(c4 + 2, s);
  313. return 0;
  314. }
  315. UNICODE_TEST(unicode_test_utf16_get);
  316. static int unicode_test_utf16_put(struct unit_test_state *uts)
  317. {
  318. u16 buffer[4] = { 0, };
  319. u16 *pos;
  320. /* Commercial at, translates to one word */
  321. pos = buffer;
  322. ut_assert(!utf16_put('@', &pos));
  323. ut_asserteq(1, pos - buffer);
  324. ut_asserteq((u16)'@', buffer[0]);
  325. ut_assert(!buffer[1]);
  326. /* Hamster face, translates to two words */
  327. pos = buffer;
  328. ut_assert(!utf16_put(0x1f439, &pos));
  329. ut_asserteq(2, pos - buffer);
  330. ut_asserteq((u16)0xd83d, buffer[0]);
  331. ut_asserteq((u16)0xdc39, buffer[1]);
  332. ut_assert(!buffer[2]);
  333. /* Illegal code */
  334. pos = buffer;
  335. ut_asserteq(-1, utf16_put(0xd888, &pos));
  336. return 0;
  337. }
  338. UNICODE_TEST(unicode_test_utf16_put);
  339. static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
  340. {
  341. ut_asserteq(3, utf16_strnlen(c1, 3));
  342. ut_asserteq(6, utf16_strnlen(c1, 13));
  343. ut_asserteq(6, utf16_strnlen(c2, 6));
  344. ut_asserteq(2, utf16_strnlen(c3, 2));
  345. ut_asserteq(2, utf16_strnlen(c4, 2));
  346. ut_asserteq(3, utf16_strnlen(c4, 3));
  347. /* illegal utf-16 word sequences */
  348. ut_asserteq(4, utf16_strnlen(i1, 16));
  349. ut_asserteq(4, utf16_strnlen(i2, 16));
  350. ut_asserteq(3, utf16_strnlen(i3, 16));
  351. return 0;
  352. }
  353. UNICODE_TEST(unicode_test_utf16_strnlen);
  354. static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
  355. {
  356. ut_asserteq(6, utf16_utf8_strlen(c1));
  357. ut_asserteq(9, utf16_utf8_strlen(c2));
  358. ut_asserteq(9, utf16_utf8_strlen(c3));
  359. ut_asserteq(12, utf16_utf8_strlen(c4));
  360. /* illegal utf-16 word sequences */
  361. ut_asserteq(4, utf16_utf8_strlen(i1));
  362. ut_asserteq(4, utf16_utf8_strlen(i2));
  363. ut_asserteq(3, utf16_utf8_strlen(i3));
  364. return 0;
  365. }
  366. UNICODE_TEST(unicode_test_utf16_utf8_strlen);
  367. static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
  368. {
  369. ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
  370. ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
  371. ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
  372. ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
  373. ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
  374. ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
  375. return 0;
  376. }
  377. UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
  378. static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
  379. {
  380. char buf[16];
  381. char *pos;
  382. pos = buf;
  383. utf16_utf8_strcpy(&pos, c1);
  384. ut_asserteq(6, pos - buf);
  385. ut_asserteq_str(d1, buf);
  386. pos = buf;
  387. utf16_utf8_strcpy(&pos, c2);
  388. ut_asserteq(9, pos - buf);
  389. ut_asserteq_str(d2, buf);
  390. pos = buf;
  391. utf16_utf8_strcpy(&pos, c3);
  392. ut_asserteq(9, pos - buf);
  393. ut_asserteq_str(d3, buf);
  394. pos = buf;
  395. utf16_utf8_strcpy(&pos, c4);
  396. ut_asserteq(12, pos - buf);
  397. ut_asserteq_str(d4, buf);
  398. /* Illegal utf-16 strings */
  399. pos = buf;
  400. utf16_utf8_strcpy(&pos, i1);
  401. ut_asserteq(4, pos - buf);
  402. ut_asserteq_str("i1?l", buf);
  403. pos = buf;
  404. utf16_utf8_strcpy(&pos, i2);
  405. ut_asserteq(4, pos - buf);
  406. ut_asserteq_str("i2?l", buf);
  407. pos = buf;
  408. utf16_utf8_strcpy(&pos, i3);
  409. ut_asserteq(3, pos - buf);
  410. ut_asserteq_str("i3?", buf);
  411. return 0;
  412. }
  413. UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
  414. static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
  415. {
  416. char buf[16];
  417. char *pos;
  418. pos = buf;
  419. memset(buf, 0, sizeof(buf));
  420. utf16_utf8_strncpy(&pos, c1, 4);
  421. ut_asserteq(4, pos - buf);
  422. ut_assert(!buf[4]);
  423. ut_assert(!strncmp(buf, d1, 4));
  424. pos = buf;
  425. memset(buf, 0, sizeof(buf));
  426. utf16_utf8_strncpy(&pos, c2, 10);
  427. ut_asserteq(9, pos - buf);
  428. ut_assert(buf[4]);
  429. ut_assert(!strncmp(buf, d2, SIZE_MAX));
  430. pos = buf;
  431. memset(buf, 0, sizeof(buf));
  432. utf16_utf8_strncpy(&pos, c3, 2);
  433. ut_asserteq(6, pos - buf);
  434. ut_assert(!buf[6]);
  435. ut_assert(!strncmp(buf, d3, 6));
  436. pos = buf;
  437. memset(buf, 0, sizeof(buf));
  438. utf16_utf8_strncpy(&pos, c4, 2);
  439. ut_asserteq(8, pos - buf);
  440. ut_assert(!buf[8]);
  441. ut_assert(!strncmp(buf, d4, 8));
  442. pos = buf;
  443. memset(buf, 0, sizeof(buf));
  444. utf16_utf8_strncpy(&pos, c4, 10);
  445. ut_asserteq(12, pos - buf);
  446. ut_assert(buf[5]);
  447. ut_assert(!strncmp(buf, d4, SIZE_MAX));
  448. return 0;
  449. }
  450. UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
  451. static int unicode_test_utf_to_lower(struct unit_test_state *uts)
  452. {
  453. ut_asserteq('@', utf_to_lower('@'));
  454. ut_asserteq('a', utf_to_lower('A'));
  455. ut_asserteq('z', utf_to_lower('Z'));
  456. ut_asserteq('[', utf_to_lower('['));
  457. ut_asserteq('m', utf_to_lower('m'));
  458. /* Latin letter O with diaresis (umlaut) */
  459. ut_asserteq(0x00f6, utf_to_lower(0x00d6));
  460. #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  461. /* Cyrillic letter I*/
  462. ut_asserteq(0x0438, utf_to_lower(0x0418));
  463. #endif
  464. return 0;
  465. }
  466. UNICODE_TEST(unicode_test_utf_to_lower);
  467. static int unicode_test_utf_to_upper(struct unit_test_state *uts)
  468. {
  469. ut_asserteq('`', utf_to_upper('`'));
  470. ut_asserteq('A', utf_to_upper('a'));
  471. ut_asserteq('Z', utf_to_upper('z'));
  472. ut_asserteq('{', utf_to_upper('{'));
  473. ut_asserteq('M', utf_to_upper('M'));
  474. /* Latin letter O with diaresis (umlaut) */
  475. ut_asserteq(0x00d6, utf_to_upper(0x00f6));
  476. #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  477. /* Cyrillic letter I */
  478. ut_asserteq(0x0418, utf_to_upper(0x0438));
  479. #endif
  480. return 0;
  481. }
  482. UNICODE_TEST(unicode_test_utf_to_upper);
  483. static int unicode_test_u16_strncmp(struct unit_test_state *uts)
  484. {
  485. ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
  486. ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
  487. ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
  488. ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
  489. ut_assert(u16_strcmp(L"abc", L"abc") == 0);
  490. ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
  491. ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
  492. return 0;
  493. }
  494. UNICODE_TEST(unicode_test_u16_strncmp);
  495. static int unicode_test_u16_strsize(struct unit_test_state *uts)
  496. {
  497. ut_asserteq_64(u16_strsize(c1), 14);
  498. ut_asserteq_64(u16_strsize(c2), 18);
  499. ut_asserteq_64(u16_strsize(c3), 8);
  500. ut_asserteq_64(u16_strsize(c4), 14);
  501. return 0;
  502. }
  503. UNICODE_TEST(unicode_test_u16_strsize);
  504. #ifdef CONFIG_EFI_LOADER
  505. static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
  506. {
  507. u16 buf[16];
  508. u16 const expected[] = L"Capsule0AF9";
  509. u16 *pos;
  510. memset(buf, 0xeb, sizeof(buf));
  511. pos = efi_create_indexed_name(buf, "Capsule", 0x0af9);
  512. ut_asserteq_mem(expected, buf, sizeof(expected));
  513. ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
  514. return 0;
  515. }
  516. UNICODE_TEST(unicode_test_efi_create_indexed_name);
  517. #endif
  518. int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  519. {
  520. struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
  521. const int n_ents = ll_entry_count(struct unit_test, unicode_test);
  522. return cmd_ut_category("Unicode", "unicode_test_",
  523. tests, n_ents, argc, argv);
  524. }