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