blob: 47c3f52774cdd2798c5f364a93c486db0dd0b6d7 [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 Schuchardtf823e322022-12-18 05:32:14 +000070static 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}
79UNICODE_TEST(unicode_test_u16_strnlen);
80
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010081static int unicode_test_u16_strdup(struct unit_test_state *uts)
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010082{
83 u16 *copy = u16_strdup(c4);
84
85 ut_assert(copy != c4);
Simon Glassf91f3662020-05-10 12:52:45 -060086 ut_asserteq_mem(copy, c4, sizeof(c4));
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010087 free(copy);
Simon Glassf91f3662020-05-10 12:52:45 -060088
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010089 return 0;
90}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010091UNICODE_TEST(unicode_test_u16_strdup);
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010092
Heinrich Schuchardtbc196812019-02-15 23:12:50 +010093static int unicode_test_u16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +010094{
95 u16 *r;
96 u16 copy[10];
97
98 r = u16_strcpy(copy, c1);
99 ut_assert(r == copy);
Simon Glassf91f3662020-05-10 12:52:45 -0600100 ut_asserteq_mem(copy, c1, sizeof(c1));
101
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +0100102 return 0;
103}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100104UNICODE_TEST(unicode_test_u16_strcpy);
Heinrich Schuchardtabb93cb2018-12-14 22:00:37 +0100105
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200106/* U-Boot uses UTF-16 strings in the EFI context only. */
107#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100108static int unicode_test_string16(struct unit_test_state *uts)
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200109{
110 char buf[20];
Heinrich Schuchardtc672dd72022-01-29 18:28:08 +0100111 int ret;
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200112
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
Heinrich Schuchardtc672dd72022-01-29 18:28:08 +0100145 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
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200175 return 0;
176}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100177UNICODE_TEST(unicode_test_string16);
Heinrich Schuchardtfbba2f62018-08-31 21:31:30 +0200178#endif
179
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100180static int unicode_test_utf8_get(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200181{
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 }
Marek Vasutfa847bb2023-03-10 04:33:13 +0100195 ut_asserteq_ptr(s, d2 + 9);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200196
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 }
Marek Vasutfa847bb2023-03-10 04:33:13 +0100206 ut_asserteq_ptr(s, d3 + 9);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200207
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
Heinrich Schuchardtddbaff52021-02-27 14:08:37 +0100214 /* 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
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200220 return 0;
221}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100222UNICODE_TEST(unicode_test_utf8_get);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200223
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100224static int unicode_test_utf8_put(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200225{
226 char buffer[8] = { 0, };
227 char *pos;
228
229 /* Commercial at, translates to one character */
230 pos = buffer;
Marek Vasutfa847bb2023-03-10 04:33:13 +0100231 ut_assert(!utf8_put('@', &pos));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200232 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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100260UNICODE_TEST(unicode_test_utf8_put);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200261
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100262static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200263{
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));
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200271 ut_asserteq(4, utf8_utf16_strlen(j2));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200272 ut_asserteq(3, utf8_utf16_strlen(j3));
273
274 return 0;
275}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100276UNICODE_TEST(unicode_test_utf8_utf16_strlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200277
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100278static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200279{
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));
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200289 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200290 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
291
292 return 0;
293}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100294UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200295
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 */
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100304static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200305{
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
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100315static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200316{
317 u16 buf[16];
318 u16 *pos;
319
320 pos = buf;
321 utf8_utf16_strcpy(&pos, d1);
322 ut_asserteq(6, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100323 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200324
325 pos = buf;
326 utf8_utf16_strcpy(&pos, d2);
327 ut_asserteq(8, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100328 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200329
330 pos = buf;
331 utf8_utf16_strcpy(&pos, d3);
332 ut_asserteq(3, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100333 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200334
335 pos = buf;
336 utf8_utf16_strcpy(&pos, d4);
337 ut_asserteq(6, pos - buf);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100338 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200339
340 /* Illegal utf-8 strings */
341 pos = buf;
342 utf8_utf16_strcpy(&pos, j1);
343 ut_asserteq(4, pos - buf);
Simon Glass5b9a5b22022-01-23 12:55:14 -0700344 ut_assert(!unicode_test_u16_strcmp(buf, u"j1?l", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200345
346 pos = buf;
347 utf8_utf16_strcpy(&pos, j2);
Heinrich Schuchardt35cbb792018-09-12 00:05:32 +0200348 ut_asserteq(4, pos - buf);
Simon Glass5b9a5b22022-01-23 12:55:14 -0700349 ut_assert(!unicode_test_u16_strcmp(buf, u"j2?l", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200350
351 pos = buf;
352 utf8_utf16_strcpy(&pos, j3);
353 ut_asserteq(3, pos - buf);
Simon Glass5b9a5b22022-01-23 12:55:14 -0700354 ut_assert(!unicode_test_u16_strcmp(buf, u"j3?", SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200355
356 return 0;
357}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100358UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200359
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100360static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200361{
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]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100370 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200371
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]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100377 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200378
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]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100384 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200385
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]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100391 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200392
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]);
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100398 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200399
400 return 0;
401}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100402UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200403
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100404static int unicode_test_utf16_get(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200405{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100428UNICODE_TEST(unicode_test_utf16_get);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200429
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100430static int unicode_test_utf16_put(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200431{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100456UNICODE_TEST(unicode_test_utf16_put);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200457
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100458static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200459{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100474UNICODE_TEST(unicode_test_utf16_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200475
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100476static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200477{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100490UNICODE_TEST(unicode_test_utf16_utf8_strlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200491
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100492static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200493{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100502UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200503
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100504static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200505{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100547UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200548
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100549static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200550{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100591UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200592
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100593static int unicode_test_utf_to_lower(struct unit_test_state *uts)
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200594{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100608UNICODE_TEST(unicode_test_utf_to_lower);
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200609
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100610static int unicode_test_utf_to_upper(struct unit_test_state *uts)
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200611{
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}
Heinrich Schuchardtbc196812019-02-15 23:12:50 +0100625UNICODE_TEST(unicode_test_utf_to_upper);
Heinrich Schuchardt1a1012a2018-09-04 19:34:57 +0200626
Heinrich Schuchardt07355762022-12-29 14:44:04 +0100627static 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}
650UNICODE_TEST(unicode_test_u16_strcasecmp);
651
AKASHI Takahiro79907a42019-09-18 10:26:30 +0900652static int unicode_test_u16_strncmp(struct unit_test_state *uts)
653{
Simon Glass5b9a5b22022-01-23 12:55:14 -0700654 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);
AKASHI Takahiro79907a42019-09-18 10:26:30 +0900661 return 0;
662}
663UNICODE_TEST(unicode_test_u16_strncmp);
664
Heinrich Schuchardtefe3b5c2020-05-09 09:16:49 +0200665static 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}
673UNICODE_TEST(unicode_test_u16_strsize);
674
Heinrich Schuchardt73bb90c2021-02-27 14:08:36 +0100675static 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}
702UNICODE_TEST(unicode_test_utf_to_cp);
703
Heinrich Schuchardte91789e2021-02-27 14:08:38 +0100704static 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
718static 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}
733UNICODE_TEST(unicode_test_utf8_to_cp437_stream);
734
735static 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
749static 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};
Heinrich Schuchardtaeba3852024-01-18 18:57:12 +0100755 const u32 u3[] = {0x6f5c, 0x6c34, 0x8266};
Heinrich Schuchardte91789e2021-02-27 14:08:38 +0100756 const u32 u4[] = {0x6A, 0x32, 0x6C, 0x00};
Heinrich Schuchardtaeba3852024-01-18 18:57:12 +0100757 const u32 u5[] = {0x0392, 0x20, 0x69, 0x73, 0x20, 0x6E, 0x6F, 0x74,
758 0x20, 0x42, 0x00};
Heinrich Schuchardte91789e2021-02-27 14:08:38 +0100759
760 memset(buf, 0, sizeof(buf));
761 utf8_to_utf32_stream_helper(d1, buf);
762 ut_asserteq_mem(u1, buf, sizeof(u1));
763
764 memset(buf, 0, sizeof(buf));
765 utf8_to_utf32_stream_helper(d2, buf);
766 ut_asserteq_mem(u2, buf, sizeof(u2));
767
768 memset(buf, 0, sizeof(buf));
Heinrich Schuchardtaeba3852024-01-18 18:57:12 +0100769 utf8_to_utf32_stream_helper(d3, buf);
Heinrich Schuchardte91789e2021-02-27 14:08:38 +0100770 ut_asserteq_mem(u3, buf, sizeof(u3));
771
772 memset(buf, 0, sizeof(buf));
Heinrich Schuchardtaeba3852024-01-18 18:57:12 +0100773 utf8_to_utf32_stream_helper(d5, buf);
774 ut_asserteq_mem(u5, buf, sizeof(u5));
775
776 memset(buf, 0, sizeof(buf));
Heinrich Schuchardte91789e2021-02-27 14:08:38 +0100777 utf8_to_utf32_stream_helper(j2, buf);
778 ut_asserteq_mem(u4, buf, sizeof(u4));
779
780 return 0;
781}
782UNICODE_TEST(unicode_test_utf8_to_utf32_stream);
783
Heinrich Schuchardtaf114232020-10-30 12:23:59 +0100784#ifdef CONFIG_EFI_LOADER
785static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
786{
787 u16 buf[16];
Simon Glass5b9a5b22022-01-23 12:55:14 -0700788 u16 const expected[] = u"Capsule0AF9";
Heinrich Schuchardtaf114232020-10-30 12:23:59 +0100789 u16 *pos;
790
791 memset(buf, 0xeb, sizeof(buf));
Ilias Apalodimasfe179d72020-12-31 12:26:46 +0200792 pos = efi_create_indexed_name(buf, sizeof(buf), "Capsule", 0x0af9);
Heinrich Schuchardtaf114232020-10-30 12:23:59 +0100793
794 ut_asserteq_mem(expected, buf, sizeof(expected));
795 ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
796
797 return 0;
798}
799UNICODE_TEST(unicode_test_efi_create_indexed_name);
800#endif
801
Masahisa Kojimab8cd1e72022-04-28 17:09:35 +0900802static int unicode_test_u16_strlcat(struct unit_test_state *uts)
803{
804 u16 buf[40];
805 u16 dest[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f, 0};
806 u16 src[] = {0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
807 u16 concat_str[] = {0x3053, 0x3093, 0x306b, 0x3061, 0x306f,
808 0x03B1, 0x2172, 0x6F5C, 0x8247, 0};
809 u16 null_src = u'\0';
810 size_t ret, expected;
811 int i;
812
813 /* dest and src are empty string */
814 memset(buf, 0, sizeof(buf));
Dan Carpenterbe5f9a72023-07-27 10:12:58 +0300815 ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf));
Matthias Schiffer7c00b802023-07-14 13:24:51 +0200816 ut_asserteq(0, ret);
Masahisa Kojimab8cd1e72022-04-28 17:09:35 +0900817
818 /* dest is empty string */
819 memset(buf, 0, sizeof(buf));
Dan Carpenterbe5f9a72023-07-27 10:12:58 +0300820 ret = u16_strlcat(buf, src, ARRAY_SIZE(buf));
Matthias Schiffer7c00b802023-07-14 13:24:51 +0200821 ut_asserteq(4, ret);
Masahisa Kojimab8cd1e72022-04-28 17:09:35 +0900822 ut_assert(!unicode_test_u16_strcmp(buf, src, 40));
823
824 /* src is empty string */
825 memset(buf, 0xCD, (sizeof(buf) - sizeof(u16)));
826 buf[39] = 0;
827 memcpy(buf, dest, sizeof(dest));
Dan Carpenterbe5f9a72023-07-27 10:12:58 +0300828 ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf));
Matthias Schiffer7c00b802023-07-14 13:24:51 +0200829 ut_asserteq(5, ret);
Masahisa Kojimab8cd1e72022-04-28 17:09:35 +0900830 ut_assert(!unicode_test_u16_strcmp(buf, dest, 40));
831
832 for (i = 0; i <= 40; i++) {
833 memset(buf, 0xCD, (sizeof(buf) - sizeof(u16)));
834 buf[39] = 0;
835 memcpy(buf, dest, sizeof(dest));
Matthias Schiffer7c00b802023-07-14 13:24:51 +0200836 expected = min(5, i) + 4;
Masahisa Kojimab8cd1e72022-04-28 17:09:35 +0900837 ret = u16_strlcat(buf, src, i);
838 ut_asserteq(expected, ret);
839 if (i <= 6) {
840 ut_assert(!unicode_test_u16_strcmp(buf, dest, 40));
841 } else if (i < 10) {
842 ut_assert(!unicode_test_u16_strcmp(buf, concat_str, i - 1));
843 } else {
844 ut_assert(!unicode_test_u16_strcmp(buf, concat_str, 40));
845 }
846 }
847
848 return 0;
849}
850UNICODE_TEST(unicode_test_u16_strlcat);
851
Simon Glass09140112020-05-10 11:40:03 -0600852int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200853{
Simon Glassa7a98752021-03-07 17:35:10 -0700854 struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
855 const int n_ents = UNIT_TEST_SUITE_COUNT(unicode_test);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200856
Philippe Reynes4ad4edf2019-12-17 19:07:04 +0100857 return cmd_ut_category("Unicode", "unicode_test_",
858 tests, n_ents, argc, argv);
Heinrich Schuchardtf11a1642018-08-31 21:31:28 +0200859}