lib/charset: utf8_get() should return error
[platform/kernel/u-boot.git] / test / unicode_ut.c
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
8 #include <common.h>
9 #include <charset.h>
10 #include <command.h>
11 #include <efi_loader.h>
12 #include <errno.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <test/test.h>
16 #include <test/suites.h>
17 #include <test/ut.h>
18
19 /* Linker list entry for a Unicode test */
20 #define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
21
22 /* Constants c1-c4 and d1-d4 encode the same letters */
23
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,
32                          0x0000};
33
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};
38
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,
43                           0x72, 0x00};
44 /* Three characters translating to one utf-16 word each */
45 static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
46                           0xa6, 0x00};
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
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};
56
57 static int unicode_test_u16_strlen(struct unit_test_state *uts)
58 {
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));
63         return 0;
64 }
65 UNICODE_TEST(unicode_test_u16_strlen);
66
67 static int unicode_test_u16_strdup(struct unit_test_state *uts)
68 {
69         u16 *copy = u16_strdup(c4);
70
71         ut_assert(copy != c4);
72         ut_asserteq_mem(copy, c4, sizeof(c4));
73         free(copy);
74
75         return 0;
76 }
77 UNICODE_TEST(unicode_test_u16_strdup);
78
79 static int unicode_test_u16_strcpy(struct unit_test_state *uts)
80 {
81         u16 *r;
82         u16 copy[10];
83
84         r = u16_strcpy(copy, c1);
85         ut_assert(r == copy);
86         ut_asserteq_mem(copy, c1, sizeof(c1));
87
88         return 0;
89 }
90 UNICODE_TEST(unicode_test_u16_strcpy);
91
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)
95 {
96         char buf[20];
97
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));
103         ut_assert(!buf[9]);
104
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));
109         ut_assert(!buf[17]);
110
111         memset(buf, 0xff, sizeof(buf));
112         sprintf(buf, "%-8.2ls", c4);
113         ut_asserteq(' ', buf[8]);
114         ut_assert(!strncmp(buf, d4, 8));
115         ut_assert(!buf[14]);
116
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);
121
122         memset(buf, 0xff, sizeof(buf));
123         sprintf(buf, "%ls", i2);
124         ut_asserteq_str("i2?l", buf);
125
126         memset(buf, 0xff, sizeof(buf));
127         sprintf(buf, "%ls", i3);
128         ut_asserteq_str("i3?", buf);
129
130         return 0;
131 }
132 UNICODE_TEST(unicode_test_string16);
133 #endif
134
135 static int unicode_test_utf8_get(struct unit_test_state *uts)
136 {
137         const char *s;
138         s32 code;
139         int i;
140
141         /* Check characters less than 0x800 */
142         s = d2;
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);
147                 if (!code)
148                         break;
149         }
150         ut_asserteq_ptr(s, d2 + 9)
151
152         /* Check characters less than 0x10000 */
153         s = d3;
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);
158                 if (!code)
159                         break;
160         }
161         ut_asserteq_ptr(s, d3 + 9)
162
163         /* Check character greater 0xffff */
164         s = d4;
165         code = utf8_get((const char **)&s);
166         ut_asserteq(0x0001048d, code);
167         ut_asserteq_ptr(s, d4 + 4);
168
169         /* Check illegal character */
170         s = j4;
171         code = utf8_get((const char **)&s);
172         ut_asserteq(-1, code);
173         ut_asserteq_ptr(j4 + 1, s);
174
175         return 0;
176 }
177 UNICODE_TEST(unicode_test_utf8_get);
178
179 static int unicode_test_utf8_put(struct unit_test_state *uts)
180 {
181         char buffer[8] = { 0, };
182         char *pos;
183
184         /* Commercial at, translates to one character */
185         pos = buffer;
186         ut_assert(!utf8_put('@', &pos))
187         ut_asserteq(1, pos - buffer);
188         ut_asserteq('@', buffer[0]);
189         ut_assert(!buffer[1]);
190
191         /* Latin letter G with acute, translates to two charactes */
192         pos = buffer;
193         ut_assert(!utf8_put(0x1f4, &pos));
194         ut_asserteq(2, pos - buffer);
195         ut_asserteq_str("\xc7\xb4", buffer);
196
197         /* Tagalog letter i, translates to three characters */
198         pos = buffer;
199         ut_assert(!utf8_put(0x1701, &pos));
200         ut_asserteq(3, pos - buffer);
201         ut_asserteq_str("\xe1\x9c\x81", buffer);
202
203         /* Hamster face, translates to four characters */
204         pos = buffer;
205         ut_assert(!utf8_put(0x1f439, &pos));
206         ut_asserteq(4, pos - buffer);
207         ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
208
209         /* Illegal code */
210         pos = buffer;
211         ut_asserteq(-1, utf8_put(0xd888, &pos));
212
213         return 0;
214 }
215 UNICODE_TEST(unicode_test_utf8_put);
216
217 static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
218 {
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));
223
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
229         return 0;
230 }
231 UNICODE_TEST(unicode_test_utf8_utf16_strlen);
232
233 static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
234 {
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));
241
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));
246
247         return 0;
248 }
249 UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
250
251 /**
252  * ut_u16_strcmp() - Compare to u16 strings.
253  *
254  * @a1:         first string
255  * @a2:         second string
256  * @count:      number of u16 to compare
257  * Return:      -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
258  */
259 static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
260 {
261         for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
262                 if (*a1 < *a2)
263                         return -1;
264                 if (*a1 > *a2)
265                         return 1;
266         }
267         return 0;
268 }
269
270 static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
271 {
272         u16 buf[16];
273         u16 *pos;
274
275         pos = buf;
276         utf8_utf16_strcpy(&pos, d1);
277         ut_asserteq(6, pos - buf);
278         ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
279
280         pos = buf;
281         utf8_utf16_strcpy(&pos, d2);
282         ut_asserteq(8, pos - buf);
283         ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
284
285         pos = buf;
286         utf8_utf16_strcpy(&pos, d3);
287         ut_asserteq(3, pos - buf);
288         ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
289
290         pos = buf;
291         utf8_utf16_strcpy(&pos, d4);
292         ut_asserteq(6, pos - buf);
293         ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
294
295         /* Illegal utf-8 strings */
296         pos = buf;
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));
300
301         pos = buf;
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));
305
306         pos = buf;
307         utf8_utf16_strcpy(&pos, j3);
308         ut_asserteq(3, pos - buf);
309         ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
310
311         return 0;
312 }
313 UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
314
315 static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
316 {
317         u16 buf[16];
318         u16 *pos;
319
320         pos = buf;
321         memset(buf, 0, sizeof(buf));
322         utf8_utf16_strncpy(&pos, d1, 4);
323         ut_asserteq(4, pos - buf);
324         ut_assert(!buf[4]);
325         ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
326
327         pos = buf;
328         memset(buf, 0, sizeof(buf));
329         utf8_utf16_strncpy(&pos, d2, 10);
330         ut_asserteq(8, pos - buf);
331         ut_assert(buf[4]);
332         ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
333
334         pos = buf;
335         memset(buf, 0, sizeof(buf));
336         utf8_utf16_strncpy(&pos, d3, 2);
337         ut_asserteq(2, pos - buf);
338         ut_assert(!buf[2]);
339         ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
340
341         pos = buf;
342         memset(buf, 0, sizeof(buf));
343         utf8_utf16_strncpy(&pos, d4, 2);
344         ut_asserteq(4, pos - buf);
345         ut_assert(!buf[4]);
346         ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
347
348         pos = buf;
349         memset(buf, 0, sizeof(buf));
350         utf8_utf16_strncpy(&pos, d4, 10);
351         ut_asserteq(6, pos - buf);
352         ut_assert(buf[5]);
353         ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
354
355         return 0;
356 }
357 UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
358
359 static int unicode_test_utf16_get(struct unit_test_state *uts)
360 {
361         const u16 *s;
362         s32 code;
363         int i;
364
365         /* Check characters less than 0x10000 */
366         s = c2;
367         for (i = 0; i < 9; ++i) {
368                 code = utf16_get((const u16 **)&s);
369                 ut_asserteq(c2[i], code);
370                 if (!code)
371                         break;
372         }
373         ut_asserteq_ptr(c2 + 8, s);
374
375         /* Check character greater 0xffff */
376         s = c4;
377         code = utf16_get((const u16 **)&s);
378         ut_asserteq(0x0001048d, code);
379         ut_asserteq_ptr(c4 + 2, s);
380
381         return 0;
382 }
383 UNICODE_TEST(unicode_test_utf16_get);
384
385 static int unicode_test_utf16_put(struct unit_test_state *uts)
386 {
387         u16 buffer[4] = { 0, };
388         u16 *pos;
389
390         /* Commercial at, translates to one word */
391         pos = buffer;
392         ut_assert(!utf16_put('@', &pos));
393         ut_asserteq(1, pos - buffer);
394         ut_asserteq((u16)'@', buffer[0]);
395         ut_assert(!buffer[1]);
396
397         /* Hamster face, translates to two words */
398         pos = buffer;
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]);
404
405         /* Illegal code */
406         pos = buffer;
407         ut_asserteq(-1, utf16_put(0xd888, &pos));
408
409         return 0;
410 }
411 UNICODE_TEST(unicode_test_utf16_put);
412
413 static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
414 {
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));
421
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));
426
427         return 0;
428 }
429 UNICODE_TEST(unicode_test_utf16_strnlen);
430
431 static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
432 {
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));
437
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));
442
443         return 0;
444 }
445 UNICODE_TEST(unicode_test_utf16_utf8_strlen);
446
447 static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
448 {
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));
455         return 0;
456 }
457 UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
458
459 static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
460 {
461         char buf[16];
462         char *pos;
463
464         pos = buf;
465         utf16_utf8_strcpy(&pos, c1);
466         ut_asserteq(6, pos - buf);
467         ut_asserteq_str(d1, buf);
468
469         pos = buf;
470         utf16_utf8_strcpy(&pos, c2);
471         ut_asserteq(9, pos - buf);
472         ut_asserteq_str(d2, buf);
473
474         pos = buf;
475         utf16_utf8_strcpy(&pos, c3);
476         ut_asserteq(9, pos - buf);
477         ut_asserteq_str(d3, buf);
478
479         pos = buf;
480         utf16_utf8_strcpy(&pos, c4);
481         ut_asserteq(12, pos - buf);
482         ut_asserteq_str(d4, buf);
483
484         /* Illegal utf-16 strings */
485         pos = buf;
486         utf16_utf8_strcpy(&pos, i1);
487         ut_asserteq(4, pos - buf);
488         ut_asserteq_str("i1?l", buf);
489
490         pos = buf;
491         utf16_utf8_strcpy(&pos, i2);
492         ut_asserteq(4, pos - buf);
493         ut_asserteq_str("i2?l", buf);
494
495         pos = buf;
496         utf16_utf8_strcpy(&pos, i3);
497         ut_asserteq(3, pos - buf);
498         ut_asserteq_str("i3?", buf);
499
500         return 0;
501 }
502 UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
503
504 static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
505 {
506         char buf[16];
507         char *pos;
508
509         pos = buf;
510         memset(buf, 0, sizeof(buf));
511         utf16_utf8_strncpy(&pos, c1, 4);
512         ut_asserteq(4, pos - buf);
513         ut_assert(!buf[4]);
514         ut_assert(!strncmp(buf, d1, 4));
515
516         pos = buf;
517         memset(buf, 0, sizeof(buf));
518         utf16_utf8_strncpy(&pos, c2, 10);
519         ut_asserteq(9, pos - buf);
520         ut_assert(buf[4]);
521         ut_assert(!strncmp(buf, d2, SIZE_MAX));
522
523         pos = buf;
524         memset(buf, 0, sizeof(buf));
525         utf16_utf8_strncpy(&pos, c3, 2);
526         ut_asserteq(6, pos - buf);
527         ut_assert(!buf[6]);
528         ut_assert(!strncmp(buf, d3, 6));
529
530         pos = buf;
531         memset(buf, 0, sizeof(buf));
532         utf16_utf8_strncpy(&pos, c4, 2);
533         ut_asserteq(8, pos - buf);
534         ut_assert(!buf[8]);
535         ut_assert(!strncmp(buf, d4, 8));
536
537         pos = buf;
538         memset(buf, 0, sizeof(buf));
539         utf16_utf8_strncpy(&pos, c4, 10);
540         ut_asserteq(12, pos - buf);
541         ut_assert(buf[5]);
542         ut_assert(!strncmp(buf, d4, SIZE_MAX));
543
544         return 0;
545 }
546 UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
547
548 static int unicode_test_utf_to_lower(struct unit_test_state *uts)
549 {
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));
560 #endif
561         return 0;
562 }
563 UNICODE_TEST(unicode_test_utf_to_lower);
564
565 static int unicode_test_utf_to_upper(struct unit_test_state *uts)
566 {
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));
577 #endif
578         return 0;
579 }
580 UNICODE_TEST(unicode_test_utf_to_upper);
581
582 static int unicode_test_u16_strncmp(struct unit_test_state *uts)
583 {
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);
591         return 0;
592 }
593 UNICODE_TEST(unicode_test_u16_strncmp);
594
595 static int unicode_test_u16_strsize(struct unit_test_state *uts)
596 {
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);
601         return 0;
602 }
603 UNICODE_TEST(unicode_test_u16_strsize);
604
605 static int unicode_test_utf_to_cp(struct unit_test_state *uts)
606 {
607         int ret;
608         s32 c;
609
610         c = '\n';
611         ret = utf_to_cp(&c, codepage_437);
612         ut_asserteq(0, ret);
613         ut_asserteq('\n', c);
614
615         c = 'a';
616         ret = utf_to_cp(&c, codepage_437);
617         ut_asserteq(0, ret);
618         ut_asserteq('a', c);
619
620         c = 0x03c4; /* Greek small letter tau */
621         ret = utf_to_cp(&c, codepage_437);
622         ut_asserteq(0, ret);
623         ut_asserteq(0xe7, c);
624
625         c = 0x03a4; /* Greek capital letter tau */
626         ret = utf_to_cp(&c, codepage_437);
627         ut_asserteq(-ENOENT, ret);
628         ut_asserteq('?', c);
629
630         return 0;
631 }
632 UNICODE_TEST(unicode_test_utf_to_cp);
633
634 #ifdef CONFIG_EFI_LOADER
635 static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
636 {
637         u16 buf[16];
638         u16 const expected[] = L"Capsule0AF9";
639         u16 *pos;
640
641         memset(buf, 0xeb, sizeof(buf));
642         pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
643
644         ut_asserteq_mem(expected, buf, sizeof(expected));
645         ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
646
647         return 0;
648 }
649 UNICODE_TEST(unicode_test_efi_create_indexed_name);
650 #endif
651
652 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
653 {
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);
656
657         return cmd_ut_category("Unicode", "unicode_test_",
658                                tests, n_ents, argc, argv);
659 }