blob: 33fc8b0ee1e257f3a52c77dd6c3c558fcca5431d [file] [log] [blame]
Heinrich Schuchardtb8b6c812018-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 Schuchardt31805542020-10-30 12:23:59 +010011#include <efi_loader.h>
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +020012#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Heinrich Schuchardtb8b6c812018-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};
50
51/* Illegal utf-8 strings */
52static const char j1[] = {0x6a, 0x31, 0xa1, 0x6c, 0x00};
53static const char j2[] = {0x6a, 0x32, 0xc3, 0xc3, 0x6c, 0x00};
54static const char j3[] = {0x6a, 0x33, 0xf0, 0x90, 0xf0, 0x00};
55
Heinrich Schuchardt551983d2019-07-14 17:47:46 +020056static int unicode_test_u16_strlen(struct unit_test_state *uts)
57{
58 ut_asserteq(6, u16_strlen(c1));
59 ut_asserteq(8, u16_strlen(c2));
60 ut_asserteq(3, u16_strlen(c3));
61 ut_asserteq(6, u16_strlen(c4));
62 return 0;
63}
64UNICODE_TEST(unicode_test_u16_strlen);
65
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +010066static int unicode_test_u16_strdup(struct unit_test_state *uts)
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010067{
68 u16 *copy = u16_strdup(c4);
69
70 ut_assert(copy != c4);
Simon Glassa3186e62020-05-10 12:52:45 -060071 ut_asserteq_mem(copy, c4, sizeof(c4));
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010072 free(copy);
Simon Glassa3186e62020-05-10 12:52:45 -060073
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010074 return 0;
75}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +010076UNICODE_TEST(unicode_test_u16_strdup);
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010077
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +010078static int unicode_test_u16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010079{
80 u16 *r;
81 u16 copy[10];
82
83 r = u16_strcpy(copy, c1);
84 ut_assert(r == copy);
Simon Glassa3186e62020-05-10 12:52:45 -060085 ut_asserteq_mem(copy, c1, sizeof(c1));
86
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010087 return 0;
88}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +010089UNICODE_TEST(unicode_test_u16_strcpy);
Heinrich Schuchardt45fceca2018-12-14 22:00:37 +010090
Heinrich Schuchardt485599d2018-08-31 21:31:30 +020091/* U-Boot uses UTF-16 strings in the EFI context only. */
92#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +010093static int unicode_test_string16(struct unit_test_state *uts)
Heinrich Schuchardt485599d2018-08-31 21:31:30 +020094{
95 char buf[20];
96
97 /* Test length and precision */
98 memset(buf, 0xff, sizeof(buf));
99 sprintf(buf, "%8.6ls", c2);
100 ut_asserteq(' ', buf[1]);
101 ut_assert(!strncmp(&buf[2], d2, 7));
102 ut_assert(!buf[9]);
103
104 memset(buf, 0xff, sizeof(buf));
105 sprintf(buf, "%8.6ls", c4);
106 ut_asserteq(' ', buf[4]);
107 ut_assert(!strncmp(&buf[5], d4, 12));
108 ut_assert(!buf[17]);
109
110 memset(buf, 0xff, sizeof(buf));
111 sprintf(buf, "%-8.2ls", c4);
112 ut_asserteq(' ', buf[8]);
113 ut_assert(!strncmp(buf, d4, 8));
114 ut_assert(!buf[14]);
115
116 /* Test handling of illegal utf-16 sequences */
117 memset(buf, 0xff, sizeof(buf));
118 sprintf(buf, "%ls", i1);
119 ut_asserteq_str("i1?l", buf);
120
121 memset(buf, 0xff, sizeof(buf));
122 sprintf(buf, "%ls", i2);
123 ut_asserteq_str("i2?l", buf);
124
125 memset(buf, 0xff, sizeof(buf));
126 sprintf(buf, "%ls", i3);
127 ut_asserteq_str("i3?", buf);
128
129 return 0;
130}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100131UNICODE_TEST(unicode_test_string16);
Heinrich Schuchardt485599d2018-08-31 21:31:30 +0200132#endif
133
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100134static int unicode_test_utf8_get(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200135{
136 const char *s;
137 s32 code;
138 int i;
139
140 /* Check characters less than 0x800 */
141 s = d2;
142 for (i = 0; i < 8; ++i) {
143 code = utf8_get((const char **)&s);
144 /* c2 is the utf-8 encoding of d2 */
145 ut_asserteq(c2[i], code);
146 if (!code)
147 break;
148 }
149 ut_asserteq_ptr(s, d2 + 9)
150
151 /* Check characters less than 0x10000 */
152 s = d3;
153 for (i = 0; i < 4; ++i) {
154 code = utf8_get((const char **)&s);
155 /* c3 is the utf-8 encoding of d3 */
156 ut_asserteq(c3[i], code);
157 if (!code)
158 break;
159 }
160 ut_asserteq_ptr(s, d3 + 9)
161
162 /* Check character greater 0xffff */
163 s = d4;
164 code = utf8_get((const char **)&s);
165 ut_asserteq(0x0001048d, code);
166 ut_asserteq_ptr(s, d4 + 4);
167
168 return 0;
169}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100170UNICODE_TEST(unicode_test_utf8_get);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200171
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100172static int unicode_test_utf8_put(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200173{
174 char buffer[8] = { 0, };
175 char *pos;
176
177 /* Commercial at, translates to one character */
178 pos = buffer;
179 ut_assert(!utf8_put('@', &pos))
180 ut_asserteq(1, pos - buffer);
181 ut_asserteq('@', buffer[0]);
182 ut_assert(!buffer[1]);
183
184 /* Latin letter G with acute, translates to two charactes */
185 pos = buffer;
186 ut_assert(!utf8_put(0x1f4, &pos));
187 ut_asserteq(2, pos - buffer);
188 ut_asserteq_str("\xc7\xb4", buffer);
189
190 /* Tagalog letter i, translates to three characters */
191 pos = buffer;
192 ut_assert(!utf8_put(0x1701, &pos));
193 ut_asserteq(3, pos - buffer);
194 ut_asserteq_str("\xe1\x9c\x81", buffer);
195
196 /* Hamster face, translates to four characters */
197 pos = buffer;
198 ut_assert(!utf8_put(0x1f439, &pos));
199 ut_asserteq(4, pos - buffer);
200 ut_asserteq_str("\xf0\x9f\x90\xb9", buffer);
201
202 /* Illegal code */
203 pos = buffer;
204 ut_asserteq(-1, utf8_put(0xd888, &pos));
205
206 return 0;
207}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100208UNICODE_TEST(unicode_test_utf8_put);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200209
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100210static int unicode_test_utf8_utf16_strlen(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200211{
212 ut_asserteq(6, utf8_utf16_strlen(d1));
213 ut_asserteq(8, utf8_utf16_strlen(d2));
214 ut_asserteq(3, utf8_utf16_strlen(d3));
215 ut_asserteq(6, utf8_utf16_strlen(d4));
216
217 /* illegal utf-8 sequences */
218 ut_asserteq(4, utf8_utf16_strlen(j1));
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200219 ut_asserteq(4, utf8_utf16_strlen(j2));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200220 ut_asserteq(3, utf8_utf16_strlen(j3));
221
222 return 0;
223}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100224UNICODE_TEST(unicode_test_utf8_utf16_strlen);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200225
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100226static int unicode_test_utf8_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200227{
228 ut_asserteq(3, utf8_utf16_strnlen(d1, 3));
229 ut_asserteq(6, utf8_utf16_strnlen(d1, 13));
230 ut_asserteq(6, utf8_utf16_strnlen(d2, 6));
231 ut_asserteq(2, utf8_utf16_strnlen(d3, 2));
232 ut_asserteq(4, utf8_utf16_strnlen(d4, 2));
233 ut_asserteq(6, utf8_utf16_strnlen(d4, 3));
234
235 /* illegal utf-8 sequences */
236 ut_asserteq(4, utf8_utf16_strnlen(j1, 16));
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200237 ut_asserteq(4, utf8_utf16_strnlen(j2, 16));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200238 ut_asserteq(3, utf8_utf16_strnlen(j3, 16));
239
240 return 0;
241}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100242UNICODE_TEST(unicode_test_utf8_utf16_strnlen);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200243
244/**
245 * ut_u16_strcmp() - Compare to u16 strings.
246 *
247 * @a1: first string
248 * @a2: second string
249 * @count: number of u16 to compare
250 * Return: -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
251 */
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100252static int unicode_test_u16_strcmp(const u16 *a1, const u16 *a2, size_t count)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200253{
254 for (; (*a1 || *a2) && count; ++a1, ++a2, --count) {
255 if (*a1 < *a2)
256 return -1;
257 if (*a1 > *a2)
258 return 1;
259 }
260 return 0;
261}
262
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100263static int unicode_test_utf8_utf16_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200264{
265 u16 buf[16];
266 u16 *pos;
267
268 pos = buf;
269 utf8_utf16_strcpy(&pos, d1);
270 ut_asserteq(6, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100271 ut_assert(!unicode_test_u16_strcmp(buf, c1, SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200272
273 pos = buf;
274 utf8_utf16_strcpy(&pos, d2);
275 ut_asserteq(8, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100276 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200277
278 pos = buf;
279 utf8_utf16_strcpy(&pos, d3);
280 ut_asserteq(3, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100281 ut_assert(!unicode_test_u16_strcmp(buf, c3, SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200282
283 pos = buf;
284 utf8_utf16_strcpy(&pos, d4);
285 ut_asserteq(6, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100286 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200287
288 /* Illegal utf-8 strings */
289 pos = buf;
290 utf8_utf16_strcpy(&pos, j1);
291 ut_asserteq(4, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100292 ut_assert(!unicode_test_u16_strcmp(buf, L"j1?l", SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200293
294 pos = buf;
295 utf8_utf16_strcpy(&pos, j2);
Heinrich Schuchardtfc5f1a12018-09-12 00:05:32 +0200296 ut_asserteq(4, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100297 ut_assert(!unicode_test_u16_strcmp(buf, L"j2?l", SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200298
299 pos = buf;
300 utf8_utf16_strcpy(&pos, j3);
301 ut_asserteq(3, pos - buf);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100302 ut_assert(!unicode_test_u16_strcmp(buf, L"j3?", SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200303
304 return 0;
305}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100306UNICODE_TEST(unicode_test_utf8_utf16_strcpy);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200307
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100308static int unicode_test_utf8_utf16_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200309{
310 u16 buf[16];
311 u16 *pos;
312
313 pos = buf;
314 memset(buf, 0, sizeof(buf));
315 utf8_utf16_strncpy(&pos, d1, 4);
316 ut_asserteq(4, pos - buf);
317 ut_assert(!buf[4]);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100318 ut_assert(!unicode_test_u16_strcmp(buf, c1, 4));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200319
320 pos = buf;
321 memset(buf, 0, sizeof(buf));
322 utf8_utf16_strncpy(&pos, d2, 10);
323 ut_asserteq(8, pos - buf);
324 ut_assert(buf[4]);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100325 ut_assert(!unicode_test_u16_strcmp(buf, c2, SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200326
327 pos = buf;
328 memset(buf, 0, sizeof(buf));
329 utf8_utf16_strncpy(&pos, d3, 2);
330 ut_asserteq(2, pos - buf);
331 ut_assert(!buf[2]);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100332 ut_assert(!unicode_test_u16_strcmp(buf, c3, 2));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200333
334 pos = buf;
335 memset(buf, 0, sizeof(buf));
336 utf8_utf16_strncpy(&pos, d4, 2);
337 ut_asserteq(4, pos - buf);
338 ut_assert(!buf[4]);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100339 ut_assert(!unicode_test_u16_strcmp(buf, c4, 4));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200340
341 pos = buf;
342 memset(buf, 0, sizeof(buf));
343 utf8_utf16_strncpy(&pos, d4, 10);
344 ut_asserteq(6, pos - buf);
345 ut_assert(buf[5]);
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100346 ut_assert(!unicode_test_u16_strcmp(buf, c4, SIZE_MAX));
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200347
348 return 0;
349}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100350UNICODE_TEST(unicode_test_utf8_utf16_strncpy);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200351
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100352static int unicode_test_utf16_get(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200353{
354 const u16 *s;
355 s32 code;
356 int i;
357
358 /* Check characters less than 0x10000 */
359 s = c2;
360 for (i = 0; i < 9; ++i) {
361 code = utf16_get((const u16 **)&s);
362 ut_asserteq(c2[i], code);
363 if (!code)
364 break;
365 }
366 ut_asserteq_ptr(c2 + 8, s);
367
368 /* Check character greater 0xffff */
369 s = c4;
370 code = utf16_get((const u16 **)&s);
371 ut_asserteq(0x0001048d, code);
372 ut_asserteq_ptr(c4 + 2, s);
373
374 return 0;
375}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100376UNICODE_TEST(unicode_test_utf16_get);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200377
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100378static int unicode_test_utf16_put(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200379{
380 u16 buffer[4] = { 0, };
381 u16 *pos;
382
383 /* Commercial at, translates to one word */
384 pos = buffer;
385 ut_assert(!utf16_put('@', &pos));
386 ut_asserteq(1, pos - buffer);
387 ut_asserteq((u16)'@', buffer[0]);
388 ut_assert(!buffer[1]);
389
390 /* Hamster face, translates to two words */
391 pos = buffer;
392 ut_assert(!utf16_put(0x1f439, &pos));
393 ut_asserteq(2, pos - buffer);
394 ut_asserteq((u16)0xd83d, buffer[0]);
395 ut_asserteq((u16)0xdc39, buffer[1]);
396 ut_assert(!buffer[2]);
397
398 /* Illegal code */
399 pos = buffer;
400 ut_asserteq(-1, utf16_put(0xd888, &pos));
401
402 return 0;
403}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100404UNICODE_TEST(unicode_test_utf16_put);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200405
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100406static int unicode_test_utf16_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200407{
408 ut_asserteq(3, utf16_strnlen(c1, 3));
409 ut_asserteq(6, utf16_strnlen(c1, 13));
410 ut_asserteq(6, utf16_strnlen(c2, 6));
411 ut_asserteq(2, utf16_strnlen(c3, 2));
412 ut_asserteq(2, utf16_strnlen(c4, 2));
413 ut_asserteq(3, utf16_strnlen(c4, 3));
414
415 /* illegal utf-16 word sequences */
416 ut_asserteq(4, utf16_strnlen(i1, 16));
417 ut_asserteq(4, utf16_strnlen(i2, 16));
418 ut_asserteq(3, utf16_strnlen(i3, 16));
419
420 return 0;
421}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100422UNICODE_TEST(unicode_test_utf16_strnlen);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200423
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100424static int unicode_test_utf16_utf8_strlen(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200425{
426 ut_asserteq(6, utf16_utf8_strlen(c1));
427 ut_asserteq(9, utf16_utf8_strlen(c2));
428 ut_asserteq(9, utf16_utf8_strlen(c3));
429 ut_asserteq(12, utf16_utf8_strlen(c4));
430
431 /* illegal utf-16 word sequences */
432 ut_asserteq(4, utf16_utf8_strlen(i1));
433 ut_asserteq(4, utf16_utf8_strlen(i2));
434 ut_asserteq(3, utf16_utf8_strlen(i3));
435
436 return 0;
437}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100438UNICODE_TEST(unicode_test_utf16_utf8_strlen);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200439
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100440static int unicode_test_utf16_utf8_strnlen(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200441{
442 ut_asserteq(3, utf16_utf8_strnlen(c1, 3));
443 ut_asserteq(6, utf16_utf8_strnlen(c1, 13));
444 ut_asserteq(7, utf16_utf8_strnlen(c2, 6));
445 ut_asserteq(6, utf16_utf8_strnlen(c3, 2));
446 ut_asserteq(8, utf16_utf8_strnlen(c4, 2));
447 ut_asserteq(12, utf16_utf8_strnlen(c4, 3));
448 return 0;
449}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100450UNICODE_TEST(unicode_test_utf16_utf8_strnlen);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200451
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100452static int unicode_test_utf16_utf8_strcpy(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200453{
454 char buf[16];
455 char *pos;
456
457 pos = buf;
458 utf16_utf8_strcpy(&pos, c1);
459 ut_asserteq(6, pos - buf);
460 ut_asserteq_str(d1, buf);
461
462 pos = buf;
463 utf16_utf8_strcpy(&pos, c2);
464 ut_asserteq(9, pos - buf);
465 ut_asserteq_str(d2, buf);
466
467 pos = buf;
468 utf16_utf8_strcpy(&pos, c3);
469 ut_asserteq(9, pos - buf);
470 ut_asserteq_str(d3, buf);
471
472 pos = buf;
473 utf16_utf8_strcpy(&pos, c4);
474 ut_asserteq(12, pos - buf);
475 ut_asserteq_str(d4, buf);
476
477 /* Illegal utf-16 strings */
478 pos = buf;
479 utf16_utf8_strcpy(&pos, i1);
480 ut_asserteq(4, pos - buf);
481 ut_asserteq_str("i1?l", buf);
482
483 pos = buf;
484 utf16_utf8_strcpy(&pos, i2);
485 ut_asserteq(4, pos - buf);
486 ut_asserteq_str("i2?l", buf);
487
488 pos = buf;
489 utf16_utf8_strcpy(&pos, i3);
490 ut_asserteq(3, pos - buf);
491 ut_asserteq_str("i3?", buf);
492
493 return 0;
494}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100495UNICODE_TEST(unicode_test_utf16_utf8_strcpy);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200496
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100497static int unicode_test_utf16_utf8_strncpy(struct unit_test_state *uts)
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200498{
499 char buf[16];
500 char *pos;
501
502 pos = buf;
503 memset(buf, 0, sizeof(buf));
504 utf16_utf8_strncpy(&pos, c1, 4);
505 ut_asserteq(4, pos - buf);
506 ut_assert(!buf[4]);
507 ut_assert(!strncmp(buf, d1, 4));
508
509 pos = buf;
510 memset(buf, 0, sizeof(buf));
511 utf16_utf8_strncpy(&pos, c2, 10);
512 ut_asserteq(9, pos - buf);
513 ut_assert(buf[4]);
514 ut_assert(!strncmp(buf, d2, SIZE_MAX));
515
516 pos = buf;
517 memset(buf, 0, sizeof(buf));
518 utf16_utf8_strncpy(&pos, c3, 2);
519 ut_asserteq(6, pos - buf);
520 ut_assert(!buf[6]);
521 ut_assert(!strncmp(buf, d3, 6));
522
523 pos = buf;
524 memset(buf, 0, sizeof(buf));
525 utf16_utf8_strncpy(&pos, c4, 2);
526 ut_asserteq(8, pos - buf);
527 ut_assert(!buf[8]);
528 ut_assert(!strncmp(buf, d4, 8));
529
530 pos = buf;
531 memset(buf, 0, sizeof(buf));
532 utf16_utf8_strncpy(&pos, c4, 10);
533 ut_asserteq(12, pos - buf);
534 ut_assert(buf[5]);
535 ut_assert(!strncmp(buf, d4, SIZE_MAX));
536
537 return 0;
538}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100539UNICODE_TEST(unicode_test_utf16_utf8_strncpy);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200540
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100541static int unicode_test_utf_to_lower(struct unit_test_state *uts)
Heinrich Schuchardt15552f72018-09-04 19:34:57 +0200542{
543 ut_asserteq('@', utf_to_lower('@'));
544 ut_asserteq('a', utf_to_lower('A'));
545 ut_asserteq('z', utf_to_lower('Z'));
546 ut_asserteq('[', utf_to_lower('['));
547 ut_asserteq('m', utf_to_lower('m'));
548 /* Latin letter O with diaresis (umlaut) */
549 ut_asserteq(0x00f6, utf_to_lower(0x00d6));
550#ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
551 /* Cyrillic letter I*/
552 ut_asserteq(0x0438, utf_to_lower(0x0418));
553#endif
554 return 0;
555}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100556UNICODE_TEST(unicode_test_utf_to_lower);
Heinrich Schuchardt15552f72018-09-04 19:34:57 +0200557
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100558static int unicode_test_utf_to_upper(struct unit_test_state *uts)
Heinrich Schuchardt15552f72018-09-04 19:34:57 +0200559{
560 ut_asserteq('`', utf_to_upper('`'));
561 ut_asserteq('A', utf_to_upper('a'));
562 ut_asserteq('Z', utf_to_upper('z'));
563 ut_asserteq('{', utf_to_upper('{'));
564 ut_asserteq('M', utf_to_upper('M'));
565 /* Latin letter O with diaresis (umlaut) */
566 ut_asserteq(0x00d6, utf_to_upper(0x00f6));
567#ifdef CONFIG_EFI_UNICODE_CAPITALIZATION
568 /* Cyrillic letter I */
569 ut_asserteq(0x0418, utf_to_upper(0x0438));
570#endif
571 return 0;
572}
Heinrich Schuchardt46da93c2019-02-15 23:12:50 +0100573UNICODE_TEST(unicode_test_utf_to_upper);
Heinrich Schuchardt15552f72018-09-04 19:34:57 +0200574
AKASHI Takahiro0d1705e2019-09-18 10:26:30 +0900575static int unicode_test_u16_strncmp(struct unit_test_state *uts)
576{
577 ut_assert(u16_strncmp(L"abc", L"abc", 3) == 0);
578 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 3) == 0);
579 ut_assert(u16_strncmp(L"abcdef", L"abcghi", 6) < 0);
580 ut_assert(u16_strncmp(L"abcghi", L"abcdef", 6) > 0);
581 ut_assert(u16_strcmp(L"abc", L"abc") == 0);
582 ut_assert(u16_strcmp(L"abcdef", L"deghi") < 0);
583 ut_assert(u16_strcmp(L"deghi", L"abcdef") > 0);
584 return 0;
585}
586UNICODE_TEST(unicode_test_u16_strncmp);
587
Heinrich Schuchardt3bb3d862020-05-09 09:16:49 +0200588static int unicode_test_u16_strsize(struct unit_test_state *uts)
589{
590 ut_asserteq_64(u16_strsize(c1), 14);
591 ut_asserteq_64(u16_strsize(c2), 18);
592 ut_asserteq_64(u16_strsize(c3), 8);
593 ut_asserteq_64(u16_strsize(c4), 14);
594 return 0;
595}
596UNICODE_TEST(unicode_test_u16_strsize);
597
Heinrich Schuchardt31805542020-10-30 12:23:59 +0100598#ifdef CONFIG_EFI_LOADER
599static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
600{
601 u16 buf[16];
602 u16 const expected[] = L"Capsule0AF9";
603 u16 *pos;
604
605 memset(buf, 0xeb, sizeof(buf));
606 pos = efi_create_indexed_name(buf, "Capsule", 0x0af9);
607
608 ut_asserteq_mem(expected, buf, sizeof(expected));
609 ut_asserteq(pos - buf, u16_strnlen(buf, SIZE_MAX));
610
611 return 0;
612}
613UNICODE_TEST(unicode_test_efi_create_indexed_name);
614#endif
615
Simon Glassed38aef2020-05-10 11:40:03 -0600616int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200617{
618 struct unit_test *tests = ll_entry_start(struct unit_test, unicode_test);
619 const int n_ents = ll_entry_count(struct unit_test, unicode_test);
620
Philippe Reynes1f99f842019-12-17 19:07:04 +0100621 return cmd_ut_category("Unicode", "unicode_test_",
622 tests, n_ents, argc, argv);
Heinrich Schuchardtb8b6c812018-08-31 21:31:28 +0200623}