unicode_ut.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. /* Letter not in code page 437 */
  45. static const char d5[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F,
  46. 0x74, 0x20, 0x42, 0x00};
  47. /* Illegal utf-8 strings */
  48. static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
  49. static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
  50. static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
  51. static const char j4[] = {0xa1, 0x00};
  52. static int unicode_test_u16_strlen(struct unit_test_state *uts)
  53. {
  54. ut_asserteq(6, u16_strlen(c1));
  55. ut_asserteq(8, u16_strlen(c2));
  56. ut_asserteq(3, u16_strlen(c3));
  57. ut_asserteq(6, u16_strlen(c4));
  58. return 0;
  59. }
  60. UNICODE_TEST(unicode_test_u16_strlen);
  61. static int unicode_test_u16_strdup(struct unit_test_state *uts)
  62. {
  63. u16 *copy = u16_strdup(c4);
  64. ut_assert(copy != c4);
  65. ut_asserteq_mem(copy, c4, sizeof(c4));
  66. free(copy);
  67. return 0;
  68. }
  69. UNICODE_TEST(unicode_test_u16_strdup);
  70. static int unicode_test_u16_strcpy(struct unit_test_state *uts)
  71. {
  72. u16 *r;
  73. u16 copy[10];
  74. r = u16_strcpy(copy, c1);
  75. ut_assert(r == copy);
  76. ut_asserteq_mem(copy, c1, sizeof(c1));
  77. return 0;
  78. }
  79. UNICODE_TEST(unicode_test_u16_strcpy);
  80. /* U-Boot uses UTF-16 strings in the EFI context only. */
  81. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  82. static int unicode_test_string16(struct unit_test_state *uts)
  83. {
  84. char buf[20];
  85. /* Test length and precision */
  86. memset(buf, 0xff, sizeof(buf));
  87. sprintf(buf, "%8.6ls", c2);
  88. ut_asserteq(' ', buf[1]);
  89. ut_assert(!strncmp(&buf[2], d2, 7));
  90. ut_assert(!buf[9]);
  91. memset(buf, 0xff, sizeof(buf));
  92. sprintf(buf, "%8.6ls", c4);
  93. ut_asserteq(' ', buf[4]);
  94. ut_assert(!strncmp(&buf[5], d4, 12));
  95. ut_assert(!buf[17]);
  96. memset(buf, 0xff, sizeof(buf));
  97. sprintf(buf, "%-8.2ls", c4);
  98. ut_asserteq(' ', buf[8]);
  99. ut_assert(!strncmp(buf, d4, 8));
  100. ut_assert(!buf[14]);
  101. /* Test handling of illegal utf-16 sequences */
  102. memset(buf, 0xff, sizeof(buf));
  103. sprintf(buf, "%ls", i1);
  104. ut_asserteq_str("i1?l", buf);
  105. memset(buf, 0xff, sizeof(buf));
  106. sprintf(buf, "%ls", i2);
  107. ut_asserteq_str("i2?l", buf);
  108. memset(buf, 0xff, sizeof(buf));
  109. sprintf(buf, "%ls", i3);
  110. ut_asserteq_str("i3?", buf);
  111. return 0;
  112. }
  113. UNICODE_TEST(unicode_test_string16);
  114. #endif
  115. static int unicode_test_utf8_get(struct unit_test_state *uts)
  116. {
  117. const char *s;
  118. s32 code;
  119. int i;
  120. /* Check characters less than 0x800 */
  121. s = d2;
  122. for (i = 0; i < 8; ++i) {
  123. code = utf8_get((const char **)&s);
  124. /* c2 is the utf-8 encoding of d2 */
  125. ut_asserteq(c2[i], code);
  126. if (!code)
  127. break;
  128. }
  129. ut_asserteq_ptr(s, d2 + 9)
  130. /* Check characters less than 0x10000 */
  131. s = d3;
  132. for (i = 0; i < 4; ++i) {
  133. code = utf8_get((const char **)&s);
  134. /* c3 is the utf-8 encoding of d3 */
  135. ut_asserteq(c3[i], code);
  136. if (!code)
  137. break;
  138. }
  139. ut_asserteq_ptr(s, d3 + 9)
  140. /* Check character greater 0xffff */
  141. s = d4;
  142. code = utf8_get((const char **)&s);
  143. ut_asserteq(0x0001048d, code);
  144. ut_asserteq_ptr(s, d4 + 4);
  145. /* Check illegal character */
  146. s = j4;
  147. code = utf8_get((const char **)&s);
  148. ut_asserteq(-1, code);
  149. ut_asserteq_ptr(j4 + 1, s);
  150. return 0;
  151. }
  152. UNICODE_TEST(unicode_test_utf8_get);
  153. static int unicode_test_utf8_put(struct unit_test_state *uts)
  154. {
  155. char buffer[8] = { 0, };
  156. char *pos;
  157. /* Commercial at, translates to one character */
  158. pos = buffer;
  159. ut_assert(!utf8_put('@', &pos))
  160. ut_asserteq(1, pos - buffer);
  161. ut_asserteq('@', buffer[0]);
  162. ut_assert(!buffer[1]);
  163. /* Latin letter G with acute, translates to two charactes */
  164. pos = buffer;
  165. ut_assert(!utf8_put(0x1f4, &pos));
  166. ut_asserteq(2, pos - buffer);
  167. ut_asserteq_str("\xc7\xb4", buffer);
  168. /* Tagalog letter i, translates to three characters */
  169. pos = buffer;
  170. ut_assert(!utf8_put(0x1701, &pos));
  171. ut_asserteq(3, pos - buffer);
  172. ut_asserteq_str("\xe1\x9c\x81", buffer);
  173. /* Hamster face, translates to four characters */
  174. pos = buffer;
  175. ut_assert(!utf8_put(0x1f439, &pos));
  176. ut_asserteq(4, pos - buffer);
  177. ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
  178. /* Illegal code */
  179. pos = buffer;
  180. ut_asserteq(-1, utf8_put(0xd888, &pos));
  181. return 0;
  182. }
  183. UNICODE_TEST(unicode_test_utf8_put);
  184. static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
  185. {
  186. ut_asserteq(6, utf8_utf16_strlen(d1));
  187. ut_asserteq(8, utf8_utf16_strlen(d2));
  188. ut_asserteq(3, utf8_utf16_strlen(d3));
  189. ut_asserteq(6, utf8_utf16_strlen(d4));
  190. /* illegal utf-8 sequences */
  191. ut_asserteq(4, utf8_utf16_strlen(j1));
  192. ut_asserteq(4, utf8_utf16_strlen(j2));
  193. ut_asserteq(3, utf8_utf16_strlen(j3));
  194. return 0;
  195. }
  196. UNICODE_TEST(unicode_test_utf8_utf16_strlen);
  197. static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
  198. {
  199. ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
  200. ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
  201. ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
  202. ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
  203. ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
  204. ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
  205. /* illegal utf-8 sequences */
  206. ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
  207. ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
  208. ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
  209. return 0;
  210. }
  211. UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
  212. /**
  213. * ut_u16_strcmp() - Compare to u16 strings.
  214. *
  215. * @a1: first string
  216. * @a2: second string
  217. * @count: number of u16 to compare
  218. * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
  219. */
  220. static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
  221. {
  222. for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
  223. if (*a1 < *a2)
  224. return -1;
  225. if (*a1 > *a2)
  226. return 1;
  227. }
  228. return 0;
  229. }
  230. static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
  231. {
  232. u16 buf[16];
  233. u16 *pos;
  234. pos = buf;
  235. utf8_utf16_strcpy(&pos, d1);
  236. ut_asserteq(6, pos - buf);
  237. ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
  238. pos = buf;
  239. utf8_utf16_strcpy(&pos, d2);
  240. ut_asserteq(8, pos - buf);
  241. ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
  242. pos = buf;
  243. utf8_utf16_strcpy(&pos, d3);
  244. ut_asserteq(3, pos - buf);
  245. ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
  246. pos = buf;
  247. utf8_utf16_strcpy(&pos, d4);
  248. ut_asserteq(6, pos - buf);
  249. ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
  250. /* Illegal utf-8 strings */
  251. pos = buf;
  252. utf8_utf16_strcpy(&pos, j1);
  253. ut_asserteq(4, pos - buf);
  254. ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
  255. pos = buf;
  256. utf8_utf16_strcpy(&pos, j2);
  257. ut_asserteq(4, pos - buf);
  258. ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
  259. pos = buf;
  260. utf8_utf16_strcpy(&pos, j3);
  261. ut_asserteq(3, pos - buf);
  262. ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
  263. return 0;
  264. }
  265. UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
  266. static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
  267. {
  268. u16 buf[16];
  269. u16 *pos;
  270. pos = buf;
  271. memset(buf, 0, sizeof(buf));
  272. utf8_utf16_strncpy(&pos, d1, 4);
  273. ut_asserteq(4, pos - buf);
  274. ut_assert(!buf[4]);
  275. ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
  276. pos = buf;
  277. memset(buf, 0, sizeof(buf));
  278. utf8_utf16_strncpy(&pos, d2, 10);
  279. ut_asserteq(8, pos - buf);
  280. ut_assert(buf[4]);
  281. ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
  282. pos = buf;
  283. memset(buf, 0, sizeof(buf));
  284. utf8_utf16_strncpy(&pos, d3, 2);
  285. ut_asserteq(2, pos - buf);
  286. ut_assert(!buf[2]);
  287. ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
  288. pos = buf;
  289. memset(buf, 0, sizeof(buf));
  290. utf8_utf16_strncpy(&pos, d4, 2);
  291. ut_asserteq(4, pos - buf);
  292. ut_assert(!buf[4]);
  293. ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
  294. pos = buf;
  295. memset(buf, 0, sizeof(buf));
  296. utf8_utf16_strncpy(&pos, d4, 10);
  297. ut_asserteq(6, pos - buf);
  298. ut_assert(buf[5]);
  299. ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
  300. return 0;
  301. }
  302. UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
  303. static int unicode_test_utf16_get(struct unit_test_state *uts)
  304. {
  305. const u16 *s;
  306. s32 code;
  307. int i;
  308. /* Check characters less than 0x10000 */
  309. s = c2;
  310. for (i = 0; i < 9; ++i) {
  311. code = utf16_get((const u16 **)&s);
  312. ut_asserteq(c2[i], code);
  313. if (!code)
  314. break;
  315. }
  316. ut_asserteq_ptr(c2 + 8, s);
  317. /* Check character greater 0xffff */
  318. s = c4;
  319. code = utf16_get((const u16 **)&s);
  320. ut_asserteq(0x0001048d, code);
  321. ut_asserteq_ptr(c4 + 2, s);
  322. return 0;
  323. }
  324. UNICODE_TEST(unicode_test_utf16_get);
  325. static int unicode_test_utf16_put(struct unit_test_state *uts)
  326. {
  327. u16 buffer[4] = { 0, };
  328. u16 *pos;
  329. /* Commercial at, translates to one word */
  330. pos = buffer;
  331. ut_assert(!utf16_put('@', &pos));
  332. ut_asserteq(1, pos - buffer);
  333. ut_asserteq((u16)'@', buffer[0]);
  334. ut_assert(!buffer[1]);
  335. /* Hamster face, translates to two words */
  336. pos = buffer;
  337. ut_assert(!utf16_put(0x1f439, &pos));
  338. ut_asserteq(2, pos - buffer);
  339. ut_asserteq((u16)0xd83d, buffer[0]);
  340. ut_asserteq((u16)0xdc39, buffer[1]);
  341. ut_assert(!buffer[2]);
  342. /* Illegal code */
  343. pos = buffer;
  344. ut_asserteq(-1, utf16_put(0xd888, &pos));
  345. return 0;
  346. }
  347. UNICODE_TEST(unicode_test_utf16_put);
  348. static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
  349. {
  350. ut_asserteq(3, utf16_strnlen(c1, 3));
  351. ut_asserteq(6, utf16_strnlen(c1, 13));
  352. ut_asserteq(6, utf16_strnlen(c2, 6));
  353. ut_asserteq(2, utf16_strnlen(c3, 2));
  354. ut_asserteq(2, utf16_strnlen(c4, 2));
  355. ut_asserteq(3, utf16_strnlen(c4, 3));
  356. /* illegal utf-16 word sequences */
  357. ut_asserteq(4, utf16_strnlen(i1, 16));
  358. ut_asserteq(4, utf16_strnlen(i2, 16));
  359. ut_asserteq(3, utf16_strnlen(i3, 16));
  360. return 0;
  361. }
  362. UNICODE_TEST(unicode_test_utf16_strnlen);
  363. static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
  364. {
  365. ut_asserteq(6, utf16_utf8_strlen(c1));
  366. ut_asserteq(9, utf16_utf8_strlen(c2));
  367. ut_asserteq(9, utf16_utf8_strlen(c3));
  368. ut_asserteq(12, utf16_utf8_strlen(c4));
  369. /* illegal utf-16 word sequences */
  370. ut_asserteq(4, utf16_utf8_strlen(i1));
  371. ut_asserteq(4, utf16_utf8_strlen(i2));
  372. ut_asserteq(3, utf16_utf8_strlen(i3));
  373. return 0;
  374. }
  375. UNICODE_TEST(unicode_test_utf16_utf8_strlen);
  376. static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
  377. {
  378. ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
  379. ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
  380. ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
  381. ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
  382. ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
  383. ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
  384. return 0;
  385. }
  386. UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
  387. static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
  388. {
  389. char buf[16];
  390. char *pos;
  391. pos = buf;
  392. utf16_utf8_strcpy(&pos, c1);
  393. ut_asserteq(6, pos - buf);
  394. ut_asserteq_str(d1, buf);
  395. pos = buf;
  396. utf16_utf8_strcpy(&pos, c2);
  397. ut_asserteq(9, pos - buf);
  398. ut_asserteq_str(d2, buf);
  399. pos = buf;
  400. utf16_utf8_strcpy(&pos, c3);
  401. ut_asserteq(9, pos - buf);
  402. ut_asserteq_str(d3, buf);
  403. pos = buf;
  404. utf16_utf8_strcpy(&pos, c4);
  405. ut_asserteq(12, pos - buf);
  406. ut_asserteq_str(d4, buf);
  407. /* Illegal utf-16 strings */
  408. pos = buf;
  409. utf16_utf8_strcpy(&pos, i1);
  410. ut_asserteq(4, pos - buf);
  411. ut_asserteq_str("i1?l", buf);
  412. pos = buf;
  413. utf16_utf8_strcpy(&pos, i2);
  414. ut_asserteq(4, pos - buf);
  415. ut_asserteq_str("i2?l", buf);
  416. pos = buf;
  417. utf16_utf8_strcpy(&pos, i3);
  418. ut_asserteq(3, pos - buf);
  419. ut_asserteq_str("i3?", buf);
  420. return 0;
  421. }
  422. UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
  423. static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
  424. {
  425. char buf[16];
  426. char *pos;
  427. pos = buf;
  428. memset(buf, 0, sizeof(buf));
  429. utf16_utf8_strncpy(&pos, c1, 4);
  430. ut_asserteq(4, pos - buf);
  431. ut_assert(!buf[4]);
  432. ut_assert(!strncmp(buf, d1, 4));
  433. pos = buf;
  434. memset(buf, 0, sizeof(buf));
  435. utf16_utf8_strncpy(&pos, c2, 10);
  436. ut_asserteq(9, pos - buf);
  437. ut_assert(buf[4]);
  438. ut_assert(!strncmp(buf, d2, SIZE_MAX));
  439. pos = buf;
  440. memset(buf, 0, sizeof(buf));
  441. utf16_utf8_strncpy(&pos, c3, 2);
  442. ut_asserteq(6, pos - buf);
  443. ut_assert(!buf[6]);
  444. ut_assert(!strncmp(buf, d3, 6));
  445. pos = buf;
  446. memset(buf, 0, sizeof(buf));
  447. utf16_utf8_strncpy(&pos, c4, 2);
  448. ut_asserteq(8, pos - buf);
  449. ut_assert(!buf[8]);
  450. ut_assert(!strncmp(buf, d4, 8));
  451. pos = buf;
  452. memset(buf, 0, sizeof(buf));
  453. utf16_utf8_strncpy(&pos, c4, 10);
  454. ut_asserteq(12, pos - buf);
  455. ut_assert(buf[5]);
  456. ut_assert(!strncmp(buf, d4, SIZE_MAX));
  457. return 0;
  458. }
  459. UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
  460. static int unicode_test_utf_to_lower(struct unit_test_state *uts)
  461. {
  462. ut_asserteq('@', utf_to_lower('@'));
  463. ut_asserteq('a', utf_to_lower('A'));
  464. ut_asserteq('z', utf_to_lower('Z'));
  465. ut_asserteq('[', utf_to_lower('['));
  466. ut_asserteq('m', utf_to_lower('m'));
  467. /* Latin letter O with diaresis (umlaut) */
  468. ut_asserteq(0x00f6, utf_to_lower(0x00d6));
  469. #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  470. /* Cyrillic letter I*/
  471. ut_asserteq(0x0438, utf_to_lower(0x0418));
  472. #endif
  473. return 0;
  474. }
  475. UNICODE_TEST(unicode_test_utf_to_lower);
  476. static int unicode_test_utf_to_upper(struct unit_test_state *uts)
  477. {
  478. ut_asserteq('`', utf_to_upper('`'));
  479. ut_asserteq('A', utf_to_upper('a'));
  480. ut_asserteq('Z', utf_to_upper('z'));
  481. ut_asserteq('{', utf_to_upper('{'));
  482. ut_asserteq('M', utf_to_upper('M'));
  483. /* Latin letter O with diaresis (umlaut) */
  484. ut_asserteq(0x00d6, utf_to_upper(0x00f6));
  485. #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
  486. /* Cyrillic letter I */
  487. ut_asserteq(0x0418, utf_to_upper(0x0438));
  488. #endif
  489. return 0;
  490. }
  491. UNICODE_TEST(unicode_test_utf_to_upper);
  492. static int unicode_test_u16_strncmp(struct unit_test_state *uts)
  493. {
  494. ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
  495. ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
  496. ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
  497. ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
  498. ut_assert(u16_strcmp(L"abc", L"abc") == 0);
  499. ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
  500. ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
  501. return 0;
  502. }
  503. UNICODE_TEST(unicode_test_u16_strncmp);
  504. static int unicode_test_u16_strsize(struct unit_test_state *uts)
  505. {
  506. ut_asserteq_64(u16_strsize(c1), 14);
  507. ut_asserteq_64(u16_strsize(c2), 18);
  508. ut_asserteq_64(u16_strsize(c3), 8);
  509. ut_asserteq_64(u16_strsize(c4), 14);
  510. return 0;
  511. }
  512. UNICODE_TEST(unicode_test_u16_strsize);
  513. static int unicode_test_utf_to_cp(struct unit_test_state *uts)
  514. {
  515. int ret;
  516. s32 c;
  517. c = '\n';
  518. ret = utf_to_cp(&c, codepage_437);
  519. ut_asserteq(0, ret);
  520. ut_asserteq('\n', c);
  521. c = 'a';
  522. ret = utf_to_cp(&c, codepage_437);
  523. ut_asserteq(0, ret);
  524. ut_asserteq('a', c);
  525. c = 0x03c4; /* Greek small letter tau */
  526. ret = utf_to_cp(&c, codepage_437);
  527. ut_asserteq(0, ret);
  528. ut_asserteq(0xe7, c);
  529. c = 0x03a4; /* Greek capital letter tau */
  530. ret = utf_to_cp(&c, codepage_437);
  531. ut_asserteq(-ENOENT, ret);
  532. ut_asserteq('?', c);
  533. return 0;
  534. }
  535. UNICODE_TEST(unicode_test_utf_to_cp);
  536. static void utf8_to_cp437_stream_helper(const char *in, char *out)
  537. {
  538. char buffer[5];
  539. int ret;
  540. *buffer = 0;
  541. for (; *in; ++in) {
  542. ret = utf8_to_cp437_stream(*in, buffer);
  543. if (ret)
  544. *out++ = ret;
  545. }
  546. *out = 0;
  547. }
  548. static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts)
  549. {
  550. char buf[16];
  551. utf8_to_cp437_stream_helper(d1, buf);
  552. ut_asserteq_str("U-Boot", buf);
  553. utf8_to_cp437_stream_helper(d2, buf);
  554. ut_asserteq_str("kafb\xa0tur", buf);
  555. utf8_to_cp437_stream_helper(d5, buf);
  556. ut_asserteq_str("? is not B", buf);
  557. utf8_to_cp437_stream_helper(j2, buf);
  558. ut_asserteq_str("j2l", buf);
  559. return 0;
  560. }
  561. UNICODE_TEST(unicode_test_utf8_to_cp437_stream);
  562. static void utf8_to_utf32_stream_helper(const char *in, s32 *out)
  563. {
  564. char buffer[5];
  565. int ret;
  566. *buffer = 0;
  567. for (; *in; ++in) {
  568. ret = utf8_to_utf32_stream(*in, buffer);
  569. if (ret)
  570. *out++ = ret;
  571. }
  572. *out = 0;
  573. }
  574. static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts)
  575. {
  576. s32 buf[16];
  577. const u32 u1[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000};
  578. const u32 u2[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00};
  579. const u32 u3[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74,
  580. 0x20, 0x42, 0x00};
  581. const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00};
  582. memset(buf, 0, sizeof(buf));
  583. utf8_to_utf32_stream_helper(d1, buf);
  584. ut_asserteq_mem(u1, buf, sizeof(u1));
  585. memset(buf, 0, sizeof(buf));
  586. utf8_to_utf32_stream_helper(d2, buf);
  587. ut_asserteq_mem(u2, buf, sizeof(u2));
  588. memset(buf, 0, sizeof(buf));
  589. utf8_to_utf32_stream_helper(d5, buf);
  590. ut_asserteq_mem(u3, buf, sizeof(u3));
  591. memset(buf, 0, sizeof(buf));
  592. utf8_to_utf32_stream_helper(j2, buf);
  593. ut_asserteq_mem(u4, buf, sizeof(u4));
  594. return 0;
  595. }
  596. UNICODE_TEST(unicode_test_utf8_to_utf32_stream);
  597. #ifdef CONFIG_EFI_LOADER
  598. static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
  599. {
  600. u16 buf[16];
  601. u16 const expected[] = L"Capsule0AF9";
  602. u16 *pos;
  603. memset(buf, 0xeb, sizeof(buf));
  604. pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
  605. ut_asserteq_mem(expected, buf, sizeof(expected));
  606. ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
  607. return 0;
  608. }
  609. UNICODE_TEST(unicode_test_efi_create_indexed_name);
  610. #endif
  611. int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  612. {
  613. struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
  614. const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test);
  615. return cmd_ut_category("Unicode", "unicode_test_",
  616. tests, n_ents, argc, argv);
  617. }