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};
50 /* Letter not in code page 437 */
51 static const char d5[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F,
52 0x74, 0x20, 0x42, 0x00};
54 /* Illegal utf-8 strings */
55 static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
56 static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
57 static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
58 static const char j4[] = {0xa1, 0x00};
60 static int unicode_test_u16_strlen(struct unit_test_state *uts)
62 ut_asserteq(6, u16_strlen(c1));
63 ut_asserteq(8, u16_strlen(c2));
64 ut_asserteq(3, u16_strlen(c3));
65 ut_asserteq(6, u16_strlen(c4));
68 UNICODE_TEST(unicode_test_u16_strlen);
70 static int unicode_test_u16_strdup(struct unit_test_state *uts)
72 u16 *copy = u16_strdup(c4);
74 ut_assert(copy != c4);
75 ut_asserteq_mem(copy, c4, sizeof(c4));
80 UNICODE_TEST(unicode_test_u16_strdup);
82 static int unicode_test_u16_strcpy(struct unit_test_state *uts)
87 r = u16_strcpy(copy, c1);
89 ut_asserteq_mem(copy, c1, sizeof(c1));
93 UNICODE_TEST(unicode_test_u16_strcpy);
95 /* U-Boot uses UTF-16 strings in the EFI context only. */
96 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
97 static int unicode_test_string16(struct unit_test_state *uts)
102 /* Test length and precision */
103 memset(buf, 0xff, sizeof(buf));
104 sprintf(buf, "%8.6ls", c2);
105 ut_asserteq(' ', buf[1]);
106 ut_assert(!strncmp(&buf[2], d2, 7));
109 memset(buf, 0xff, sizeof(buf));
110 sprintf(buf, "%8.6ls", c4);
111 ut_asserteq(' ', buf[4]);
112 ut_assert(!strncmp(&buf[5], d4, 12));
115 memset(buf, 0xff, sizeof(buf));
116 sprintf(buf, "%-8.2ls", c4);
117 ut_asserteq(' ', buf[8]);
118 ut_assert(!strncmp(buf, d4, 8));
121 /* Test handling of illegal utf-16 sequences */
122 memset(buf, 0xff, sizeof(buf));
123 sprintf(buf, "%ls", i1);
124 ut_asserteq_str("i1?l", buf);
126 memset(buf, 0xff, sizeof(buf));
127 sprintf(buf, "%ls", i2);
128 ut_asserteq_str("i2?l", buf);
130 memset(buf, 0xff, sizeof(buf));
131 sprintf(buf, "%ls", i3);
132 ut_asserteq_str("i3?", buf);
134 memset(buf, 0xff, sizeof(buf));
135 ret = snprintf(buf, 4, "%ls", c1);
137 ut_asserteq_str("U-B", buf);
139 memset(buf, 0xff, sizeof(buf));
140 ret = snprintf(buf, 6, "%ls", c2);
141 ut_asserteq_str("kafb", buf);
144 memset(buf, 0xff, sizeof(buf));
145 ret = snprintf(buf, 7, "%ls", c2);
146 ut_asserteq_str("kafb\xC3\xA1", buf);
149 memset(buf, 0xff, sizeof(buf));
150 ret = snprintf(buf, 8, "%ls", c3);
151 ut_asserteq_str("\xE6\xBD\x9C\xE6\xB0\xB4", buf);
154 memset(buf, 0xff, sizeof(buf));
155 ret = snprintf(buf, 11, "%ls", c4);
156 ut_asserteq_str("\xF0\x90\x92\x8D\xF0\x90\x92\x96", buf);
157 ut_asserteq(12, ret);
159 memset(buf, 0xff, sizeof(buf));
160 ret = snprintf(buf, 4, "%ls", c4);
161 ut_asserteq_str("", buf);
162 ut_asserteq(12, ret);
166 UNICODE_TEST(unicode_test_string16);
169 static int unicode_test_utf8_get(struct unit_test_state *uts)
175 /* Check characters less than 0x800 */
177 for (i = 0; i < 8; ++i) {
178 code = utf8_get((const char **)&s);
179 /* c2 is the utf-8 encoding of d2 */
180 ut_asserteq(c2[i], code);
184 ut_asserteq_ptr(s, d2 + 9)
186 /* Check characters less than 0x10000 */
188 for (i = 0; i < 4; ++i) {
189 code = utf8_get((const char **)&s);
190 /* c3 is the utf-8 encoding of d3 */
191 ut_asserteq(c3[i], code);
195 ut_asserteq_ptr(s, d3 + 9)
197 /* Check character greater 0xffff */
199 code = utf8_get((const char **)&s);
200 ut_asserteq(0x0001048d, code);
201 ut_asserteq_ptr(s, d4 + 4);
203 /* Check illegal character */
205 code = utf8_get((const char **)&s);
206 ut_asserteq(-1, code);
207 ut_asserteq_ptr(j4 + 1, s);
211 UNICODE_TEST(unicode_test_utf8_get);
213 static int unicode_test_utf8_put(struct unit_test_state *uts)
215 char buffer[8] = { 0, };
218 /* Commercial at, translates to one character */
220 ut_assert(!utf8_put('@', &pos))
221 ut_asserteq(1, pos - buffer);
222 ut_asserteq('@', buffer[0]);
223 ut_assert(!buffer[1]);
225 /* Latin letter G with acute, translates to two charactes */
227 ut_assert(!utf8_put(0x1f4, &pos));
228 ut_asserteq(2, pos - buffer);
229 ut_asserteq_str("\xc7\xb4", buffer);
231 /* Tagalog letter i, translates to three characters */
233 ut_assert(!utf8_put(0x1701, &pos));
234 ut_asserteq(3, pos - buffer);
235 ut_asserteq_str("\xe1\x9c\x81", buffer);
237 /* Hamster face, translates to four characters */
239 ut_assert(!utf8_put(0x1f439, &pos));
240 ut_asserteq(4, pos - buffer);
241 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
245 ut_asserteq(-1, utf8_put(0xd888, &pos));
249 UNICODE_TEST(unicode_test_utf8_put);
251 static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
253 ut_asserteq(6, utf8_utf16_strlen(d1));
254 ut_asserteq(8, utf8_utf16_strlen(d2));
255 ut_asserteq(3, utf8_utf16_strlen(d3));
256 ut_asserteq(6, utf8_utf16_strlen(d4));
258 /* illegal utf-8 sequences */
259 ut_asserteq(4, utf8_utf16_strlen(j1));
260 ut_asserteq(4, utf8_utf16_strlen(j2));
261 ut_asserteq(3, utf8_utf16_strlen(j3));
265 UNICODE_TEST(unicode_test_utf8_utf16_strlen);
267 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
269 ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
270 ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
271 ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
272 ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
273 ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
274 ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
276 /* illegal utf-8 sequences */
277 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
278 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
279 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
283 UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
286 * ut_u16_strcmp() - Compare to u16 strings.
290 * @count: number of u16 to compare
291 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
293 static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
295 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
304 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
310 utf8_utf16_strcpy(&pos, d1);
311 ut_asserteq(6, pos - buf);
312 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
315 utf8_utf16_strcpy(&pos, d2);
316 ut_asserteq(8, pos - buf);
317 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
320 utf8_utf16_strcpy(&pos, d3);
321 ut_asserteq(3, pos - buf);
322 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
325 utf8_utf16_strcpy(&pos, d4);
326 ut_asserteq(6, pos - buf);
327 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
329 /* Illegal utf-8 strings */
331 utf8_utf16_strcpy(&pos, j1);
332 ut_asserteq(4, pos - buf);
333 ut_assert(!unicode_test_u16_strcmp(buf, u"j1?l", SIZE_MAX));
336 utf8_utf16_strcpy(&pos, j2);
337 ut_asserteq(4, pos - buf);
338 ut_assert(!unicode_test_u16_strcmp(buf, u"j2?l", SIZE_MAX));
341 utf8_utf16_strcpy(&pos, j3);
342 ut_asserteq(3, pos - buf);
343 ut_assert(!unicode_test_u16_strcmp(buf, u"j3?", SIZE_MAX));
347 UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
349 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
355 memset(buf, 0, sizeof(buf));
356 utf8_utf16_strncpy(&pos, d1, 4);
357 ut_asserteq(4, pos - buf);
359 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
362 memset(buf, 0, sizeof(buf));
363 utf8_utf16_strncpy(&pos, d2, 10);
364 ut_asserteq(8, pos - buf);
366 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
369 memset(buf, 0, sizeof(buf));
370 utf8_utf16_strncpy(&pos, d3, 2);
371 ut_asserteq(2, pos - buf);
373 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
376 memset(buf, 0, sizeof(buf));
377 utf8_utf16_strncpy(&pos, d4, 2);
378 ut_asserteq(4, pos - buf);
380 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
383 memset(buf, 0, sizeof(buf));
384 utf8_utf16_strncpy(&pos, d4, 10);
385 ut_asserteq(6, pos - buf);
387 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
391 UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
393 static int unicode_test_utf16_get(struct unit_test_state *uts)
399 /* Check characters less than 0x10000 */
401 for (i = 0; i < 9; ++i) {
402 code = utf16_get((const u16 **)&s);
403 ut_asserteq(c2[i], code);
407 ut_asserteq_ptr(c2 + 8, s);
409 /* Check character greater 0xffff */
411 code = utf16_get((const u16 **)&s);
412 ut_asserteq(0x0001048d, code);
413 ut_asserteq_ptr(c4 + 2, s);
417 UNICODE_TEST(unicode_test_utf16_get);
419 static int unicode_test_utf16_put(struct unit_test_state *uts)
421 u16 buffer[4] = { 0, };
424 /* Commercial at, translates to one word */
426 ut_assert(!utf16_put('@', &pos));
427 ut_asserteq(1, pos - buffer);
428 ut_asserteq((u16)'@', buffer[0]);
429 ut_assert(!buffer[1]);
431 /* Hamster face, translates to two words */
433 ut_assert(!utf16_put(0x1f439, &pos));
434 ut_asserteq(2, pos - buffer);
435 ut_asserteq((u16)0xd83d, buffer[0]);
436 ut_asserteq((u16)0xdc39, buffer[1]);
437 ut_assert(!buffer[2]);
441 ut_asserteq(-1, utf16_put(0xd888, &pos));
445 UNICODE_TEST(unicode_test_utf16_put);
447 static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
449 ut_asserteq(3, utf16_strnlen(c1, 3));
450 ut_asserteq(6, utf16_strnlen(c1, 13));
451 ut_asserteq(6, utf16_strnlen(c2, 6));
452 ut_asserteq(2, utf16_strnlen(c3, 2));
453 ut_asserteq(2, utf16_strnlen(c4, 2));
454 ut_asserteq(3, utf16_strnlen(c4, 3));
456 /* illegal utf-16 word sequences */
457 ut_asserteq(4, utf16_strnlen(i1, 16));
458 ut_asserteq(4, utf16_strnlen(i2, 16));
459 ut_asserteq(3, utf16_strnlen(i3, 16));
463 UNICODE_TEST(unicode_test_utf16_strnlen);
465 static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
467 ut_asserteq(6, utf16_utf8_strlen(c1));
468 ut_asserteq(9, utf16_utf8_strlen(c2));
469 ut_asserteq(9, utf16_utf8_strlen(c3));
470 ut_asserteq(12, utf16_utf8_strlen(c4));
472 /* illegal utf-16 word sequences */
473 ut_asserteq(4, utf16_utf8_strlen(i1));
474 ut_asserteq(4, utf16_utf8_strlen(i2));
475 ut_asserteq(3, utf16_utf8_strlen(i3));
479 UNICODE_TEST(unicode_test_utf16_utf8_strlen);
481 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
483 ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
484 ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
485 ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
486 ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
487 ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
488 ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
491 UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
493 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
499 utf16_utf8_strcpy(&pos, c1);
500 ut_asserteq(6, pos - buf);
501 ut_asserteq_str(d1, buf);
504 utf16_utf8_strcpy(&pos, c2);
505 ut_asserteq(9, pos - buf);
506 ut_asserteq_str(d2, buf);
509 utf16_utf8_strcpy(&pos, c3);
510 ut_asserteq(9, pos - buf);
511 ut_asserteq_str(d3, buf);
514 utf16_utf8_strcpy(&pos, c4);
515 ut_asserteq(12, pos - buf);
516 ut_asserteq_str(d4, buf);
518 /* Illegal utf-16 strings */
520 utf16_utf8_strcpy(&pos, i1);
521 ut_asserteq(4, pos - buf);
522 ut_asserteq_str("i1?l", buf);
525 utf16_utf8_strcpy(&pos, i2);
526 ut_asserteq(4, pos - buf);
527 ut_asserteq_str("i2?l", buf);
530 utf16_utf8_strcpy(&pos, i3);
531 ut_asserteq(3, pos - buf);
532 ut_asserteq_str("i3?", buf);
536 UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
538 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
544 memset(buf, 0, sizeof(buf));
545 utf16_utf8_strncpy(&pos, c1, 4);
546 ut_asserteq(4, pos - buf);
548 ut_assert(!strncmp(buf, d1, 4));
551 memset(buf, 0, sizeof(buf));
552 utf16_utf8_strncpy(&pos, c2, 10);
553 ut_asserteq(9, pos - buf);
555 ut_assert(!strncmp(buf, d2, SIZE_MAX));
558 memset(buf, 0, sizeof(buf));
559 utf16_utf8_strncpy(&pos, c3, 2);
560 ut_asserteq(6, pos - buf);
562 ut_assert(!strncmp(buf, d3, 6));
565 memset(buf, 0, sizeof(buf));
566 utf16_utf8_strncpy(&pos, c4, 2);
567 ut_asserteq(8, pos - buf);
569 ut_assert(!strncmp(buf, d4, 8));
572 memset(buf, 0, sizeof(buf));
573 utf16_utf8_strncpy(&pos, c4, 10);
574 ut_asserteq(12, pos - buf);
576 ut_assert(!strncmp(buf, d4, SIZE_MAX));
580 UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
582 static int unicode_test_utf_to_lower(struct unit_test_state *uts)
584 ut_asserteq('@', utf_to_lower('@'));
585 ut_asserteq('a', utf_to_lower('A'));
586 ut_asserteq('z', utf_to_lower('Z'));
587 ut_asserteq('[', utf_to_lower('['));
588 ut_asserteq('m', utf_to_lower('m'));
589 /* Latin letter O with diaresis (umlaut) */
590 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
591 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
592 /* Cyrillic letter I*/
593 ut_asserteq(0x0438, utf_to_lower(0x0418));
597 UNICODE_TEST(unicode_test_utf_to_lower);
599 static int unicode_test_utf_to_upper(struct unit_test_state *uts)
601 ut_asserteq('`', utf_to_upper('`'));
602 ut_asserteq('A', utf_to_upper('a'));
603 ut_asserteq('Z', utf_to_upper('z'));
604 ut_asserteq('{', utf_to_upper('{'));
605 ut_asserteq('M', utf_to_upper('M'));
606 /* Latin letter O with diaresis (umlaut) */
607 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
608 #ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
609 /* Cyrillic letter I */
610 ut_asserteq(0x0418, utf_to_upper(0x0438));
614 UNICODE_TEST(unicode_test_utf_to_upper);
616 static int unicode_test_u16_strncmp(struct unit_test_state *uts)
618 ut_assert(u16_strncmp(u"abc", u"abc", 3) == 0);
619 ut_assert(u16_strncmp(u"abcdef", u"abcghi", 3) == 0);
620 ut_assert(u16_strncmp(u"abcdef", u"abcghi", 6) < 0);
621 ut_assert(u16_strncmp(u"abcghi", u"abcdef", 6) > 0);
622 ut_assert(u16_strcmp(u"abc", u"abc") == 0);
623 ut_assert(u16_strcmp(u"abcdef", u"deghi") < 0);
624 ut_assert(u16_strcmp(u"deghi", u"abcdef") > 0);
627 UNICODE_TEST(unicode_test_u16_strncmp);
629 static int unicode_test_u16_strsize(struct unit_test_state *uts)
631 ut_asserteq_64(u16_strsize(c1), 14);
632 ut_asserteq_64(u16_strsize(c2), 18);
633 ut_asserteq_64(u16_strsize(c3), 8);
634 ut_asserteq_64(u16_strsize(c4), 14);
637 UNICODE_TEST(unicode_test_u16_strsize);
639 static int unicode_test_utf_to_cp(struct unit_test_state *uts)
645 ret = utf_to_cp(&c, codepage_437);
647 ut_asserteq('\n', c);
650 ret = utf_to_cp(&c, codepage_437);
654 c = 0x03c4; /* Greek small letter tau */
655 ret = utf_to_cp(&c, codepage_437);
657 ut_asserteq(0xe7, c);
659 c = 0x03a4; /* Greek capital letter tau */
660 ret = utf_to_cp(&c, codepage_437);
661 ut_asserteq(-ENOENT, ret);
666 UNICODE_TEST(unicode_test_utf_to_cp);
668 static void utf8_to_cp437_stream_helper(const char *in, char *out)
675 ret = utf8_to_cp437_stream(*in, buffer);
682 static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts)
686 utf8_to_cp437_stream_helper(d1, buf);
687 ut_asserteq_str("U-Boot", buf);
688 utf8_to_cp437_stream_helper(d2, buf);
689 ut_asserteq_str("kafb\xa0tur", buf);
690 utf8_to_cp437_stream_helper(d5, buf);
691 ut_asserteq_str("? is not B", buf);
692 utf8_to_cp437_stream_helper(j2, buf);
693 ut_asserteq_str("j2l", buf);
697 UNICODE_TEST(unicode_test_utf8_to_cp437_stream);
699 static void utf8_to_utf32_stream_helper(const char *in, s32 *out)
706 ret = utf8_to_utf32_stream(*in, buffer);
713 static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts)
717 const u32 u1[] = {0x55, 0x2D, 0x42, 0x6F, 0x6F, 0x74, 0x0000};
718 const u32 u2[] = {0x6B, 0x61, 0x66, 0x62, 0xE1, 0x74, 0x75, 0x72, 0x00};
719 const u32 u3[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74,
721 const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00};
723 memset(buf, 0, sizeof(buf));
724 utf8_to_utf32_stream_helper(d1, buf);
725 ut_asserteq_mem(u1, buf, sizeof(u1));
727 memset(buf, 0, sizeof(buf));
728 utf8_to_utf32_stream_helper(d2, buf);
729 ut_asserteq_mem(u2, buf, sizeof(u2));
731 memset(buf, 0, sizeof(buf));
732 utf8_to_utf32_stream_helper(d5, buf);
733 ut_asserteq_mem(u3, buf, sizeof(u3));
735 memset(buf, 0, sizeof(buf));
736 utf8_to_utf32_stream_helper(j2, buf);
737 ut_asserteq_mem(u4, buf, sizeof(u4));
741 UNICODE_TEST(unicode_test_utf8_to_utf32_stream);
743 #ifdef CONFIG_EFI_LOADER
744 static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
747 u16 const expected[] = u"Capsule0AF9";
750 memset(buf, 0xeb, sizeof(buf));
751 pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
753 ut_asserteq_mem(expected, buf, sizeof(expected));
754 ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
758 UNICODE_TEST(unicode_test_efi_create_indexed_name);
761 static int unicode_test_u16_strlcat(struct unit_test_state *uts)
764 u16 dest[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, 0};
765 u16 src[] = {0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
766 u16 concat_str[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f,
767 0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
768 u16 null_src = u'\0';
769 size_t ret, expected;
772 /* dest and src are empty string */
773 memset(buf, 0, sizeof(buf));
774 ret = u16_strlcat(buf, &null_src, sizeof(buf));
777 /* dest is empty string */
778 memset(buf, 0, sizeof(buf));
779 ret = u16_strlcat(buf, src, sizeof(buf));
781 ut_assert(!unicode_test_u16_strcmp(buf, src, 40));
783 /* src is empty string */
784 memset(buf, 0xCD, (sizeof(buf) - sizeof(u16)));
786 memcpy(buf, dest, sizeof(dest));
787 ret = u16_strlcat(buf, &null_src, sizeof(buf));
789 ut_assert(!unicode_test_u16_strcmp(buf, dest, 40));
791 for (i = 0; i <= 40; i++) {
792 memset(buf, 0xCD, (sizeof(buf) - sizeof(u16)));
794 memcpy(buf, dest, sizeof(dest));
796 ret = u16_strlcat(buf, src, i);
797 ut_asserteq(expected, ret);
799 ut_assert(!unicode_test_u16_strcmp(buf, dest, 40));
801 ut_assert(!unicode_test_u16_strcmp(buf, concat_str, i - 1));
803 ut_assert(!unicode_test_u16_strcmp(buf, concat_str, 40));
809 UNICODE_TEST(unicode_test_u16_strlcat);
811 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
813 struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
814 const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test);
816 return cmd_ut_category("Unicode", "unicode_test_",
817 tests, n_ents, argc, argv);