blob: 26d96336f36b26b4923f6ff25f1943ca64ed46c2 [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>
11#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +020014#include <test/test.h>
15#include <test/suites.h>
16#include <test/ut.h>
17
18/* Linker list entry for a Unicode test */
19#define UNICODE_TEST(_name) UNIT_TEST(_name, 0, unicode_test)
20
21/* Constants c1-c4 and d1-d4 encode the same letters */
22
23/* Six characters translating to one utf-8 byte each. */
24static const u16 c1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
25/* One character translating to two utf-8 bytes */
26static const u16 c2[] = {0x6b, 0x61, 0x66, 0x62, 0xe1, 0x74, 0x75, 0x72, 0x00};
27/* Three characters translating to three utf-8 bytes each */
28static const u16 c3[] = {0x6f5c, 0x6c34, 0x8266, 0x00};
29/* Three letters translating to four utf-8 bytes each */
30static const u16 c4[] = {0xd801, 0xdc8d, 0xd801, 0xdc96, 0xd801, 0xdc87,
31 0x0000};
32
33/* Illegal utf-16 strings */
34static const u16 i1[] = {0x69, 0x31, 0xdc87, 0x6c, 0x00};
35static const u16 i2[] = {0x69, 0x32, 0xd801, 0xd801, 0x6c, 0x00};
36static const u16 i3[] = {0x69, 0x33, 0xd801, 0x00};
37
38/* Six characters translating to one utf-16 word each. */
39static const char d1[] = {0x55, 0x2d, 0x42, 0x6f, 0x6f, 0x74, 0x00};
40/* Eight characters translating to one utf-16 word each */
41static const char d2[] = {0x6b, 0x61, 0x66, 0x62, 0xc3, 0xa1, 0x74, 0x75,
42 0x72, 0x00};
43/* Three characters translating to one utf-16 word each */
44static const char d3[] = {0xe6, 0xbd, 0x9c, 0xe6, 0xb0, 0xb4, 0xe8, 0x89,
45 0xa6, 0x00};
46/* Three letters translating to two utf-16 word each */
47static const char d4[] = {0xf0, 0x90, 0x92, 0x8d, 0xf0, 0x90, 0x92, 0x96,
48 0xf0, 0x90, 0x92, 0x87, 0x00};
49
50/* Illegal utf-8 strings */
51static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
52static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
53static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
54
Heinrich Schuchardt02b31dc2019-07-14 17:47:46 +020055static int unicode_test_u16_strlen(struct unit_test_state *uts)
56{
57 ut_asserteq(6, u16_strlen(c1));
58 ut_asserteq(8, u16_strlen(c2));
59 ut_asserteq(3, u16_strlen(c3));
60 ut_asserteq(6, u16_strlen(c4));
61 return 0;
62}
63UNICODE_TEST(unicode_test_u16_strlen);
64
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010065static int unicode_test_u16_strdup(struct unit_test_state *uts)
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010066{
67 u16 *copy = u16_strdup(c4);
68
69 ut_assert(copy != c4);
Simon Glassf91f3662020-05-10 12:52:45 -060070 ut_asserteq_mem(copy, c4, sizeof(c4));
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010071 free(copy);
Simon Glassf91f3662020-05-10 12:52:45 -060072
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010073 return 0;
74}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010075UNICODE_TEST(unicode_test_u16_strdup);
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010076
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010077static int unicode_test_u16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010078{
79 u16 *r;
80 u16 copy[10];
81
82 r = u16_strcpy(copy, c1);
83 ut_assert(r == copy);
Simon Glassf91f3662020-05-10 12:52:45 -060084 ut_asserteq_mem(copy, c1, sizeof(c1));
85
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010086 return 0;
87}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010088UNICODE_TEST(unicode_test_u16_strcpy);
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010089
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +020090/* U-Boot uses UTF-16 strings in the EFI context only. */
91#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010092static int unicode_test_string16(struct unit_test_state *uts)
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +020093{
94 char buf[20];
95
96 /* Test length and precision */
97 memset(buf, 0xff, sizeof(buf));
98 sprintf(buf, "%8.6ls", c2);
99 ut_asserteq(' ', buf[1]);
100 ut_assert(!strncmp(&buf[2], d2, 7));
101 ut_assert(!buf[9]);
102
103 memset(buf, 0xff, sizeof(buf));
104 sprintf(buf, "%8.6ls", c4);
105 ut_asserteq(' ', buf[4]);
106 ut_assert(!strncmp(&buf[5], d4, 12));
107 ut_assert(!buf[17]);
108
109 memset(buf, 0xff, sizeof(buf));
110 sprintf(buf, "%-8.2ls", c4);
111 ut_asserteq(' ', buf[8]);
112 ut_assert(!strncmp(buf, d4, 8));
113 ut_assert(!buf[14]);
114
115 /* Test handling of illegal utf-16 sequences */
116 memset(buf, 0xff, sizeof(buf));
117 sprintf(buf, "%ls", i1);
118 ut_asserteq_str("i1?l", buf);
119
120 memset(buf, 0xff, sizeof(buf));
121 sprintf(buf, "%ls", i2);
122 ut_asserteq_str("i2?l", buf);
123
124 memset(buf, 0xff, sizeof(buf));
125 sprintf(buf, "%ls", i3);
126 ut_asserteq_str("i3?", buf);
127
128 return 0;
129}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100130UNICODE_TEST(unicode_test_string16);
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200131#endif
132
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100133static int unicode_test_utf8_get(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200134{
135 const char *s;
136 s32 code;
137 int i;
138
139 /* Check characters less than 0x800 */
140 s = d2;
141 for (i = 0; i < 8; ++i) {
142 code = utf8_get((const char **)&s);
143 /* c2 is the utf-8 encoding of d2 */
144 ut_asserteq(c2[i], code);
145 if (!code)
146 break;
147 }
148 ut_asserteq_ptr(s, d2 + 9)
149
150 /* Check characters less than 0x10000 */
151 s = d3;
152 for (i = 0; i < 4; ++i) {
153 code = utf8_get((const char **)&s);
154 /* c3 is the utf-8 encoding of d3 */
155 ut_asserteq(c3[i], code);
156 if (!code)
157 break;
158 }
159 ut_asserteq_ptr(s, d3 + 9)
160
161 /* Check character greater 0xffff */
162 s = d4;
163 code = utf8_get((const char **)&s);
164 ut_asserteq(0x0001048d, code);
165 ut_asserteq_ptr(s, d4 + 4);
166
167 return 0;
168}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100169UNICODE_TEST(unicode_test_utf8_get);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200170
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100171static int unicode_test_utf8_put(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200172{
173 char buffer[8] = { 0, };
174 char *pos;
175
176 /* Commercial at, translates to one character */
177 pos = buffer;
178 ut_assert(!utf8_put('@', &pos))
179 ut_asserteq(1, pos - buffer);
180 ut_asserteq('@', buffer[0]);
181 ut_assert(!buffer[1]);
182
183 /* Latin letter G with acute, translates to two charactes */
184 pos = buffer;
185 ut_assert(!utf8_put(0x1f4, &pos));
186 ut_asserteq(2, pos - buffer);
187 ut_asserteq_str("\xc7\xb4", buffer);
188
189 /* Tagalog letter i, translates to three characters */
190 pos = buffer;
191 ut_assert(!utf8_put(0x1701, &pos));
192 ut_asserteq(3, pos - buffer);
193 ut_asserteq_str("\xe1\x9c\x81", buffer);
194
195 /* Hamster face, translates to four characters */
196 pos = buffer;
197 ut_assert(!utf8_put(0x1f439, &pos));
198 ut_asserteq(4, pos - buffer);
199 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
200
201 /* Illegal code */
202 pos = buffer;
203 ut_asserteq(-1, utf8_put(0xd888, &pos));
204
205 return 0;
206}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100207UNICODE_TEST(unicode_test_utf8_put);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200208
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100209static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200210{
211 ut_asserteq(6, utf8_utf16_strlen(d1));
212 ut_asserteq(8, utf8_utf16_strlen(d2));
213 ut_asserteq(3, utf8_utf16_strlen(d3));
214 ut_asserteq(6, utf8_utf16_strlen(d4));
215
216 /* illegal utf-8 sequences */
217 ut_asserteq(4, utf8_utf16_strlen(j1));
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200218 ut_asserteq(4, utf8_utf16_strlen(j2));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200219 ut_asserteq(3, utf8_utf16_strlen(j3));
220
221 return 0;
222}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100223UNICODE_TEST(unicode_test_utf8_utf16_strlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200224
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100225static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200226{
227 ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
228 ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
229 ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
230 ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
231 ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
232 ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
233
234 /* illegal utf-8 sequences */
235 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200236 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200237 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
238
239 return 0;
240}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100241UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200242
243/**
244 * ut_u16_strcmp() - Compare to u16 strings.
245 *
246 * @a1: first string
247 * @a2: second string
248 * @count: number of u16 to compare
249 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
250 */
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100251static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200252{
253 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
254 if (*a1 < *a2)
255 return -1;
256 if (*a1 > *a2)
257 return 1;
258 }
259 return 0;
260}
261
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100262static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200263{
264 u16 buf[16];
265 u16 *pos;
266
267 pos = buf;
268 utf8_utf16_strcpy(&pos, d1);
269 ut_asserteq(6, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100270 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200271
272 pos = buf;
273 utf8_utf16_strcpy(&pos, d2);
274 ut_asserteq(8, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100275 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200276
277 pos = buf;
278 utf8_utf16_strcpy(&pos, d3);
279 ut_asserteq(3, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100280 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200281
282 pos = buf;
283 utf8_utf16_strcpy(&pos, d4);
284 ut_asserteq(6, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100285 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200286
287 /* Illegal utf-8 strings */
288 pos = buf;
289 utf8_utf16_strcpy(&pos, j1);
290 ut_asserteq(4, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100291 ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200292
293 pos = buf;
294 utf8_utf16_strcpy(&pos, j2);
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200295 ut_asserteq(4, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100296 ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200297
298 pos = buf;
299 utf8_utf16_strcpy(&pos, j3);
300 ut_asserteq(3, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100301 ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200302
303 return 0;
304}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100305UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200306
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100307static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200308{
309 u16 buf[16];
310 u16 *pos;
311
312 pos = buf;
313 memset(buf, 0, sizeof(buf));
314 utf8_utf16_strncpy(&pos, d1, 4);
315 ut_asserteq(4, pos - buf);
316 ut_assert(!buf[4]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100317 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200318
319 pos = buf;
320 memset(buf, 0, sizeof(buf));
321 utf8_utf16_strncpy(&pos, d2, 10);
322 ut_asserteq(8, pos - buf);
323 ut_assert(buf[4]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100324 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200325
326 pos = buf;
327 memset(buf, 0, sizeof(buf));
328 utf8_utf16_strncpy(&pos, d3, 2);
329 ut_asserteq(2, pos - buf);
330 ut_assert(!buf[2]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100331 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200332
333 pos = buf;
334 memset(buf, 0, sizeof(buf));
335 utf8_utf16_strncpy(&pos, d4, 2);
336 ut_asserteq(4, pos - buf);
337 ut_assert(!buf[4]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100338 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200339
340 pos = buf;
341 memset(buf, 0, sizeof(buf));
342 utf8_utf16_strncpy(&pos, d4, 10);
343 ut_asserteq(6, pos - buf);
344 ut_assert(buf[5]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100345 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200346
347 return 0;
348}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100349UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200350
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100351static int unicode_test_utf16_get(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200352{
353 const u16 *s;
354 s32 code;
355 int i;
356
357 /* Check characters less than 0x10000 */
358 s = c2;
359 for (i = 0; i < 9; ++i) {
360 code = utf16_get((const u16 **)&s);
361 ut_asserteq(c2[i], code);
362 if (!code)
363 break;
364 }
365 ut_asserteq_ptr(c2 + 8, s);
366
367 /* Check character greater 0xffff */
368 s = c4;
369 code = utf16_get((const u16 **)&s);
370 ut_asserteq(0x0001048d, code);
371 ut_asserteq_ptr(c4 + 2, s);
372
373 return 0;
374}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100375UNICODE_TEST(unicode_test_utf16_get);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200376
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100377static int unicode_test_utf16_put(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200378{
379 u16 buffer[4] = { 0, };
380 u16 *pos;
381
382 /* Commercial at, translates to one word */
383 pos = buffer;
384 ut_assert(!utf16_put('@', &pos));
385 ut_asserteq(1, pos - buffer);
386 ut_asserteq((u16)'@', buffer[0]);
387 ut_assert(!buffer[1]);
388
389 /* Hamster face, translates to two words */
390 pos = buffer;
391 ut_assert(!utf16_put(0x1f439, &pos));
392 ut_asserteq(2, pos - buffer);
393 ut_asserteq((u16)0xd83d, buffer[0]);
394 ut_asserteq((u16)0xdc39, buffer[1]);
395 ut_assert(!buffer[2]);
396
397 /* Illegal code */
398 pos = buffer;
399 ut_asserteq(-1, utf16_put(0xd888, &pos));
400
401 return 0;
402}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100403UNICODE_TEST(unicode_test_utf16_put);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200404
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100405static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200406{
407 ut_asserteq(3, utf16_strnlen(c1, 3));
408 ut_asserteq(6, utf16_strnlen(c1, 13));
409 ut_asserteq(6, utf16_strnlen(c2, 6));
410 ut_asserteq(2, utf16_strnlen(c3, 2));
411 ut_asserteq(2, utf16_strnlen(c4, 2));
412 ut_asserteq(3, utf16_strnlen(c4, 3));
413
414 /* illegal utf-16 word sequences */
415 ut_asserteq(4, utf16_strnlen(i1, 16));
416 ut_asserteq(4, utf16_strnlen(i2, 16));
417 ut_asserteq(3, utf16_strnlen(i3, 16));
418
419 return 0;
420}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100421UNICODE_TEST(unicode_test_utf16_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200422
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100423static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200424{
425 ut_asserteq(6, utf16_utf8_strlen(c1));
426 ut_asserteq(9, utf16_utf8_strlen(c2));
427 ut_asserteq(9, utf16_utf8_strlen(c3));
428 ut_asserteq(12, utf16_utf8_strlen(c4));
429
430 /* illegal utf-16 word sequences */
431 ut_asserteq(4, utf16_utf8_strlen(i1));
432 ut_asserteq(4, utf16_utf8_strlen(i2));
433 ut_asserteq(3, utf16_utf8_strlen(i3));
434
435 return 0;
436}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100437UNICODE_TEST(unicode_test_utf16_utf8_strlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200438
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100439static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200440{
441 ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
442 ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
443 ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
444 ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
445 ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
446 ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
447 return 0;
448}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100449UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200450
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100451static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200452{
453 char buf[16];
454 char *pos;
455
456 pos = buf;
457 utf16_utf8_strcpy(&pos, c1);
458 ut_asserteq(6, pos - buf);
459 ut_asserteq_str(d1, buf);
460
461 pos = buf;
462 utf16_utf8_strcpy(&pos, c2);
463 ut_asserteq(9, pos - buf);
464 ut_asserteq_str(d2, buf);
465
466 pos = buf;
467 utf16_utf8_strcpy(&pos, c3);
468 ut_asserteq(9, pos - buf);
469 ut_asserteq_str(d3, buf);
470
471 pos = buf;
472 utf16_utf8_strcpy(&pos, c4);
473 ut_asserteq(12, pos - buf);
474 ut_asserteq_str(d4, buf);
475
476 /* Illegal utf-16 strings */
477 pos = buf;
478 utf16_utf8_strcpy(&pos, i1);
479 ut_asserteq(4, pos - buf);
480 ut_asserteq_str("i1?l", buf);
481
482 pos = buf;
483 utf16_utf8_strcpy(&pos, i2);
484 ut_asserteq(4, pos - buf);
485 ut_asserteq_str("i2?l", buf);
486
487 pos = buf;
488 utf16_utf8_strcpy(&pos, i3);
489 ut_asserteq(3, pos - buf);
490 ut_asserteq_str("i3?", buf);
491
492 return 0;
493}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100494UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200495
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100496static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200497{
498 char buf[16];
499 char *pos;
500
501 pos = buf;
502 memset(buf, 0, sizeof(buf));
503 utf16_utf8_strncpy(&pos, c1, 4);
504 ut_asserteq(4, pos - buf);
505 ut_assert(!buf[4]);
506 ut_assert(!strncmp(buf, d1, 4));
507
508 pos = buf;
509 memset(buf, 0, sizeof(buf));
510 utf16_utf8_strncpy(&pos, c2, 10);
511 ut_asserteq(9, pos - buf);
512 ut_assert(buf[4]);
513 ut_assert(!strncmp(buf, d2, SIZE_MAX));
514
515 pos = buf;
516 memset(buf, 0, sizeof(buf));
517 utf16_utf8_strncpy(&pos, c3, 2);
518 ut_asserteq(6, pos - buf);
519 ut_assert(!buf[6]);
520 ut_assert(!strncmp(buf, d3, 6));
521
522 pos = buf;
523 memset(buf, 0, sizeof(buf));
524 utf16_utf8_strncpy(&pos, c4, 2);
525 ut_asserteq(8, pos - buf);
526 ut_assert(!buf[8]);
527 ut_assert(!strncmp(buf, d4, 8));
528
529 pos = buf;
530 memset(buf, 0, sizeof(buf));
531 utf16_utf8_strncpy(&pos, c4, 10);
532 ut_asserteq(12, pos - buf);
533 ut_assert(buf[5]);
534 ut_assert(!strncmp(buf, d4, SIZE_MAX));
535
536 return 0;
537}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100538UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200539
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100540static int unicode_test_utf_to_lower(struct unit_test_state *uts)
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200541{
542 ut_asserteq('@', utf_to_lower('@'));
543 ut_asserteq('a', utf_to_lower('A'));
544 ut_asserteq('z', utf_to_lower('Z'));
545 ut_asserteq('[', utf_to_lower('['));
546 ut_asserteq('m', utf_to_lower('m'));
547 /* Latin letter O with diaresis (umlaut) */
548 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
549#ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
550 /* Cyrillic letter I*/
551 ut_asserteq(0x0438, utf_to_lower(0x0418));
552#endif
553 return 0;
554}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100555UNICODE_TEST(unicode_test_utf_to_lower);
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200556
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100557static int unicode_test_utf_to_upper(struct unit_test_state *uts)
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200558{
559 ut_asserteq('`', utf_to_upper('`'));
560 ut_asserteq('A', utf_to_upper('a'));
561 ut_asserteq('Z', utf_to_upper('z'));
562 ut_asserteq('{', utf_to_upper('{'));
563 ut_asserteq('M', utf_to_upper('M'));
564 /* Latin letter O with diaresis (umlaut) */
565 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
566#ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
567 /* Cyrillic letter I */
568 ut_asserteq(0x0418, utf_to_upper(0x0438));
569#endif
570 return 0;
571}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100572UNICODE_TEST(unicode_test_utf_to_upper);
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200573
AKASHI Takahiro79907a42019-09-18 10:26:30 +0900574static int unicode_test_u16_strncmp(struct unit_test_state *uts)
575{
576 ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
577 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
578 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
579 ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
580 ut_assert(u16_strcmp(L"abc", L"abc") == 0);
581 ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
582 ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
583 return 0;
584}
585UNICODE_TEST(unicode_test_u16_strncmp);
586
Heinrich Schuchardtefe3b5c2020-05-09 09:16:49 +0200587static int unicode_test_u16_strsize(struct unit_test_state *uts)
588{
589 ut_asserteq_64(u16_strsize(c1), 14);
590 ut_asserteq_64(u16_strsize(c2), 18);
591 ut_asserteq_64(u16_strsize(c3), 8);
592 ut_asserteq_64(u16_strsize(c4), 14);
593 return 0;
594}
595UNICODE_TEST(unicode_test_u16_strsize);
596
Simon Glass09140112020-05-10 11:40:03 -0600597int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200598{
599 struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
600 const int n_ents = ll_entry_count(struct unit_test, unicode_test);
601
Philippe Reynes4ad4edf2019-12-17 19:07:04 +0100602 return cmd_ut_category("Unicode", "unicode_test_",
603 tests, n_ents, argc, argv);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200604}