1 // SPDX-License-Identifier: GPL-2.0+
3 * Unit tests for Unicode functions
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
11 #include <efi_loader.h>
15 #include <test/test.h>
16 #include <test/suites.h>
19 /* Linker list entry for a Unicode test */
20 #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
22 /* Constants c1-c4 and d1-d4 encode the same letters */
24 /* Six characters translating to one utf-8 byte each. */
25 static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
26 /* One character translating to two utf-8 bytes */
27 static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
28 /* Three characters translating to three utf-8 bytes each */
29 static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
30 /* Three letters translating to four utf-8 bytes each */
31 static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
34 /* Illegal utf-16 strings */
35 static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
36 static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
37 static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
39 /* Six characters translating to one utf-16 word each. */
40 static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
41 /* Eight characters translating to one utf-16 word each */
42 static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
44 /* Three characters translating to one utf-16 word each */
45 static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
47 /* Three letters translating to two utf-16 word each */
48 static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
49 0xf0, 0x90, 0x92, 0x87, 0x00};
51 /* Illegal utf-8 strings */
52 static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
53 static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
54 static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
55 static const char j4[] = {0xa1, 0x00};
57 static int unicode_test_u16_strlen(struct unit_test_state *uts)
59 ut_asserteq(6, u16_strlen(c1));
60 ut_asserteq(8, u16_strlen(c2));
61 ut_asserteq(3, u16_strlen(c3));
62 ut_asserteq(6, u16_strlen(c4));
65 UNICODE_TEST(unicode_test_u16_strlen);
67 static int unicode_test_u16_strdup(struct unit_test_state *uts)
69 u16 *copy = u16_strdup(c4);
71 ut_assert(copy != c4);
72 ut_asserteq_mem(copy, c4, sizeof(c4));
77 UNICODE_TEST(unicode_test_u16_strdup);
79 static int unicode_test_u16_strcpy(struct unit_test_state *uts)
84 r = u16_strcpy(copy, c1);
86 ut_asserteq_mem(copy, c1, sizeof(c1));
90 UNICODE_TEST(unicode_test_u16_strcpy);
92 /* U-Boot uses UTF-16 strings in the EFI context only. */
93 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
94 static int unicode_test_string16(struct unit_test_state *uts)
98 /* Test length and precision */
99 memset(buf, 0xff, sizeof(buf));
100 sprintf(buf, "%8.6ls", c2);
101 ut_asserteq(' ', buf[1]);
102 ut_assert(!strncmp(&buf[2], d2, 7));
105 memset(buf, 0xff, sizeof(buf));
106 sprintf(buf, "%8.6ls", c4);
107 ut_asserteq(' ', buf[4]);
108 ut_assert(!strncmp(&buf[5], d4, 12));
111 memset(buf, 0xff, sizeof(buf));
112 sprintf(buf, "%-8.2ls", c4);
113 ut_asserteq(' ', buf[8]);
114 ut_assert(!strncmp(buf, d4, 8));
117 /* Test handling of illegal utf-16 sequences */
118 memset(buf, 0xff, sizeof(buf));
119 sprintf(buf, "%ls", i1);
120 ut_asserteq_str("i1?l", buf);
122 memset(buf, 0xff, sizeof(buf));
123 sprintf(buf, "%ls", i2);
124 ut_asserteq_str("i2?l", buf);
126 memset(buf, 0xff, sizeof(buf));
127 sprintf(buf, "%ls", i3);
128 ut_asserteq_str("i3?", buf);
132 UNICODE_TEST(unicode_test_string16);
135 static int unicode_test_utf8_get(struct unit_test_state *uts)
141 /* Check characters less than 0x800 */
143 for (i = 0; i < 8; ++i) {
144 code = utf8_get((const char **)&s);
145 /* c2 is the utf-8 encoding of d2 */
146 ut_asserteq(c2[i], code);
150 ut_asserteq_ptr(s, d2 + 9)
152 /* Check characters less than 0x10000 */
154 for (i = 0; i < 4; ++i) {
155 code = utf8_get((const char **)&s);
156 /* c3 is the utf-8 encoding of d3 */
157 ut_asserteq(c3[i], code);
161 ut_asserteq_ptr(s, d3 + 9)
163 /* Check character greater 0xffff */
165 code = utf8_get((const char **)&s);
166 ut_asserteq(0x0001048d, code);
167 ut_asserteq_ptr(s, d4 + 4);
169 /* Check illegal character */
171 code = utf8_get((const char **)&s);
172 ut_asserteq(-1, code);
173 ut_asserteq_ptr(j4 + 1, s);
177 UNICODE_TEST(unicode_test_utf8_get);
179 static int unicode_test_utf8_put(struct unit_test_state *uts)
181 char buffer[8] = { 0, };
184 /* Commercial at, translates to one character */
186 ut_assert(!utf8_put('@', &pos))
187 ut_asserteq(1, pos - buffer);
188 ut_asserteq('@', buffer[0]);
189 ut_assert(!buffer[1]);
191 /* Latin letter G with acute, translates to two charactes */
193 ut_assert(!utf8_put(0x1f4, &pos));
194 ut_asserteq(2, pos - buffer);
195 ut_asserteq_str("\xc7\xb4", buffer);
197 /* Tagalog letter i, translates to three characters */
199 ut_assert(!utf8_put(0x1701, &pos));
200 ut_asserteq(3, pos - buffer);
201 ut_asserteq_str("\xe1\x9c\x81", buffer);
203 /* Hamster face, translates to four characters */
205 ut_assert(!utf8_put(0x1f439, &pos));
206 ut_asserteq(4, pos - buffer);
207 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
211 ut_asserteq(-1, utf8_put(0xd888, &pos));
215 UNICODE_TEST(unicode_test_utf8_put);
217 static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
219 ut_asserteq(6, utf8_utf16_strlen(d1));
220 ut_asserteq(8, utf8_utf16_strlen(d2));
221 ut_asserteq(3, utf8_utf16_strlen(d3));
222 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));
231 UNICODE_TEST(unicode_test_utf8_utf16_strlen);
233 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
235 ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
236 ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
237 ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
238 ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
239 ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
240 ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
242 /* illegal utf-8 sequences */
243 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
244 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
245 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
249 UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
252 * ut_u16_strcmp() - Compare to u16 strings.
256 * @count: number of u16 to compare
257 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
259 static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
261 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
270 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
276 utf8_utf16_strcpy(&pos, d1);
277 ut_asserteq(6, pos - buf);
278 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
281 utf8_utf16_strcpy(&pos, d2);
282 ut_asserteq(8, pos - buf);
283 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
286 utf8_utf16_strcpy(&pos, d3);
287 ut_asserteq(3, pos - buf);
288 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
291 utf8_utf16_strcpy(&pos, d4);
292 ut_asserteq(6, pos - buf);
293 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
295 /* Illegal utf-8 strings */
297 utf8_utf16_strcpy(&pos, j1);
298 ut_asserteq(4, pos - buf);
299 ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
302 utf8_utf16_strcpy(&pos, j2);
303 ut_asserteq(4, pos - buf);
304 ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
307 utf8_utf16_strcpy(&pos, j3);
308 ut_asserteq(3, pos - buf);
309 ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
313 UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
315 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
321 memset(buf, 0, sizeof(buf));
322 utf8_utf16_strncpy(&pos, d1, 4);
323 ut_asserteq(4, pos - buf);
325 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
328 memset(buf, 0, sizeof(buf));
329 utf8_utf16_strncpy(&pos, d2, 10);
330 ut_asserteq(8, pos - buf);
332 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
335 memset(buf, 0, sizeof(buf));
336 utf8_utf16_strncpy(&pos, d3, 2);
337 ut_asserteq(2, pos - buf);
339 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
342 memset(buf, 0, sizeof(buf));
343 utf8_utf16_strncpy(&pos, d4, 2);
344 ut_asserteq(4, pos - buf);
346 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
349 memset(buf, 0, sizeof(buf));
350 utf8_utf16_strncpy(&pos, d4, 10);
351 ut_asserteq(6, pos - buf);
353 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
357 UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
359 static int unicode_test_utf16_get(struct unit_test_state *uts)
365 /* Check characters less than 0x10000 */
367 for (i = 0; i < 9; ++i) {
368 code = utf16_get((const u16 **)&s);
369 ut_asserteq(c2[i], code);
373 ut_asserteq_ptr(c2 + 8, s);
375 /* Check character greater 0xffff */
377 code = utf16_get((const u16 **)&s);
378 ut_asserteq(0x0001048d, code);
379 ut_asserteq_ptr(c4 + 2, s);
383 UNICODE_TEST(unicode_test_utf16_get);
385 static int unicode_test_utf16_put(struct unit_test_state *uts)
387 u16 buffer[4] = { 0, };
390 /* Commercial at, translates to one word */
392 ut_assert(!utf16_put('@', &pos));
393 ut_asserteq(1, pos - buffer);
394 ut_asserteq((u16)'@', buffer[0]);
395 ut_assert(!buffer[1]);
397 /* Hamster face, translates to two words */
399 ut_assert(!utf16_put(0x1f439, &pos));
400 ut_asserteq(2, pos - buffer);
401 ut_asserteq((u16)0xd83d, buffer[0]);
402 ut_asserteq((u16)0xdc39, buffer[1]);
403 ut_assert(!buffer[2]);
407 ut_asserteq(-1, utf16_put(0xd888, &pos));
411 UNICODE_TEST(unicode_test_utf16_put);
413 static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
415 ut_asserteq(3, utf16_strnlen(c1, 3));
416 ut_asserteq(6, utf16_strnlen(c1, 13));
417 ut_asserteq(6, utf16_strnlen(c2, 6));
418 ut_asserteq(2, utf16_strnlen(c3, 2));
419 ut_asserteq(2, utf16_strnlen(c4, 2));
420 ut_asserteq(3, utf16_strnlen(c4, 3));
422 /* illegal utf-16 word sequences */
423 ut_asserteq(4, utf16_strnlen(i1, 16));
424 ut_asserteq(4, utf16_strnlen(i2, 16));
425 ut_asserteq(3, utf16_strnlen(i3, 16));
429 UNICODE_TEST(unicode_test_utf16_strnlen);
431 static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
433 ut_asserteq(6, utf16_utf8_strlen(c1));
434 ut_asserteq(9, utf16_utf8_strlen(c2));
435 ut_asserteq(9, utf16_utf8_strlen(c3));
436 ut_asserteq(12, utf16_utf8_strlen(c4));
438 /* illegal utf-16 word sequences */
439 ut_asserteq(4, utf16_utf8_strlen(i1));
440 ut_asserteq(4, utf16_utf8_strlen(i2));
441 ut_asserteq(3, utf16_utf8_strlen(i3));
445 UNICODE_TEST(unicode_test_utf16_utf8_strlen);
447 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
449 ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
450 ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
451 ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
452 ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
453 ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
454 ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
457 UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
459 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
465 utf16_utf8_strcpy(&pos, c1);
466 ut_asserteq(6, pos - buf);
467 ut_asserteq_str(d1, buf);
470 utf16_utf8_strcpy(&pos, c2);
471 ut_asserteq(9, pos - buf);
472 ut_asserteq_str(d2, buf);
475 utf16_utf8_strcpy(&pos, c3);
476 ut_asserteq(9, pos - buf);
477 ut_asserteq_str(d3, buf);
480 utf16_utf8_strcpy(&pos, c4);
481 ut_asserteq(12, pos - buf);
482 ut_asserteq_str(d4, buf);
484 /* Illegal utf-16 strings */
486 utf16_utf8_strcpy(&pos, i1);
487 ut_asserteq(4, pos - buf);
488 ut_asserteq_str("i1?l", buf);
491 utf16_utf8_strcpy(&pos, i2);
492 ut_asserteq(4, pos - buf);
493 ut_asserteq_str("i2?l", buf);
496 utf16_utf8_strcpy(&pos, i3);
497 ut_asserteq(3, pos - buf);
498 ut_asserteq_str("i3?", buf);
502 UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
504 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
510 memset(buf, 0, sizeof(buf));
511 utf16_utf8_strncpy(&pos, c1, 4);
512 ut_asserteq(4, pos - buf);
514 ut_assert(!strncmp(buf, d1, 4));
517 memset(buf, 0, sizeof(buf));
518 utf16_utf8_strncpy(&pos, c2, 10);
519 ut_asserteq(9, pos - buf);
521 ut_assert(!strncmp(buf, d2, SIZE_MAX));
524 memset(buf, 0, sizeof(buf));
525 utf16_utf8_strncpy(&pos, c3, 2);
526 ut_asserteq(6, pos - buf);
528 ut_assert(!strncmp(buf, d3, 6));
531 memset(buf, 0, sizeof(buf));
532 utf16_utf8_strncpy(&pos, c4, 2);
533 ut_asserteq(8, pos - buf);
535 ut_assert(!strncmp(buf, d4, 8));
538 memset(buf, 0, sizeof(buf));
539 utf16_utf8_strncpy(&pos, c4, 10);
540 ut_asserteq(12, pos - buf);
542 ut_assert(!strncmp(buf, d4, SIZE_MAX));
546 UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
548 static int unicode_test_utf_to_lower(struct unit_test_state *uts)
550 ut_asserteq('@', utf_to_lower('@'));
551 ut_asserteq('a', utf_to_lower('A'));
552 ut_asserteq('z', utf_to_lower('Z'));
553 ut_asserteq('[', utf_to_lower('['));
554 ut_asserteq('m', utf_to_lower('m'));
555 /* Latin letter O with diaresis (umlaut) */
556 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
557 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
558 /* Cyrillic letter I*/
559 ut_asserteq(0x0438, utf_to_lower(0x0418));
563 UNICODE_TEST(unicode_test_utf_to_lower);
565 static int unicode_test_utf_to_upper(struct unit_test_state *uts)
567 ut_asserteq('`', utf_to_upper('`'));
568 ut_asserteq('A', utf_to_upper('a'));
569 ut_asserteq('Z', utf_to_upper('z'));
570 ut_asserteq('{', utf_to_upper('{'));
571 ut_asserteq('M', utf_to_upper('M'));
572 /* Latin letter O with diaresis (umlaut) */
573 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
574 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
575 /* Cyrillic letter I */
576 ut_asserteq(0x0418, utf_to_upper(0x0438));
580 UNICODE_TEST(unicode_test_utf_to_upper);
582 static int unicode_test_u16_strncmp(struct unit_test_state *uts)
584 ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
585 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
586 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
587 ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
588 ut_assert(u16_strcmp(L"abc", L"abc") == 0);
589 ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
590 ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
593 UNICODE_TEST(unicode_test_u16_strncmp);
595 static int unicode_test_u16_strsize(struct unit_test_state *uts)
597 ut_asserteq_64(u16_strsize(c1), 14);
598 ut_asserteq_64(u16_strsize(c2), 18);
599 ut_asserteq_64(u16_strsize(c3), 8);
600 ut_asserteq_64(u16_strsize(c4), 14);
603 UNICODE_TEST(unicode_test_u16_strsize);
605 static int unicode_test_utf_to_cp(struct unit_test_state *uts)
611 ret = utf_to_cp(&c, codepage_437);
613 ut_asserteq('\n', c);
616 ret = utf_to_cp(&c, codepage_437);
620 c = 0x03c4; /* Greek small letter tau */
621 ret = utf_to_cp(&c, codepage_437);
623 ut_asserteq(0xe7, c);
625 c = 0x03a4; /* Greek capital letter tau */
626 ret = utf_to_cp(&c, codepage_437);
627 ut_asserteq(-ENOENT, ret);
632 UNICODE_TEST(unicode_test_utf_to_cp);
634 #ifdef CONFIG_EFI_LOADER
635 static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
638 u16 const expected[] = L"Capsule0AF9";
641 memset(buf, 0xeb, sizeof(buf));
642 pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
644 ut_asserteq_mem(expected, buf, sizeof(expected));
645 ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
649 UNICODE_TEST(unicode_test_efi_create_indexed_name);
652 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
654 struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
655 const int n_ents = ll_entry_count(struct unit_test, unicode_test);
657 return cmd_ut_category("Unicode", "unicode_test_",
658 tests, n_ents, argc, argv);