unicode_ut.c 22 KB

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