blob: f2f63d5367b2582aa087314378d6549098ca6a5e [file] [log] [blame]
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +02001// 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>
Heinrich Schuchardtaf114232020-10-30 12:23:59 +010011#include <efi_loader.h>
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +020012#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +020015#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. */
25static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
26/* One character translating to two utf-8 bytes */
27static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
28/* Three characters translating to three utf-8 bytes each */
29static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
30/* Three letters translating to four utf-8 bytes each */
31static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
32 0x0000};
33
34/* Illegal utf-16 strings */
35static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
36static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
37static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
38
39/* Six characters translating to one utf-16 word each. */
40static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
41/* Eight characters translating to one utf-16 word each */
42static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
43 0x72, 0x00};
44/* Three characters translating to one utf-16 word each */
45static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
46 0xa6, 0x00};
47/* Three letters translating to two utf-16 word each */
48static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
49 0xf0, 0x90, 0x92, 0x87, 0x00};
Heinrich Schuchardte91789e2021-02-27 14:08:38 +010050/* Letter not in code page 437 */
51static const char d5[] = {0xCE, 0x92, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F,
52 0x74, 0x20, 0x42, 0x00};
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +020053
54/* Illegal utf-8 strings */
55static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
56static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
57static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
Heinrich Schuchardtddbaff52021-02-27 14:08:37 +010058static const char j4[] = {0xa1, 0x00};
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +020059
Heinrich Schuchardt02b31dc2019-07-14 17:47:46 +020060static 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}
68UNICODE_TEST(unicode_test_u16_strlen);
69
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010070static int unicode_test_u16_strdup(struct unit_test_state *uts)
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010071{
72 u16 *copy = u16_strdup(c4);
73
74 ut_assert(copy != c4);
Simon Glassf91f3662020-05-10 12:52:45 -060075 ut_asserteq_mem(copy, c4, sizeof(c4));
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010076 free(copy);
Simon Glassf91f3662020-05-10 12:52:45 -060077
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010078 return 0;
79}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010080UNICODE_TEST(unicode_test_u16_strdup);
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010081
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010082static int unicode_test_u16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010083{
84 u16 *r;
85 u16 copy[10];
86
87 r = u16_strcpy(copy, c1);
88 ut_assert(r == copy);
Simon Glassf91f3662020-05-10 12:52:45 -060089 ut_asserteq_mem(copy, c1, sizeof(c1));
90
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010091 return 0;
92}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010093UNICODE_TEST(unicode_test_u16_strcpy);
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010094
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +020095/* U-Boot uses UTF-16 strings in the EFI context only. */
96#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010097static int unicode_test_string16(struct unit_test_state *uts)
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +020098{
99 char buf[20];
Heinrich Schuchardtc672dd72022-01-29 18:28:08 +0100100 int ret;
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200101
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));
107 ut_assert(!buf[9]);
108
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));
113 ut_assert(!buf[17]);
114
115 memset(buf, 0xff, sizeof(buf));
116 sprintf(buf, "%-8.2ls", c4);
117 ut_asserteq(' ', buf[8]);
118 ut_assert(!strncmp(buf, d4, 8));
119 ut_assert(!buf[14]);
120
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);
125
126 memset(buf, 0xff, sizeof(buf));
127 sprintf(buf, "%ls", i2);
128 ut_asserteq_str("i2?l", buf);
129
130 memset(buf, 0xff, sizeof(buf));
131 sprintf(buf, "%ls", i3);
132 ut_asserteq_str("i3?", buf);
133
Heinrich Schuchardtc672dd72022-01-29 18:28:08 +0100134 memset(buf, 0xff, sizeof(buf));
135 ret = snprintf(buf, 4, "%ls", c1);
136 ut_asserteq(6, ret);
137 ut_asserteq_str("U-B", buf);
138
139 memset(buf, 0xff, sizeof(buf));
140 ret = snprintf(buf, 6, "%ls", c2);
141 ut_asserteq_str("kafb", buf);
142 ut_asserteq(9, ret);
143
144 memset(buf, 0xff, sizeof(buf));
145 ret = snprintf(buf, 7, "%ls", c2);
146 ut_asserteq_str("kafb\xC3\xA1", buf);
147 ut_asserteq(9, ret);
148
149 memset(buf, 0xff, sizeof(buf));
150 ret = snprintf(buf, 8, "%ls", c3);
151 ut_asserteq_str("\xE6\xBD\x9C\xE6\xB0\xB4", buf);
152 ut_asserteq(9, ret);
153
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);
158
159 memset(buf, 0xff, sizeof(buf));
160 ret = snprintf(buf, 4, "%ls", c4);
161 ut_asserteq_str("", buf);
162 ut_asserteq(12, ret);
163
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200164 return 0;
165}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100166UNICODE_TEST(unicode_test_string16);
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200167#endif
168
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100169static int unicode_test_utf8_get(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200170{
171 const char *s;
172 s32 code;
173 int i;
174
175 /* Check characters less than 0x800 */
176 s = d2;
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);
181 if (!code)
182 break;
183 }
184 ut_asserteq_ptr(s, d2 + 9)
185
186 /* Check characters less than 0x10000 */
187 s = d3;
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);
192 if (!code)
193 break;
194 }
195 ut_asserteq_ptr(s, d3 + 9)
196
197 /* Check character greater 0xffff */
198 s = d4;
199 code = utf8_get((const char **)&s);
200 ut_asserteq(0x0001048d, code);
201 ut_asserteq_ptr(s, d4 + 4);
202
Heinrich Schuchardtddbaff52021-02-27 14:08:37 +0100203 /* Check illegal character */
204 s = j4;
205 code = utf8_get((const char **)&s);
206 ut_asserteq(-1, code);
207 ut_asserteq_ptr(j4 + 1, s);
208
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200209 return 0;
210}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100211UNICODE_TEST(unicode_test_utf8_get);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200212
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100213static int unicode_test_utf8_put(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200214{
215 char buffer[8] = { 0, };
216 char *pos;
217
218 /* Commercial at, translates to one character */
219 pos = buffer;
220 ut_assert(!utf8_put('@', &pos))
221 ut_asserteq(1, pos - buffer);
222 ut_asserteq('@', buffer[0]);
223 ut_assert(!buffer[1]);
224
225 /* Latin letter G with acute, translates to two charactes */
226 pos = buffer;
227 ut_assert(!utf8_put(0x1f4, &pos));
228 ut_asserteq(2, pos - buffer);
229 ut_asserteq_str("\xc7\xb4", buffer);
230
231 /* Tagalog letter i, translates to three characters */
232 pos = buffer;
233 ut_assert(!utf8_put(0x1701, &pos));
234 ut_asserteq(3, pos - buffer);
235 ut_asserteq_str("\xe1\x9c\x81", buffer);
236
237 /* Hamster face, translates to four characters */
238 pos = buffer;
239 ut_assert(!utf8_put(0x1f439, &pos));
240 ut_asserteq(4, pos - buffer);
241 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
242
243 /* Illegal code */
244 pos = buffer;
245 ut_asserteq(-1, utf8_put(0xd888, &pos));
246
247 return 0;
248}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100249UNICODE_TEST(unicode_test_utf8_put);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200250
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100251static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200252{
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));
257
258 /* illegal utf-8 sequences */
259 ut_asserteq(4, utf8_utf16_strlen(j1));
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200260 ut_asserteq(4, utf8_utf16_strlen(j2));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200261 ut_asserteq(3, utf8_utf16_strlen(j3));
262
263 return 0;
264}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100265UNICODE_TEST(unicode_test_utf8_utf16_strlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200266
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100267static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200268{
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));
275
276 /* illegal utf-8 sequences */
277 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200278 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200279 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
280
281 return 0;
282}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100283UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200284
285/**
286 * ut_u16_strcmp() - Compare to u16 strings.
287 *
288 * @a1: first string
289 * @a2: second string
290 * @count: number of u16 to compare
291 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
292 */
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100293static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200294{
295 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
296 if (*a1 < *a2)
297 return -1;
298 if (*a1 > *a2)
299 return 1;
300 }
301 return 0;
302}
303
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100304static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200305{
306 u16 buf[16];
307 u16 *pos;
308
309 pos = buf;
310 utf8_utf16_strcpy(&pos, d1);
311 ut_asserteq(6, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100312 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200313
314 pos = buf;
315 utf8_utf16_strcpy(&pos, d2);
316 ut_asserteq(8, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100317 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200318
319 pos = buf;
320 utf8_utf16_strcpy(&pos, d3);
321 ut_asserteq(3, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100322 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200323
324 pos = buf;
325 utf8_utf16_strcpy(&pos, d4);
326 ut_asserteq(6, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100327 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200328
329 /* Illegal utf-8 strings */
330 pos = buf;
331 utf8_utf16_strcpy(&pos, j1);
332 ut_asserteq(4, pos - buf);
Simon Glass5b9a5b22022-01-23 12:55:14 -0700333 ut_assert(!unicode_test_u16_strcmp(buf, u"j1?l", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200334
335 pos = buf;
336 utf8_utf16_strcpy(&pos, j2);
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200337 ut_asserteq(4, pos - buf);
Simon Glass5b9a5b22022-01-23 12:55:14 -0700338 ut_assert(!unicode_test_u16_strcmp(buf, u"j2?l", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200339
340 pos = buf;
341 utf8_utf16_strcpy(&pos, j3);
342 ut_asserteq(3, pos - buf);
Simon Glass5b9a5b22022-01-23 12:55:14 -0700343 ut_assert(!unicode_test_u16_strcmp(buf, u"j3?", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200344
345 return 0;
346}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100347UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200348
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100349static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200350{
351 u16 buf[16];
352 u16 *pos;
353
354 pos = buf;
355 memset(buf, 0, sizeof(buf));
356 utf8_utf16_strncpy(&pos, d1, 4);
357 ut_asserteq(4, pos - buf);
358 ut_assert(!buf[4]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100359 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200360
361 pos = buf;
362 memset(buf, 0, sizeof(buf));
363 utf8_utf16_strncpy(&pos, d2, 10);
364 ut_asserteq(8, pos - buf);
365 ut_assert(buf[4]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100366 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200367
368 pos = buf;
369 memset(buf, 0, sizeof(buf));
370 utf8_utf16_strncpy(&pos, d3, 2);
371 ut_asserteq(2, pos - buf);
372 ut_assert(!buf[2]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100373 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200374
375 pos = buf;
376 memset(buf, 0, sizeof(buf));
377 utf8_utf16_strncpy(&pos, d4, 2);
378 ut_asserteq(4, pos - buf);
379 ut_assert(!buf[4]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100380 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200381
382 pos = buf;
383 memset(buf, 0, sizeof(buf));
384 utf8_utf16_strncpy(&pos, d4, 10);
385 ut_asserteq(6, pos - buf);
386 ut_assert(buf[5]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100387 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200388
389 return 0;
390}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100391UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200392
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100393static int unicode_test_utf16_get(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200394{
395 const u16 *s;
396 s32 code;
397 int i;
398
399 /* Check characters less than 0x10000 */
400 s = c2;
401 for (i = 0; i < 9; ++i) {
402 code = utf16_get((const u16 **)&s);
403 ut_asserteq(c2[i], code);
404 if (!code)
405 break;
406 }
407 ut_asserteq_ptr(c2 + 8, s);
408
409 /* Check character greater 0xffff */
410 s = c4;
411 code = utf16_get((const u16 **)&s);
412 ut_asserteq(0x0001048d, code);
413 ut_asserteq_ptr(c4 + 2, s);
414
415 return 0;
416}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100417UNICODE_TEST(unicode_test_utf16_get);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200418
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100419static int unicode_test_utf16_put(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200420{
421 u16 buffer[4] = { 0, };
422 u16 *pos;
423
424 /* Commercial at, translates to one word */
425 pos = buffer;
426 ut_assert(!utf16_put('@', &pos));
427 ut_asserteq(1, pos - buffer);
428 ut_asserteq((u16)'@', buffer[0]);
429 ut_assert(!buffer[1]);
430
431 /* Hamster face, translates to two words */
432 pos = buffer;
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]);
438
439 /* Illegal code */
440 pos = buffer;
441 ut_asserteq(-1, utf16_put(0xd888, &pos));
442
443 return 0;
444}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100445UNICODE_TEST(unicode_test_utf16_put);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200446
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100447static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200448{
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));
455
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));
460
461 return 0;
462}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100463UNICODE_TEST(unicode_test_utf16_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200464
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100465static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200466{
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));
471
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));
476
477 return 0;
478}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100479UNICODE_TEST(unicode_test_utf16_utf8_strlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200480
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100481static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200482{
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));
489 return 0;
490}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100491UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200492
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100493static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200494{
495 char buf[16];
496 char *pos;
497
498 pos = buf;
499 utf16_utf8_strcpy(&pos, c1);
500 ut_asserteq(6, pos - buf);
501 ut_asserteq_str(d1, buf);
502
503 pos = buf;
504 utf16_utf8_strcpy(&pos, c2);
505 ut_asserteq(9, pos - buf);
506 ut_asserteq_str(d2, buf);
507
508 pos = buf;
509 utf16_utf8_strcpy(&pos, c3);
510 ut_asserteq(9, pos - buf);
511 ut_asserteq_str(d3, buf);
512
513 pos = buf;
514 utf16_utf8_strcpy(&pos, c4);
515 ut_asserteq(12, pos - buf);
516 ut_asserteq_str(d4, buf);
517
518 /* Illegal utf-16 strings */
519 pos = buf;
520 utf16_utf8_strcpy(&pos, i1);
521 ut_asserteq(4, pos - buf);
522 ut_asserteq_str("i1?l", buf);
523
524 pos = buf;
525 utf16_utf8_strcpy(&pos, i2);
526 ut_asserteq(4, pos - buf);
527 ut_asserteq_str("i2?l", buf);
528
529 pos = buf;
530 utf16_utf8_strcpy(&pos, i3);
531 ut_asserteq(3, pos - buf);
532 ut_asserteq_str("i3?", buf);
533
534 return 0;
535}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100536UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200537
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100538static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200539{
540 char buf[16];
541 char *pos;
542
543 pos = buf;
544 memset(buf, 0, sizeof(buf));
545 utf16_utf8_strncpy(&pos, c1, 4);
546 ut_asserteq(4, pos - buf);
547 ut_assert(!buf[4]);
548 ut_assert(!strncmp(buf, d1, 4));
549
550 pos = buf;
551 memset(buf, 0, sizeof(buf));
552 utf16_utf8_strncpy(&pos, c2, 10);
553 ut_asserteq(9, pos - buf);
554 ut_assert(buf[4]);
555 ut_assert(!strncmp(buf, d2, SIZE_MAX));
556
557 pos = buf;
558 memset(buf, 0, sizeof(buf));
559 utf16_utf8_strncpy(&pos, c3, 2);
560 ut_asserteq(6, pos - buf);
561 ut_assert(!buf[6]);
562 ut_assert(!strncmp(buf, d3, 6));
563
564 pos = buf;
565 memset(buf, 0, sizeof(buf));
566 utf16_utf8_strncpy(&pos, c4, 2);
567 ut_asserteq(8, pos - buf);
568 ut_assert(!buf[8]);
569 ut_assert(!strncmp(buf, d4, 8));
570
571 pos = buf;
572 memset(buf, 0, sizeof(buf));
573 utf16_utf8_strncpy(&pos, c4, 10);
574 ut_asserteq(12, pos - buf);
575 ut_assert(buf[5]);
576 ut_assert(!strncmp(buf, d4, SIZE_MAX));
577
578 return 0;
579}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100580UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200581
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100582static int unicode_test_utf_to_lower(struct unit_test_state *uts)
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200583{
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));
594#endif
595 return 0;
596}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100597UNICODE_TEST(unicode_test_utf_to_lower);
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200598
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100599static int unicode_test_utf_to_upper(struct unit_test_state *uts)
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200600{
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));
611#endif
612 return 0;
613}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100614UNICODE_TEST(unicode_test_utf_to_upper);
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200615
AKASHI Takahiro79907a42019-09-18 10:26:30 +0900616static int unicode_test_u16_strncmp(struct unit_test_state *uts)
617{
Simon Glass5b9a5b22022-01-23 12:55:14 -0700618 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);
AKASHI Takahiro79907a42019-09-18 10:26:30 +0900625 return 0;
626}
627UNICODE_TEST(unicode_test_u16_strncmp);
628
Heinrich Schuchardtefe3b5c2020-05-09 09:16:49 +0200629static int unicode_test_u16_strsize(struct unit_test_state *uts)
630{
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);
635 return 0;
636}
637UNICODE_TEST(unicode_test_u16_strsize);
638
Heinrich Schuchardt73bb90c2021-02-27 14:08:36 +0100639static int unicode_test_utf_to_cp(struct unit_test_state *uts)
640{
641 int ret;
642 s32 c;
643
644 c = '\n';
645 ret = utf_to_cp(&c, codepage_437);
646 ut_asserteq(0, ret);
647 ut_asserteq('\n', c);
648
649 c = 'a';
650 ret = utf_to_cp(&c, codepage_437);
651 ut_asserteq(0, ret);
652 ut_asserteq('a', c);
653
654 c = 0x03c4; /* Greek small letter tau */
655 ret = utf_to_cp(&c, codepage_437);
656 ut_asserteq(0, ret);
657 ut_asserteq(0xe7, c);
658
659 c = 0x03a4; /* Greek capital letter tau */
660 ret = utf_to_cp(&c, codepage_437);
661 ut_asserteq(-ENOENT, ret);
662 ut_asserteq('?', c);
663
664 return 0;
665}
666UNICODE_TEST(unicode_test_utf_to_cp);
667
Heinrich Schuchardte91789e2021-02-27 14:08:38 +0100668static void utf8_to_cp437_stream_helper(const char *in, char *out)
669{
670 char buffer[5];
671 int ret;
672
673 *buffer = 0;
674 for (; *in; ++in) {
675 ret = utf8_to_cp437_stream(*in, buffer);
676 if (ret)
677 *out++ = ret;
678 }
679 *out = 0;
680}
681
682static int unicode_test_utf8_to_cp437_stream(struct unit_test_state *uts)
683{
684 char buf[16];
685
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);
694
695 return 0;
696}
697UNICODE_TEST(unicode_test_utf8_to_cp437_stream);
698
699static void utf8_to_utf32_stream_helper(const char *in, s32 *out)
700{
701 char buffer[5];
702 int ret;
703
704 *buffer = 0;
705 for (; *in; ++in) {
706 ret = utf8_to_utf32_stream(*in, buffer);
707 if (ret)
708 *out++ = ret;
709 }
710 *out = 0;
711}
712
713static int unicode_test_utf8_to_utf32_stream(struct unit_test_state *uts)
714{
715 s32 buf[16];
716
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,
720 0x20, 0x42, 0x00};
721 const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00};
722
723 memset(buf, 0, sizeof(buf));
724 utf8_to_utf32_stream_helper(d1, buf);
725 ut_asserteq_mem(u1, buf, sizeof(u1));
726
727 memset(buf, 0, sizeof(buf));
728 utf8_to_utf32_stream_helper(d2, buf);
729 ut_asserteq_mem(u2, buf, sizeof(u2));
730
731 memset(buf, 0, sizeof(buf));
732 utf8_to_utf32_stream_helper(d5, buf);
733 ut_asserteq_mem(u3, buf, sizeof(u3));
734
735 memset(buf, 0, sizeof(buf));
736 utf8_to_utf32_stream_helper(j2, buf);
737 ut_asserteq_mem(u4, buf, sizeof(u4));
738
739 return 0;
740}
741UNICODE_TEST(unicode_test_utf8_to_utf32_stream);
742
Heinrich Schuchardtaf114232020-10-30 12:23:59 +0100743#ifdef CONFIG_EFI_LOADER
744static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
745{
746 u16 buf[16];
Simon Glass5b9a5b22022-01-23 12:55:14 -0700747 u16 const expected[] = u"Capsule0AF9";
Heinrich Schuchardtaf114232020-10-30 12:23:59 +0100748 u16 *pos;
749
750 memset(buf, 0xeb, sizeof(buf));
Ilias Apalodimasfe179d72020-12-31 12:26:46 +0200751 pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
Heinrich Schuchardtaf114232020-10-30 12:23:59 +0100752
753 ut_asserteq_mem(expected, buf, sizeof(expected));
754 ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
755
756 return 0;
757}
758UNICODE_TEST(unicode_test_efi_create_indexed_name);
759#endif
760
Simon Glass09140112020-05-10 11:40:03 -0600761int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200762{
Simon Glassa7a98752021-03-07 17:35:10 -0700763 struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
764 const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200765
Philippe Reynes4ad4edf2019-12-17 19:07:04 +0100766 return cmd_ut_category("Unicode", "unicode_test_",
767 tests, n_ents, argc, argv);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200768}