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};
56 static int unicode_test_u16_strlen(struct unit_test_state *uts)
58 ut_asserteq(6, u16_strlen(c1));
59 ut_asserteq(8, u16_strlen(c2));
60 ut_asserteq(3, u16_strlen(c3));
61 ut_asserteq(6, u16_strlen(c4));
64 UNICODE_TEST(unicode_test_u16_strlen);
66 static int unicode_test_u16_strdup(struct unit_test_state *uts)
68 u16 *copy = u16_strdup(c4);
70 ut_assert(copy != c4);
71 ut_asserteq_mem(copy, c4, sizeof(c4));
76 UNICODE_TEST(unicode_test_u16_strdup);
78 static int unicode_test_u16_strcpy(struct unit_test_state *uts)
83 r = u16_strcpy(copy, c1);
85 ut_asserteq_mem(copy, c1, sizeof(c1));
89 UNICODE_TEST(unicode_test_u16_strcpy);
91 /* U-Boot uses UTF-16 strings in the EFI context only. */
92 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
93 static int unicode_test_string16(struct unit_test_state *uts)
97 /* Test length and precision */
98 memset(buf, 0xff, sizeof(buf));
99 sprintf(buf, "%8.6ls", c2);
100 ut_asserteq(' ', buf[1]);
101 ut_assert(!strncmp(&buf[2], d2, 7));
104 memset(buf, 0xff, sizeof(buf));
105 sprintf(buf, "%8.6ls", c4);
106 ut_asserteq(' ', buf[4]);
107 ut_assert(!strncmp(&buf[5], d4, 12));
110 memset(buf, 0xff, sizeof(buf));
111 sprintf(buf, "%-8.2ls", c4);
112 ut_asserteq(' ', buf[8]);
113 ut_assert(!strncmp(buf, d4, 8));
116 /* Test handling of illegal utf-16 sequences */
117 memset(buf, 0xff, sizeof(buf));
118 sprintf(buf, "%ls", i1);
119 ut_asserteq_str("i1?l", buf);
121 memset(buf, 0xff, sizeof(buf));
122 sprintf(buf, "%ls", i2);
123 ut_asserteq_str("i2?l", buf);
125 memset(buf, 0xff, sizeof(buf));
126 sprintf(buf, "%ls", i3);
127 ut_asserteq_str("i3?", buf);
131 UNICODE_TEST(unicode_test_string16);
134 static int unicode_test_utf8_get(struct unit_test_state *uts)
140 /* Check characters less than 0x800 */
142 for (i = 0; i < 8; ++i) {
143 code = utf8_get((const char **)&s);
144 /* c2 is the utf-8 encoding of d2 */
145 ut_asserteq(c2[i], code);
149 ut_asserteq_ptr(s, d2 + 9)
151 /* Check characters less than 0x10000 */
153 for (i = 0; i < 4; ++i) {
154 code = utf8_get((const char **)&s);
155 /* c3 is the utf-8 encoding of d3 */
156 ut_asserteq(c3[i], code);
160 ut_asserteq_ptr(s, d3 + 9)
162 /* Check character greater 0xffff */
164 code = utf8_get((const char **)&s);
165 ut_asserteq(0x0001048d, code);
166 ut_asserteq_ptr(s, d4 + 4);
170 UNICODE_TEST(unicode_test_utf8_get);
172 static int unicode_test_utf8_put(struct unit_test_state *uts)
174 char buffer[8] = { 0, };
177 /* Commercial at, translates to one character */
179 ut_assert(!utf8_put('@', &pos))
180 ut_asserteq(1, pos - buffer);
181 ut_asserteq('@', buffer[0]);
182 ut_assert(!buffer[1]);
184 /* Latin letter G with acute, translates to two charactes */
186 ut_assert(!utf8_put(0x1f4, &pos));
187 ut_asserteq(2, pos - buffer);
188 ut_asserteq_str("\xc7\xb4", buffer);
190 /* Tagalog letter i, translates to three characters */
192 ut_assert(!utf8_put(0x1701, &pos));
193 ut_asserteq(3, pos - buffer);
194 ut_asserteq_str("\xe1\x9c\x81", buffer);
196 /* Hamster face, translates to four characters */
198 ut_assert(!utf8_put(0x1f439, &pos));
199 ut_asserteq(4, pos - buffer);
200 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
204 ut_asserteq(-1, utf8_put(0xd888, &pos));
208 UNICODE_TEST(unicode_test_utf8_put);
210 static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
212 ut_asserteq(6, utf8_utf16_strlen(d1));
213 ut_asserteq(8, utf8_utf16_strlen(d2));
214 ut_asserteq(3, utf8_utf16_strlen(d3));
215 ut_asserteq(6, utf8_utf16_strlen(d4));
217 /* illegal utf-8 sequences */
218 ut_asserteq(4, utf8_utf16_strlen(j1));
219 ut_asserteq(4, utf8_utf16_strlen(j2));
220 ut_asserteq(3, utf8_utf16_strlen(j3));
224 UNICODE_TEST(unicode_test_utf8_utf16_strlen);
226 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
228 ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
229 ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
230 ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
231 ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
232 ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
233 ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
235 /* illegal utf-8 sequences */
236 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
237 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
238 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
242 UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
245 * ut_u16_strcmp() - Compare to u16 strings.
249 * @count: number of u16 to compare
250 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
252 static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
254 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
263 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
269 utf8_utf16_strcpy(&pos, d1);
270 ut_asserteq(6, pos - buf);
271 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
274 utf8_utf16_strcpy(&pos, d2);
275 ut_asserteq(8, pos - buf);
276 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
279 utf8_utf16_strcpy(&pos, d3);
280 ut_asserteq(3, pos - buf);
281 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
284 utf8_utf16_strcpy(&pos, d4);
285 ut_asserteq(6, pos - buf);
286 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
288 /* Illegal utf-8 strings */
290 utf8_utf16_strcpy(&pos, j1);
291 ut_asserteq(4, pos - buf);
292 ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
295 utf8_utf16_strcpy(&pos, j2);
296 ut_asserteq(4, pos - buf);
297 ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
300 utf8_utf16_strcpy(&pos, j3);
301 ut_asserteq(3, pos - buf);
302 ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
306 UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
308 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
314 memset(buf, 0, sizeof(buf));
315 utf8_utf16_strncpy(&pos, d1, 4);
316 ut_asserteq(4, pos - buf);
318 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
321 memset(buf, 0, sizeof(buf));
322 utf8_utf16_strncpy(&pos, d2, 10);
323 ut_asserteq(8, pos - buf);
325 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
328 memset(buf, 0, sizeof(buf));
329 utf8_utf16_strncpy(&pos, d3, 2);
330 ut_asserteq(2, pos - buf);
332 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
335 memset(buf, 0, sizeof(buf));
336 utf8_utf16_strncpy(&pos, d4, 2);
337 ut_asserteq(4, pos - buf);
339 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
342 memset(buf, 0, sizeof(buf));
343 utf8_utf16_strncpy(&pos, d4, 10);
344 ut_asserteq(6, pos - buf);
346 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
350 UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
352 static int unicode_test_utf16_get(struct unit_test_state *uts)
358 /* Check characters less than 0x10000 */
360 for (i = 0; i < 9; ++i) {
361 code = utf16_get((const u16 **)&s);
362 ut_asserteq(c2[i], code);
366 ut_asserteq_ptr(c2 + 8, s);
368 /* Check character greater 0xffff */
370 code = utf16_get((const u16 **)&s);
371 ut_asserteq(0x0001048d, code);
372 ut_asserteq_ptr(c4 + 2, s);
376 UNICODE_TEST(unicode_test_utf16_get);
378 static int unicode_test_utf16_put(struct unit_test_state *uts)
380 u16 buffer[4] = { 0, };
383 /* Commercial at, translates to one word */
385 ut_assert(!utf16_put('@', &pos));
386 ut_asserteq(1, pos - buffer);
387 ut_asserteq((u16)'@', buffer[0]);
388 ut_assert(!buffer[1]);
390 /* Hamster face, translates to two words */
392 ut_assert(!utf16_put(0x1f439, &pos));
393 ut_asserteq(2, pos - buffer);
394 ut_asserteq((u16)0xd83d, buffer[0]);
395 ut_asserteq((u16)0xdc39, buffer[1]);
396 ut_assert(!buffer[2]);
400 ut_asserteq(-1, utf16_put(0xd888, &pos));
404 UNICODE_TEST(unicode_test_utf16_put);
406 static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
408 ut_asserteq(3, utf16_strnlen(c1, 3));
409 ut_asserteq(6, utf16_strnlen(c1, 13));
410 ut_asserteq(6, utf16_strnlen(c2, 6));
411 ut_asserteq(2, utf16_strnlen(c3, 2));
412 ut_asserteq(2, utf16_strnlen(c4, 2));
413 ut_asserteq(3, utf16_strnlen(c4, 3));
415 /* illegal utf-16 word sequences */
416 ut_asserteq(4, utf16_strnlen(i1, 16));
417 ut_asserteq(4, utf16_strnlen(i2, 16));
418 ut_asserteq(3, utf16_strnlen(i3, 16));
422 UNICODE_TEST(unicode_test_utf16_strnlen);
424 static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
426 ut_asserteq(6, utf16_utf8_strlen(c1));
427 ut_asserteq(9, utf16_utf8_strlen(c2));
428 ut_asserteq(9, utf16_utf8_strlen(c3));
429 ut_asserteq(12, utf16_utf8_strlen(c4));
431 /* illegal utf-16 word sequences */
432 ut_asserteq(4, utf16_utf8_strlen(i1));
433 ut_asserteq(4, utf16_utf8_strlen(i2));
434 ut_asserteq(3, utf16_utf8_strlen(i3));
438 UNICODE_TEST(unicode_test_utf16_utf8_strlen);
440 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
442 ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
443 ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
444 ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
445 ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
446 ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
447 ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
450 UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
452 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
458 utf16_utf8_strcpy(&pos, c1);
459 ut_asserteq(6, pos - buf);
460 ut_asserteq_str(d1, buf);
463 utf16_utf8_strcpy(&pos, c2);
464 ut_asserteq(9, pos - buf);
465 ut_asserteq_str(d2, buf);
468 utf16_utf8_strcpy(&pos, c3);
469 ut_asserteq(9, pos - buf);
470 ut_asserteq_str(d3, buf);
473 utf16_utf8_strcpy(&pos, c4);
474 ut_asserteq(12, pos - buf);
475 ut_asserteq_str(d4, buf);
477 /* Illegal utf-16 strings */
479 utf16_utf8_strcpy(&pos, i1);
480 ut_asserteq(4, pos - buf);
481 ut_asserteq_str("i1?l", buf);
484 utf16_utf8_strcpy(&pos, i2);
485 ut_asserteq(4, pos - buf);
486 ut_asserteq_str("i2?l", buf);
489 utf16_utf8_strcpy(&pos, i3);
490 ut_asserteq(3, pos - buf);
491 ut_asserteq_str("i3?", buf);
495 UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
497 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
503 memset(buf, 0, sizeof(buf));
504 utf16_utf8_strncpy(&pos, c1, 4);
505 ut_asserteq(4, pos - buf);
507 ut_assert(!strncmp(buf, d1, 4));
510 memset(buf, 0, sizeof(buf));
511 utf16_utf8_strncpy(&pos, c2, 10);
512 ut_asserteq(9, pos - buf);
514 ut_assert(!strncmp(buf, d2, SIZE_MAX));
517 memset(buf, 0, sizeof(buf));
518 utf16_utf8_strncpy(&pos, c3, 2);
519 ut_asserteq(6, pos - buf);
521 ut_assert(!strncmp(buf, d3, 6));
524 memset(buf, 0, sizeof(buf));
525 utf16_utf8_strncpy(&pos, c4, 2);
526 ut_asserteq(8, pos - buf);
528 ut_assert(!strncmp(buf, d4, 8));
531 memset(buf, 0, sizeof(buf));
532 utf16_utf8_strncpy(&pos, c4, 10);
533 ut_asserteq(12, pos - buf);
535 ut_assert(!strncmp(buf, d4, SIZE_MAX));
539 UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
541 static int unicode_test_utf_to_lower(struct unit_test_state *uts)
543 ut_asserteq('@', utf_to_lower('@'));
544 ut_asserteq('a', utf_to_lower('A'));
545 ut_asserteq('z', utf_to_lower('Z'));
546 ut_asserteq('[', utf_to_lower('['));
547 ut_asserteq('m', utf_to_lower('m'));
548 /* Latin letter O with diaresis (umlaut) */
549 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
550 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
551 /* Cyrillic letter I*/
552 ut_asserteq(0x0438, utf_to_lower(0x0418));
556 UNICODE_TEST(unicode_test_utf_to_lower);
558 static int unicode_test_utf_to_upper(struct unit_test_state *uts)
560 ut_asserteq('`', utf_to_upper('`'));
561 ut_asserteq('A', utf_to_upper('a'));
562 ut_asserteq('Z', utf_to_upper('z'));
563 ut_asserteq('{', utf_to_upper('{'));
564 ut_asserteq('M', utf_to_upper('M'));
565 /* Latin letter O with diaresis (umlaut) */
566 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
567 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
568 /* Cyrillic letter I */
569 ut_asserteq(0x0418, utf_to_upper(0x0438));
573 UNICODE_TEST(unicode_test_utf_to_upper);
575 static int unicode_test_u16_strncmp(struct unit_test_state *uts)
577 ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
578 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
579 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
580 ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
581 ut_assert(u16_strcmp(L"abc", L"abc") == 0);
582 ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
583 ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
586 UNICODE_TEST(unicode_test_u16_strncmp);
588 static int unicode_test_u16_strsize(struct unit_test_state *uts)
590 ut_asserteq_64(u16_strsize(c1), 14);
591 ut_asserteq_64(u16_strsize(c2), 18);
592 ut_asserteq_64(u16_strsize(c3), 8);
593 ut_asserteq_64(u16_strsize(c4), 14);
596 UNICODE_TEST(unicode_test_u16_strsize);
598 #ifdef CONFIG_EFI_LOADER
599 static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
602 u16 const expected[] = L"Capsule0AF9";
605 memset(buf, 0xeb, sizeof(buf));
606 pos = efi_create_indexed_name(buf, "Capsule", 0x0af9);
608 ut_asserteq_mem(expected, buf, sizeof(expected));
609 ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
613 UNICODE_TEST(unicode_test_efi_create_indexed_name);
616 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
618 struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
619 const int n_ents = ll_entry_count(struct unit_test, unicode_test);
621 return cmd_ut_category("Unicode", "unicode_test_",
622 tests, n_ents, argc, argv);